OpenMP Tutorial. Rudi Eigenmann of Purdue, Sanjiv Shah of Intel and others too numerous to name have contributed content for this tutorial.

Similar documents
COSC 6374 Parallel Computation. Introduction to OpenMP. Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel)

Data Environment: Default storage attributes

Lecture 2 A Hand-on Introduction to OpenMP

COSC 6374 Parallel Computation. Introduction to OpenMP(I) Some slides based on material by Barbara Chapman (UH) and Tim Mattson (Intel)

Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG

Lecture 2 A Hand-on Introduction to OpenMP

Introduction to. Slides prepared by : Farzana Rahman 1

Introduction to OpenMP

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

Parallel Programming Principle and Practice. Lecture 6 Shared Memory Programming OpenMP. Jin, Hai

Programming with OpenMP*

Multi-core Architecture and Programming

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

Cluster Computing 2008


OpenMP: The "Easy" Path to Shared Memory Computing

OpenMP Tutorial. Seung-Jai Min. School of Electrical and Computer Engineering Purdue University, West Lafayette, IN

A Hands-on Introduction to OpenMP *

PARLab Parallel Boot Camp

EPL372 Lab Exercise 5: Introduction to OpenMP

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

Shared Memory Programming Paradigm!

Introduction to OpenMP

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

Parallel programming using OpenMP

Massimo Bernaschi Istituto Applicazioni del Calcolo Consiglio Nazionale delle Ricerche.

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

Introduction to OpenMP.

Alfio Lazzaro: Introduction to OpenMP

DPHPC: Introduction to OpenMP Recitation session

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

Shared Memory Parallelism using OpenMP

Parallel Programming with OpenMP. CS240A, T. Yang

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

The OpenMP * Common Core:

Multicore Computing. Arash Tavakkol. Instructor: Department of Computer Engineering Sharif University of Technology Spring 2016

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

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

DPHPC: Introduction to OpenMP Recitation session

Shared memory programming

Parallelising Scientific Codes Using OpenMP. Wadud Miah Research Computing Group

Shared Memory Parallelism - OpenMP

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

COMP4300/8300: The OpenMP Programming Model. Alistair Rendell

Introduction to OpenMP

Multithreading in C with OpenMP

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

Introduction to OpenMP

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

Overview: The OpenMP Programming Model

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

Introduc4on to OpenMP and Threaded Libraries Ivan Giro*o

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

Introduction to OpenMP

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

OpenMP * Past, Present and Future

Introduction to OpenMP

Session 4: Parallel Programming with OpenMP

Introduction to Programming with OpenMP

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

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

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

OPENMP OPEN MULTI-PROCESSING

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

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

OpenMP on Ranger and Stampede (with Labs)

Programming with OpenMP* Intel Software College

Lecture 4: OpenMP Open Multi-Processing

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

A Hands-on Introduction to OpenMP *

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

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

Computational Mathematics

OpenMP Algoritmi e Calcolo Parallelo. Daniele Loiacono

OpenMP programming. Thomas Hauser Director Research Computing Research CU-Boulder

Shared Memory Programming with OpenMP

Parallel Programming in C with MPI and OpenMP

CMSC 714 Lecture 4 OpenMP and UPC. Chau-Wen Tseng (from A. Sussman)

cp r /global/scratch/workshop/openmp-wg-oct2017 cd openmp-wg-oct2017 && ls Current directory

OpenMP - Introduction

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

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

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

Practical in Numerical Astronomy, SS 2012 LECTURE 12

Introduction to OpenMP

OPENMP TIPS, TRICKS AND GOTCHAS

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

Mango DSP Top manufacturer of multiprocessing video & imaging solutions.

Data Handling in OpenMP

Introduction to OpenMP. Motivation

ECE 574 Cluster Computing Lecture 10

Distributed Systems + Middleware Concurrent Programming with OpenMP

Programming Shared-memory Platforms with OpenMP. Xu Liu

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

Introduction to OpenMP

15-418, Spring 2008 OpenMP: A Short Introduction

Introduction to Standard OpenMP 3.1

Introduction to OpenMP

Parallel Programming using OpenMP

Parallel Programming using OpenMP

Transcription:

OpenMP * in Action Tim Mattson Intel Corporation Barbara Chapman University of Houston Acknowledgements: Rudi Eigenmann of Purdue, Sanjiv Shah of Intel and others too numerous to name have contributed content for this tutorial. * The name OpenMP is the property of the OpenMP Architecture Review Board. 1 Agenda Parallel computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables Recapitulation 2 The OpenMP API for Multithreaded Programming 1

