I have a program which uses POSIX timer (timer_create()
). Essentially the program sets a timer and starts performing some lengthy (potentially infinite) computation. When the timer expires and a signal handler is called, the handler prints the best result yet that has been computed and quits the program.
I consider doing the computation in parallel using OpenMP, because it should speed it up.
In pthreads, there are special functions for example for setting signal masks for my threads or so. Does OpenMP provide such control, or do I have to accept the fact that the signal can be delivered to any of the threads OpenMP creates?
Also, in case I am currently in a parallel section of my code and my handler is called, can it still safely kill the application (exit(0);
) and do things like locking OpenMP locks?
OpenMP 3.1 standard says nothing about signals.
As I know, every popular OpenMP implementation on Linux/UNIX is based on pthreads, so OpenMP thread is pthread's thread. And generic rules of pthreads and signals apply.
Does OpenMP provide such control
No any specific control; but you can try to use pthread's control. Only problem is to know how much OpenMP threads are used and where to place controlling statement.
the signal can be delivered to any of the threads OpenMP creates?
By default, yes, it will be delivered to any thread.
my handler is called,
Usual rules about signal handler still applies. Functions allowed in signal handler are listed at http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html (at the end of page)
And printf
is not allowed (write
is). You can use printf if you know that at the moment of signal printf is not used by any thread (e.g. you has no printf in parallel region).
can it still safely kill the application (exit(0);)
Yes it can: abort()
and _exit()
are allowed from handler.
Linux/Unix will terminate all threads when any thread does exit
or abort
.
and do things like locking OpenMP locks?
You should not, but if you know that this lock will be not locked at the time of signal handler run, you can try to do this.
!! UPDATE
There is an example of adopting signalling to OpenMP http://www.cs.colostate.edu/~cs675/OpenMPvsThreads.pdf ("OpenMP versus Threading in C/C++"). In short: set a flag in handler and add checks of this flag in every thread at every Nth loop iteration.
Adapting a signal based exception mechanism to a parallel region
Something that occurs more with C/C++
applications that with Fortran applications is that
the program uses a sophisticated user interface.
Genehunter is a simple example where the user
may interrupt the computation of one family tree
by pressing control-C so that it can go on to the
next family tree in a clinical database about the
disease. The premature termination is handled in
the serial version by a C++ like exception
mechanism involving a signal handler, setjump,
and longjump.OpenMP does not permit unstructured control
flow to cross a parallel construct boundary. We
modified the exception handling in the OpenMP
version by changing the interrupt handler into a
polling mechanism. The thread that catches the
control-C signal sets a shared flag. All threads
check the flag at the beginning of the loop by
calling the routine has_hit_interrupt( )
and skip the iteration if it is set. When the loop
ends, the master checks the flag and can easily
execute the longjump to complete the
exceptional exit (See Figure 1.)