0% found this document useful (0 votes)
32 views12 pages

A Synchornization Constructs

The document discusses synchronization constructs in OpenMP including the nowait clause and barrier construct. The nowait clause allows threads to ignore implicit barriers, while the barrier construct enforces explicit synchronization between all threads. Examples are provided to illustrate the syntax and use of each construct.

Uploaded by

Variable 14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views12 pages

A Synchornization Constructs

The document discusses synchronization constructs in OpenMP including the nowait clause and barrier construct. The nowait clause allows threads to ignore implicit barriers, while the barrier construct enforces explicit synchronization between all threads. Examples are provided to illustrate the syntax and use of each construct.

Uploaded by

Variable 14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Synchronization

Nowait clause, Barrier Construct

Dr M Rajasekhara Babu
Vellore Institute of Technology (VIT)
Vellore-632014, Tamil Nadu, India
Outline
Session objectives

Nowait clause: Syntax, Example

The Barrier construct, Syntax,


Example

Summary

Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 2


Objectives
To provide knowledge on
synchronization constructs

To appraise with nowait


clause significance

To appraise with barrier


construct importance and
use
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 3
 The Nowait Clause: Syntax
The Nowait Clause
• The nowait clause allows threads to
ignore the implicit barrier.
• When you use the nowait clause, the
threads do not wait for the other threads
at the end of the constructs.
• The syntax for the nowait clause is:

#pragma construct nowait


{[...]};

Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 4


 The Nowait Clause: Syntax
The Nowait Clause
• If you want to ignore the implicit
barrier at the end of a for loop
• use the following syntax

#pragma omp for nowait


for (...)
{...};
• can use the nowait clause when the
threads are likely to wait between
independent computations.

Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 5


 The Nowait Clause: Syntax, Example The Nowait Clause
#pragma omp for schedule(dynamic,1)
nowait
for (int i=0; i<n; i++)
a[i] = bigFunc1(i);

#pragma omp for schedule(dynamic,1)


for (int j=0; j<m; j++)
b[j] = bigFunc2(j);

• In the above example, threads enter


the first loop and execute it.
• In the absence of the nowait clause,
threads pause for all iterations of
the first loop to complete .
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore S
lide.# 6
 The Nowait Clause: Syntax, Example The Nowait Clause
schedule(dynamic,1) nowait
#pragma omp for
for (int i=0; i<n; i++)
a[i] = bigFunc1(i);

#pragma omp for schedule(dynamic,1)


for (int j=0; j<m; j++)
b[j] = bigFunc2(j);

• With the nowait clause, when the work


exhausts in the first loop, threads begin
executing work in the second loop.
• As a result, the nowait clause ignores
the implicit barrier at the end of the first
loop.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 7
 The Nowait Clause: Syntax, Example The Barrier Construct
 The Barrier Construct
• In contrast to the implicit barriers,
the barrier construct enforces
explicit barrier synchronization.
• This construct synchronizes all the
threads in the team.
• When a thread encounters a barrier
construct, it waits for all the other
threads to reach that barrier.

Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 8


 The Nowait Clause: Syntax, Example
 The Barrier Construct: Syntax
The Barrier Construct
• All threads then start executing
the code that follows the
barrier construct.
• The syntax for the barrier
construct is provided here:

#pragma omp barrier

Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 9


 The Nowait Clause: Syntax, Example
 The Barrier Construct: Syntax, Example
The Barrier Construct
#pragma omp parallel shared(A,B,C)
{
DoSomeWork(A,B);
printf (“Processed A into B\n”);
#pragma omp barrier
DoSomeWork(B,C);
printf (“Processed B into C\n”);
}

• In this example, A, B, and C are shared


variables.
• Threads enter the parallel region and execute
the DoSomeWork() function in parallel with A
and B.

Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 10


 The Nowait Clause: Syntax, Example
 The Barrier Construct: Syntax, Example
The Barrier Construct
#pragma omp parallel shared(A,B,C)
{
DoSomeWork(A,B);
printf (“Processed A into B\n”);
#pragma omp barrier
DoSomeWork(B,C);
printf (“Processed B into C\n”);
}
• You need to update the value of B before
performing any computation involving B
by calling the second DoSomeWork()
function.
• Setting the barrier synchronization creates a
threshold.
• Threads need to wait for the other threads
before entering the next code block.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore S
lide.# 11
Summary
• The Nowait Clause
– Syntax
#pragma construct nowait
– Example
• The Barrier Construct
– Syntax
#pragma omp barrier
– Example

Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 12

You might also like