OpenMP * Overview: C$OMP THREADPRIVATE(/ABC/) C$OMP parallel do shared(a, b, c) call OMP_INIT_LOCK (ilok) C$OMP SINGLE PRIVATE(X) C$OMP FLUSH #pragma omp critical C$OMP PARALLEL REDUCTION (+: A, B) #pragma omp parallel for private(a, B) CALL OMP_SET_NUM_THREADS(10) OpenMP: An API for Writing Multithreaded Applications call omp_test_lock(jlok) C$OMP ATOMIC C$OMP PARALLEL DO ORDERED PRIVATE (A, B, C) C$OMP MASTER A set of compiler setenv directives OMP_SCHEDULE and library dynamic routines for parallel application programmers C$OMP ORDERED Greatly simplifies writing multi-threaded (MT) programs in Fortran, C and C++ C$OMP SECTIONS Standardizes last 20 years of SMP practice!$omp BARRIER C$OMP PARALLEL COPYIN(/blk/) C$OMP DO lastprivate(xx) Nthrds = OMP_GET_NUM_PROCS() omp_set_lock(lck) * The name OpenMP is the property of the OpenMP Architecture Review Board. 3 What are Threads? Thread: an independent flow of control Runtime entity created to execute sequence of instructions Threads require: A program counter A register state An area in memory, including a call stack A thread id A process is executed by one or more threads that share: Address space Attributes such as UserID, open files, working directory, etc. 4 The OpenMP API for Multithreaded Programming 2

A Shared Memory Architecture Shared memory cache1 cache2 cache3 cachen proc1 proc2 proc3 procn 5 How Can We Exploit Threads? A thread programming model must provide (at least) the means to: Create and destroy threads Distribute the computation among threads Coordinate actions of threads on shared data (usually) specify which data is shared and which is private to a thread 6 The OpenMP API for Multithreaded Programming 3

How Does OpenMP Enable Us to Exploit Threads? OpenMP provides thread programming model at a high level. The user does not need to specify all the details Especially with respect to the assignment of work to threads Creation of threads User makes strategic decisions Compiler figures out details Alternatives: MPI POSIX thread library is lower level Automatic parallelization is even higher level (user does nothing) But usually successful on simple codes only 7 OpenMP Overview: How do threads interact? OpenMP is a shared memory model. Threads communicate by sharing variables. Unintended sharing of data causes race conditions: race condition: when the program s outcome changes as the threads are scheduled differently. To control race conditions: Use synchronization to protect data conflicts. Synchronization is expensive so: Change how data is accessed to minimize the need for synchronization. 8 The OpenMP API for Multithreaded Programming 4

Agenda Parallel Computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables Recapitulation 9 OpenMP: Some syntax details to get us started Most of the constructs in OpenMP are compiler directives. For C and C++, the directives are pragmas with the form: #pragma omp construct [clause [clause] ] For Fortran, the directives are comments and take one of the forms: Fixed form *$OMP construct [clause [clause] ] C$OMP construct [clause [clause] ] Free form (but works for fixed form too)!$omp construct [clause [clause] ] Include file and the OpenMP lib module #include <omp.h> use omp_lib 10 The OpenMP API for Multithreaded Programming 5

OpenMP: Structured blocks (C/C++) Most OpenMP* constructs apply to structured blocks. Structured block: a block with one point of entry at the top and one point of exit at the bottom. The only branches allowed are STOP statements in Fortran and exit() in C/C++. #pragma omp parallel int id = omp_get_thread_num(); more: res[id] = do_big_job(id); if(!conv(res[id]) goto more; printf( All done \n ); A structured block if(go_now()) goto more; #pragma omp parallel int id = omp_get_thread_num(); more: res[id] = do_big_job(id); if(conv(res[id]) goto done; go to more; done: if(!really_done()) goto more; Not A structured block 11 OpenMP: Structured Block Boundaries In C/C++: a block is a single statement or a group of statements between brackets #pragma omp parallel id = omp_get_thread_num(); res(id) = lots_of_work(id); In Fortran: a block is a single statement or a group of statements between directive/end-directive pairs. C$OMP PARALLEL 10 wrk(id) = garbage(id) res(id) = wrk(id)**2 if(.not.conv(res(id)) goto 10 C$OMP END PARALLEL #pragma omp for for(i=0;i<n;i++) res[i] = big_calc(i); A[I] = B[I] + res[i]; C$OMP PARALLEL DO do I=1,N res(i)=bigcomp(i) end do C$OMP END PARALLEL DO 12 The OpenMP API for Multithreaded Programming 6

