简体   繁体   English

在OpenMP的并行区域内调用exit()是不好的做法吗?

[英]Is it a bad practice to call exit() inside a parallel region in OpenMP?

I have a program using both MPI and OpenMP. 我有一个同时使用MPI和OpenMP的程序。

The master spawns several slaves. 主人产生了几个奴隶。 Each slave is multithreaded with OpenMP and one thread is dedicated to communications (MPI_THREAD_FUNNELED). 每个从站都具有OpenMP多线程,并且一个线程专用于通信(MPI_THREAD_FUNNELED)。

When the communication thread receives a message from the master indicating that the process has to stop I don't want to wait for all threads inside the parallel region to finish. 当通信线程从主服务器接收到一条消息,指示该进程必须停止时,我不想等待并行区域内的所有线程完成。

So for now I call the exit() function inside the parallel region but I'm wondering if it's a bad practice and if there is a more elegant way to exit a process inside a parallel region ? 因此,现在我在并行区域内调用exit()函数,但我想知道这是否不好,是否还有一种更优雅的方式退出并行区域内的进程?

Summary 摘要

This is valid OpenMP but incorrect MPI . 这是有效的OpenMP,MPI不正确

OpenMP OpenMP的

From page 3 of OpenMP 4.0: 从OpenMP 4.0的第3页:

For C/C++, an executable statement, possibly compound, with a single entry at the top and a single exit at the bottom, or an OpenMP construct. 对于C / C ++,是一个可执行语句(可能是复合语句),在顶部是单个条目,在底部是单个出口,或者是OpenMP构造。

... ...

Calls to exit() are allowed in a structured block. 在结构化块中允许调用exit()。

MPI MPI

From page 357 of MPI 3.1 (definition of MPI_Finalize ): 从MPI 3.1的第​​357页( MPI_Finalize定义):

This routine cleans up all MPI state. 此例程清除所有MPI状态。 If an MPI program terminates normally (ie, not due to a call to MPI_ABORT or an unrecoverable error) then each process must call MPI_FINALIZE before it exits. 如果MPI程序正常终止(即,不是由于对MPI_ABORT的调用或不可恢复的错误引起的),则每个进程必须在退出前调用MPI_FINALIZE。

Practical consequencies 实际后果

In practice, there are usually minimal adverse consequences to violating this part of the MPI standard. 实际上,违反MPI标准的这一部分通常具有最小的负面影响。 However, it is possible that not calling MPI_Finalize will cause resource leaks in some implementations, which may eventually accumulate to make nodes in your system unusable until they are rebooted. 但是,在某些实现中,不调用MPI_Finalize可能会导致资源泄漏,最终可能会累积使系统中的节点不可用,直到重新引导它们为止。

Because MPI_Finalize is collective, it cannot be used like exit , although you can - in theory - use MPI_Abort to exit locally. 因为MPI_Finalize是集体的,所以它不能像exit一样使用,尽管从理论上讲,您可以使用MPI_Abort在本地退出。 However, this may tear down the entire MPI environment, since many implementations are not rigorous about localizing failures, even if MPI_Abort is called as MPI_Abort(MPI_COMM_SELF,0) . 但是,这可能会破坏整个MPI环境,因为即使将MPI_Abort称为MPI_Abort(MPI_COMM_SELF,0) ,许多实现也不会对本地化故障进行严格的MPI_Abort(MPI_COMM_SELF,0)

It's safe from OS' point of view. 从OS的角度来看,这是安全的。 OS closes all handles, terminates threads and frees all associated memory when you exit a process. 退出进程时,操作系统会关闭所有句柄,终止线程并释放所有关联的内存。 Modern operating systems have to do that because processes can exit inadvertently and that must not affect the system stability. 现代操作系统必须这样做,因为进程可能会意外退出,并且一定不会影响系统稳定性。

But from your app's point of view it all depends. 但是从您的应用程序的角度来看,这全都取决于。 Can your app exit in a dirty state? 您的应用程序可以在脏状态下退出吗? If you miss a disk write would it corrupt your data files? 如果您错过磁盘写操作,会损坏您的数据文件吗? If you don't send a packet would be transaction clean, would everything still keep in sync? 如果您不发送数据包,则将清除事务,一切是否仍保持同步? It's all up to what your app is doing. 这完全取决于您的应用程序在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM