NEURON
nrntimeout.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #include <nrnmpi.h>
3 
4 
5 #if NRNMPI
7 
8 #if defined(HAVE_SETITIMER) && defined(HAVE_SIGACTION)
9 
10 #include <stdio.h>
11 #include <signal.h>
12 #include <sys/time.h>
13 #include <section.h>
14 
15 static double told;
16 static struct itimerval value;
17 static struct sigaction act, oact;
18 
19 #define NRNTIMEOUT_DEBUG 0
20 #if NRNTIMEOUT_DEBUG
21 extern double nrn_time();
22 static old_nrn_time;
23 #endif
24 
25 static void timed_out(int sig) {
26 #if NRNTIMEOUT_DEBUG
27 double z = nrn_time();
28 printf("timed_out(%d) wall_elapse=%g told=%g t=%g\n", sig, z-old_nrn_time, told, nrn_threads->_t);
29 old_nrn_time = z;
30 #endif
31  if (nrn_threads->_t == told) { /* nothing has been accomplished since last signal*/
32  printf("nrn_timeout t=%g\n", nrn_threads->_t);
33  if (nrntimeout_call) {
34  (*nrntimeout_call)();
35  }
36  nrnmpi_abort(0);
37  }
38  told = nrn_threads->_t;
39 }
40 
41 void nrn_timeout(int seconds) {
42  if (nrnmpi_myid != 0) { return; }
43 #if NRNTIMEOUT_DEBUG
44  printf("nrn_timeout(%d) t=%g\n", seconds, nrn_threads->_t);
45  old_nrn_time = nrn_time();
46 #endif
47  if (seconds) {
48  told = nrn_threads->_t;
49  act.sa_handler = timed_out;
50  act.sa_flags = SA_RESTART;
51  if(sigaction(SIGALRM, &act, &oact)) {
52  printf("sigaction failed\n");
53  nrnmpi_abort(0);
54  }
55  } else {
56  sigaction(SIGALRM, &oact, (struct sigaction*)0);
57  }
58  value.it_interval.tv_sec = seconds;
59  value.it_interval.tv_usec = 0;
60  value.it_value.tv_sec = seconds;
61  value.it_value.tv_usec = 0;
62  if(setitimer(ITIMER_REAL, &value, (struct itimerval*)0)) {
63  printf("setitimer failed\n");
64  nrnmpi_abort(0);
65  }
66 
67 }
68 
69 #else
70 
71 void nrn_timeout(int seconds) { }
72 
73 #endif /* not HAVE_SETITIMER */
74 
75 #endif /*NRNMPI*/
76 
void nrnmpi_abort(int errcode)
Definition: nrnmpi.cpp:205
void
void(* nrntimeout_call)()
NrnThread * nrn_threads
Definition: multicore.cpp:45
#define printf
Definition: mwprefix.h:26
static uint32_t value
Definition: scoprand.cpp:26
double _t
Definition: multicore.h:59
int nrnmpi_myid
double nrn_time(void)
Definition: ftime.cpp:19