Distributed Systems CS 15-440 Programming Models- Part II Lecture 14, Oct 28, 2013 Mohammad Hammoud 1.

Slides:



Advertisements
Similar presentations
Parallel Processing1 Parallel Processing (CS 667) Lecture 9: Advanced Point to Point Communication Jeremy R. Johnson *Parts of this lecture was derived.
Advertisements

Its.unc.edu 1 Collective Communication University of North Carolina - Chapel Hill ITS - Research Computing Instructor: Mark Reed
MPI Collective Communications
Sahalu Junaidu ICS 573: High Performance Computing 8.1 Topic Overview Matrix-Matrix Multiplication Block Matrix Operations A Simple Parallel Matrix-Matrix.
A Message Passing Standard for MPP and Workstations Communications of the ACM, July 1996 J.J. Dongarra, S.W. Otto, M. Snir, and D.W. Walker.
Point-to-Point Communication Self Test with solution.
SOME BASIC MPI ROUTINES With formal datatypes specified.
Distributed Memory Programming with MPI. What is MPI? Message Passing Interface (MPI) is an industry standard message passing system designed to be both.
MPI Point-to-Point Communication CS 524 – High-Performance Computing.
Collective Communication.  Collective communication is defined as communication that involves a group of processes  More restrictive than point to point.
Distributed Systems CS Programming Models- Part II Lecture 17, Nov 2, 2011 Majd F. Sakr, Mohammad Hammoud andVinay Kolar 1.
1 Tuesday, October 10, 2006 To err is human, and to blame it on a computer is even more so. -Robert Orben.
Distributed Systems CS Programming Models- Part I Lecture 16, Oct 31, 2011 Majd F. Sakr, Mohammad Hammoud andVinay Kolar 1.
A Brief Look At MPI’s Point To Point Communication Brian T. Smith Professor, Department of Computer Science Director, Albuquerque High Performance Computing.
Today Objectives Chapter 6 of Quinn Creating 2-D arrays Thinking about “grain size” Introducing point-to-point communications Reading and printing 2-D.
Basics of Message-passing Mechanics of message-passing –A means of creating separate processes on different computers –A way to send and receive messages.
Mapping Techniques for Load Balancing
Parallel Programming with Java
CS 179: GPU Programming Lecture 20: Cross-system communication.
A Message Passing Standard for MPP and Workstations Communications of the ACM, July 1996 J.J. Dongarra, S.W. Otto, M. Snir, and D.W. Walker.
L15: Putting it together: N-body (Ch. 6) October 30, 2012.
Parallel Processing1 Parallel Processing (CS 676) Lecture 7: Message Passing using MPI * Jeremy R. Johnson *Parts of this lecture was derived from chapters.
2a.1 Message-Passing Computing More MPI routines: Collective routines Synchronous routines Non-blocking routines ITCS 4/5145 Parallel Computing, UNC-Charlotte,
1 MPI: Message-Passing Interface Chapter 2. 2 MPI - (Message Passing Interface) Message passing library standard (MPI) is developed by group of academics.
Specialized Sending and Receiving David Monismith CS599 Based upon notes from Chapter 3 of the MPI 3.0 Standard
PP Lab MPI programming VI. Program 1 Break up a long vector into subvectors of equal length. Distribute subvectors to processes. Let them compute the.
Parallel Computing A task is broken down into tasks, performed by separate workers or processes Processes interact by exchanging information What do we.
Parallel Programming with MPI Prof. Sivarama Dandamudi School of Computer Science Carleton University.
CS 838: Pervasive Parallelism Introduction to MPI Copyright 2005 Mark D. Hill University of Wisconsin-Madison Slides are derived from an online tutorial.
MPI Communications Point to Point Collective Communication Data Packaging.
Message Passing Programming Model AMANO, Hideharu Textbook pp. 140-147.
Summary of MPI commands Luis Basurto. Large scale systems Shared Memory systems – Memory is shared among processors Distributed memory systems – Each.
1 Overview on Send And Receive routines in MPI Kamyar Miremadi November 2004.
MPI (continue) An example for designing explicit message passing programs Advanced MPI concepts.
Distributed Systems CS /640 Programming Models Borrowed and adapted from our good friends at CMU-Doha, Qatar Majd F. Sakr, Mohammad Hammoud andVinay.
Parallel Programming with MPI By, Santosh K Jena..
CSCI-455/522 Introduction to High Performance Computing Lecture 4.
1 Message Passing Models CEG 4131 Computer Architecture III Miodrag Bolic.
Distributed Systems CS Programming Models Gregory Kesden Borrowed and adapted from our good friends at CMU-Doha, Qatar Majd F. Sakr, Mohammad Hammoud.
Message-Passing Computing Chapter 2. Programming Multicomputer Design special parallel programming language –Occam Extend existing language to handle.
MPI Point to Point Communication CDP 1. Message Passing Definitions Application buffer Holds the data for send or receive Handled by the user System buffer.
1 BİL 542 Parallel Computing. 2 Message Passing Chapter 2.
Introduction to Parallel Programming at MCSR Message Passing Computing –Processes coordinate and communicate results via calls to message passing library.
Message Passing Interface (MPI) 2 Amit Majumdar Scientific Computing Applications Group San Diego Supercomputer Center Tim Kaiser (now at Colorado School.
-1.1- MPI Lectured by: Nguyễn Đức Thái Prepared by: Thoại Nam.
Parallel Algorithms & Implementations: Data-Parallelism, Asynchronous Communication and Master/Worker Paradigm FDI 2007 Track Q Day 2 – Morning Session.
Message Passing Programming Based on MPI Collective Communication I Bora AKAYDIN
Message Passing Interface Using resources from
MPI-Message Passing Interface. What is MPI?  MPI is a specification for the developers and users of message passing libraries. By itself, it is NOT a.
COMP7330/7336 Advanced Parallel and Distributed Computing MPI Programming: 1. Collective Operations 2. Overlapping Communication with Computation Dr. Xiao.
MPI_Alltoall By: Jason Michalske. What is MPI_Alltoall? Each process sends distinct data to each receiver. The Jth block of process I is received by process.
3/12/2013Computer Engg, IIT(BHU)1 MPI-2. POINT-TO-POINT COMMUNICATION Communication between 2 and only 2 processes. One sending and one receiving. Types:
Introduction to MPI Programming Ganesh C.N.
CS4402 – Parallel Computing
MPI Point to Point Communication
Distributed Systems CS
An Introduction to Parallel Programming with MPI
Collective Communication Operations
Distributed Systems CS
More on MPI Nonblocking point-to-point routines Deadlock
Distributed Systems CS
Lecture 14: Inter-process Communication
A Message Passing Standard for MPP and Workstations
Distributed Systems CS
More on MPI Nonblocking point-to-point routines Deadlock
Barriers implementations
Hardware Environment VIA cluster - 8 nodes Blade Server – 5 nodes
September 4, 1997 Parallel Processing (CS 667) Lecture 9: Advanced Point to Point Communication Jeremy R. Johnson *Parts of this lecture was derived.
5- Message-Passing Programming
September 4, 1997 Parallel Processing (CS 730) Lecture 9: Advanced Point to Point Communication Jeremy R. Johnson *Parts of this lecture was derived.
Presentation transcript:

Distributed Systems CS Programming Models- Part II Lecture 14, Oct 28, 2013 Mohammad Hammoud 1

Today…  Last Session  Consistency and Replication- Part IV  Programming Models – Part I  Today’s Session  Programming Models – Part II  Announcements:  Problem Solving Assignment 2 (PS2) grades are out  PS3 will be posted by tonight. Due date: Monday Nov 13, 2013  Project 3 is due on Wednesday Nov 8,

Objectives Discussion on Programming Models Why parallelizing our programs? Parallel computer architectures Traditional Models of parallel programming Types of Parallel Program Message Passing Interface (MPI) MapReduce, Pregel and GraphLab Types of Parallel Program

SPMD and MPMD  When we run multiple processes with message-passing, there are further categorizations regarding how many different programs are cooperating in parallel execution  We distinguish between two models: 1.Single Program Multiple Data (SPMD) model 2.Multiple Programs Multiple Data (MPMD) model 4

SPMD  In the SPMD model, there is only one program and each process uses the same executable working on different sets of data 5 a.out Node 1 Node 2 Node 3

MPMD  The MPMD model uses different programs for different processes, but the processes collaborate to solve the same problem  MPMD has two styles, the master/worker and the coupled analysis a.out Node 1Node 2 Node 3 b.out a.out Node 1 b.out Node 2 c.out Node 3 1. MPMD: Master/Slave 2. MPMD: Coupled Analysis a.out= Structural Analysis, b.out = fluid analysis and c.out = thermal analysis Example

An Example A Sequential Program 1.Read array a() from the input file 2.Set is=1 and ie=6 //is = index start and ie = index end 3.Process from a(is) to a(ie) 4.Write array a() to the output file a a a isie  Colored shapes indicate the initial values of the elements  Black shapes indicate the values after they are processed

An Example Process 0 1.Read array a() from the input file 2.Get my rank 3.If rank==0 then is=1, ie=2 If rank==1 then is=3, ie=4 If rank==2 then is=5, ie=6 4.Process from a(is) to a(ie) 5.Gather the results to process 0 6.If rank==0 then write array a() to the output file a a a isie Process 1 1.Read array a() from the input file 2.Get my rank 3.If rank==0 then is=1, ie=2 If rank==1 then is=3, ie=4 If rank==2 then is=5, ie=6 4.Process from a(is) to a(ie) 5.Gather the results to process 0 6.If rank==0 then write array a() to the output file a a isie Process 2 1.Read array a() from the input file 2.Get my rank 3.If rank==0 then is=1, ie=2 If rank==1 then is=3, ie=4 If rank==2 then is=5, ie=6 4.Process from a(is) to a(ie) 5.Gather the results to process 0 6.If rank==0 then write array a() to the output file a a isie Under which category does this program lie?

Concluding Remarks  To summarize, keep the following 3 points in mind:  The purpose of parallelization is to reduce the time spent on computation– A caveat, however, is that communication might increase as the cluster size is scaled up  Ideally, the parallel program is p times faster than the sequential program, where p is the number of processes involved in the parallel execution, but this is not always achievable  Message-passing is the tool to consolidate what parallelization has separated. It should not be regarded as the parallelization itself 9

Objectives Discussion on Programming Models Why parallelizing our programs? Parallel computer architectures Types of Parallel Programs Message Passing Interface (MPI) MapReduce, Pregel and GraphLab Traditional Models of parallel programming Message Passing Interface (MPI)

Message Passing Interface  In this part, the following concepts of MPI will be described:  Basics  Point-to-point communication  Collective communication 11

Message Passing Interface  In this part, the following concepts of MPI will be described:  Basics  Point-to-point communication  Collective communication 12

What is MPI?  The Message Passing Interface (MPI) is a message passing library standard for writing message passing programs  The goal of MPI is to establish a portable, efficient, and flexible standard for message passing  By itself, MPI is NOT a library - but rather the specification of what such a library should be  MPI is not an IEEE or ISO standard, but has in fact, become the industry standard for writing message passing programs on HPC platforms 13

Reasons for using MPI 14 ReasonDescription Standardization MPI is the only message passing library which can be considered a standard. It is supported on virtually all HPC platforms ReasonDescription Standardization MPI is the only message passing library which can be considered a standard. It is supported on virtually all HPC platforms Portability There is no need to modify your source code when you port your application to a different platform that supports the MPI standard ReasonDescription Standardization MPI is the only message passing library which can be considered a standard. It is supported on virtually all HPC platforms Portability There is no need to modify your source code when you port your application to a different platform that supports the MPI standard Performance Opportunities Vendor implementations should be able to exploit native hardware features to optimize performance ReasonDescription Standardization MPI is the only message passing library which can be considered a standard. It is supported on virtually all HPC platforms Portability There is no need to modify your source code when you port your application to a different platform that supports the MPI standard Performance Opportunities Vendor implementations should be able to exploit native hardware features to optimize performance Functionality Over 115 routines are defined ReasonDescription Standardization MPI is the only message passing library which can be considered a standard. It is supported on virtually all HPC platforms Portability There is no need to modify your source code when you port your application to a different platform that supports the MPI standard Performance Opportunities Vendor implementations should be able to exploit native hardware features to optimize performance Functionality Over 115 routines are defined Availability A variety of implementations are available, both vendor and public domain

The Programming Model  MPI is an example of a message passing programming model  MPI is now used on just about any common parallel architecture including MPP, SMP clusters, workstation clusters and heterogeneous networks  With MPI the programmer is responsible for correctly identifying parallelism and implementing parallel algorithms using MPI constructs 15

Communicators and Groups  MPI uses objects called communicators/groups to define which collection of processes may communicate with each other to solve a certain problem  Most MPI routines require you to specify a communicator as an argument  The communicator MPI_COMM_WORLD is often used in calling communication subroutines  MPI_COMM_WORLD is the predefined communicator that includes all of your MPI processes 16

Ranks  Within a communicator, every process has its own unique, integer identifier referred to as rank, assigned by the system when the process initializes  A rank is sometimes called a task ID. Ranks are contiguous and begin at zero  Ranks are used by the programmer to specify the source and destination of messages  Ranks are often also used conditionally by the application to control program execution (e.g., if rank=0 do this / if rank=1 do that) 17

Multiple Communicators  It is possible that a problem consists of several sub-problems where each can be solved concurrently  This type of application is typically found in the category of MPMD coupled analysis  We can create a new communicator for each sub-problem as a subset of an existing communicator  MPI allows you to achieve that by using MPI_COMM_SPLIT 18

Example of Multiple Communicators  Consider a problem with a fluid dynamics part and a structural analysis part, where each part can be computed in parallel Rank=0 Comm_Fluid Rank=1 Rank=2 Rank=3 Rank=0 Rank=4 Comm_Struct Rank=1 Rank=5 Rank=2 Rank=6 Rank=3 Rank=7 MPI_COMM_WORLD Ranks within MPI_COMM_WORLD are printed in red Ranks within Comm_Fluid are printed with green Ranks within Comm_Struct are printed with blue

Message Passing Interface  In this part, the following concepts of MPI will be described:  Basics  Point-to-point communication  Collective communication 20

Point-to-Point Communication  MPI point-to-point operations typically involve message passing between two, and only two, different MPI tasks  One task performs a send operation and the other performs a matching receive operation  Ideally, every send operation would be perfectly synchronized with its matching receive  This is rarely the case. Somehow or other, the MPI implementation must be able to deal with storing data when the two tasks are out of sync 21 Processor1Processor2 Network sendA recvA

Two Cases  Consider the following two cases: 1.A send operation occurs 5 seconds before the receive is ready - where is the message stored while the receive is pending? 2.Multiple sends arrive at the same receiving task which can only accept one send at a time - what happens to the messages that are "backing up"? 22

Steps Involved in Point-to-Point Communication 1.The data is copied to the user buffer by the user 2.The user calls one of the MPI send routines 3.The system copies the data from the user buffer to the system buffer 4.The system sends the data from the system buffer to the destination process 1.The user calls one of the MPI receive routines 2.The system receives the data from the source process and copies it to the system buffer 3.The system copies data from the system buffer to the user buffer 4.The user uses data in the user buffer sendbuf sysbuf Call a send routine Now sendbuf can be reused Copying data from sendbuf to sysbuf Send data from sysbuf to destination recvbuf sysbuf Call a recev routine Now recvbuf contains valid data Copying data from sysbuf to recvbuf Receive data from source to sysbuf Process 0 User ModeKernel Mode Process 1 User ModeKernel Mode Data Sender Receiver

Blocking Send and Receive  When we use point-to-point communication routines, we usually distinguish between blocking and non-blocking communication  A blocking send routine will only return after it is safe to modify the application buffer for reuse  Safe means that modifications will not affect the data intended for the receive task  This does not imply that the data was actually received by the receiver- it may be sitting in the system buffer at the sender side 24 Rank 0Rank 1 sendbuf recvbuf sendbuf Safe to modify sendbuf Network

Blocking Send and Receive  A blocking send can be: 1.Synchronous: Means there is a handshaking occurring with the receive task to confirm a safe send 2.Asynchronous: Means the system buffer at the sender side is used to hold the data for eventual delivery to the receiver  A blocking receive only returns after the data has arrived (i.e., stored at the application recvbuf) and is ready for use by the program 25

Non-Blocking Send and Receive (1)  Non-blocking send and non-blocking receive routines behave similarly  They return almost immediately  They do not wait for any communication events to complete such as:  Message copying from user buffer to system buffer  Or the actual arrival of a message 26

Non-Blocking Send and Receive (2)  However, it is unsafe to modify the application buffer until you make sure that the requested non-blocking operation was actually performed by the library  If you use the application buffer before the copy completes:  Incorrect data may be copied to the system buffer (in case of non-blocking send)  Or your receive buffer does not contain what you want (in case of non-blocking receive)  You can make sure of the completion of the copy by using MPI_WAIT() after the send or receive operations 27

Why Non-Blocking Communication?  Why do we use non-blocking communication despite its complexity?  Non-blocking communication is generally faster than its corresponding blocking communication  We can overlap computations while the system is copying data back and forth between application and system buffers 28

MPI Point-To-Point Communication Routines 29 RoutineSignature Blocking send int MPI_Send( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm ) Non-blocking send int MPI_Isend( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request ) Blocking receive int MPI_Recv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status ) Non-blocking receive int MPI_Irecv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Request *request ) RoutineSignature Blocking send int MPI_Send( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm ) Non-blocking send int MPI_Isend( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request ) Blocking receive int MPI_Recv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status ) Non-blocking receive int MPI_Irecv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Request *request ) RoutineSignature Blocking send int MPI_Send( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm ) Non-blocking send int MPI_Isend( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request ) Blocking receive int MPI_Recv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status ) Non-blocking receive int MPI_Irecv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Request *request ) RoutineSignature Blocking send int MPI_Send( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm ) Non-blocking send int MPI_Isend( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request ) Blocking receive int MPI_Recv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status ) Non-blocking receive int MPI_Irecv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Request *request ) RoutineSignature Blocking send int MPI_Send( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm ) Non-blocking send int MPI_Isend( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, MPI_Request *request ) Blocking receive int MPI_Recv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status ) Non-blocking receive int MPI_Irecv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Request *request )

