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 shared memory (and distributed shared memory) multiprocessors. It extends programming languages (C/C++ and Fortran) by • a set of compiler directives (called pragmas) to express shared memory parallelism. • runtime library routines and environment variables that are used to examine and modify execution parameters. 2 Computational Mathematics, OpenMP , Sharif University Fall 2015
Execution Model OpenMP is based on the fork-join execution model. At the start of an OpenMP program, a single thread (master thread) is executing 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
Execution Model OpenMP is based on the fork-join execution model. At the start of an OpenMP program, a single thread (master thread) is executing • Creating teams of threads Master er Threa ead 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
Execution Model OpenMP is based on the fork-join execution model. At the start of an OpenMP program, a single thread (master thread) is executing • Creating teams of threads • sharing work among threads Work orker er Thre reads ads Master er Threa ead 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
Execution Model OpenMP is based on the fork-join execution model. At the start of an OpenMP program, a single thread (master thread) is executing • Creating teams of threads • sharing work among threads Work orker er Thre reads ads Master er Threa ead Paral alle lel Re Regio ions ns 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
Execution Model OpenMP is based on the fork-join execution model. At the start of an OpenMP program, a single thread (master thread) is executing • Creating teams of threads • sharing work among threads Work orker er Thre reads ads • synchronizing the threads Work orker er Thre reads ads Master er Threa ead Paral alle lel Re Regio ions ns 3 Computational Mathematics, OpenMP , Sharif University Fall 2015
OpenMP Pragma Syntax Most constructs in OpenMP are compiler directives or pragmas. • For C and C++, the pragmas take the form: #pragma omp construct [clause [clause]…] 8 Computational Mathematics, OpenMP , Sharif University Fall 2015
OpenMP Pragma Syntax Most constructs in OpenMP are compiler directives or pragmas. • For C and C++, the pragmas take the form: #pragma omp construct [clause [clause]…] Example: #pragma omp parallel num_threads(4) 8 Computational Mathematics, OpenMP , Sharif University Fall 2015
OpenMP Pragma Syntax Most constructs in OpenMP are compiler directives or pragmas. • For C and C++, the pragmas take the form: #pragma omp construct [clause [clause]…] Example: #pragma omp parallel num_threads(4) Most OpenMP constructs apply to a “ structured block ”. • Structured block: a block of one or more statements with one point of entry at the top and one point of exit at the bottom. 8 Computational Mathematics, OpenMP , Sharif University Fall 2015
Parallel Regions 9 Computational Mathematics, OpenMP , Sharif University Fall 2015
Parallel Regions Defines parallel region over structured block of code #pragma omp parallel { block } 9 Computational Mathematics, OpenMP , Sharif University Fall 2015
Parallel Regions Defines parallel region over structured block of code #pragma omp parallel { block } 9 Computational Mathematics, OpenMP , Sharif University Fall 2015
Parallel Regions Defines parallel region over structured block of code • If the master thread arrives at a parallel directive it spawns some new threads and forms a team of treads. #pragma omp parallel { block } Trea ead d 1 Trea ead d 2 Trea ead d 3 9 Computational Mathematics, OpenMP , Sharif University Fall 2015
Parallel Regions Defines parallel region over structured block of code • If the master thread arrives at a parallel directive it spawns some new threads and forms a team of treads. • At the end of the parallel section the execution is joined again in the single master thread. #pragma omp parallel { block } Trea ead d 1 Trea ead d 2 Trea ead d 3 9 Computational Mathematics, OpenMP , Sharif University Fall 2015
Parallel Regions Defines parallel region over structured block of code • If the master thread arrives at a parallel directive it spawns some new threads and forms a team of treads. • At the end of the parallel section the execution is joined again in the single master thread. • There is an implicite barrier at this point. #pragma omp parallel { block } Trea ead d 1 Trea ead d 2 Trea ead d 3 9 Computational Mathematics, OpenMP , Sharif University Fall 2015
How Many Threads? Number of openMP threads can be set using: • Environmental variable OMP_NUM_THREADS • Runtime function omp_set_num_threads(n) 15 Computational Mathematics, OpenMP , Sharif University Fall 2015
How Many Threads? Number of openMP threads can be set using: • Environmental variable OMP_NUM_THREADS • Runtime function omp_set_num_threads(n) Other useful function to get information about threads: • Runtime function omp_get_num_threads() • Returns number of threads in parallel region • Returns 1 if called outside parallel region 15 Computational Mathematics, OpenMP , Sharif University Fall 2015
How Many Threads? Number of openMP threads can be set using: • Environmental variable OMP_NUM_THREADS • Runtime function omp_set_num_threads(n) Other useful function to get information about threads: • Runtime function omp_get_num_threads() • Returns number of threads in parallel region • Returns 1 if called outside parallel region • Runtime function omp_get_thread_num() • Returns id of thread in team • Value between [0,n-1] // where n = #threads • Master thread always has id 0 15 Computational Mathematics, OpenMP , Sharif University Fall 2015
First OpenMP Example #include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \ n”, ID); } } 7 Computational Mathematics, OpenMP , Sharif University Fall 2015
First OpenMP Example OpenMP include file #include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \ n”, ID); } } 7 Computational Mathematics, OpenMP , Sharif University Fall 2015
First OpenMP Example OpenMP include file #include “omp.h” void main() { Parallel region with default #pragma omp parallel number of threads { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \ n”, ID); } } 7 Computational Mathematics, OpenMP , Sharif University Fall 2015
First OpenMP Example OpenMP include file #include “omp.h” void main() { Parallel region with default #pragma omp parallel number of threads { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); Runtime library function to printf(“ world(%d) \ n”, ID); return a thread ID. } } 7 Computational Mathematics, OpenMP , Sharif University Fall 2015
First OpenMP Example OpenMP include file #include “omp.h” void main() { Parallel region with default #pragma omp parallel number of threads { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); Runtime library function to printf(“ world(%d) \ n”, ID); return a thread ID. } } End of the Parallel region 7 Computational Mathematics, OpenMP , Sharif University Fall 2015
First OpenMP Example OpenMP include file #include “omp.h” void main() { Parallel region with default #pragma omp parallel number of threads hello(1) hello(0) world(1) { int ID = omp_get_thread_num(); world(0) printf(“ hello(%d) ”, ID); hello (3) hello(2) world(3) Runtime library function to printf(“ world(%d) \ n”, ID); return a thread ID. world(2) } } End of the Parallel region 7 Computational Mathematics, OpenMP , Sharif University Fall 2015
A first OpenMP example Each thread executes a copy of the code within the structured block #include “omp.h” void main() { Runtime function to request a certain number of threads num_threads(4); omp_set_ #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \ n”, ID); } } 13 Computational Mathematics, OpenMP , Sharif University Fall 2015
Shared Memory Model int ID = omp_get_thread_num(); THREADS = omp_get_num_threads(); 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 24 Computational Mathematics, OpenMP , Sharif University Fall 2015
Recommend
More recommend