openMP lastprivate 和 firstprivate 到同一个变量

openMP lastprivate and firstprivate to the same variable

在同一个变量上应用 firstprivate 和 lastprivate 是否正确?

例如:

void main (){
    int a= 100, i;
    #pragma omp for firstprivate(a) lastprivate(a)
    for(i = 0; i <9; i++){
        bla bla bla;
    }
    printf("a= %d",a);
}

谢谢!

OpenMP specification 4.0 版第 2.14.3 节所述:

A list item that specifies a given variable may not appear in more than one clause on the same directive, except that a variable may be specified in both firstprivate and lastprivate clauses.

考虑到这一点实际上很有意义。 firstprivate 在进入并行区域时影响列表变量的值,而 lastprivate 在退出该区域时影响它们。两者是不冲突的,它们的组合使用允许某些变量 "propagate" 通过该区域,并以与顺序情况相同的方式通过并行代码修改它们的值。它主要适用于并行循环。