The following code shows my implementation of list ranking algorithm with OpenMP. When I execute this code without the pragmas I get the correct results, but when I include the pragmas, I get errors (occasionally) in the output. The outputs are shown at the bottom. You can see that the second time the output was wrong. This occurs randomly. When I remove the pragmas my output is always correct. Is there an error in the way I used the pragmas or is there a dependency I am missing. (The sequential output is the expected output. When the parallel output matches sequential output the program prints DATA OK)
length and number of threads are 16.
#define NSIZE 1
#define NMAX 16
int Ns[NSIZE] = {16};
int A[NMAX] = {14,13,5,16,11,10,9,12,0,8,7,15,4,3,2,1};
int B[NMAX + 1] = {0};
int S[NMAX + 1] = {0};
int Rp[NMAX + 1] = {0};
int next[NMAX+1] = {0};
for(int i = 1, j=0; i <= n; i++, j++)
{
B[i] = A[j];
}
int chunk = ceil(length/nthreads);
int i, j;
int tid;
//#pragma omp parallel num_threads(nthreads)
//{
//#pragma omp for schedule(dynamic, chunk) private(i)
for(i = 1; i <= length; i++)
{
Rp[i] = 1;
next[i] = S[i];
}
for(i = 1; i<=log2(length); i++)
{
#pragma omp parallel num_threads(nthreads) shared(Rp,next,chunk) private(j)
{
#pragma omp for schedule(dynamic,chunk)
for(j = 1; j <= length; j++)
{
if(next[j]!=0)
{
Rp[j] = Rp[j] + Rp[next[j]];
next[j] = next[next[j]];
}
}
}
}
OUTPUT:
./a.out -- (This was the output when I ran the program the first time)
from parallel
data OK
Input: 14 13 5 16 11 10 9 12 0 8 7 15 4 3 2 1
Sequential: 6 10 4 8 3 15 1 13 0 14 2 12 9 5 11 7
Parallel : 6 10 4 8 3 15 1 13 0 14 2 12 9 5 11 7
./a.out -- (output when I ran the program the second time)
from parallel
data MISMATCH!!!
Input: 14 13 5 16 11 10 9 12 0 8 7 15 4 3 2 1
Sequential: 6 10 4 8 3 15 1 13 0 14 2 12 9 5 11 7
Parallel : 6 10 4 8 3 15 1 13 0 10 2 12 9 5 11 7
./a.out -- (output when I ran the program the third time)
from parallel
data OK
Input: 14 13 5 16 11 10 9 12 0 8 7 15 4 3 2 1
Sequential: 6 10 4 8 3 15 1 13 0 14 2 12 9 5 11 7
Parallel : 6 10 4 8 3 15 1 13 0 14 2 12 9 5 11 7