Runtime Behavior of an OpenMP Application
OpenMP works using a combination of compiler support plus a runtime library. The compiler uses the directives in the source code to produce appropriate blocks of code together with the necessary calls into the runtime library.
For example, when the compiler encounters a directive defining a parallel region, the compiler will produce a new block of code that will be executed by the threads in paral-lel. The runtime library is responsible for assigning the work to the various threads.
OpenMP follows a fork-join type model. The runtime library will create a team of threads. When a parallel region is encountered, the work will be divided between mem-bers of this team of threads. At the end of the region, the original thread, called the mas-ter thread, will continue executing the code after the region. The other threads, called the worker threads, will wait until the master thread reaches the next parallel region and new work is assigned to them. The number of threads that will be used is set by the environ-ment variable OMP_NUM_THREADS, but this can be adjusted by the application at runtime by calls into the runtime support library. For example, consider the code in Listing 7.22.
Listing 7.22 OpenMP Parallel Region
#pragma omp parallel
printf( "Thread\n" );
When compiled and run, each thread of the number of threads specified by OMP_NUM_THREADS will execute the parallel region. For the Solaris Studio compiler to recognize the OpenMP directives, it needs the flag -xopenmp. The flag -xloopinfo tells the compiler to provide information about the parallelization it has undertaken. Listing 7.23 shows the output of compiling and running this program. Each of the two threads executes the parallel region and prints the output Thread.
Listing 7.23 OpenMP Parallel Region
$ cc -O -xopenmp -xloopinfo omptest.c
$ export OMP_NUM_THREADS=2
Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.