OpenMP* SINGLE region needs a COPYPRIVATE clause

A PRIVATE or THREADPRIVATE variable was assigned inside a SINGLE region, used afterwards, and was not named in a COPYPRIVATE clause.

The THREADPRIVATE and PRIVATE directives specify that variables are replicated, with each thread having its own copy. The COPYPRIVATE clause is used to broadcast values acquired by a single thread to the copies of the named variable(s) belonging to other threads executing the region.

If a THREADPRIVATE or PRIVATE variable is assigned in a SINGLE region and then used after that region, then a COPYPRIVATE clause must be used. Otherwise, the value of the variable at the use is indeterminate, since its value was set on one thread and used on another.

Sometimes the copy must be performed by a particular thread, for instance, the master thread. In such cases, the COPYPRIVATE clause cannot be used to do the broadcast directly. Instead, it is necessary to create a temporary in a single-threaded region and provide access by applying COPYPRIVATE to that temporary shared variable.

ID

Observation

Description

1

Memory write

The place where the COPYPRIVATE variable was written

2

OpenMP declaration

The place where the SINGLE region was declared

Example

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

int main(int argc, char **argv)
{
    int i, sum = 0, offset;
    int a[100];

    #pragma omp parallel private(a, offset) reduction(+:sum)
    {
    #pragma omp single // copyprivate(offset) is needed
        {
           offset = 1;
        }
    #pragma omp for
        for (i = 0; i < 100; i++) {
           a[i] = i;
           sum = sum + a[i] + offset;
        }
    }
    printf("%d\n", sum);
    return 0;
}
        

Copyright © 2010, Intel Corporation. All rights reserved.