Reputation: 91
Since I am working a lot with Openmp, I had this question in mind. I read somewhere that when working with tasks, there is no specific way for the tasks to be executed. Like in this example
// Compile with: g++ -O3 test.cpp -fopenmp
#include <cstdio>
int main(){
int a = -3;
#pragma omp parallel num_threads(3)
{
#pragma omp task
a = 3;
#pragma omp task
a++;
}
printf("%d\n", a);
return 0;
}
Does this mean that any thread can execute the 1st ready task (could be one of the three a++ or the a=3)??
Upvotes: 0
Views: 512
Reputation: 50623
Yes, any thread can execute the 1st ready task. If this is not Ok, you can add task dependencies using the depend
clause. Note however that you can specify dependencies only in the same task region (ie. between sibling tasks of the same parent task but not with others). The task scheduling tends to change significantly between different runtime implementation (eg. GOMP of GCC versus IOMP of Clang/ICC).
Note that variables in task regions are implicitly copied (like using firstprivate
) as opposed to parallel
regions. However, this is not the case when they are shared in the parent parallel section like in your code as pointed out by @Laci in the comments (in this case, they are shared by the tasks).
Also please note that the #pragma omp single
only applies to the next statement, that is, the following task directive and not the second one. This means the second task directive should generate 3 task (1 per thread).
Upvotes: 1