Programming Shared-memory Platforms with OpenMP

Similar documents
Programming Shared-memory Platforms with OpenMP. Xu Liu

COMP4300/8300: The OpenMP Programming Model. Alistair Rendell. Specifications maintained by OpenMP Architecture Review Board (ARB)

COMP4300/8300: The OpenMP Programming Model. Alistair Rendell

Programming Shared Address Space Platforms using OpenMP

Overview: The OpenMP Programming Model

Introduction to OpenMP. Rogelio Long CS 5334/4390 Spring 2014 February 25 Class

OPENMP OPEN MULTI-PROCESSING

Data Handling in OpenMP

OpenMP Overview. in 30 Minutes. Christian Terboven / Aachen, Germany Stand: Version 2.

Lecture 4: OpenMP Open Multi-Processing

[Potentially] Your first parallel application

Review. Tasking. 34a.cpp. Lecture 14. Work Tasking 5/31/2011. Structured block. Parallel construct. Working-Sharing contructs.

Introduction to OpenMP

Module 10: Open Multi-Processing Lecture 19: What is Parallelization? The Lecture Contains: What is Parallelization? Perfectly Load-Balanced Program

Advanced C Programming Winter Term 2008/09. Guest Lecture by Markus Thiele

Mango DSP Top manufacturer of multiprocessing video & imaging solutions.

A brief introduction to OpenMP

Introduction to OpenMP

OpenMP 2. CSCI 4850/5850 High-Performance Computing Spring 2018

Shared Memory Parallelism - OpenMP

Multithreading in C with OpenMP

OpenMP 4.0/4.5: New Features and Protocols. Jemmy Hu

OpenMP Application Program Interface

Shared Memory Parallelism using OpenMP

OpenMP Application Program Interface

Review. Lecture 12 5/22/2012. Compiler Directives. Library Functions Environment Variables. Compiler directives for construct, collapse clause

Introduction to. Slides prepared by : Farzana Rahman 1

OpenMP threading: parallel regions. Paolo Burgio

by system default usually a thread per CPU or core using the environment variable OMP_NUM_THREADS from within the program by using function call

OpenMP. OpenMP. Portable programming of shared memory systems. It is a quasi-standard. OpenMP-Forum API for Fortran and C/C++

An Introduction to OpenMP

Programming Shared Address Space Platforms

Introduction to OpenMP

ECE 574 Cluster Computing Lecture 10

Introduction to OpenMP. OpenMP basics OpenMP directives, clauses, and library routines

Shared Memory programming paradigm: openmp

OpenMP Algoritmi e Calcolo Parallelo. Daniele Loiacono

OpenMP Technical Report 3 on OpenMP 4.0 enhancements

Introduction to OpenMP. Martin Čuma Center for High Performance Computing University of Utah

Parallelising Scientific Codes Using OpenMP. Wadud Miah Research Computing Group

Introduction to OpenMP. Martin Čuma Center for High Performance Computing University of Utah

OpenMP Programming. Prof. Thomas Sterling. High Performance Computing: Concepts, Methods & Means

Shared Memory Programming Model

Topics. Introduction. Shared Memory Parallelization. Example. Lecture 11. OpenMP Execution Model Fork-Join model 5/15/2012. Introduction OpenMP

Shared Memory Programming with OpenMP

Introduction [1] 1. Directives [2] 7

Practical stuff! ü OpenMP. Ways of actually get stuff done in HPC:

OpenMP 4. CSCI 4850/5850 High-Performance Computing Spring 2018

OpenMP. António Abreu. Instituto Politécnico de Setúbal. 1 de Março de 2013

Parallel Programming with OpenMP. CS240A, T. Yang

OpenMP C and C++ Application Program Interface Version 1.0 October Document Number

OpenMP Library Functions and Environmental Variables. Most of the library functions are used for querying or managing the threading environment

PC to HPC. Xiaoge Wang ICER Jan 27, 2016

HPCSE - I. «OpenMP Programming Model - Part I» Panos Hadjidoukas

