1 / 56

Introduction to OpenMP

Introduction to OpenMP. Rogelio Long CS 5334/4390 January 31, 2017. Acknowledgment. These slides are adapted from the Lawrence Livermore OpenMP Tutorial by Blaise Barney at https://computing.llnl.gov/tutorials/openMP/ and the

amarvin
Download Presentation

Introduction to OpenMP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to OpenMP Rogelio Long CS 5334/4390 January 31, 2017

  2. Acknowledgment These slides are adapted from the Lawrence Livermore OpenMP Tutorial by Blaise Barney at https://computing.llnl.gov/tutorials/openMP/ and the Introduction to Parallel Computing'', Addison Wesley, 2003 Powerpoint.

  3. What is OpenMP? • An Application Program Interface (API) that may be used to explicitly direct multi-threaded, shared memory parallelism • Comprises three primary API components • Compiler Directives • Runtime Library Routines • Environment Variables • Portable • The API is specified for C/C++ and Fortran • Has been implemented for most major platforms including Unix/Linux platforms and Windows NT

  4. What is OpenMP? (cont.) • Standardized • Jointly defined and endorsed by a group of major computer hardware and software vendors • Expected to become an ANSI standard later??? • What does OpenMP stand for? • Short version: Open Multi-Processing • Long version: Open specifications for Multi-Processing via collaborative work between interested parties from the hardware and software industry, government and academia.

  5. OpenMP is NOT • Meant for distributed memory parallel systems (by itself) • Necessarily implemented identically by all vendors • Guaranteed to make the most efficient use of shared memory • Required to check for data dependencies, data conflicts, race conditions, or deadlocks • Required to check for code sequences that cause a program to be classified as non-conforming • Meant to cover compiler-generated automatic parallelization and directives to the compiler to assist such parallelization • Designed to guarantee that input or output to the same file is synchronous when executed in parallel (The programmer is responsible for synchronizing input and output).

  6. References • OpenMP website: openmp.orgAPI specifications, FAQ, presentations, discussions, media releases, calendar, membership application and more... • Wikipedia: en.wikipedia.org/wiki/OpenMP • Barbara Chapman, Gabriele Jost, and Ruud van der Pas: Using OpenMP. The MIT Press, 2008. • Compiler documentation • IBM: www-4.ibm.com/software/ad/fortran • Cray: http://docs.cray.com/ (Cray Fortran Reference Manual) • Intel: www.intel.com/software/products/compilers/ • PGI: www.pgroup.com • PathScale: www.pathscale.com • GNU: gnu.org

  7. History of OpenMP • In the early 90's, vendors of shared-memory machines supplied similar, directive-based, Fortran programming extensions. • The user would augment a serial Fortran program with directives specifying which loops were to be parallelized. • The compiler would be responsible for automatically parallelizing such loops across the SMP processors. • Implementations were all functionally similar, but were divergent. • First attempt at a standard was the draft for ANSI X3H5 in 1994. It was never adopted, largely due to waning interest as distributed memory machines became popular.

  8. History of OpenMP (cont.) • The OpenMP standard specification started in the spring of 1997, taking over where ANSI X3H5 had left off, as newer shared memory machine architectures started to become prevalent. • Led by the OpenMP Architecture Review Board (ARB). Original ARB members included: (Disclaimer: all partner names derived from the OpenMP web site) • Compaq / Digital • Hewlett-Packard Company • Intel Corporation • International Business Machines (IBM) • Kuck & Associates, Inc. (KAI) • Silicon Graphics, Inc. • Sun Microsystems, Inc. • U.S. Department of Energy ASCI program

  9. Other Contributors • Endorsing application developers • ADINA R&D, Inc. • ANSYS, Inc. • Dash Associates • Fluent, Inc. • ILOG CPLEX Division • Livermore Software Technology Corporation (LSTC) • MECALOG SARL • Oxford Molecular Group PLC • The Numerical Algorithms Group Ltd.(NAG) • Endorsing software vendors • Absoft Corporation • Edinburgh Portable Compilers • GENIAS Software GmBH • Myrias Computer Technologies, Inc. • The Portland Group, Inc. (PGI)

  10. OpenMP Release History

  11. Goals of OpenMP • Standardization • Provide a standard among a variety of shared memory architectures/platforms • Lean and mean • Establish a simple and limited set of directives for programming shared memory machines • Significant parallelism can be implemented by using just 3 or 4 directives. • Ease of Use • Provide capability to incrementally parallelize a serial program, unlike message-passing libraries which typically require an all or nothing approach • Provide the capability to implement both coarse-grain and fine-grain parallelism • Portability • Supports Fortran (77, 90, and 95), C, and C++ • Public forum for API and membership

  12. OpenMP Programming Model • Shared memory, thread-based parallelism • OpenMP is based upon the existence of multiple threads in the shared memory programming paradigm. • A shared memory process consists of multiple threads. • Explicit Parallelism • OpenMP is an explicit (not automatic) programming model, offering the programmer full control over parallelization. • OpenMP uses the fork-join model of parallel execution. • Compiler directive based • Most OpenMP parallelism is specified through the use of compiler directives which are imbedded in C/C++ or Fortran source code. • Nested parallelism support • The API provides for the placement of parallel constructs inside of other parallel constructs. • Implementations may or may not support this feature.

  13. Fork-Join Model • All OpenMP programs begin as a single process: the master thread. The master thread executes sequentially until the first parallel region construct is encountered. • FORK: the master thread then creates a team of parallel threads • The statements in the program that are enclosed by the parallel region construct are then executed in parallel among the team threads. • JOIN: When the team threads complete the statements in the parallel region construct, they synchronize and terminate, leaving only the master thread

  14. I/O • OpenMP specifies nothing about parallel I/O. This is particularly important if multiple threads attempt to write/read from the same file. • If every thread conducts I/O to a different file, the issues are not as significant. • It is entirely up to the programmer to ensure that I/O is conducted correctly within the context of a multi-threaded program.

  15. Memory Model • OpenMP provides a "relaxed-consistency" and "temporary" view of thread memory (in their words). In other words, threads can "cache" their data and are not required to maintain exact consistency with real memory all of the time. • When it is critical that all threads view a shared variable identically, the programmer is responsible for insuring that the variable is FLUSHed by all threads as needed. • More on this later...

  16. OpenMP Programming Model • OpenMP directives in C and C++ are based on the #pragma compiler directives. • A directive consists of a directive name followed by clauses. #pragma omp directive [clause list] • OpenMP programs execute serially until they encounter the parallel directive, which creates a group of threads. #pragma omp parallel [clause list] /* structured block */ • The main thread that encounters the parallel directive becomes the master of this group of threads and is assigned the thread id 0 within the group.

  17. OpenMP Programming Model • The clause list is used to specify conditional parallelization, number of threads, and data handling. • Conditional Parallelization: The clause if (scalar expression) determines whether the parallel construct results in creation of threads. • Degree of Concurrency: The clause num_threads(integer expression) specifies the number of threads that are created. • Data Handling: The clause private (variable list) indicates variables local to each thread. The clause firstprivate (variable list) is similar to the private, except values of variables are initialized to corresponding values before the parallel directive. The clause shared (variable list) indicates that variables are shared across all the threads.

  18. OpenMP Programming Model • A sample OpenMP program along with its Pthreads translation that might be performed by an OpenMP compiler.

  19. OpenMP Code Structure – C/C++ #include <omp.h> main () { int var1, var2, var3; Serial code . . . Beginning of parallel section. Fork a team of threads. Specify variable scoping #pragma omp parallel private(var1, var2) shared(var3) { Parallel section executed by all threads . . . All threads join master thread and disband } Resume serial code . . . }

  20. OpenMP Code Structure - Fortran PROGRAM HELLO INTEGER VAR1, VAR2, VAR3 Serial code . . . Beginning of parallel section. Fork a team of threads. Specify variable scoping !$OMP PARALLEL PRIVATE(VAR1, VAR2) SHARED(VAR3) Parallel section executed by all threads . . . All threads join master thread and disband !$OMP END PARALLEL Resume serial code . . . END

  21. PARALLEL Region – Number of Threads • The number of threads in a parallel region is determined by the following factors, in order of precedence: • Evaluation of the IF clause • Setting of the NUM_THREADS clause • Use of the omp_set_num_threads() library function • Setting of the OMP_NUM_THREADS environment variable • Implementation default - usually the number of cores on a node. • Threads are numbered from 0 (master thread) to N-1

  22. OMP_SET_NUM_THREADS() • Sets the number of threads that will be used in the next parallel region • Must be a positive integer • Can only be called from serial portion of the code • Has precedence over the OMP_NUM_THREADS environment variable

  23. OMP_GET_NUM_THREADS() and OMP_GET_THREAD_NUM() • OMP_GET_NUM_THREADS() • Returns the number of threads that are currently in the team executing the parallel region from which it is called • OMP_GET_THREAD_NUM() • Returns the thread number of the thread, within the team, making this call. This number will be between 0 and OMP_GET_NUM_THREADS-1. The master thread of the team is thread 0. • If called from a nested parallel region, or a serial region, this function will return 0.

  24. OMP_GET_MAX_THREADS() • Returns the maximum value that can be returned by a call to the OMP_GET_NUM_THREADS function • Generally reflects the number of threads as set by the OMP_NUM_THREADS environment variable or the OMP_SET_NUM_THREADS() library routine. • May be called from both serial and parallel regions of code

  25. OMP_GET_THREAD_LIMIT() and OMP_GET_NUM_PROCS() • OMP_GET_THREAD_LIMIT() • New with OpenMP 3.0 • Returns the maximum number of OpenMP threads available to a program • OMP_GET_NUM_PROCS() • Returns the number of processors that are available to the program

  26. OpenMP Programming Model #pragma omp parallel if (is_parallel== 1) num_threads(8) \ private (a) shared (b) firstprivate(c) { /* structured block */ } • If the value of the variable is_parallel equals one, eight threads are created. • Each of these threads gets private copies of variables a and c, and shares a single value of variable b. • The value of each copy of c is initialized to the value of c before the parallel directive. • The default state of a variable is specified by the clause default (shared) or default (none).

  27. PRIVATE and SHARED Clauses • PRIVATE Clause • Declares variables in its list to be private to each thread • A new object of the same type is declared once for each thread in the team. • All references to the original object are replaced with references to the new object. • Variables declared PRIVATE should be assumed to be uninitialized for each thread. • SHARED Clause • Declares variables in its list to be shared among all threads in the team • A shared variable exists in only one memory location and all threads can read or write to that address • It is the programmer's responsibility to ensure that multiple threads properly access SHARED variables (such as via CRITICAL sections)

  28. FIRSTPRIVATE and LASTPRIVATE Clauses • FIRSTPRIVATE Clause • Combines the behavior of the PRIVATE clause with automatic initialization of the variables in its list • Listed variables are initialized according to the value of their original objects prior to entry into the parallel or work-sharing construct. • LASTPRIVATE Clause • Combines the behavior of the PRIVATE clause with a copy from the last loop iteration or section to the original variable object • The value copied back into the original variable object is obtained from the last (sequentially) iteration or section of the enclosing construct. • For example, the team member that executes the final iteration for a DO section, or the team member that executes the last SECTION of a SECTIONS context, performs the copy with its own values.

  29. PRIVATE Variables Example main() { int A = 10; int B, C; int n = 20; #pragma omp parallel { #pragma omp for private(i) firstprivate(A) lastprivate(B) for (int i=0; i<n; i++) { .... B = A + i; /* A undefined unless declared firstprivate */ .... } C = B; /* B undefined unless declared lastprivate */ } /* end of parallel region */ }

  30. PARALLEL Region Restrictions • A parallel region must be a structured block that does not span multiple routines or code files • It is illegal to branch into or out of a parallel region • Only a single IF clause is permitted • Only a single NUM_THREADS clause is permitted

  31. PARALLEL Region Example - C #include <omp.h> main () { intnthreads, tid; /* Fork a team of threads with each thread having a private tid variable */ #pragmaomp parallel private(tid) { /* Obtain and print thread id */ tid = omp_get_thread_num(); printf("Hello World from thread = %d\n", tid); /* Only master thread does this */ if (tid == 0) { nthreads = omp_get_num_threads(); printf("Number of threads = %d\n", nthreads); } } /* All threads join master thread and terminate */ }

  32. PARALLEL Region Example - Fortran PROGRAM HELLO INTEGER NTHREADS, TID, OMP_GET_NUM_THREADS, + OMP_GET_THREAD_NUM C Fork a team of threads with each thread having a private TID variable !$OMP PARALLEL PRIVATE(TID) C Obtain and print thread id TID = OMP_GET_THREAD_NUM() PRINT *, 'Hello World from thread = ', TID C Only master thread does this IF (TID .EQ. 0) THEN NTHREADS = OMP_GET_NUM_THREADS() PRINT *, 'Number of threads = ', NTHREADS END IF C All threads join master thread and disband !$OMP END PARALLEL END

  33. Reduction Clause in OpenMP • The reduction clause specifies how multiple local copies of a variable at different threads are combined into a single copy at the master when threads exit. • The usage of the reduction clause is reduction (operator: variable list). • The variables in the list are implicitly specified as being private to threads. • The operator can be one of +, *, -, &, |, ^, &&, and ||. #pragma omp parallel reduction(+: sum) num_threads(8) { /* compute local sums here */ } /*sum here contains sum of all local instances of sums */

  34. REDUCTION Clause Example - C #include <omp.h> main () { int i, n, chunk; float a[100], b[100], result; /* Some initializations */ n = 100; chunk = 10; result = 0.0; for (i=0; i < n; i++) { a[i] = i * 1.0; b[i] = i * 2.0; } #pragma omp parallel for default(shared) private(i) \ schedule(static,chunk) reduction(+:result) for (i=0; i < n; i++) result = result + (a[i] * b[i]); printf("Final result= %f\n",result); }

  35. REDUCTION Clause Example - Fortran PROGRAM DOT_PRODUCT INTEGER N, CHUNKSIZE, CHUNK, I PARAMETER (N=100) PARAMETER (CHUNKSIZE=10) REAL A(N), B(N), RESULT ! Some initializations DO I = 1, N A(I) = I * 1.0 B(I) = I * 2.0 ENDDO RESULT= 0.0 CHUNK = CHUNKSIZE !$OMP PARALLEL DO !$OMP& DEFAULT(SHARED) PRIVATE(I) !$OMP& SCHEDULE(STATIC,CHUNK) !$OMP& REDUCTION(+:RESULT) DO I = 1, N RESULT = RESULT + (A(I) * B(I)) ENDDO !$OMP END PARALLEL DO NOWAIT PRINT *, 'Final Result= ', RESULT END

  36. Approximate Pi sum = 0; for (i = 0; i < npoints; i++) { rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1); rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1); if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) sum ++; }

  37. OpenMP Programming: Example /* ****************************************************** An OpenMP version of a threaded program to compute PI. ****************************************************** */ #pragma omp parallel default(private) shared (npoints) \ reduction(+: sum) num_threads(8) { num_threads = omp_get_num_threads(); sample_points_per_thread = npoints / num_threads; sum = 0; for (i = 0; i < sample_points_per_thread; i++) { rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1); rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1); if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) sum ++; } }

  38. Specifying Concurrent Tasks in OpenMP • The parallel directive can be used in conjunction with other directives to specify concurrency across iterations and tasks. • OpenMP provides two directives - for and sections - to specify concurrent iterations and tasks. • The for directive is used to split parallel iteration spaces across threads. The general form of a for directive is as follows: #pragma omp for [clause list] /* for loop */ • The clauses that can be used in this context are: private, firstprivate, lastprivate, reduction, schedule, nowait, and ordered.

  39. Specifying Concurrent Tasks in OpenMP: Example #pragma omp parallel default(private) shared (npoints) \ reduction(+: sum) num_threads(8) { sum = 0; #pragma omp for for (i = 0; i < npoints; i++) { rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1); rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1); if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) sum ++; } }

  40. Assigning Iterations to Threads • The schedule clause of the for directive deals with the assignment of iterations to threads. • The general form of the schedule directive is schedule(scheduling_class[, parameter]). • OpenMP supports four scheduling classes: static, dynamic, guided, and runtime.

  41. SCHEDULE Clause • Describes how iterations of the loop are divided among the threads in the team. The default schedule is implementation dependent. STATIC Loop iterations are divided into pieces of size chunk and then statically assigned to threads. If chunk is not specified, the iterations are evenly (if possible) divided contiguously among the threads. DYNAMIC Loop iterations are divided into pieces of size chunk, and dynamically scheduled among the threads; when a thread finishes one chunk, it is dynamically assigned another. The default chunk size is 1. GUIDED For a chunk size of 1, the size of each chunk is proportional to the number of unassigned iterations divided by the number of threads, decreasing to 1. For a chunk size with value k (greater than 1), the size of each chunk is determined in the same way with the restriction that the chunks do not contain fewer than k iterations (except for the last chunk to be assigned, which may have fewer than k iterations). The default chunk size is 1. RUNTIME The scheduling decision is deferred until runtime by the environment variable OMP_SCHEDULE. It is illegal to specify a chunk size for this clause. AUTO The scheduling decision is delegated to the compiler and/or runtime system.

  42. Assigning Iterations to Threads: Example /* static scheduling of matrix multiplication loops */ #pragma omp parallel default(private) shared (a, b, c, dim) \ num_threads(4) #pragma omp for schedule(static) for (i = 0; i < dim; i++) { for (j = 0; j < dim; j++) { c(i,j) = 0; for (k = 0; k < dim; k++) { c(i,j) += a(i, k) * b(k, j); } } }

  43. Assigning Iterations to Threads: Example • Three different schedules using the static scheduling class of OpenMP.

  44. Parallel For Loops • Often, it is desirable to have a sequence of for-directives within a parallel construct that do not execute an implicit barrier at the end of each for directive. • OpenMP provides a clause - nowait, which can be used with a for directive.

  45. Parallel For Loops: Example #pragma omp parallel { #pragma omp for nowait for (i = 0; i < nmax; i++) if (isEqual(name, current_list[i]) processCurrentName(name); #pragma omp for for (i = 0; i < mmax; i++) if (isEqual(name, past_list[i]) processPastName(name); }

  46. The sections Directive • OpenMP supports non-iterative parallel task assignment using the sections directive. • The general form of the sections directive is as follows: #pragma omp sections [clause list] { [#pragma omp section /* structured block */ ] [#pragma omp section /* structured block */ ] ... }

  47. The sections Directive: Example #pragma omp parallel { #pragma omp sections { #pragma omp section { taskA(); } #pragma omp section { taskB(); } #pragma omp section { taskC(); } } }

  48. Nesting parallel Directives • Nested parallelism can be enabled using the OMP_NESTED environment variable. • If the OMP_NESTED environment variable is set to TRUE, nested parallelism is enabled. • In this case, each parallel directive creates a new team of threads.

  49. FLUSH Directive • Identifies a synchronization point at which the implementation must provide a consistent view of memory • Thread-visible variables are written back to memory at this point. • Necessary to instruct the compiler that a variable must be written to/read from the memory system, i.e. that a variable cannot be kept in a local CPU register • Keeping a variable in a register in a loop is very common when producing efficient machine language code for a loop.

  50. FLUSH Directive (2) Fortran: !$OMP FLUSH (list) C/C++: #pragmaomp flush (list) newline • The optional list contains a list of named variables that will be flushed in order to avoid flushing all variables. For pointers in the list, the pointer itself is flushed, not the object to which it points. • Implementations must ensure any prior modifications to thread-visible variables are visible to all threads after this point; i.e., compilers must restore values from registers to memory, hardware might need to flush write buffers, etc. • The FLUSH directive is implied for the directives shown in the table below. The directive is not implied if a NOWAIT clause is present.

More Related