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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With