Message Order  MPI guarantees that messages will not overtake each other  If a sender sends two messages M1 and M2 in succession to the same destination, and both match the same receive, the receive operation will receive M1 before M2  If a receiver posts two receives R1 and R2, in succession, and both are looking for the same message, R1 will receive the message before R2 30

Fairness  MPI does not guarantee fairness – it is up to the programmer to prevent operation starvation  For instance, if task 0 and task 1 send competing messages (i.e., messages that match the same receive) to task 2, only one of the sends will complete 31 Task 0 Msg A Task 1 Msg A Task 2 ?

Unidirectional Communication  When you send a message from process 0 to process 1, there are four combinations of MPI subroutines to choose from 1.Blocking send and blocking receive 2.Non-blocking send and blocking receive 3.Blocking send and non-blocking receive 4.Non-blocking send and non-blocking receive 32 Rank 0Rank 1 sendbuf recvbuf sendbuf

Bidirectional Communication  When two processes exchange data with each other, there are essentially 3 cases to consider:  Case 1: Both processes call the send routine first, and then receive  Case 2: Both processes call the receive routine first, and then send  Case 3: One process calls send and receive routines in this order, and the other calls them in the opposite order 33 Rank 0Rank 1 sendbuf recvbuf sendbuf

Bidirectional Communication- Deadlocks  With bidirectional communication, we have to be careful about deadlocks  When a deadlock occurs, processes involved in the deadlock will not proceed any further  Deadlocks can take place: 1.Either due to the incorrect order of send and receive 2.Or due to the limited size of the system buffer 34 Rank 0Rank 1 sendbuf recvbuf sendbuf

