Introduction To OpenMP
Introduction To OpenMP
Introduction To OpenMP
Dr M Rajasekhara Babu
Vellore Institute of Technology (VIT)
Vellore-632014, Tamil Nadu, India
Outline
Session objectives
Summary
call omp_test_lock(jlok)
C$OMP parallel do shared(a, b, c)
C$OMP MASTER
call OMP_INIT_LOCK (ilok)
C$OMP
http://www.openmp.org
SINGLE PRIVATE(X)
C$OMP ATOMIC
Directives, Environment
OpenMP library
Compiler variables
– Solution stack O O O O
R R I
– Architecture I
K N K N
– Programming model
• Fork-Join model
Parallel
Master
Regions
Thread
• Fork
– It creates a team of parallel threads called worker
threads that execute the code in the parallel region
concurrently.
• Join
– At the end of the parallel region, the worker threads
synchronize and terminate, leaving only the master
thread.
Parallel programming
– Models
Terms for OpenMP
– Popular models
OpenMP
• Parallel constructs
– Solution stack – Indicate to the compiler to use multiple threads
– Architecture to facilitate parallel execution of code.
– Programming model
• Fork-Join model • Work-sharing constructs
Terms for OpenMP
– Indicate to the compiler to generate code to
automatically distribute workload across the
team of threads.
• Data environment constructs
– Control data environment during the execution
of parallel constructs by making variables
shared or private.
Unstructured block
#pragma omp parallel
{
more: res(id) = do_big_job(id);
if (conv(res[id])) goto done;
goto more;
}
done: if (!really_done()) goto more;
Parallel programming
– Models
Flow diagram
– Popular models • Defines parallel
OpenMP
– Solution stack
region over
#pragma omp parallel
– Architecture structured block of
– Programming model code
• Fork-Join model
Terms for OpenMP
• Threads are created Thread Thread Thread Thread
as ‘parallel’ pragma 0 1 2 3
OpenMP Core Syntax
Parallel constructs is crossed
Structured block • Threads block at end
Unstructured block
– Flow diagram
of region
• Data is shared C/C++ :
among threads #pragma omp parallel
{
unless specified block
otherwise }
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 16
Parallel programming
– Models
OpenMP first program
– Popular models
OpenMP
• program that prints “hello world”.
– Solution stack
– Architecture
– Programming model int main()
• Fork-Join model
Terms for OpenMP {
OpenMP Core Syntax
Parallel constructs
Structured block
Unstructured block int ID = 0;
– Flow diagram
OpenMP first program printf(“ hello(%d) ”, ID);
printf(“ world(%d) \n”, ID);
}
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 17
Parallel programming
– Models
OpenMP first program
– Popular models
OpenMP
• Multithreaded program that prints “hello
– Solution stack world”.“omp.h”
#include
– Architecture int main()
– Programming model
• Fork-Join model
{
Terms for OpenMP
OpenMP Core Syntax #pragma omp parallel
Parallel constructs
Structured block {
Unstructured block int ID = 0;
– Flow diagram
OpenMP first program printf(“ hello(%d) ”, ID);
printf(“ world(%d) \n”, ID);
}
}
Dr M Rajasekhara Babu, Vellore Institute of Technology (VIT)-Vellore Slide.# 18
Parallel programming
– Models
OpenMP first program
– Popular models
• Multithreaded program that prints “hello world”.
OpenMP OpenMP include
– Solution stack #include “omp.h” file
– Architecture
– Programming model int main()
Parallel region
• Fork-Join model { with default
Terms for OpenMP number of threads
Structured block
{ hello (3) hello(2) world(3) world(2)