在 OpenMP 任务依赖关系中,依赖子句参数是否需要指向实际变量?

In OpenMP task dependencies does the dependency clause parameter need to point to an actual variable?

考虑这段代码:

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; }            
        }
    }
    
}

请注意,在依赖子句中,我们将“in: x”和“out: x”指向变量 x。这些依赖子句是否需要实际指向一个变量,或者它们只是占位符名称?换句话说,如果我改为使用“in: bbb”、“out: bbb”,其中 bbb 是某个任意名称并且不对应于我代码中的变量,那么这段代码是否仍然正确?

您程序中的依赖项 x 是一个 存储位置 。这个概念在 OpenMP 5.1 specification 中没有明确定义,但文档说明了一些关于它的要点 ($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.

以上段落由以下完成:

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.

因此,简而言之,是的,x需要是一个变量。它在运行时可能不存在,因为编译器可以对分配在堆栈上的变量执行优化,但存储位置无论如何都会与地址相关联(例如在实践中可能指向堆栈上的另一个变量)。

请注意,依赖项与任务中访问的实际数据不匹配:您可以在代码中使用声明为 x 的变量 y 仅使任务依赖,然后访问在任务中 x。但是,您必须确保依赖项足以避免 x.

上的任何竞争条件

另请注意,存储位置可以是数组项,如 arr[0]