EE/CSCI 451 Introduction to Parallel and Distributed Computation. Discussion #4 2/3/2017 University of Southern California

OpenMP Fundamentals Fork-join model and data environment

Introduction to OpenMP

POSIX Threads and OpenMP tasks

Parallel and Distributed Programming. OpenMP

Advanced OpenMP. Tasks

Introduction to OpenMP. Martin Čuma Center for High Performance Computing University of Utah

Distributed Systems + Middleware Concurrent Programming with OpenMP

Introduction to OpenMP.

Tasking in OpenMP 4. Mirko Cestari - Marco Rorro -

1 of 6 Lecture 7: March 4. CISC 879 Software Support for Multicore Architectures Spring Lecture 7: March 4, 2008

OpenMP 4.5: Threading, vectorization & offloading

OpenMP I. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS16/17. HPAC, RWTH Aachen

Parallel Computing Parallel Programming Languages Hwansoo Han

OpenMP Lab on Nested Parallelism and Tasks

EPL372 Lab Exercise 5: Introduction to OpenMP

CSL 860: Modern Parallel

Introduction to OpenMP

Practical in Numerical Astronomy, SS 2012 LECTURE 12

Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG

OpenMP - II. Diego Fabregat-Traver and Prof. Paolo Bientinesi WS15/16. HPAC, RWTH Aachen

EE/CSCI 451: Parallel and Distributed Computation

Session 4: Parallel Programming with OpenMP

OpenMP. Application Program Interface. CINECA, 14 May 2012 OpenMP Marco Comparato

OpenMP on Ranger and Stampede (with Labs)

HPC Practical Course Part 3.1 Open Multi-Processing (OpenMP)

Introduction to Standard OpenMP 3.1

15-418, Spring 2008 OpenMP: A Short Introduction

Programming with Shared Memory PART II. HPC Fall 2012 Prof. Robert van Engelen

Synchronisation in Java - Java Monitor

UvA-SARA High Performance Computing Course June Clemens Grelck, University of Amsterdam. Parallel Programming with Compiler Directives: OpenMP

Introduction to OpenMP

Shared memory parallel computing

Advanced OpenMP. OpenMP Basics

Module 11: The lastprivate Clause Lecture 21: Clause and Routines. The Lecture Contains: The lastprivate Clause. Data Scope Attribute Clauses

OpenMP examples. Sergeev Efim. Singularis Lab, Ltd. Senior software engineer

Introduction to OpenMP

Introduction to Programming with OpenMP

Data Environment: Default storage attributes

Multi-core Architecture and Programming

A Short Introduction to OpenMP. Mark Bull, EPCC, University of Edinburgh

Parallel programming using OpenMP

Shared Memory Programming Models I

Parallel Programming with OpenMP. CS240A, T. Yang, 2013 Modified from Demmel/Yelick s and Mary Hall s Slides

Parallel Programming in C with MPI and OpenMP

Programming with OpenMP*

Transcription:

Programming Shared-memory Platforms with OpenMP John Mellor-Crummey Department of Computer Science Rice University johnmc@rice.edu COMP 422/534 Lecture 7 31 February 2017

Introduction to OpenMP OpenMP directives concurrency directives parallel regions loops, sections, tasks Topics for Today synchronization directives reductions, barrier, critical, ordered data handling clauses shared, private, firstprivate, lastprivate tasks Performance tuning hints Library primitives Environment variables 2

What is OpenMP? Open specifications for Multi Processing An API for explicit multi-threaded, shared memory parallelism Three components compiler directives runtime library routines environment variables Higher-level than library-based programming models implicit mapping and load balancing of work Portable API is specified for C/C++ and Fortran implementations on almost all platforms Standard 3

OpenMP at a Glance Application User Compiler Environment Variables Runtime Library OS Threads (e.g., Pthreads) 4

OpenMP Is Not An automatic parallel programming model parallelism is explicit programmer has full control (and responsibility) over parallelization Meant for distributed-memory parallel systems (by itself) designed for shared address spaced machines Necessarily implemented identically by all vendors Guaranteed to make the most efficient use of shared memory no data locality control 5

