gpt4 book ai didi

c - OpenMP 并行程序中的信号处理

转载 作者:太空狗 更新时间:2023-10-29 17:03:52 29 4
gpt4 key购买 nike

我有一个使用 POSIX 计时器的程序( timer_create() )。本质上,程序设置了一个计时器并开始执行一些冗长的(可能是无限的)计算。当计时器到期并调用信号处理程序时,处理程序打印出已计算出的最佳结果并退出程序。

我考虑使用 OpenMP 进行并行计算,因为它应该加快计算速度。

在 pthreads 中,有一些特殊的功能,例如为我的线程设置信号掩码等。 OpenMP 是否提供这样的控制,或者我是否必须接受信号可以传递到 OpenMP 创建的任何线程的事实?

另外,如果我当前处于代码的并行部分并且我的处理程序被调用,它是否仍然可以安全地终止应用程序( exit(0); )并执行诸如锁定 OpenMP 锁之类的操作?

最佳答案

OpenMP 3.1 标准没有提及信号。
据我所知,Linux/UNIX 上每个流行的 OpenMP 实现都是基于 pthread 的,因此 OpenMP 线程是 pthread 的线程。并且 pthread 和信号的通用规则适用。

Does OpenMP provide such control


没有任何特定的控制;但是你可以尝试使用pthread的控制。唯一的问题是要知道使用了多少 OpenMP 线程以及在哪里放置控制语句。

the signal can be delivered to any of the threads OpenMP creates?


默认情况下,是的,它将被传递到任何线程。

my handler is called,


有关信号处理程序的通常规则仍然适用。信号处理程序中允许的函数列于 http://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html (在页面末尾)
printf不允许( write 是)。如果您知道在发出信号时 printf 未被任何线程使用(例如,并行区域中没有 printf),则可以使用 printf。

can it still safely kill the application (exit(0);)


是的,它可以: abort()_exit()允许从处理程序。
Linux/Unix 将在任何线程执行时终止所有线程 exitabort .

and do things like locking OpenMP locks?


你不应该,但如果你知道这个锁在信号处理程序运行时不会被锁定,你可以尝试这样做。
!!更新
有一个对OpenMP采用信令的例子 http://www.cs.colostate.edu/~cs675/OpenMPvsThreads.pdf (“OpenMP 与 C/C++ 中的线程”)。简而言之:在处理程序中设置一个标志,并在每个第 N 次循环迭代的每个线程中添加此标志的检查。

Adapting a signal based exception mechanism to a parallel region

Something that occurs more with C/C++applications that with Fortran applications is thatthe program uses a sophisticated user interface.Genehunter is a simple example where the usermay interrupt the computation of one family treeby pressing control-C so that it can go on to thenext family tree in a clinical database about thedisease. The premature termination is handled inthe serial version by a C++ like exceptionmechanism involving a signal handler, setjump,and longjump.OpenMP does not permit unstructured controlflow to cross a parallel construct boundary. Wemodified the exception handling in the OpenMPversion by changing the interrupt handler into apolling mechanism. The thread that catches thecontrol-C signal sets a shared flag. All threadscheck the flag at the beginning of the loop bycalling the routine has_hit_interrupt( )and skip the iteration if it is set. When the loopends, the master checks the flag and can easilyexecute the longjump to complete theexceptional exit (See Figure 1.)

关于c - OpenMP 并行程序中的信号处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8138168/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com