Work-sharing constructs:
Loop Construct
Dr M Rajasekhara Babu
Vellore Institute of Technology (VIT)
Vellore-632014, Tamil Nadu, India
Outline
Session objectives
About work-sharing construct
Loop work-sharing construct
Sample Program Demo
Summary
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 2
Objectives
To provide knowledge on
OpenMP
To Combine serial and
parallel codes by using
various constructs
To create threaded codes
by using OpenMP
pragmas.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 3
About Work-sharing constructs Work-sharing constructs
• There are three types worksharing
constructs
– loop construct
– sections construct
– single construct
• Restrictions
– Each worksharing region must be encountered
by all threads in a team or by none at all
– The sequence of worksharing regions and
barrier regions encountered must be the same
for every thread in a team.
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 4
About Work-sharing constructs Loop Construct
Loop work-sharing construct
• Loop Construct
– The loop construct specifies that the iterations
of one or more associated loops will be shared
in parallel by all threads.
• FOR
– Splits loop iterations into threads
– Must be in the parallel region
– Must precede the loop
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 5
About Work-sharing constructs
Loop work-sharing construct
FOR-Loop Construct
– Syntax with two pragmas
• Syntax
#pragma omp parallel
#pragma omp parallel
#pragma omp for
for(i=0; i<N; i++) #pragma omp for
{ i=0 i=3 i=6 i=9
Do_Work(i); i=1 i=4 i=7 i = 10
i=2 i=5 i=8 i = 11
}
• Example Implicit barrier
#pragma omp parallel
#pragma omp for
for(i = 0; i < 12; i++)
c[i] = a[i] + b[i]
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 6
About Work-sharing constructs
Loop work-sharing construct
FOR-Loop Construct
– Syntax with two pragmas
– Combining programs into single pragma
• Combining pragmas
#pragma omp parallel
{
#pragma omp for
for (i=0; i< MAX; i++) {
res[i] = huge();
}
}
#pragma omp parallel for
for (i=0; i< MAX; i++) {
res[i] = huge();
}
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 7
About Work-sharing constructs
Loop work-sharing construct
OpenMP program Demo
– Syntax with two pragmas
– Combining programs into single pragma
– Demo
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 8
Summary
• About Work-sharing constructs
• Loop work-sharing construct
– Syntax with two pragmas
#pragma omp parallel
#pragma omp for
for()
{ }
– Combining programs into single pragma
#pragma omp parallel for
for()
{ }
• Demo
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 9