SC’05 OpenMP Tutorial

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. 1
* The name “OpenMP” is the property of the OpenMP Architecture Review Board.

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
2

Copyright is held by the authors/owners

The OpenMP API for Multithreaded Programming

1

SC’05 OpenMP Tutorial
OpenMP* Overview:
C$OMP FLUSH C$OMP THREADPRIVATE(/ABC/) #pragma omp critical CALL OMP_SET_NUM_THREADS(10) C$OMP OpenMP: shared(a, b, c) parallel do An API for Writing Multithreaded call omp_test_lock(jlok) call OMP_INIT_LOCK (ilok) C$OMP

Applications

C$OMP ATOMIC

C$OMP MASTER

routines for parallel application programmers C$OMP ORDERED Greatly simplifies writing multi-threaded (MT) C$OMP PARALLEL REDUCTION (+: A, B) programs in Fortran, C and C++ C$OMP SECTIONS #pragma ompStandardizes last 20 years of !$OMPpractice parallel for private(A, B) SMP BARRIER
C$OMP PARALLEL DO ORDERED PRIVATE (A, B, C) C$OMP PARALLEL COPYIN(/blk/) C$OMP DO lastprivate(XX) omp_set_lock(lck)
3

SINGLE set of compiler directives and library A PRIVATE(X) setenv OMP_SCHEDULE “dynamic”

Nthrds = OMP_GET_NUM_PROCS()

* The name “OpenMP” is the property of the OpenMP Architecture Review Board.

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, 4 etc.

The OpenMP API for Multithreaded Programming

2

SC’05 OpenMP Tutorial
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

– 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 Where Does OpenMP Run? Hardware Platforms Shared Memory Systems Distributed Shared Memory Systems (ccNUMA) Distributed Memory Systems Machines with Chip MultiThreading OpenMP support Available Available cache cache cache cache CPU CPU CPU CPU Shared bus Available via Software DSM Available Shared Memory Shared memory architecture 8 The OpenMP API for Multithreaded Programming 4 .SC’05 OpenMP Tutorial How Does OpenMP Enable Us to Exploit Threads? • OpenMP provides thread programming model at a “high level”.

9 Agenda • Parallel computing. • Threads communicate by sharing variables. threads. • 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. 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 10 The OpenMP API for Multithreaded Programming 5 .SC’05 OpenMP Tutorial OpenMP Overview: How do threads interact? • OpenMP is a shared memory model. • Unintended sharing of data causes race conditions: • race condition: when the program’s outcome changes as the threads are scheduled differently.

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. Layer (OpenMP API) OpenMP library System layer Runtime library OS/system support for shared memory. 11 OpenMP: Some syntax details to get us started • Most of the constructs in OpenMP are compiler directives. Compiler Environment variables Prog.h> use omp_lib 12 The OpenMP API for Multithreaded Programming 6 . the directives are pragmas with the form: #pragma omp construct [clause [clause]…] – For Fortran.SC’05 OpenMP Tutorial OpenMP Parallel Computing Solution Stack User layer End User Application Directives. – For C and C++.

Not A structured block 13 A structured block OpenMP: Structured blocks (Fortran) – Most OpenMP constructs apply to structured blocks. more: res[id] = do_big_job(id). if(!conv(res[id]) goto more. #pragma omp parallel { int id = omp_get_thread_num(). go to more.not. • The only “branches” allowed are STOP statements in Fortran and exit() in C/C++.conv(res(id)) goto 10 C$OMP END PARALLEL print *. • The only “branches” allowed are STOP statements in Fortran and exit() in C/C++.id C$OMP PARALLEL 10 wrk(id) = garbage(id) 30 res(id)=wrk(id)**2 if(conv(res(id))goto 20 go to 10 C$OMP END PARALLEL if(not_DONE) goto 30 20 print *. • Structured block: a block with one point of entry at the top and one point of exit at the bottom. more: res[id] = do_big_job(id). } printf(“ All done \n”). } done: if(!really_done()) goto more. if(go_now()) goto more. if(conv(res[id]) goto done. #pragma omp parallel { int id = omp_get_thread_num(). id 14 A structured block Not A structured block The OpenMP API for Multithreaded Programming 7 .SC’05 OpenMP Tutorial OpenMP: Structured blocks (C/C++) • Most OpenMP* constructs apply to structured blocks. C$OMP PARALLEL 10 wrk(id) = garbage(id) res(id) = wrk(id)**2 if(. • Structured block: a block of code with one point of entry at the top and one point of exit at the bottom.

SC’05 OpenMP Tutorial 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_thread_num().I++){ res[I] = big_calc(I).not. omp_get_thread_num iam = omp_get_thread_num() C$OMP CRITICAL print*.N res(I)=bigComp(I) end do C$OMP END PARALLEL DO 15 OpenMP Definitions: “Constructs” vs.f subroutine whoami external omp_get_thread_num integer iam.’Hello from ‘. “Regions” in OpenMP OpenMP constructs occupy a single compilation unit while a region can span multiple source files.I<N. poo. A[I] = B[I] + res[I]. C$OMP PARALLEL 10 wrk(id) = garbage(id) res(id) = wrk(id)**2 if(. } #pragma omp for for(I=0. res(id) = lots_of_work(id).conv(res(id)) goto 10 C$OMP END PARALLEL C$OMP PARALLEL DO do I=1. iam C$OMP END CRITICAL Orphan constructs return can execute outside a end parallel region 16 + A Parallel construct The Parallel region is the text of the construct plus any code called inside the construct The OpenMP API for Multithreaded Programming 8 .f call whoami C$OMP PARALLEL call whoami C$OMP END PARALLEL bar. } • In Fortran: a block is a single statement or a group of statements between directive/end-directive pairs.

threads. pooh(ID.A) for ID = 0 to 3 18 * The name “OpenMP” is the property of the OpenMP Architecture Review Board The OpenMP API for Multithreaded Programming 9 . To create a 4 thread Parallel region: Runtime function to double A[1000]. 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 17 The OpenMP* API Parallel Regions • You create threads in OpenMP* with the “omp parallel” pragma.A). • For example. Runtime function } returning a thread ID Each thread executes a copy of the code within the structured block Each thread calls pooh(ID. number of threads #pragma omp parallel { int ID = omp_get_thread_num().SC’05 OpenMP Tutorial Agenda • Parallel computing. request a certain omp_set_num_threads(4).

SC’05 OpenMP Tutorial
The OpenMP* API

Parallel Regions
• You create threads in OpenMP* with the “omp parallel” pragma. • For example, To create a 4 thread Parallel region: double A[1000];
Each thread executes a copy of the code within the structured block clause to request a certain number of threads

#pragma omp parallel num_threads(4) { int ID = omp_get_thread_num(); pooh(ID,A); Runtime function } returning a thread ID
19
* The name “OpenMP” is the property of the OpenMP Architecture Review Board

Each thread calls pooh(ID,A) for ID = 0 to 3

The OpenMP* API

Parallel Regions
• Each thread executes the same code redundantly.
double A[1000]; omp_set_num_threads(4)
A single copy of A is shared between all threads.

double A[1000]; omp_set_num_threads(4); #pragma omp parallel { int ID = omp_get_thread_num(); pooh(ID, A); } printf(“all done\n”);

pooh(0,A)

pooh(1,A)

pooh(2,A)

pooh(3,A)

printf(“all done\n”);

Threads wait here for all threads to 20 finish before proceeding (i.e. a barrier)

* The name “OpenMP” is the property of the OpenMP Architecture Review Board

The OpenMP API for Multithreaded Programming

10

SC’05 OpenMP Tutorial
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); }
21

Exercise:
A multi-threaded “Hello world” program
• Write a multithreaded program where each thread prints “hello world”.
OpenMP include file #include “omp.h” void main() Parallel region with default Sample Output: { number of threads

#pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }
End of the Parallel region

hello(1) hello(0) world(1) world(0) hello (3) hello(2) world(3) world(2)
22

Runtime library function to return a thread ID.

The OpenMP API for Multithreaded Programming

11

SC’05 OpenMP Tutorial
Parallel Regions and the “if” clause
Active vs inactive parallel regions.
• An optional if clause causes the parallel region to be active only if the logical expression within the clause evaluates to true. • An if clause that evaluates to false causes the parallel region to be inactive (i.e. executed by a team of size one). double A[N]; #pragma omp parallel if(N>1000) { int ID = omp_get_thread_num(); pooh(ID,A); }
* The name “OpenMP” is the property of the OpenMP Architecture Review Board

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

• • • •

The OpenMP compiler OpenMP usage: common design patterns Case studies and examples Background information and extra details
24

The OpenMP API for Multithreaded Programming

12

I<N.i++) { a[i] = a[i] + b[i]. Use the “nowait” clause to turn off the barrier.} } #pragma omp parallel #pragma omp for schedule(static) for(i=0. } By default. there is a barrier at the end of the “omp for”. #pragma omp for nowait “nowait” is useful between two consecutive. independent omp for loops.} OpenMP parallel region OpenMP parallel region and a work-sharing forconstruct 26 The OpenMP API for Multithreaded Programming 13 . Nthrds = omp_get_num_threads(). for(i=istart. iend. id = omp_get_thread_num(). Nthrds. istart.I<iend.I++){ NEAT_STUFF(I).i++) { a[i] = a[i] + b[i]. iend = (id+1) * N / Nthrds. 25 Work Sharing Constructs A motivating example Sequential code for(i=0.i++) { a[i] = a[i] + b[i].I<N.I<N. i. istart = id * N / Nthrds.} #pragma omp parallel { int id.SC’05 OpenMP Tutorial 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.