Case 1. Send First and Then Receive  Consider the following two snippets of pseudo-code:  MPI_ISEND immediately followed by MPI_WAIT is logically equivalent to MPI_SEND 35 IF (myrank==0) THEN CALL MPI_SEND(sendbuf, …) CALL MPI_RECV(recvbuf, …) ELSEIF (myrank==1) THEN CALL MPI_SEND(sendbuf, …) CALL MPI_RECV(recvbuf, …) ENDIF IF (myrank==0) THEN CALL MPI_ISEND(sendbuf, …, ireq, …) CALL MPI_WAIT(ireq, …) CALL MPI_RECV(recvbuf, …) ELSEIF (myrank==1) THEN CALL MPI_ISEND(sendbuf, …, ireq, …) CALL MPI_WAIT(ireq, …) CALL MPI_RECV(recvbuf, …) ENDIF

Case 1. Send First and Then Receive  What happens if the system buffer is larger than the send buffer?  What happens if the system buffer is smaller than the send buffer? Rank 0Rank 1 sendbuf sysbuf sendbuf sysbuf recvbuf Network Rank 0Rank 1 sendbuf sysbuf sendbuf sysbuf recvbuf Network DEADLOCK!

Case 1. Send First and Then Receive  Consider the following pseudo-code:  The code is free from deadlock because:  The program immediately returns from MPI_ISEND and starts receiving data from the other process  In the meantime, data transmission is completed and the calls of MPI_WAIT for the completion of send at both processes do not lead to a deadlock IF (myrank==0) THEN CALL MPI_ISEND(sendbuf, …, ireq, …) CALL MPI_RECV(recvbuf, …) CALL MPI_WAIT(ireq, …) ELSEIF (myrank==1) THEN CALL MPI_ISEND(sendbuf, …, ireq, …) CALL MPI_RECV(recvbuf, …) CALL MPI_WAIT(ireq, …) ENDIF

