OpenMP
Department of Computer Engineering Sharif University of Technology e-mail: azad@sharif.edu
Instructor
OpenMP Instructor PanteA Zardoshti Department of Computer - - PowerPoint PPT Presentation
OpenMP Instructor PanteA Zardoshti Department of Computer Engineering Sharif University of Technology e-mail: azad@sharif.edu What Is OpenMP? OpenMP in an application programming interface that provides a parallel programming model for
Department of Computer Engineering Sharif University of Technology e-mail: azad@sharif.edu
Instructor
2
provides a parallel programming model for shared memory (and distributed shared memory) multiprocessors. It extends programming languages (C/C++ and Fortran) by
parallelism.
examine and modify execution parameters.
What Is OpenMP?
Computational Mathematics, OpenMP , Sharif University Fall 2015
3
thread) is executing
Execution Model
Computational Mathematics, OpenMP , Sharif University Fall 2015
3
thread) is executing
Execution Model
Master er Threa ead
Computational Mathematics, OpenMP , Sharif University Fall 2015
3
thread) is executing
Execution Model
Master er Threa ead
Work
er Thre reads ads
Computational Mathematics, OpenMP , Sharif University Fall 2015
3
thread) is executing
Execution Model
Master er Threa ead
Paral alle lel Re Regio ions ns Work
er Thre reads ads
Computational Mathematics, OpenMP , Sharif University Fall 2015
3
thread) is executing
Execution Model
Master er Threa ead
Paral alle lel Re Regio ions ns Work
er Thre reads ads Work
er Thre reads ads
Computational Mathematics, OpenMP , Sharif University Fall 2015
8
#pragma omp construct [clause [clause]…]
OpenMP Pragma Syntax
Computational Mathematics, OpenMP , Sharif University Fall 2015
8
#pragma omp construct [clause [clause]…]
#pragma omp parallel num_threads(4)
OpenMP Pragma Syntax
Computational Mathematics, OpenMP , Sharif University Fall 2015
8
#pragma omp construct [clause [clause]…]
#pragma omp parallel num_threads(4)
entry at the top and one point of exit at the bottom.
OpenMP Pragma Syntax
Computational Mathematics, OpenMP , Sharif University Fall 2015
9
Parallel Regions
Computational Mathematics, OpenMP , Sharif University Fall 2015
9
Parallel Regions
#pragma omp parallel { block }
Computational Mathematics, OpenMP , Sharif University Fall 2015
9
Parallel Regions
#pragma omp parallel { block }
Computational Mathematics, OpenMP , Sharif University Fall 2015
9
some new threads and forms a team of treads.
Parallel Regions
#pragma omp parallel { block }
Trea ead d 1 Trea ead d 2 Trea ead d 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
9
some new threads and forms a team of treads.
again in the single master thread.
Parallel Regions
#pragma omp parallel { block }
Trea ead d 1 Trea ead d 2 Trea ead d 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
9
some new threads and forms a team of treads.
again in the single master thread.
Parallel Regions
#pragma omp parallel { block }
Trea ead d 1 Trea ead d 2 Trea ead d 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
15
How Many Threads?
Computational Mathematics, OpenMP , Sharif University Fall 2015
15
How Many Threads?
Computational Mathematics, OpenMP , Sharif University Fall 2015
15
How Many Threads?
Computational Mathematics, OpenMP , Sharif University Fall 2015
7
#include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }
First OpenMP Example
Computational Mathematics, OpenMP , Sharif University Fall 2015
7
#include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }
First OpenMP Example
OpenMP include file
Computational Mathematics, OpenMP , Sharif University Fall 2015
7
#include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }
First OpenMP Example
OpenMP include file Parallel region with default number of threads
Computational Mathematics, OpenMP , Sharif University Fall 2015
7
#include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }
First OpenMP Example
OpenMP include file Parallel region with default number of threads Runtime library function to return a thread ID.
Computational Mathematics, OpenMP , Sharif University Fall 2015
7
#include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }
First OpenMP Example
OpenMP include file Parallel region with default number of threads Runtime library function to return a thread ID. End of the Parallel region
Computational Mathematics, OpenMP , Sharif University Fall 2015
7
#include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }
First OpenMP Example
OpenMP include file Parallel region with default number of threads Runtime library function to return a thread ID. End of the Parallel region
hello(1) hello(0) world(1) world(0) hello (3) hello(2) world(3) world(2)
Computational Mathematics, OpenMP , Sharif University Fall 2015
13
#include “omp.h” void main() { num_threads(4); #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); }
A first OpenMP example
Runtime function to request a certain number of threads
Computational Mathematics, OpenMP , Sharif University Fall 2015
24
Shared Memory Model
Trea ead d 1 Trea ead d 2 Trea ead d 3 Trea ead d 4
Var : ID Var: THREA EADS DS Shared d Memory
Computational Mathematics, OpenMP , Sharif University Fall 2015
24
same time). This can lead to a race condition. Different runs of same program might give different results because of race conditions.
Shared Memory Model
Trea ead d 1 Trea ead d 2 Trea ead d 3 Trea ead d 4
Var : ID Var: THREA EADS DS Shared d Memory
Computational Mathematics, OpenMP , Sharif University Fall 2015
25
within an OpenMP block.This is done using the following OpenMP clauses:
Data scoping clauses
Computational Mathematics, OpenMP , Sharif University Fall 2015
25
within an OpenMP block.This is done using the following OpenMP clauses:
Data scoping clauses
Computational Mathematics, OpenMP , Sharif University Fall 2015
25
within an OpenMP block.This is done using the following OpenMP clauses:
#pragma omp parallel private(a,b,c)
Data scoping clauses
Computational Mathematics, OpenMP , Sharif University Fall 2015
25
within an OpenMP block.This is done using the following OpenMP clauses:
#pragma omp parallel private(a,b,c)
include index variables (Fortran, C/C++) and variables declared inside parallel region (C/C++)
Data scoping clauses
Computational Mathematics, OpenMP , Sharif University Fall 2015
26
Data scoping clauses(cont.)
Computational Mathematics, OpenMP , Sharif University Fall 2015
26
with the original value (before the omp region started) of 'x‘
Data scoping clauses(cont.)
Computational Mathematics, OpenMP , Sharif University Fall 2015
26
with the original value (before the omp region started) of 'x‘
last work sharing will be copied to shared version. To be used with for Directive.
Data scoping clauses(cont.)
Computational Mathematics, OpenMP , Sharif University Fall 2015
26
with the original value (before the omp region started) of 'x‘
last work sharing will be copied to shared version. To be used with for Directive.
Data scoping clauses(cont.)
Computational Mathematics, OpenMP , Sharif University Fall 2015
27
Example
Int a; Void foo ( ) { int b , c;
#pragma omp parallel private ( b ) {
int d ;
#pragma omp task parallel {
int e ; a = b = c = d = e =
} } }
Computational Mathematics, OpenMP , Sharif University Fall 2015
27
Example
Int a; Void foo ( ) { int b , c;
#pragma omp parallel private ( b ) {
int d ;
#pragma omp task parallel {
int e ; a = b = c = d = e =
} } }
shared
Computational Mathematics, OpenMP , Sharif University Fall 2015
27
Example
Int a; Void foo ( ) { int b , c;
#pragma omp parallel private ( b ) {
int d ;
#pragma omp task parallel {
int e ; a = b = c = d = e =
} } }
shared firstprivate
Computational Mathematics, OpenMP , Sharif University Fall 2015
27
Example
Int a; Void foo ( ) { int b , c;
#pragma omp parallel private ( b ) {
int d ;
#pragma omp task parallel {
int e ; a = b = c = d = e =
} } }
shared firstprivate shared
Computational Mathematics, OpenMP , Sharif University Fall 2015
27
Example
Int a; Void foo ( ) { int b , c;
#pragma omp parallel private ( b ) {
int d ;
#pragma omp task parallel {
int e ; a = b = c = d = e =
} } }
shared firstprivate shared firstprivate
Computational Mathematics, OpenMP , Sharif University Fall 2015
27
Example
Int a; Void foo ( ) { int b , c;
#pragma omp parallel private ( b ) {
int d ;
#pragma omp task parallel {
int e ; a = b = c = d = e =
} } }
shared firstprivate shared firstprivate private
Computational Mathematics, OpenMP , Sharif University Fall 2015
28
want is to share work among all threads so we can solve our problems faster.
#pragma omp parallel { for (i=0;i<100;i++) A(i) = A(i) + B }
Work Sharing
Partition the iteration space manually, every thread computes N/num iterations
Computational Mathematics, OpenMP , Sharif University Fall 2015
29
The only thing needed is to add the #pragma omp for
#pragma omp parallel { #pragma omp for for (i=0;i<100;i++) { A(i) = A(i) + B } }
#pragma omp parallel for
Work Sharing(cont.)
Computational Mathematics, OpenMP , Sharif University Fall 2015
29
The only thing needed is to add the #pragma omp for
for (i=0;i<100;i++) { A(i) = A(i) + B }
#pragma omp parallel for
Work Sharing(cont.)
Computational Mathematics, OpenMP , Sharif University Fall 2015
30
for (i = 0; i < n; i++) c[i] = a[i] + b[i];
Example
Computational Mathematics, OpenMP , Sharif University Fall 2015
30
#pragma omp parallel for shared(n, a, b, c) \ private(i) for (i = 0; i < n; i++) c[i] = a[i] + b[i];
Example
Computational Mathematics, OpenMP , Sharif University Fall 2015
31
Example Parallel Execution
Computational Mathematics, OpenMP , Sharif University Fall 2015