An Introduction to Parallel Programming Peter Pacheco Chapter

  • Slides: 22
Download presentation
An Introduction to Parallel Programming Peter Pacheco Chapter 3 Distributed Memory Programming with MPI

An Introduction to Parallel Programming Peter Pacheco Chapter 3 Distributed Memory Programming with MPI Copyright © 2010, Elsevier Inc. All rights Reserved 1

n n Writing your first MPI program. Using the common MPI functions. Copyright ©

n n Writing your first MPI program. Using the common MPI functions. Copyright © 2010, Elsevier Inc. All rights Reserved # Chapter Subtitle Roadmap 2

A distributed memory system Copyright © 2010, Elsevier Inc. All rights Reserved 3

A distributed memory system Copyright © 2010, Elsevier Inc. All rights Reserved 3

A shared memory system Copyright © 2010, Elsevier Inc. All rights Reserved 4

A shared memory system Copyright © 2010, Elsevier Inc. All rights Reserved 4

Hello World! (a classic) Copyright © 2010, Elsevier Inc. All rights Reserved 5

Hello World! (a classic) Copyright © 2010, Elsevier Inc. All rights Reserved 5

Identifying MPI processes n n Common practice to identify processes by nonnegative integer ranks.

Identifying MPI processes n n Common practice to identify processes by nonnegative integer ranks. p processes are numbered 0, 1, 2, . . p-1 Copyright © 2010, Elsevier Inc. All rights Reserved 6

Compilation wrapper script to compile source file mpicc -g -Wall -o mpi_hello. c produce

Compilation wrapper script to compile source file mpicc -g -Wall -o mpi_hello. c produce debugging information create this executable file name (as opposed to default a. out) turns on all warnings Copyright © 2010, Elsevier Inc. All rights Reserved 7

Execution mpiexec -n <number of processes> <executable> mpiexec -n 1. /mpi_hello run with 1

Execution mpiexec -n <number of processes> <executable> mpiexec -n 1. /mpi_hello run with 1 process mpiexec -n 4. /mpi_hello run with 4 processes Copyright © 2010, Elsevier Inc. All rights Reserved 8

Execution mpiexec -n 1. /mpi_hello Greetings from process 0 of 1 ! mpiexec -n

Execution mpiexec -n 1. /mpi_hello Greetings from process 0 of 1 ! mpiexec -n 4. /mpi_hello Greetings from process 0 of 4 ! Greetings from process 1 of 4 ! Greetings from process 2 of 4 ! Greetings from process 3 of 4 ! Copyright © 2010, Elsevier Inc. All rights Reserved 9

MPI Programs n Written in C. n n n Has main. Uses stdio. h,

MPI Programs n Written in C. n n n Has main. Uses stdio. h, string. h, etc. Need to add mpi. h header file. Identifiers defined by MPI start with “MPI_”. First letter following underscore is uppercase. n n For function names and MPI-defined types. Helps to avoid confusion. Copyright © 2010, Elsevier Inc. All rights Reserved 10

MPI Components n MPI_Init n n Tells MPI to do all the necessary setup.

MPI Components n MPI_Init n n Tells MPI to do all the necessary setup. MPI_Finalize n Tells MPI we’re done, so clean up anything allocated for this program. Copyright © 2010, Elsevier Inc. All rights Reserved 11

Basic Outline Copyright © 2010, Elsevier Inc. All rights Reserved 12

Basic Outline Copyright © 2010, Elsevier Inc. All rights Reserved 12

Communicators n A collection of processes that can send messages to each other. MPI_Init

Communicators n A collection of processes that can send messages to each other. MPI_Init defines a communicator that consists of all the processes created when the program is started. n Called MPI_COMM_WORLD. n Copyright © 2010, Elsevier Inc. All rights Reserved 13

SPMD n n n Single-Program Multiple-Data We compile one program. Process 0 does something

SPMD n n n Single-Program Multiple-Data We compile one program. Process 0 does something different. n n Receives messages and prints them while the other processes do the work. The if-else construct makes our program SPMD. Copyright © 2010, Elsevier Inc. All rights Reserved 14

Communication Copyright © 2010, Elsevier Inc. All rights Reserved 15

Communication Copyright © 2010, Elsevier Inc. All rights Reserved 15

Data types Copyright © 2010, Elsevier Inc. All rights Reserved 16

Data types Copyright © 2010, Elsevier Inc. All rights Reserved 16

Communication Copyright © 2010, Elsevier Inc. All rights Reserved 17

Communication Copyright © 2010, Elsevier Inc. All rights Reserved 17

Message matching r MPI_Send src = q MPI_Recv dest = r q Copyright ©

Message matching r MPI_Send src = q MPI_Recv dest = r q Copyright © 2010, Elsevier Inc. All rights Reserved 18

Receiving messages n A receiver can get a message without knowing: n n n

Receiving messages n A receiver can get a message without knowing: n n n the amount of data in the message, the sender of the message, or the tag of the message. Copyright © 2010, Elsevier Inc. All rights Reserved 19

status_p argument MPI_Status* status; status. MPI_SOURCE status. MPI_TAG MPI_SOURCE MPI_TAG MPI_ERROR Copyright © 2010,

status_p argument MPI_Status* status; status. MPI_SOURCE status. MPI_TAG MPI_SOURCE MPI_TAG MPI_ERROR Copyright © 2010, Elsevier Inc. All rights Reserved 20

How much data am I receiving? Copyright © 2010, Elsevier Inc. All rights Reserved

How much data am I receiving? Copyright © 2010, Elsevier Inc. All rights Reserved 21

Issues with send and receive n n n Exact behavior is determined by the

Issues with send and receive n n n Exact behavior is determined by the MPI implementation. MPI_Send may behave differently with regard to buffer size, cutoffs and blocking. MPI_Recv always blocks until a matching message is received. Copyright © 2010, Elsevier Inc. All rights Reserved 22