Work Sharing and Thread Data Management

Work Sharing and Thread Data Management

Work sharing

Overview
  • 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.
    • task: for offloading
Work sharing construct: sections
  • Used when parallelize predetermined number of independent work units.
  • Within a primary sections construct, there can be multiple section construct.
  • A section can be executed by any available thread in the current team, including having multiple sections done by the same thread.

  • Inside csc466, create a file named hello_sections.c with the following contents:
Compile and run hello_sections.c
Challenge: section

Given the following functions: $y=x^{4} + 15x^{3} + 10x^{2} + 2x$ develop an OpenMP program called poly_openmp.c with sections/section directives. Each section should handle the calculations for one term of the polynomial.

Solution
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.
  • Inside csc466, create the following files:

hello_sections_nosingle.c: ```c linenums=”1” –8<– “docs/csc466/lectures/data/openmp/hello_sections_nosingle.c”

1
2
3
4
5
`hello_sections_single.c`: 

```c linenums="1"
--8<-- "docs/csc466/lectures/data/openmp/hello_sections_single.c"

hello_sections_single_nowait.c:

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/hello_sections_nowait.c"

  • Compile and run the above files:
Compile and run singles

Shared and Private Data

Overview
  • 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.
  • Inside csc466, create a file named counter_openmp.c with the following contents:

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/counter_openmp.c"

shared and private data
Shared and Private
  • Shared variables: accessible and modifiable by all threads
  • Private variables: each thread gets its own copy (uninitialized by default)
  • Firstprivate: each thread getss an initialized copy of a variable.
  • Lastprivate save the last computed value of a variable after parallel execution

```c linenums=”1” –8<– “docs/csc466/lectures/data/openmp/shared_private.c”

Explain the reason why the value of j is the way it is
  • What happens if you set j to 0 outside the #pragma omp parallel section?
Run and explain the results of the last variable

c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/first_last.c"