Case 2. Receive First and Then Send  Would the following pseudo-code lead to a deadlock?  A deadlock will occur regardless of how much system buffer we have  What if we use MPI_ISEND instead of MPI_SEND?  A deadlock still occurs IF (myrank==0) THEN CALL MPI_RECV(recvbuf, …) CALL MPI_SEND(sendbuf, …) ELSEIF (myrank==1) THEN CALL MPI_RECV(recvbuf, …) CALL MPI_ISEND(sendbuf, …) ENDIF

Case 2. Receive First and Then Send  What about the following pseudo-code?  It can be safely executed IF (myrank==0) THEN CALL MPI_IRECV(recvbuf, …, ireq, …) CALL MPI_SEND(sendbuf, …) CALL MPI_WAIT(ireq, …) ELSEIF (myrank==1) THEN CALL MPI_IRECV(recvbuf, …, ireq, …) CALL MPI_SEND(sendbuf, …) CALL MPI_WAIT(ireq, …) ENDIF

Case 3. One Process Sends and Receives; the other Receives and Sends  What about the following code?  It is always safe to order the calls of MPI_(I)SEND and MPI_(I)RECV at the two processes in an opposite order  In this case, we can use either blocking or non-blocking subroutines IF (myrank==0) THEN CALL MPI_SEND(sendbuf, …) CALL MPI_RECV(recvbuf, …) ELSEIF (myrank==1) THEN CALL MPI_RECV(recvbuf, …) CALL MPI_SEND(sendbuf, …) ENDIF

