Introduction to OpenMP. Lecture 4: Work sharing directives

Similar documents
Introduction to OpenMP

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

Shared Memory Programming with OpenMP

Advanced OpenMP. OpenMP Basics

!OMP #pragma opm _OPENMP

Parallelising Scientific Codes Using OpenMP. Wadud Miah Research Computing Group

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

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

OpenMP - Introduction

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

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

ECE 574 Cluster Computing Lecture 10

OPENMP TIPS, TRICKS AND GOTCHAS

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

[Potentially] Your first parallel application

Introduction to Standard OpenMP 3.1

Shared Memory Programming with OpenMP. Lecture 3: Parallel Regions

Introduction to OpenMP. Tasks. N.M. Maclaren September 2017

Introduction to OpenMP

CS 470 Spring Mike Lam, Professor. Advanced OpenMP

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

Introduction to OpenMP

Threaded Programming. Lecture 3: Parallel Regions. Parallel region directive. Code within a parallel region is executed by all threads.

OpenMP 4.0/4.5. Mark Bull, EPCC

CS691/SC791: Parallel & Distributed Computing

OpenMP 4.0. Mark Bull, EPCC

Introduction to Programming with OpenMP

Advanced OpenMP. Tasks

Lecture 4: OpenMP Open Multi-Processing

Parallel Programming. OpenMP Parallel programming for multiprocessors for loops

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

Introduction to OpenMP

Advanced OpenMP. Lecture 11: OpenMP 4.0

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

Introduction to OpenMP

CS4961 Parallel Programming. Lecture 5: More OpenMP, Introduction to Data Parallel Algorithms 9/5/12. Administrative. Mary Hall September 4, 2012

CS 426. Parallel Programming with OpenMP

Allows program to be incrementally parallelized

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 TIPS, TRICKS AND GOTCHAS

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

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

Shared Memory Programming with OpenMP (3)

OpenMP Introduction. CS 590: High Performance Computing. OpenMP. A standard for shared-memory parallel programming. MP = multiprocessing

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

Introduction to OpenMP

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

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

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

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

Parallel Programming with OpenMP. CS240A, T. Yang

High Performance Computing: Tools and Applications

Programming Shared Memory Systems with OpenMP Part I. Book

Parallel Programming. Exploring local computational resources OpenMP Parallel programming for multiprocessors for loops

Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG

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

Compiling and running OpenMP programs. C/C++: cc fopenmp o prog prog.c -lomp CC fopenmp o prog prog.c -lomp. Programming with OpenMP*

COMP Parallel Computing. SMM (2) OpenMP Programming Model

CSL 860: Modern Parallel

Parallel Programming

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

OpenMP 4.5: Threading, vectorization & offloading

OpenMP Tutorial. Dirk Schmidl. IT Center, RWTH Aachen University. Member of the HPC Group Christian Terboven

Overview: The OpenMP Programming Model

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

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

Practical in Numerical Astronomy, SS 2012 LECTURE 12

Parallel Programming

Introduction to OpenMP

15-418, Spring 2008 OpenMP: A Short Introduction

Multi-core Architecture and Programming

Introduction to OpenMP

Introduction to OpenMP

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

Shared Memory Programming Paradigm!

Introduction to OpenMP

Performance Tuning and OpenMP

Introduction to OpenMP

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

Synchronization. Event Synchronization

Introduction to. Slides prepared by : Farzana Rahman 1

Towards OpenMP for Java

Using OpenMP. Shaohao Chen Research Boston University

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

Introduction to OpenMP

Computing on Graphs An Overview

Introduction to OpenMP. Lecture 2: OpenMP fundamentals

CS 470 Spring Mike Lam, Professor. Advanced OpenMP

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

Introduction to OpenMP

Session 4: Parallel Programming with OpenMP

Programming with OpenMP*

OpenMP on Ranger and Stampede (with Labs)

Enhancements in OpenMP 2.0

Computational Mathematics

Introduction to OpenMP

UPDATES. 1. Threads.v. hyperthreading

EPL372 Lab Exercise 5: Introduction to OpenMP

OPENMP OPEN MULTI-PROCESSING

Transcription:

Introduction to OpenMP Lecture 4: Work sharing directives

Work sharing directives Directives which appear inside a parallel region and indicate how work should be shared out between threads Parallel do/for loops Single directive Master directive Sections Workshare