OpenMP Targets Ease of Use OpenMP does not require that single-threaded code be changed for threading enables incremental parallelization of a serial program OpenMP only adds compiler directives pragmas (C/C++); significant comments in Fortran if a compiler does not recognize a directive, it ignores it simple & limited set of directives for shared memory programs significant parallelism possible using just 3 or 4 directives both coarse-grain and fine-grain parallelism If OpenMP is disabled when compiling a program, the program will execute sequentially 6

OpenMP: Fork-Join Parallelism OpenMP program begins execution as a single master thread Master thread executes sequentially until 1 st parallel region When a parallel region is encountered, master thread creates a group of threads becomes the master of this group of threads is assigned the thread id 0 within the group F o r k J o i n F o r k J o i n F o r k J o i n master thread shown in red 7

OpenMP Directive Format OpenMP directive forms C and C++ use compiler directives prefix: #pragma Fortran uses significant comments prefixes:!$omp, c$omp, *$omp A directive consists of a directive name followed by clauses C: #pragma omp parallel num_threads(4) Fortran:!$omp parallel num_threads(4) 8

A Simple Example Using parallel #include <stdio.h> #include <omp.h> Program int main() #pragma omp parallel num_threads(4) int i = omp_get_thread_num(); printf("hello from thread %d\n", i); Output Hello from thread 0 order of output may vary! Hello from thread 1 Hello from thread 2 Hello from thread 3 9

OpenMP parallel Region Directive #pragma omp parallel [clause list] Typical clauses in [clause list] Conditional parallelization if (scalar expression) determines whether the parallel construct creates threads Degree of concurrency num_threads(integer expression): max # threads to create Data Scoping private (variable list) specifies variables local to each thread firstprivate (variable list) similar to the private private variables are initialized to variable value before the parallel directive shared (variable list) specifies that variables are shared across all the threads default (data scoping specifier) default data scoping specifier may be shared or none A few more clauses on slide 38 10

Interpreting an OpenMP Parallel Directive #pragma omp parallel if (is_parallel==1) num_threads(8) \ shared(b) private(a) firstprivate(c) default(none) /* structured block */ Meaning if (is_parallel== 1) num_threads(8) If the value of the variable is_parallel is one, create 8 threads shared(b) each thread shares a single copy of variable b private(a) firstprivate(c) each thread gets private copies of variables a and c each private copy of c is initialized with the value of c in the initial thread, which is the one that encounters the parallel directive default(none) default state of a variable is specified as none (rather than shared) signals error if not all variables are specified as shared or private 11

Specifying Worksharing Within the scope of a parallel directive, worksharing directives allow concurrency between iterations or tasks OpenMP provides four directives DO/for: concurrent loop iterations sections: concurrent tasks workshare: partition execution of statements in block single: one arbitrary thread executes the code 12

Worksharing DO/for Directive for directive partitions parallel iterations across threads DO is the analogous directive for Fortran Usage: #pragma omp for [clause list] /* for loop */ Possible clauses in [clause list] private, firstprivate, lastprivate reduction schedule, nowait, and ordered Implicit barrier at end of for loop 13

A Simple Example Using parallel and for Program void main() #pragma omp parallel num_threads(3) int i; printf( Hello world\n ); #pragma omp for for (i = 1; i <= 4; i++) printf( Iteration %d\n,i); printf( Goodbye world\n ); Output Hello world Hello world Hello world Iteration 1 Iteration 2 Iteration 3 Iteration 4 Goodbye world Goodbye world Goodbye world 14

Reduction Clause for Parallel Directive Specifies how to combine local copies of a variable in different threads into a single copy at the master when threads exit Usage: reduction (operator: variable list) variables in list are implicitly private to threads Reduction operators: +, *, -, &,, ^, &&, and Usage sketch #pragma omp parallel reduction(+: sum) num_threads(8) /* compute local sum in each thread here */ /* sum here contains sum of all local instances of sum */ 15