A Recommendation  Considering the previous options, performance, and the avoidance of deadlocks, it is recommended to use the following code: IF (myrank==0) THEN CALL MPI_ISEND(sendbuf, …, ireq1, …) CALL MPI_IRECV(recvbuf, …, ireq2, …) ELSEIF (myrank==1) THEN CALL MPI_ISEND(sendbuf, …, ireq1, …) CALL MPI_IRECV(recvbuf, …, ireq2, …) ENDIF CALL MPI_WAIT(ireq1, …) CALL MPI_WAIT(ireq2, …)

Message Passing Interface  In this part, the following concepts of MPI will be described:  Basics  Point-to-point communication  Collective communication 42

Collective Communication  Collective communication allows you to exchange data among a group of processes  It must involve all processes in the scope of a communicator  The communicator argument in a collective communication routine should specify which processes are involved in the communication  Hence, it is the programmer's responsibility to ensure that all processes within a communicator participate in any collective operation 43

Patterns of Collective Communication  There are several patterns of collective communication: 1.Broadcast 2.Scatter 3.Gather 4.Allgather 5.Alltoall 6.Reduce 7.Allreduce 8.Scan 9.Reducescatter 44

1. Broadcast  Broadcast sends a message from the process with rank root to all other processes in the group 45 A P0 P1 P2 P3 Data Process Broadcast A A A A P0 P1 P2 P3 Data Process int MPI_Bcast ( void *buffer, int count, MPI_Datatype datatype, int root, MPI_Comm comm )

