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

Similar documents
Lecture 2 A Hand-on Introduction to OpenMP

Introduction to. Slides prepared by : Farzana Rahman 1

Data Environment: Default storage attributes

Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG

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

Multi-core Architecture and Programming

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

Lecture 2 A Hand-on Introduction to OpenMP

Programming with OpenMP*

Introduction to OpenMP

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

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

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

Cluster Computing 2008

OpenMP: The "Easy" Path to Shared Memory Computing


EPL372 Lab Exercise 5: Introduction to OpenMP

Introduction to OpenMP.

Alfio Lazzaro: Introduction to OpenMP

PARLab Parallel Boot Camp

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

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

DPHPC: Introduction to OpenMP Recitation session

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

A Hands-on Introduction to OpenMP *

Lecture 4: OpenMP Open Multi-Processing

Parallel Programming with OpenMP. CS240A, T. Yang

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

OpenMP * Past, Present and Future

Session 4: Parallel Programming with OpenMP

Programming with OpenMP* Intel Software College

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

Overview: The OpenMP Programming Model

Parallel programming using OpenMP

Introduction to OpenMP

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

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

DPHPC: Introduction to OpenMP Recitation session

Massimo Bernaschi Istituto Applicazioni del Calcolo Consiglio Nazionale delle Ricerche.

Shared Memory Parallelism using OpenMP

Parallel Programming

A brief introduction to OpenMP

A Hands-on Introduction to OpenMP *

Shared Memory Programming Paradigm!

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

COMP4300/8300: The OpenMP Programming Model. Alistair Rendell

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

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

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

ECE 574 Cluster Computing Lecture 10

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

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

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

Multithreading in C with OpenMP

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

Shared memory programming

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

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

Parallelising Scientific Codes Using OpenMP. Wadud Miah Research Computing Group

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

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

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

Introduction to OpenMP. Lecture 4: Work sharing directives

Parallel Programming using OpenMP

Parallel Programming using OpenMP

Introduction to OpenMP

CS 470 Spring Mike Lam, Professor. Advanced OpenMP

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

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

Introduction to OpenMP

Parallel Programming in C with MPI and OpenMP

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

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

Allows program to be incrementally parallelized

OpenMP. Dr. William McDoniel and Prof. Paolo Bientinesi WS17/18. HPAC, RWTH Aachen

Parallel Programming in C with MPI and OpenMP

Introduction to OpenMP

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

Introduction to OpenMP

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

Computational Mathematics

Introduction to Standard OpenMP 3.1

Parallel Programming. OpenMP Parallel programming for multiprocessors for loops

The OpenMP * Common Core:

ME759 High Performance Computing for Engineering Applications

Shared Memory Parallelism - OpenMP

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

Introduction to Programming with OpenMP

Introduction to OpenMP

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

OPENMP TIPS, TRICKS AND GOTCHAS

15-418, Spring 2008 OpenMP: A Short Introduction

Introduction to OpenMP

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

Parallel Programming in C with MPI and OpenMP

Programming Shared-memory Platforms with OpenMP. Xu Liu

Shared Memory Programming with OpenMP

OpenMP Algoritmi e Calcolo Parallelo. Daniele Loiacono

OpenMP - Introduction

Introduction to OpenMP. Motivation

Transcription:

Parallel Programming Principle and Practice Lecture 6 Shared Memory Programming OpenMP Jin, Hai School of Computer Science and Technology Huazhong University of Science and Technology

Outline OpenMP Overview Creating Threads Parallel Loops Synchronization Data Environment Tasks 2

Architecture for Shared Memory Model Uniform Memory Access Non-Uniform Memory Access 3

Thread Based Parallelism OpenMP programs accomplish parallelism exclusively through the use of threads A thread of execution is the smallest unit of processing that can be scheduled by an operating system The idea of a subroutine that can be scheduled to run autonomously might help explain what a thread is Threads exist within the resources of a single process Without the process, they cease to exist Typically, the number of threads match the number of machine processors/cores However, the actual use of threads is up to the application 4

Explicit Parallelism OpenMP is an explicit (not automatic) programming model, offering the programmer full control over parallelization Parallelization can be as simple as taking a serial program and inserting compiler directives... Or as complex as inserting subroutines to set multiple levels of parallelism, locks and even nested locks 5