Running Example: Monte Carlo Estimation of Pi (0,0.5) Approximate Pi generate random points with x, y [-0.5, 0.5] test if point inside the circle, i.e., x 2 + y 2 < (0.5) 2 ratio of circle to square = πr 2 / 4r 2 = π / 4 π 4 * (number of points inside the circle) / (number of points total) (0,0) (0.5,0) 16

OpenMP Reduction Clause Example OpenMP threaded program to estimate 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++) coord_x =(double)(rand_r(&seed))/(double)(rand_max) - 0.5; coord_y =(double)(rand_r(&seed))/(double)(rand_max) - 0.5; if ((coord_x * coord_x + coord_y * coord_y) < 0.25) sum++; here, user manually divides work a local copy of sum for each thread all local copies of sum added together and stored in master 17

Using Worksharing for Directive #pragma omp parallel default(private) shared (npoints) \ reduction(+: sum) num_threads(8) sum = 0; #pragma omp for for (i = 0; i < npoints; i++) worksharing for divides work rand_no_x =(double)(rand_r(&seed))/(double)(rand_max); rand_no_y =(double)(rand_r(&seed))/(double)(rand_max); if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) sum ++; Implicit barrier at end of loop 18

Mapping Iterations to Threads schedule clause of the for directive Recipe for mapping iterations to threads Usage: schedule(scheduling_class[,chunk]). Four scheduling classes static: work partitioned at compile time iterations statically divided into pieces of size chunk statically assigned to threads dynamic: work evenly partitioned at run time iterations are divided into pieces of size chunk chunks dynamically scheduled among the threads when a thread finishes one chunk, it is dynamically assigned another default chunk size is 1 guided: guided self-scheduling chunk size is exponentially reduced with each dispatched piece of work the default minimum chunk size is 1 runtime: scheduling decision from environment variable OMP_SCHEDULE illegal to specify a chunk size for this clause. 19