Parallel do loops Loops are the most common source of parallelism in most codes. Parallel loop directives are therefore very important! A parallel do/for loop divides up the iterations of the loop between threads. There is a synchronisation point at the end of the loop: all threads must finish their iterations before any thread can proceed

Parallel do/for loops (cont) Syntax: Fortran: C/C++:!$OMP DO [clauses] do loop [!$OMP END DO ] #pragma omp for [clauses] for loop

Parallel do/for loops (cont) With no additional clauses, the DO/FOR directive will partition the iterations as equally as possible between the threads. However, this is implementation dependent, and there is still some ambiguity: e.g. 7 iterations, 3 threads. Could partition as 3+3+1 or 3+2+2

Restrictions in C/C++ Because the for loop in C is a general while loop, there are restrictions on the form it can take. It has to have determinable trip count - it must be of the form: for (var = a; var logical-op b; incr-exp) where logical-op is one of <, <=, >, >= and incr-exp is var = var +/- incr or semantic equivalents such as var++. Also cannot modify var within the loop body.

Parallel do/for loops (cont) How can you tell if a loop is parallel or not? Useful test: if the loop gives the same answers if it is run in reverse order, then it is almost certainly parallel Jumps out of the loop are not permitted. e.g. do i=2,n a(i)=2*a(i-1) end do

Parallel do/for loops (cont) 2. 3. ix = base do i=1,n a(ix) = a(ix)*b(i) ix = ix + stride end do do i=1,n b(i)= (a(i)-a(i-1))*0.5 end do

Parallel do loops (example) Example:!$OMP PARALLEL!$OMP DO do i=1,n b(i) = (a(i)-a(i-1))*0.5 end do!$omp END DO!$OMP END PARALLEL

Parallel for loops (example) Example: #pragma omp parallel { #pragma omp for for (i=0; i < n; i++) { } } // omp parallel b[i] = (a[i]-a[i-1])*0.5;

Parallel DO/FOR directive This construct is so common that there is a shorthand form which combines parallel region and DO/FOR directives: Fortran: C/C++:!$OMP PARALLEL DO [clauses] do loop [!$OMP END PARALLEL DO ] #pragma omp parallel for [clauses] for loop

Clauses DO/FOR directive can take PRIVATE, FIRSTPRIVATE and REDUCTION clauses which refer to the scope of the loop. Note that the parallel loop index variable is PRIVATE by default other loop indices are private by default in Fortran, but not in C. PARALLEL DO/FOR directive can take all clauses available for PARALLEL directive.

SCHEDULE clause The SCHEDULE clause gives a variety of options for specifying which loops iterations are executed by which thread. Syntax: Fortran: SCHEDULE (kind[, chunksize]) C/C++: schedule(kind[, chunksize]) where kind is one of STATIC, DYNAMIC, GUIDED, AUTO or RUNTIME and chunksize is an integer expression with positive value. E.g.!$OMP DO SCHEDULE(DYNAMIC,4)

STATIC schedule With no chunksize specified, the iteration space is divided into (approximately) equal chunks, and one chunk is assigned to each thread in order (block schedule). If chunksize is specified, the iteration space is divided into chunks, each of chunksize iterations, and the chunks are assigned cyclically to each thread in order (block cyclic schedule)

STATIC schedule

DYNAMIC schedule DYNAMIC schedule divides the iteration space up into chunks of size chunksize, and assigns them to threads on a first-come-first-served basis. i.e. as a thread finish a chunk, it is assigned the next chunk in the list. When no chunksize is specified, it defaults to 1.

GUIDED schedule GUIDED schedule is similar to DYNAMIC, but the chunks start off large and get smaller exponentially. The size of the next chunk is proportional to the number of remaining iterations divided by the number of threads. The chunksize specifies the minimum size of the chunks. When no chunksize is specified it defaults to 1.

DYNAMIC and GUIDED schedules

AUTO schedule Lets the runtime have full freedom to choose its own assignment of iterations to threads If the parallel loop is executed many times, the runtime can evolve a good schedule which has good load balance and low overheads.

Choosing a schedule When to use which schedule? STATIC best for load balanced loops - least overhead. STATIC,n good for loops with mild or smooth load imbalance, but can induce overheads. DYNAMIC useful if iterations have widely varying loads, but ruins data locality. GUIDED often less expensive than DYNAMIC, but beware of loops where the first iterations are the most expensive! AUTO may be useful if the loop is executed many times over