2-3. Scatter and Gather  Scatter distributes distinct messages from a single source task to each task in the group  Gather gathers distinct messages from each task in the group to a single destination task ABCD P0 P1 P2 P3 Data Process Scatter A B C D P0 P1 P2 P3 Data Process int MPI_Scatter ( void *sendbuf, int sendcnt, MPI_Datatype sendtype, void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root, MPI_Comm comm ) Gather int MPI_Gather ( void *sendbuf, int sendcnt, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm )

4. All Gather  Allgather gathers data from all tasks and distribute them to all tasks. Each task in the group, in effect, performs a one-to-all broadcasting operation within the group A B C D P0 P1 P2 P3 Data Process allgather ABCD ABCD ABCD ABCD P0 P1 P2 P3 Data Process int MPI_Allgather ( void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm )

5. All To All  With Alltoall, each task in a group performs a scatter operation, sending a distinct message to all the tasks in the group in order by index A0A1A2A3 B0B1B2B3 C0C1C2C3 D0D1D2D3 P0 P1 P2 P3 Data Process Alltoall int MPI_Alltoall( void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcnt, MPI_Datatype recvtype, MPI_Comm comm ) A0B0C0D0 A1B1C1D1 A2B2C2D2 A3B3C3D3 P0 P1 P2 P3 Data Process

