Skip to content
Snippets Groups Projects
Commit 5729c111 authored by Elias Pipping's avatar Elias Pipping
Browse files

[Extend] Handle SIGINT, SIGTERM, and SIGXCPU

If either signal is detected, we try to terminate gracefully. If we're
given enough time before a nastier signal like SIGKILL is sent, we'll
then be able to finish computing and writing out the current timestep.

If we do not take such precautions, chances are the HDF5 output will be
corrupted.
parent d10e929c
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
#undef HAVE_IPOPT #undef HAVE_IPOPT
#endif #endif
#include <atomic>
#include <cmath> #include <cmath>
#include <csignal>
#include <exception> #include <exception>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
...@@ -71,6 +73,9 @@ Dune::ParameterTree getParameters(int argc, char *argv[]) { ...@@ -71,6 +73,9 @@ Dune::ParameterTree getParameters(int argc, char *argv[]) {
return parset; return parset;
} }
static std::atomic<bool> terminationRequested(false);
void handleSignal(int signum) { terminationRequested = true; }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
try { try {
Dune::MPIHelper::instance(argc, argv); Dune::MPIHelper::instance(argc, argv);
...@@ -268,6 +273,10 @@ int main(int argc, char *argv[]) { ...@@ -268,6 +273,10 @@ int main(int argc, char *argv[]) {
return stateEnergyNorm.diff(fineAlpha, coarseAlpha) > refinementTolerance; return stateEnergyNorm.diff(fineAlpha, coarseAlpha) > refinementTolerance;
}; };
std::signal(SIGXCPU, handleSignal);
std::signal(SIGINT, handleSignal);
std::signal(SIGTERM, handleSignal);
AdaptiveTimeStepper<NonlinearFactory, MyUpdater, AdaptiveTimeStepper<NonlinearFactory, MyUpdater,
EnergyNorm<ScalarMatrix, ScalarVector>> EnergyNorm<ScalarMatrix, ScalarVector>>
adaptiveTimeStepper(factory, parset, myGlobalFriction, current, adaptiveTimeStepper(factory, parset, myGlobalFriction, current,
...@@ -286,6 +295,11 @@ int main(int argc, char *argv[]) { ...@@ -286,6 +295,11 @@ int main(int argc, char *argv[]) {
current.state_->extractAlpha(programState.alpha); current.state_->extractAlpha(programState.alpha);
report(); report();
if (terminationRequested) {
std::cerr << "Terminating prematurely" << std::endl;
break;
}
} }
} catch (Dune::Exception &e) { } catch (Dune::Exception &e) {
Dune::derr << "Dune reported error: " << e << std::endl; Dune::derr << "Dune reported error: " << e << std::endl;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment