Skip to content

Commit

Permalink
add option for saving on interruption
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienDoerner committed Aug 28, 2024
1 parent beee8ad commit 3608993
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions include/crpropa/ModuleList.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,15 @@ class ModuleList: public Module {
iterator end();
const_iterator end() const;

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

private:
module_list_t modules;
bool showProgress;
Module* interruptAction;
bool haveInterruptAction = false;
std::vector<int> notFinished; // list with not finished numbers of candidates
};

/**
Expand Down
43 changes: 39 additions & 4 deletions src/ModuleList.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ void ModuleList::run(Candidate* candidate, bool recursive, bool secondariesFirst
run(candidate->secondaries[i], recursive, secondariesFirst);
}
}

// dump candidae and secondaries if interrupted.
if (candidate->isActive() && (g_cancel_signal_flag != 0))
dumpCandidate(candidate);
}

void ModuleList::run(ref_ptr<Candidate> candidate, bool recursive, bool secondariesFirst) {
Expand Down Expand Up @@ -114,8 +118,10 @@ 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)
if (g_cancel_signal_flag != 0) {
notFinished.push_back(i);
continue;
}

try {
run(candidates->operator[](i), recursive);
Expand All @@ -132,8 +138,14 @@ void ModuleList::run(const candidate_vector_t *candidates, bool recursive, bool
::signal(SIGINT, old_sigint_handler);
::signal(SIGTERM, old_sigterm_handler);
// Propagate signal to old handler.
if (g_cancel_signal_flag > 0)
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";
}
}

void ModuleList::run(SourceInterface *source, size_t count, bool recursive, bool secondariesFirst) {
Expand All @@ -156,8 +168,10 @@ 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)
if (g_cancel_signal_flag !=0) {
notFinished.push_back(i);
continue;
}

ref_ptr<Candidate> candidate;

Expand Down Expand Up @@ -189,8 +203,10 @@ void ModuleList::run(SourceInterface *source, size_t count, bool recursive, bool
::signal(SIGINT, old_signal_handler);
::signal(SIGTERM, old_sigterm_handler);
// Propagate signal to old handler.
if (g_cancel_signal_flag > 0)
if (g_cancel_signal_flag > 0) {
raise(g_cancel_signal_flag);
std::cerr << "Number of not started candidates from source: " << notFinished.size() << "\n";
}
}

ModuleList::iterator ModuleList::begin() {
Expand Down Expand Up @@ -222,6 +238,25 @@ void ModuleList::showModules() const {
std::cout << getDescription();
}

void ModuleList::setInterruptAction(Module* 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";
return;
}
if (haveInterruptAction) {
interruptAction -> process(cand);
for (int i = 0; i < cand -> secondaries.size(); i++) {
if (cand -> secondaries[i] -> isActive())
dumpCandidate(cand -> secondaries[i]);
}
}
}

ModuleListRunner::ModuleListRunner(ModuleList *mlist) : mlist(mlist) {
}

Expand Down

0 comments on commit 3608993

Please sign in to comment.