Answer the question
In order to leave comments, you need to log in
How to fix invalid controlling predicate error?
I do not understand what is the problem?
volatile bool flag=false;
#pragma omp parallel for shared(flag)
for (int i = 0; x < b; i++)
{
if(flag) continue;
S = S + 4*(x/(x+1));
x = x + h;
if (x >= b) flag=true;
S = S + 2*(x/(x+1));
x = x + h;
}
Answer the question
In order to leave comments, you need to log in
OpenMP only works with standard loops. You have initialization on i, condition on x, i++ iteration. OpenMP simply does not know how to understand how many iterations there are and how they can be parallelized at all.
You need to do this:
const int num_iterations=1000000;
const double h = (b-a)/num_iterations/2.0
x = a;
#pragma omp parallel for reduction (+:S)
for (int i = 0; i < maxi; i++)
{
S = S + 1*(x/(x+1));
x = x + h;
S = S + 4*(x/(x+1));
x = x + h;
S = S + 1*(x/(x+1));
}
reduction (+:S)
openMP to the instruction, because you need to calculate the total amount of the same. Without this, each thread will sum up something separately. Sharing S between threads is difficult - maybe a data race.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question