4

My OpenMP program is like this:

#include <stdio.h>
#include <omp.h>

int main (void)
{
    int i = 10;

    #pragma omp parallel lastprivate(i)
    {
        printf("thread %d: i = %d\n", omp_get_thread_num(), i);
        i = 1000 + omp_get_thread_num();
    }

    printf("i = %d\n", i);

    return 0;
}

Use gcc to compile it and generate following errors:

# gcc -fopenmp test.c
test.c: In function 'main':
test.c:8:26: error: 'lastprivate' is not valid for '#pragma omp parallel'
     #pragma omp parallel lastprivate(i)
                          ^~~~~~~~~~~

Why does OpenMP forbid use lastprivate in #pragma omp parallel?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
  • 1
    Possible duplicate of [How are firstprivate and lastprivate different than private clauses in OpenMP?](http://stackoverflow.com/questions/15304760/how-are-firstprivate-and-lastprivate-different-than-private-clauses-in-openmp) – LPs Apr 07 '17 at 10:09

1 Answers1

5

The meaning of lastprivate, is to assign "the sequentially last iteration of the associated loops, or the lexically last section construct [...] to the original list item."

Hence, there it no meaning for a pure parallel construct. It would not be a good idea to use a meaning like "the last thread to exit the parallel construct" - that would be a race condition.

Zulan
  • 21,896
  • 6
  • 49
  • 109