RUNTIME schedule The RUNTIME schedule defers the choice of schedule to run time, when it is determined by the value of the environment variable OMP_SCHEDULE. e.g. export OMP_SCHEDULE= guided,4 It is illegal to specify a chunksize in the code with the RUNTIME schedule.

Nested loops For perfectly nested rectangular loops we can parallelise 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++) {... } } Argument is number of loops to collapse starting from the outside Will form a single loop of length NxM and then parallelise that. Useful if N is O(no. of threads) so parallelising the outer loop may not have good load balance

SINGLE directive Indicates that a block of code is to be executed by a single thread only. The first thread to reach the SINGLE directive will execute the block There is a synchronisation point at the end of the block: all the other threads wait until block has been executed.

SINGLE directive (cont) Syntax: Fortran:!$OMP SINGLE [clauses] block!$omp END SINGLE C/C++: #pragma omp single [clauses] structured block

SINGLE directive (cont) Example: #pragma omp parallel { setup(x); #pragma omp single { input(y); } work(x,y); }

SINGLE directive (cont) SINGLE directive can take PRIVATE and FIRSTPRIVATE clauses. Directive must contain a structured block: cannot branch into or out of it.

MASTER directive Indicates that a block of code should be executed by the master thread (thread 0) only. There is no synchronisation at the end of the block: other threads skip the block and continue executing: N.B. different from SINGLE in this respect.

MASTER directive (cont) Syntax: Fortran:!$OMP MASTER block!$omp END MASTER C/C++: #pragma omp master structured block

Parallel sections Allows separate blocks of code to be executed in parallel (e.g. several independent subroutines) There is a synchronisation point at the end of the blocks: all threads must finish their blocks before any thread can proceed Not scalable: the source code determines the amount of parallelism available. Rarely used, except with nested parallelism - see later!

Parallel sections (cont) Syntax: Fortran:!$OMP SECTIONS [clauses] [!$OMP SECTION ] block [!$OMP SECTION block ]...!$OMP END SECTIONS

Parallel sections (cont) C/C++: #pragma omp sections [clauses] { [ #pragma omp section ] structured-block [ #pragma omp section } structured-block... ]

Parallel sections (cont) Example:!$OMP PARALLEL!$OMP SECTIONS!$OMP SECTION call init(x)!$omp SECTION call init(y)!$omp SECTION call init(z)!$omp END SECTIONS!$OMP END PARALLEL

Parallel sections (cont) SECTIONS directive can take PRIVATE, FIRSTPRIVATE, LASTPRIVATE (see later) and clauses. Each section must contain a structured block: cannot branch into or out of a section.

Parallel section (cont) Shorthand form: Fortran:!$OMP PARALLEL SECTIONS [clauses]...!$omp END PARALLEL SECTIONS C/C++: #pragma omp parallel sections [clauses] {... }

Workshare directive A worksharing directive (!) which allows parallelisation of Fortran 90 array operations, WHERE and FORALL constructs. Syntax:!$OMP WORKSHARE block!$omp END WORKSHARE

Workshare directive (cont.) Simple example REAL A(100,200), B(100,200), C(100,200)...!$OMP PARALLEL!$OMP WORKSHARE A=B+C!$OMP END WORKSHARE!$OMP END PARALLEL N.B. No schedule clause: distribution of work units to threads is entirely up to the compiler! There is a synchronisation point at the end of the workshare: all threads must finish their work before any thread can proceed

Workshare directive (cont.) Can also contain array intrinsic functions, WHERE and FORALL constructs, scalar assignment to shared variables, ATOMIC and CRITICAL directives. No branches in or out of block. No function calls except array intrinsics and those declared ELEMENTAL. Combined directive:!$omp PARALLEL WORKSHARE block!$omp END PARALLEL WORKSHARE

Workshare directive (cont.) Example:!$OMP PARALLEL WORKSHARE REDUCTION(+:t) A = B + C WHERE (D.ne. 0) E = 1/D t = t + SUM(F) FORALL (i=1:n, X(i)=0) X(i)= 1!$OMP END PARALLEL WORKSHARE

Exercise Redo the Mandelbrot example using a worksharing do/for directive.