26
An Introduction to An Introduction to Parallel Programming Parallel Programming with MPI with MPI February 17, 19, 24, 26 February 17, 19, 24, 26 2004 2004 David Adams David Adams http:// http:// research.cs.vt.edu/lasca/schedule research.cs.vt.edu/lasca/schedule

An Introduction to Parallel Programming with MPI

  • Upload
    irina

  • View
    32

  • Download
    2

Embed Size (px)

DESCRIPTION

An Introduction to Parallel Programming with MPI. February 17, 19, 24, 26 2004 David Adams http://research.cs.vt.edu/lasca/schedule. Creating Accounts. https://admin.cs.vt.edu/cslab/index.pl. Outline. Disclaimers Overview of basic parallel programming on a cluster with the goals of MPI - PowerPoint PPT Presentation

Citation preview

Page 1: An Introduction to Parallel Programming with MPI

An Introduction to Parallel An Introduction to Parallel Programming with MPIProgramming with MPI

February 17, 19, 24, 26February 17, 19, 24, 26 20042004

David AdamsDavid Adamshttp://http://research.cs.vt.edu/lasca/scheduleresearch.cs.vt.edu/lasca/schedule

Page 2: An Introduction to Parallel Programming with MPI

Creating AccountsCreating Accounts

https://https://admin.cs.vt.edu/cslab/index.pladmin.cs.vt.edu/cslab/index.pl

Page 3: An Introduction to Parallel Programming with MPI

OutlineOutline DisclaimersDisclaimers Overview of basic parallel programming on a cluster Overview of basic parallel programming on a cluster

with the goals of MPIwith the goals of MPI Batch system interactionBatch system interaction Startup proceduresStartup procedures Quick reviewQuick review Blocking message passingBlocking message passing

Non-blocking message passingNon-blocking message passing Lab dayLab day

Collective communicationsCollective communications

Page 4: An Introduction to Parallel Programming with MPI

ReviewReview

Functions we have covered in detail:Functions we have covered in detail:MPI_INITMPI_INIT MPI_FINALIZEMPI_FINALIZEMPI_COMM_SIZE MPI_COMM_SIZE MPI_COMM_RANKMPI_COMM_RANKMPI_SENDMPI_SEND MPI_RECVMPI_RECV

Useful constants:Useful constants:MPI_COMM_WORLD MPI_COMM_WORLD MPI_ANY_SOURCEMPI_ANY_SOURCEMPI_ANY_TAGMPI_ANY_TAG MPI_SUCCESSMPI_SUCCESS

Page 5: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

SENDRECV

RECVSEND

RECVSEND

Page 6: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 1

Page 7: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 2

Page 8: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 3

Page 9: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 4

Page 10: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 5

Page 11: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 6

Page 12: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 7

Page 13: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 8

Page 14: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 9

Page 15: An Introduction to Parallel Programming with MPI

Motivating Example for DeadlockMotivating Example for Deadlock

P10

P2P1

P9

P8

P7 P6

P5

P4

P3

Timestep: 10!

Page 16: An Introduction to Parallel Programming with MPI

SolutionSolution

MPI_SENDRECV(sendbuf, sendcount, MPI_SENDRECV(sendbuf, sendcount, sendtype, dest, sendtag, recvbuf, sendtype, dest, sendtag, recvbuf, recvcount, recvtype, source, recvtag, recvcount, recvtype, source, recvtag, comm, status, ierror)comm, status, ierror)

The semantics of a send-receive operation is The semantics of a send-receive operation is what would be obtained if the caller forked two what would be obtained if the caller forked two concurrent threads, one to execute the send, concurrent threads, one to execute the send, and one to execute the receive, followed by a and one to execute the receive, followed by a join of these two threads.join of these two threads.

Page 17: An Introduction to Parallel Programming with MPI

Nonblocking Message PassingNonblocking Message Passing

Allows for the overlap of communication Allows for the overlap of communication and computation.and computation.Completion of a message is broken into Completion of a message is broken into four steps instead of two.four steps instead of two. post-sendpost-send complete-sendcomplete-send post-receivepost-receive complete-receivecomplete-receive

