


















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



















CMSC 838T – Lecture 8
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
0 Automatic parallelization of sequential programs is difficult 0 Easier to write programs to explicitly exploit parallelism
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
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
0 Pthreads, Java threads
0 Data parallel (SIMD) – Fortran 90, HPF 0 Parallel loops / tasks (MIMD) – OpenMP, UPC
0 MPI, SHMEM, Distributed Java
0 Co-Array Fortran, Global Arrays, UPC
CMSC 838T – Lecture 8
Parallel Programming Overview
0 Explicit threads 0 Implicit threads
0 Explicit messages 0 Implicit messages (special nonlocal accesses)
0 OpenMP 0 MPI
OpenMP
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
#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
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
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)
0 Both local & shared memory 0 Fortran 90 + data distribution directives 0 Parallelism : data-parallelism, directives for loops, functions 0 Compilers generate explicit communication
CMSC 838T – Lecture 8
Parallel Programming Overview
0 Explicit threads 0 Implicit threads
0 Explicit messages 0 Implicit messages (special nonlocal accesses)
0 OpenMP 0 MPI
Co-Array Fortran
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
CMSC 838T – Lecture 8
Global Arrays
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
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
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
shared int *x, *y, z[100]; upc_forall (i = 0; i < 100; j++) { z[i] = *x++ × *y++; }
CMSC 838T – Lecture 8
Comparison – Portability & Performance
0 Portability – poor to medium 0 Performance – poor to excellent (depending on architecture)
0 Portability – poor 0 Performance – excellent
0 Portability – moderate 0 Performance – poor to medium (depending on application)
0 Portability – excellent 0 Performance – excellent
Comparison – Sources of Error
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
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
0 Explicit threads 0 Implicit threads
0 Explicit messages 0 Implicit messages (special nonlocal accesses)
0 OpenMP 0 MPI
Calculating π – Sequential Program
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
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);
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); }
OpenMP compiler
Parallel Programming Overview
0 Explicit threads 0 Implicit threads
0 Explicit messages 0 Implicit messages (special nonlocal accesses)
0 OpenMP 0 MPI
CMSC 838T – Lecture 8
MPI Language Binding – C and Fortran
0 Has “bindings” for C, Fortran, other languages
0 mpi.h must be included 0 MPI functions return error codes or MPI_SUCCESS
0 mpif.h must be included 0 All MPI calls are subroutines, with a place for the return code in the last argument.
SPMD Programming 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
0 My process number 0 Total number of processors
0 Call synchronization library functions
CMSC 838T – Lecture 8
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
0 Using MPI library calls
#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
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
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
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
0 Wait until send() call with matching src / tag is received 0 May freeze if matching message is not received!
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
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
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); }