27 The OpenMP API The schedule clause Schedule Clause STATIC DYNAMIC GUIDED When To Use Pre-determined and predictable by the programmer Unpredictable. – schedule(guided[.chunk]) – Deal-out blocks of iterations of size “chunk” to each thread. The size of the block starts large and shrinks down to size “chunk” as the calculation proceeds.chunk]) – Threads dynamically grab blocks of iterations.chunk]) – Each thread grabs “chunk” iterations off a queue until all iterations have been handled.SC’05 OpenMP Tutorial OpenMP For/Do construct: The schedule clause • The schedule clause affects how loop iterations are mapped onto threads – schedule(static [. – schedule(dynamic[. – schedule(runtime) – Schedule and chunk size taken from the OMP_SCHEDULE environment variable. 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 28 The OpenMP API for Multithreaded Programming 14 .

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

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

SC’05 OpenMP Tutorial
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
33

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.
34

The OpenMP API for Multithreaded Programming

17

SC’05 OpenMP Tutorial
Data Sharing Examples
program sort common /input/ A(10) integer index(10) !$OMP PARALLEL call work(index) !$OMP END PARALLEL print*, index(1)
A, index and count are shared by all threads. temp is local to each thread

subroutine work (index) common /input/ A(10) integer index(*) real temp(10) integer count save count ………… A, index, count temp temp temp

A, index, count
* Third party trademarks and names are the property of their respective owner.

35

Data Environment:
Changing storage attributes
• One can selectively change storage attributes constructs using the following clauses*
• • • • SHARED PRIVATE FIRSTPRIVATE THREADPRIVATE All the clauses on this page apply to the OpenMP Construct NOT the entire region.

• The value of a private inside a parallel loop can be transmitted to a global value outside the loop with:
• LASTPRIVATE

• The default status can be modified with:
• DEFAULT (PRIVATE | SHARED | NONE)
All data clauses apply to parallel regions and worksharing constructs except36 “shared” which only applies to parallel regions.

The OpenMP API for Multithreaded Programming

18

SC’05 OpenMP Tutorial

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 program wrong IS = 0 C$OMP PARALLEL DO PRIVATE(IS) DO J=1,1000 IS was not IS = IS + J END DO initialized print *, IS

Regardless of initialization, IS is undefined at this point

37

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 Each thread gets its own IS = IS + J 1000 CONTINUE with an initial value of 0 print *, IS

IS

Regardless of initialization, IS is undefined at this point

38

The OpenMP API for Multithreaded Programming

19

1000 Each thread gets its own IS = IS + J with an initial value of 0 1000 CONTINUE print *..SC’05 OpenMP Tutorial Lastprivate Clause • Lastprivate passes the value of a private from the last iteration to a global variable.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 . for J=1000) 39 OpenMP: A data environment test • Consider this example of PRIVATE and FIRSTPRIVATE variables A..e. program closer IS = 0 C$OMP PARALLEL DO FIRSTPRIVATE(IS) C$OMP+ LASTPRIVATE(IS) DO J=1. IS IS IS is defined as its value at the “last sequential” iteration (I. The values of “B” and “C” are undefined. and C = 1 C$OMP PARALLEL PRIVATE(B) C$OMP& FIRSTPRIVATE(C) • Are A. – B’s initial value is undefined – C’s initial value equals 1 Outside this parallel region . “A” is shared by all threads. equals 1 “B” and “C” are local to each thread. 40 The OpenMP API for Multithreaded Programming 20 .B...B.

41 OpenMP: Reduction example • Remember the code we used to demo private. • The variables in “list” must be shared in the enclosing parallel region. IS 42 The OpenMP API for Multithreaded Programming 21 .SC’05 OpenMP Tutorial 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. • Local copies are reduced into a single value and combined with the original global value.1000 IS = IS + J CONTINUE print *. firstprivate and lastprivate. 0 for “+”).1000 IS = IS + J 1000 CONTINUE print *.g. IS 1000 Here is the correct way to parallelize this code. program closer IS = 0 #pragma omp parallel for reduction(+:IS) DO J=1. • Compiler finds standard reduction expressions containing “op” and uses them to update the local copy. program closer IS = 0 DO J=1.

number Most neg. C/C++ only has default(shared) or default(none). .true. Operand Initial value + * . MIN* MAX* Operand iand ior ieor & | ^ && || Initial value All bits on 0 o ~0 0 0 1 0 43 0 1 0 . . number * Min and Max are not available in C/C++ 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. . . 44 The OpenMP API for Multithreaded Programming 22 .eqv.AND. Must list storage attribute for each variable in static extent Only the Fortran API supports default(private).true. Largest pos.SC’05 OpenMP Tutorial OpenMP: Reduction operands/initial-values • A range of associative operands can be used with reduction: • Initial values are the ones that make sense mathematically. . .OR.false.neqv.false.

– THREADPRIVATE preserves global scope within each thread • Threadprivate variables can be initialized using COPYIN or by using DATA statements. 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 45 Threadprivate • Makes global data private to a thread – Fortran: COMMON blocks – C: File scope and static variables • Different from making them PRIVATE – with PRIVATE global variables are masked.SC’05 OpenMP Tutorial Default Clause Example itotal = 1000 C$OMP PARALLEL PRIVATE(np. 46 The OpenMP API for Multithreaded Programming 23 .

B(N) !$OMP THREADPRIVATE(/buf/) do i=1. each thread executing these routines has its own copy 47 of the common block /buf/. Copyin You initialize threadprivate data using a copyin clause.B(N) !$OMP THREADPRIVATE(/buf/) do i=1. subroutine poo parameter (N=1000) common/buf/A(N).A) !$OMP PARALLEL COPYIN(A) … Now each thread sees threadprivate array A initialied … to the global value set in the subroutine init_data() !$OMP END PARALLEL end 48 The OpenMP API for Multithreaded Programming 24 . parameter (N=1000) common/buf/A(N) !$OMP THREADPRIVATE(/buf/) C Initialize the A array call init_data(N. N A(i) = sqrt(B(i)) end do return end Because of the threadprivate construct. N B(i)= const* A(i) end do return end subroutine bar parameter (N=1000) common/buf/A(N).SC’05 OpenMP Tutorial A threadprivate example Consider two different routines called within a parallel region.

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 50 The OpenMP API for Multithreaded Programming 25 . int). choice) { #pragma omp single copyprivate (Nsize. int). choice. threads. #include <omp.SC’05 OpenMP Tutorial Copyprivate Used with a single region to broadcast values of privates from one member of a team to the rest of the team. choice) input_parameters (Nsize. #pragma omp parallel private (Nsize. } } 49 Agenda • Parallel Computing. do_work(Nsize. choice).h> void input_parameters (int. void main() { int Nsize. // fetch values of input parameters void do_work(int. choice).

C$OMP PARALLEL DO PRIVATE(B) C$OMP& SHARED(RES) DO 100 I=1. RES) C$OMP END CRITICAL 100 CONTINUE 52 The OpenMP API for Multithreaded Programming 26 .SC’05 OpenMP Tutorial OpenMP: Synchronization • High level synchronization: • • • • critical atomic barrier ordered • Low level synchronization • flush • locks (both simple and nested) 51 OpenMP: Synchronization • Only one thread at a time can enter a critical region.NITERS B = DOIT(I) C$OMP CRITICAL CALL CONSUME (B.

#pragma omp parallel { float B. Threads wait their turn – only one at a time calls consume() 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().i<niters. float res. #pragma omp for for(i=0. #pragma omp critical consume (B. RES). int i.i++){ B = big_job(i).SC’05 OpenMP Tutorial The OpenMP* API Synchronization – critical (in C/C++) • Only one thread at a time can enter a critical region. } } 53 * The mark “OpenMP” is the property of the OpenMP Architecture Review Board. C$OMP ATOMIC X = X + temp C$OMP END PARALLEL 54 The OpenMP API for Multithreaded Programming 27 .

#pragma ordered res += consum(tmp). } A[id] = big_calc3(id). #pragma omp parallel private (tmp) #pragma omp for ordered for (I=0.i++){ B[i]=big_calc2(C.i++){C[i]=big_calc3(I.I<N.i<N.i<N. i). C) private(id) { id=omp_get_thread_num(). B.SC’05 OpenMP Tutorial OpenMP: Synchronization • Barrier: Each thread waits until all threads arrive.A).} #pragma omp for nowait for(i=0. implicit barrier at the A[id] = big_calc1(id).I++){ tmp = NEAT_STUFF(I). } no implicit barrier implicit barrier at the end due to nowait 55 of a parallel region OpenMP: Synchronization • The ordered region executes in the sequential order. } 56 The OpenMP API for Multithreaded Programming 28 . end of a for work#pragma omp barrier sharing construct #pragma omp for for(i=0. #pragma omp parallel shared (A.

• Variables in registers or write buffers must be updated in memory. B. • All memory operations (both reads and writes) defined after the sequence point must follow the flush.SC’05 OpenMP Tutorial 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) 57 OpenMP: Synchronization • The flush construct denotes a sequence point where a thread tries to create a consistent view of memory for a subset of variables called the flush set. • Arguments to flush define the flush set: #pragma omp flush(A. C) • The flush set is all thread visible variables if no argument list is provided #pragma omp flush • For the variables in the flush set: • All memory operations (both reads and writes) defined prior to the sequence point must complete. 58 The OpenMP API for Multithreaded Programming 29 .

integer ISYNC(NUM_THREADS) C$OMP PARALLEL DEFAULT (PRIVATE) SHARED (ISYNC) IAM = OMP_GET_THREAD_NUM() ISYNC(IAM) = 0 Make sure other threads can C$OMP BARRIER see my write.EQ.SC’05 OpenMP Tutorial Shared Memory Architecture a Shared memory a cache1 cache2 cache3 cacheN proc1 proc2 a proc3 procN 59 OpenMP: A flush example • This example shows how flush is used to implement pair-wise synchronization. CALL WORK() ISYNC(IAM) = 1 ! I’m all done. signal this to other threads C$OMP FLUSH(ISYNC) DO WHILE (ISYNC(NEIGH) . C$OMP END PARALLEL Note: OpenMP’s flush is analogous to a fence in other shared memory API’s. 0) C$OMP FLUSH(ISYNC) Make sure the read picks up a END DO good copy from memory. 60 The OpenMP API for Multithreaded Programming 30 .

61 OpenMP Memory Model: Basic Terms Program order Source code Wa Wb Ra Rb . • Reorder flush constructs that don’t have overlapping flush sets. . . compiler Code order Executable code Wb Rb Wa Ra . .SC’05 OpenMP Tutorial What is the Big Deal with Flush? • Compilers reorder instructions to better exploit the functional units and keep the machine busy • Flush interacts with instruction reordering: – A compiler CANNOT do the following: • Reorder read/writes of variables in a flush set relative to a flush. – A compiler CAN do the following: • Reorder instructions NOT involving variables in the flush set relative to the flush. It just ensures that a thread’s values are made consistent with main memory. RW’s in any semantically equivalent order thread thread private view a b threadprivate private view b a threadprivate memory a b Commit order 62 The OpenMP API for Multithreaded Programming 31 . the flush operation does not actually synchronize different threads. . • Reorder flush constructs when flush sets overlap. • So you need to use flush carefully Also.

your turn. a lock implies a flush of all thread visible variables – omp_init_lock(). id. } 64 omp_destroy_lock(&lck). omp_init_lock(&lck). omp_test_lock().SC’05 OpenMP Tutorial OpenMP: Lock routines • Simple Lock routines: – A simple lock is available if it is unset. In OpenMP 2. 63 OpenMP: Simple Locks • Protect resources with locks. so the next thread omp_unset_lock(&lck). omp_unset_lock(). omp_destroy_lock() • Nested Locks – A nested lock is available if it is unset or if it is set but owned by the thread executing the nested lock function – omp_init_nest_lock().5. omp_unset_nest_lock(). id) { Wait here for id = omp_get_thread_num(). The OpenMP API for Multithreaded Programming 32 . tmp). omp_destroy_nest_lock() Note: a thread always accesses the most recent copy of the lock. tmp = do_lots_of_work(id). gets a turn. Release the lock printf(“%d %d”. Free-up storage when done. omp_set_lock(). omp_set_nest_lock(). so you don’t need to use a flush on the lock variable. omp_lock_t lck. omp_test_nest_lock(). omp_set_lock(&lck). #pragma omp parallel private (tmp.

} pair. omp_unset_nest_lock(&p->lck).} void incr_pair(pair *p.} void f(pair *p) { extern int work1(). #pragma omp section incr_b(p.work1().h> typedef struct{int a. p->b =+b.a). int a. int b) {omp_set_nest_lock(&p->lck). int a) { p->a +=a.work3()).b. void incr_a(pair *p. Is that enough? – Do we need conditions variables? – Monotonic flags? – Other pairwise synchronization? • The flush is subtle … even experts get confused on when it is required.b). int b) { omp_set_nest_lock(&p->lck). work3(). omp_unset_nest_lock(&p->lck).SC’05 OpenMP Tutorial OpenMP: Nested Locks #include <omp. 66 The OpenMP API for Multithreaded Programming 33 .} f() calls incr_b() and incr_pair() … but incr_pair() calls incr_b() too. incr_b(p. work2()). so you need nestable locks void incr_b (pair *p. #pragma omp parallel sections { #pragma omp section incr_pair(p. } } 65 Synchronization challenges • OpenMP only includes high level synchronization directives that “have a sequential reading”. omp_nest_lock_t lck. work2(). incr_a(p.

omp_get_num_threads(). 68 The OpenMP API for Multithreaded Programming 34 . omp_get_thread_num(). 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 67 OpenMP: Library routines: • Runtime environment routines: • Modify/Check the number of threads – omp_set_num_threads(). threads. 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.SC’05 OpenMP Tutorial Agenda • Parallel computing.

#pragma omp parallel Protect this { int id=omp_get_thread_num(). then (2) save the number you got. (1) set the number threads.h> Request as many threads void main() as you have processors. omp_set_num_threads( omp_num_procs() ). 70 The OpenMP API for Multithreaded Programming 35 .SC’05 OpenMP Tutorial OpenMP: Library Routines • To used a fixed. – OMP_NUM_THREADS int_literal … Plus several less commonly used environment variables. op since #pragma omp single num_threads = omp_get_num_threads(). chunk_size]” • Set the default number of threads to use. #include <omp. Memory stores are do_lots_of_stuff(id). not atomic } } 69 OpenMP: Environment Variables: • Control how “omp for schedule(RUNTIME)” loop iterations are scheduled. known number of threads used in a program. { int num_threads. – OMP_SCHEDULE “schedule[.

72 The OpenMP API for Multithreaded Programming 36 .0 (1+x2) dx = π F(x) = 4.SC’05 OpenMP Tutorial Agenda • Parallel computing. we know that: ∫ 0 4.0 We can approximate the integral as a sum of rectangles: ∑ F(x )Δx ≈ π i N i=0 1. 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 71 Let’s pause for a quick recap by example: Numerical Integration 4.0 0.0/(1+x2) 2.0 X Where each rectangle has width Δx and height F(xi) at the middle of interval i.0 1 Mathematically.

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

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

number of threads by } setting the environment variable. sum += 4. MPI_DOUBLE. char *argv[]) { int i. MPI_SUM. you set pi = step * sum. MPI_Reduce(&sum.SC’05 OpenMP Tutorial OpenMP recap : Use Environment variables to set number of threads #include <omp.0 .0/(double) num_steps. my_id. &numprocs) . double x.h> static long num_steps = 100000. void main () { int i.0+x*x). pi.0. numprocs.0/(1. OMP_NUM_THREADS 77 MPI: Pi program #include <mpi. i+numprocs) { x = (i+0. double x. MPI_COMM_WORLD) . 1.5)*step. i++){ x = (i+0. sum = 0.0+x*x). step = 1. i<num_steps . void main (int argc. } In practice. step. for (i=my_id. 0. } 78 The OpenMP API for Multithreaded Programming 39 . pi. step = 1. &argv) .5)*step. sum = sum + 4. MPI_Comm_Rank(MPI_COMM_WORLD. #pragma omp parallel for private(x) reduction(+:sum) for (i=0.h> static long num_steps = 100000.0/(double) num_steps . &pi. sum = 0.i<= num_steps. } sum *= step .0/(1. MPI_Init(&argc. MPI_Comm_Size(MPI_COMM_WORLD. &my_id) . double step.

static long num_steps = 100000. int i. i<NUM_THREADS. sum = sum + 4.SC’05 OpenMP Tutorial Pi Program: Win32 API. DWORD threadID.INFINITE). 0. threads. } WaitForMultipleObjects(NUM_THREADS. double x. TRUE.0+x*x).0/(double) num_steps. int threadArg[NUM_THREADS].0. LeaveCriticalSection(&hUpdateMutex). for(i=0. global_sum += sum. &threadArg[i]. } void main () { double pi. start. (LPTHREAD_START_ROUTINE) Pi. void Pi (void *arg) { int i. &threadID).h> #define NUM_THREADS 2 HANDLE thread_handles[NUM_THREADS]. CRITICAL_SECTION hUpdateMutex. for (i=0. pi = global_sum * step. } EnterCriticalSection(&hUpdateMutex). step = 1. PI #include <windows. sum = 0.pi). i=i+NUM_THREADS){ x = (i-0. 0. printf(" pi is %f \n". for (i=start. InitializeCriticalSection(&hUpdateMutex).5)*step. i++) threadArg[i] = i+1. double step. double global_sum = 0.0/(1. 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 80 The OpenMP API for Multithreaded Programming 40 .i<= num_steps.0. } Doubles code size! Agenda • Parallel computing. i<NUM_THREADS. i++){ thread_handles[i] = CreateThread(0. start = *(int *) arg. thread_handles.

SC’05 OpenMP Tutorial How is OpenMP Compiled ? Most Fortran/C compilers today implement OpenMP • The user provides the required switch or switches • Sometimes this also needs a specific optimization level. threadprivate. We can only give an idea of how it is done here!! 82 The OpenMP API for Multithreaded Programming 41 . -mp (SGI. firstprivate.UH. Fujitsu). suspends. NEC). private. -qsmp=omp (IBM) /openmp flag (Microsoft Visual Studio 2005). etc.org 81 Under the Hood: How Does OpenMP Really Work? Programmer • • • • States what is to be carried out in parallel by multiple threads Gives strategy for assigning work to threads Arranges for threads to synchronize Specify data sharing attributes: shared. PathScale. OdinMP. wakes up. • Freeware: Omni. private… The details of how OpenMP is implemented varies from one compiler to another. OMPi.compunity. … Check information at http://www.… Compiler (with the help of a runtime library) • • • • • • Transforms OpenMP programs into multi-threaded code Manages threads: creates. PGI). Open64. terminates threads Figures out the details of the work to be performed by each thread Implements thread synchronization Arranges storage for different data and performs their initializations: shared. so manual should be consulted • May also need to set threads’ stacksize explicitly Examples • Commercial: -openmp (Intel. --openmp (Lahey. Sun.

