pumpkinjuice
pumpkinjuice

Reputation: 75

openmp critical section inside for loop

I have the following code that updates something inside a for loop, with another for loop coming after it. However, I got the error: "expected a declaration" at the beginning of the second loop. The problem seems to be at the "critical" part, because if I delete it, the error will be gone. I'm fresh new to openMP and I was following an example here: http://www.viva64.com/en/a/0054/#ID0EBUEM (refer to "5. Too many entries to critical sections"). Anybody has any idea what I'm doing wrong here?

Besides, is it true that "If the comparison is performed before the critical section, the critical section will not be entered during all iterations of the loop"?

Another thing is that I actually want to parallelize the two loops at the same time, but since the operations inside the loops are different, I use two thread teams here, hoping that if there are threads that are not needed in the first loop, they can start executing the second loop immediately. Will this work?

double maxValue = 0.0;
#pragma omp parallel for schedule (dynamic) //first loop
    for (int i = 0; i < n; i++){
        if (some condition satisfied)
        {
            #pragma omp atomic
            count++;
            continue;
        }

        double tmp = getValue(i);

        #pragma omp flush(maxValue)
        if (tmp > maxValue){
            #pragma omp critical(updateMaxValue){
            if (tmp > maxValue){
                maxValue = tmp;
                //update some other variables
                  ...
            }
            }
        }
    }
#pragma omp parallel for schedule (dynamic) //second loop
    for (int i = 0; i < m; i++){
       //some operations...
     }
#pragma omp barrier

Sorry that I have so many questions and thanks in advance!

Upvotes: 0

Views: 8397

Answers (1)

Piotr Skotnicki
Piotr Skotnicki

Reputation: 48447

However, I got the error: "expected a declaration" at the beginning of the second loop.

You have a syntax error - an opening brace, if present, must be moved to a new line:

#pragma omp critical(updateMaxValue){
//                                 ~^~

should be changed to:

#pragma omp critical(updateMaxValue)
{

(You don't need it actually, since the if-statement that follows is a structured block).


Another thing is that I actually want to parallelize the two loops at the same time, but since the operations inside the loops are different, I use two thread teams here, hoping that if there are threads that are not needed in the first loop, they can start executing the second loop immediately.

Use a single parallel region, and then a nowait clause on the first for-loop:

#pragma omp parallel
{
    #pragma omp for schedule(dynamic) nowait
    //                                ~~~~~^
    for (int i = 0; i < n; i++)
    {
        // ...
    }

    #pragma omp for schedule(dynamic)
    for (int i = 0; i < m; i++)
    {
        // ...
    }
}

Upvotes: 1

Related Questions