parallel programming using openmp
play

Parallel programming using OpenMP Computer Architecture J. Daniel - PowerPoint PPT Presentation

Parallel programming using OpenMP Parallel programming using OpenMP Computer Architecture J. Daniel Garca Snchez (coordinator) David Expsito Singh Francisco Javier Garca Blas ARCOS Group Computer Science and Engineering Department


  1. Parallel programming using OpenMP Parallel programming using OpenMP Computer Architecture J. Daniel García Sánchez (coordinator) David Expósito Singh Francisco Javier García Blas ARCOS Group Computer Science and Engineering Department University Carlos III of Madrid cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 1/49

  2. Parallel programming using OpenMP Introduction Introduction 1 Threads in OpenMP 2 3 Synchronization Parallel loops 4 Synchronize with master 5 6 Data sharing Sections and scheduling 7 Conclusion 8 cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 2/49

  3. Parallel programming using OpenMP Introduction What is OpenMP? It is an language extension for expressing parallel applications in shared memory systems. Components : Compiler directives. Library functions. Environment variables. Simplifies the way of writing parallel programs. Mappings for FORTRAN, C and C++. cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 3/49

  4. Parallel programming using OpenMP Introduction Constructs Directives: #pragma omp directive [clause] Example : Setup the number of threads. #pragma omp parallel num_threads(4) Library functions: #include <omp.h> // Include to call OpenMP API functions Example : Get the number of threads in use. int n = omp_get_num_threads(); cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 4/49

  5. Parallel programming using OpenMP Introduction Exercise 1: Sequential ex1seq.cpp #include <iostream> int main() { using namespace std; int id = 0; cout << "Hello(" << id << ") "; cout << "World(" << id << ")"; return 0; } Print to standard output. cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 5/49

  6. Parallel programming using OpenMP Introduction Exercise 1: Parallel Compiler flags: ex1par.cpp gcc : -fopenmp Intel Linux : -openmp #include <iostream> Intel Windows : #include <omp.h> /Qopenmp int main() { Microsoft Visual using namespace std; Studio : /openmp #pragma omp parallel { int id = omp_get_thread_num(); cout << "Hola(" << id << ") "; cout << "Mundo(" << id << ")"; } return 0; } cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 6/49

  7. Parallel programming using OpenMP Introduction Exercise 1 Goal : Verify you have a working environment. Activities : 1 Compile sequential version and run. 2 Compile parallel version and run. 3 Add a call to function omp_get_num_threads() to print the number of threads: a) Before the pragma . b) Just after pragma . c) Within the block. d) Before exiting the program, but outside the block. cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 7/49

  8. Parallel programming using OpenMP Introduction Observations A model for multi-threaded shared memory. Communication through shared variables. Accidental sharing → race conditions . Result depending on threads scheduling. Avoiding race conditions. Synchronize to avoid conflicts. Cost of synchronizations. Modify access pattern. Minimize needed synchronizations. cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 8/49

  9. Parallel programming using OpenMP Threads in OpenMP Introduction 1 Threads in OpenMP 2 3 Synchronization Parallel loops 4 Synchronize with master 5 6 Data sharing Sections and scheduling 7 Conclusion 8 cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 9/49

  10. Parallel programming using OpenMP Threads in OpenMP Fork-join parallelism Sequential application with parallel sections: Master thread : Started with main program. A parallel section starts a thread set. Parallelism can be nested . A parallel region is a block marked with the parallel directive. #pragma omp parallel cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 10/49

  11. Parallel programming using OpenMP Threads in OpenMP Selecting the number of threads Invoking a library function. OpenMP directive. Example Example // ... // ... omp_set_num_threads(4); #pragma omp parallel num_threads(4) #pragma omp parallel { { // Parallel region // Parallel region } } cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 11/49

  12. Parallel programming using OpenMP Threads in OpenMP Exercise 2: Computing π Computing π . � 1 4 π = 1 + x 2 dx 0 Approximation: N � F ( x i )∆ x π ≈ i = 0 Adding area of N rectangles: Base : ∆ x . Height : F ( x i ) . cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 12/49

  13. Parallel programming using OpenMP Threads in OpenMP Exercise 2: Sequential version Computing π (I) Computing π (II) #include <iostream> double sum = 0.0; #include <iomanip> for ( int i=0;i<nsteps; ++i) { #include <chrono> double x = (i+0.5) ∗ step; sum += 4.0 / (1.0 + x ∗ x); int main() { } using namespace std; double pi = step ∗ sum; using namespace std::chrono; auto t2 = clk :: now(); constexpr long nsteps = 10000000; auto diff = duration_cast<microseconds>(t2 − t1); double step = 1.0 / double (nsteps); cout << "PI= " << setprecision(10) << pi << endl; using clk = high_resolution_clock; cout << "Tiempo= " << diff.count() << "us" << endl; auto t1 = clk :: now(); return 0; } cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 13/49

  14. Parallel programming using OpenMP Threads in OpenMP Measuring time in C++11 include files: #include <chrono> Clock type: using clk = chrono::high_resolution_clock; Get a time point: auto t1 = clk :: now(); Time difference (time unit can be specified). auto diff = duration_cast<microseconds>(t2 − t1); Get difference value. cout << diff .count(); cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 14/49

  15. Parallel programming using OpenMP Threads in OpenMP Time measurement example Example #include <chrono> void f () { using namespace std; using namespace std::chrono; using clk = chrono::high_resolution_clock; auto t1 = clk :: now(); g(); auto t2 = clk :: now(); auto diff = duration_cast<microseconds>(t2 − t1); cout << "Time= " << diff .count << "microseconds" << endl; } cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 15/49

  16. Parallel programming using OpenMP Threads in OpenMP Time measurement in OpenMP Time point: double t1 = omp_get_wtime(); Time difference: double t1 = omp_get_wtime(); double t2 = omp_get_wtime(); double diff = t2 − t1; Time difference between two successive ticks: double tick = omp_get_wtick(); cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 16/49

  17. Parallel programming using OpenMP Threads in OpenMP Exercise 2 Create a parallel version from the π sequential version using a parallel clause. Observations : Include time measurements. Print the number of threads in use. Take special care with shared variables. Idea : Use an array and accumulate partial sum for each thread in the parallel region. cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 17/49

  18. Parallel programming using OpenMP Synchronization Introduction 1 Threads in OpenMP 2 3 Synchronization Parallel loops 4 Synchronize with master 5 6 Data sharing Sections and scheduling 7 Conclusion 8 cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 18/49

  19. Parallel programming using OpenMP Synchronization Synchronization mechanisms Synchronization : Mechanism used to establish constraints on the access order to shared variables. Goal : Avoid data races. Alternatives : High level : critical , atomic , barrier , ordered . Low level : flush , lock. cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 19/49

  20. Parallel programming using OpenMP Synchronization critical Guarantees mutual exclusion . Only a thread at a time can enter the critical region. Example #pragma omp parallel Calls to f() are performed in parallel. { for ( int i=0;i<max;++i) { Only a thread can enter function g() x = f( i ); at a time. #pragma omp critical g(x); } cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 20/49

  21. Parallel programming using OpenMP Synchronization atomic Guarantees atomic update of a single memory location. Avoid data races in variable update. Example #pragma omp parallel { Calls to f() performed in parallel. for ( int i=0;i<max;++i) { x = f( i ); Updates to s are thread-safe . #pragma omp atomic s += g(x) } cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 21/49

  22. Parallel programming using OpenMP Synchronization Exercise 3 Modify program from exercise 2. Evaluate: a) Critical section. b) Atomic access. cbed – Computer Architecture – ARCOS Group – http://www.arcos.inf.uc3m.es 22/49

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