Skip to content

Commit

Permalink
change interrput action to output type
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienDoerner committed Sep 5, 2024
1 parent 3608993 commit bb34f63
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 23 deletions.
5 changes: 3 additions & 2 deletions include/crpropa/ModuleList.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "crpropa/Candidate.h"
#include "crpropa/Module.h"
#include "crpropa/Source.h"
#include "crpropa/module/Output.h"

#include <list>
#include <sstream>
Expand Down Expand Up @@ -47,13 +48,13 @@ class ModuleList: public Module {
iterator end();
const_iterator end() const;

void setInterruptAction(Module* action);
void setInterruptAction(Output* action);
void dumpCandidate(Candidate* cand) const;

private:
module_list_t modules;
bool showProgress;
Module* interruptAction;
Output* interruptAction;
bool haveInterruptAction = false;
std::vector<int> notFinished; // list with not finished numbers of candidates
};
Expand Down
22 changes: 18 additions & 4 deletions include/crpropa/module/Output.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ namespace crpropa {
They can be easily customised by enabling/disabling specific columns.
*/
class Output: public Module {
protected:
double lengthScale, energyScale;
std::bitset<64> fields;

public:
struct Property
{
std::string name;
std::string comment;
Variant defaultValue;
};

protected:
double lengthScale, energyScale;
std::bitset<64> fields;

std::vector<Property> properties;

bool oneDimensional;
Expand Down Expand Up @@ -163,6 +165,18 @@ class Output: public Module {
size_t size() const;

void process(Candidate *) const;

/**
* write the indices of not started candidates into the output file.
* Used for interrupting the simulation
* @param indices list of not started indices
*/
virtual void dumpIndexList(std::vector<int> indices) {
std::cout << "indices:\t";
for (int i = 0; i < indices.size(); i++)
std::cout << indices[i] << ", ";
std::cout << "\n";
};
};

/** @}*/
Expand Down
7 changes: 5 additions & 2 deletions python/2_headers.i
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@
%feature("director") crpropa::AbstractCondition;
%include "crpropa/Module.h"

%template(OutputRefPtr) crpropa::ref_ptr<Output>;
%feature("director") crpropa::Output;
%ignore crpropa::Output::dumpIndexList(std::vector<int>);
%include "crpropa/module/Output.h"

%implicitconv crpropa::ref_ptr<crpropa::MagneticField>;
%template(MagneticFieldRefPtr) crpropa::ref_ptr<crpropa::MagneticField>;
%feature("director") crpropa::MagneticField;
Expand Down Expand Up @@ -394,8 +399,6 @@
}
}


%include "crpropa/module/Output.h"
%include "crpropa/module/DiffusionSDE.h"
%include "crpropa/module/TextOutput.h"
%include "crpropa/module/HDF5Output.h"
Expand Down
41 changes: 26 additions & 15 deletions src/ModuleList.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <algorithm>
#include <csignal>
#include <bits/stdc++.h>
#ifndef sighandler_t
typedef void (*sighandler_t)(int);
#endif
Expand Down Expand Up @@ -119,6 +120,7 @@ void ModuleList::run(const candidate_vector_t *candidates, bool recursive, bool
#pragma omp parallel for schedule(OMP_SCHEDULE)
for (size_t i = 0; i < count; i++) {
if (g_cancel_signal_flag != 0) {
#pragma omp critical(interrupt_write)
notFinished.push_back(i);
continue;
}
Expand All @@ -140,11 +142,15 @@ void ModuleList::run(const candidate_vector_t *candidates, bool recursive, bool
// Propagate signal to old handler.
if (g_cancel_signal_flag > 0) {
raise(g_cancel_signal_flag);
std::cerr << "in total " << notFinished.size() << " Candidates have not been started.\n";
std::cerr << "this containes the following numbers from the CandidateVector: \n";
for (size_t i = 0; i < notFinished.size(); i++)
std::cerr << notFinished[i] << ", ";
std::cerr << "\n";
std::cerr << "############################################################################\n";
std::cerr << "# Interrupted CRPropa simulation \n";
std::cerr << "# in total " << notFinished.size() << " Candidates have not been started.\n";
std::cerr << "# the indicies of the vector haven been written to to output file. \n";
std::cerr << "############################################################################\n";

// dump list to output file
std::sort(notFinished.begin(), notFinished.end());
interruptAction -> dumpIndexList(notFinished);
}
}

Expand All @@ -169,6 +175,7 @@ void ModuleList::run(SourceInterface *source, size_t count, bool recursive, bool
#pragma omp parallel for schedule(OMP_SCHEDULE)
for (size_t i = 0; i < count; i++) {
if (g_cancel_signal_flag !=0) {
#pragma omp critical(interrupt_write)
notFinished.push_back(i);
continue;
}
Expand Down Expand Up @@ -205,7 +212,10 @@ void ModuleList::run(SourceInterface *source, size_t count, bool recursive, bool
// Propagate signal to old handler.
if (g_cancel_signal_flag > 0) {
raise(g_cancel_signal_flag);
std::cerr << "Number of not started candidates from source: " << notFinished.size() << "\n";
std::cerr << "############################################################################\n";
std::cerr << "# Interrupted CRPropa simulation \n";
std::cerr << "# Number of not started candidates from source: " << notFinished.size() << "\n";
std::cerr << "############################################################################\n";
}
}

Expand Down Expand Up @@ -238,22 +248,23 @@ void ModuleList::showModules() const {
std::cout << getDescription();
}

void ModuleList::setInterruptAction(Module* action) {
void ModuleList::setInterruptAction(Output* action) {
interruptAction = action;
haveInterruptAction = true;
}

void ModuleList::dumpCandidate(Candidate *cand) const {
if (! cand -> isActive()) {
KISS_LOG_WARNING << "Try to dump a candidate which is not active anymore! \n";
if (!haveInterruptAction)
return;
}
if (haveInterruptAction) {

if (cand -> isActive())
interruptAction -> process(cand);
for (int i = 0; i < cand -> secondaries.size(); i++) {
if (cand -> secondaries[i] -> isActive())
dumpCandidate(cand -> secondaries[i]);
}
else
KISS_LOG_WARNING << "Try to dump a candidate which is not active anymore! \n";

for (int i = 0; i < cand -> secondaries.size(); i++) {
if (cand -> secondaries[i])
dumpCandidate(cand -> secondaries[i]);
}
}

Expand Down

0 comments on commit bb34f63

Please sign in to comment.