Misuse of OpenMP* THREADPRIVATE variable

A THREADPRIVATE variable was last assigned inside a parallel region and used outside that region.

The THREADPRIVATE directive specifies that variables are replicated, with each thread having its own copy. Because OpenMP* assigns work to threads in a somewhat unpredictable fashion, the value of a particular thread's copy becomes indeterminate after a scheduling point. These error diagnostic flags cases where a THREADPRIVATE variable was read when its value was in an indeterminate state.

There are two main ways a THREADPRIVATE variable can become indeterminate. One case is when leaving a parallel region in which the variable was modified. The problem here is that it is generally impossible to predict which threads will have executed which assignments. Another case is when entering a parallel region. The problem here is that it is generally impossible to predict which threads will be enlisted to join the team that executes the region. Of course, if a parallel region has a COPYIN clause, then the copy of that variable for each member of the team is initialized on entry from the copy of that variable belonging to the master thread.

Specifically, this message flags the case where a value was assigned to a THREADPRIVATE variable in a parallel region and then read outside that region.

ID

Observation

Description

1

Bad memory access

The place the variable was used

2

Memory write

The place the variable was last assigned

Example


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

int a[1000];
#pragma omp threadprivate(a)

int main(int argc, char **argv)
{
    int i;
    int sum = 0; 

#pragma omp parallel for
    for (i = 0; i < 1000; i++) {
       a[i] = i;
    }
    
    for (i = 0; i < 1000; i++) {
        sum = sum + a[i];  // bad: used "a" after parallel
    }

    printf("%d\n", sum);
    
    return 0;
}
        

Copyright © 2010, Intel Corporation. All rights reserved.