09/17/2010CS4961 CS4961 Parallel Programming Lecture 8: Introduction to Threads and Data Parallelism in OpenMP Mary Hall September 17, 2009.

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 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,
1 ITCS4145/5145, Parallel Programming B. Wilkinson Feb 21, 2012 Programming with Shared Memory Introduction to OpenMP.
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.
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.
Parallel Programming in Java with Shared Memory Directives.
Lecture 4: Parallel Programming Models. Parallel Programming Models Parallel Programming Models: Data parallelism / Task parallelism Explicit parallelism.
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.
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.
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.
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
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
Includes slides from course CS194 at UC Berkeley, by prof. Katherine Yelick Shared Memory Programming Pthreads: an overview Ing. Andrea Marongiu
Multicore Programming (Parallel Computing) HP Training Session 5 Prof. Dan Connors.
High-Performance Parallel Scientific Computing 2008 Purdue University OpenMP Tutorial Seung-Jai Min School of Electrical and Computer.
09/08/2011CS4961 CS4961 Parallel Programming Lecture 6: More OpenMP, Introduction to Data Parallel Algorithms Mary Hall September 8, 2011.
Introduction to OpenMP
Introduction to OpenMP Eric Aubanel Advanced Computational Research Laboratory Faculty of Computer Science, UNB Fredericton, New Brunswick.
09/09/2010CS4961 CS4961 Parallel Programming Lecture 6: Data Parallelism in OpenMP, cont. Introduction to Data Parallel Algorithms Mary Hall September.
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,
12/22/ Thread Model for Realizing Concurrency B. Ramamurthy.
MPI and OpenMP.
Threaded Programming Lecture 2: Introduction to OpenMP.
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.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
MPSoC Architectures OpenMP Alberto Bosio
Introduction to OpenMP
Lecture 5: Shared-memory Computing with Open MP
CS427 Multicore Architecture and Parallel Computing
Open[M]ulti[P]rocessing
Computer Engg, IIT(BHU)
Introduction to OpenMP
September 4, 1997 Parallel Processing (CS 667) Lecture 5: Shared Memory Parallel Programming with OpenMP* Jeremy R. Johnson Parallel Processing.
Computer Science Department
CS4961 Parallel Programming Lecture 4: Memory Systems and Introduction to Threads (Pthreads and OpenMP) Mary Hall August 30, /30/2012 CS4230.
Realizing Concurrency using Posix Threads (pthreads)
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.
Introduction to High Performance Computing Lecture 20
Programming with Shared Memory Introduction to OpenMP
Introduction to OpenMP
Realizing Concurrency using the thread model
Presentation transcript:

09/17/2010CS4961 CS4961 Parallel Programming Lecture 8: Introduction to Threads and Data Parallelism in OpenMP Mary Hall September 17, 2009

Administrative Programming assignment 1 is posted (after class) Due, Tuesday, September 24 before class -Use the “handin” program on the CADE machines -Use the following command: “handin cs4961 prog1 ” Mailing list set up: 09/17/2010CS4961

Project 1 Assignment PART I Each of the files t2.c, t3.c, t4.c and t5.c from the website cannot be vectorized by the ICC compiler. Your assignment is to produce the equivalent but vectorizable nt2.c, nt3.c, nt4.c and n5t.c. To determine whether or not the compiler has vectorized a loop in the code, look at the output of the compiler that results from the flag –vec-report3. If it says: “remark: LOOP WAS VECTORIZED.”, then you have succeeded! Hints: There are several reasons why the above examples cannot be vectorized. In some cases, the compiler is concerned about the efficiency of the vectorized code. This may be because of the cost of alignment, concerns about exceeding the register capacity (there are only bit registers on most SSE3-supporting platforms!) or the presence of data dependences. In other cases, there is concern about correctness, due to aliasing. 09/17/2010CS4961

Project 1 Assignment, cont. PART II The code example youwrite.c is simplified from a sparse matrix- vector multiply operation used in conjugate gradient. In the example, the compiler is able to generate vectorizable code, but it must deal with alignment in the inner loop. In this portion, we want you to be a replacement for the compiler and use the intrinsic operations found in Appendix C (pp ) of the document found at: m. The way this code will be graded is from looking at it (no running of the code this time around), so it would help if you use comments to describe the operations you are implementing. It would be a good idea to test the code you write, but you will need to generate some artificial input and compare the output of the vectorized code to that of the sequential version. m 09/17/2010CS4961

Things to Remember in this Assignment What are the barriers to “vectorization”? -Correctness -Dependences -Performance -Alignment -Control flow -Packing to get data contiguous 09/17/2010CS4961

Today’s Lecture Brief Overview of POSIX Threads 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/17/2010CS4961

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

Overview of POSIX Threads POSIX: Portable Operating System Interface for UNIX -Interface to Operating System utilities PThreads: The POSIX threading interface -System calls to create and synchronize threads -Should be relatively uniform across UNIX-like OS platforms PThreads contain support for -Creating parallelism -Synchronizing -No explicit support for communication, because shared memory is implicit; a pointer to shared data is passed to a thread 09/17/2010CS4961

Forking Posix Threads thread_id is the thread id or handle (used to halt, etc.) thread_attribute various attributes -standard default values obtained by passing a NULL pointer thread_fun the function to be run (takes and returns void*) fun_arg an argument can be passed to thread_fun when it starts errorcode will be set nonzero if the create operation fails Signature: int pthread_create(pthread_t *, const pthread_attr_t *, void * (*)(void *), void *); Example call: errcode = pthread_create(&thread_id; &thread_attribute &thread_fun; &fun_arg); 09/17/2010CS4961

