简体   繁体   English

如何有条件地终止OpenMP中的并行区域?

[英]How do I conditionally terminate a parallel region in OpenMP?

I have an OpenMP with C++ program. 我有一个带有C ++程序的OpenMP。 There are parallel regions that contain #pragma omp task inside a parallel region. 在并行区域内有包含#pragma omp task的并行区域。 Now, I would like to know how to terminate the parallel region depending on a condition that any of the running threads meet. 现在,我想知道如何根据任何正在运行的线程满足的条件来终止并行区域。

#pragma omp parallel
{
 #pragma omp task
 {
   //upon reaching a condition i would like to break out of the parallel region. (all threads should exit this parallel region)
 }

}

You can't terminate a parallel construct prematurely. 您不能过早终止并行构造。 OpenMP has no construct for this and it specifies that parallel regions may have only one exit point (so no branching out of the region...). OpenMP对此没有构造,它指定并行区域可能只有一个出口点(因此没有分支出该区域...)。

I think the only (sane and portable) way to accomplish that is to have a variable which indicates if the work is finished and have the threads check that variable regularly (using atomic instructions and/or flushes to ensure correct visiblity). 我认为唯一(明智且可移植的)方法是使用一个变量来指示工作是否完成,并让线程定期检查该变量(使用原子指令和/或冲洗来确保正确的可见性)。 If the variable indicates that the work is done the threads can skip their remaining work (by putting the remaining work in an if body which isn't branched into if the work is done). 如果变量指示工作已完成,则线程可以跳过其剩余工作(通过将剩余工作放在if主体中(如果完成工作则不分支))。

It might be possible to write system specific code, which suspends the other threads and sets them to the end of the block (eg manipulating stack and instructionpointers...) but that doesn't seem very advisable (meaning it's probably very brittle). 可能可以编写系统特定的代码,该代码会挂起其他线程并将它们设置在块的末尾(例如,操纵堆栈和指令指针...),但这似乎并不明智(这可能很脆弱)。

If you'd tell us a bit more about what you are trying to do (and why you need this), it might be easier to help you (eg by prosposing a design which doesn't need to do this). 如果您要告诉我们更多有关您要做什么的信息(以及您为什么需要这样做),可能会更容易为您提供帮助(例如,提出不需要这样做的设计)。

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

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