Statically Mapping Iterations to Threads /* 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); static schedule maps iterations to threads at compile time 20

Avoiding Unwanted Synchronization Default: worksharing for loops end with an implicit barrier Often, less synchronization is appropriate series of independent for-directives within a parallel construct nowait clause modifies a for directive avoids implicit barrier at end of for 21

Avoiding Synchronization with nowait #pragma omp parallel #pragma omp for nowait for (i = 0; i < nmax; i++) a[i] =...; #pragma omp for for (i = 0; i < mmax; i++) b[i] =... anything but a...; any thread can begin second loop immediately without waiting for other threads to finish first loop 22

Worksharing sections Directive sections directive enables specification of task parallelism Usage #pragma omp sections [clause list] [#pragma omp section ] /* structured block */ [#pragma omp section ]... /* structured block */ brackets here represent that section is optional, not the syntax for using them 23

Using the sections Directive #pragma omp parallel #pragma omp sections #pragma omp section taska(); #pragma omp section taskb(); #pragma omp section taskc(); parallel section encloses all parallel work sections: task parallelism three concurrent tasks; tasks need not be procedure calls 24

25 Nesting parallel Directives Nested parallelism enabled using the OMP_NESTED environment variable OMP_NESTED = TRUE nested parallelism is enabled Each parallel directive creates a new team of threads F o r k J o i n F o r k J o i n F o r k J o i n F o r k J o i n master thread shown in red

Synchronization Constructs in OpenMP #pragma omp barrier wait until all threads arrive here #pragma omp single [clause list] structured block #pragma omp master structured block single-threaded execution Use MASTER instead of SINGLE wherever possible MASTER = IF-statement with no implicit BARRIER equivalent to IF(omp_get_thread_num() == 0)... SINGLE: implemented like other worksharing constructs keeping track of which thread reached SINGLE first adds overhead 26

Synchronization Constructs in OpenMP #pragma omp critical [(name)] structured block critical section: like a named lock #pragma omp ordered structured block for loops with carried dependences 27

Example Using critical #pragma omp parallel #pragma omp for nowait shared(best_cost) for (i = 0; i < nmax; i++) my_cost =...; #pragma omp critical if (best_cost < my_cost) best_cost = my_cost; critical ensures mutual exclusion when accessing shared state 28

Example Using ordered #pragma omp parallel #pragma omp for nowait shared(a) for (k = 0; k < nmax; k++) #pragma omp ordered a[k] = a[k-1] + ; ordered ensures carried dependence does not cause a data race 29

Orphaned Directives Directives may not be lexically nested in a parallel region may occur in a separate program unit...!$omp parallel call phase1 call phase2!$omp end parallel... subroutine phase1!$omp do private(i) shared(n) do i = 1, n call some_work(i) end do!$omp end do end subroutine phase2!$omp do private(j) shared(n) do j = 1, n call more_work(j) end do!$omp end do end Dynamically bind to enclosing parallel region at run time Benefits enables parallelism to be added with a minimum of restructuring improves performance: enables single parallel region to bind with worksharing constructs in multiple called routines Execution rules an orphaned worksharing construct is executed serially when not called from within a parallel region 30

OpenMP 3.0 Tasks Motivation: support parallelization of irregular problems unbounded loops recursive algorithms producer consumer What is a task? work unit execution can begin immediately, or be deferred components of a task code to execute, data environment, internal control variables Task execution data environment is constructed at creation tasks are executed by threads of a team a task can be tied to a thread (i.e. migration/stealing not allowed) by default: a task is tied to the first thread that executes it 31

OpenMP 3.0 Tasks #pragma omp task [clause list] Possible clauses in [clause list] Conditional parallelization if (scalar expression) determines whether the construct creates a task Binding to threads untied Data scoping private (variable list) specifies variables local to the child task firstprivate (variable list) similar to the private private variables are initialized to value in parent task before the directive shared (variable list) specifies that variables are shared with the parent task default (data handling specifier) default data handling specifier may be shared or none 32

Composing Tasks and Regions #pragma omp parallel #pragma omp task x(); #pragma omp barrier #pragma omp single #pragma omp task y(); one x task created for each thread in the parallel region all x tasks complete at barrier one y task created region end: y task completes 33

Data Scoping for Tasks is Tricky If no default clause specified Static and global variables are shared Automatic (local) variables are private Variables for orphaned tasks are firstprivate by default Variables for non-orphaned tasks inherit the shared attribute task variables are firstprivate unless shared in the enclosing context 34

Fibonacci using (Power9ed) OpenMP 3.0 Tasks int fib ( int n ) int x,y; if ( n < 2 ) return n; #pragma omp task shared(x) x = fib(n - 1); #pragma omp task shared(y) y = fib(n - 2); #pragma omp taskwait return x + y; need shared for x and y; default would be firstprivate suspend parent task until children finish int main (int argc, char **argv) int n, result; n = atoi(argv[1]); #pragma omp parallel #pragma omp single result = fib(n); printf( fib(%d) = %d\n, n, result); only one thread performs the outermost call create team of threads to execute tasks 35

List Traversal Element first, e; #pragma omp parallel #pragma omp single for (e = first; e; e = e->next) #pragma omp task firstprivate(e) process(e); Is the use of variables safe as written? 36

Task Scheduling Tied tasks only the thread that the task is tied to may execute it task can only be suspended at a suspend point task creation task finish taskwait barrier if a task is not suspended at a barrier, it can only switch to a descendant of any task tied to the thread Untied tasks no scheduling restrictions can suspend at any point can switch to any task implementation may schedule for locality and/or load balance 37

Summary of Clause Applicability 38

Performance Tuning Hints Parallelize at the highest level, e.g. outermost DO/for loops!$omp PARALLEL... do j = 1, 20000!$OMP DO do k = 1, 10000... enddo!k!$omp END DO enddo!j...!$omp END PARALLEL!$OMP PARALLEL...!$OMP DO do k = 1, 10000 do j = 1, 20000... enddo!j enddo!k!$omp END DO...!$OMP END PARALLEL Slower Faster 39

Performance Tuning Hints Merge independent parallel loops when possible!$omp PARALLEL...!$OMP DO statement 1!$OMP END DO!$OMP DO statement 2!$OMP END DO...!$OMP END PARALLEL!$OMP PARALLEL...!$OMP DO statement 1 statement 2!$OMP END DO...!$OMP END PARALLEL Slower Faster 40

Performance Tuning Hints Minimize use of synchronization BARRIER CRITICAL sections if necessary, use named CRITICAL for fine-grained locking ORDERED regions Use NOWAIT clause to avoid unnecessary barriers adding NOWAIT to a region s final DO eliminates a redundant barrier data =... #pragma omp flush (data) data_available = true; Use explicit FLUSH with care flushes can evict cached values subsequent data accesses may require reloads from memory 41

OpenMP Library Functions Processor count int omp_get_num_procs(); /* # processors currently available */ int omp_in_parallel(); /* determine whether running in parallel */ Thread count and identity /* max # threads for next parallel region. only call in serial region */ void omp_set_num_threads(int num_threads); int omp_get_num_threads(); /*# threads currently active */ 42

OpenMP Library Functions Controlling and monitoring thread creation void omp_set_dynamic (int dynamic_threads); int omp_get_dynamic (); void omp_set_nested (int nested); int omp_get_nested (); Mutual exclusion void omp_init_lock(omp_lock_t *lock); void omp_destroy_lock(omp_lock_t *lock); void omp_set_lock(omp_lock_t *lock); void omp_unset_lock(omp_lock_t *lock); int omp_test_lock(omp_lock_t *lock); Lock routines have a nested lock counterpart for recursive mutexes 43

OpenMP Environment Variables OMP_NUM_THREADS specifies the default number of threads for a parallel region OMP_DYNAMIC specfies if the number of threads can be dynamically changed OMP_NESTED enables nested parallelism (may be nominal: one thread) OMP_SCHEDULE specifies scheduling of for-loops if the clause specifies runtime OMP_STACKSIZE (for non-master threads) OMP_WAIT_POLICY (active or passive) OMP_MAX_ACTIVE_LEVELS integer value for maximum # nested parallel regions OMP_THREAD_LIMIT (# threads for entire program) 44

OpenMP Directives vs. Library-based Models Directive advantages directives facilitate a variety of thread-related tasks frees programmer from initializing attribute objects setting up thread arguments partitioning iteration spaces, Directive disadvantages data exchange is less apparent leads to mysterious overheads data movement, false sharing, and contention API is less expressive than Pthreads lacks condition waits, locks of different types, and flexibility for building composite synchronization operations 45

OpenMP is Continuing to Evolve OpenMP 4.5 is the most recent standard (November 2015) Features new to OpenMP 4 SIMD support e.g., a[0:n-1] = 0 locality and affinity control mapping of threads to processor cores proc_bind ( master, spread, close ) additional synchronization mechanisms e.g., taskgroup, taskwait offload computation to accelerators, e.g. GPUs Work in progress OpenMP Tools API (OMPT) 46

References Blaise Barney. LLNL OpenMP tutorial. http://www.llnl.gov/computing/tutorials/ openmp Adapted from slides Programming Shared Address Space Platforms by Ananth Grama Ananth Grama, Anshul Gupta, George Karypis, and Vipin Kumar. Introduction to Parallel Computing. Chapter 7. Addison Wesley, 2003. Sun Microsystems. OpenMP OpenMP API User's Guide. Chapter 7 Performance Considerations http://docs.sun.com/source/819-3694/7_tuning.html Alberto Duran. OpenMP 3.0: What s New?. IWOMP 2008. http:// cobweb.ecn.purdue.edu/paramount/iwomp2008/documents/omp30 Stephen Blair-Chappell. Expressing Parallelism Using the Intel Compiler. http://www.polyhedron.com/web_images/documents/expressing%20parallelism %20Using%20Intel%20Compiler.pdf Rusty Lusk et al. Programming Models and Runtime Systems, Exascale Software Center Meeting, ANL, Jan. 2011. OpenMP 4.5 Standard, http://www.openmp.org/mp-documents/openmp-4.5.pdf OpenMP Application Program Interface Examples. http://openmp.org/mpdocuments/openmp_examples_4.0.1.pdf 47