Lecture 06 - OpenMP
Lecture 06 - OpenMP
Lecture 06 - OpenMP
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.
Simple Example to Run a program Parallel
#include<omp.h>
int main(){
#pragma omp parallel
printf("hello %d\n",id);
}
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! This Process is executed by %d\n",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 %d\n",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 Word\n");}
}
}
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