Agenda Parallel computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables Recapitulation The OpenMP compiler OpenMP usage: common design patterns Case studies and examples Background information and extra details 13 The OpenMP* API Parallel Regions You create threads in OpenMP* with the omp parallel pragma. For example, To create a 4 thread Parallel region: Each thread executes a copy of the code within the structured block double A[1000]; omp_set_num_threads(4); #pragma omp parallel int ID = omp_get_thread_num(); pooh(id,a); Each thread calls pooh(id,a) for ID = 0 to 3 Runtime function to request a certain number of threads Runtime function returning a thread ID * The name OpenMP is the property of the OpenMP Architecture Review Board 14 The OpenMP API for Multithreaded Programming 7

The OpenMP* API Parallel Regions Each thread executes the same code redundantly. double A[1000]; omp_set_num_threads(4) double A[1000]; omp_set_num_threads(4); #pragma omp parallel int ID = omp_get_thread_num(); pooh(id, A); printf( all done\n ); A single copy of A is shared between all threads. pooh(0,a) pooh(1,a) pooh(2,a) pooh(3,a) printf( all done\n ); Threads wait here for all threads to finish before proceeding (i.e. a barrier) * The name OpenMP is the property of the OpenMP Architecture Review Board 15 Exercise: A multi-threaded Hello world program Write a multithreaded program where each thread prints hello world. void main() int ID = 0; printf( hello(%d), ID); printf( world(%d) \n, ID); 16 The OpenMP API for Multithreaded Programming 8

Exercise: A multi-threaded Hello world program Write a multithreaded program where each thread prints hello world. #include omp.h void main() #pragma omp parallel Parallel region with default number of threads OpenMP include file int ID = omp_get_thread_num(); printf( hello(%d), ID); printf( world(%d) \n, ID); End of the Parallel region Sample Output: hello(1) hello(0) world(1) world(0) hello (3) hello(2) world(3) world(2) Runtime library function to return a thread ID. 17 Agenda Parallel computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables Recapitulation 18 The OpenMP API for Multithreaded Programming 9

OpenMP: Work-Sharing Constructs The for Work-Sharing construct splits up loop iterations among the threads in a team #pragma omp parallel #pragma omp for for (I=0;I<N;I++) NEAT_STUFF(I); By default, there is a barrier at the end of the omp for. Use the nowait clause to turn off the barrier. #pragma omp for nowait nowait is useful between two consecutive, independent omp for loops. 19 Work Sharing Constructs A motivating example Sequential code for(i=0;i<n;i++) a[i] = a[i] + b[i]; OpenMP parallel region #pragma omp parallel int id, i, Nthrds, istart, iend; id = omp_get_thread_num(); Nthrds = omp_get_num_threads(); istart = id * N / Nthrds; iend = (id+1) * N / Nthrds; for(i=istart;i<iend;i++) a[i] = a[i] + b[i]; OpenMP parallel region and a work-sharing forconstruct #pragma omp parallel #pragma omp for schedule(static) for(i=0;i<n;i++) a[i] = a[i] + b[i]; 20 The OpenMP API for Multithreaded Programming 10

OpenMP For/Do construct: The schedule clause The schedule clause affects how loop iterations are mapped onto threads schedule(static [,chunk]) Deal-out blocks of iterations of size chunk to each thread. schedule(dynamic[,chunk]) Each thread grabs chunk iterations off a queue until all iterations have been handled. schedule(guided[,chunk]) Threads dynamically grab blocks of iterations. The size of the block starts large and shrinks down to size chunk as the calculation proceeds. schedule(runtime) Schedule and chunk size taken from the OMP_SCHEDULE environment variable. 21 The OpenMP API The schedule clause Schedule Clause STATIC DYNAMIC GUIDED When To Use Pre-determined and predictable by the programmer Unpredictable, highly variable work per iteration Special case of dynamic to reduce scheduling overhead Least work at runtime : scheduling done at compile-time Most work at runtime : complex scheduling logic used at run-time 22 The OpenMP API for Multithreaded Programming 11

