Kristofer
Kristofer

Reputation: 1487

Assuring that buffer access is private using openmp

Suppose I have the following function:

void fun (){

#pragma omp parallel private(i, x, d0, d1)
    {
        #pragma omp for
        for (i = 0; i < n; i++) {

            d0 = calc_dist();
            d1 = calc_dist();
            x = ((d0 < d1) ? 0 : 1);
            buffer1[i] = x;

            #pragma omp atomic update
            group_size[x] += 1;


        } 

    }
}

I want to know if accessing buffer1 buffer1[i] = x is still private having the fact that i and x are set to be private variables in the pragma section? If not, is it possible to allow buffer access private?

Upvotes: 0

Views: 59

Answers (1)

Zulan
Zulan

Reputation: 22660

Your example access to buffer1[i] = x; is fine. It works because in the work-sharing loop, no two threads will ever get the same i and therefore no two threads will ever access the same memory.

Note that technically, buffer1[i] is not private, private applies only to variables. buffer1 is a shared variable.

You must not access any other element of buffer1 in any way within the loop. E.g. do not do anything like foo = buffer1[i-1].

This all works for regular C arrays or pointers, but there must not be any aliasing in effect.

Upvotes: 1

Related Questions