First of, here's the sample code:
#include <iostream>
int main()
{
int x = 100;
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task depend (in: x)
{ x += 1; }
#pragma omp task depend (out: x)
{ x *= 2; }
}
}
printf("x = %d\n", x); // prints 202
}
From what I understand, task depend(in: x) should wait for anything with depend(out: x), but that doesn't seem to be what's happening.
In this particular case, the output is 202 suggesting that x was incremented first and then doubled.
In fact, I have tried both in/out and out/in, as well as switching the order in which the tasks themselves are defined. Regardless of in/out dependencies, the tasks always execute top to bottom.
Am I misunderstanding the meaning of task depend?
The implementation is doing the correct thing.
Apart from the order that is established through the depend clause, also the position of the task creation in the source code has an impact on the dependency.
In your example, the first task has an in dependency on the x variable. As there's no existing task in the queues that has an out or inout dependency for the same variable, the task is ready to execute immediately and the implementation will likely do so even before the second task is encountered.
If I exchange the two tasks:
int main()
{
int x = 100;
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task depend (out: x)
{ x *= 2; }
#pragma omp task depend (in: x)
{ x += 1; }
}
}
printf("x = %d\n", x); // prints 201
}
The code prints 201, as expected. I tested this with the Intel Compiler 18.0.3.
The correct modeling of the task dependences for your code would look like this:
int main()
{
int x = 100;
#pragma omp parallel
{
#pragma omp single
{
#pragma omp task depend (inout: x)
{ x *= 2; }
#pragma omp task depend (inout: x)
{ x += 1; }
}
}
printf("x = %d\n", x); // prints 201
}
The code still prints 202, but now the dependences correctly model the actual usages of x: task 1 reads the old value of x and modifies it, so it should use an inout dependence. Task 2 also reads x and modifies it, so it should also use an inout dependence.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With