Skip to content

Commit

Permalink
Merge pull request #2110 from su2code/feature_signal_handling
Browse files Browse the repository at this point in the history
Signal handling - SIGTERM will stop calculation and force file saving
  • Loading branch information
pcarruscag authored Sep 15, 2024
2 parents 4f7a4c7 + cd80746 commit c918e04
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions SU2_CFD/src/drivers/CSinglezoneDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ void CSinglezoneDriver::StartSolver() {
TimeIter = config_container[ZONE_0]->GetRestart_Iter();

/*--- Run the problem until the number of time iterations required is reached. ---*/
while ( TimeIter < config_container[ZONE_0]->GetnTime_Iter() ) {

/*--- or until a SIGTERM signal stops the loop. We catch SIGTERM and exit gracefully ---*/
while ( TimeIter < config_container[ZONE_0]->GetnTime_Iter()) {
/*--- Perform some preprocessing before starting the time-step simulation. ---*/

Preprocess(TimeIter);
Expand Down
24 changes: 22 additions & 2 deletions SU2_CFD/src/output/COutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
* License along with SU2. If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <csignal>

#include "../../../Common/include/geometry/CGeometry.hpp"
#include "../../include/solvers/CSolver.hpp"

Expand All @@ -47,6 +50,15 @@
#include "../../include/output/filewriter/CSU2BinaryFileWriter.hpp"
#include "../../include/output/filewriter/CSU2MeshFileWriter.hpp"

namespace {
volatile sig_atomic_t STOP;

void signalHandler(int signum) {
std::cout << "Interrupt signal (" << signum << ") received, saving files and exiting.\n";
STOP = 1;
}
}

COutput::COutput(const CConfig *config, unsigned short ndim, bool fem_output):
rank(SU2_MPI::GetRank()),
size(SU2_MPI::GetSize()),
Expand Down Expand Up @@ -169,7 +181,11 @@ COutput::COutput(const CConfig *config, unsigned short ndim, bool fem_output):
volumeDataSorter = nullptr;
surfaceDataSorter = nullptr;

headerNeeded = false;
headerNeeded = false;

/*--- Setup a signal handler for SIGTERM. ---*/

signal(SIGTERM, signalHandler);
}

COutput::~COutput() {
Expand Down Expand Up @@ -233,7 +249,7 @@ void COutput::SetHistoryOutput(CGeometry ****geometry, CSolver *****solver, CCon

if (config[ZONE_0]->GetMultizone_Problem())
Iter = OuterIter;

/*--- Turbomachinery Performance Screen summary output---*/
if (Iter%100 == 0 && rank == MASTER_NODE) {
SetTurboPerformance_Output(TurboPerf, config[val_iZone], TimeIter, OuterIter, InnerIter);
Expand Down Expand Up @@ -946,9 +962,13 @@ bool COutput::ConvergenceMonitoring(CConfig *config, unsigned long Iteration) {

if (convFields.empty() || Iteration < config->GetStartConv_Iter()) convergence = false;

/*--- If a SIGTERM signal is sent to one of the processes, we set convergence to true. ---*/
if (STOP) convergence = true;

/*--- Apply the same convergence criteria to all processors. ---*/

unsigned short local = convergence, global = 0;

SU2_MPI::Allreduce(&local, &global, 1, MPI_UNSIGNED_SHORT, MPI_MAX, SU2_MPI::GetComm());
convergence = global > 0;

Expand Down

0 comments on commit c918e04

Please sign in to comment.