The following code works based on the OpenMP 4.0 specification:
The out and inout dependence-types. The generated task will be a dependent task of all previously generated sibling tasks that reference at least one of the list items in an in, out, or inout dependence-type list.
This means that task3 becomes dependent of task2. Right? but it does not make sense! Why should an input-output dependency task be a dependent of an input dependency task?
What do I need to do in order to make them independent? p.s: code tested with g++ 4.9 on Linux.
#include <stdio.h>
#include <omp.h>
#include <unistd.h>
int main() {
int x,y;
#pragma omp parallel num_threads(10)
{
#pragma omp single nowait
{
#pragma omp task depend (out:x) //task1
{
x=1;
}
#pragma omp task depend(in:x) depend(out:y) //task2
{
sleep(2); //Does task3 wait for us? Yes!
y=x+1;
}
#pragma omp task depend (inout:x) //task3
{
x++;
printf("task3(x): %d
" , x);
}
#pragma omp task depend (in:x,y) //task4
{
printf("task4 (x+y): %d
" , x+y);
}
}
}
return 0;
}
See Question&Answers more detail:os