Auburn University http://www.eng.auburn.edu/~xqin COMP7330/7336 Advanced Parallel and Distributed Computing A bug in the rwlock program Dr. Xiao Qin.

Slides:



Advertisements
Similar presentations
Indian Institute of Science Bangalore, India भारतीय विज्ञान संस्थान बंगलौर, भारत Supercomputer Education and Research Centre (SERC) Adapted from: o “MPI-Message.
Advertisements

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:
DISTRIBUTED AND HIGH-PERFORMANCE COMPUTING CHAPTER 7: SHARED MEMORY PARALLEL PROGRAMMING.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Introduction to OpenMP For a more detailed tutorial see: Look at the presentations.
1 Friday, November 10, 2006 “ Programs for sale: Fast, Reliable, Cheap: choose two.” -Anonymous.
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.
Arquitectura de Sistemas Paralelos e Distribuídos Paulo Marques Dep. Eng. Informática – Universidade de Coimbra Ago/ OpenMP.
1 Parallel Programming With OpenMP. 2 Contents  Overview of Parallel Programming & OpenMP  Difference between OpenMP & MPI  OpenMP Programming Model.
Programming with Shared Memory Introduction to OpenMP
Shared Memory Parallelism - OpenMP Sathish Vadhiyar Credits/Sources: OpenMP C/C++ standard (openmp.org) OpenMP tutorial (
Introduction to OpenMP
UNIT -6 PROGRAMMING SHARED ADDRESS SPACE PLATFORMS THREAD BASICS PREPARED BY:-H.M.PATEL.
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 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)
04/10/25Parallel and Distributed Programming1 Shared-memory Parallel Programming Taura Lab M1 Yuuki Horita.
OpenMP Martin Kruliš Jiří Dokulil. OpenMP OpenMP Architecture Review Board Compaq, HP, Intel, IBM, KAI, SGI, SUN, U.S. Department of Energy,…
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.
Introduction to OpenMP
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,
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-3. OMP_INIT_LOCK OMP_INIT_NEST_LOCK Purpose: ● This subroutine initializes a lock associated with the lock variable.
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,
CPE779: More on OpenMP Based on slides by Laxmikant V. Kale and David Padua of the University of Illinois.
3/12/2013Computer Engg, IIT(BHU)1 OpenMP-2. Environment Variables OMP_NUM_THREADS OMP_SCHEDULE.
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.
COMP7330/7336 Advanced Parallel and Distributed Computing OpenMP: Programming Model Dr. Xiao Qin Auburn University
COMP7330/7336 Advanced Parallel and Distributed Computing MapReduce - Introduction Dr. Xiao Qin Auburn University
COMP7330/7336 Advanced Parallel and Distributed Computing POSIX Thread API Dr. Xiao Qin Auburn University
COMP7330/7336 Advanced Parallel and Distributed Computing POSIX Thread API Dr. Xiao Qin Auburn University
B. Estrade, LSU – High Performance Computing Enablement Group OpenMP II B. Estrade.
OpenMP An API : For Writing Portable SMP Application Software Rider NCHC GTD.
COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University
MPSoC Architectures OpenMP Alberto Bosio
Introduction to OpenMP
Martin Kruliš Jiří Dokulil
Shared Memory Parallelism - OpenMP
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Thread Basics Dr. Xiao Qin Auburn University.
CS427 Multicore Architecture and Parallel Computing
Auburn University COMP7330/7336 Advanced Parallel and Distributed Computing Improving Barrier Performance Dr. Xiao Qin.
Open[M]ulti[P]rocessing
Computer Engg, IIT(BHU)
Introduction to OpenMP
Shared-Memory Programming
Computer Science Department
Shared Memory Programming with OpenMP
Parallel Programming with OpenMP
Programming with Shared Memory
Introduction to High Performance Computing Lecture 20
Programming with Shared Memory Introduction to OpenMP
Introduction to OpenMP
OpenMP Martin Kruliš.
OpenMP Parallel Programming
Presentation transcript:

Auburn University http://www.eng.auburn.edu/~xqin COMP7330/7336 Advanced Parallel and Distributed Computing A bug in the rwlock program Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu 17 Minutes: slides 1-2 Slides are adopted and modified from materials developed Drs. Grama, Gupta, Karypis, and Kumar