OpenMP: Work-Sharing Constructs The Sections work-sharing construct gives a different structured block to each thread. #pragma omp parallel #pragma omp sections #pragma omp section X_calculation(); #pragma omp section y_calculation(); #pragma omp section z_calculation(); By default, there is a barrier at the end of the omp sections. Use the nowait clause to turn off the barrier. 23 OpenMP: Work-Sharing Constructs The master construct denotes a structured block that is only executed by the master thread. The other threads just skip it (no synchronization is implied). #pragma omp parallel private (tmp) do_many_things(); #pragma omp master exchange_boundaries(); #pragma barrier do_many_other_things(); 24 The OpenMP API for Multithreaded Programming 12

OpenMP: Work-Sharing Constructs The single construct denotes a block of code that is executed by only one thread. A barrier is implied at the end of the single block. #pragma omp parallel private (tmp) do_many_things(); #pragma omp single exchange_boundaries(); do_many_other_things(); 25 The OpenMP* API Combined parallel/work-share OpenMP* shortcut: Put the parallel and the workshare on the same line double res[max]; int i; #pragma omp parallel #pragma omp for for (i=0;i< MAX; i++) res[i] = huge(); double res[max]; int i; #pragma omp parallel for for (i=0;i< MAX; i++) res[i] = huge(); These are equivalent There s also a parallel sections construct. 26 The OpenMP API for Multithreaded Programming 13

Agenda Parallel computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables Recapitulation 27 Data Environment: Default storage attributes Shared Memory programming model: Most variables are shared by default Global variables are SHARED among threads Fortran: COMMON blocks, SAVE variables, MODULE variables C: File scope variables, static But not everything is shared... Stack variables in sub-programs called from parallel regions are PRIVATE Automatic variables within a statement block are PRIVATE. 28 The OpenMP API for Multithreaded Programming 14

Data Sharing Examples program sort common /input/ A(10) integer index(10)!$omp PARALLEL call work(index)!$omp END PARALLEL print*, index(1) subroutine work (index) common /input/ A(10) integer index(*) real temp(10) integer count save count A, index, count A, index and count are shared by all threads. temp temp temp is local to each thread A, index, count * Third party trademarks and names are the property of their respective owner. temp 29 Data Environment: Changing storage attributes One can selectively change storage attributes constructs using the following clauses* SHARED PRIVATE FIRSTPRIVATE THREADPRIVATE The value of a private inside a parallel loop can be transmitted to a global value outside the loop with: LASTPRIVATE All the clauses on this page apply to OpenMP construct NOT to the entire region. The default status can be modified with: DEFAULT (PRIVATE SHARED NONE) All data clauses apply to parallel regions and worksharing constructs except shared which only applies to parallel regions. 30 The OpenMP API for Multithreaded Programming 15

Private Clause private(var) creates a local copy of var for each thread. The value is uninitialized Private copy is not storage-associated with the original The original is undefined at the end Regardless of initialization, IS is undefined at this point program wrong IS = 0 C$OMP PARALLEL DO PRIVATE(IS) DO J=1,1000 IS = IS + J END DO print *, IS IS was not initialized 31 Firstprivate Clause Firstprivate is a special case of private. Initializes each private copy with the corresponding value from the master thread. program almost_right IS = 0 C$OMP PARALLEL DO FIRSTPRIVATE(IS) DO J=1,1000 IS = IS + J 1000 CONTINUE print *, IS Each thread gets its own IS with an initial value of 0 Regardless of initialization, IS is undefined at this point 32 The OpenMP API for Multithreaded Programming 16

Lastprivate Clause Lastprivate passes the value of a private from the last iteration to a global variable. program closer IS = 0 C$OMP PARALLEL DO FIRSTPRIVATE(IS) C$OMP+ LASTPRIVATE(IS) DO J=1,1000 IS = IS + J 1000 CONTINUE print *, IS Each thread gets its own IS with an initial value of 0 IS is defined as its value at the last sequential iteration (i.e. for J=1000) 33 OpenMP: A data environment test Consider this example of PRIVATE and FIRSTPRIVATE variables A,B, and C = 1 C$OMP PARALLEL PRIVATE(B) C$OMP& FIRSTPRIVATE(C) Are A,B,C local to each thread or shared inside the parallel region? What are their initial values inside and after the parallel region? Inside this parallel region... A is shared by all threads; equals 1 B and C are local to each thread. B s initial value is undefined C s initial value equals 1 Outside this parallel region... The values of B and C are undefined. 34 The OpenMP API for Multithreaded Programming 17

