Open
Multiprocessing
(OpenMP)
Nazia Shahzadi
Lecturer (FCSE)
1
OpenMP
Open Multi-processing (OpenMP) is a technique of
parallelizing a section(s) of C/C++/Fortran code.
OpenMP is also seen as an extension to C/C++/Fortran
languages by adding parallelizing features to them.
OpenMP uses a portable, scalable model that gives
programmers a simple and flexible interface for
developing parallel applications for platforms that
ranges from the normal desktop computer to the high-
end supercomputers.
Parallel Programming Standards
• Thread Libraries (Runtime Library
Routines)
- Win32 API / Posix threads
• Compiler Directives OUR FOCUS
- OpenMP (Shared memory
programming)
• Message Passing Libraries
- MPI (Distributed memory
programming)
• Environment Variables
ECE 563 Programming Parallel Machines 4
Shared Memory Parallel
Programming in the Multi-Core Era
• Desktop and Laptop
– 2, 4, 8 cores and …?
• A single node in
distributed memory
clusters
– Steele cluster node:
2 8 (16) cores
• Shared memory
hardware
Accelerators ECE 563 Programming Parallel Machines 5
OpenMP:
Some syntax details to get us started
• Most of the constructs in OpenMP are compiler
directives or pragmas.
– For C and C++, the pragmas take the form:
#pragma omp construct [clause [clause]…]
– For Fortran, the directives take one of the forms:
C$OMP construct [clause [clause]…]
!$OMP construct [clause [clause]…]
*$OMP construct [clause [clause]…]
• Include files
#include “omp.h”
6
How is OpenMP typically used?
• “Hello World” Program using OpenMP
Parallel Program
Sequential Program
#include “omp.h”
#include<stdio.h> #include“stdio.h”
Int main(){ int main() {
#pragma omp parallel
printf("Hello, World! {
(Sequential)\n"); int thread_id =
omp_get_thread_num();
return 0; printf(“HELLO,
} CS417! Thread ID: %d\
n", thread_id);}
return 0; 7
}
OpenMP Runtime Functions
Some syntax details to get us started
• #include <omp.h>
• Needed to access open declarations of the OpenMP
runtime library routines
• Int omp_get_num_threads();
• Returns the number of threads in the current team
• Int omp_get_thread_num();
• Returns the thread number in the current team
8
Output of Both Programs
• “Hello World” Program using OpenMP
Sequential Program Parallel Program
Hello, World! (Sequential)
9
How is OpenMP typically used?
• OpenMP is usually used to parallelize loops:
• Find your most time consuming loops.
• Split them up between threads.
Parallel Program
Sequential Program
#include “omp.h”
void main() void main()
{ {
int i, k, N=1000; int i, k,
double A[N], B[N], C[N]; N=1000;
for (i=0; i<N; i++) { double A[N],
A[i] = B[i] + k*C[i] B[N], C[N];
} omp_set_num_t
} hreads(4);
#pragma omp parallel for
for (i=0; i<N; i++) { 10
A[i] = B[i] + k*C[i];
}
}
How is OpenMP typically used?
(Cont.)
• Single Program Multiple Data (SPMD)
Parallel Program
#include “omp.h”
void main()
{
int i, k,
N=1000;
double A[N],
B[N], C[N];
#pragma omp parallel for
for (i=0; i<N; i++) {
A[i] = B[i] + k*C[i];
}
}
11
How is OpenMP typically used?
(Cont.)
• Single Program Multiple Data (SPMD)
Thread 0
Thread 1
void main() Thread 2
{ void main() Thread 3
int i, k, N=1000; void main()
int i,C[N];
, B[N], k, N =1000; void main()
{ double
int i,C[N];
, B[N], k, N =1000;
A[N] lb = { double
int i,C[N];
, B[N], k, N=1000;
0; A[N] lb = { double
double A[N], B[N], C[N];
ub = 250; <ub;i++)
250; { A[N] lb =
lb = 750;
for (i=lb;ii] ub = 500; <ub;i++)
+ k*C[i]; 500; {
ub = 1000;
A[i] = for (i=lb;i i] ub = 750; <ub;i++) {
+ k*C[i];
} for (i=lb;i<ub;i++)
B[ A[i] = for (i=lb;i i] + k*C[i];
} { A[i] = B[i] +
} B[ A[i] =
} k*C[i];
} B[
} }
}
12
Execution Model
OpenMP Fork-and-Join model
printf(“program begin\n”);
N = 1000; Serial
#pragma omp parallel for
for (i=0; i<N; i++) Parallel
A[i] = B[i] + C[i];
M = 500; Serial
#pragma omp parallel for
for (j=0; j<M; j++) Parallel
p[j] = q[j] – r[j];
printf(“program done\n”); Serial
14
Execution Model
(Cont….)
OpenMP Language
Extensions
Installation of OpenMP
in Windows
You can follow this link to install the OpenMP
environment on Windows
https://www.youtube.com/watch?
v=r2B5nz4n8Ds&pp=ygURT3Blbk1QIGluIHdpbmRvd3M%3D
Resources
http://www.openmp.org
http://openmp.org/wp/resources
18