简体   繁体   English

openMP lastprivate 和 firstprivate 到同一个变量

[英]openMP lastprivate and firstprivate to the same variable

Is it correct apply firstprivate and lastprivate at the same variable?在同一个变量中应用 firstprivate 和 lastprivate 是否正确?

For example:例如:

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

Thank you!谢谢!

As written in the OpenMP specification version 4.0, Section 2.14.3:OpenMP 规范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.指定给定变量的列表项不能出现在同一指令的多个子句中,除非变量可以在firstprivatelastprivate子句中指定。

It actually makes a lot of sense to allow for that.允许这样做实际上很有意义。 firstprivate affects the value of the list variables on entry into the parallel region, while lastprivate affects them on exit from the region. firstprivate在进入并行区域时影响列表变量的值,而lastprivate在退出该区域时影响它们。 Both are non-conflicting and their combined use allows for certain variables to "propagate" through the region and get their values modified by the parallel code in the same way as in the sequential case.两者都是非冲突的,并且它们的组合使用允许某些变量通过区域“传播”并以与顺序情况相同的方式通过并行代码修改它们的值。 It mostly makes sense with parallel loops.它主要适用于并行循环。

In firstprivate(x) and in the clause of parallel region the variable x can be recognized as initial variable with some value which was defined somewhere to all the team threads.Each team thread owns its own private x.在 firstprivate(x) 和并行区域的子句中,变量 x 可以被识别为具有某个值的初始变量,该值在某处定义给所有团队线程。每个团队线程拥有自己的私有 x。 In lastprivate(x) in the clause of parallel region, it is about the last iteration of loops.在并行区域子句中的lastprivate(x)中,它是关于循环的最后一次迭代。 x will be given the final value in the last iteration. x 将在最后一次迭代中获得最终值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM