Handle SIGINT
This commit is contained in:
parent
59100a471f
commit
8bb80c0b0f
16
src/main.c
16
src/main.c
@ -3,6 +3,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <complex.h>
|
#include <complex.h>
|
||||||
#include <fftw3.h>
|
#include <fftw3.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -45,6 +46,9 @@ _Complex double* set_driving(unsigned int driving, nstrophy_parameters parameter
|
|||||||
// set initial condition
|
// set initial condition
|
||||||
_Complex double* set_init(unsigned int init, nstrophy_parameters parameters, FILE* initfile);
|
_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_UK 1
|
||||||
#define COMMAND_EEA 2
|
#define COMMAND_EEA 2
|
||||||
#define COMMAND_QUIET 3
|
#define COMMAND_QUIET 3
|
||||||
@ -57,6 +61,16 @@ _Complex double* set_init(unsigned int init, nstrophy_parameters parameters, FIL
|
|||||||
#define INIT_FILE 3
|
#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 main (
|
||||||
int argc,
|
int argc,
|
||||||
const char* argv[]
|
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);
|
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){
|
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);
|
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){
|
else if(command==COMMAND_QUIET){
|
||||||
|
@ -230,8 +230,20 @@ 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);
|
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);
|
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){
|
if(running_avg_window!=0){
|
||||||
free(save_print_e);
|
free(save_print_e);
|
||||||
free(save_print_a);
|
free(save_print_a);
|
||||||
@ -241,9 +253,6 @@ int eea(
|
|||||||
free(save_print_short_en);
|
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);
|
ns_free_tmps(u, tmp1, tmp2, tmp3, fft1, fft2, ifft);
|
||||||
return(0);
|
return(0);
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,9 @@
|
|||||||
|
|
||||||
#define M_PI 3.14159265358979323846
|
#define M_PI 3.14159265358979323846
|
||||||
|
|
||||||
|
// abort signal
|
||||||
|
extern volatile bool g_abort;
|
||||||
|
|
||||||
// arrays on which the ffts are performed
|
// arrays on which the ffts are performed
|
||||||
typedef struct fft_vects {
|
typedef struct fft_vects {
|
||||||
fftw_complex* fft;
|
fftw_complex* fft;
|
||||||
|
Loading…
Reference in New Issue
Block a user