EPL372 Lab Exercise 5: Introduction to OpenMP

Similar documents
Introduction to OpenMP

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

Lecture 4: OpenMP Open Multi-Processing

Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG

Shared Memory Parallelism - OpenMP

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

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

OpenMP - Introduction

Introduction to OpenMP

Session 4: Parallel Programming with OpenMP

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

High Performance Computing: Tools and Applications

Shared Memory Parallelism using OpenMP

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

Introduction to. Slides prepared by : Farzana Rahman 1

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

PC to HPC. Xiaoge Wang ICER Jan 27, 2016

Distributed Systems + Middleware Concurrent Programming with OpenMP

OpenMP Algoritmi e Calcolo Parallelo. Daniele Loiacono

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

CS691/SC791: Parallel & Distributed Computing

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

Alfio Lazzaro: Introduction to OpenMP

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

15-418, Spring 2008 OpenMP: A Short Introduction

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

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

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

DPHPC: Introduction to OpenMP Recitation session

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

Lecture 2 A Hand-on Introduction to OpenMP

Parallel and Distributed Programming. OpenMP

OpenMP threading: parallel regions. Paolo Burgio

ECE 574 Cluster Computing Lecture 10

CS 470 Spring Mike Lam, Professor. OpenMP

Parallel programming using OpenMP

OpenMP Fundamentals Fork-join model and data environment

CS 470 Spring Mike Lam, Professor. OpenMP

Multithreading in C with OpenMP

An Introduction to OpenMP

OpenMP on Ranger and Stampede (with Labs)

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

Shared Memory Programming with OpenMP

Parallelising Scientific Codes Using OpenMP. Wadud Miah Research Computing Group

Introduction to OpenMP.

Parallel Programming

CSL 860: Modern Parallel

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

Shared Memory Programming Paradigm!

Programming Shared Memory Systems with OpenMP Part I. Book

Practical in Numerical Astronomy, SS 2012 LECTURE 12

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

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

OpenMP loops. Paolo Burgio.

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

Shared memory parallel computing

CS 5220: Shared memory programming. David Bindel

Introduction to Standard OpenMP 3.1

Multi-core Architecture and Programming

Shared Memory programming paradigm: openmp

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

Synchronisation in Java - Java Monitor

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

Overview: The OpenMP Programming Model

Data Environment: Default storage attributes

Mango DSP Top manufacturer of multiprocessing video & imaging solutions.

Parallel Programming with OpenMP. CS240A, T. Yang

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

Introduction to OpenMP

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

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

A brief introduction to OpenMP

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

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

Parallel Programming in C with MPI and OpenMP

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

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

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

Parallel Programming: OpenMP

OpenMP Application Program Interface

Introduction to OpenMP

Parallel and Distributed Computing

Shared Memory Programming with OpenMP

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

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

Shared Memory Programming Model

In-Class Guerrilla Development of MPI Examples

Assignment 1 OpenMP Tutorial Assignment

COMP4510 Introduction to Parallel Computation. Shared Memory and OpenMP. Outline (cont d) Shared Memory and OpenMP

OpenMP. A parallel language standard that support both data and functional Parallelism on a shared memory system

Concurrent Programming with OpenMP

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

OPENMP OPEN MULTI-PROCESSING

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

Parallel Computing. Lecture 13: OpenMP - I

EE/CSCI 451: Parallel and Distributed Computation

OpenMP: The "Easy" Path to Shared Memory Computing

DPHPC: Introduction to OpenMP Recitation session

Practical Workbook Parallel and Distributed Computing

Shared memory programming

Transcription:

EPL372 Lab Exercise 5: Introduction to OpenMP References: https://computing.llnl.gov/tutorials/openmp/ http://openmp.org/wp/openmp-specifications/ http://openmp.org/mp-documents/openmp-4.0-c.pdf http://openmp.org/mp-documents/openmp4.0.0.examples.pdf http://www.training.prace-ri.eu/uploads/tx_pracetmo/omp_tutorial2.pdf 1