Generate object code for the target architecture 84 The OpenMP API for Multithreaded Programming 42 . build the intermediate representation (IR) • Middle End: – Analyze and optimize program. ensure that it is errorfree.SC’05 OpenMP Tutorial Overview of OpenMP Translation Process • Compiler processes directives and uses them to create explicitly multithreaded code • Generated code makes calls to a runtime library – The runtime library also implements the OpenMP user-level run-time routines • Details are different for each compiler. Determine layout of program data in memory. “Lower” IR to machine-like form • Back End: – Complete optimization. but strategies are similar • Runtime library and details of memory management also proprietary • Fortunately the basic translation is not all that difficult 83 Structure of a Compiler Source code Front End MiddleEnd Back End Target code • Front End: – Read in source program.

C or C++) • Parse OpenMP directives • Check them for correctness – Is directive in the right place? Is the information correct? Is the form of the for loop permitted? …. And there might be no error message!! 85 OpenMP Compiler Middle End • Preprocess OpenMP constructs Source code FE ME BE object code – Translate SECTIONs to DO/FOR constructs – Make implicit BARRIERs explicit – Apply even more correctness checks • Apply some optimizations to code to ensure it performs well – Merge adjacent parallel regions – Merge adjacent barriers OpenMP directives reduce scope in which some optimizations can be applied. Compiler writer must work hard to avoid a negative impact on performance. 86 The OpenMP API for Multithreaded Programming 43 . • Create an intermediate representation with OpenMP annotations for further handling Nasty problem: incorrect OpenMP sentinel means directive may not be recognized.SC’05 OpenMP Tutorial OpenMP Compiler Front End Source code FE ME BE object code In addition to reading in the base language (Fortran.

It makes the scope of OpenMP data attributes explicit. lastprivate. etc 87 Outlining Create a procedure containing the region enclosed by a parallel construct • Shared data passed as arguments – Referenced via their address in routine • Private data stored on thread’s stack – Threadprivate may be on stack or heap • Visible during debugging • An alternative is called microtasking Outlining introduces a few overheads.g.SC’05 OpenMP Tutorial OpenMP Compiler: Rest of Processing • Translate OpenMP constructs Source code – Simple direct one-to-one substitution • Replace certain OpenMP constructs by calls to runtime routines. • Add necessary synchronization via runtime library • Translate parallel and worksharing constructs and clauses e.: barrier. • e. but makes the translation comparatively straightforward.g. reduction. for. 88 The OpenMP API for Multithreaded Programming 44 . flush. etc • Also implement variable data attributes.: parallel. set up storage and arrange for initialization – Thread’s stack might be used to hold all private data – Instantiate new variables to realize private. etc FE ME BE object code – Complex code transformation: to get multi-threaded code with calls to runtime library • For slave threads: create a separate task that contains the code in a parallel region • For master thread: fork slave threads so they execute their tasks. as well as carrying out the task along with slave threads. atomic. etc – Add assignment statements to realize firstprivate.

incr. } } • Translated multi-threaded code with runtime library calls //here is the outlined code void __ompregion_main1(…) { int ID =ompc_get_thread_num().i += incr ) { … } // Implicit BARRIER ompc_barrier().uppder. 90 The OpenMP API for Multithreaded Programming 45 .ID). … } 89 OpenMP Transformations – Do/For • Transform original loop so each thread performs only its own portion • Most of scheduling calculations usually hidden in runtime • Some extra work to handle firstprivate.h> void main() { #pragma omp parallel { int ID=omp_get_thread_num (). } /* end of ompregion_main1*/ void main() { … __ompc_fork(&__ompregion_main1 . i++ ) { …} • Transformed Code tid = ompc_get_thread_num().. lower. printf(“Hello world(%d)”.ID).i < upper. printf(“Hello world(%d)”. lastprivate • Original Code #pragma omp for for( i = 0. ompc_static_init (tid.…).). i < n.SC’05 OpenMP Tutorial An Outlining Example: Hello world • Original Code #include <omp. for( i = lower.

} Is_single = ompc_single(tid). if((Is_single == 1)) { b = b + 1. • The runtime function for the single construct might use a lock to test and set an internal flag in order to ensure only one thread get the work done #pragma omp parallel {#pragma omp master a=a+1. if((Is_master == 1)) { a = a + 1.} • Transformed Code Is_master= ompc_master(tid). very easy to test for.} ompc_barrier(). incr.SC’05 OpenMP Tutorial OpenMP Transformations – Reduction • • Reduction variables can be translated into a twostep operation • First. sum = (sum + local_sum). #pragma omp single b=b+1. ompc_critical().). } ompc_barrier().i<=num_steps. … ompc_static_init (tid. 92 The OpenMP API for Multithreaded Programming 46 . lower. each thread performs its own reduction using a private variable • Then the global sum is formed • A critical construct might be used to ensure atomicity of the final reduction Original Code #pragma omp parallel for \ reduction (+:sum) private (x) for(i=1. for( i = lower.i++) { … sum=sum+x . ompc_end_critical().i += incr ) { … local_sum = local_sum +x.} Transformed Code float local_sum..uppder. 91 • OpenMP Transformation –Single/Master • Original Code • Master thread has a threadid of 0.i < upper.

it requires a lot of work by the compiler to do a good job • So there may be a performance penalty • Original Code REAL AA(N.N).1 DO I=1. &px). int foo() { #pragma omp threadprivate(px) bar( &px ).thdprv_px. } • • Transformed Code static int px. BB(N. bar( local_px ).1 AA(I. BB(N.J) = BB(I. 93 } OpenMP Transformations – WORKSHARE • WORKSHARE can be translated to OMP DO during preprocessing phase • If there are several different array statements involved. … tid = ompc_get_thread_num(). static int ** thdprv_px.J) END DO END DO !$OMP END PARALLEL 94 The OpenMP API for Multithreaded Programming 47 .N).N) !$OMP PARALLEL !$OMP DO DO J=1. local_px=get_thdprv(tid. int _ompregion_foo1() { int* local_px.N) !$OMP PARALLEL !$OMP WORKSHARE AA = BB !$OMP END WORKSHARE !$OMP END PARALLEL • Transformed Code REAL AA(N.N.N.SC’05 OpenMP Tutorial OpenMP Transformations – Threadprivate • Every threadprivate variable reference becomes an indirect reference through an auxiliary structure to the private copy Every thread needs to find its index into the auxiliary structure – This can be expensive – Some OS’es (and codegen schemes) dedicate register to identify thread – Otherwise OpenMP runtime has to do this • Original Code static int px.

dyn-var. sched_var. atomic) Some routines in runtime library – e.g.g. nest-var..() and some simple constructs (e. destroy threads – Routines to schedule work to threads • Manage queue of work • Provide schedulers for static. Passed by value registers Program counter Outlining creates a new scope: private data become local variables for the outlined routine.are heavily accessed. Local variables can be saved on stack – Includes compiler-generated temporaries – Private variables. Threadprivate Local data pointers to shared variables Arg. numthreads. barrier. suspend them and wake them up/ spin them. 96 The OpenMP API for Multithreaded Programming 48 . so they must be carefully implemented and tuned. etc • Implement library routines omp_. dynamic and guided • Maintain internal control variables – threadid. including firstprivate and lastprivate – Could be a lot of data – Local variables in a procedure called within a parallel region are private by default • Global Data Code … main() __ompregion_main1() • Location of threadprivate data depends on implementation – On heap – On local stack 95 … Role of Runtime Library • Thread management and work dispatch – Routines to create threads. to return the threadid .SC’05 OpenMP Tutorial Runtime Memory Allocation One possible organization of memory Heap Threadprivate • … … stack Thread 2 stack Thread 1 stack Main process stack …. The runtime library should avoid any unnecessary internal synchronization.

98 * Courtesy of J. Bull. /* The last one reset flags*/ if (team->barrier_count == team->team_size) { team->barrier_count = 0.. /* Wait for the last to reset the barrier*/ OMPC_WAIT_WHILE(team->barrier_flag == barrier_flag). team->barrier_flag = barrier_flag ^ 1.SC’05 OpenMP Tutorial Synchronization • Barrier is main synchronization construct since many other constructs may introduce it implicitly. barrier_flag = team->barrier_flag. } One simple way to implement barrier • Each thread team maintains a barrier counter and a barrier flag. The OpenMP API for Multithreaded Programming 49 . Lund. • All other waiting threads can then proceed. • Each thread increments the barrier counter when it enters the barrier and waits for a barrier flag to be set by the last one. Sep. It in turn is often implemented using locks. } pthread_mutex_unlock(&(team->barrier_lock)). EWOMP '99. void __ompc_barrier (omp_team_t *team) { … pthread_mutex_lock(&(team->barrier_lock)). M. the counter will be equal to the team size and the barrier flag is reset. /* Xor: toggle*/ pthread_mutex_unlock(&(team->barrier_lock)). • Parallel reduction is costly because it often uses critical region to summarize variables at the end. 1999. • When the last thread enters the barrier and increment the counter. team->barrier_count++. return. 97 Constructs That Use a Barrier Synchronization Overheads (in cycles) on SGI Origin 2000* • Careful implementation can achieve modest overhead for most synchronization constructs. "Measuring Synchronisation and Scheduling Overheads in OpenMP".

