
hpc.cineca.it Introduction to OpenMP Fabio Pitari - [email protected] Massimiliano Guarrasi - [email protected] Bologna - July 3, 2019 SuperComputing Application and Innovation hpc.cineca.it workshare Introduction How to avoid data races OpenMP vs MPI Critical construct OpenMP execution model Reduction clause OpenMP programming model Barrier construct OpenMP memory model Atomic construct Worksharing constructs OpenMP tasking Worksharing constructs rules Task construct for/do loop Task synchronization sections Depend clause single Conclusions SuperComputing Application and Innovation hpc.cineca.it Introduction SuperComputing Application and Innovation Disadvantageshpc.cineca.it of MPI Each MPI process can only access its local memory ) The data to be shared must be exchanged with explicit inter-process communications ! It is in charge to the programmer to design and implement the data exchange between process (taking care of work balance) You can not adopt a strategy of incremental parallelization ) The communication structure of the entire program has to be implemented It is difficult to maintain a single version of the code for the serial and MPI program ) Additional variables are needed ) Need to add a lot of boilerplate code SuperComputing Application and Innovation Whathpc.cineca.it is OpenMP? De-facto standard Application Program Interface (API) to write shared memory parallel applications in C, C++ and Fortran Consists of compilers directives, run-time routines and environment variables "Open specifications for Multi Processing" maintained by the OpenMP Architecture Review Board (http://www.openmp.org) Founding concepts The "workers" who do the work in parallel (thread) "cooperate" through shared memory Memory accesses instead of explicit messages "local" model parallelization of the serial code It allows an incremental parallelization SuperComputing Application and Innovation MPI Executionhpc.cineca.it model MPI model Everything lies in a huge parallel region where the same code is executed by all the ranks Comunication among ranks has to be explicitely built when needed SuperComputing Application and Innovation OpenMPhpc.cineca.it Execution model Fork-Join model Fork At the beginning of a parallel region the master thread creates a team of threads composed by itself and by a set of other threads Join At the end of the parallel region the thread team ends the execution and only the master thread continues the execution of the (serial) program SuperComputing Application and Innovation /* Serial part */ #pragma omp parallel { } /* Serial part */ OpenMP:hpc.cineca.it Hello world A set of instructions can be executed on the whole set of threads using the parallel directive C/C++ int main () { printf("Hello world\n"); return 0; } SuperComputing Application and Innovation OpenMP:hpc.cineca.it Hello world A set of instructions can be executed on the whole set of threads using the parallel directive C/C++ int main () { /* Serial part */ #pragma omp parallel { printf("Hello world\n"); } /* Serial part */ return 0; } SuperComputing Application and Innovation ! Serial code !$OMP PARALLEL !$OMP END PARALLEL ! Resume serial code OpenMP:hpc.cineca.it Hello world A set of instructions can be executed on the whole set of threads using the parallel directive Fortran PROGRAM HELLO Print *, "Hello World!!!" END PROGRAM HELLO SuperComputing Application and Innovation OpenMP:hpc.cineca.it Hello world A set of instructions can be executed on the whole set of threads using the parallel directive Fortran PROGRAM HELLO ! Serial code !$OMP PARALLEL Print *, "Hello World!!!" !$OMP END PARALLEL ! Resume serial code END PROGRAM HELLO SuperComputing Application and Innovation Programminghpc.cineca.it model Compiler directives // C++ #pragma omp <directive > [clause [clause] ...] ! Fortran !$omp <directive > [clause [clause]...] directive mark the block of code that should be made parallel clause add information to the directives ) Variables handling and scoping (shared, private, default) ) Execution control (how many threads, work distribution...) Note GNU (gcc, g++, gfortran) -fopenmp Intel (icc, icpc, ifort) -qopenmp SuperComputing Application and Innovation Programminghpc.cineca.it model Compiler directives Environment variables OMP_NUM_THREADS size set the number of threads OMP_DYNAMIC true|false set the number of threads automatically OMP_PLACES cores|num_places set the place where to allocate threads OMP_PROC_BIND true|false|close|spread bound threads to such place OMP_NESTED true|false allows nested parallelism Other commonly used variables will be clearer later (listed here for reference): OMP_STACKSIZE size [B|K|M|G] size of the stack for threads OMP_SCHEDULE schedule[,chunk] iteration scheduling scheme Note $ export VARIABLE_NAME = value SuperComputing Application and Innovation Programminghpc.cineca.it model Compiler directives Environment variables Run-time library omp_get_thread_num() get thread ID omp_get_num_threads() get number of threads in the team for threads omp_set_num_threads(int n) set the number of threads omp_get_wtime() returns elapsed wallclock time omp_get_wtick() returns number of seconds between ticks Note #include <omp.h> // C++ USE omp_lib ! Fortran SuperComputing Application and Innovation Conditionalhpc.cineca.it compilation C/C++ #ifdef _OPENMP printf("OpenMP support:%d",_OPENMP); #else printf("Serial execution."); #endif Fortran #ifdef _OPENMP print *,"OpenMP support" #else print *,"Serial execution." #endif Note The macro _OPENMP has the value yyyymm, which contains the release date of the OpenMP version currently being used SuperComputing Application and Innovation Processhpc.cineca.it and thread A process is an instance of a computer program Some information included in a process are: Text ) Machine code Data ) Global variables Stack ) Local variables Program counter (PC) ) A pointer to the instruction to be executed SuperComputing Application and Innovation Multi-threadedhpc.cineca.it processes The process contains several concurrent execution flows (threads) Each thread has its own program counter (PC) Each thread has its own private stack (variables local to the thread) The instructions executed by a thread can access: the process global memory (data) the thread local stack SuperComputing Application and Innovation OpenMPhpc.cineca.it memory model Shared-Local model each thread is allowed to have a temporary view of the shared memory each thread has access to a thread-private memory two kinds of data-sharing attributes: private and shared SuperComputing Application and Innovation Distributedhpc.cineca.it and shared memory SuperComputing Application and Innovation UMA andhpc.cineca.it NUMA systems SuperComputing Application and Innovation Definedhpc.cineca.it or undefined? What's the result? #include <stdio.h> #include <omp.h> void main(){ int a; a = 0; #pragma omp parallel { // omp_get_thread_num // returns the id of the thread a = a + omp_get_thread_num(); } printf("%d\n", a); } SuperComputing Application and Innovation Race conditionhpc.cineca.it A race condition (or data race) is when two or more threads access the same memory location: asyncronously and, without holding any common exclusive locks and, at least one of the accesses is a write/store In this case the resulting values are undefined SuperComputing Application and Innovation Criticalhpc.cineca.it construct #include <stdio.h> The critical construct is a #include <omp.h> possible solution to data races: void main(){ int a; The block of code inside a a = 0; critical construct is #pragma omp parallel executed by only one { // omp_get_thread_num thread at a time // returns the id of the thread It locks the associated #pragma omp critical region a = a + omp_get_thread_num(); } printf("%d\n", a); } SuperComputing Application and Innovation Handshpc.cineca.it on Exercise 1.1 #include <stdio.h> #ifdef _OPENMP #include <omp.h> #endif Compile int main(int argc, char* argv[]) { Run #ifdef _OPENMP int iam; Experiment with the #pragma omp parallel { /* the parallel block starts here */ OMP_NUM_THREADS iam=omp_get_thread_num(); variable. #pragma omp critical printf("Hello from %d\n",iam); Did you obtain the } /* the parallel block ends here */ behaviour you expected? #else printf("Hello, this is a serial program.\n"); #endif return 0; } SuperComputing Application and Innovation Sharedhpc.cineca.it and private variables Inside a parallel region the scope of a variable can be shared or private. Shared Private There is only one instance of Each thread has a copy of the variable. The the variable. variable is accessible only by the owner thread. Directive: Directive: private(a,b,c,…) shared(a,b,c,…) Values are undefined on entry and exit The variable is Directive: firstprivate(a,b,c,…) accessible by all variables are initialized with the value that threads in the team the original object had before entering the Threads can read and parallel construct write the variable simultaneously Directive: lastprivate(a,b,c,…) the thread that executes the sequentially last Note: Variables are shared iteration or section updates the value of the by default variable SuperComputing Application and Innovation Data-sharinghpc.cineca.it attributes int a=0; float b=1.5; int c=3; #pragma omp parallel shared(a) private(b) firstprivate (c) { // each thread can access to "a" // each thread has its own copy of "b", with an undefined value // each thread has its own copy of "c", with c=3 } Best practice: Nullify the default with the clause default (none) SuperComputing Application
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages79 Page
-
File Size-