What is OpenMP OpenMP: An API for Writing Multithreaded Applications A set of compiler directives and library routines for parallel application programmers Greatly simplifies writing multi-threaded (MT) programs in Fortran, C and C++ Standardizes last 20 years of SMP practice Current Version 4.0 (http://openmp.org/wp/openmpspecifications/) 2

OpenMP Solution Stack End User Application Directives, Compiler OpenMP library Environment variables OpenMP Runtime library OS/system support for shared memory and threading Proc1 Proc 2 Proc 3 ProcN Shared Address Space 3

History of OpenMP Today version 4.0 http://openmp.org/mp-documents/openmp-4.0-c.pdf 4

Programming Execution Model Shared Memory, Thread Based Parallelism: OpenMP is based upon the existence of multiple threads in the shared memory programming paradigm. A shared memory process consists of multiple threads. Explicit Parallelism: OpenMP is an explicit (not automatic) programming model, offering the programmer full control over parallelization. Fork - Join Model: - OpenMP uses the fork-join model of parallel execution - All OpenMP programs begin as a single process: the master thread. The master thread executes sequentially until the first parallel region construct is encountered - FORK: the master thread then creates a team of parallel threads - The statements in the program that are enclosed by the parallel region construct are then executed in parallel among the various team threads - JOIN: When the team threads complete the statements in the parallel region construct, they synchronize and terminate, leaving only the master thread 5

The essence of OpenMP Create threads that execute in a shared address space: The only way to create threads is with the parallel construct Once created, all threads execute the code inside the construct. Split up the work between threads by one of two means: SPMD (Single program Multiple Data) all threads execute the same code and you use the thread ID to assign work to a thread. Workshare constructs split up loops and tasks between threads. Manage data environment to avoid data access conflicts Synchronization so correct results are produced regardless of how threads are scheduled. Carefully manage which data can be private (local to each thread) and shared. 6

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

C / C++ - General Code Structure #include <omp.h> main () { int var1, var2, var3; Serial code. Beginning of parallel section. Fork a team of threads. Specify variable scoping #pragma omp parallel private(var1, var2) shared(var3) { Parallel section executed by all threads. Other OpenMP directives. Run-time Library calls. All threads join master thread and disband } Resume serial code. } 8

Specifying the number of threads The number of threads is controlled by an internal control variable (ICV) called nthreads-var. When a parallel construct is found a parallel region with a maximum of nthreads-var is created Parallel constructs can be nested creating nested parallelism The nthreads-var can be modified through the omp_set_num_threads API called (omp_set_num_threads (2)) the OMP_NUM_THREADS environment variable Additionally, the num_threads clause causes the implementation to ignore the ICV and use the value of the clause for that region. 9

Other useful routines API calls int omp_get_num_threads() Returns the number of threads in the current team int omp_get_thread_num() Returns the id of the thread in the current team int omp_get_num_procs() Returns the number of processors in the machine int omp_get_max_threads() Returns the maximum number of threads that will be used in the next parallel region double omp_get_wtime() Returns the number of seconds since an arbitrary point in the past 10

Data environment shared the variable inside the construct is the same as the one outside the construct. private the variable inside the construct is a new variable of the same type with an undefined value Firstprivate the variable inside the construct is a new variable of the same type but it is initialized to the original variable value. 11

12

REDUCTION Clause Purpose: The REDUCTION clause performs a reduction on the variables that appear in its list. A private copy for each list variable is created for each thread. At the end of the reduction, the reduction variable is applied to all private copies of the shared variable, and the final result is written to the global shared variable. #include <omp.h> main () { int i, n, chunk; float a[100], b[100], result; /* Some initializations */ n = 100; chunk = 10; result = 0.0; for (i=0; i < n; i++) { a[i] = i * 1.0; b[i] = i * 2.0; } #pragma omp parallel for \ default(shared) private(i) \ schedule(static,chunk) \ reduction(+:result) for (i=0; i < n; i++) result = result + (a[i] * b[i]); printf("final result= %f\n",result); } 13

OpenMP Examples Compile and Run ExampleOMP1.c Compile and Run ExampleOMP2.c Compile and Run ExampleOMP3.c 14

Debuging OpenMP Threads gcc -fopenmp -Wall -Werror example3omp.c -g -o a.out gdb./a.out b 47 r thread info threads 15