. &_do_upper. __ompc_scheduler_init(__ompv_gtid_s1.). _i = _do_lower. // Schedule(dynamic. dynamic . so final details usually deferred to runtime • Two simple runtime library calls are enough to handle static case: Constant overhead 99 Dynamic Scheduling : Under The Hood _gtid_s1 = __ompc_get_thread_num(). • A while loop to grab available loop iterations from a work queue •Similar way to implement STATIC with a chunk size and GUIDED scheduling Average overhead= c1*(iteration space/chunksize)+c2 100 The OpenMP API for Multithreaded Programming 50 . &_do_lower. mpni_status = __ompc_schedule_next(_gtid_s1. } mpni_status = __ompc_schedule_next(_gtid_s1. _do_lower = 0. chunksize. &_do_lower. } for(_i = _do_lower. */ // the outlined task for each thread _gtid_s1 = __ompc_get_thread_num(). stride.). &_do_upper. &_do_stride). temp_limit = n -1. #pragma omp for schedule(static) &_do_stride. _i <= _do_upper. _i = _i + _do_stride) { do_sth(). _i <= _do_upper. temp_limit = n – 1 __ompc_static_init(_gtid_s1 . while(mpni_status) { if(_do_upper > temp_limit) { _do_upper = temp_limit. static. for (i=0. } do_sth()..i++) if(_do_upper > temp_limit) { { _do_upper = temp_limit. &_do_upper. &_do_lower.SC’05 OpenMP Tutorial Static Scheduling: Under The Hood / *Static even: static without specifying chunk size. scheduler divides loop iterations evenly onto each thread. _do_upper. } • Scheduling is performed during runtime. _i ++) } { do_sth(). } // The OpenMP code // possible unknown loop upper bound: n // unknown number of threads to be used • Most (if not all) OpenMP compilers choose static as default scheduling method • Number of threads and loop bounds possibly unknown. for(_i = _do_lower.i<n. chunksize) _do_upper = temp_limit. &_do_stride).do_lower..

EWOMP '99.. each implementation is a little different from all others. – Use dynamic/guided otherwise * Courtesy of J. Lund. "Measuring Synchronisation and Scheduling Overheads in OpenMP".. M. 101 Implementation-Defined Issues • Each implementation must decide what is in compiler and what is in runtime • OpenMP also leaves some issues to the implementation – – – – – Default number of threads Default schedule and default for schedule (runtime) Number of threads to execute nested parallel regions Behavior in case of thread exhaustion And many others. 1999. Despite many similarities. Bull. 102 The OpenMP API for Multithreaded Programming 51 .SC’05 OpenMP Tutorial Using OpenMP Scheduling Constructs Scheduling Overheads (in cycles) on Sun HPC 3500* • Conclusion: – Use default static scheduling when work load is balanced and thread processing capability is constant. Sep.

and OpenMP • The core elements of OpenMP – Thread creation – Workshare constructs – Managing the data environment – Synchronization – The runtime library – Recapitulation • The OpenMP compiler • OpenMP usage: common design patterns • Case studies and examples • Background information and extra details 103 Turning Novice Parallel programmers into Experts • How do you pass-on expert knowledge to novices quickly and effectively? 1.SC’05 OpenMP Tutorial Agenda • Parallel Computing. “how do expert parallel programmers think?” 2. Publish the framework. threads. 4. The Object Oriented Software Community has found that a language of design patterns is a useful way to construct such a framework.e. Understand expert knowledge. 104 • The OpenMP API for Multithreaded Programming 52 . Validate (peer review) the framework so it represents a true consensus view. 3. Express that expertise in a consistent framework. i.

