Openmp 1
Openmp 1
• OpenMP
• Program Structure
• Directives : parallel , for
• Clauses
• If
• Private
• Shared
• Default(shared/none)
• Firstprivate
• Num_threads
• References
1. Introduction
• Serial Programming • What is parallel
• Develop a serial program and programming?
Optimize for performance
• Obtain the same amount of
• Real World scenario: computation with multiple
• Run multiple programs cores or threads at low
• Large and Complex problems frequency (Fast)
• Time consuming
• Solution:
• Use parallel machines
• Use Multi-core Machines
• Why Parallel ?
• Reduce the execution time
• Run multiple programs
1. Introduction
• Concurrency
• Condition of a system in
which multiple tasks are
logically active at the same
time ….but they may not
necessarily run in parallel
• Parallelism
• Subset of concurrency
• Condition of a system in
which multiple tasks are
active at the same time and
run in parallel
1. Introduction
• Shared Memory Machines • Distributed Memory Machines
• All processors share the same • Each processor has its own
memory memory
• The variables can be shared or • The variables are Independent
private • Communication by passing
• Communication via shared messages (network)
memory
Multi-Processing
Multi-threading • Difficult to program
• Portable, easy to program and • Scalable
use
• Not very scalable
• MPI based Programming
• OpenMP based Programming
1. OpenMP : API
Open Specification for Multi-Processing (OpenMP)
• Library used to divide computational work in a program and add
parallelism to serial program (create threads)
• An Application Program Interface (API) that is used explicitly direct
multi-threaded, shared memory parallelism
• API Components
• Compiler Directives
• Runtime library routines
• Environment variables
• Standardization
• Jointly defined and endorsed by major computer hardware and software
vendors
1. OpenMP
FORK – JOIN Parallelism
• OpenMP program begin as a single process: the master thread. The master
thread executes sequentially until the first parallel region construct is
encountered.
• When a parallel region is encountered, master thread
– Create a group of threads by FORK.
– Becomes the master of this group of threads and is assigned the thread id 0 within the group.
• The statement in the program that are enclosed by the parallel region
construct are then executed in parallel among these threads.
• JOIN: When the threads complete executing the statement in the parallel region
construct, they synchronize and terminate, leaving only the master thread.
1. OpenMP
• I/O
• OpenMP does not specify parallel I/O.
• It is up to the programmer to ensure that I/O is conducted correctly within
the context of a multithreaded program.
• Memory Model
• Threads can “cache” their data and are not required to maintain exact
consistency with real memory all the time.
• When it is critical that all threads view a shared variable identically, the
programmer is responsible for ensuring that the variable is updated by all
threads as needed. (flush)
1. OpenMP : API
• Architecture
1. OpenMP : API
• OpenMP Language Extensions
2. OpenMP Programming
• Clauses
• Not all the clauses are valid on all directives. The set of clauses that is valid on a
particular directive is described with the directive. Most of the clauses accept a
comma-separated list of list items. All list items appearing in a clause must be
visible.
Hello, world.
#include <stdio.h>
#include <omp.h> OR
Hello, world.
#include <stdio.h>
#include <omp.h> OR
Threadnum =1
int low = N*threadnum/numthreads, Threadnum =0
Numthreads =2 Numthreads =2
high = N*(threadnum+1)/numthreads; Low=50
Low=0
High=50 High=100
for (i=low; i<high; i++)
a[i]=b[i]+c[i]
}
2. OpenMP Programming: Directives – Default(none)
Threadnum =0 Threadnum =1
int low = N*threadnum/numthreads, Numthreads =2
Numthreads =2
high = N*(threadnum+1)/numthreads; Low=0 Low=50
High=50 High=100
for (i=low; i<high; i++)
a[i]=b[i]+c[i]
}
2. OpenMP Programming: Directives - shared
The variable x is private here. The value of x is 10 before parallel region. Since
x is private, the value 10 is not reflected in threads. When the parallel region
is entered, the x contains some garbage value as it is not initialed .
2. OpenMP Programming: Directives - Shared
• OpenMP
• Program Structure
• Directives : parallel , for
• Clauses
• If
• Private
• Shared
• Default(shared/none)
• Firstprivate
• Num_threads
Reference
Text Books and/or Reference Books:
1. Professional CUDA C Programming – John Cheng, Max Grossman, Ty McKercher, 2014
2. B.Wilkinson, M.Allen, ”Parallel Programming: Techniques and Applications Using Networked
Workstations and Parallel Computers”, Pearson Education, 1999
3. I.Foster, ”Designing and building parallel programs”, 2003
4. Parallel Programming in C using OpenMP and MPI – Micheal J Quinn, 2004
5. Introduction to Parallel Programming – Peter S Pacheco, Morgan Kaufmann Publishers,
2011
6. Advanced Computer Architectures: A design approach, Dezso Sima, Terence Fountain, Peter
Kacsuk, 2002
7. Parallel Computer Architecture : A hardware/Software Approach, David E Culler, Jaswinder
Pal Singh Anoop Gupta, 2011 8. Introduction to Parallel Computing, Ananth Grama, Anshul
Gupta, George Karypis, Vipin Kumar, Pearson, 2011
Reference
Acknowledgements
1. Introduction to OpenMP https://www3.nd.edu/~zxu2/acms60212-40212/Lec-12-OpenMP.pdf
2. Introduction to parallel programming for shared memory
Machines https://www.youtube.com/watch?v=LL3TAHpxOig
3. OpenMP Application Program Interface Version 2.5 May 2005
4. OpenMP Application Program Interface Version 5.0 November 2018