Simple Threading Example void* SayHello(void *foo) { printf( "Hello, world!\n" ); return NULL; } int main() { pthread_t threads[16]; int tn; for(tn=0; tn<16; tn++) { pthread_create(&threads[tn], NULL, SayHello, NULL); } for(tn=0; tn<16 ; tn++) { pthread_join(threads[tn], NULL); } return 0; } Compile using gcc –lpthread But overhead of thread creation is nontrivial SayHello should have a significant amount of work 09/17/2010CS4961

Shared Data and Threads Variables declared outside of main are shared Object allocated on the heap may be shared (if pointer is passed) Variables on the stack are private: passing pointer to these around to other threads can cause problems Often done by creating a large “thread data” struct -Passed into all threads as argument -Simple example: char *message = "Hello World!\n"; pthread_create( &thread1, NULL, (void*)&print_fun, (void*) message); 09/17/2010CS4961

Posix Thread Example #include void print_fun( void *message ) { printf("%s \n", message); } main() { pthread_t thread1, thread2; char *message1 = "Hello"; char *message2 = "World"; pthread_create( &thread1, NULL, (void*)&print_fun, (void*) message1); pthread_create(&thread2, NULL, (void*)&print_fun, (void*) message2); return(0); } Compile using gcc –lpthread Note: There is a race condition in the print statements 09/17/2010CS4961

Synchronization: Creating and Initializing a Barrier To (dynamically) initialize a barrier, use code similar to this (which sets the number of threads to 3): pthread_barrier_t b; pthread_barrier_init(&b,NULL,3); The second argument specifies an object attribute; using NULL yields the default attributes. To wait at a barrier, a process executes: pthread_barrier_wait(&b); This barrier could have been statically initialized by assigning an initial value created using the macro PTHREAD_BARRIER_INITIALIZER(3). 09/17/2010CS4961

Mutexes (aka Locks) in POSIX Threads To create a mutex: #include pthread_mutex_t amutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_init(&amutex, NULL); To use it: int pthread_mutex_lock(amutex); int pthread_mutex_unlock(amutex); To deallocate a mutex int pthread_mutex_destroy(pthread_mutex_t *mutex); Multiple mutexes may be held, but can lead to deadlock: thread1 thread2 lock(a) lock(b) lock(b) lock(a) 09/17/2010CS4961

Summary of Programming with Threads POSIX Threads are based on OS features -Can be used from multiple languages (need appropriate header) -Familiar language for most of program -Ability to shared data is convenient Pitfalls -Data race bugs are very nasty to find because they can be intermittent -Deadlocks are usually easier, but can also be intermittent OpenMP is commonly used today as a simpler alternative, but it is more restrictive 09/17/2010CS4961

OpenMP Motivation Thread libraries are hard to use -P-Threads/Solaris threads have many library calls for initialization, synchronization, thread creation, condition variables, etc. -Programmer must code with multiple threads in mind Synchronization between threads introduces a new dimension of program correctness Wouldn’t it be nice to write serial programs and somehow parallelize them “automatically”? -OpenMP can parallelize many serial programs with relatively few annotations that specify parallelism and independence -It is not automatic: you can still make errors in your annotations 09/17/2010CS4961

OpenMP: Prevailing Shared Memory Programming Approach Model for parallel programming Shared-memory parallelism Portable across shared-memory architectures Scalable Incremental parallelization Compiler based Extensions to existing programming languages (Fortran, C and C++) -mainly by directives -a few library routines See 09/17/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/17/2010CS4961

Open MP Example: Parallel Loop All pragmas begin: #pragma Example: Convert 32-bit RGB image to 8-bit gray scale ||ism is “element-wise” … for correctness, each element must be independent (work sharing) Preprocessor calculates loop bounds for each thread directly from serial source #pragma omp parallel for for (i=0; i < numPixels; i++) { pGrayScaleBitmap[i] = (unsigned BYTE) (pRGBBitmap[i].red * pRGBBitmap[i].green * pRGBBitmap[i].blue * 0.114); } 09/17/2010CS4961

Another OpenMP example Pragmas as modest extension to existing language * Parallelism (SPMD, task and data parallel) * Data sharing (shared, private, reduction) * Work sharing or scheduling * Locks and critical sections 09/17/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/17/2010CS4961

OpenMP Execution Model 09/17/2010CS4961

OpenMP directive format C 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/17/2010CS4961

OpenMP for directive 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/17/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/17/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/17/2010CS4961

Loop scheduling 2 (2) 09/17/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/17/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 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/17/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/17/2010CS4961

OpenMp Reductions OpenMP has reduce sum = 0; #pragma omp parallel for reduction(+:sum) for (i=0; i < 100; i++) { sum += array[i]; } Reduce ops and init() values: + 0 bitwise & ~0 logical & bitwise | 0 logical | 0 * 1 bitwise ^ 0 09/17/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 09/17/2010CS4961

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/17/2010CS4961

OpenMP critical C / C++: cnt = 0; f=7; #pragma omp parallel { #pragma omp for for (i=0; i<20; i++) { if (b[i] == 0) { #pragma omp critical cnt ++; } /* endif */ a[i] = b[i] + f * (i+1); } /* end for */ } /*omp end parallel */ 09/17/2010CS4961

09/17/2010CS4961 Summary of Lecture OpenMP, data-parallel constructs only -Task-parallel constructs next time What’s good? -Small changes are required to produce a parallel program from sequential -Avoid having to express low-level mapping details -Portable and scalable, correct on 1 processor What is missing? -No scan -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