Reputation: 889
Consider this 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; }
}
}
}
notice that in the dependency clauses we have "in: x" and "out: x" pointing to variable x. Do these dependency clauses need to actually point to a variable or are they just placeholder names? In other words if instead I use "in: bbb", "out: bbb" where bbb is some arbitrary name and does not correspond to a variable in my code, would this code still be correct?
Upvotes: 2
Views: 171
Reputation: 50518
The dependency x
in your program is a storage location. This concept is not explicitly defined in the OpenMP 5.1 specification but the document states few important points about it ($1.4.1):
All OpenMP threads have access to a place to store and to retrieve variables, called the memory. A given storage location in the memory may be associated with one or more devices, such that only threads on associated devices have access to it.
The above paragraph is completed by the following one:
A storage location in memory that is associated with a given device has a device address that may be dereferenced by a thread executing on that device, but it may not be generally accessible from other devices. A different device may obtain a device pointer that refers to this device address. The manner in which a program can obtain the referenced device address from a device pointer, outside of mechanisms specified by OpenMP, is implementation defined.
Thus, put it shortly, yes, x
need to be a variable. It may not exist at runtime because the compiler can perform optimizations on variables allocated on the stack but the storage location will be associated with an address anyway (that may point on another variable on the stack for example in practice).
Note that the dependencies could not match with the actual data accessed in the task: you can use a variable y
declared as x
in your code only to make the tasks dependent and then access to x
in the task. However, you must ensure that the dependencies are sufficient to avoid any race condition on x
.
Note also that the storage location can be an array item like arr[0]
.
Upvotes: 2