OpenMP: Reduction Combines an accumulation operation across threads: reduction (op : list) Inside a parallel or a work-sharing construct: A local copy of each list variable is made and initialized depending on the op (e.g. 0 for + ). Compiler finds standard reduction expressions containing op and uses them to update the local copy. Local copies are reduced into a single value and combined with the original global value. The variables in list must be shared in the enclosing parallel region. 35 OpenMP: Reduction example Remember the code we used to demo private, firstprivate and lastprivate. program closer IS = 0 DO J=1,1000 IS = IS + J 1000 CONTINUE print *, IS Here is the correct way to parallelize this code. program closer IS = 0 #pragma omp parallel for reduction(+:is) DO J=1,1000 IS = IS + J 1000 CONTINUE print *, IS 36 The OpenMP API for Multithreaded Programming 18

OpenMP: Reduction operands/initial-values A range of associative operands can be used with reduction: Initial values are the ones that make sense mathematically. Operand Initial value + 0 * 1-0.AND..OR..NEQV..EQV. MIN* MAX*.true..false..false..true. Largest pos. number Most neg. number Operand Initial value iand All bits on ior 0 ieor 0 & ~0 0 ^ 0 && 1 0 * Min and Max are not available in C/C++ 37 Default Clause Note that the default storage attribute is DEFAULT(SHARED) (so no need to use it) To change default: DEFAULT(PRIVATE) each variable in static extent of the parallel region is made private as if specified in a private clause mostly saves typing DEFAULT(NONE): no default for variables in static extent. Must list storage attribute for each variable in static extent Only the Fortran API supports default(private). C/C++ only has default(shared) or default(none). 38 The OpenMP API for Multithreaded Programming 19

Default Clause Example itotal = 1000 C$OMP PARALLEL PRIVATE(np, each) np = omp_get_num_threads() each = itotal/np C$OMP END PARALLEL Are these two codes equivalent? itotal = 1000 C$OMP PARALLEL DEFAULT(PRIVATE) SHARED(itotal) np = omp_get_num_threads() each = itotal/np C$OMP END PARALLEL yes 39 Agenda Parallel computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables Recapitulation 40 The OpenMP API for Multithreaded Programming 20

OpenMP: Synchronization High level synchronization: critical atomic barrier ordered Low level synchronization flush locks (both simple and nested) 41 The OpenMP* API Synchronization critical (in C/C++) Only one thread at a time can enter a critical region. float res; Threads wait their turn only one at a time calls consume() #pragma omp parallel float B; int i; #pragma omp for for(i=0;i<niters;i++) B = big_job(i); #pragma omp critical consume (B, RES); * The mark OpenMP is the property of the OpenMP Architecture Review Board. 42 The OpenMP API for Multithreaded Programming 21

OpenMP: Synchronization Atomic provides mutual exclusion execution but only applies to the update of a memory location (the update of X in the following example) C$OMP PARALLEL PRIVATE(B) B = DOIT(I) tmp = big_ugly(); C$OMP ATOMIC X = X + temp C$OMP END PARALLEL 43 OpenMP: Synchronization Barrier: Each thread waits until all threads arrive. #pragma omp parallel shared (A, B, C) private(id) id=omp_get_thread_num(); A[id] = big_calc1(id); implicit barrier at the #pragma omp barrier end of a for worksharing construct #pragma omp for for(i=0;i<n;i++)c[i]=big_calc3(i,a); #pragma omp for nowait for(i=0;i<n;i++) B[i]=big_calc2(C, i); A[id] = big_calc3(id); implicit barrier at the end no implicit barrier of a parallel region due to nowait44 The OpenMP API for Multithreaded Programming 22

OpenMP: Synchronization The ordered region executes in the sequential order. #pragma omp parallel private (tmp) #pragma omp for ordered for (I=0;I<N;I++) tmp = NEAT_STUFF(I); #pragma ordered res += consum(tmp); 45 OpenMP: Implicit synchronization Barriers are implied on the following OpenMP constructs: end parallel end do (except when nowait is used) end sections (except when nowait is used) end single (except when nowait is used) 46 The OpenMP API for Multithreaded Programming 23

Agenda Parallel computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables Recapitulation 47 OpenMP: Library routines: Runtime environment routines: Modify/Check the number of threads omp_set_num_threads(), omp_get_num_threads(), omp_get_thread_num(), omp_get_max_threads() Are we in a parallel region? omp_in_parallel() How many processors in the system? omp_num_procs() plus several less commonly used routines. 48 The OpenMP API for Multithreaded Programming 24

