The pragma "master" OpenMP should not be included in the "prime" for parallelism "

Why does the Intel compiler not allow me to indicate that some actions in the openmp parallel for block should only be performed using the main thread?

And how can I do what I am trying to achieve without such functionality?

What I'm trying to do is update the progress bar via a callback in parallel for:

 long num_items_computed = 0; #pragma omp parallel for schedule (guided) for (...a range of items...) { //update item count #pragma omp atomic num_items_computed++; //update progress bar with number of items computed //master thread only due to com marshalling #pragma omp master set_progressor_callback(num_items_computed); //actual computation goes here ...blah... } 

I want only the main thread to call the callback, because if I don't use it (say using omp critical instead, to ensure that only one thread uses the callback right away), I get the following runtime exception:

 The application called an interface that was marshalled for a different thread. 

... therefore, the desire to keep all callbacks in the main thread.

Thanks in advance.

+6
source share
2 answers
 #include <omp.h> void f(){} int main() { #pragma omp parallel for schedule (guided) for (int i = 0; i < 100; ++i) { #pragma omp master f(); } return 0; } 

C3034 compiler error OpenMP "master" directive cannot be directly embedded in the "parallel for" directive Visual Studio 2010 OpenMP 2.0

Maybe so:

 long num_items_computed = 0; #pragma omp parallel for schedule (guided) for (...a range of items...) { //update item count #pragma omp atomic num_items_computed++; //update progress bar with number of items computed //master thread only due to com marshalling //#pragma omp master it is error //#pragma omp critical it is right if (omp_get_thread_num() == 0) // may be good set_progressor_callback(num_items_computed); //actual computation goes here ...blah... } 
+6
source

The reason you get the error is because the main thread does not exist in most cases when the code reaches the line #pragma omp master . For example, take the code from Artyom:

 #include <omp.h> void f(){} int main() { #pragma omp parallel for schedule (guided) for (int i = 0; i < 100; ++i) { #pragma omp master f(); } return 0; } 

If the code is compiled, the following may happen:

Suppose thread 0 starts (main thread). He reaches the pragma, which actually says "Master, do the following piece of code." The master function can perform a function. However, what happens when thread 1 or 2 or 3, etc. Reaches this part of the code?

The main directive tells the present / listen command that the main thread must execute f() . But the team is the only flow, and there is no presence of the master. The program did not know what to do in the past.

And therefore, I think the master cannot be inside the for loop.

Substituting master directive with if (omp_get_thread_num() == 0) works, because now the program says: "If you are a master, do it, otherwise ignore it."

+3
source

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


All Articles