1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 5 Shared Memory Programming with OpenMP An Introduction to Parallel Programming Peter Pacheco.

Slides:



Advertisements
Similar presentations
The University of Adelaide, School of Computer Science
Advertisements

1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 1 Why Parallel Computing? An Introduction to Parallel Programming Peter Pacheco.
OpenMP.
Parallel Processing with OpenMP
Introduction to Openmp & openACC
Introductions to Parallel Programming Using OpenMP
NewsFlash!! Earth Simulator no longer #1. In slightly less earthshaking news… Homework #1 due date postponed to 10/11.
1 Chapter 1 Why Parallel Computing? An Introduction to Parallel Programming Peter Pacheco.
May 2, 2015©2006 Craig Zilles1 (Easily) Exposing Thread-level Parallelism  Previously, we introduced Multi-Core Processors —and the (atomic) instructions.
1 Programming Explicit Thread-level Parallelism  As noted previously, the programmer must specify how to parallelize  But, want path of least effort.
Open[M]ulti[P]rocessing Pthreads: Programmer explicitly define thread behavior openMP: Compiler and system defines thread behavior Pthreads: Library independent.
PARALLEL PROGRAMMING WITH OPENMP Ing. Andrea Marongiu
Scientific Programming OpenM ulti- P rocessing M essage P assing I nterface.
1 Tuesday, November 07, 2006 “If anything can go wrong, it will.” -Murphy’s Law.
DISTRIBUTED AND HIGH-PERFORMANCE COMPUTING CHAPTER 7: SHARED MEMORY PARALLEL PROGRAMMING.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Parallel Programming by Tiago Sommer Damasceno Using OpenMP
1 ITCS4145/5145, Parallel Programming B. Wilkinson Feb 21, 2012 Programming with Shared Memory Introduction to OpenMP.
CSCI-6964: High Performance Parallel & Distributed Computing (HPDC) AE 216, Mon/Thurs 2-3:20 p.m. Pthreads (reading Chp 7.10) Prof. Chris Carothers Computer.
A Very Short Introduction to OpenMP Basile Schaeli EPFL – I&C – LSP Vincent Keller EPFL – STI – LIN.
Programming with Shared Memory Introduction to OpenMP
CS470/570 Lecture 5 Introduction to OpenMP Compute Pi example OpenMP directives and options.
Shared Memory Parallelization Outline What is shared memory parallelization? OpenMP Fractal Example False Sharing Variable scoping Examples on sharing.
Parallel Programming in Java with Shared Memory Directives.
Upcrc.illinois.edu OpenMP Lab Introduction. Compiling for OpenMP Open project Properties dialog box Select OpenMP Support from C/C++ -> Language.
Lecture 5: Shared-memory Computing with Open MP. Shared Memory Computing.
Chapter 17 Shared-Memory Programming. Introduction OpenMP is an application programming interface (API) for parallel programming on multiprocessors. It.
OpenMP - Introduction Süha TUNA Bilişim Enstitüsü UHeM Yaz Çalıştayı
1 OpenMP Writing programs that use OpenMP. Using OpenMP to parallelize many serial for loops with only small changes to the source code. Task parallelism.
OpenMP Blue Waters Undergraduate Petascale Education Program May 29 – June
OpenMP: Open specifications for Multi-Processing What is OpenMP? Join\Fork model Join\Fork model Variables Variables Explicit parallelism Explicit parallelism.
Lecture 8: OpenMP. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism / Implicit parallelism.
O PEN MP (O PEN M ULTI -P ROCESSING ) David Valentine Computer Science Slippery Rock University.
The University of Adelaide, School of Computer Science
OpenMP – Introduction* *UHEM yaz çalıştayı notlarından derlenmiştir. (uhem.itu.edu.tr)
09/06/2011CS4961 CS4961 Parallel Programming Lecture 5: Introduction to Threads (Pthreads and OpenMP) Mary Hall September 6, 2011.
CS 838: Pervasive Parallelism Introduction to OpenMP Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from online references.
Work Replication with Parallel Region #pragma omp parallel { for ( j=0; j
OpenMP fundamentials Nikita Panov
Introduction to OpenMP
Introduction to OpenMP Eric Aubanel Advanced Computational Research Laboratory Faculty of Computer Science, UNB Fredericton, New Brunswick.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 2 Parallel Hardware and Parallel Software An Introduction to Parallel Programming Peter Pacheco.
Threaded Programming Lecture 2: Introduction to OpenMP.
Introduction to Pragnesh Patel 1 NICS CSURE th June 2015.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-1. OpenMP is a portable, multiprocessing API for shared memory computers OpenMP is not a “language” Instead,
Special Topics in Computer Engineering OpenMP* Essentials * Open Multi-Processing.
CPE779: More on OpenMP Based on slides by Laxmikant V. Kale and David Padua of the University of Illinois.
Heterogeneous Computing using openMP lecture 2 F21DP Distributed and Parallel Technology Sven-Bodo Scholz.
COMP7330/7336 Advanced Parallel and Distributed Computing OpenMP: Programming Model Dr. Xiao Qin Auburn University
1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 4 Shared Memory Programming with Pthreads An Introduction to Parallel Programming Peter Pacheco.
Heterogeneous Computing using openMP lecture 1 F21DP Distributed and Parallel Technology Sven-Bodo Scholz.
Introduction to OpenMP
SHARED MEMORY PROGRAMMING WITH OpenMP
Shared Memory Parallelism - OpenMP
Lecture 5: Shared-memory Computing with Open MP
SHARED MEMORY PROGRAMMING WITH OpenMP
CS427 Multicore Architecture and Parallel Computing
Introduction to Parallel Programming
Open[M]ulti[P]rocessing
Computer Engg, IIT(BHU)
Introduction to OpenMP
Shared-Memory Programming
Computer Science Department
OpenMP Quiz B. Wilkinson January 22, 2016.
Multi-core CPU Computing Straightforward with OpenMP
Programming with Shared Memory Introduction to OpenMP
Introduction to OpenMP
OpenMP Quiz.
OpenMP Parallel Programming
Parallel Programming with OPENMP
Presentation transcript:

1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 5 Shared Memory Programming with OpenMP An Introduction to Parallel Programming Peter Pacheco

2 Copyright © 2010, Elsevier Inc. All rights Reserved Roadmap Writing programs that use OpenMP. Using OpenMP to parallelize many serial for loops with only small changes to the source code. Task parallelism. Standard problems in shared-memory programming. # Chapter Subtitle

3 OpenMP An API for shared-memory parallel programming. MP = multiprocessing Designed for systems in which each thread or process can potentially have access to all available memory. System is viewed as a collection of cores or CPU’s, all of which have access to main memory. Copyright © 2010, Elsevier Inc. All rights Reserved

4 A shared memory system Copyright © 2010, Elsevier Inc. All rights Reserved

5 Pragmas Special preprocessor instructions. Typically added to a system to allow behaviors that aren’t part of the basic C specification. Compilers that don’t support the pragmas ignore them. Copyright © 2010, Elsevier Inc. All rights Reserved #pragma

6 Copyright © 2010, Elsevier Inc. All rights Reserved

7 gcc −g −Wall −fopenmp −o omp_hello omp_hello. c. / omp_hello 4 compiling running with 4 threads Hello from thread 0 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 3 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 0 of 4 Hello from thread 3 of 4 Hello from thread 1 of 4 Hello from thread 2 of 4 Hello from thread 0 of 4 possible outcomes

8 OpenMp pragmas Copyright © 2010, Elsevier Inc. All rights Reserved # pragma omp parallel Most basic parallel directive. The number of threads that run the following structured block of code is determined by the run-time system.

9 A process forking and joining two threads Copyright © 2010, Elsevier Inc. All rights Reserved

10 clause Text that modifies a directive. The num_threads clause can be added to a parallel directive. It allows the programmer to specify the number of threads that should execute the following block. Copyright © 2010, Elsevier Inc. All rights Reserved # pragma omp parallel num_threads ( thread_count )

11 Important There may be system-defined limitations on the number of threads that a program can start. The OpenMP standard doesn’t guarantee that this will actually start thread_count threads. Most current systems can start hundreds or even thousands of threads. Unless we’re trying to start a lot of threads, we will almost always get the desired number of threads. Copyright © 2010, Elsevier Inc. All rights Reserved

12 Some terminology In OpenMP parlance the collection of threads executing the parallel block — the original thread and the new threads — is called a team, the original thread is called the master, and the additional threads are called slaves. Copyright © 2010, Elsevier Inc. All rights Reserved

13 In case the compiler doesn’t support OpenMP Copyright © 2010, Elsevier Inc. All rights Reserved # include #ifdef _OPENMP # include #endif

14 In case the compiler doesn’t support OpenMP Copyright © 2010, Elsevier Inc. All rights Reserved # ifdef _OPENMP int my_rank = omp_get_thread_num ( ); int thread_count = omp_get_num_threads ( ); # e l s e int my_rank = 0; int thread_count = 1; # endif

15 THE TRAPEZOIDAL RULE Copyright © 2010, Elsevier Inc. All rights Reserved

16 The trapezoidal rule Copyright © 2010, Elsevier Inc. All rights Reserved

17 Serial algorithm Copyright © 2010, Elsevier Inc. All rights Reserved

18 A First OpenMP Version 1) We identified two types of tasks: a) computation of the areas of individual trapezoids, and b) adding the areas of trapezoids. 2) There is no communication among the tasks in the first collection, but each task in the first collection communicates with task 1b. Copyright © 2010, Elsevier Inc. All rights Reserved

19 A First OpenMP Version 3) We assumed that there would be many more trapezoids than cores. So we aggregated tasks by assigning a contiguous block of trapezoids to each thread (and a single thread to each core). Copyright © 2010, Elsevier Inc. All rights Reserved

20 Assignment of trapezoids to threads Copyright © 2010, Elsevier Inc. All rights Reserved

21 Copyright © 2010, Elsevier Inc. All rights Reserved Unpredictable results when two (or more) threads attempt to simultaneously execute: global_result += my_result ;

22 Mutual exclusion Copyright © 2010, Elsevier Inc. All rights Reserved # pragma omp critical global_result += my_result ; only one thread can execute the following structured block at a time

23 Copyright © 2010, Elsevier Inc. All rights Reserved

24 Copyright © 2010, Elsevier Inc. All rights Reserved

25 SCOPE OF VARIABLES Copyright © 2010, Elsevier Inc. All rights Reserved

26 Scope In serial programming, the scope of a variable consists of those parts of a program in which the variable can be used. In OpenMP, the scope of a variable refers to the set of threads that can access the variable in a parallel block. Copyright © 2010, Elsevier Inc. All rights Reserved

27 Scope in OpenMP A variable that can be accessed by all the threads in the team has shared scope. A variable that can only be accessed by a single thread has private scope. The default scope for variables declared before a parallel block is shared. Copyright © 2010, Elsevier Inc. All rights Reserved

28 Concluding Remarks OpenMP is a standard for programming shared-memory systems. OpenMP uses both special functions and preprocessor directives called pragmas. OpenMP programs start multiple threads rather than multiple processes. Many OpenMP directives can be modified by clauses. Copyright © 2010, Elsevier Inc. All rights Reserved