Docsity

Parallel Programming Languages: Shared Memory vs. Distributed Memory Paradigms - Prof. Cha, Study notes of Computer Science

Toggle sidebar
bg1
1
CMSC 838T – Lecture 8
CMSC 838T – Lecture 8
Parallel programming
0Examine parallel programming paradigms & languages
0Case study of OpenMP and MPI
Region 1 Region 2
Fork-join paradigm
(OpenMP)
SPMD paradigm
(MPI)
Proc #1
Instructions
Data
Proc #n
Instructions
Data
Program
Proc #2
Instructions
Data
…
CMSC 838T – Lecture 8
Parallel Programming Languages
Why parallel programming languages?
0Automatic parallelization of sequential programs is difficult
0Easier to write programs to explicitly exploit parallelism
Goals
0Easy to program
zDecomposition, mapping, communication, synchronization
zAvoids errors – data races, deadlocks, buffer overflows
0Flexible paradigm & architecture independent
zSupports wide range of parallel applications
zRuns on a variety of parallel architectures
0Performance
zAble to achieve good performance on parallel architecture
zSimple programming model to sustain performance
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a

Partial preview of the text

Download Parallel Programming Languages: Shared Memory vs. Distributed Memory Paradigms - Prof. Cha and more Study notes Computer Science in PDF only on Docsity!

CMSC 838T – Lecture 8

CMSC 838T – Lecture 8

‹ Parallel programming

0 Examine parallel programming paradigms & languages 0 Case study of OpenMP and MPI

Region 1 Region 2

Fork-join paradigm (OpenMP)

SPMD paradigm (MPI)

Proc #

Instructions

Data

Proc #n

Instructions

Data

Program

Proc #

Instructions

Data

Parallel Programming Languages

‹ Why parallel programming languages?

0 Automatic parallelization of sequential programs is difficult 0 Easier to write programs to explicitly exploit parallelism

‹ Goals

0 Easy to program z Decomposition, mapping, communication, synchronization z Avoids errors – data races, deadlocks, buffer overflows 0 Flexible paradigm & architecture independent z Supports wide range of parallel applications z Runs on a variety of parallel architectures 0 Performance z Able to achieve good performance on parallel architecture z Simple programming model to sustain performance

CMSC 838T – Lecture 8

Parallel Programming Languages

‹ Goals conflict!

‹ In practice, languages fall into two camps

0 Shared memory paradigm z Specify parallelism z Relatively easy to program z Runs efficiently on shared-memory architectures z Some can run on distributed memory architectures (efficiency varies, depending on application) 0 Distributed memory paradigm z Specify parallelism & interprocessor communication z More difficult to program z Runs on both shared & distributed-memory architectures

Parallel Programming Languages

‹ Shared memory with explicit threads

0 Pthreads, Java threads

‹ Shared memory with implicit threads

0 Data parallel (SIMD) – Fortran 90, HPF 0 Parallel loops / tasks (MIMD) – OpenMP, UPC

‹ Distributed memory with explicit communication

0 MPI, SHMEM, Distributed Java

‹ Distributed memory with special global accesses

0 Co-Array Fortran, Global Arrays, UPC

CMSC 838T – Lecture 8

Parallel Programming Overview

‹ Motivation

‹ Shared memory paradigms

0 Explicit threads 0 Implicit threads

‹ Distributed memory paradigms

0 Explicit messages 0 Implicit messages (special nonlocal accesses)

‹ Comparisons

‹ Case study

0 OpenMP 0 MPI

OpenMP

‹ Characteristics

0 Both local & shared memory (depending on directives) 0 Parallelism : directives for parallel loops, functions 0 Compilers convert programs into Pthreads 0 Not available on clusters

‹ Example

#pragma omp parallel for private(i) for (i=0; i<NUPDATE; i++) { int ran = random(); table[ ran & (TABSIZE-1) ] ^= stable[ ran >> (64-LSTSIZE) ]; }

CMSC 838T – Lecture 8

FORTRAN 90

‹ Characteristics

0 Extension of 1D vector operations 0 Arrays treated as single object 0 Operations on array performed in parallel (SIMD, data-parallel) 0 Many array-based intrinsic operators and functions

‹ Examples

real, dimension(100, 100) :: x, y, z z = x + y! z(i, j) = x(i, j) + y(i, j) x = 1.0! x(i, j) = 1. b = x .eq. y! if x(i, j) == y(i, j) then b(i, j) = .true. else b(i, j) = .false. where (x>1) y=1! if x(i, j) >1 then y(i, j) = 1 i = sum (x)! i = scalar sum of all elements in x

High Performance Fortran (HPF)

‹ Characteristics

