Docsity

Understanding OpenMP's 'omp sections' Directive and Synchronization Constructs, Slides of Computer Science

Toggle sidebar
bg1
Objectives_template
file:///D|/...ary,%20Dr.%20Sanjeev%20K%20Aggrwal%20&%20Dr.%20Rajat%20Moona/Multi-core_Architecture/lecture%2020/20_1.htm[6/14/2012 12:03:38 PM]
Module 10: Open Multi-Processing
Lecture 20: The “omp sections” Directive
The Lecture Contains:
The “omp sections” Directive
The “single” Directive
Work-Sharing Constructs: WORKSHARE Directive
Combined Parallel Work-sharing Constructs
Synchronization
Synchronization Construct: “critical” Directive
Synchronization Constructs: “atomic” Clause
Barrier
Synchronization Constructs: “barrier” Directive
Synchronization Constructs: “ordered” Directive
Synchronization Constructs: “flush” Directive
Data Environment:
Changing Storage Attributes
Data Scope Attribute Clauses
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Understanding OpenMP's 'omp sections' Directive and Synchronization Constructs and more Slides Computer Science in PDF only on Docsity!

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

The Lecture Contains:

The “omp sections” Directive

The “single” Directive

Work-Sharing Constructs: WORKSHARE Directive

Combined Parallel Work-sharing Constructs

Synchronization

Synchronization Construct: “critical” Directive

Synchronization Constructs: “atomic” Clause

Barrier

Synchronization Constructs: “barrier” Directive

Synchronization Constructs: “ordered” Directive

Synchronization Constructs: “flush” Directive

Data Environment:

Changing Storage Attributes

Data Scope Attribute Clauses

file:///D|/...ry,%20Dr.%20Sanjeev%20K%20Aggrwal%20&%20Dr.%20Rajat%20Moona/Multi-core_Architecture/lecture%2020/20_1a.htm[6/14/2012 12:03:38 PM]

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

The “omp sections” Directive

Clauses Supported: “sections” Directive

Private(list) Lastprivate(list) Firstprivate(list) Reduction(operator:list) Nowait

Questions

What happens if the number of threads and the number of sections are different? More threads than sections?

Example 1

A Parallel Section Example

#pragma omp parallel shared(n,a,b,c,d) private(i) { #pragma omp sections nowait { #pragma omp section for (i = 0; i < n-1; i++) b[i] = (a[i] + a[i+1])/2; #pragma omp section for (i = 0; i < n; i++) d[i] = 1.0/c[i]; } /– End of sections –/ } /– End of parallel region –/

By default, there is a barrier at the end of the “omp sections”. Use of “nowait” clause turns off the barrier

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

Combined Parallel Work-sharing Constructs

OpenMP provides combined Parallel Worksharing directive that are merely shortcuts: PARALLEL DO/parallel for Parallel SECTIONS These directives behave same as an individual PARALLEL directive being immediately followed by a separate work-sharing directive. Most of the rules and clauses that apply to both directives are in effect

#pragma omp parallel #pragma omp for for (...)

#pragma omp parallel for for (....)

#pragma omp parallel #pragma omp sections {...}

#pragma omp parallel sections {.... }

Synchronization

Consider a simple example where two threads on two different processors are both trying to increment a variable x at the same time (assume x is initially 0):

THREAD 1 increment(x){ x = x+1; } THREAD 1 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)

THREAD 2

increment(x){ x = x+1; } THREAD 2 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)

What are the possible outputs?

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

Synchronization

Consider a simple example where two threads on two different processors are both trying to increment a variable x at the same time (assume x is initially 0):

THREAD 1 increment(x){ x = x+1; } THREAD 1 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)

THREAD 2

increment(x){ x = x+1; } THREAD 2 10 LOAD A, (x address) 20 ADD A, 1 30 STORE A, (x address)

What are the possible outputs? After exuction of both threads resultant value of x may be 1 for some execution sequence To avoid situations like this the incrementation of x must be synchronized between the two threads

Synchronization is used to impose order constraints and to protect access to shared data

High Level Synchronization