OpenMP Overview C$OMP FLUSH #pragma omp critical C$OMP THREADPRIVATE(/ABC/) C$OMP parallel do shared(a, b, c) call OMP_INIT_LOCK (ilok) C$OMP SINGLE PRIVATE(X) C$OMP PARALLEL REDUCTION (+: A, B) #pragma omp parallel for private(a, B) CALL OMP_SET_NUM_THREADS(10) OpenMP: An API for Writing Multithreaded Applications call omp_test_lock(jlok) C$OMP ATOMIC C$OMP PARALLEL DO ORDERED PRIVATE (A, B, C) C$OMP MASTER A set of compiler setenv directives OMP_SCHEDULE and library dynamic routines for parallel application programmers C$OMP ORDERED Greatly simplifies writing multi-threaded (MT) programs in Fortran, C and C++ C$OMP SECTIONS Standardizes last 20 years of SMP practice!$omp BARRIER C$OMP PARALLEL COPYIN(/blk/) C$OMP DO lastprivate(xx) Nthrds = OMP_GET_NUM_PROCS() omp_set_lock(lck) 6

OpenMP Release History 1998 OpenMP C/C++ 1.0 2002 OpenMP C/C++ 2.0 A single specification for Fortran, C and C++ 2005 2008 2011 OpenMP 2.5 OpenMP 3.0 OpenMP 3.1 OpenMP Fortran 1.0 1997 OpenMP Fortran 1.1 1999 OpenMP Fortran 2.0 2000 Tasking, other new features A few more features and bug fixes 7

OpenMP Core Syntax Most of the constructs in OpenMP are compiler directives #pragma omp construct [clause [clause] ] Example #pragma omp parallel num_threads(4) Function prototypes and types in the file #include <omp.h> Most OpenMP constructs apply to a structured block Structured block: a block of one or more statements with one point of entry at the top and one point of exit at the bottom It s OK to have an exit() within the structured block 8

OpenMP Overview: How do Threads Interact? OpenMP is a multi-threading, shared address model Threads communicate by sharing variables Unintended sharing of data causes race conditions Race condition: when the program s outcome changes as the threads are scheduled differently To control race conditions Use synchronization to protect data conflicts Synchronization is expensive Change how data is accessed to minimize the need for synchronization 9

Outline OpenMP Overview Creating Threads Parallel Loops Synchronization Data Environment Tasks 10

OpenMP Programming Model Fork-Join Parallelism All OpenMP programs begin as a single process: the master thread. The master thread executes sequentially until the first parallel region construct is encountered Master thread spawns a team of threads as needed Parallelism added incrementally until performance goals are met: i.e. the sequential program evolves into a parallel program 11 11

Thread Creation: Parallel Regions Create threads in OpenMP with the parallel construct For example, to create a 4 thread in parallel region Each thread executes a copy of the code within the structured block double A[1000]; #pragma omp parallel num_threads(4) { int ID = omp_get_thread_num(); pooh(id,a); clause to request a certain number of threads Runtime function returning a thread ID Each thread calls pooh(id,a) for ID = 0 to 3 12

Thread Creation: Parallel Regions Each thread executes the same code redundantly double A[1000]; double A[1000]; #pragma omp parallel num_threads(4) { int ID = omp_get_thread_num(); pooh(id, A); printf( all done\n ); omp_set_num_threads(4) A single copy of A is shared between all threads pooh(0, A) pooh(1, A) pooh(2, A) pooh(3, A) printf( all done\n ); Threads wait here for all threads to finish before proceeding (i.e. a barrier) 13

Outline OpenMP Overview Creating Threads Parallel Loops Synchronization Data Environment Tasks 14

Loop Worksharing Constructs Loop worksharing construct splits up loop iterations among the threads in a team #pragma omp parallel { #pragma omp for for (I=0;I<N;I++){ NEAT_STUFF(I); Loop construct name C/C++: for Fortran: do The variable I is made private to each thread by default. You could do this explicitly with a private(i) clause 15

