I am learning omp and came across this issue... consider following code where:
- Arrays
a,b, andcare initialized
#pragma omp parallel num_threads(4)
{
#pragma omp for schedule(static, 64)
for(int i = 0; i < 256; i++)
{
d[i] = a[i];
if( i + 1 < 256 )
d[i+1] = b[i];
if( i + 2 < 256 )
d[i+2] = c[i];
}
}
While running this code multiple times, following observations are noted:
- Random results (correct/in-correct) are seen
- in-correct values are assigned to
dat indexi=64 ori=128
Considering the number of threads assigned (4) and number of iterations (256), I applied schedule(static, 64) with chunk size 64. I am assuming scheduling with static will make the threads run in sequence. I even applied #pragma omp critical over assignments but that didn't work and random behavior persisted.
OMP_NUM_THREADSinstead of a hard-codednum_threads(4)which is a bad practice. Threads does not run in sequence, because otherwise it would not be parallel in the first place. Here, it defines which thread compute which items (i.e. partitioning).daccesses. And even if it would not be a problem, a loop of 256 item copy is cheap (I assume copied objects are cheap) while creating threads and distributing the work is certainly more expensive than running this in sequential... Why do you want to parallelize such a loop in the first place?