Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Extensive API library for finer control

The Master Construct, The Atomic Construct

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

The Master Construct, Syntax,


Example

The Atomic construct, Syntax,


Example

Summary

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


Objectives
To provide knowledge on
Extensive API library for
finer control

To appraise with master


construct syntax and an
example

To acquaint with atomic


construct syntax and
example
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 3
 The Master Construct: Syntax
The Master Construct
• The master construct denotes the block
of code to be executed only by the
master thread.
• The syntax for the master construct is:

#pragma omp master { }

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


 The Master Construct: Syntax, Example
The Master Construct
#pragma omp parallel
{
DoManyThings();
#pragma omp master
{
// if not master, skip to next statement
ExchangeBoundaries();
}
DoManyMoreThings();
}

• The team of threads starts executing in the


parallel region.
• The threads execute the DoManyThings()
function concurrently.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 5
 The Master Construct: Syntax, Example The Master Construct
#pragma omp parallel
{
DoManyThings();
#pragma omp master
{
// if not master, skip to next statement
ExchangeBoundaries();
}
DoManyMoreThings();
}
• Only the master thread is allowed to enter the
master region. There is no implicit barrier at the
end.
• Thus, the remaining threads skip the master
region and start executing the
DoManyMoreThings() function without waiting
for the master thread to complete
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 6
 The Master Construct: Syntax, Example The Atomic Construct
 The Atomic Construct
• The atomic construct is used to protect
shared variables.
• This is done by ensuring that the
specific memory location is updated
atomically.
• It prohibits multiple threads from
writing at the same memory location,
which prevents data races.

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


 The Master Construct: Syntax, Example The Atomic Construct
 The Atomic Construct: Syntax
• The atomic construct works only on
the single statement that follows the
directive.
• The syntax for the atomic construct is
provided here:

#pragma omp atomic

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


 The Master Construct: Syntax, Example The Atomic Construct
 The Atomic Construct: Syntax, Example
#pragma omp parallel for
shared(x,y,index,n)
for (i=0; i<n; i++)
{
#pragma omp atomic
x[index[i]] += work1(i);
y[i] += work2(i);
}
• In the above example, value of index[i] is
the same for different values of the
variable i.
• Therefore, updates to x must be protected
and updating an element of the array x[] is
declared as atomic.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 9
 The Master Construct: Syntax, Example
 The Atomic Construct: Syntax, Example
The Atomic Construct
#pragma omp parallel for
shared(x,y,index,n)
for (i=0; i<n; i++)
{
#pragma omp atomic
x[index[i]] +=
work1(i);
y[i] += work2(i);
}
• The other computations, such as the call
to work1() and the computation of the
value in index[i], cannot be done
atomically.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 10
 The Master Construct: Syntax, Example The Atomic Construct
 The Atomic Construct: Syntax, Example
#pragma omp parallel for
shared(x,y,index,n)
for (i=0; i<n; i++)
{
#pragma omp atomic
x[index[i]] += work1(i);
y[i] += work2(i);
}

• If a critical region were used, instead of the atomic,


all computations within the statement (call to
work1(), computation of index[i], and update of
element in x) would be protected.
• The atomic construct protects only individual
elements of the x[] array.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 11
Summary
• The Master Construct
– Syntax
#pragma omp master { }
– Example
• The Atomic Construct
– Syntax
#pragma omp atomic
– Example

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

You might also like