Handle SIGINT

This commit is contained in:
Ian Jauslin 2023-04-12 15:23:35 -04:00
parent 59100a471f
commit 8bb80c0b0f
3 changed files with 31 additions and 3 deletions

View File

@ -3,6 +3,7 @@
#include <math.h>
#include <complex.h>
#include <fftw3.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
@ -45,6 +46,9 @@ _Complex double* set_driving(unsigned int driving, nstrophy_parameters parameter
// set initial condition
_Complex double* set_init(unsigned int init, nstrophy_parameters parameters, FILE* initfile);
// signal handler
void sig_handler (int signo);
#define COMMAND_UK 1
#define COMMAND_EEA 2
#define COMMAND_QUIET 3
@ -57,6 +61,16 @@ _Complex double* set_init(unsigned int init, nstrophy_parameters parameters, FIL
#define INIT_FILE 3
// global variable to handle interrupts
volatile bool g_abort = false;
// signal handler
void sig_handler (int signo){
if (signo == SIGINT){
g_abort = true;
}
}
int main (
int argc,
const char* argv[]
@ -126,6 +140,8 @@ int main (
uk(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, parameters.print_freq, parameters.starting_time, nthreads, savefile);
}
else if(command==COMMAND_EEA){
// register signal handler to handle aborts
signal(SIGINT, sig_handler);
eea(parameters.K1, parameters.K2, parameters.N1, parameters.N2, parameters.nsteps, parameters.nu, parameters.delta, parameters.L, u0, g, parameters.irreversible, parameters.print_freq, parameters.avg_window, parameters.starting_time, nthreads, savefile);
}
else if(command==COMMAND_QUIET){

View File

@ -230,7 +230,19 @@ int eea(
fprintf(stderr,"%d % .8e % .8e % .8e % .8e % .8e % .8e % .8e\n",t,t*delta, avg_a, avg_e, avg_en, alpha, energy, enstrophy);
printf("%8d % .15e % .15e % .15e % .15e % .15e % .15e % .15e\n",t,t*delta, avg_a, avg_e, avg_en, alpha, energy, enstrophy);
}
// catch abort signal
if (g_abort){
// print u to stderr if no savefile
if (savefile==NULL){
savefile=stderr;
}
break;
}
}
// save final entry to savefile
write_u(u, K1, K2, savefile);
if(running_avg_window!=0){
free(save_print_e);
@ -241,9 +253,6 @@ int eea(
free(save_print_short_en);
}
// save final entry to savefile
write_u(u, K1, K2, savefile);
ns_free_tmps(u, tmp1, tmp2, tmp3, fft1, fft2, ifft);
return(0);
}

View File

@ -7,6 +7,9 @@
#define M_PI 3.14159265358979323846
// abort signal
extern volatile bool g_abort;
// arrays on which the ffts are performed
typedef struct fft_vects {
fftw_complex* fft;