09/09/2010CS4961 CS4961 Parallel Programming Lecture 6: Data Parallelism in OpenMP, cont. Introduction to Data Parallel Algorithms Mary Hall September.

Slides:



Advertisements
Similar presentations
NewsFlash!! Earth Simulator no longer #1. In slightly less earthshaking news… Homework #1 due date postponed to 10/11.
Advertisements

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.
Mohsan Jameel Department of Computing NUST School of Electrical Engineering and Computer Science 1.
PARALLEL PROGRAMMING WITH OPENMP Ing. Andrea Marongiu
1 OpenMP—An API for Shared Memory Programming Slides are based on:
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 Lecture Set 4 POSIX Threads Overview & OpenMP Johnnie Baker February 2,
Introduction to OpenMP For a more detailed tutorial see: Look at the presentations.
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.
OpenMPI Majdi Baddourah
A Very Short Introduction to OpenMP Basile Schaeli EPFL – I&C – LSP Vincent Keller EPFL – STI – LIN.
Introduction to OpenMP Introduction OpenMP basics OpenMP directives, clauses, and library routines.
1 Parallel Programming With OpenMP. 2 Contents  Overview of Parallel Programming & OpenMP  Difference between OpenMP & MPI  OpenMP Programming Model.
CS240A, T. Yang, 2013 Modified from Demmel/Yelick’s and Mary Hall’s Slides 1 Parallel Programming with OpenMP.
Programming with Shared Memory Introduction to OpenMP
Shared Memory Parallelization Outline What is shared memory parallelization? OpenMP Fractal Example False Sharing Variable scoping Examples on sharing.
Shared Memory Parallelism - OpenMP Sathish Vadhiyar Credits/Sources: OpenMP C/C++ standard (openmp.org) OpenMP tutorial (
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ı
ECE 1747 Parallel Programming Shared Memory: OpenMP Environment and Synchronization.
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.
Introduction to OpenMP. OpenMP Introduction Credits:
OpenMP OpenMP A.Klypin Shared memory and OpenMP Simple Example Threads Dependencies Directives Handling Common blocks Synchronization Improving load balance.
Lecture 8: OpenMP. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism / Implicit parallelism.
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
High-Performance Parallel Scientific Computing 2008 Purdue University OpenMP Tutorial Seung-Jai Min School of Electrical and Computer.
Threaded Programming Lecture 4: Work sharing directives.
09/08/2011CS4961 CS4961 Parallel Programming Lecture 6: More OpenMP, Introduction to Data Parallel Algorithms Mary Hall September 8, 2011.
10/02/2012CS4230 CS4230 Parallel Programming Lecture 11: Breaking Dependences and Task Parallel Algorithms Mary Hall October 2,
09/17/2010CS4961 CS4961 Parallel Programming Lecture 8: Introduction to Threads and Data Parallelism in OpenMP Mary Hall September 17, 2009.
Introduction to OpenMP
Introduction to OpenMP Eric Aubanel Advanced Computational Research Laboratory Faculty of Computer Science, UNB Fredericton, New Brunswick.
09/07/2012CS4230 CS4230 Parallel Programming Lecture 7: Loop Scheduling cont., and Data Dependences Mary Hall September 7,
Shared Memory Parallelism - OpenMP Sathish Vadhiyar Credits/Sources: OpenMP C/C++ standard (openmp.org) OpenMP tutorial (
9/22/2011CS4961 CS4961 Parallel Programming Lecture 9: Task Parallelism in OpenMP Mary Hall September 22,
09/02/2010CS4961 CS4961 Parallel Programming Lecture 4: CTA, cont. Data and Task Parallelism Mary Hall September 2,
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,
10/05/2010CS4961 CS4961 Parallel Programming Lecture 13: Task Parallelism in OpenMP Mary Hall October 5,
Heterogeneous Computing using openMP lecture 2 F21DP Distributed and Parallel Technology Sven-Bodo Scholz.
CPE779: Shared Memory and OpenMP Based on slides by Laxmikant V. Kale and David Padua of the University of Illinois.
CS240A, T. Yang, Parallel Programming with OpenMP.
OpenMP – Part 2 * *UHEM yaz çalıştayı notlarından derlenmiştir. (uhem.itu.edu.tr)
Introduction to OpenMP
Shared Memory Parallelism - OpenMP
Lecture 5: Shared-memory Computing with Open MP
CS427 Multicore Architecture and Parallel Computing
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing A bug in the rwlock program Dr. Xiao Qin.
Open[M]ulti[P]rocessing
Computer Engg, IIT(BHU)
Introduction to OpenMP
Computer Science Department
CS4961 Parallel Programming Lecture 4: Memory Systems and Introduction to Threads (Pthreads and OpenMP) Mary Hall August 30, /30/2012 CS4230.
CS4961 Parallel Programming Lecture 5: Data and Task Parallelism, cont
Parallel Programming with OpenMP
CS4961 Parallel Programming Lecture 5: More OpenMP, Introduction to Data Parallel Algorithms Mary Hall September 4, /04/2012 CS4230.
Prof. Thomas Sterling Department of Computer Science
Introduction to High Performance Computing Lecture 20
Programming with Shared Memory Introduction to OpenMP
Introduction to OpenMP
Shared-Memory Paradigm & OpenMP
Presentation transcript:

09/09/2010CS4961 CS4961 Parallel Programming Lecture 6: Data Parallelism in OpenMP, cont. Introduction to Data Parallel Algorithms Mary Hall September 9, 2010

Homework 2, Due Friday, Sept. 10, 11:59 PM To submit your homework: -Submit a PDF file -Use the “handin” program on : - “handin cs4961 hw2 ” the CADE machines -Use the following command Problem 1 (based on #1 in text on p. 59): Consider the Try2 algorithm for “count3s” from Figure 1.9 of p.19 of the text. Assume you have an input array of 1024 elements, 4 threads, and that the input data is evenly split among the four processors so that accesses to the input array are local and have unit cost. Assume there is an even distribution of appearances of 3 in the elements assigned to each thread which is a constant we call NTPT. What is a bound for the memory cost for a particular thread predicted by the CTA expressed in terms of λ and NTPT. 09/09/2010CS4961

Homework 2, cont Problem 2 (based on #2 in text on p. 59), cont.: Now provide a bound for the memory cost for a particular thread predicted by CTA for the Try4 algorithm of Fig. 114 on p. 23 (or Try3 assuming each element is placed on a separate cache line). Problem 3: For these examples, how is algorithm selection impacted by the value of NTPT? Problem 4 (in general, not specific to this problem): How is algorithm selection impacted by the value of λ? 09/09/2010CS4961

Preview of Programming Assignment 1 Write the prefix sum computation from HW1 in OpenMP for a problem size and data set to be provided. Report your results in a separate README file. -What is the parallel speedup of your code? To compute parallel speedup, you will need to time the execution of both the sequential and parallel code, and report speedup = Time(seq) / Time (parallel) -If your code does not speed up, you will need to adjust the parallelism granularity, the amount of work each processor does between synchronization points. -If possible, you should try different mappings to processors to find a version that achieves the best speedup. -What is the scheduling strategy you used to get the best speedup? 09/09/2010CS4961

Today’s Lecture Data Parallelism in OpenMP -Expressing Parallel Loops -Parallel Regions (SPMD) -Scheduling Loops -Synchronization Sources of material: -Jim Demmel and Kathy Yelick, UCB -Allan Snavely, SDSC -Larry Snyder, Univ. of Washington - 09/09/2010CS4961

Programming with Threads Several Thread Libraries PTHREADS is the Posix Standard [IEEE std, 1995] -Solaris threads are very similar -Relatively low level -Portable but possibly slow OpenMP is newer standard [1997] -Support for scientific programming on shared memory architectures P4 (Parmacs) is another portable package [1987] -Higher level than Pthreads - 09/09/2010CS4961

A Programmer’s View of OpenMP OpenMP is a portable, threaded, shared-memory programming specification with “light” syntax -Exact behavior depends on OpenMP implementation! -Requires compiler support (C/C++ or Fortran) OpenMP will: -Allow a programmer to separate a program into serial regions and parallel regions, rather than concurrently-executing threads. -Hide stack management -Provide synchronization constructs OpenMP will not: -Parallelize automatically -Guarantee speedup -Provide freedom from data races 09/09/2010CS4961

Programming Model – Data Sharing Parallel programs often employ two types of data -Shared data, visible to all threads, similarly named -Private data, visible to a single thread (often stack-allocated) OpenMP: shared variables are shared private variables are private Default is shared Loop index is private PThreads: Global-scoped variables are shared Stack-allocated variables are private // shared, globals int bigdata[1024]; void* foo(void* bar) { // private, stack int tid; /* Calculation goes here */ } int bigdata[1024]; void* foo(void* bar) { int tid; #pragma omp parallel \ shared ( bigdata ) \ private ( tid ) { /* Calc. here */ }

OpenMP Data Parallel Construct: Parallel Loop All pragmas begin: #pragma Compiler calculates loop bounds for each thread directly from serial source (computation decomposition) Compiler also manages data partitioning of Res Synchronization also automatic (barrier) 09/09/2010CS4961

OpenMP Execution Model Fork-join model of parallel execution Begin execution as a single process (master thread) Start of a parallel construct: -Master thread creates team of threads Completion of a parallel construct: -Threads in the team synchronize -- implicit barrier Only master thread continues execution Implementation optimization: -Worker threads spin waiting on next fork fork join 09/09/2010CS4961

OpenMP Execution Model 09/09/2010CS4961

OpenMP directive format C (also Fortran and C++ bindings) Pragmas, format #pragma omp directive_name [ clause [ clause ]... ] new-line Conditional compilation #ifdef _OPENMP block, e.g., printf(“%d avail.processors\n”,omp_get_num_procs()); #endif Case sensitive Include file for library routines #ifdef _OPENMP #include #endif 09/09/2010CS4961

Limitations and Semantics Not all “element-wise” loops can be ||ized #pragma omp parallel for for (i=0; i < numPixels; i++) {} -Loop index: signed integer -Termination Test:,=> with loop invariant int -Incr/Decr by loop invariant int; change each iteration -Count up for,>= -Basic block body: no control in/out except at top Threads are created and iterations divvied up; requirements ensure iteration count is predictable 09/09/2010CS4961

OpenMP Synchronization Implicit barrier -At beginning and end of parallel constructs -At end of all other control constructs -Implicit synchronization can be removed with nowait clause Explicit synchronization -critical -atomic (single statement) -barrier 09/09/2010CS4961

OpenMp Reductions OpenMP has reduce operation sum = 0; #pragma omp parallel for reduction(+:sum) for (i=0; i < 100; i++) { sum += array[i]; } Reduce ops and init() values (C and C++): + 0 bitwise & ~0 logical & bitwise | 0 logical | 0 * 1 bitwise ^ 0 FORTRAN also supports min and max reductions 09/09/2010CS4961

Programming Model – Loop Scheduling schedule clause determines how loop iterations are divided among the thread team -static([chunk]) divides iterations statically between threads -Each thread receives [chunk] iterations, rounding as necessary to account for all iterations -Default [chunk] is ceil( # iterations / # threads ) -dynamic([chunk]) allocates [chunk] iterations per thread, allocating an additional [chunk] iterations when a thread finishes -Forms a logical work queue, consisting of all loop iterations -Default [chunk] is 1 -guided([chunk]) allocates dynamically, but [chunk] is exponentially reduced with each allocation 09/09/2010CS4961

Loop scheduling 2 (2) 09/09/2010CS4961

More loop scheduling attributes RUNTIME The scheduling decision is deferred until runtime by the environment variable OMP_SCHEDULE. It is illegal to specify a chunk size for this clause. AUTO The scheduling decision is delegated to the compiler and/or runtime system. NO WAIT / nowait: If specified, then threads do not synchronize at the end of the parallel loop. ORDERED: Specifies that the iterations of the loop must be executed as they would be in a serial program. COLLAPSE: Specifies how many loops in a nested loop should be collapsed into one large iteration space and divided according to the schedule clause. The sequential execution of the iterations in all associated loops determines the order of the iterations in the collapsed iteration space. 09/09/2010CS4961

Impact of Scheduling Decision Load balance -Same work in each iteration? -Processors working at same speed? Scheduling overhead -Static decisions are cheap because they require no run-time coordination -Dynamic decisions have overhead that is impacted by complexity and frequency of decisions Data locality -Particularly within cache lines for small chunk sizes -Also impacts data reuse on same processor 09/09/2010CS4961

A Few Words About Data Distribution (Ch. 5) Data distribution describes how global data is partitioned across processors. -Recall the CTA model and the notion that a portion of the global address space is physically co-located with each processor This data partitioning is implicit in OpenMP and may not match loop iteration scheduling Compiler will try to do the right thing with static scheduling specifications 09/09/2010CS4961

Common Data Distributions Consider a 1-Dimensional array to solve the count3s problem, 16 elements, 4 threads CYCLIC (chunk = 1): for (i = 0; i<blocksize; i++) … in [i*blocksize + tid]; BLOCK (chunk = 4): for (i=tid*blocksize; i<(tid+1) *blocksize; i++) … in[i]; BLOCK-CYCLIC (chunk = 2): CS /09/2010

OpenMP critical directive Enclosed code – executed by all threads, but – restricted to only one thread at a time #pragma omp critical [ ( name ) ] new-line structured-block A thread waits at the beginning of a critical region until no other thread in the team is executing a critical region with the same name. All unnamed critical directives map to the same unspecified name. 09/09/2010CS4961

Variation: OpenMP parallel and for directives Syntax: #pragma omp for [ clause [ clause ]... ] new-line for-loop clause can be one of the following: shared (list) private( list) reduction( operator: list) schedule( type [, chunk ] ) nowait (C/C++: on #pragma omp for) #pragma omp parallel private(f) { f=7; #pragma omp for for (i=0; i<20; i++) a[i] = b[i] + f * (i+1); } /* omp end parallel */ 09/09/2010CS4961

OpenMP parallel region construct Block of code to be executed by multiple threads in parallel Each thread executes the same code redundantly (SPMD) -Work within work-sharing constructs is distributed among the threads in a team Example with C/C++ syntax #pragma omp parallel [ clause [ clause ]... ] new-line structured-block clause can include the following: private (list) shared (list) 09/09/2010CS4961

OpenMP environment variables OMP_NUM_THREADS  sets the number of threads to use during execution  when dynamic adjustment of the number of threads is enabled, the value of this environment variable is the maximum number of threads to use  For example, setenv OMP_NUM_THREADS 16 [csh, tcsh] export OMP_NUM_THREADS=16 [sh, ksh, bash] OMP_SCHEDULE  applies only to do/for and parallel do/for directives that have the schedule type RUNTIME  sets schedule type and chunk size for all such loops  For example, setenv OMP_SCHEDULE GUIDED,4 [csh, tcsh] export OMP_SCHEDULE= GUIDED,4 [sh, ksh, bash] 09/09/2010CS4961

OpenMP runtime library, Query Functions omp_get_num_threads : Returns the number of threads currently in the team executing the parallel region from which it is called int omp_get_num_threads(void); omp_get_thread_num: Returns the thread number, within the team, that lies between 0 and omp_get_num_threads()-1, inclusive. The master thread of the team is thread 0 int omp_get_thread_num(void); 09/09/2010CS4961

09/09/2010CS4961 Summary of Lecture OpenMP, data-parallel constructs only -Task-parallel constructs later What’s good? -Small changes are required to produce a parallel program from sequential (parallel formulation) -Avoid having to express low-level mapping details -Portable and scalable, correct on 1 processor What is missing? -Not completely natural if want to write a parallel code from scratch -Not always possible to express certain common parallel constructs -Locality management -Control of performance