0

I have a code which has data dependency.

code example:

int data[1000*3];
int result[1000]={0,};

// initialize data[]

for(i=0; i<1000; i++)
{
   a = data[i*3 + 0];
   b = data[i*3 + 1];
   c = data[i*3 + 2];

   if( (a>b) && (a>c) )   // This line makes data dependency
      result[i]++;
}

I want to remove data dependency to parallelize this code. I know I have to edit my code, but don't know how to fix it.

How do I have to fix this code?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
soongk
  • 259
  • 3
  • 17
  • 1
    I don't see a data dependency, but sometimes the compiler gets confused when you do indexing math inside the square brackets. As long as you privatize a, b, and c, this should be parallelizable. Since you tagged this with openacc, I'd suggest using `acc parallel loop` instead of `acc kernels` if the compiler is telling you that it can't parallelize due to a data dependency. – jefflarkin Oct 06 '15 at 22:01
  • Actually, I found data dependency from another code like that(used NVVP). But as this sample code, NVVP dosen't tell me that makes data dependency. I think my real code's dependency occured from other reasons. I'll try to found other reason. Thank you for help, jeff. :) – soongk Oct 08 '15 at 06:13
  • Two things to look for: 1) If you're accessing multiple arrays, make sure the pointers are declared `restrict` so the compiler knows they're not aliased. 2) Indexing math inside the square brackets. – jefflarkin Oct 08 '15 at 09:45

1 Answers1

0

Try:

int data[1000*3];
int result[1000]={0,};

// initialize data[]

for(i=0; i<1000; i++)
{
   if( (data[i*3 + 0] > data[i*3 + 1]) && (data[i*3 + 0] > data[i*3 + 2]) )   
      result[i]++;
}
Miner_Glitch
  • 537
  • 4
  • 16