Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I conditionally terminate a parallel region in OpenMP?

I have an OpenMP with C++ program. There are parallel regions that contain #pragma omp task inside a parallel region. 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)
 }

}
like image 894
Ankit Avatar asked Dec 12 '11 23:12

Ankit


People also ask

Which clause removes the implicit barrier in OpenMP?

Implicit synchronization via an implied barrier directive is performed only at exit from the for, single, or sections directive, if the nowait clause is not applied to the directive (the clause removes the implicit barrier).

Is nested parallelism possible in OpenMP?

OpenMP parallel regions can be nested inside each other. If nested parallelism is disabled, then the new team created by a thread encountering a parallel construct inside a parallel region consists only of the encountering thread. If nested parallelism is enabled, then the new team may consist of more than one thread.

What does #pragma OMP parallel for do?

#pragma omp parallel spawns a group of threads, while #pragma omp for divides loop iterations between the spawned threads. You can do both things at once with the fused #pragma omp parallel for directive.

Is there an implicit barrier after #pragma OMP parallel region?

Yes, "There is an implicit barrier at the end of the parallel construct."


1 Answers

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...).

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).

It might be possible to write system specific code, which suspends the other threads and sets them to the end of the block (e.g. 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 (e.g. by prosposing a design which doesn't need to do this).

like image 182
Grizzly Avatar answered Oct 21 '22 15:10

Grizzly