OpenMP - default子句

OpenMP是基于共享内存编程模型的,这意味着除非明确指定,否则所有的变量在并行区域中都是共享的。

使用default子句,程序员可以更改这种默认行为。default(none)指定在并行区域内,除非明确指定为sharedprivate,否则所有变量都是未定义的。这要求程序员显式地指定每个变量的作用域;default(shared),显式地指定了除非明确指定为private,否则所有变量在并行区域内都是共享的,实际上恢复了OpenMP的默认行为。firstprivate

#include <iostream>
#include <omp.h>
using namespace std;

int main(int argc, char* argv[]){
    int a = 2;
    #pragma omp parallel default(a)
    {
        int id = omp_get_thread_num();
        if (id == 3){
            a = 5;
            cout << "thread " << id << " set value of a to 5" << endl;
        }
        #pragma omp barrier
        if (id == 6){
            cout << "thread " << id << " read value of a to " << a << endl;
        }
    }
    return 0;
}
thread 3 set value of a to 5
thread 6 read value of a to 5
设置none,但不指明相关变量的共享类型
 test/openmp/pragma.cpp:11:13: error: variable 'a' must have explicitly specified data sharing attributes
            a = 5;
            ^
test/openmp/pragma.cpp:7:34: note: explicit data sharing attribute requested here
    #pragma omp parallel default(none)
                                 ^
test/openmp/pragma.cpp:12:13: error: variable 'cout' must have explicitly specified data sharing attributes
            cout << "thread " << id << " set value of a to 5" << endl;
            ^~~~
test/openmp/pragma.cpp:7:34: note: explicit data sharing attribute requested here
    #pragma omp parallel default(none)
                                 ^
2 errors generated.

。。。

posted @ 2024-03-15 16:55  酥炸小黄瓜  阅读(58)  评论(0编辑  收藏  举报