OpenMP in C++

General OpenMP discussion

OpenMP in C++

Postby gedney » Wed May 07, 2008 11:32 am

I am new user of OpenMP. I am using it with a dual QuadCore processor system, with MS VisualStudio 05.

I am successfully parallelizing my code with omp, however, the data is changed with numthreads > 1. My guess is that it has to do with shared vs. private variables in the parallel loop.

My question is the following: In C++, are local variables and pointers instantiated within the parallel for loop considered private?

For example, consider a piece of psuedo code that exemplifies some of my questions:
Code: Select all

float** arrayA;
float** arrayB;
float* result;


// skip some stuff

int i;
#pragma omp parallel for
for(i = 0; i < NUM_WIDGETS; ++i)
    int index = i * VECTOR_LENGTH;
    float* vecA = arrayA[index];
    float* vecB = arrayB[index];

    float sum(0.);

    int j;
    for(j = 0; j < VECTOR_LENGTH; ++j)
         sum += vecA[j]*vecB[j];
    result[i] = sum;

My expection is the following: by default i is declared private, and arrayA, arrayB, result, NUM_WIDGETS, VECTOR_LENGTH are shared. Also, by default, the variables local to the loop index, vecA, vecB, sum, and j are private.

Are these assumptions correct? Or do index, vecA, vecB, sum, and j need to be declared private?

Thanks for fielding such a basic question. :)

Re: OpenMP in C++

Postby ejd » Wed May 07, 2008 12:29 pm

If you look at the OpenMP spec, section Data-sharing Attribute Rules for Variables Referenced in a Construct, you will find that it states:
The following variables have predetermined data-sharing attributes:
• Variables with automatic storage duration that are declared in a scope inside the construct are private

So all of your assumptions are correct and you do not need to declare anything more than you have already - if the compiler is doing what it is suppose to. The other point to remember though, is that OpenMP will make the pointer private and not what the pointer points to.

What data is being changed? Your code example doesn't seem to show anything being written to other than "sum" which is private, the loop indices which are private, and "result" which is a shared array indexed by the outer private "i". None of these look like a problem.
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: OpenMP in C++

Postby gedney » Wed May 07, 2008 8:34 pm

Thanks for your reply.

The piece of code I put there was toy code made up just to illustrate my question, which you have answered for me.

It turns out, in the parallelized loop I had a call that was altering member data of the class. Each thread was overwriting this data. Since the data space need be specific to each thread, it was changing the results. This is an easy fix.

Again, I appreciate your help. I've learned quite a bit on my first day of using OpenMP. Your answer reassured my shaken confidence and helped me to locate the problem.

Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 3 guests