OpenMP: Work sharing and controlling thread data
Overview
Teaching: 0 min
Exercises: 0 minQuestions
How do OpenMP threads implicitly and explicitly share work among themsleves?
How do data are managed?
Objectives
Understand the basic work sharing constructs: for, secions, and single.
Understand thread-based data sharing in OpenMP
Understand the outcomes of OpenMP programs with different work sharing constructs.
1. Work sharing constructs
- OpenMP utilizes work sharing constructs to facilitate dividing parallelizable work among a number of threads.
- The work sharing constructs are:
- for: divide loop iterations among threads.
- sections: divide sections of codes among themselves.
- single: the section is executed by a single thread.
2. Work sharing construct: sections
- Used when parallelize predetermined number of independent work units.
- Within a primary
sections
construct, there can be multiplesection
construct.- A
section
can be executed by any available thread in the current team, including having multiple sections done by the same thread.
3. Hands on: sections
- In the EXPLORER window, double-click on
csc466/openmp
and selectNew Dir
to create a new directory inopenmp
calledsections
.- Inside
sections
, create a file namedhello_sections.c
with the following contents:
4. Challenge
Given the following functions: y=x4 + 15x3 + 10x2 + 2x
develop an OpenMP program calledpoly_openmp.c
withsections
/section
directives. Each section should handle the calculations for one term of the polynomial.Solution
5. Work sharing construct: single
- Limits the execution of a block to a single thread.
- All other threads will skip the execution of this block but wait until the block is finished before moving on.
- To enable proceed without waiting, a nowait clause can be added.
6. Hands on: single
- Inside
sections
, create the following files:
hello_sections_nosingle.c
:
hello_sections_single.c
:
hello_sections_single_nowait.c
:Compile and run the above files:
7. Shared and private data
- Data declared outside of a parallel region will be shared among all threads.
- Data declared inside of a parallel region will be private to individual thread.
8. Hands on: problem with shared data
- Inside
sections
, create a file namedcounter_openmp.c
with the following contents:
Key Points