Critical Atomic Barrier Ordered

Low Level Synchronization

Flush Locks

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

Synchronization Constructs: “atomic” Clause

Format: atomic

#pragma omp atomic statement expression

The atomic directive specifies that a specific memory location must be updated atomically, rather than letting multiple threads attempt to write to it

Barrier

Why barriers?

Suppose we run each of these two loops in parallel

for( i = 0; i < N; i++) a[i] = b[i] + c[i]; for(i = 0; i < N; i++) d[i] = a[i] + b[i];

This may give us wrong result WHY?

Barrier

We should have updated all a[]’s before using them

for( i = 0; i < N; i++) a[i] = b[i] + c[i];

All threads should wait here for other threads to complete so we need a barrier here

for(i = 0; i < N; i++) d[i] = a[i] + b[i];

All threads wait at the barrier point and only continue when all threads have reached the barrier point If there is the guarantee that the mapping of iterations onto threads is identical for both loops, there will not be a data race in this case

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

Synchronization Constructs: “barrier” Directive

Format: barrier

#pragma omp barrier

A thread will wait at barrier until all other threads have reached that barrier. All threads then resume executing in parallel the code that follows the barrier

Synchronization Constructs: “ordered” directive

Used within a DO/for loop with and ordered clause The ordered directive specifies that iterations of the enclosed loop will be executed in the same

order as if they were executed on a serial processor.

Format

#pragma omp ordered structured block

Example

#pragma omp parallel for ordered for(I = 0 ; I < N ; I++){ tmp = NEAT STUFF(I); #pragma omp ordered res += consume(tmp); }

Synchronization Constructs: “flush” Directive

Format

pragma omp flush (list)

Defines a sequence point at which a thread is guaranteed to see a consistent view of memory with respect to the “flush set” The flush set is : List of variables In absence of list all thread visible variable are in list Flush forces data to be updated in memory so other threads see the most recent value

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

Data Scope Attribute Clauses

The Data Scope Attribute Clauses are used to explicitly define how variables should be scoped.They include :

Private Firstprivate Lastprivate Shared Default Reduction Copyin

“private” Clause

Purpose: The private clause declares variables in its list to be private to each thread Format: private (list)

A new object of the same type is declared for each thread in the team Private(var) creates a new local copy of var for each thread

Example: “private” Clause

void wrong() { int tmp = 0; #pragma omp parallel for private(tmp) for (int j = 0; j < 1000; ++j) tmp += j; printf(“%d”, tmp); }

What is wrong with the code? Value of tmp was not initialized at line 5 What value of tmp it will be printed in second last line? In OpenMP 2.5 the value of the shared variable is undefined after the region So tmp is unspecified for OpenMP 2.5 while 0 in OpenMP 3.

file:///D|/...ry,%20Dr.%20Sanjeev%20K%20Aggrwal%20&%20Dr.%20Rajat%20Moona/Multi-core_Architecture/lecture%2020/20_10.htm[6/14/2012 12:03:40 PM]

Module 10: Open Multi-Processing

Lecture 20: The “omp sections” Directive

Data Scope Attribute Clauses

“firstprivate” Clause

Purpose: The firstprivate clause combines the behavior of the private clause with automatic initialization of the variables in the list Format: firstprivate (list)

Special case of private clause Initializes each private copy with the corresponding value from the master thread prior to entry into the parallel or work-sharing construct

The “firstprivate” Clause

Example: “firstprivate” Clause

void useless() { int tmp = 0; #pragma omp parallel for firstprivate(tmp) for (int j = 0; j ¡ 1000; ++j) tmp += j; printf(“%d”, tmp); }

Each thread gets its own code with initial value 0.Is there something still wrong with the code? What value of tmp it will be printed in second last line? Tmp is unspecified for OpenMP 2.5 while 0 in OpenMP 3.

“lastprivate” Clause

Purpose: The lastprivate clause combines the behavior of the private clause with a copy from the last loop iteration or section to the original variable object. Format: lastprivate (list)

The value copied back into the original variable object is obtained from the last (sequentially) iteration or section of the enclosing construct.