Shared Memory Parallelism - Parallel and Distributed Computing - Lecture Slides, Slides of Parallel Computing and Programming

During the course of work of the Parallel and Distributed Computing we learn the core of the programming. The main points disucss in these lecture slides are:Shared Memory Parallelism, Join Model, Parent Thread, Child Threads, Gang Scheduling, Shared-Memory Threading, Multi-Vendor Support, Programming Model, Fork-Join Parallelism, Data Environment, Parallel Regions

Typology: Slides

2012/2013

Uploaded on 04/24/2013

banamala
banamala 🇮🇳

4.4

(19)

114 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
How Shared Memory Parallelism
Behaves
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Partial preview of the text

Download Shared Memory Parallelism - Parallel and Distributed Computing - Lecture Slides and more Slides Parallel Computing and Programming in PDF only on Docsity!

How Shared Memory Parallelism

Behaves

The Fork/Join Model

  • Many shared memory parallel systems use a programming model called Fork/Join. Each program begins executing on just a single thread, called the parent.
  • Fork: When a parallel region is reached, the parent thread spawns additional child threads as needed.
  • Join: When the parallel region ends, the child threads shut down, leaving only the parent still running.

The Fork/Join Model (cont’d)

  • In principle, as a parallel section completes, the child threads shut down (join the parent), forking off again when the parent reaches another parallel section.
  • In practice, the child threads often continue to exist but are idle.
  • Why?

Principle vs. Practice

5

Fork

Join

Start

End

Fork

Join

Start

End

Idle

OpenMP

What Is OpenMP?

  • Portable, shared-memory threading API
    • Fortran, C, and C++
    • Multi-vendor support for both Linux and Windows
  • Standardizes task & loop-level parallelism
  • Supports coarse-grained parallelism
  • Combines serial and parallel code in single source
  • Standardizes ~ 20 years of compiler-directed threading experience

8

http://www.openmp.org Current spec is OpenMP 3. 318 Pages (combined C/C++ and Fortran)

A Few Syntax Details to Get Started

  • Most of the constructs in OpenMP are compiler directives or pragmas - For C and C++, the pragmas take the form: - #pragma omp construct *clause *clause+…+
  • Header file
    • #include <omp.h>

Agenda

  • What is OpenMP?
  • Parallel regions
  • Worksharing
  • Data environment
  • Synchronization

Code 1: Hello World(s)

Worksharing

  • Worksharing is the general term used in OpenMP to describe distribution of work across threads.
  • Three examples of worksharing in OpenMP are: - omp for construct - omp sections construct - omp task construct 14

Automatically divides work among threads

Combining constructs

  • These two code segments are equivalent

16

#pragma omp parallel { #pragma omp for for (i=0;i< MAX; i++) { res[i] = huge(); } } #pragma omp parallel for for (i=0;i< MAX; i++) { res[i] = huge(); }

The Private Clause

  • Reproduces the variable for each task
    • Variables are un-initialized; C++ object is default constructed
    • Any value external to the parallel region is undefined

17

void work(float c, int N) { #pragma omp parallel for private(x,y)**^ float x, y; int i; for(i=0; i<N; i++) { x = a[i]; y = b[i]; c[i] = x + y; }^ }

Schedule Clause Example

19

#pragma omp parallel for schedule (static, 8) for( int i = start; i <= end; i += 2 ) { if ( TestForPrime(i) ) gPrimesFound++; }

Iterations are divided into chunks of 8

  • If start = 3, then first chunk is i ={3,5,7,9,11,13,15,17}

Data Scoping – What’s shared

  • OpenMP uses a shared-memory programming model
  • Shared variable - a variable whose name provides access to a the same block of storage for each task region - Shared clause can be used to make items explicitly shared - Global variables are shared among tasks - C/C++: File scope variables, namespace scope variables, static variables, Variables with const-qualified type having no mutable member are shared, Static variablesDocsity.com^20