pthread_rwlock_t rwlock; int minimum_value = 100; void *find_min_rwlock(void *list_ptr) { int *my_min = (int *)list_ptr; pthread_rwlock_rdlock(&rwlock); if (*my_min < minimum_value) { pthread_rwlock_unlock(&rwlock); pthread_rwlock_wrlock(&rwlock); minimum_value = *my_min; } pthread_exit(NULL); int main(void) { int i, args[2] = {56, 32}; pthread_t threads[2]; OpenMP directives in C and C++ are based on the #pragma compiler directives. Sample directive name: parallel, for, sections

void *find_min_rwlock(void *list_ptr) { int *my_min = (int *)list_ptr; pthread_rwlock_rdlock(&rwlock); if (*my_min < minimum_value) pthread_rwlock_unlock(&rwlock); pthread_rwlock_wrlock(&rwlock); minimum_value = *my_min; } pthread_exit(NULL); OpenMP directives in C and C++ are based on the #pragma compiler directives. Sample directive name: parallel, for, sections

Auburn University http://www.eng.auburn.edu/~xqin COMP7330/7336 Advanced Parallel and Distributed Computing OpenMP: Speficying Concurrent Tasks Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu 17 Minutes: slides 1-2 bugs in the rwlock program. 33 Minutes: slides 3-24 Slides are adopted and modified from materials developed Drs. Grama, Gupta, Karypis, and Kumar

Review: The #pragma Compiler Directives A directive consists of a directive name followed by clauses. #pragma omp directive [clause list] OpenMP programs execute serially until they encounter the parallel directive, which creates a group of threads. #pragma omp parallel [clause list] /* structured block */ The main thread that encounters the parallel directive becomes the master of this group of threads and is assigned the thread id 0 within the group. OpenMP directives in C and C++ are based on the #pragma compiler directives. Sample directive name: parallel, for, sections

Review: A clause list specifies … Conditional Parallelization: The clause if (scalar expression) determines whether the parallel construct results in creation of threads. Degree of Concurrency: The clause num_threads(integer expression) specifies the number of threads that are created. Data Handling: The clause private (variable list) indicates variables local to each thread. The clause firstprivate (variable list) is similar to the private, except values of variables are initialized to corresponding values before the parallel directive. The clause shared (variable list) indicates that variables are shared across all the threads. Specify conditional parallelization, number of threads, and data handling. Conditional Parallelization: The clause if (scalar expression) determines whether the parallel construct results in creation of threads. Degree of Concurrency: The clause num_threads(integer expression) specifies the number of threads that are created. Data Handling: The clause private (variable list) indicates variables local to each thread. The clause firstprivate (variable list) is similar to the private, except values of variables are initialized to corresponding values before the parallel directive. The clause shared (variable list) indicates that variables are shared across all the threads.

Specifying Concurrent Tasks in OpenMP OpenMP provides two directives - for and sections - to specify concurrent iterations and tasks. The for directive is used to split parallel iteration spaces across threads. The general form of a for directive is as follows: #pragma omp parallel for [clause list] /* for loop */ Q1: What are the clauses that can be used in this context? private, firstprivate, lastprivate, reduction, schedule, nowait, and ordered. The parallel directive can be used in conjunction with other directives to specify concurrency across iterations and tasks.

OpenMP Programming: Version 1.0 demo: pi.c /* ****************************************************** An OpenMP version of a threaded program to compute PI. ****************************************************** */ #pragma omp parallel default(private) shared (npoints) \ reduction(+: sum) num_threads(8) { num_threads = omp_get_num_threads(); sample_points_per_thread = npoints / num_threads; sum = 0; for (i = 0; i < sample_points_per_thread; i++) { rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1); rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1); if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) sum ++; }

OpenMP Programming: Version 2.0 Q2: what’s new? #pragma omp parallel default(private) shared (npoints) \ reduction(+: sum) num_threads(8) { sum = 0; #pragma omp for for (i = 0; i < npoints; i++) { rand_no_x =(double)(rand_r(&seed))/(double)((2<<14)-1); rand_no_y =(double)(rand_r(&seed))/(double)((2<<14)-1); if (((rand_no_x - 0.5) * (rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)) < 0.25) sum ++; }

Assigning Iterations to Threads The schedule clause of the for directive deals with the assignment of iterations to threads. The general form of the schedule directive is schedule(scheduling_class[, parameter]) OpenMP supports four scheduling classes: static, dynamic, guided, and runtime.

Assigning Iterations to Threads: Example /* static scheduling of matrix multiplication loops */ #pragma omp parallel default(private) shared (a, b, c, dim) \ num_threads(4) #pragma omp for schedule(static) for (i = 0; i < dim; i++) { for (j = 0; j < dim; j++) { c(i,j) = 0; for (k = 0; k < dim; k++) { c(i,j) += a(i, k) * b(k, j); }

Assigning Iterations to Threads: dim=128, num_threads=4, size=32 (see Fig. a) Default chunk size schedule(static, 16) Three different schedules using the static scheduling class of OpenMP.

Other Scheduling Policies Dynamic Scheduling schedule(dynamic [, chunk-size]) Guided Scheduling schedule(guided [, chunk-size]) Dynanmic scheduling: for dynamic load balancing. 100 iterations Chunk size 5 16 threads: 12 threads -> 1 chunk (idle) solution. Reduce the chunk size. 4 threads -> 2 chunks Runtime Scheduling: delay scheduling decisions until runtime

Parallel For Loops Often, it is desirable to have a sequence of for-directives within a parallel construct that do not execute an implicit barrier at the end of each for directive. OpenMP provides a clause - nowait, which can be used with a for directive.

Parallel For Loops: Example Why does nowait work for this case? #pragma omp parallel { #pragma omp for nowait for (i = 0; i < nmax; i++) if (isEqual(name, current_list[i]) processCurrentName(name); #pragma omp for for (i = 0; i < mmax; i++) if (isEqual(name, past_list[i]) processPastName(name); }

The sections Directive OpenMP supports non-iterative parallel task assignment using the sections directive. The general form of the sections directive is as follows: #pragma omp sections [clause list] { [#pragma omp section /* structured block */ ] ... }

The sections Directive: Example #pragma omp parallel { #pragma omp sections #pragma omp section taskA(); } taskB(); taskC();

Nesting parallel Directives Nested parallelism can be enabled using the OMP_NESTED environment variable. If the OMP_NESTED environment variable is set to TRUE, nested parallelism is enabled. In this case, each parallel directive creates a new team of threads.

Synchronization Constructs in OpenMP #pragma omp barrier #pragma omp single [clause list] structured block #pragma omp master #pragma omp critical [(name)] #pragma omp ordered

OpenMP Library Functions A number of functions that allow a programmer to control the execution of threaded programs. /* thread and processor count */ void omp_set_num_threads (int num_threads); int omp_get_num_threads (); int omp_get_max_threads (); int omp_get_thread_num (); int omp_get_num_procs (); int omp_in_parallel(); In addition to directives, OpenMP also supports a number of functions that allow a programmer to control the execution of threaded programs.

OpenMP Library Functions /* controlling and monitoring thread creation */ void omp_set_dynamic (int dynamic_threads); int omp_get_dynamic (); void omp_set_nested (int nested); int omp_get_nested (); /* mutual exclusion */ void omp_init_lock (omp_lock_t *lock); void omp_destroy_lock (omp_lock_t *lock); void omp_set_lock (omp_lock_t *lock); void omp_unset_lock (omp_lock_t *lock); int omp_test_lock (omp_lock_t *lock); In addition, all lock routines also have a nested lock counterpart for recursive mutexes.

Environment Variables in OpenMP OMP_NUM_THREADS: This environment variable specifies the default number of threads created upon entering a parallel region. OMP_SET_DYNAMIC: Determines if the number of threads can be dynamically changed. OMP_NESTED: Turns on nested parallelism. OMP_SCHEDULE: Scheduling of for-loops if the clause specifies runtime

Explicit Threads versus Directive Based Programming Directives layered on top of threads facilitate a variety of thread-related tasks. A programmer is rid of the tasks of initializing attributes objects, setting up arguments to threads, partitioning iteration spaces, etc. There are some drawbacks to using directives as well. An artifact of explicit threading is that data exchange is more apparent. This helps in alleviating some of the overheads from data movement, false sharing, and contention. Explicit threading also provides a richer API in the form of condition waits, locks of different types, and increased flexibility for building composite synchronization operations. Finally, since explicit threading is used more widely than OpenMP, tools and support for Pthreads programs are easier to find.

Summary For and section directives Assigning Iterations to Threads (the schedule clause) Synchronization constructs and functions