How to conditionally terminate a parallel area in OpenMP?

I have an OpenMP program with C ++. There are parallel areas that contain #pragma omp task inside a parallel area. Now I would like to know how to end a parallel area depending on the condition that any of the working threads encounters.

 #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) } } 
+6
source share
1 answer

You cannot prematurely complete the parallel construction. OpenMP does not have a constructor for this, and it indicates that parallel regions can have only one exit point (therefore, there is no branching from the region ...).

I think the only (reasonable and portable) way to achieve this is to have a variable that indicates whether the work is completed and that threads regularly check this variable (using atomic instructions and / or flushes to ensure proper visibility). If the variable indicates that the work is completed, the threads can skip the remaining work (by putting the remaining work in the if body, which is not forked if the work is done).

It may be possible to write system code that pauses other threads and sets them to the end of the block (for example, manipulates the stack and instructions ...), but this does not seem very appropriate (it is probably very fragile).

If you tell us a little more about what you are trying to do (and why you need it), it may be easier for you to help you (for example, by using a design that does not need to be done).

+5
source

Source: https://habr.com/ru/post/903641/


All Articles