How does the SECTIONS directive in OpenMP distribute work?

The code posted by the OP will never execute in parallel, because the parallel keyword does not appear. The fact that the OP got ids different from 0 shows that probably his code was embedded in a parallel directive. However, this is not clear from his post, and might confuse beginners.

The minimum sensible example is (for the first example posted by the OP):

#pragma omp parallel sections
{
    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }

    #pragma omp section
    { 
        printf ("id = %d, \n", omp_get_thread_num());
    }
}

On my machine, this prints

id = 0,
id = 1,

showing that the two sections are being executed by different threads.

It’s worth noting that however this code can not extract more parallelism than two threads: if it is executed with more threads, the other threads don’t have any work to do and will just sit down idle.

Leave a Comment