user1019083
user1019083

Reputation: 381

parallelizing using openmp

I have this function which I would like to parallelize using openmp:

for(i=len-1;i>=0;i--){
  if(bin[i]==49) // bin is a binary number & is
                 // stored in a string. 49 is ascii value of 1
  {
     s=(s*x)%n;    
  }
  x=(x*x)%n;
}

I tried using #pragma omp parallel for but it dint work. I tried with the reduction function too and yet I got wrong answers.
I think the reason is because value of s depends on x (which is dependent on each steps value).

Upvotes: 1

Views: 387

Answers (1)

Mysticial
Mysticial

Reputation: 471229

You are correct that the dependency on x causes problems. This dependency is between iterations. Each iteration requires the x from the previous iteration. So it renders the entire loop not parallelizable.

It looks like this loop is computing a power-modulus using repeated squaring.

So in short: No you won't be able to parallelize it. The dependency is inherent in the algorithm that you're using.

Upvotes: 2

Related Questions