Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the SECTIONS directive in OpenMP distribute work?

Tags:

openmp

In OpenMP when using omp sections, will the threads be distributed to the blocks inside the sections, or will each thread be assigned to each sections?

When nthreads == 3:

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

Output:

id=1 id=1 

But when I execute the following code:

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

Output:

id=1 id=1  id=2 id=2 

From these output I can't understand what the concept of sections is in OpenMP.

like image 267
kar Avatar asked May 05 '10 05:05

kar


People also ask

What is the use of sections in OpenMP?

SummaryThe sections construct is a non-iterative worksharing construct that contains a set of structured blocks that are to be distributed among and executed by the threads in a team. Each structured block is executed once by one of the threads in the team in the context of its implicit task.

Which directive must precede the directive #pragma OMP sections?

Following segments must be preceded by an omp section directive. All omp section directives must appear within the lexical construct of the program source code segment associated with the omp sections directive.

What is the role of the OpenMP for directive?

OpenMP directives exploit shared memory parallelism by defining various types of parallel regions. Parallel regions can include both iterative and non-iterative segments of program code.

What is the format of directive in OpenMP?

2.1 Directive Format. Each directive starts with #pragma omp. The remainder of the directive follows the conventions of the C and C++ standards for compiler directives. In particular, white space can be used before and after the #, and sometimes white space must be used to separate the words in a directive.


1 Answers

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.

like image 110
Spock Avatar answered Oct 03 '22 10:10

Spock