OpenMP: Library Routines To use a known, fixed number of threads used in a program, (1) tell the system that you don t want dynamic adjustment of the number of threads, (2) set the number threads, then (3) save the number you got. #include <omp.h> void main() int num_threads; omp_set_dynamic( 0 ); omp_set_num_threads( omp_num_procs() ); #pragma omp parallel int id=omp_get_thread_num(); #pragma omp single num_threads = omp_get_num_threads(); do_lots_of_stuff(id); Disable dynamic adjustment of the number of threads. Request as many threads as you have processors. Protect this op since Memory stores are not atomic Even in this case, the system may give you fewer threads than requested. If the precise # of threads matters, test for it and respond accordingly. 49 OpenMP: Environment Variables: Control how omp for schedule(runtime) loop iterations are scheduled. OMP_SCHEDULE schedule[, chunk_size] Set the default number of threads to use. OMP_NUM_THREADS int_literal Plus several less commonly used environment variables. 50 The OpenMP API for Multithreaded Programming 25

Agenda Parallel Computing, threads, and OpenMP The core elements of OpenMP Thread creation Workshare constructs Managing the data environment Synchronization The runtime library and environment variables recapitulation 51 Let s pause for a quick recap by example: Numerical Integration Mathematically, we know that: 4.0 1 0 4.0 (1+x 2 ) dx = π 2.0 We can approximate the integral as a sum of rectangles: N F(x i ) x π i = 0 0.0 X 1.0 Where each rectangle has width x and height F(x i ) at the middle of interval i. 52 The OpenMP API for Multithreaded Programming 26

PI Program: an example static long num_steps = 100000; double step; void main () int i; double x, pi, sum = 0.0; step = 1.0/(double) num_steps; for (i=0;i<= num_steps; i++) x = (i+0.5)*step; sum = sum + 4.0/(1.0+x*x); pi = step * sum; 53 #include <omp.h> static long num_steps = 100000; double step; #define NUM_THREADS 2 void main () int i, id, nthreads; double x, pi, sum[num_threads]; step = 1.0/(double) num_steps; omp_set_num_threads(num_threads); #pragma omp parallel private (i, id, x) id = omp_get_thread_num(); #pragma omp single nthreads = omp_get_num_threads(); OpenMP recap: Parallel Region for (i=id, sum[id]=0.0;i< num_steps; i=i+nthreads) x = (i+0.5)*step; sum[id] += 4.0/(1.0+x*x); for(i=0, pi=0.0;i<nthreads;i++)pi += sum[i] * step; Promote scalar to an array dimensioned by number of threads to avoid race condition. You can t t assume that you ll get the number of threads you requested. Prevent write conflicts with the single. Performance will suffer due to false sharing of the sum array. 54 The OpenMP API for Multithreaded Programming 27

OpenMP recap: Synchronization (critical region) #include <omp.h> static long num_steps = 100000; double step; #define NUM_THREADS 2 void main () int i, id, nthreads; double x, pi, sum; step = 1.0/(double) num_steps; omp_set_num_threads(num_threads); #pragma omp parallel private (i, id, x, sum) id = omp_get_thread_num(); #pragma omp single nthreads = omp_get_num_threads(); for (i=id, sum=0.0;i< num_steps; i=i+nthreads) x = (i+0.5)*step; sum += 4.0/(1.0+x*x); #pragma omp critical pi += sum * step; No array, so no false sharing. Note: this method of combining partial sums doesn t scale very well. 55 OpenMP recap : Parallel for with a reduction i private by default #include <omp.h> static long num_steps = 100000; double step; #define NUM_THREADS 2 void main () int i; double x, pi, sum = 0.0; step = 1.0/(double) num_steps; omp_set_num_threads(num_threads); #pragma omp parallel for private(x) reduction(+:sum) for (i=0;i<= num_steps; i++) x = (i+0.5)*step; sum = sum + 4.0/(1.0+x*x); pi = step * sum; For good OpenMP implementations, reduction is more scalable than critical. Note: we created a parallel program without changing any code and by adding 4 simple lines! 56 The OpenMP API for Multithreaded Programming 28

OpenMP recap : Use environment variables to set number of threads #include <omp.h> static long num_steps = 100000; double step; void main () int i; double x, pi, sum = 0.0; step = 1.0/(double) num_steps; #pragma omp parallel for private(x) reduction(+:sum) for (i=0;i<= num_steps; i++) x = (i+0.5)*step; sum = sum + 4.0/(1.0+x*x); pi = step * sum; In practice, you set number of threads by setting the environment variable, OMP_NUM_THREADS 57 The OpenMP API for Multithreaded Programming 29