-
Notifications
You must be signed in to change notification settings - Fork 72
Handling of interruptions #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
3608993
bb34f63
ee91511
db8beea
1730399
2b7a4d1
0b2bd13
d9cca5b
c8c5508
ea66e0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Interrupting simulations on runtime | ||
------------------------------------------------ | ||
|
||
CRPropa simulations can be interrupted on runtime with the `SIGTERM` or `SIGINT` signals. | ||
If the user defines an output for the interruption (called `InterruptAction`) all candidates which are currently in the simulation will be passed to this output. | ||
In the error stream the user will see a message denoting the number of candidates which have not been started yet. | ||
If the simulation was run with a `candidateVector` as source, the indices of the candidates which have not been started yet will be printed or written to the file. | ||
For a simulation with a source interface, a restart with the missing number of candidates will be sufficient to continue the simulation. | ||
|
||
.. toctree:: | ||
:caption: Using a candidateVector as source | ||
:maxdepth: 1 | ||
|
||
example_notebooks/interrupting_simulations/interrupt_candidateVector.ipynb | ||
|
||
.. toctree:: | ||
:caption: Using a source interface | ||
:maxdepth: 1 | ||
|
||
example_notebooks/interrupting_simulations/interrupt_source.ipynb | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
|
||
#include <algorithm> | ||
#include <csignal> | ||
#include <bits/stdc++.h> | ||
#ifndef sighandler_t | ||
typedef void (*sighandler_t)(int); | ||
#endif | ||
|
@@ -87,6 +88,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) { | ||
|
@@ -114,8 +119,11 @@ 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) { | ||
#pragma omp critical(interrupt_write) | ||
notFinished.push_back(i); | ||
continue; | ||
} | ||
|
||
try { | ||
run(candidates->operator[](i), recursive); | ||
|
@@ -132,8 +140,18 @@ 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 << "############################################################################\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"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: "indices" |
||
std::cerr << "############################################################################\n"; | ||
|
||
// dump list to output file | ||
std::sort(notFinished.begin(), notFinished.end()); | ||
interruptAction -> dumpIndexList(notFinished); | ||
} | ||
} | ||
|
||
void ModuleList::run(SourceInterface *source, size_t count, bool recursive, bool secondariesFirst) { | ||
|
@@ -156,8 +174,11 @@ 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) { | ||
#pragma omp critical(interrupt_write) | ||
notFinished.push_back(i); | ||
continue; | ||
} | ||
|
||
ref_ptr<Candidate> candidate; | ||
|
||
|
@@ -189,8 +210,13 @@ 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 << "############################################################################\n"; | ||
std::cerr << "# Interrupted CRPropa simulation \n"; | ||
std::cerr << "# Number of not started candidates from source: " << notFinished.size() << "\n"; | ||
std::cerr << "############################################################################\n"; | ||
} | ||
} | ||
|
||
ModuleList::iterator ModuleList::begin() { | ||
|
@@ -222,6 +248,26 @@ void ModuleList::showModules() const { | |
std::cout << getDescription(); | ||
} | ||
|
||
void ModuleList::setInterruptAction(Output* action) { | ||
interruptAction = action; | ||
haveInterruptAction = true; | ||
} | ||
|
||
void ModuleList::dumpCandidate(Candidate *cand) const { | ||
if (!haveInterruptAction) | ||
return; | ||
|
||
if (cand -> isActive()) | ||
JulienDoerner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
interruptAction -> process(cand); | ||
else | ||
KISS_LOG_WARNING << "Try to dump a candidate which is not active anymore! \n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve message. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to improve the message. It contains the function which is raising the error and the the serial number of the candidate to trace down the reason for this. |
||
|
||
for (int i = 0; i < cand -> secondaries.size(); i++) { | ||
if (cand -> secondaries[i]) | ||
JulienDoerner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dumpCandidate(cand -> secondaries[i]); | ||
JulienDoerner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
ModuleListRunner::ModuleListRunner(ModuleList *mlist) : mlist(mlist) { | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.