-
Notifications
You must be signed in to change notification settings - Fork 69
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
Handling of interruptions #503
Open
JulienDoerner
wants to merge
9
commits into
CRPropa:master
Choose a base branch
from
JulienDoerner:Interrupt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3608993
add option for saving on interruption
JulienDoerner bb34f63
change interrput action to output type
JulienDoerner ee91511
add dumping of index list
JulienDoerner db8beea
add example for interrupted simulations
JulienDoerner 1730399
add more details in the interrupt examle
JulienDoerner 2b7a4d1
update depth
JulienDoerner 0b2bd13
exclude interrupt examples from testing
JulienDoerner d9cca5b
fix whitespace
JulienDoerner c8c5508
apply code convention
JulienDoerner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
396 changes: 396 additions & 0 deletions
396
doc/pages/example_notebooks/interrupting_simulations/interrupt_candidateVector.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
355 changes: 355 additions & 0 deletions
355
doc/pages/example_notebooks/interrupting_simulations/interrupt_source.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 << "# A total of " << 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); | ||
} | ||
} | ||
|
||
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,27 @@ 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()) | ||
interruptAction->process(cand); | ||
else | ||
KISS_LOG_WARNING << "ModuleList::dumpCandidate is called with a non active candidate. This should not happen for the interrupt action. Please check candidate with serieal number " | ||
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: serial number. |
||
<< cand->getSerialNumber() << std::endl; | ||
|
||
for (int i = 0; i < cand->secondaries.size(); i++) { | ||
if (cand->secondaries[i]) | ||
dumpCandidate(cand->secondaries[i]); | ||
} | ||
} | ||
|
||
ModuleListRunner::ModuleListRunner(ModuleList *mlist) : mlist(mlist) { | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: "indices"