Creating Parallel Regions
- Updated2023-02-21
- 2 minute(s) read
Creating Parallel Regions
OpenMP uses the fork/join model of parallel control. A master thread executes code until it reaches a parallel region. The master thread creates a team of threads and participates in the execution of the program with the team (fork). Once the team of threads complete execution, the threads synchronize and join the master thread (join). The following image displays the fork/join model.
1 Master thread | 2 Team of threads executing the parallel region, including master | |
3 Master thread after the team joins |
OpenMP provides directives for creating multiple, concurrently executing threads, as well as directives you can use to divide work among existing parallel threads.
Use the parallel directive to create a team of threads that execute concurrently on code within the parallel region. The following code shows a parallel region declared in the main function.
#ifdef _OPENMP
#include <omp.h>
#endif
#include <ansi_c.h>
#include <stdio.h>
int main (int argc, char *argv[])
{
#pragma omp parallel
{
printf("Hello World from thread = %d\n", omp_get_thread_num());
printf("Press the Enter key to exit.");
getchar ();
}
}
The OpenMP implementation directs the threads within the parallel region to divide up the work and execute it concurrently. The default behavior is that the parallel region creates as many threads as the number of processors available on the machine.
Work-sharing directives allow you granularity on how work is divided among an existing set of threads. Work-sharing directives do not create new threads. Use the work-sharing directives for, sections, and single to divide work among a team of threads in an intentional manner. The for directive is useful for dividing the iterations of a for loop between threads. The sections directive is used to assign sections of code to different threads in the team. The single directive is useful when the executing code should not be split among different threads. The following code shows the parallel region defined at the beginning of the main function and the more granular control of how threads do the work using the single directive.
#ifdef _OPENMP
#include <omp.h>
#endif
#include <ansi_c.h>
#include <stdio.h>
void work1() {}
void work2() {}
int main (int argc, char *argv[])
{
#pragma omp parallel
{
#pragma omp single
printf("Beginning work1.\n"); // executed by only one of the threads in the team
work1();
#pragma omp single
printf("Finishing work1.\n");
#pragma omp single nowait
printf("Finished work1 and beginning work2.\n");
work2();
}
}
In the previous example the parallel directive creates the team of threads and each work function in the parallel region is then executed by a single thread. The nowait clause in the last single directive indicates that the threads that did not execute that specific single directive can continue past the implied barrier of the single directive.
Related Topics
Communicating between Threads and Sharing Data