0 Both local & shared memory 0 Fortran 90 + data distribution directives 0 Parallelism : data-parallelism, directives for loops, functions 0 Compilers generate explicit communication

‹ Example

REAL X(N,N), Y(N,N)

!HPF$ DISTRIBUTE X(BLOCK, *)

!HPF$ ALIGN (: , :) WITH X(: , :) :: Y

FORALL (I = 1,N-1, J = 1,N-1) &

Y(I,J) = 0.25 * (X(I-1,J-1) + X(I-1,J+1) + X(I+1,J-1) + X(I+1,J+1))

X = Y

CMSC 838T – Lecture 8

Parallel Programming Overview

‹ Motivation

‹ Shared memory paradigms

0 Explicit threads 0 Implicit threads

‹ Distributed memory paradigms

0 Explicit messages 0 Implicit messages (special nonlocal accesses)

‹ Comparisons

‹ Case study

0 OpenMP 0 MPI

Co-Array Fortran

‹ Characteristics

0 Local memory, shared global arrays 0 Parallelism : single program on multiple nodes (SPMD) 0 Provides illusion of shared multidimensional arrays z Extra array dimension specifying processor location 0 Simple approach to specifying non-local accesses 0 User responsible for specifying processor

‹ Example

REAL, DIMENSION (N) [*] :: X, Y

X(:) = Y(:) [ MOD(THIS_IMAGE + 1, NUM_IMAGES) ]

CMSC 838T – Lecture 8

Global Arrays

‹ Characteristics

0 Local memory, shared global arrays 0 Parallelism : single program on multiple nodes (SPMD) 0 Provides illusion of shared multidimensional arrays 0 Library routines z Copy rectangular shaped data in & out of global arrays z Scatter / gather / accumulate operations on global array 0 Designed to be more restrictive, easier to use than MPI

‹ Example

NGA_Access(g_a, lo, hi, &table, &ld); for (j = 0; j < PROCS; j++) { for (i = 0; i < counts[j]; i++) { table[index-lo[0]] ^= stable[copy[i] >> (64-LSTSIZE)]; } } NGA_Release_update(g_a, lo, hi);

UPC

‹ Characteristics

0 Local memory, shared arrays accessed by global pointers 0 Parallelism : single program on multiple nodes (SPMD) 0 Provides illusion of shared one-dimensional arrays 0 Features z Data distribution declarations for arrays z Cast global pointers to local pointers for efficiency z One-sided communication routines (memput / memget) 0 Compilers translate global pointers, generate communication

‹ Example

shared int *x, *y, z[100]; upc_forall (i = 0; i < 100; j++) { z[i] = *x++ × *y++; }

CMSC 838T – Lecture 8

Comparison – Portability & Performance

‹ Shared memory

0 Portability – poor to medium 0 Performance – poor to excellent (depending on architecture)

‹ Shared memory with explicit threads

0 Portability – poor 0 Performance – excellent

‹ Distributed memory with special global accesses

0 Portability – moderate 0 Performance – poor to medium (depending on application)

‹ Distributed memory with explicit communication

0 Portability – excellent 0 Performance – excellent

Comparison – Sources of Error

‹ Shared memory with (implicit or explicit) threads

0 Data races (very difficult to find) z Variable read / written at wrong time z Output depends on thread execution order z Example X = 1; Y = X; (value of Y varies) Y = X; X = 2; 0 Deadlock z Threads frozen waiting for lock synchronization

‹ Distributed memory with explicit communication

0 Buffer overflow z Too many messages received by thread z Unable to buffer / process messages exceeding limit z Thread freezes waiting for dropped messages

CMSC 838T – Lecture 8

Parallel Programming Overview

‹ Motivation

‹ Shared memory paradigms

0 Explicit threads 0 Implicit threads

‹ Distributed memory paradigms

0 Explicit messages 0 Implicit messages (special nonlocal accesses)

‹ Comparisons

‹ Case study

0 OpenMP 0 MPI

Calculating π – Sequential Program

‹ Compute an approximation to π

0 Using numerical integration 0 Find the area under the curve 4/(1+x^2 ) between 0 and 1

= π ∫ (^) + dx x

1 0 1 2

4

CMSC 838T – Lecture 8

OpenMP – Programming Model

‹ Fork-join parallelism (restricted form of MIMD)

0 Normally single thread of control (master) 0 Worker threads spawned when parallel region encountered 0 Barrier synchronization required at end of parallel region

Master Thread

Parallel Regions

OpenMP – Example Parallel Region

double a[1000]; omp_set_num_threads(4); #pragma omp parallel { int id = omp_thread_num(); foo(id,a); } printf(“all done \n”);

double a[1000];

#pragma omp parallel

foo(3,a);

printf(“all done \n”);

foo(0,a); foo(1,a); foo(2,a);

