Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenMP ordered clause

How is the ordered clause in OpenMP supposed to be correctly used? There is this test code to check if the loop will be executed by increasing values of n but it is not always the case.

Did I misinterpret the definition of the ordered clause?

The ordered construct specifies a structured block in a loop region that will be executed in the order of the loop iterations. This sequentializes and orders the code within an ordered region while allowing code outside the region to run in parallel.

    #include <stdio.h>
    #include <stdlib.h>
    #include <omp.h>

    int main(){

        int n;
    omp_set_num_threads(4);
    #pragma omp parallel
        {
    #pragma omp for ordered
            for (n=0;n<10;n++)
                printf("n = %d\n",n);
        }
        return 0;
    }

When compiling with

   gcc -Wall -Wextra -fopenmp test_par.c

The output is

    ./a.out 
    n = 0 
    n = 1 
    n = 2 
    n = 9 
    n = 3 
    n = 4 
    n = 5 
    n = 6 
    n = 7 
    n = 8 
like image 667
Suzanka Avatar asked Nov 27 '15 12:11

Suzanka


People also ask

What is ordered clause in OpenMP?

Summary The order clause specifies an expected order of execution for the iterations of the associated loops of a loop-associated directive. Description The order clause specifies an expected order of execution for the iterations of the associated loops of a loop-associated directive.

What is Nowait clause in OpenMP?

15.6 nowait Clause The nowait clause overrides any synchronization that would otherwise occur at the end of a construct. It can also specify that an interoperability requirement set includes the nowait property. If the construct includes an implicit barrier, the nowait clause specifies that the barrier will not occur.

What is default clause OpenMP?

The default(firstprivate) clause causes all variables in the construct that have implicitly determined data-sharing attributes to be firstprivate. The default(private) clause causes all variables referenced in the construct that have implicitly determined data-sharing attributes to be private.


1 Answers

The OpenMP ordered clause is supposed to be used in two distinct stages:

  1. As a clause of the for directive, to indicate some possible ordering of iterations; and then
  2. As a standalone directive to indicate which parts of the statements inside the loop are to be kept ordered.

So in essence, your example should be:

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main(){

    int n;
    omp_set_num_threads(4);
    #pragma omp parallel
    {
        #pragma omp for ordered
        for (n=0;n<10;n++)
            #pragma omp ordered
            printf("n = %d\n",n);
    }
    return 0;
}

Which gives:

$ g++ -fopenmp order.cc
$ ./a.out 
n = 0
n = 1
n = 2
n = 3
n = 4
n = 5
n = 6
n = 7
n = 8
n = 9

However, two remarks here:

  1. This parallel loop is actually fully sequentialised as there is nothing outside of the ordered directive
  2. Moreover, if you which to mitigate the impact of serialisation induced by the ordered directive, you should play with the schedule directive. A good candidate for an "effective" scheduling in the case of an ordered loop would be a schedule( static, 1 ) for example.

And finally, Hristo Iliev explains it all better than I'll ever be able to here

like image 134
Gilles Avatar answered Nov 01 '22 01:11

Gilles