openmp
play

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


  1. OpenMP Instructor PanteA Zardoshti Department of Computer Engineering Sharif University of Technology e-mail: azad@sharif.edu

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. Parallel Regions 9 Computational Mathematics, OpenMP , Sharif University Fall 2015

  12. Parallel Regions  Defines parallel region over structured block of code #pragma omp parallel { block } 9 Computational Mathematics, OpenMP , Sharif University Fall 2015

  13. Parallel Regions  Defines parallel region over structured block of code #pragma omp parallel { block } 9 Computational Mathematics, OpenMP , Sharif University Fall 2015

  14. 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

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend