OpenMP并行与浮点范围 [英] OpenMP parallel for with floating-point range

查看:223
本文介绍了OpenMP并行与浮点范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的程序:

$ pre $ int $
#pragma omp parallel for reduction(+:sum)
for(double x = 0; x <10; x + = 0.1)
sum + = x * x;
}

当我编译它时,出现错误无效类型为迭代变量'x'



我想这意味着我只能应用一个 / code>构造为基于整数的循环。但是我的循环的内部真的取决于它是浮点。

有没有办法说服OpenMP来做到这一点?有没有推荐的替代方法?

解决方案

从评论:

不,OpenMP不会为你这么做,原因和这个回答中给出的有关OpenMP循环的问题一样整数运算;编译器很难推断浮点数 - 尤其是编译器在进入循环之前需要知道循环的tripcount,循环中的浮点运算使得这一般非常困难,即使有一些简单的情况那就没事了(比如循环0.5到10.0)。因为同样的原因,即使是纯粹的串行优化/向量化这种形式的循环也会受到影响。最好的办法是做一个等价的整数循环,并根据整数索引计算你的float值。

$ p $ for(int i = 0; i <100; i ++){
double x = 0.1 * i;
sum + = x * x;
}


I have the following program:

int main(){
   double sum=0;
   #pragma omp parallel for reduction(+:sum)
   for(double x=0;x<10;x+=0.1)
   sum+=x*x;
}

When I compile it, I get the error invalid type for iteration variable ‘x’.

I take this to mean that I can only apply a parallel for construct to integer-based loops. But the internals of my loop really do depend on it being floating-point.

Is there a way to convince OpenMP to do this? Is there a recommended alternative method?

解决方案

From comments:

No, OpenMP won't do this for you for the same reasons as in this answer given to a question about OpenMP loops with integer arithmetic; it's extremely difficult for the compiler to reason about floating point numbers - in particular, the compiler needs to know before entering the loop the loop's tripcount, and floating point arithmetic in the loop makes that very difficult in general, even if there are some simple cases that would be ok (say, looping by 0.5 to 10.0).

For the same reason, even purely serial optimization/vectorization of loops of this form would suffer. The best way to do it is to make an equivalent integer loop and calculate your float based on the integer index;

for (int i=0; i<100; i++) { 
    double x=0.1*i; 
    sum += x*x; 
}

这篇关于OpenMP并行与浮点范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