SlideShare a Scribd company logo
3
Most read
14
Most read
20
Most read
Parallel and distributed
computing
OUTLINE
Introduction to OpenMP
Introduction to OpenMP
•OpenMP Is open Multi-Processing
•Open is free i.e. freeware
•this is not hardware or not a software even not a tool but its
is an API, i.e. it is an library that is used with some
programming languages
•OpenMP is available for C, C++ and FORTRAN Languages
•OpenMP is for shared memory model, create multiple
threads to share the memory of main process.
Introduction to OpenMP
•It is managed by consortium OpenMP Architecture Review
Board (OpenMP ARB) Which is formed by several
companies like AMD, Intel, IBM, HP, Nvidia, Oracle etc.
OpenMP
•Shared memory, thread-based parallelism
• Shared memory process consists of multiple threads
•Explicit Programming
• Programmer has full control over parallelization. OpenMP is not
an automatic parallel programming model.
•Compiler Directive based
• Parallelism is specified through the use of compiler directives
which are embedded in the source code.
OpenMP
•Each process starts with one main thread this thread is
called as master thread in OpenMP.
• For particular block of code, we create multiple threads
along with this master thread, theses extra threads other
than master are called slave Thread.
OpenMP
It is also called fork-join Model, because all slave threads after
execution get joined to the master thread i.e. Process starts
with single master threads and ends with single master
thread.
Parallel Memory Architecture
•Shared Memory
•Distributed Memory
•Hybrid
Header file use in C,C++ Language
For C, C++ we need to include “omp.h” as header file.
OpenMP has directives that allow programmer to:
•specify the parallel region
•specify whether the variables in the parallel
section are private or shared
•specify how/if the threads are synchronized
•specify how to parallelize loops
•specify how the works is divided between
threads (scheduling)
Simple Example to create Threads
#include<omp.h>
int main(){
#pragma omp parallel
{
int id = omp_get_thread_num();
printf("hello %dn",id);
}
}
• By default the number of threads created is equal to the numbers of
processors cores
Creating required number of threads
#include<omp.h>
int main(){
#pragma omp parallel num_threads(7)
{
int id = omp_get_thread_num();
printf("hello %dn",id);
}
}
Creating required number of threads
• If you want to create specific number of threads, use
num_threads() and a number indicating number of
threads to be created should be passed as argument
to num_threads().
• In previous example, seven threads will be created
each one will be responsible for printing the required
task.
Creating multiple threads using for loop
int main(){
#pragma omp parallel
{
int id = omp_get_thread_num();
for(int i=0;i<3;i++)
{
printf(“Hellow Wordn");}
}
}
Creating multiple threads using for loop
In the previous snippet, since we are not mentioning the number of
threads, number of threads will be equl to the number of cores.
This for loop will have iterations which are done by these many number
of threads in PC
Allocating different work to different threads
In OpenMP, we can allocate different work to different thread by using
SECTIONS e.g.
Allocating different work to different threads
#include<omp.h>
int main(){
#pragma omp parallel sections num_threads(3)
{
#pragma omp section
{ printf("hello word onen"); }
#pragma omp section
{ printf("hello word twon"); }
#pragma omp section
{ printf("hello word threen"); }
}
In this Example; we have create three threads by mentioning num_threads(3) and each thread is
printing different line.
How do threads interact
• OpenMP is a multithreading, shared address model and threads
communicate by sharing variables
• Un intended sharing of data may cause race conditions
• So to control race conditions we use synchronization to protect data
conflicts
• However synchronization is expensive so we many need to change
how data is accessed to minimized the need for synchornization
Synchronization
• Critical: enclosed code block will be executed by only one
thread at a time, not simultaneously. Often use to protect
shared data from race condition.
• Atomic: memory update (write, read) in next instruction will
be performed atomically. Doesn’t make entre statement
atomic but only memory update is atomic. Better
performance than critical
• Ordered: structured block is executed in order in which
iterations would be executed in sequential loop
Synchronization
• Barrier: each thread waits until all of other threads of a team
have reached this point. A work-sharing construct has an
implicit barrier synchronization at the end.
• nowail: specifies that threads completing assigned work can
proceed without waiting for all threads in the team to finish.
In the absence of this clause, threads encounter a barrier
synchronization at the end of the work sharing construct.
Synchronization
• Synchronization is used to impose order constraints and to protect
access to shared data
• #pragma omp critical: this directive identifies section of code that
must be executed by single thread at a time
• At the start of critical region it is identified by a given name until no
other thread in the program is executing a critical region with the
same name.
Synchronize Threads in OpenMP
In OpenMP we can avoid race condition among threads by using
preprocessor directive “pragma omp critical”
int main()
{ #pragma omp parallel
{ #pragma omp critical
{ int x=x+1; }}}
We have created 5 threads, here only one thread will increment value
of x at one time.
List of some functions of OpenMP Library
• Omp_set_num_threads(4): request certain number of threads
• Omp_get_thread_num(): returning a thread ID
• #pragma omp parallel num threads(3): clause to run parallel with specific
number of threads

More Related Content

Similar to Parallel and Distributed Computing Chapter 5 (20)

PPT
Lecture6
tt_aljobory
 
PPTX
Intro to OpenMP
jbp4444
 
ODP
openmp
Neel Bhad
 
PDF
Introduction to OpenMP (Performance)
Akhila Prabhakaran
 
PPT
Parllelizaion
Vivek Kantariya
 
PDF
Omp tutorial cpugpu_programming_cdac
Ganesan Narayanasamy
 
PPT
Nbvtalkataitamimageprocessingconf
Nagasuri Bala Venkateswarlu
 
ODP
OpenMp
Neel Bhad
 
PPT
openmp.ppt
FAfazi1
 
PPT
openmp.ppt
GopalPatidar13
 
PPTX
openmp final2.pptx
GopalPatidar13
 
PPTX
Parallel Computing - openMP -- Lecture 5
arnabsahuyspm
 
PPT
Lecture8
tt_aljobory
 
PPTX
25-MPI-OpenMP.pptx
GopalPatidar13
 
PPT
OpenMP
ZongYing Lyu
 
PPT
OpenMP-Quinn17_L4bOpen <MP_Open MP_Open MP
Balasubramanian699229
 
PDF
Multi-Processor computing with OpenMP
Stefan Coetzee
 
PDF
Open MP cheet sheet
Piyush Mittal
 
PPTX
Shared Memory Programming with Pthreads and OpenMP
Dilum Bandara
 
PDF
Intel parallel programming
Nirma University
 
Lecture6
tt_aljobory
 
Intro to OpenMP
jbp4444
 
openmp
Neel Bhad
 
Introduction to OpenMP (Performance)
Akhila Prabhakaran
 
Parllelizaion
Vivek Kantariya
 
Omp tutorial cpugpu_programming_cdac
Ganesan Narayanasamy
 
Nbvtalkataitamimageprocessingconf
Nagasuri Bala Venkateswarlu
 
OpenMp
Neel Bhad
 
openmp.ppt
FAfazi1
 
openmp.ppt
GopalPatidar13
 
openmp final2.pptx
GopalPatidar13
 
Parallel Computing - openMP -- Lecture 5
arnabsahuyspm
 
Lecture8
tt_aljobory
 
25-MPI-OpenMP.pptx
GopalPatidar13
 
OpenMP
ZongYing Lyu
 
OpenMP-Quinn17_L4bOpen <MP_Open MP_Open MP
Balasubramanian699229
 
Multi-Processor computing with OpenMP
Stefan Coetzee
 
Open MP cheet sheet
Piyush Mittal
 
Shared Memory Programming with Pthreads and OpenMP
Dilum Bandara
 
Intel parallel programming
Nirma University
 

More from AbdullahMunir32 (20)

PDF
Mobile Application Development-Lecture 15 & 16.pdf
AbdullahMunir32
 
PDF
Mobile Application Development-Lecture 13 & 14.pdf
AbdullahMunir32
 
PDF
Mobile Application Development -Lecture 11 & 12.pdf
AbdullahMunir32
 
PDF
Mobile Application Development -Lecture 09 & 10.pdf
AbdullahMunir32
 
PDF
Mobile Application Development -Lecture 07 & 08.pdf
AbdullahMunir32
 
PDF
Mobile Application Development Lecture 05 & 06.pdf
AbdullahMunir32
 
PDF
Mobile Application Development-Lecture 03 & 04.pdf
AbdullahMunir32
 
PDF
Mobile Application Development-Lecture 01 & 02.pdf
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 13
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 12
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 11
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 10
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 9
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 8
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 7
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 6
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 4
AbdullahMunir32
 
PDF
Parallel and Distributed Computing chapter 3
AbdullahMunir32
 
PDF
Parallel and Distributed Computing Chapter 2
AbdullahMunir32
 
PDF
Parallel and Distributed Computing chapter 1
AbdullahMunir32
 
Mobile Application Development-Lecture 15 & 16.pdf
AbdullahMunir32
 
Mobile Application Development-Lecture 13 & 14.pdf
AbdullahMunir32
 
Mobile Application Development -Lecture 11 & 12.pdf
AbdullahMunir32
 
Mobile Application Development -Lecture 09 & 10.pdf
AbdullahMunir32
 
Mobile Application Development -Lecture 07 & 08.pdf
AbdullahMunir32
 
Mobile Application Development Lecture 05 & 06.pdf
AbdullahMunir32
 
Mobile Application Development-Lecture 03 & 04.pdf
AbdullahMunir32
 
Mobile Application Development-Lecture 01 & 02.pdf
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 13
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 12
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 11
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 10
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 9
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 8
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 7
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 6
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 4
AbdullahMunir32
 
Parallel and Distributed Computing chapter 3
AbdullahMunir32
 
Parallel and Distributed Computing Chapter 2
AbdullahMunir32
 
Parallel and Distributed Computing chapter 1
AbdullahMunir32
 
Ad

Recently uploaded (20)

PPTX
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
DOCX
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PDF
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PDF
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
PPTX
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
PPTX
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
PDF
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
PDF
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
PDF
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
PDF
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
PPTX
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
PPTX
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
PDF
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
 
PPTX
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
PDF
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
PDF
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
PPTX
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
PDF
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
Lesson 1 Cell (Structures, Functions, and Theory).pptx
marvinnbustamante1
 
MUSIC AND ARTS 5 DLL MATATAG LESSON EXEMPLAR QUARTER 1_Q1_W1.docx
DianaValiente5
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
Nanotechnology and Functional Foods Effective Delivery of Bioactive Ingredien...
rmswlwcxai8321
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
Gladiolous Cultivation practices by AKL.pdf
kushallamichhame
 
Parsing HTML read and write operations and OS Module.pptx
Ramakrishna Reddy Bijjam
 
Iván Bornacelly - Presentation of the report - Empowering the workforce in th...
EduSkills OECD
 
Learning Styles Inventory for Senior High School Students
Thelma Villaflores
 
COM and NET Component Services 1st Edition Juval Löwy
kboqcyuw976
 
Wikinomics How Mass Collaboration Changes Everything Don Tapscott
wcsqyzf5909
 
Supply Chain Security A Comprehensive Approach 1st Edition Arthur G. Arway
rxgnika452
 
Practice Gardens and Polytechnic Education: Utilizing Nature in 1950s’ Hu...
Lajos Somogyvári
 
SYMPATHOMIMETICS[ADRENERGIC AGONISTS] pptx
saip95568
 
Rapid Mathematics Assessment Score sheet for all Grade levels
DessaCletSantos
 
PLANNING A HOSPITAL AND NURSING UNIT.pptx
PRADEEP ABOTHU
 
Cooperative wireless communications 1st Edition Yan Zhang
jsphyftmkb123
 
TechSoup Microsoft Copilot Nonprofit Use Cases and Live Demo - 2025.06.25.pdf
TechSoup
 
Natural Language processing using nltk.pptx
Ramakrishna Reddy Bijjam
 
Quiz Night Live May 2025 - Intra Pragya Online General Quiz
Pragya - UEM Kolkata Quiz Club
 
Ad

Parallel and Distributed Computing Chapter 5

  • 3. Introduction to OpenMP •OpenMP Is open Multi-Processing •Open is free i.e. freeware •this is not hardware or not a software even not a tool but its is an API, i.e. it is an library that is used with some programming languages •OpenMP is available for C, C++ and FORTRAN Languages •OpenMP is for shared memory model, create multiple threads to share the memory of main process.
  • 4. Introduction to OpenMP •It is managed by consortium OpenMP Architecture Review Board (OpenMP ARB) Which is formed by several companies like AMD, Intel, IBM, HP, Nvidia, Oracle etc.
  • 5. OpenMP •Shared memory, thread-based parallelism • Shared memory process consists of multiple threads •Explicit Programming • Programmer has full control over parallelization. OpenMP is not an automatic parallel programming model. •Compiler Directive based • Parallelism is specified through the use of compiler directives which are embedded in the source code.
  • 6. OpenMP •Each process starts with one main thread this thread is called as master thread in OpenMP. • For particular block of code, we create multiple threads along with this master thread, theses extra threads other than master are called slave Thread.
  • 7. OpenMP It is also called fork-join Model, because all slave threads after execution get joined to the master thread i.e. Process starts with single master threads and ends with single master thread.
  • 8. Parallel Memory Architecture •Shared Memory •Distributed Memory •Hybrid
  • 9. Header file use in C,C++ Language For C, C++ we need to include “omp.h” as header file.
  • 10. OpenMP has directives that allow programmer to: •specify the parallel region •specify whether the variables in the parallel section are private or shared •specify how/if the threads are synchronized •specify how to parallelize loops •specify how the works is divided between threads (scheduling)
  • 11. Simple Example to create Threads #include<omp.h> int main(){ #pragma omp parallel { int id = omp_get_thread_num(); printf("hello %dn",id); } }
  • 12. • By default the number of threads created is equal to the numbers of processors cores
  • 13. Creating required number of threads #include<omp.h> int main(){ #pragma omp parallel num_threads(7) { int id = omp_get_thread_num(); printf("hello %dn",id); } }
  • 14. Creating required number of threads • If you want to create specific number of threads, use num_threads() and a number indicating number of threads to be created should be passed as argument to num_threads(). • In previous example, seven threads will be created each one will be responsible for printing the required task.
  • 15. Creating multiple threads using for loop int main(){ #pragma omp parallel { int id = omp_get_thread_num(); for(int i=0;i<3;i++) { printf(“Hellow Wordn");} } }
  • 16. Creating multiple threads using for loop In the previous snippet, since we are not mentioning the number of threads, number of threads will be equl to the number of cores. This for loop will have iterations which are done by these many number of threads in PC
  • 17. Allocating different work to different threads In OpenMP, we can allocate different work to different thread by using SECTIONS e.g.
  • 18. Allocating different work to different threads #include<omp.h> int main(){ #pragma omp parallel sections num_threads(3) { #pragma omp section { printf("hello word onen"); } #pragma omp section { printf("hello word twon"); } #pragma omp section { printf("hello word threen"); } } In this Example; we have create three threads by mentioning num_threads(3) and each thread is printing different line.
  • 19. How do threads interact • OpenMP is a multithreading, shared address model and threads communicate by sharing variables • Un intended sharing of data may cause race conditions • So to control race conditions we use synchronization to protect data conflicts • However synchronization is expensive so we many need to change how data is accessed to minimized the need for synchornization
  • 20. Synchronization • Critical: enclosed code block will be executed by only one thread at a time, not simultaneously. Often use to protect shared data from race condition. • Atomic: memory update (write, read) in next instruction will be performed atomically. Doesn’t make entre statement atomic but only memory update is atomic. Better performance than critical • Ordered: structured block is executed in order in which iterations would be executed in sequential loop
  • 21. Synchronization • Barrier: each thread waits until all of other threads of a team have reached this point. A work-sharing construct has an implicit barrier synchronization at the end. • nowail: specifies that threads completing assigned work can proceed without waiting for all threads in the team to finish. In the absence of this clause, threads encounter a barrier synchronization at the end of the work sharing construct.
  • 22. Synchronization • Synchronization is used to impose order constraints and to protect access to shared data • #pragma omp critical: this directive identifies section of code that must be executed by single thread at a time • At the start of critical region it is identified by a given name until no other thread in the program is executing a critical region with the same name.
  • 23. Synchronize Threads in OpenMP In OpenMP we can avoid race condition among threads by using preprocessor directive “pragma omp critical” int main() { #pragma omp parallel { #pragma omp critical { int x=x+1; }}} We have created 5 threads, here only one thread will increment value of x at one time.
  • 24. List of some functions of OpenMP Library • Omp_set_num_threads(4): request certain number of threads • Omp_get_thread_num(): returning a thread ID • #pragma omp parallel num threads(3): clause to run parallel with specific number of threads