Nan Xiao
Nan Xiao

Reputation: 17477

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

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?

Upvotes: 4

Views: 1804

Answers (1)

Zulan
Zulan

Reputation: 22670

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.

Upvotes: 5

Related Questions