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).
You are correct that the dependency on
xcauses problems. This dependency is between iterations. Each iteration requires thexfrom 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.