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."
source share