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