Page 18: An Introduction to Parallel Programming with MPI

Posting OperationsPosting Operations

MPI_ISEND (BUF, COUNT, DATATYPE, DEST, TAG, MPI_ISEND (BUF, COUNT, DATATYPE, DEST, TAG, COMM, REQUEST, IERROR)COMM, REQUEST, IERROR)

IN <type> BUF(*)IN <type> BUF(*) IN INTEGER, COUNT, DATATYPE, DEST, TAG, COMM,IN INTEGER, COUNT, DATATYPE, DEST, TAG, COMM, OUT IERROR, REQUESTOUT IERROR, REQUEST

MPI_IRECV (BUF, COUNT, DATATYPE, SOURCE, MPI_IRECV (BUF, COUNT, DATATYPE, SOURCE, TAG, COMM, REQUEST, IERROR)TAG, COMM, REQUEST, IERROR)

IN <type> BUF(*)IN <type> BUF(*) IN INTEGER, COUNT, DATATYPE, SOURCE, TAG, COMM,IN INTEGER, COUNT, DATATYPE, SOURCE, TAG, COMM, OUT IERROR, REQUESTOUT IERROR, REQUEST

Page 19: An Introduction to Parallel Programming with MPI

Request ObjectsRequest ObjectsAll nonblocking communications use request All nonblocking communications use request objects to identify communication operations and objects to identify communication operations and link the posting operation with the completion link the posting operation with the completion operation.operation.Conceptually, they can be thought of as a Conceptually, they can be thought of as a pointer to a specific message instance floating pointer to a specific message instance floating around in MPI space.around in MPI space.Just as in pointers, request handles must be Just as in pointers, request handles must be treated with care or you can create request treated with care or you can create request handle leaks (like a memory leak) and handle leaks (like a memory leak) and completely lose access to the status of a completely lose access to the status of a message.message.

Page 20: An Introduction to Parallel Programming with MPI

Request ObjectsRequest Objects

The value MPI_REQUEST_NULL is used to The value MPI_REQUEST_NULL is used to indicate an invalid request handle. Operations indicate an invalid request handle. Operations that deallocate request objects set the request that deallocate request objects set the request handle to this value.handle to this value.

Posting operations allocate memory for request Posting operations allocate memory for request objects and completion operations deallocate objects and completion operations deallocate that memory and clean up the space.that memory and clean up the space.

Page 21: An Introduction to Parallel Programming with MPI

Completion OperationsCompletion OperationsMPI_WAIT(REQUEST, STATUS, IERROR)MPI_WAIT(REQUEST, STATUS, IERROR)

INOUT INTEGER REQUESTINOUT INTEGER REQUEST OUT STATUS, IERROROUT STATUS, IERROR

A call to MPI_WAIT returns when the operation identified by A call to MPI_WAIT returns when the operation identified by REQUEST is complete.REQUEST is complete.MPI_WAIT is the blocking version of completion operations where MPI_WAIT is the blocking version of completion operations where the program has determined it can’t do any more useful work the program has determined it can’t do any more useful work without completing the current message. In this case, it chooses to without completing the current message. In this case, it chooses to block until the corresponding send or receive completes.block until the corresponding send or receive completes.In iterative parallel code, it is often the case that an MPI_WAIT is In iterative parallel code, it is often the case that an MPI_WAIT is placed directly before the next post operation that intends to use the placed directly before the next post operation that intends to use the same request object variable.same request object variable.Successful completion of the function MPI_WAIT will set Successful completion of the function MPI_WAIT will set REQUEST=MPI_REQUEST_NULL.REQUEST=MPI_REQUEST_NULL.

Page 22: An Introduction to Parallel Programming with MPI

Completion OperationsCompletion OperationsMPI_TEST(REQUEST, FLAG, STATUS, IERROR)MPI_TEST(REQUEST, FLAG, STATUS, IERROR)

