J
J
jeyyykeyyy2020-11-01 16:15:33
C++ / C#
jeyyykeyyy, 2020-11-01 16:15:33

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

1 answer(s)
W
Wataru, 2020-11-01
@wataru

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));
}

It is more convenient if you first fix the number of iterations, and not the step h (the step is calculated from here). Because if the segment is not completely divisible by h, then you need to process it somehow, and in general, is it possible to count like that?
I added 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 question

Ask a Question

731 491 924 answers to any question