Loop Worksharing Constructs A Motivating Example Sequential code OpenMP parallel region OpenMP parallel region and a worksharing for construct for(i=0;i<n;i++) { a[i] = a[i] + b[i]; #pragma omp parallel { int id, i, Nthrds, istart, iend; id = omp_get_thread_num(); Nthrds = omp_get_num_threads(); istart = id * N / Nthrds; iend = (id+1) * N / Nthrds; if (id == Nthrds-1)iend = N; for(i=istart;i<iend;i++) { a[i] = a[i] + b[i]; #pragma omp parallel #pragma omp for for(i=0;i<n;i++) { a[i] = a[i] + b[i]; 16

Loop Worksharing Constructs: The schedule clause The schedule clause affects how loop iterations are mapped onto threads schedule(static[,chunk]) Deal-out blocks of iterations of size chunk to each thread schedule(dynamic[,chunk]) Each thread grabs chunk iterations off a queue until all iterations have been handled schedule(guided[,chunk]) Threads dynamically grab blocks of iterations. The size of the block starts large and shrinks down to size chunk as the calculation proceeds schedule(runtime) schedule(auto) Schedule and chunk size taken from the OMP_SCHEDULE environment variable (or the runtime library) Schedule is left up to the runtime to choose (does not have to be any of the above) 17

Loop Worksharing Constructs: The schedule clause Schedule Clause STATIC DYNAMIC GUIDED AUTO When To Use Pre-determined and predictable by the programmer Unpredictable, highly variable work per iteration Special case of dynamic to reduce scheduling overhead When the runtime can learn from previous executions of the same loop Least work at runtime : scheduling done at compile-time Most work at runtime : complex scheduling logic used at run-time 18

Working with Loops Basic approach Find compute intensive loops Make the loop iterations independent.. So they can safely execute in any order without loop-carried dependencies Place the appropriate OpenMP directive and test int i, j, A[MAX]; j = 5; for (i=0;i< MAX; i++) { j +=2; A[i] = big(j); Note: loop index i is private by default Remove loop carried dependence int i, A[MAX]; #pragma omp parallel for for (i=0;i< MAX; i++) { int j = 5 + 2*(i+1); A[i] = big(j); 19

Nested Loops For perfectly nested rectangular loops we can parallelize multiple loops in the nest with the collapse clause #pragma omp parallel for collapse(2) for (int i=0; i<n; i++) { for (int j=0; j<m; j++) {... Will form a single loop of length NxM and then parallelize that Useful if N is O(no. of threads) so parallelizing the outer loop may not have good load balance Number of loops to be parallelized, counting from the outside 20

Rules for Collapse Clause Only one collapse clause is allowed on a worksharing DO or PARALLEL DO directive The specified number of loops must be present lexically. None of the loops can be in a called subroutine The loops must form a rectangular iteration space and the bounds and stride of each loop must be invariant over all the loops If the loop indices are of different size, the index with the largest size will be used for the collapsed loop The loops must be perfectly nested. There is no intervening code nor any OpenMP directive between the loops which are collapsed The associated do-loops must be structured blocks. Their execution must not be terminated by an EXIT statement If multiple loops are associated to the loop construct, only an iteration of the innermost associated loop may be curtailed by a CYCLE statement, and there must be no branches to any of the loop termination statements except for the innermost associated loop 21

Reduction How do we handle this case? double ave=0.0, A[MAX]; int i; for (i=0;i< MAX; i++) { ave + = A[i]; ave = ave/max; We are combining values into a single accumulation variable (ave) there is a true dependence between loop iterations that can t be trivially removed This is a very common situation it is called a reduction Support for reduction operations is included in most parallel programming environments 22

Reduction OpenMP reduction clause reduction (op : list) Inside a parallel or a worksharing construct A local copy of each list variable is made and initialized depending on the op (e.g. 0 for + ) Updates occur on the local copy Local copies are reduced into a single value and combined with the original global value The variables in list must be shared in the enclosing parallel region double ave=0.0, A[MAX]; int i; #pragma omp parallel for reduction (+:ave) for (i=0;i< MAX; i++) { ave + = A[i]; ave = ave/max; 23

OpenMP: Reduction Operands/Initial Values Many different associative operands can be used with reduction Initial values are the ones that make sense mathematically Operator Initial value + 0 * 1-0 C/C++ only Operator Initial value & ~0 0 ^ 0 && 1 0 Fortran Only Operator Initial value.and..true..or..false..neqv..false..ieor. 0.IOR. 0.IAND. All bits on.eqv..true. MIN Largest pos. number MAX Most neg. number 24

Example: Numerical Integration Mathematically, we know that: 4.0 1 0 4.0 (1+x 2 ) dx = 2.0 We can approximate the integral as a sum of rectangles: N F(x i ) x i = 0 0.0 X 1.0 where each rectangle has width x and height F(x i ) at the middle of interval i 25

Numerical Integration: Serial PI Program static long num_steps = 100000; double step; void main () { int i; double x, pi, sum = 0.0; step = 1.0/(double) num_steps; for (i=0;i< num_steps; i++){ x = (i+0.5)*step; sum = sum + 4.0/(1.0+x*x); pi = step * sum; 26

Numerical Integration: Solution #include <omp.h> static long num_steps = 100000; double step; void main () { int i; double x, pi, sum = 0.0; step = 1.0/(double) num_steps; #pragma omp parallel { double x; #pragma omp for reduction(+:sum) for (i=0;i< num_steps; i++){ x = (i+0.5)*step; sum = sum + 4.0/(1.0+x*x); pi = step * sum; 27

Single Worksharing Construct The single construct denotes a block of code that is executed by only one thread (not necessarily the master thread) A barrier is implied at the end of the single block (can remove the barrier with a nowait clause) #pragma omp parallel { do_many_things(); #pragma omp single { exchange_boundaries(); do_many_other_things(); 28

Outline OpenMP Overview Creating Threads Parallel Loops Synchronization Data Environment Tasks 29

Synchronization High level synchronization critical atomic barrier ordered Low level synchronization flush Synchronization is used to impose order constraints and to protect access to shared data 30

Synchronization: Critical Mutual exclusion: Only one thread at a time can enter a critical region float res; #pragma omp parallel { float B; int i, id, nthrds; id = omp_get_thread_num(); Threads wait their turn only one at a time calls consume() nthrds = omp_get_num_threads(); for(i=id;i<niters;i+=nthrds){ B = big_job(i); #pragma omp critical res += consume (B); 31

Synchronization: Atomic atomic provides mutual exclusion but only applies to the update of a memory location (the update of X in the following example) #pragma omp parallel { double tmp, B; B = DOIT(); tmp = big_ugly(b); #pragma omp atomic X += big_ugly(b); tmp; Atomic only protects the read/update of X 32

Synchronization: Barrier barrier: Each thread waits until all threads arrive #pragma omp parallel shared (A, B, C) private(id) { id=omp_get_thread_num(); A[id] = big_calc1(id); #pragma omp barrier #pragma omp for for(i=0;i<n;i++){ C[i]=big_calc3(i,A); #pragma omp for nowait for(i=0;i<n;i++){ B[i]=big_calc2(C, i); A[id] = big_calc4(id); implicit barrier at the end of a for worksharing construct no implicit barrier due to nowait implicit barrier at the end of a parallel region 33

Master Construct The master construct denotes a structured block that is only executed by the master thread The other threads just skip it (no synchronization is implied) #pragma omp parallel { do_many_things(); #pragma omp master { exchange_boundaries(); #pragma omp barrier do_many_other_things(); 34

Outline OpenMP Overview Creating Threads Parallel Loops Synchronization Data Environment Tasks 35

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 Both: dynamically allocated memory (ALLOCATE, malloc, new) But not everything is shared... Stack variables in subprograms (Fortran) or functions (C) called from parallel regions are PRIVATE Automatic variables within a statement block are PRIVATE 36

Data Sharing: Examples double A[10]; int main() { int index[10]; #pragma omp parallel work(index); printf( %d\n, index[0]); extern double A[10]; void work(int *index) { double temp[10]; static int count;... A, index, count A, index and count are shared by all threads temp temp temp temp is local to each thread A, index, count 37

Data Sharing: Changing Storage Attributes One can selectively change storage attributes for constructs using the following clauses* SHARED PRIVATE FIRSTPRIVATE The final value of a private inside a parallel loop can be transmitted to the shared variable outside the loop with LASTPRIVATE The default attributes can be overridden with DEFAULT (PRIVATE SHARED NONE) All the clauses on this page apply to the OpenMP construct NOT to the entire region DEFAULT(PRIVATE) is Fortran only All data clauses apply to parallel constructs and worksharing constructs except shared which only applies to parallel constructs. 38

Data Sharing: Private Clause private(var) creates a new local copy of var for each thread The value of the private copies is uninitialized The value of the original variable is unchanged after the region void wrong() { int tmp = 0; #pragma omp parallel for private(tmp) for (int j = 0; j < 1000; ++j) tmp += j; printf( %d\n, tmp); tmp was not initialized tmp is 0 here 39

Data Sharing: Private Clause When is Original Variable Valid? The original variable s value is unspecified if it is referenced outside of the construct Implementations may reference the original variable or a copy.. a dangerous programming practice! int tmp; void danger() { tmp = 0; #pragma omp parallel private(tmp) work(); printf( %d\n, tmp); tmp has unspecified value extern int tmp; void work() { tmp = 5; unspecified which copy of tmp 40

Firstprivate Clause Variables initialized from shared variable C++ objects are copy-constructed incr = 0; #pragma omp parallel for firstprivate(incr) for (i = 0; i <= MAX; i++) { if ((i%2)==0) incr++; A[i] = incr; Each thread gets its own copy of incr with an initial value of 0 41

Lastprivate Clause Variables update shared variable using value from last iteration C++ objects are updated as if by assignment void sq2(int n, double *lastterm) { double x; int i; #pragma omp parllel for lastprivate(x) for (i = 0; i < n; i++){ x = a[i]*a[i] + b[i]*b[i]; b[i] = sqrt(x); *lastterm = x; x has the value it held for the last sequential iteration (i.e., for i=(n-1)) 42

Data Sharing: Default Clause Note that the default storage attribute is DEFAULT(SHARED) (so no need to use it) Exception: #pragma omp task To change default: DEFAULT(PRIVATE) each variable in the construct is made private as if specified in a private clause mostly saves typing DEFAULT(NONE): no default for variables in static extent. Must list storage attribute for each variable in static extent. Good programming practice! Only the Fortran API supports default(private). C/C++ only has default(shared) or default(none). 43

Outline OpenMP Overview Creating Threads Parallel Loops Synchronization Data Environment Tasks 44

What are Tasks? Tasks are independent units of work Threads are assigned to perform the work of each task Tasks may be deferred, may be executed immediately The runtime system decides which of the above Tasks are composed of code to execute data environment internal control variables (ICV) Serial Parallel 45

Task Construct Explicit Task View A team of threads is created at the omp parallel construct A single thread is chosen to execute the while loop lets call this thread L Thread L operates the while loop, creates tasks, and fetches next pointers Each time L crosses the omp task construct it generates a new task and has a thread assigned to it Each task runs in its own thread All tasks complete at the barrier at the end of the parallel region s single construct #pragma omp parallel { #pragma omp single { // block 1 node * p = head; while (p) { //block 2 #pragma omp task private(p) process(p); p = p->next; //block 3 46

Simple Task Example #pragma omp parallel num_threads(8) // assume 8 threads { #pragma omp single private(p) { while (p) { #pragma omp task { processwork(p); p = p->next; A pool of 8 threads is created here One thread gets to execute the while loop The single while loop thread creates a task for each instance of processwork() 47

Why are Tasks Useful? Have potential to parallelize irregular patterns and recursive function calls #pragma omp parallel { #pragma omp single { // block 1 node * p = head; while (p) { //block 2 #pragma omp task process(p); p = p->next; //block 3 Single Threaded Block 1 Block 2 Task 1 Block 3 Block 2 Task 2 Block 3 Block 2 Task 3 Time Thr1 Thr2 Thr3 Thr4 Block 1 Block 3 Block 3 Idle Block 2 Task 1 Idle Time Saved Block 2 Task 2 Block 2 Task 3 48

When are Tasks Guaranteed to Complete Tasks are gauranteed to be complete at thread barriers #pragma omp barrier or task barriers #pragma omp taskwait 49

Task Completion Example #pragma omp parallel { #pragma omp task foo(); #pragma omp barrier #pragma omp single { #pragma omp task bar(); Multiple foo tasks created here one for each thread All foo tasks guaranteed to be completed here One bar task created here bar task guaranteed to be completed here 50

Recursive Matrix Multiplication Consider recursive matrix multiplication, described in next 3 slides How would you parallelize this program using OpenMP tasks? What data considerations need to be addressed? 51

Recursive Matrix Multiplication Quarter each input matrix and output matrix Treat each submatrix as a single element and multiply 8 submatrix multiplications, 4 additions A 1,1 A 1,2 B 1,1 B 1,2 C 1,1 C 1,2 A 2,1 A 2,2 B 2,1 B 2,2 C 2,1 C 2,2 A B C C 1,1 = A 1,1 B 1,1 + A 1,2 B 2,1 C 2,1 = A 2,1 B 1,1 + A 2,2 B 2,1 C 1,2 = A 1,1 B 1,2 + A 1,2 B 2,2 C 2,2 = A 2,1 B 1,2 + A 2,2 B 2,2 52

How to Multiply Submatrices? Use the same routine that is computing the full matrix multiplication Quarter each input submatrix and output submatrix Treat each sub-submatrix as a single element and multiply A11 A 1,1 A11 A 1,2 1,2 B11 B 1,1 1,1 B11 B 1,2 1,2 C11 C 1,1 1,1 C11C 1,2 1,2 A11 A 2,1 A11 A 2,2 2,2 B11 B 2,1 2,1 B11 B 2,2 2,2 C11 C 2,1 2,1 C11 C 2,2 2,2 A 1,1 A B C C 1,1 = A 1,1 B 1,1 + A 1,2 B 2,1 B 1,1 C 1,1 C11 1,1 = A11 1,1 B11 1,1 + A11 1,2 B11 2,1 53

Recursively Multiply Submatrices C 1,1 = A 1,1 B 1,1 + A 1,2 B 2,1 C 2,1 = A 2,1 B 1,1 + A 2,2 B 2,1 C 1,2 = A 1,1 B 1,2 + A 1,2 B 2,2 C 2,2 = A 2,1 B 1,2 + A 2,2 B 2,2 Need range of indices to define each submatrix to be used void matmultrec(int mf, int ml, int nf, int nl, int pf, int pl, double **A, double **B, double **C) {// Dimensions: A[mf..ml][pf..pl] B[pf..pl][nf..nl] C[mf..ml][nf..nl] // C11 += A11*B11 matmultrec(mf, mf+(ml-mf)/2, nf, nf+(nl-nf)/2, pf, pf+(pl-pf)/2, A, B, C); // C11 += A12*B21 matmultrec(mf, mf+(ml-mf)/2, nf, nf+(nl-nf)/2, pf+(pl-pf)/2, pl, A, B, C);... Also need stopping criteria for recursion 54

Recursive Solution #define THRESHOLD 32768 // product size below which simple matmult code is called void matmultrec(int mf, int ml, int nf, int nl, int pf, int pl, double **A, double **B, double **C) // Dimensions: A[mf..ml][pf..pl] B[pf..pl][nf..nl] C[mf..ml][nf..nl] { if ((ml-mf)*(nl-nf)*(pl-pf) < THRESHOLD) matmult (mf, ml, nf, nl, pf, pl, A, B, C); else { #pragma omp task { matmultrec(mf, mf+(ml-mf)/2, nf, nf+(nl-nf)/2, pf, pf+(pl-pf)/2, A, B, C); // C11 += A11*B11 matmultrec(mf, mf+(ml-mf)/2, nf, nf+(nl-nf)/2, pf+(pl-pf)/2, pl, A, B, C); // C11 += A12*B21 #pragma omp task { matmultrec(mf, mf+(ml-mf)/2, nf+(nl-nf)/2, nl, pf, pf+(pl-pf)/2, A, B, C); // C12 += A11*B12 matmultrec(mf, mf+(ml-mf)/2, nf+(nl-nf)/2, nl, pf+(pl-pf)/2, pl, A, B, C); // C12 += A12*B22 #pragma omp task { matmultrec(mf+(ml-mf)/2, ml, nf, nf+(nl-nf)/2, pf, pf+(pl-pf)/2, A, B, C); // C21 += A21*B11 matmultrec(mf+(ml-mf)/2, ml, nf, nf+(nl-nf)/2, pf+(pl-pf)/2, pl, A, B, C); // C21 += A22*B21 #pragma omp task { matmultrec(mf+(ml-mf)/2, ml, nf+(nl-nf)/2, nl, pf, pf+(pl-pf)/2, A, B, C); // C22 += A21*B12 matmultrec(mf+(ml-mf)/2, ml, nf+(nl-nf)/2, nl, pf+(pl-pf)/2, pl, A, B, C); // C22 += A22*B22 #pragma omp taskwait Could be executed in parallel as 4 tasks Each task executes the two calls for the same output submatrix of C 55

International Workshop on OpenMP (IWOMP) 56

References The content expressed in this chapter comes from Clay Breshears, Intel Corp. clay.breshears@intel.com J. Mark Bull, EPCC, The University of Edinburgh, markb@epcc.ed.ac.uk Tim Mattson, Intel Corp. timothy.g.mattson@intel.com Lawrence Livermore National Laboratory, OpenMP Tutorial, https://computing.llnl.gov/tutorials/openmp/ 57