OpenMP 中单节和节指令的区别

Cha*_*how 5 c++ parallel-processing multithreading directive openmp

根据我的理解,我可以使用single指令做与sections仅使用添加nowait标志相同的工作

section指令相比,以下代码对我没有什么不同:

    void main(){
    #pragma omp parallel
        {
            int tid = omp_get_thread_num();
    #pragma omp single nowait
            {
                printf("Thread %d in #1 single construct.\n", tid);
            }
    #pragma omp single nowait
            {
                printf("Thread %d in #2 single construct.\n", tid);
            }
    #pragma omp single nowait
            {
                printf("Thread %d in #3 single construct.\n", tid);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

谁能给我一些在不同场景中使用sectionssingle指令的例子?

Mas*_*ano 5

首先也是最重要的,在阅读代码时,指令具有明显不同的语义目的,并且使用一个指令来模仿另一个指令可能会产生很大的误导singlesections

关于技术细节,single这是支持该copyprivate条款的唯一工作共享指令,其中:

...提供了一种使用私有变量将值从一个隐式任务的数据环境广播到属于并行区域的其他隐式任务的数据环境的机制。

sections另一方面,工作共享结构支持和lastprivate子句reduction,但它single不支持。

最后,请注意您的代码片段:

#pragma omp single nowait
        {
            printf("Thread %d in #1 single construct.\n", tid);
        }
#pragma omp single nowait
        {
            printf("Thread %d in #2 single construct.\n", tid);
        }
#pragma omp single nowait
        {
            printf("Thread %d in #3 single construct.\n", tid);
        } // No barrier here
Run Code Online (Sandbox Code Playgroud)

不模仿 a sections,而是模仿 a sections nowait。要模仿 a,sections您必须记住让最后一个single构造保持其隐式屏障。