使用 openmp 并行化
我有这个函数,我想使用 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;
}
我尝试使用 #pragma omp parallel for 但它不起作用。我也尝试过使用归约函数,但得到了错误的答案。
我认为原因是因为 s 的值取决于 x (这取决于每个步骤的值)。
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是正确的,对
x
的依赖会导致问题。这种依赖性存在于迭代之间。每次迭代都需要上一次迭代的x
。因此它使得整个循环不可并行化。看起来这个循环正在使用重复平方计算幂模数。
简而言之:不,你将无法并行化它。这种依赖性是您所使用的算法所固有的。
You are correct that the dependency on
x
causes problems. This dependency is between iterations. Each iteration requires thex
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.