– Promise new features to thwart competitors. OpenMP and Java. A Shameless plug A pattern language for parallel algorithm design with examples in MPI. Now available at a bookstore near you! 106 The OpenMP API for Multithreaded Programming 53 .S. How can you use software to get rich? • Forces: The solution must resolve the forces: – It must give the buyer something they believe they need. • Solution: Construct a money pipeline – Create SW with enough functionality to do something useful most of the time. – Use bug-fixes and a slow trickle of new features to extract money 105 as you move buyers along the pipeline.SC’05 OpenMP Tutorial Design Patterns: A silly example • Name: Money Pipeline • Context: You want to get rich and all you have to work with is a C.anticipate competition. This is our hypothesis for how programmers think about parallel programming. – Every good idea is worth stealing -. This will draw buyers into your money pipeline. – It can’t be too good. or people won’t need to buy upgrades. degree and programming skills.

} Res.accumulate( tmp). *func().DATA).DATA). TYPE *tmp.I=I+Num){ I= id setup_problem(N. if (id==0)=N = get_num_procs(). & impl. get_proc_id(). TYPE *tmp. get_num_procs(). { Program SPMD_Emb_Par () global_array Data(TYPE). global_array Data(TYPE). int id setup_problem(N. { Program SPMD_Emb_Par () global_array Res(TYPE). for (int int 0. *func().accumulate( tmp). tmp (int int 0.accumulate( tmp). Units of execution + new shared data for extracted dependencies Corresponding source 107 code Algorithm Structure Design space Common patterns in OpenMP Name: The Task Parallelism Pattern • Context: – How do you exploit concurrency expressed in terms of a set of distinct tasks? • Forces – Size of task – small size to balance load vs. get_num_procs(). for if (id==0)= get_proc_id(). for if (id==0) setup_problem(N. I<N. exposing reductions. setup_problem(N. *func(). int id =N = global_array Data(TYPE). mech. *func().I=I+Num){ for = func(I).accumulate( tmp). TYPE *tmp. if (id==0)=Num = get_num_procs(). I<N. int global_array Res(TYPE). } } } Supporting stuct.SC’05 OpenMP Tutorial Pattern Language’s structure: Four design spaces in parallel software development Finding Concurrency Original Problem hm r it l go c t ur e A ru St Tasks. Data). = func(I).I=I+Num){ Data). } } Res. transforming induction variables. tmp (int I= 0. loop parallelism. or SPMD patterns. { int N = global_array Data(TYPE). int get_proc_id(). explicitly protecting (shared data pattern). I<N. } } Res. – Managing dependencies without destroying efficiency. 108 The OpenMP API for Multithreaded Programming 54 . tmp (int I= ID.DATA). tmp = func(I. – Manage dependencies by: • • • • removing them (replicating data). int global_array Res(TYPE). • Solution – Schedule tasks for execution with balanced load – use master worker. I= id Res. I<N. shared and local data Program SPMD_Emb_Par () { Program SPMD_Emb_Par () TYPE *tmp. global_array Res(TYPE). get_proc_id(). large size to reduce scheduling overhead.I=I+Num){ = func(I).

etc. – Balance the conflicting needs of scalability. modifying schedules.SC’05 OpenMP Tutorial Supporting Structures Design space Common patterns in OpenMP Name: The SPMD Pattern • Context: – How do you structure a parallel program to make interactions between threads manageable yet easy to integrate with the core computation? • Forces – Fewer programs are easier to manage. – Optimize incrementally testing at each step of the way – merging 110 loops. and portability.e. 109 Common patterns in OpenMP Supporting Structures Design space Name: The Loop Parallelism Pattern • Context: – How do you transform a serial program dominated by compute intensive loops into a parallel program without radically changing the semantics? • Forces – An existing program implies expected output … and this must be preserved even as the programs execution changes due to parallelism. – Use directives/pragmas to parallelize these loops (i. • Solution – Identify key compute intensive loops. – Assuring correctness suggests that the parallelization process should be incremental with each transformation subject to testing. but complex algorithms often need very different instruction streams on each thread. • Solution – Use a single program for all the threads. – Keep it simple … use the threads ID to select different pathways through the program. The OpenMP API for Multithreaded Programming 55 . at no point do you use the ID to manage loop parallelization by hand). – High performance requires restructuring data to optimize for cache … but this must be done carefully to avoid changing semantics. maintainability. – Keep interactions between threads explicit and at a minimum.

} Data replication #pragma omp critical used to manage pi += sum * step.5)*step. double step. sum. sum += 4. dependencies } 111 } Design pattern example: The Loop Parallelism pattern #include <omp.SC’05 OpenMP Tutorial Design pattern example: The SPMD pattern #include <omp. double x.i< num_steps. double x.0. sum = sum + 4.5)*step. for (i=id. parallelize key loops #pragma omp parallel for private(x) reduction(+:sum) for (i=0. id. executes the #pragma omp parallel private (i. nthreads. #pragma omp single nthreads = omp_get_num_threads(). pi. x.0/(double) num_steps. sum=0. sum = 0.0/(1. manage dependencies } 112 The OpenMP API for Multithreaded Programming 56 . i=i+nthreads){ x = (i+0. } Reduction used to pi = step * sum. sum) same code … use { the thread ID to id = omp_get_thread_num().h> static long num_steps = 100000. Every thread omp_set_num_threads(NUM_THREADS). step = 1. distribute work. id.0+x*x). double step.0+x*x).0.h> static long num_steps = 100000.0/(double) num_steps. i++){ x = (i+0. neutral directives to step = 1.0/(1. void main () Parallelism inserted as semantically { int i. pi.i<= num_steps. #define NUM_THREADS 2 void main () { int i.

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 113 Case Studies .General Comments: Performance Measurement • What is the baseline performance? • What is the desired improvement? • Where are the computation-intensive regions of the program? • Is timing done on a dedicated machine? If a large machine. threads. A poor data layout may kill performance on a platform with many CPUs. 114 The OpenMP API for Multithreaded Programming 57 . where is data? What about file access? Does set of processors available impact this? On large machines. it is best to use custom features to pin threads to specific resources.SC’05 OpenMP Tutorial Agenda • Parallel computing.

g. they may degrade performance. see www.SC’05 OpenMP Tutorial Case Studies . or memory) conflicts • Using one thread per physical processor is preferred for memory-intensive applications – Adding one or more threads to share the computation is an alternative to solve the load imbalance problem • Technique described by Dr. Pin threads to CPUs. dynamic loop schedules have much higher overheads than static schedules – Synchronization is expensive.General Comments: Optimal Number of Threads • May need to experiment to determine the optimal number of threads for a given problem – In SMT.uiuc. datapath.General Comments: Performance Issues • Overheads of OpenMP constructs. thread management – E.ncsa. so use NOWAIT if possible and privatize data where possible • Overheads of runtime library routines – Some are called frequently • Structure and characteristics of program – – – – Sequential part of program Load balance Cache utilization and false sharing (it can kill any speedup) Large parallel regions help reduce overheads. Give them a resource of their own by using one less CPU than the available 115 number. Case Studies . enable better cache usage and standard optimizations System or helper threads may also be active – managing or assigning work. Danesh Tafti and Weicheng Huang. cache.edu/News/ Access/Stories/LoadBalancing/ 116 The OpenMP API for Multithreaded Programming 58 . reducing the number of threads may alleviate resource (register. Otherwise.

i) = rx(j-1.N a(i.i)+… ENDDO ENDDO • Outer loop unrolling of a loop nest to block data for the cache Common /map/ A(1024). n-1 do j=2.404 MB/s to 4635. jdim) C$OMP PARLLEL DO DO i=2. n-1 do j=2.j) END DO END DO • In version on right.N DO j=1.032 – The bandwidth used for level 2 cache is decreased from 11993.N a(i.General Comments: Cache Optimizations • Loop interchange may increase cache locality through stride-1 references !$OMP PARALLEL DO DO i=1.j) = rx(i.817 MB/s 118 – Wall clock time is reduced from 77.j) + c(i.N DO i=1.General Comments: Cache Optimizations • Loop interchange and array transposition – TOMCATV code example REAL rx(jdim.5 GHz Itanium 2 processors) using the Intel compiler 8. B(1024) Common /map/ A(1024). the cache miss ration is greatly reduced – We executed these codes on Cobalt @ NCSA ( one node of SGI Altix System with 32-way 1.j) + c(i. idim) C$OMP PARLLEL DO DO i=2.j) = b(i.j) = b(i. n rx(i. n rx(j.265s The OpenMP API for Multithreaded Programming 59 .j) END DO END DO !$OMP PARALLEL DO DO j=1.0 with –O0 – The level 2 cache miss rate is changed from 0. B(1024) • Array padding – Changing the memory address affects cache mapping 117 – Increases cache hit rates *third party names are the property of their respective owners. PAD(8).383s to 8.SC’05 OpenMP Tutorial Case Studies .j-1)+… ENDDO ENDDO REAL rx(idim.744 to 0. Case Studies .

General Comments: Sources of Errors • Incorrect use of synchronization constructs – Less likely if user sticks to directives – Erroneous use of locks can lead to deadlock – Erroneous use of NOWAIT can lead to race conds. The OpenMP API for Multithreaded Programming 60 . Moreover. private) • Wrong “spelling” of sentinel It can be very hard to track race conditions. • Race conditions (true sharing) – Can be very hard to find • Wrong declared data attributes (shared vs. they can be quite slow.General Comments: Cache Optimizations • A variety of methods to improve cache performance – – – – – loop fission loop fusion loop unrolling (outer and inner loops) loop tiling array padding 119 Case Studies .SC’05 OpenMP Tutorial Case Studies . but they may fail if your OpenMP code does not rely on directives to 120 distribute work. Tools may help check for these.

Software to find oil and gas. Inc. – Interpretation • 2d/3dPAK • EarthPAK • VuPAK – Risk Reduction • • • • • AVOPAK Rock Solid Attributes SynPAK TracePAK ModPAK 122 *third party names are the property of their respective owners. Space – Coalescing loops – Wavefront Parallelism – Work Queues 121 Seismic Data Processing Example • The KINGDOM Suite+ from Seismic MicroTechnology. • Integrated package for Geological/Geophysical interpretation and risk reduction. The OpenMP API for Multithreaded Programming 61 .SC’05 OpenMP Tutorial Case Studies and Examples • More difficult problems to parallelize – Seismic Data Processing: Dynamic Workload – Jacobi: Stepwise improvement – Fine Grained locks – Equake: Time vs.

.k<iNumSamples.k<iNumSamples. iLineIndex <= nEndLine. Overhead for #pragma omp parallel for entering and leaving for(j=0.). for(j=0.k++) processing()....j<iNumTraces. iLineIndex++) { Loadline(iLineIndex. iLineIndex <= nEndLine. } Better performance. iLineIndex++) { Loadline(iLineIndex. SaveLine(iLineIndex). } Load Data Process Data Save Data Timeline 123 First OpenMP Version of Seismic Data Processing for (int iLineIndex=nStartLine.j<iNumTraces. SaveLine(iLineIndex)..j++) the parallel region for(k=0.j++) for(k=0. but not too encouraging Load Data Process Data Save Data Timeline 124 The OpenMP API for Multithreaded Programming 62 .)..SC’05 OpenMP Tutorial Seismic Data Processing for (int iLineIndex=nStartLine.k++) processing().

5 1 0. } Process Process #pragma omp for schedule(dynamic) for(j=0.5 2 1. Data } } } Load Data Process Data Save Data 125 Timeline Performance of Optimized Seismic Data Processing Application Seismic Data Processing Execution time 4 threads (2 CPUs w ith HT) 1200 1000 800 600 400 200 0 Original Sequential Optimized Sequential Static schedule dynamic (10) dynamic (1) Guided (10) Guided (1) Seconds 2 3.j++) for(k=0. iLineIndex++) { Load Load Load #pragma omp single nowait {// loading the next line data.5 3 2.k++) Data Data processing(). iLineIndex <= nEndLine.j<iNumTraces. #pragma omp single nowait Save { SaveLine(iLineIndex).)...SC’05 OpenMP Tutorial Optimized OpenMP Version of Seismic Data Processing Loadline(nStartLine. // preload the first line of data #pragma omp parallel { for (int iLineIndex=nStartLine.).4GHz Intel® XeonTM processors with Hyperthreading and Intel® Extended Memory 64 Technologies 3G RAM Seismic Data Processing Speedup 3.5 0 Original Sequential Optimized Sequential static schedule dynamic (10) 4 threads (2 CPUs w ith HT) dynamic (1) Guided (10) Guided (1) 126 The OpenMP API for Multithreaded Programming 63 .k<iNumSamples... NO WAIT! Data Data Data Loadline(iLineIndex+1...

“Pushing Loop-Level Parallelization to the Limit ”. Wolfgang Koschel. EWOMP ‘02.org • The original OpenMP program contains 2 parallel regions inside the iteration loop – Dieter an Mey. • This paper shows how to tune the performance for this program step by step 128 The OpenMP API for Multithreaded Programming 64 . Thomas Haarmann. Space – Coalescing loops – Wavefront Parallelism – Work Queues 127 Case Studies: a Jacobi Example • Solving the Helmholtz equation with a finite difference method on a regular 2D-mesh • Using an iterative Jacobi method with over-relaxation – Well suited for the study of various approaches of loop-level parallelization • Taken from – Openmp.SC’05 OpenMP Tutorial Case Studies and Examples • More difficult problems to parallelize – Seismic Data Processing: Dynamic Workload – Jacobi: Stepwise improvement – Fine Grained locks – Equake: Time vs.

n uold(i. error = 10. error. tol) ! begin iteration loop error = 0.j)) & + ay*(uold(i. tol) ! begin iteration loop error = 0.le.0 * tol k=1 do while (k.m do i=1.j) + uold(i+1.f(i.and.j) + uold(i+1.j) .j) enddo enddo !$omp end parallel do !$omp parallel do private(resid) reduction(+:error) A Jacobi Example: Version 1 Two parallel regions inside the iteration loop do j = 2.m-1 do i = 2.0 !$omp parallel do do j=1.gt.maxit .omega * resid error = error + resid*resid end do enddo !$omp end do nowait !$omp end parallel k=k+1 error = sqrt(error)/dble(n*m) enddo ! end iteration loop 130 The OpenMP API for Multithreaded Programming 65 .SC’05 OpenMP Tutorial error = 10. An nowait is added.m do i=1.j) enddo enddo !$omp end do !$omp do reduction(+:error) do j = 2.and.j+1)) & + b * uold(i.m-1 do i = 2.n-1 resid = (ax*(uold(i-1.n uold(i.j-1) + uold(i.j)) & + ay*(uold(i.j) .le.gt.j) = uold(i.n-1 resid = (ax*(uold(i-1. error.j) .j))/b A Jacobi Example: Version 2 Merging two parallel regions inside the iteration loop.j) .maxit .0 !$omp parallel private(resid) !$omp do do j=1.j-1) + uold(i.omega * resid error = error + resid*resid end do enddo !$omp end parallel do k=k+1 error = sqrt(error)/dble(n*m) enddo ! end iteration loop 129 + b * uold(i.f(i.j) = u(i.j) = uold(i.0 * tol k=1 do while (k.j+1)) u(i.j) = u(i.j))/b u(i.

maxit .n-1 …… error = error + resid*resid end do enddo !$omp end do k_priv = k_priv + 1 error_priv = sqrt(error)/dble(n*m) enddo ! end iteration loop !$omp barrier !$omp single k = k_priv error = error_priv 132 !$omp end single !$omp end parallel The OpenMP API for Multithreaded Programming 66 .le.maxit .and. error. error_priv.n uold(i.0d0 * tol do while (k_priv.m-1 …… error = error + resid*resid enddo !$omp end do k_priv = k_priv + 1 !$omp single error = sqrt(error)/dble(n*m) !$omp end single enddo ! end iteration loop !$omp single k = k_priv !$omp end single nowait 131 !$omp end parallel A Jacobi Example: Version 3 One parallel region containing the whole iteration loop.SC’05 OpenMP Tutorial error = 10.error_priv) k_priv = 1 error_priv = 10.j) = u(i.tol) ! begin iteration loop !$omp do do j=1.tol) ! begin iter.and.j) enddo enddo !$omp end do !$omp single error = 0.j) = u(i.n uold(i.0d0 * tol !$omp parallel private(resid.gt.m-1 do i = 2. !$omp parallel private(resid.j) enddo enddo !$omp end do !$omp single error = 0.m do i=1. An “end single” with an implicit barrier was here in Version 3.m do i=1.0d0 !$omp end single !$omp do reduction(+:error) do j = 2. loop !$omp do do j=1.gt.le. one of the four barriers can be eliminated. k_priv. A Jacobi Example: Version 4 By replacing the shared variable error by a private copy error_priv in the termination condition of the iteration loop.0d0 !$omp end single !$omp do reduction(+:error) do j = 2. k_priv) k_priv = 1 do while (k_priv.

0 * tol.SC’05 OpenMP Tutorial Performance Tuning of the Jacobi example • V1: the original OpenMP program with two parallel regions inside the iteration loop • V2: merges two parallel regions into one region • V3: moves the parallel region out to include the iteration loop inside • V4: replaces a shared variable by a private variable to perform the reduction so that one out of four barriers can be eliminated • V5: the worksharing constructs are eliminated in order to reduce the outlining overhead by the compiler 133 An Jacobi Example: Version 5 do j=1.n-1 …… error_priv = error_priv + resid*resid end do enddo !$omp atomic error = error + error_priv v = k_priv + 1 k_pri !$omp barrier error_priv = sqrt ( error ) / dble(n*m) enddo ! end iteration loop !$omp single k = k_priv !$omp end single !$omp end parallel 134 error = sqrt ( error ) / dble(n*m) The OpenMP API for Multithreaded Programming 67 . k_priv.le.tolh) ! begin iter.ilo + 1 .gt.ie do i = 2.ilo + 1.resid.n.j) = u(i.n uold(i.j) an atomic directive.nrem ) / nthreads !$omp parallel private(me.m. enddo enddo ! all parallel loops run from 2 to m-1 nthreads = omp_get_max_threads() ilo = 2.j) = u(i. ie = is + nchunk .n-1 The reduction is replaced uold(i. enddo do j=2.and. ie = is + nchunk else is = ilo + me * nchunk + nrem. m-1 replaced to avoid the do i=1.n-1 uold(i.j) enddo enddo !$omp barrier !$omp single error = 0 !$omp end single error_priv = 0 do j = is. k_priv = 1 The worksharing constructs by do while (k_priv. loop do j=is.j) = u(i. ihi = m-1 nrem = mod ( ihi .ie do i=2. nthreads ) nchunk = ( ihi .ie.maxit .1 end if error_priv = 10.error_priv) me = omp_get_thread_num() if ( me < nrem ) then is = ilo + me * ( nchunk + 1 ). error_priv.j) outlining overhead by the enddo compiler.is.m-1 do i=1.

1000 iterations 135 Case Studies and Examples • More difficult problems to parallelize – Seismic Data Processing: Dynamic Workload – Jacobi: Stepwise improvement – Fine Grained locks – Equake: Time vs. grid size 200x200 . Space – Coalescing loops – Wavefront Parallelism – Work Queues 136 The OpenMP API for Multithreaded Programming 68 .SC’05 OpenMP Tutorial An Jacobi Example: Version Comparison Comparison of 5 different versions of the Jacobi solver on a Sun Fire 6800.

iother)=iparent(1:nchrome. – Written in Fortran – 1500 lines of code • Most “interesting” loop: shuffle the population.iother) iparent(1:nchrome.SC’05 OpenMP Tutorial SpecOMP: Gafort • Global optimization using a genetic algorithm.000 elements. – Original loop is not parallel. performs pairwise swap of an array element with another. 137 *Other names and brands may be claimed as the property of others. randomly selected element.j)=itemp(1:nchrome) temp=fitness(iother) fitness(iother)=fitness(j) fitness(j)=temp END DO 138 The OpenMP API for Multithreaded Programming 69 .npopsiz-1 CALL ran3(1.my_cpu_id.j) iparent(1:nchrome. Shuffle populations DO j=1.0) iother=j+1+DINT(DBLE(npopsiz-j)*rand) itemp(1:nchrome)=iparent(1:nchrome. There are 40.rand.

• Solution: – use one lock per array element locks. iother. itemp. my_cpu_id) my_cpu_id = 1 !$ my_cpu_id = omp_get_thread_num() + 1 !$OMP DO DO j=1.j)=itemp(1:nchrome) temp=fitness(iother) fitness(iother)=fitness(j) fitness(j)=temp !$ IF (j < iother) THEN !$ CALL omp_unset_lock(lck(iother)) !$ CALL omp_unset_lock(lck(j)) !$ ELSE !$ CALL omp_unset_lock(lck(j)) !$ CALL omp_unset_lock(lck(iother)) !$ END IF END DO !$OMP END DO 140 !$OMP END PARALLEL The OpenMP API for Multithreaded Programming 70 .rand. temp.SC’05 OpenMP Tutorial SpecOMP: Gafort • Parallelization idea: – Perform the swaps in parallel. !$OMP PARALLEL PRIVATE(rand.000 139 Gafort parallel shuffle Exclusive access to array elements.my_cpu_id.0) iother=j+1+DINT(DBLE(npopsiz-j)*rand) !$ IF (j < iother) THEN !$ CALL omp_set_lock(lck(j)) !$ CALL omp_set_lock(lck(iother)) !$ ELSE !$ CALL omp_set_lock(lck(iother)) !$ CALL omp_set_lock(lck(j)) !$ END IF itemp(1:nchrome)=iparent(1:nchrome.j) iparent(1:nchrome. – Must protect swap to prevent races.iother) iparent(1:nchrome. Ordered locking prevents deadlock. 40. – High level synchronization (critical) would prevent all opportunity for speedup.iother)=iparent(1:nchrome.npopsiz-1 CALL ran3(1.

000 of them.5 on 8 processors) but not great. • Speedup was OK (6. • This application led us to make a change to OpenMP: – OpenMP 1. – So in OpenMP 1.0 required that locks be initialized in a serial region. – With 40.SC’05 OpenMP Tutorial Gafort Results • 99. this just wasn’t practical. 141 Case Studies and Examples • More difficult problems to parallelize – Seismic Data Processing: Dynamic Workload – Jacobi: Stepwise improvement – Fine Grained locks – Equake: Time vs.9% of the code was inside parallel section. Space – Coalescing loops – Wavefront Parallelism – Work Queues 142 The OpenMP API for Multithreaded Programming 71 .1 we changed locks so they could be initialized in parallel.

… Anext++.0. highly heterogeneous valleys – Input: grid topology with nodes.0. …… Anext++. i < nodes.SC’05 OpenMP Tutorial SPEComp: Equake • Earthquake modeling – simulates the propagation of elastic waves in large.. Alast = Aindex[i + 1].+ A[Anext][0][2]*v[col][2]. coordinates. etc – Output: reports the motion of the earthquake for a certain number of simulation timesteps • Most time consuming loop: smvp(.) – sparse matrix calculation: accumulate the results of matrix-vector product *Other names and brands may be claimed as the property of others.first = 0. } if (w2[i] == 0) { w2[i] = 1.first = 0. sum0 = A[Anext][0][0]*v[i][0] . w1[i]. sum0 += A[Anext][0][0]*v[col][0] . w1[col]. if (w2[col] == 0) { w2[col] = 1. 143 Smvp() • Frequent and scattered accesses to shared arrays w1 and w2 • Naive OpenMP: uses critical to synchronize each access – serializes most of the execution – Not scalable for (i = 0. while (Anext < Alast) { col = Acol[Anext]..first += sum0. … } w1[col]. + A[Anext][2][0]*v[i][2].. … } w1[i]. i++) { Anext = Aindex[i]. …… } 144 The OpenMP API for Multithreaded Programming 72 . ….first += A[Anext][0][0]*v[i][0] .. seismic event characteristics. + A[Anext][0][2]*v[i][2].

first = 0.i.SC’05 OpenMP Tutorial Solution SPMD-style • Replicate w1.. •exclusive access to arrays • no synchronization • Downside: • large memory consumption • extra time to duplicate data and reduce copies back to one • Performance result: • good scaling up to 64 CPUs for medium dataset #pragma omp parallel private(my_cpu_id.. if (w2[my_cpu_id][col] == 0) { w2[my_cpu_id][col] = 1.sum1... numthreads=1. } if (w2[my_cpu_id][i] == 0) { w2[my_cpu_id][i] = 1. while (Anext < Alast) { sum0 += A[Anext][0][0]*v[col][0] . } } } Lessons Learned from Equake threads 0 1 2 3 Shared Data Data replication 0 Duplicated Data 1 threads 2 3 Duplicated Data Duplicated Data 146 Shared Data Serialized access to shared data Shared Data Data replication is worthwhile when the cost of critical is too high and enough memory and bandwidth is available.first += sum0... .. i++) { …. j++) { 145 if (w2[j][i]) { w[i][0] += w1[j][i].. #endif #pragma omp for for (i = 0.. Shared Data Parallelized access to private copies Data reduction The OpenMP API for Multithreaded Programming 73 . i < nodes..sum2) { #ifdef _OPENMP my_cpu_id = omp_get_thread_num().... sum0 = A[Anext][0][0]*v[i][0] . +A[Anext][0][2]*v[i][2].. } w1[my_cpu_id][col]. } } #pragma omp parallel for private(j) // manual reduction for (i = 0..sum0.w2 for each thread.first = 0.0.0.first.first += A[Anext][0][0]*v[i][0] . j < numthreads. . w1[my_cpu_id][i]... ...col. i < nodes... i++) { for (j = 0. w1[my_cpu_id][col].. #else my_cpu_id=0. } w1[my_cpu_id][i].+ A[Anext][2][0]*v[i][2] .+ A[Anext][0][2]*v[col][2].. …. numthreads=omp-get_num_threads().. ... ...

The OpenMP API for Multithreaded Programming 74 . Space – Coalescing loops – Wavefront Parallelism – Work Queues 147 Getting more concurrency to work with: Art (SpecOMP 2001) • Art: Adaptive Resonance Theory) is an Image processing application based on neural networks and used to recognize objects in a thermal image.SC’05 OpenMP Tutorial Case Studies and Examples • More difficult problems to parallelize – Seismic Data Processing: Dynamic Workload – Jacobi: Stepwise improvement – Fine Grained locks – Equake: Time vs. • Source has over 1300 lines of C code. 148 *Other names and brands may be claimed as the property of others.

I) = Func(I.m<(gLheight+j). &mat_con[ij].m++) for (n=i. gPassFlag =0. gPassFlag = match(o. N !$OMP PARALLEL DO DO IJ = 0. } if (set_high[o][1]==TRUE) { highx[o][1] = i.n++) f1_layer[o][k++].K) ENDDO ENDDO ENDDO ENDDO … • >1 loop is parallel • None of N.J. j<jnum. } } } } 149 Coalesce Multiple Parallel Loops • Original Code • Loop Coalescing ITRIP = N .K) DO K = 1. i<inum. M. ITRIP*JTRIP-1 DO J = 1. L very large • What is best parallelization strategy? 150 The OpenMP API for Multithreaded Programming 75 .J. if (gPassFlag==1) { if (set_high[o][0]==TRUE) { highx[o][0] = i. for (i=0.I[0] = cimage[m][n]. L J = 1 + MOD(IJ. busp). for (m=j.J.n<(gLwidth+i). M I = 1 + IJ/JTRIP DO K = 1. L ENDDO A(K.SC’05 OpenMP Tutorial Key Loop in Art Problem: which loop to parallelize? Inum and jnum aren’t that big – insufficient parallelism to support scalability and compensate for parallel overhead.i++) { for (j=0.j.I) = Func(I. j++) { k=0. set_high[o][0] = FALSE. highy[o][1] = j. highy[o][0] = j. JTRIP = M DO I = 1. set_high[o][1] = FALSE.J.i.JTRIP) A(K.

set_high[o][1] = FALSE. gPassFlag = match(o. Space – Coalescing loops – Wavefront Parallelism – Work Queues 152 The OpenMP API for Multithreaded Programming 76 . highy[o][0] = j.m<(gLheight+j).I[0] = cimage[m][n]. i = ((ij%inum) * gStride) +gStartX. gPassFlag =0.m.j. k=0.SC’05 OpenMP Tutorial Indexing to support Loop Coalescing #pragma omp for private (k. } } } Key loop in Art Note: Dynamic Schedule needed because of embedded conditionals 151 Case Studies and Examples • More difficult problems to parallelize – Seismic Data Processing: Dynamic Workload – Jacobi: Stepwise improvement – Fine Grained locks – Equake: Time vs.m++) for (n=i. ij < ijmx.n. for (m=j. busp).n++) f1_layer[o][k++]. set_high[o][0] = FALSE. highy[o][1] = j.n<(gLwidth+i). gPassFlag) schedule(dynamic) for (ij = 0. ij++) { j = ((ij/inum) * gStride) + gStartY. if (gPassFlag==1) { if (set_high[o][0]==TRUE) { highx[o][0] = i. &mat_con[ij]. } if (set_high[o][1]==TRUE) { highx[o][1] = i.i.

J. N+M !$OMP DO SCHEDULE(STATIC. but stride-1 inner loop helps cache access 154 The OpenMP API for Multithreaded Programming 77 .IJSUM-2) J = IJSUM . L A(K. A(K.I).I DO K = 2. A(K.I) = Func( A(K-1.IJSUM-M). A(K.I). A(K. I-1)) ENDDO ENDDO ENDDO • • • No loop is parallel A wavefront dependence pattern What is best parallelization strategy? • What is a wavefront? – Each point on the wavefront can be computed in parallel – Each wavefront must be completed before next one – A two-dimensional example 153 Wave-fronting SOR • Manual Wavefront – combine loops and restructure to compute over wavefronts !$OMP PARALLEL PRIVATE(J) DO IJSUM= 4.1) DO I = max(2.I) = Func( A(K-1.J.J. min(N. I).I). N DO J = 2. M DO K = 2.I-1) ) ENDDO ENDDO ENDDO !$OMP END PARALLEL • Notice only I and J loops wave-fronted – Relatively easy to wavefront all three loops. L A(K.SC’05 OpenMP Tutorial SOR algorithms • Original Code DO I = 2.J. J1.J.J.J-1.

156 Reference: Shah.SC’05 OpenMP Tutorial Case Studies and Examples • More difficult problems to parallelize – Seismic Data Processing: Dynamic Workload – Jacobi: Stepwise improvement – Fine Grained locks – Equake: Time vs. For (p=list. p. Space – Coalescing loops – Wavefront Parallelism – Work Queues 155 OpenMP Enhancements : Work queues OpenMP doesn’t handle pointer following loops very well nodeptr list. p=p->next) #pragma omp task process(p->data). Haab. p=p->next) process(p->data). p. EWOMP’1999 paper. #pragma omp parallel taskq For (p=list. p!=NULL. p!=NULL. The OpenMP API for Multithreaded Programming 78 . Intel has proposed (and implemented) a taskq construct to deal with this case: nodeptr list. Petersen and Throop.

These updates are completely independent.” 158 The OpenMP API for Multithreaded Programming 79 . “Parallelizing FLAME Code with OpenMP Zee. Robert van de Geijn. Task Queues. “Parallelizing FLAME Code with OpenMP Zee. Kent Milfeld. submitted. Kent Milfeld.FLAME: Shared Memory Parallelism in Dense Linear Algebra • Traditional approach to parallel linear algebra: – Only parallelism from multithreaded BLAS • Observation: – Better speedup if parallelism is exposed at a higher level • FLAME approach: – OpenMP task queues Tze Meng Low. Robert van de Geijn. Queues. Queues. creating a new block of rows to be updated with new parts of A.” *Other names and brands may be claimed as the property of others.SC’05 OpenMP Tutorial Task queue example . submitted. Task Queues.” TOMS . and Field Van Zee.” TOMS . Tze Meng Low. and Field Van Zee. 157 Symmetric rank-k update Add A1AT0 Add A0AT0 A0 A1 AT0 AT1 C10 C11 += C A AT Note: the iteration sweeps through C and A.

5 0 0 200 400 600 800 1000 1200 matrix dimension n 1400 1600 1800 2000 Note: the above graphs is for the most naïve way of marching through the matrices. much faster ramp-up can be achieved. 4CPU) x 10 4 syrk_ln (var2) Reference FLAME OpenFLAME_nth1 OpenFLAME_nth2 OpenFLAME_nth3 OpenFLAME_nth4 2 1.SC’05 OpenMP Tutorial 159 Top line represents peak of Machine (Itanium2 1. 160 By picking blocks dynamically. The OpenMP API for Multithreaded Programming 80 . 1 0.5 MFLOPS/sec.5GHz.

San.org • OpenMP User’s Group (cOMPunity) URL: www. 2006) Patterns for Parallel Programming. Sanders.SC’05 OpenMP Tutorial Agenda • Parallel computing. London : Harcourt. 2000. Mueller. Chapman. 2004 162 The OpenMP API for Multithreaded Programming 81 . the primary source of information about OpenMP: www. Calif Morgan Kaufmann .org • Books: Parallel programming in OpenMP. 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 161 Reference Material on OpenMP • OpenMP architecture review board URL. Van der Pas. : Francisco. Chandra. threads. Addison Wesley. ISBN: 1558606718 Using OpenMP.openmp. Rohit. Mattson. Massingill.compunity. Jost. MIT Press (to appear.

. 1. Frisch MJ.1662. What Multilevel Parallel Programs do when you are not watching: a Performance analysis case study comparing MPI/OpenMP. Zima H. pp. Exploiting multiple levels of parallelism in OpenMP: a case study. Performance of the NAS benchmarks on a cluster of SMP PCs using a parallelization of the MPI programs with OpenMP. pp. B.14. Netherlands. pp. pp. pp. vol. International Journal of High Performance Computing Applications. NC. Demirbilek Z. Demirbilek Z. World Scientific Publishing. Navarro N. Publisher: Elsevier. pp. Scalmani C. Clay P. Labarta J. 3349. Bova. Shared Memory Parallel Programming with OpenMP. 2000. Couturier R. 792-796. Gomperts R. Wen..235-40.. Breshears.650-8. Applying interposition techniques for performance analysis of OPENMP parallel applications.1. Hernandez. Lecture notes in Computer Science. USA. pp. Liu Z.” Proc. Parallel and Distributed Systems. 121. CA. Liu. no. Proceedings 14th International Parallel and Distributed Processing Symposium. Applications and Technologies (PDCAT 03). Proceedings. Lecture notes in Computer Science. Vol. Proceedings of the ISCA 12th International Conference. Adhianto. Germany Ayguade E. Publisher: Elsevier. Y. Navarro N. Richard O. L. Bill Magro. Chapman B. no. Vol. Springer-Verlag. Gabb HA. 3349. 1999. Bob Kuhn.843-56. Chapman B. Chapman. Martorell X. Stefano Salvini. Towards Teracomputing. Parallel molecular dynamics using OPENMP on a shared memory machine. P.7-8. 1999. O. Breshearsz CP. Shared Memory Parallel Programming with OpenMP. Berlin. SIAM News. Labarta J. Enhancing OpenMP with features for locality control. [Conference Paper] Euro-Par'98 Parallel Processing.301-13. ISCA. Rudolf Eigenmann. vol. 4th International Euro-Par Conference. Los Alamitos. Cary.. 2005 Gonzalez M. III.172-80. Computer Physics Communications. Etiemble D. pp. 2003. Soc. Gonzalez M. P. Vol.566-71. OpenMP and HPF: integrating two paradigms.. Cuicchi CE.. Chapman B.49-64. P. Lecture notes in Computer Science. Weng T. Huang L. Parallel Computing. Henry Gabb..49-59. Weng. USA.. Netherlands. Breshears CP. USA. 2005 164 • • • • • • The OpenMP API for Multithreaded Programming 82 . “Parallelization of General Matrix Multiply Routines Using OpenMP”. vol. Ayguade E. Kendall R. Cappello F. L. Proceedings of Eighth ECMWF Workshop on the Use of Parallel Processors in Meteorology. Proceedings of the 1999 International Conference on Parallel Processing.. Z. 1999. 3349. “Dragon: An Open64-Based Interactive Program Analysis Tool for Large Applications. 163 • • • • • • OpenMP Papers (continued) • Jost G. No 9. Huang. 2005 Bova SW. Parallel Programming with Message Passing and Directives. Publisher: Sage Science Press. no. Volume 32. IPDPS 2000. 1999. Spring 2000. Mehrotra P. Gabb H. Ab initio quantum chemistry on a ccNUMA architecture using OpenMP. Dual-level parallel analysis of harbor wave response using MPI and OpenMP. 1998. Nesting OpenMP in an MPI application. Serra A. Springer-Verlag. MLP. Jan. Gimenez J.1. pp. IEEE Comput. 4th International Conference on Parallel and Distributed Computing. Mehrotra P. Lecture Notes in Computer Science Vol. Martorell X. 1999.124. Bentz J. Greg Gaertner. Cuicchi C. T. Shared Memory Parallel Programming with OpenMP.-H. and Nested OpenMP. Oliver J.SC’05 OpenMP Tutorial OpenMP Papers • Sosa CP. 29. Singapore. Soc. 2000. Labarta J. Chipot C. Nov. Efficient Implementationi of OpenMP for Clusters with Implicit Data Distribution.339-50.26. Bova SW. Steve W. IEEE Comput. July 2000.

“Implementing OpenMP using Dataflow execution Model for Data Locality and Efficient Parallel Execution. Magro W. OpenMP for networks of SMPs. 2002. Special Issues on OpenMP. Vol. T. Silvera R. ISSN 1535-766X Mattson. Petersen P.34. 2002. (HIPS-7). Number 1940. L.SC’05 OpenMP Tutorial OpenMP Papers (continued) • B. 384-390. 2000. Prabhakar.. Weng.. O.302-10.96-106. IEEE. USA. T.” In Proceedings of the 7th workshop on High-Level Parallel Programming Models and Supportive Environments. Petesen P. 3349. Zwaenepoel W.0.81-93. Chapman. Zwaenepoel W.” Parallel Computing. CA. Number 2. 137. 2000. JOMP: an OpenMP-like interface for Java. Chow Y. Journal of High Performance Computing and Networking (IJHPCN). 14(8-9): 713-739. Hernandez. 244-259. Duran A.. Tokyo Japan. 11. Mattson. 2005 Shah S. “Achieving performance under OpenMP on ccNUMA and software distributed shared memory systems. Weng and B. B. A. Publisher 165 John Wiley & Sons. Bull. E. Huang. Ltd. pp. Flexible control structures for parallelism in OpenMP. vol. B. T-H. 2000 pp. Chapman and Z. Pages 44 . Kambites. Gross T. M. Cox AL.” Concurrency and Computation: Practice and Experience. Liu. ” Workshop on OpenMP Applications and Tools.-H. Proceedings 13th International Parallel Processing Symposium and 10th Symposium on Parallel and Distributed Processing. Aug. Sigplan Notices (Acm Special Interest Group on Programming Languages). Honghui Lu. T. B. Chapman. USA. Bregier. 2005. Yuen K. Scientific Programming 9..” Int. P. Vol. 12:1219-1239. 2005 T. WOMPAT’2002. ACM. Lecture Notes in Computer Science. Lecture notes in Computer Science.. 98. Lecture notes in Computer Science. P.. Proceedings 3rd International Symposium on High Performance Computing. Springer. pp. Spring Verlag. Corbalan J. Shared Memory Parallel Programming with OpenMP. Vol. M. 2 and 3. Soc. Honghui Lu.G. • • • • • • OpenMP Papers (continued) • • Voss M. Chapman. 2002. How Good is OpenMP? Scientific Programming. IPPS/SPDP 1999. Liu. Wong C. Shah S.. no. 166 • • • • • • The OpenMP API for Multithreaded Programming 83 . Shared Memory Parallel Programming with OpenMP. Chiu E.. To appear.. Proceedings of the ACM 2000 conference on Java Grande. Z. “Towards a More Efficient Implementation of OpenMP for Clusters via Translation to Global Arrays.G. Issue 01. LNCS 2716. Hu YC. pp. Scherer A. April 2002. “An evaluation of AutoScoping in OpenMP”. p. 1999. A. 3349. Labarta J. Bull and M. IEEE Comput. J. Chapman (Guest Editors). Transparent adaptive parallelism on NOWS using OpenMP.-H.53. B. Concurrency: Practice and Experience. Throop J. Intel Technology Journal. Volume 06. “Toward Optimization of OpenMP Codes for Synchronization and Data Reuse. Man P. Vol. 2003. An Introduction to OpenMP 2. Hyper-Threading Technology: Impact on ComputerIntensive Workloads. “Improving the Performance of OpenMP by Array Privatization. 2004. F. Haab G. Los Alamitos. Chapman.. Weng. Patil.8. “Runtime Adjustment of Parallel Nested Loops”. 1999. 2001. M. 1.

The OpenMP API for Multithreaded Programming 84 .SC’05 OpenMP Tutorial Backup material • History • Foundations of OpenMP • Memory Models and the Infamous Flush • Performance optimization in OpenMP • The future of OpenMP 167 OpenMP pre-history • OpenMP based upon SMP directive standardization efforts PCF and aborted ANSI X3H5 – late 80’s • Nobody fully implemented either • Only a couple of partial solutions • Vendors considered proprietary API’s to be a competitive feature: – Every vendor had proprietary directives sets – Even KAP. a “portable” multi-platform parallelization tool used different directives on each platform 168 PCF – Parallel computing forum KAP – parallelization tool from KAI.

169 1997 OpenMP Release History 1998 OpenMP C/C++ 1.0 2000 A single specification for Fortran. needed commonality across products DEC HP IBM Intel KAI ISV .needed larger market Wrote a rough draft straw man SMP API Other vendors invited to join ASCI was tired of recoding for SMPs.1 1999 OpenMP Fortran 2.0 2002 OpenMP C/C++ 2.SC’05 OpenMP Tutorial History of OpenMP SGI Cray Merged. C and C++ 170 The OpenMP API for Multithreaded Programming 85 .0 2005 OpenMP 2.5 OpenMP Fortran 1.0 1997 OpenMP Fortran 1. Urged vendors to standardize.

SC’05 OpenMP Tutorial Backup material • History • Foundations of OpenMP • Memory Models and the Infamous Flush • Performance optimization in OpenMP • The future of OpenMP 171 The Foundations of OpenMP: OpenMP: a parallel programming API Layers of abstractions or “models” used to understand and use OpenMP Parallelism Working with concurrency 172 The OpenMP API for Multithreaded Programming 86 .

• “Parallel computing” occurs when you use concurrency to: – Solve bigger problems – Solve a fixed size problem in less time • For parallel computing. 173 The Foundations of OpenMP: OpenMP: a parallel programming API Layers of abstractions or “models” used to understand and use OpenMP Parallelism Working with concurrency 174 The OpenMP API for Multithreaded Programming 87 . • Use a parallel programming API to express the concurrency within your source code. • Restructure code to expose the concurrency. this means you need to: • Identify exploitable concurrency.SC’05 OpenMP Tutorial Concurrency: • Concurrency: • When multiple tasks are active simultaneously.

Hoc.). R. – The lower levels represent the problem in the computer’s domain. – The top levels express the problem in the original problem domain.J. 1990 Layers of abstraction in programming Domain Problem Specification Algorithm Programming Source Code Computational Computation Cost Hardware 176 Model: Bridges between domains OpenMP only defines these two! The OpenMP API for Multithreaded Programming 88 . T.G. • The models represent the problem at a different level of abstraction. but detailed enough to support simulation. Samurcay and D. Academic Press Ltd. Source: J. Psychology of Programming. Green.-M.SC’05 OpenMP Tutorial Reasoning about programming • Programming is a process of successive refinement of a solution relative to a hierarchy of models.. Gilmore (eds. • The models are informal.R.

the sequential program evolves into a parallel program. Master Thread Parallel Regions A Nested Parallel region 177 OpenMP Computational model • OpenMP was created with a particular abstract machine or computational model in mind: • Multiple processing elements. • A shared address space with “equal-time” access for each processor. • Multiple light weight processes (threads) managed outside of OpenMP (the OS or some other “third party”). Proc1 Proc2 Proc3 ProcN Shared Address Space 178 The OpenMP API for Multithreaded Programming 89 .e.SC’05 OpenMP Tutorial OpenMP Programming Model: Fork-Join Parallelism: Master thread spawns a team of threads as needed. Parallelism is added incrementally until desired performance is achieved: i.

• Specification Models: – Some parallel algorithms are natural in OpenMP: • loop-splitting.SC’05 OpenMP Tutorial What about the other models? • Cost Models: – OpenMP doesn’t say anything about the cost model – programmers are left to their own devices. • SPMD (single program multiple data). – Other specification models are hard for OpenMP • Recursive problems and list processing is challenging for OpenMP’s models. 179 Backup material • History • Foundations of OpenMP • Memory Models and the Infamous Flush • Performance optimization in OpenMP • The future of OpenMP 180 The OpenMP API for Multithreaded Programming 90 .

• OpenMP was never explicit about its memory model. but the details were never spelled out (prior to OMP 2.SC’05 OpenMP Tutorial OpenMP and Shared memory • What does OpenMP assume concerning the shared memory? • Implies that all threads access memory at the same cost.5). RW’s in any semantically equivalent order thread thread private view a b threadprivate private view b a threadprivate memory a b Commit order 182 The OpenMP API for Multithreaded Programming 91 . compiler Code order Executable code Wb Rb Wa Ra .5. – Consistency: Orderings of accesses to different addresses by multiple threads. “The OpenMP Memory Model”. IWOMP’2005 181 OpenMP Memory Model: Basic Terms Program order Source code Wa Wb Ra Rb . . This was fixed in OpenMP 2. . • Shared memory is understood in terms of: – Coherence: Behavior of the memory system when a single address is accessed by multiple threads. . • If you want to understand how threads interact in OpenMP. you need to understand the memory model. . Jay Hoeflinger and Bronis de Supinski.

5 #pragma omp parallel private(x) shared(p0. P1 = &x. 183 Coherence rules in OpenMP 2. p1) Thread 0 X = …. P0 = &x. /* references in the following line are not allowed */ … *p1 … … *p0 … You can not reference another’s threads private variables … even if you have a shared pointer between the two threads. … *p1 … … *p0 … /* the following are not allowed …*p1 … … *p1 … Nested parallel regions must keep track of the privates pointers reference. Thread 1 X = …. /* references in the following line are not allowed */ … *p1 … … *p0 … #pragma omp parallel shared(x) Thread 0 …X… …*p0 … Thread 1 …X… … *p0 … */ Thread 0 X = …. p1) Thread 0 X = ….5 #pragma omp parallel private(x) shared(p0. P0 = &x. 184 The OpenMP API for Multithreaded Programming 92 . … *p1 … … *p0 … Thread 1 X = ….SC’05 OpenMP Tutorial Coherence rules in OpenMP 2. P1 = &x. Thread 1 X = ….

Writes (W) and Synchronizations (S): – R→R. – Program order = code order = commit order • Relaxed consistency: – Remove some or the ordering constrains for memory ops (R. • Consistency models based on orderings or Reads (R). the temporary view of memory may vary from shared memory. W→S 185 Consistency • Sequential Consistency: – In a multi-processor. R→S. • They seen to be in the same overall order by each of the other processors. W. S). S→S. 186 The OpenMP API for Multithreaded Programming 93 .SC’05 OpenMP Tutorial Consistency: Memory Access Re-ordering • Re-ordering: – Compiler re-orders program order to the code order – Machine re-orders code order to the memory commit order • At a given point in time. ops (R. W→W. W. S) are sequentially consistent if: • They remain in program order for each processor. R→W.

R→S. • All R.5 and Relaxed Consistency • OpenMP 2. – The flush set is the list of variables when the list is used.5 defines consistency as a variant of weak consistency: – S ops must be in sequential order across threads.W ops that overlap the flush set and occur after the flush don’t execute until after the flush. Memory ops: R = Read. W = write.W ops that overlap the flush set and occur prior to the flush complete before the flush executes • All R.SC’05 OpenMP Tutorial OpenMP 2. • Flushes with overlapping flush sets can not be reordered. – Can not reorder S ops with R or W ops on the same thread • Weak consistency guarantees S→W. S→R . S→S • The Synchronization operation relevant to this discussion is flush. 187 Flush • Defines a sequence point at which a thread is guaranteed to see a consistent view of memory with respect to the “flush set”: – The flush set is “all thread visible variables” for a flush without an argument list. W→S. S = synchronization 188 The OpenMP API for Multithreaded Programming 94 .

It just ensures that a thread’s values 189 are made consistent with main memory. Why is it so important to understand the memory model? • Question: According to OpenMP 2. #pragma omp flush (count) Omp_unset_lock(lockvar) 190 The OpenMP API for Multithreaded Programming 95 . is the following a correct program: Thread 1 omp_set_lock(lockvar).5: Thread 2 The Compiler can reorder flush of the lock variable and the flush of count omp_set_lock(lockvar). keep machine busy • Compiler generally cannot move instructions past a barrier – Also not past a flush on all variables • But it can move them past a flush on a set of variables so long as those variables are not accessed • So need to use flush carefully Also. #pragma omp flush(count) Count++.SC’05 OpenMP Tutorial What is the Big Deal with Flush? • Compilers routinely reorder instructions that implement a program – Helps exploit the functional units.0. #pragma omp flush(count) Count++. the flush operation does not actually synchronize different threads. #pragma omp flush (count) Omp_unset_lock(lockvar) Not correct prior to OpenMP 2.

0. broken. imply a flush a1fz = a1->fz. #pragma omp flush(count.SC’05 OpenMP Tutorial Solution: • In OpenMP 2. xt = a1->dx*lambda + a1->x – a1->px. #ifdef _OPENMP omp_unset_lock(&(a1->lock)). so this code is a1->fx = 0.lockvar) Count++. #endif 192 The OpenMP API for Multithreaded Programming 96 .0. yt = a1->dy*lambda + a1->y – a1->py. Is it correct? #ifdef _OPENMP omp_set_lock (&(a1->lock)). the locks don’t a1fy = a1->fy. a1->fy = 0. #pragma omp flush(count.lockvar) Omp_unset_lock(lockvar) Thread 2 omp_set_lock(lockvar).lockvar) Count++. #pragma omp flush(count. Thread 1 omp_set_lock(lockvar). #pragma omp flush(count. a1->fz = 0. zt = a1->dz*lambda + a1->z – a1->pz.lockvar) Omp_unset_lock(lockvar) 191 Even the Experts are confused • The following code fragment is from the SpecOMP benchmark ammp. a1fx = a1->fx. #endif In OpenMP 2. you must make the flush set include variables sensitive to the desired ordering constraints.

yt = a1->dy*lambda + a1->y – a1->py. #endif To prevent problems like this. a1fx = a1->fx. a1->fx = 0. a1fz = a1->fz. the thread is allowed to read. zt = a1->dz*lambda + a1->z – a1->pz.5: lets help out the “experts” • The following code fragment is from the SpecOMP benchmark ammp. It is correct with OpenMP 2. #ifdef _OPENMP omp_unset_lock(&(a1->lock)). so thread can’t determine if the order was jumbled by the other thread prior to the synch (flush). That makes this program a1->fy = 0. • After the synch (flush).SC’05 OpenMP Tutorial OpenMP 2. If done without an intervening synch.5 defines the locks a1fy = a1->fy. a1->fz = 0.5.5 Memory Model: summary part 1 • For a properly synchronized program (without data races). correct. #ifdef _OPENMP omp_set_lock (&(a1->lock)). 194 The OpenMP API for Multithreaded Programming 97 . and by then the flush guarantees the value is in memory. #endif 193 OpenMP 2. the memory accesses of one thread appear to be sequentially consistent to each other thread. • The only way for one thread to see that a variable was written by another thread is to read it. xt = a1->dx*lambda + a1->x – a1->px. to include a full flush. OpenMP 2. this is a race.

t. • Relaxed consistency – Temporary view and memory may differ • Flush operation – Moves data between threads – Write makes temporary view “dirty” – Flush makes temporary view “empty” 195 Backup material • History • Foundations of OpenMP • Memory Models and the Infamous Flush • Performance optimization in OpenMP • The future of OpenMP 196 The OpenMP API for Multithreaded Programming 98 .r.SC’05 OpenMP Tutorial OpenMP 2.r. each other • Data ops are ordered w.t.5 Memory Model: summary part 2 • Memory ops must be divided into “data” ops and “synch” ops • Data ops (reads & writes) are not ordered w. synch ops and synch ops are ordered w.t. each other • Cross-thread access to private memory is forbidden.r.

– Is parallelism worth it? • False sharing – Symptom: cache ping ponging • Hardware/OS cannot support – No visible timer-based symptoms – Hardware counters – Thread migration/affinity – Enough bandwidth? 198 The OpenMP API for Multithreaded Programming 99 .SC’05 OpenMP Tutorial Performance & Scalability Hindrances • Too fine grained – Symptom: high overhead – Caused by: Small parallel/critical • Overly synchronized – Symptom: high overhead – Caused by: large synchronized sections – Dependencies real? • Load Imbalance – Symptom: large wait times – Caused by: uneven work distribution 197 Hindrances (continued) • True sharing – Symptom: cache ping ponging. serial region “cost” changes with number of threads.

log.e. performance tool. no barrier Miss all caches Lock acquisition Dynamic do/for. • Step 1 – Know your application – For best performance. also know your compiler. linear Constant Constant Constant Depends on contention Depends on contention Log. stop being so portable). linear Linear Depends on contention 200 The OpenMP API for Multithreaded Programming 100 . break all the good software engineering rules (I. no barrier Barrier Parallel Ordered Minimum overhead (cycles) 1-10 10-20 10-50 50-100 100-200 100-300 100-300 1000-2000 200-500 500-1000 5000-10000 Scalability Constant Constant Constant. and hardware • The safest pragma to use is: – parallel do/for • Everything else is risky! – Necessary evils • So how do you pick which constructs to use? 199 Understand the Overheads! Note: all numbers are approximate! Operation Hit L1 cache Function call Thread ID Integer divide Static do/for.SC’05 OpenMP Tutorial Performance Tuning • To tune performance.

N SUM = SUM + A(I) * B(I) ENDDO • Know your compiler! • Some issues to consider – # of threads/processors being used – How are reductions implemented? • Atomic. when using first touch policy for placing objects. not usually • Some issues to consider – Number of threads/processors being used – Bandwidth of the memory system – Value of N • Very large N. logarithmic – All the issues from the previous slide about existing distribution of A and B 202 The OpenMP API for Multithreaded Programming 101 . expanded scalar. to achieve a certain placement for object A 201 Too fine grained? • When would parallelizing this loop help? DO I = 1. or about to be distributed • On NUMA systems. critical.SC’05 OpenMP Tutorial Parallelism worth it? • When would parallelizing this loop help? DO I = 1. so A is not cache contained – Placement of Object A • If distributed onto different processor caches. N A(I) = 0 ENDDO • Unless you are very careful.

dynamic much more expensive than static – Chunked static can be very effective for load imbalance • When dealing with consecutive do’s/for’s. use the schedule clause – Remember.SC’05 OpenMP Tutorial Tuning: Load Balancing • Notorious problem for triangular loops • OpenMP aware performance analysis tool can usually pinpoint. 204 The OpenMP API for Multithreaded Programming 102 . nowait can help. but watch out for “summary effects” • Within a parallel do/for. but be careful about 203 races Load Imbalance: Thread Profiler* * Thread Profiler is a performance analysis tool from Intel Corporation.

for( i=lb[j]. i++ ) { a[i] = … b[i] = … c[i] = … } omp_unset_lock( lck[j] ). } • Idea: cycle through different parts of the array using locks! 206 • Adapt to your situation The OpenMP API for Multithreaded Programming 103 . named ones • Original Code #pragma omp critical (foo) { update( a ). 205 • Still need to avoid wait at first critical! Tuning: Locks Instead of Critical • Original Code #pragma omp critical for( i=0. update( b ). omp_set_lock( lck[j] ). for( k = 0. i++ ) { a[i] = … b[i] = … c[i] = … } • Transformed Code jstart = omp_get_thread_num(). i<n.SC’05 OpenMP Tutorial Tuning: Critical Sections • It often helps to chop up large critical sections into finer. critical (foo_b) b ). k < nlocks. i<ub[j]. k++ ) { j = ( jstart + k ) % nlocks. } • Transformed Code #pragma omp update( #pragma omp update( critical (foo_a) a ).

the barrier in the middle can be eliminated • When same object modified (or used).SC’05 OpenMP Tutorial Tuning: Eliminate Implicit Barriers • Work-sharing constructs have implicit barrier at end • When consecutive work-sharing constructs modify (& use) different objects. 1 threaded) • Each set of three bars is a serial region • Why does runtime change for serial regions? – No reason pinpointed • Time to think! – Thread migration – Data migration – Overhead? Run Time 208 The OpenMP API for Multithreaded Programming 104 . 2. barrier can be safely removed if iteration spaces align 207 Cache Ping Ponging: Varying Times for Sequential Regions • Picture shows three runs of same program (4.

1 threaded) • Each set of three bars is a parallel region • Some regions don’t scale well – The collected data does not pinpoint why • Thread migration or data migration or some system limit? • Understand Hardware/OS/Tool limits! – Your performance tool – Your system Run Time 209 Performance Optimization Summary • Getting maximal performance is difficult • Far too many things can go wrong • Must understand entire tool chain – – – – – application hardware O/S compiler performance analysis tool • With this understanding. it is possible to get good performance 210 The OpenMP API for Multithreaded Programming 105 .SC’05 OpenMP Tutorial Limits: system or tool? • Picture shows three runs of same program (4. 2.

• More control over its interaction with the runtime environment • NUMA • Constructs that work with semi-automatic parallelizing compilers … and much more 212 The OpenMP API for Multithreaded Programming 106 . • But it needs to do so much more: • Recursive algorithms.SC’05 OpenMP Tutorial Backup material • History • Foundations of OpenMP • Memory Models and the Infamous Flush • Performance optimization in OpenMP • The future of OpenMP 211 OpenMP’s future • OpenMP is great for array parallelism on shared memory machines. • More flexible iterative control structures.

#pragma omp parallel while while(i<Last){ … Independent loop iterations } 213 Automatic Data Scoping • Create a standard way to ask the compiler to figure out data scooping. j++){ x = bigCalc(j). 214 The OpenMP API for Multithreaded Programming 107 . the compiler serializes the construct int j. result[COUNT]. int i. • When in doubt. res[j] = hugeCalc(x). #pragma omp parallel for automatic for (j=0.SC’05 OpenMP Tutorial While workshare Construct • Share the iterations from a while loop among a team of threads. • Proposal from Paul Petersen of Intel Corp. double x. j<COUNT. } Ask the compiler to figure out that “x” should be private.

clever page migration and a smart OS. • Work with thread affinity.SC’05 OpenMP Tutorial OpenMP Enhancements : How should we move OpenMP beyond SMP? • OpenMP is inherently an SMP model. We don’t have any solid proposals on the table 216 to deal with these problems. This is VERY hard. but we are not making progress towards a consensus view. but all shared memory vendors build NUMA and DVSM machines. • Give up? We have lots of ideas. The OpenMP API for Multithreaded Programming 108 . 215 OpenMP Enhancements : OpenMP must be more modular • Define how OpenMP Interfaces to “other stuff”: • How can an OpenMP program work with components implemented with OpenMP? • How can OpenMP work with other thread environments? • Support library writers: • OpenMP needs an analog to MPI’s contexts. • What should we do? • Add HPF-like data distribution.

SC’05 OpenMP Tutorial Other features under consideration • Error reporting in OpenMP – OpenMP assumes all constructs work. Why not let a compiler do this? • Extensions to Data environment clauses – Automatic data scoping – Default(mixed) to make scalars private and arrays shared. • Iteration driven scheduling – Pass a vector of block sizes to support nonhomogeneous partitioning … and many more 217 The OpenMP API for Multithreaded Programming 109 . But real programs need to be able to deal with constructs that break. • Parallelizing loop nests – People now combine nested loops into a super-loop by hand.

Sign up to vote on this title
UsefulNot useful