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",
29  sig,
30  z - old_nrn_time,
31  told,
32  nrn_threads->_t);
33  old_nrn_time = z;
34 #endif
35  if (nrn_threads->_t == told) { /* nothing has been accomplished since last signal*/
36  printf("nrn_timeout t=%g\n", nrn_threads->_t);
37  if (nrntimeout_call) {
38  (*nrntimeout_call)();
39  }
40  nrnmpi_abort(0);
41  }
42  told = nrn_threads->_t;
43 }
44 
45 void nrn_timeout(int seconds) {
46  if (nrnmpi_myid != 0) {
47  return;
48  }
49 #if NRNTIMEOUT_DEBUG
50  printf("nrn_timeout(%d) t=%g\n", seconds, nrn_threads->_t);
51  old_nrn_time = nrn_time();
52 #endif
53  if (seconds) {
54  told = nrn_threads->_t;
55  act.sa_handler = timed_out;
56  act.sa_flags = SA_RESTART;
57  if (sigaction(SIGALRM, &act, &oact)) {
58  printf("sigaction failed\n");
59  nrnmpi_abort(0);
60  }
61  } else {
62  sigaction(SIGALRM, &oact, (struct sigaction*) 0);
63  }
64  value.it_interval.tv_sec = seconds;
65  value.it_interval.tv_usec = 0;
66  value.it_value.tv_sec = seconds;
67  value.it_value.tv_usec = 0;
68  if (setitimer(ITIMER_REAL, &value, (struct itimerval*) 0)) {
69  printf("setitimer failed\n");
70  nrnmpi_abort(0);
71  }
72 }
73 
74 #else
75 
76 void nrn_timeout(int seconds) {}
77 
78 #endif /* not HAVE_SETITIMER */
79 
80 #endif /*NRNMPI*/
void(* nrntimeout_call)()
double nrn_time(void)
Definition: ftime.cpp:21
void
NrnThread * nrn_threads
Definition: multicore.cpp:47
#define printf
Definition: mwprefix.h:26
void nrnmpi_abort(int errcode)
Definition: nrnmpi.cpp:205
int nrnmpi_myid
static uint32_t value
Definition: scoprand.cpp:25
double _t
Definition: multicore.h:59