omp_set_num_threads(4);

‹ Task level parallelism – #pragma omp parallel { … }

OpenMP compiler

CMSC 838T – Lecture 8

OpenMP – Example Parallel Loop

#pragma omp parallel { int id, i, nthreads,start, end; id = omp_get_thread_num(); nthreads = omp_get_num_threads(); start = id * N / nthreads ; // assigning end = (id+1) * N / nthreads ; // work for (i=start; i<end; i++) { foo(i); } }

#pragma omp parallel for for (i=0;i<N;i++) { foo(i); }

‹ Loop level parallelism – #pragma omp parallel for

  • Loop iterations are assigned to threads, invoked as functions

OpenMP compiler

Parallel Programming Overview

‹ Motivation

‹ Shared memory paradigms

0 Explicit threads 0 Implicit threads

‹ Distributed memory paradigms

0 Explicit messages 0 Implicit messages (special nonlocal accesses)

‹ Comparisons

‹ Case study

0 OpenMP 0 MPI

CMSC 838T – Lecture 8

MPI Language Binding – C and Fortran

‹ MPI is language independent

0 Has “bindings” for C, Fortran, other languages

‹ In C:

0 mpi.h must be included 0 MPI functions return error codes or MPI_SUCCESS

‹ In Fortran:

0 mpif.h must be included 0 All MPI calls are subroutines, with a place for the return code in the last argument.

‹ C++ bindings, and Fortran-90 issues, are part of MPI-

SPMD Programming Model

‹ SPMD (Single Program Multiple Data) model

0 Each processor has a copy of the same program 0 All run them at their own rate 0 May take different paths through the code

‹ Process-specific control through variables like

0 My process number 0 Total number of processors

‹ Processors may synchronize explicitly

0 Call synchronization library functions

CMSC 838T – Lecture 8

SPMD Programming Model

Processor #

Instructions

Data

Processor #n

Instructions

Data

Program

Processor #

Instructions

Data

my_id=GetID(); If (my_id == 1) Execute Task 1; If (my_id == 2) Execute Task 2; … If (my_id == n) Execute Task n; Compile

SPMD Programming Model

‹ MPI processes differentiate themselves

0 Using MPI library calls

‹ Example

#include "mpi.h" #include <stdio.h>

int main( int argc, char *argv[] ) { int rank, size; MPI_Init( &argc, &argv ); MPI_Comm_rank( MPI_COMM_WORLD, &rank ); MPI_Comm_size( MPI_COMM_WORLD, &size ); printf("I am process %d of %d.\n", rank, size); MPI_Finalize(); return 0; }

CMSC 838T – Lecture 8

MPI Message Passing – Send

MPI_Send (start, count, datatype, dest, tag, comm)

0 start a pointer to the start of the data 0 count the number of elements to be sent 0 datatype the type of the data 0 dest the ID of the destination process 0 tag the tag on the message for matching 0 comm the communicator to be used

‹ Semantics (when function returns)

0 Data has been delivered to “system” z I.e., copied to system buffer 0 Data structure (start…start+count) can be reused 0 Destination process X may not have received message! z X stores data to system buffer until matching recv() called

MPI Message Passing – Receive

MPI_ Recv(start, count, datatype, src, tag, comm, status)

0 start a pointer to the start of the data destination 0 count the number of elements to be received 0 datatype the type of the data 0 src the ID of the source process 0 tag the tag on the message for matching 0 comm the communicator to be used 0 status the place to put status information

‹ Semantics (when function called)

0 Wait until send() call with matching src / tag is received 0 May freeze if matching message is not received!

‹ Semantics (when function returns)

0 Data from send() has been received and stored into start z I.e., copied from system buffer

CMSC 838T – Lecture 8

Describing Data – MPI Datatypes

‹ Data in MPI message is described by a triple

0 〈 address, count, datatype 〉

‹ An MPI datatype is recursively defined as:

0 Predefined, corresponding to a data type from the language (e.g., MPI_INT, MPI_DOUBLE) 0 A contiguous array of datatypes 0 A strided block of datatypes 0 An indexed array of blocks of datatypes 0 An arbitrary structure of datatypes

‹ Supports using MPI on heterogeneous architectures

MPI Example – Calculating π #include "mpi.h" #include <math.h> int main(int argc, char *argv[]) { int n, myid, numprocs, i; double mypi, pi, h, sum, x; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if (myid == 0) { printf("Enter the number of intervals:\n"); scanf("%d",&n); for(i=1;i<numprocs;i++) MPI_Send(&n,1,MPI_INT,i,0,MPI_COMM_WORLD); } else { MPI_Recv(&n,1,MPI_INT,0,0,MPI_COMM_WORLD,&status); }