6-7. Reduce and All Reduce  Reduce applies a reduction operation on all tasks in the group and places the result in one task  Allreduce applies a reduction operation and places the result in all tasks in the group. This is equivalent to an MPI_Reduce followed by an MPI_Bcast A B C D P0 P1 P2 P3 Data Process Reduce int MPI_Reduce ( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, int root, MPI_Comm comm ) int MPI_Allreduce ( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm ) A*B*C*D P0 P1 P2 P3 Data Process A B C D P0 P1 P2 P3 Data Process Allreduce A*B*C*D P0 P1 P2 P3 Data Process

8. Scan  Scan computes the scan (partial reductions) of data on a collection of processes A B C D P0 P1 P2 P3 Data Process Scan int MPI_Scan ( void *sendbuf, void *recvbuf, int count, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm ) P0 P1 P2 P3 Data Process A A*B A*B*C A*B*C*D

9. Reduce Scatter  Reduce Scatter combines values and scatters the results int MPI_Reduce_scatter ( void *sendbuf, void *recvbuf, int *recvcnts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm ) Reduce Scatter A0*B0*C0*D0 A1*B1*C1*D1 A2*B2*C2*D2 A3*B3*C3*D3 P0 P1 P2 P3 Data Process A0A1A2A3 B0B1B2B3 C0C1C2C3 D0D1D2D3 P0 P1 P2 P3 Data Process

Considerations and Restrictions  Collective operations are blocking  Collective communication routines do not take message tag arguments  Collective operations within subsets of processes are accomplished by first partitioning the subsets into new groups and then attaching the new groups to new communicators 52

Next Class Discussion on Programming Models Why parallelizing our programs? Parallel computer architectures Types of Parallel Programs Message Passing Interface (MPI) MapReduce, Pregel and GraphLab Traditional Models of parallel programming MapReduce, Pregel and GraphLab