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
int NUM_WIDGETS, VECTOR_LENGTH;
// skip some stuff
#pragma omp parallel for
for(i = 0; i < NUM_WIDGETS; ++i)
int index = i * VECTOR_LENGTH;
float* vecA = arrayA[index];
float* vecB = arrayB[index];
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.