简体   繁体   中英

openMP lastprivate and firstprivate to the same variable

Is it correct apply firstprivate and lastprivate at the same variable?

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:

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.

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. 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. In lastprivate(x) in the clause of parallel region, it is about the last iteration of loops. x will be given the final value in the last iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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