Chapter 17 Shared-Memory Programming. Introduction OpenMP is an application programming interface (API) for parallel programming on multiprocessors. It.

Slides:



Advertisements
Similar presentations
Implementing Domain Decompositions Intel Software College Introduction to Parallel Programming – Part 3.
Advertisements

NewsFlash!! Earth Simulator no longer #1. In slightly less earthshaking news… Homework #1 due date postponed to 10/11.
Indian Institute of Science Bangalore, India भारतीय विज्ञान संस्थान बंगलौर, भारत Supercomputer Education and Research Centre (SERC) Adapted from: o “MPI-Message.
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.
Introduction to OpenMP For a more detailed tutorial see: Look at the presentations also see:
PARALLEL PROGRAMMING WITH OPENMP Ing. Andrea Marongiu
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.
Computer Architecture II 1 Computer architecture II Programming: POSIX Threads OpenMP.
Parallel Programming in C with MPI and OpenMP Michael J. Quinn.
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.
Parallel Programming in C with MPI and 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.
INTEL CONFIDENTIAL OpenMP for Domain Decomposition Introduction to Parallel Programming – Part 5.
Budapest, November st ALADIN maintenance and phasing workshop Short introduction to OpenMP Jure Jerman, Environmental Agency of Slovenia.
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.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 5 Shared Memory Programming with OpenMP An Introduction to Parallel Programming Peter Pacheco.
Shared Memory Parallelism - OpenMP Sathish Vadhiyar Credits/Sources: OpenMP C/C++ standard (openmp.org) OpenMP tutorial (
Steve Lantz Computing and Information Science In-Class “Guerrilla” Development of MPI Examples Week 5 Lecture Notes.
Parallel Programming in Java with Shared Memory Directives.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Parallel Programming in C with MPI and OpenMP Michael J. Quinn.
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.
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 (
MPI and 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,
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.
Heterogeneous Computing using openMP lecture 1 F21DP Distributed and Parallel Technology Sven-Bodo Scholz.
OpenMP An API : For Writing Portable SMP Application Software Rider NCHC GTD.
Parallel Programming in C with MPI and OpenMP
Introduction to OpenMP
Shared Memory Parallelism - OpenMP
Lecture 5: Shared-memory Computing with Open MP
SHARED MEMORY PROGRAMMING WITH OpenMP
Parallel Programming in C with MPI and OpenMP
Shared-memory Programming
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
Shared-Memory Programming
SHARED MEMORY PROGRAMMING WITH OpenMP
Computer Science Department
Parallel Programming with OpenMP
Programming with Shared Memory Introduction to OpenMP
Introduction to OpenMP
OpenMP Martin Kruliš.
OpenMP Parallel Programming
Shared-Memory Paradigm & OpenMP
Parallel Programming with OPENMP
Presentation transcript:

Chapter 17 Shared-Memory Programming

Introduction OpenMP is an application programming interface (API) for parallel programming on multiprocessors. It consists of a set of compiler directives and a library of support functions.

fork/join parallelism

Incremental parallelization – the process of transforming a sequential program into a parallel program one block of code at a time.

OpenMP compiler directives parallel for parallel for sections parallel sections critical single

OpenMP functions int omp_get_num_procs (void) int omp_get_num_threads (void) int omp_get_thread_num (void) void omp_set_num_threads (int t)

Parallel for Loops for (i=first; I < size; I +=prime) marked [i] = 1;

parallel for Pragma Pragma: a compiler directive in C or C++ is called a pragma. It is short for “pragmatic information”. Syntax: #pragma omp e.g. #pragma omp parallel for for (i=first; I < size; I +=prime) marked [i] = 1;

Execution context Every thread has its own execution context: an address space containing all of variables the thread may access. The execution context includes static variables, dynamically allocated data structures in the heap, and variables on the run-time stack. Shared variable Private variable

Declaring Private Variables private Clause (A clause is an optional component to a pragma) e.g. private ( ) # pragma omp parallel for private (j) for (i=0; i <= BLOCK_SIZE(id,p,n); i++) for (j=0; j < n; j++) a[i][j] = MIN (a[i][j], a[i][k] + tmp[j]);

firstprivate Clause x[0] = complex_function(); #pragma omp parallel for private (j) firstprivate (x) for (i =0; i < n; i++) for (j = 1; j < 4; j++) x[j] = g(i, x[j-1]); answer [i] = x[1] – x[3];

lastprivate Clause #pragma omp parallel for private (j) lastprivate (x) For (I = 0; I < n; i++) { x[0] = 1.0; for (j = 1; j < 4; j++) x[j] = x[j-1] * (I + 1); sum_of_powers[i] = x[0] + x[1] + x[2] + x[3]; } N_cubed = x [3];

Critical Sections #pragma omp parallel for private (x) for (i = 0; i < n; i++) { x = (i+0.5)/n; area += 4.0/(1.0 + x*x); /* Race Condition! */ } pi = area/n;

#pragma omp parallel for private (x) for (i = 0; i < n; i++) { x = (i+0.5)/n; #pragma omp critical area += 4.0/(1.0 + x*x); } pi = area/n;

Reductions Syntax: reduction ( : ) #pragma omp parallel for private (x) reduction (+:area) for (i = 0; i < n; i++) { x = (i+0.5)/n; area += 4.0/(1.0 + x*x); } pi = area/n;

Performance Improvement Inverting Loops e.g. for (i = 1; i < m; i++) for (j = 0; j < n; j++) a[i][j] = 2 * a [i-1][j];

#pragma parallel for private (i) for (j = 0; j < n; j++) for (i = 1; i < m; i++) a[i][j] = 2 * a [i-1][j];

Conditionally Executing Loops #pragma omp parallel for private (x) reduction (+:area) if (n > 5000) for (i = 0; i < n; i++) { x = (i+0.5)/n; area += 4.0/(1.0 + x*x); } pi = area/n;

Scheduling Loops Syntax: Schedule ( [, ]) Schedule (static): A static allocation of about n/t contiguous iterations to each thread. Schedule (static, C): An interleaved allocation of chunks to tasks. Each chunk contains C contiguous iterations.

Schedule (dynamic) : Iterations are dynamically allocated, one at a time, to threads. Schedule (dynamic, C) : A dynamic allocation of C iterations at a time to the tasks. Schedule (guided, C) : A dynamic allocation of iterations to tasks using the guided self-scheduling heuristic. Guided self-scheduling begins by allocating a large chunk size to each task and responds to further requests for chunks by allocating chunks of decreasing size. The size of the chunks decreases exponentially to a minimum chunk size of C.

Schedule(guided): Guided self-scheduling with a minimum chunk size of 1. Schedule(runtime): The schedule type is chosen at run-time based on the value of environment variable OMP_SCHEDULE. e.g. setenv OMP_SCHEDULE “static, 1” would set the run-time schedule to be an interleaved allocation.

More General Data Parallelism

parallel Pragama

for Pragama

single Pragama

nowait Clause

Functional Parallelism e.g. v = alpha(); w = beta(); x = gamma (v, w); y = delta (); printf (“%6.2f\n”, epsilon(x,y));

#pragma omp parallel sections { #pragma omp section /* This pragma optional */ v = alpha(); #pragma omp section w = beta(); #pragma omp section y = delta (); } x = gamma (v, w); printf (“%6.2f\n”, epsilon(x,y));

#pragma omp parallel { #pragma omp sections { #pragma omp section v = alpha(); #pragma omp section w = beta(); } #pragma omp sections { #pragma omp section x = gamma(v,w); #pragma omp section y = delta(); } printf (“%6.2f\n”, epsilon(x,y));

Example : Hello world Write a multithreaded program that prints “hello world”. #include “omp.h” void main() { #pragma omp parallel { int ID = 0; printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } }

Example : Hello world Write a multithreaded program where each thread prints “hello world”. #include “omp.h” void main() { #pragma omp parallel { int ID = omp_get_thread_num(); printf(“ hello(%d) ”, ID); printf(“ world(%d) \n”, ID); } } OpenMP include file Parallel region with default number of threads End of the Parallel region Runtime library function to return a thread ID. Sample Output: hello(1) hello(0) world(1) world(0) hello (3) hello(2) world(3) world(2)

Experimentation Write a function that using sequential implementation of the matrix times vector product in C. Write a function that using OpenMP /MPI implementation of the matrix times vector product in C. Comparing the performance of the two functions.