INOUT INTEGER REQUESTINOUT INTEGER REQUEST OUT STATUS(MPI_STATUS_SIZE)OUT STATUS(MPI_STATUS_SIZE) OUT LOGICAL FLAGOUT LOGICAL FLAG

A call to MPI_TEST returns flag=true if the operation identified by A call to MPI_TEST returns flag=true if the operation identified by REQUEST is complete.REQUEST is complete.MPI_TEST is the nonblocking version of completion operations.MPI_TEST is the nonblocking version of completion operations.If flag=true then MPI_TEST will clean up the space associated with If flag=true then MPI_TEST will clean up the space associated with REQUEST, deallocating the memory and setting REQUEST = REQUEST, deallocating the memory and setting REQUEST = MPI_REQUEST_NULL.MPI_REQUEST_NULL.MPI_TEST allows the user to create code that can attempt to MPI_TEST allows the user to create code that can attempt to communicate as much as possible but continue doing useful work if communicate as much as possible but continue doing useful work if messages are not ready.messages are not ready.

Page 23: An Introduction to Parallel Programming with MPI

Maximizing OverlapMaximizing OverlapTo achieve maximum overlap between computation and To achieve maximum overlap between computation and communication, communications should be started as communication, communications should be started as soon as possible and completed as late as possible. soon as possible and completed as late as possible.

Sends should be posted as soon as the data to be sent is Sends should be posted as soon as the data to be sent is available.available.

Receives should be posted as soon as the receive buffer can be Receives should be posted as soon as the receive buffer can be used.used.

Sends should be completed just before the send buffer is to be Sends should be completed just before the send buffer is to be reused.reused.

Receives should be completed just before the data in the buffer Receives should be completed just before the data in the buffer is to be reused.is to be reused.

Overlap can often be increased by reordering the Overlap can often be increased by reordering the computation.computation.

Page 24: An Introduction to Parallel Programming with MPI

Setting up your account for MPISetting up your account for MPI

http://courses.cs.vt.edu/~cs4234/MPI/first_http://courses.cs.vt.edu/~cs4234/MPI/first_exercise.htmlexercise.htmlList of all MCB 124 machines List of all MCB 124 machines http://www.cslab.vt.edu/124.shtmlhttp://www.cslab.vt.edu/124.shtml

Page 25: An Introduction to Parallel Programming with MPI

More StuffMore Stuffhttp://courses.cs.vt.edu/~cs4234/MPI/first_exercise.htmlhttp://courses.cs.vt.edu/~cs4234/MPI/first_exercise.html

Put /home/grads/raghavgn/mpich-1.2.5/bin in your path. Put /home/grads/raghavgn/mpich-1.2.5/bin in your path. ––open file .bash_profile and append :/home/grads/raghavgn/mpich-open file .bash_profile and append :/home/grads/raghavgn/mpich-1.2.5/bin1.2.5/bin

PATH=$PATH:$HOME/binPATH=$PATH:$HOME/bin PATH=$PATH:$HOME/bin:/home/grads/raghavgn/mpich-1.2.5/PATH=$PATH:$HOME/bin:/home/grads/raghavgn/mpich-1.2.5/

binbin

Make a subdirectory, mkdir MPI, and cd to it. Make a subdirectory, mkdir MPI, and cd to it. cp -r /home/grads/daadams3/MPI .cp -r /home/grads/daadams3/MPI .

Page 26: An Introduction to Parallel Programming with MPI

Compilation and ExecutionCompilation and Execution

Two folders, one for C and one for FORTRAN77Two folders, one for C and one for FORTRAN77 Hello world example Hello world example For C:For C:

Compile and link: mpicc -o hello hello.c Compile and link: mpicc -o hello hello.c Run on 4 processors: mpirun -np 4 –Run on 4 processors: mpirun -np 4 –machinefile ../mymachines hellomachinefile ../mymachines hello

For FortranFor FortranCompile and link: mpif77 –o hello hello.fCompile and link: mpif77 –o hello hello.fRun on 4 processors: mpirun -np 4 hello –Run on 4 processors: mpirun -np 4 hello –machinefile ../mymachines hellomachinefile ../mymachines hello