Distributed Systems CS

Slides:



Advertisements
Similar presentations
Multiple Processor Systems
Advertisements

Multiple Processor Systems
Prepared 7/28/2011 by T. O’Neil for 3460:677, Fall 2011, The University of Akron.
SE-292 High Performance Computing
Class CS 775/875, Spring 2011 Amit H. Kumar, OCCS Old Dominion University.
Multiple Processor Systems
CSCI 8150 Advanced Computer Architecture Hwang, Chapter 1 Parallel Computer Models 1.2 Multiprocessors and Multicomputers.
Background Computer System Architectures Computer System Software.
Parallel System Performance CS 524 – High-Performance Computing.
An Introduction To PARALLEL PROGRAMMING Ing. Andrea Marongiu
Multiple Processor Systems Chapter Multiprocessors 8.2 Multicomputers 8.3 Distributed systems.
1 Lecture 1: Parallel Architecture Intro Course organization:  ~5 lectures based on Culler-Singh textbook  ~5 lectures based on Larus-Rajwar textbook.
An Introduction to Parallel Computing Dr. David Cronk Innovative Computing Lab University of Tennessee Distribution A: Approved for public release; distribution.
Multiprocessors CSE 471 Aut 011 Multiprocessors - Flynn’s Taxonomy (1966) Single Instruction stream, Single Data stream (SISD) –Conventional uniprocessor.
1 CSE SUNY New Paltz Chapter Nine Multiprocessors.
Parallel System Performance CS 524 – High-Performance Computing.
CPE 731 Advanced Computer Architecture Multiprocessor Introduction
Distributed Systems CS Programming Models- Part I Lecture 16, Oct 31, 2011 Majd F. Sakr, Mohammad Hammoud andVinay Kolar 1.
1 Computer Science, University of Warwick Architecture Classifications A taxonomy of parallel architectures: in 1972, Flynn categorised HPC architectures.
Lecture 37: Chapter 7: Multiprocessors Today’s topic –Introduction to multiprocessors –Parallelism in software –Memory organization –Cache coherence 1.
Fundamental Issues in Parallel and Distributed Computing Assaf Schuster, Computer Science, Technion.
Introduction to Symmetric Multiprocessors Süha TUNA Bilişim Enstitüsü UHeM Yaz Çalıştayı
Computer System Architectures Computer System Software
Multiple Processor Systems. Multiprocessor Systems Continuous need for faster and powerful computers –shared memory model ( access nsec) –message passing.
Performance Evaluation of Parallel Processing. Why Performance?
1 Interconnects Shared address space and message passing computers can be constructed by connecting processors and memory unit using a variety of interconnection.
Data Warehousing 1 Lecture-24 Need for Speed: Parallelism Virtual University of Pakistan Ahsan Abdullah Assoc. Prof. & Head Center for Agro-Informatics.
Parallel Computer Architecture and Interconnect 1b.1.
Introduction, background, jargon Jakub Yaghob. Literature T.G.Mattson, B.A.Sanders, B.L.Massingill: Patterns for Parallel Programming, Addison- Wesley,
Multiprocessing. Going Multi-core Helps Energy Efficiency William Holt, HOT Chips 2005 Adapted from UC Berkeley "The Beauty and Joy of Computing"
Multiple Processor Systems Chapter Multiprocessors 8.2 Multicomputers 8.3 Distributed systems.
ECE200 – Computer Organization Chapter 9 – Multiprocessors.
Lecture 13: Multiprocessors Kai Bu
Multiple Processor Systems. Multiprocessor Systems Continuous need for faster computers –shared memory model ( access nsec) –message passing multiprocessor.
Distributed Systems CS /640 Programming Models Borrowed and adapted from our good friends at CMU-Doha, Qatar Majd F. Sakr, Mohammad Hammoud andVinay.
Lecture 3 : Performance of Parallel Programs Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Outline Why this subject? What is High Performance Computing?
COMP381 by M. Hamdi 1 Clusters: Networks of WS/PC.
Computer Organization CS224 Fall 2012 Lesson 52. Introduction  Goal: connecting multiple computers to get higher performance l Multiprocessors l Scalability,
3/12/2013Computer Engg, IIT(BHU)1 INTRODUCTION-2.
Multiprocessor So far, we have spoken at length microprocessors. We will now study the multiprocessor, how they work, what are the specific problems that.
August 13, 2001Systems Architecture II1 Systems Architecture II (CS ) Lecture 11: Multiprocessors: Uniform Memory Access * Jeremy R. Johnson Monday,
Spring EE 437 Lillevik 437s06-l22 University of Portland School of Engineering Advanced Computer Architecture Lecture 22 Distributed computer Interconnection.
Multiprocessor  Use large number of processor design for workstation or PC market  Has an efficient medium for communication among the processor memory.
Background Computer System Architectures Computer System Software.
Introduction Goal: connecting multiple computers to get higher performance – Multiprocessors – Scalability, availability, power efficiency Job-level (process-level)
Processor Level Parallelism 2. How We Got Here Developments in PC CPUs.
The University of Adelaide, School of Computer Science
Lecture 13 Parallel Processing. 2 What is Parallel Computing? Traditionally software has been written for serial computation. Parallel computing is the.
Lecture 13: Multiprocessors Kai Bu
INTRODUCTION TO HIGH PERFORMANCE COMPUTING AND TERMINOLOGY.
Introduction to Parallel Computing: MPI, OpenMP and Hybrid Programming
18-447: Computer Architecture Lecture 30B: Multiprocessors
Lecture 5 Approaches to Concurrency: The Multiprocessor
Introduction to parallel programming
CS5102 High Performance Computer Systems Thread-Level Parallelism
Distributed Systems CS
Guoliang Chen Parallel Computing Guoliang Chen
Lecture 1: Parallel Architecture Intro
Kai Bu 13 Multiprocessors So today, we’ll finish the last part of our lecture sessions, multiprocessors.
Multiple Processor Systems
Introduction to Multiprocessors
Distributed Systems CS
Distributed Systems CS
Distributed Systems CS
High Performance Computing
PERFORMANCE MEASURES. COMPUTATIONAL MODELS Equal Duration Model:  It is assumed that a given task can be divided into n equal subtasks, each of which.
Chapter 4 Multiprocessors
Introduction, background, jargon
Operating System Overview
Presentation transcript:

Distributed Systems CS 15-440 Programming Models Gregory Kesden Borrowed and adapted from our good friends at CMU-Doha, Qatar Majd F. Sakr, Mohammad Hammoud andVinay Kolar

Discussion on Programming Models Objectives Discussion on Programming Models MapReduce Message Passing Interface (MPI) Examples of parallel processing Traditional models of parallel programming Parallel computer architectures Why parallelism? Why parallelism?

Amdahl’s Law We parallelize our programs in order to run them faster How much faster will a parallel program run? Suppose that the sequential execution of a program takes T1 time units and the parallel execution on p processors takes Tp time units Suppose that out of the entire execution of the program, s fraction of it is not parallelizable while 1-s fraction is parallelizable Then the speedup (Amdahl’s formula):  

Amdahl’s Law: An Example Suppose that 80% of you program can be parallelized and that you use 4 processors to run your parallel version of the program The speedup you can get according to Amdahl is: Although you use 4 processors you cannot get a speedup more than 2.5 times (or 40% of the serial running time)  

Real Vs. Actual Cases Amdahl’s argument is too simplified to be applied to real cases When we run a parallel program, there are a communication overhead and a workload imbalance among processes in general 20 80 20 80 Serial Serial Parallel 20 20 Parallel 20 20 Process 1 Process 1 Process 2 Process 2 Cannot be parallelized Process 3 Process 3 Cannot be parallelized Can be parallelized Communication overhead Process 4 Can be parallelized Process 4 Load Unbalance 1. Parallel Speed-up: An Ideal Case 2. Parallel Speed-up: An Actual Case

Guidelines In order to efficiently benefit from parallelization, we ought to follow these guidelines: Maximize the fraction of our program that can be parallelized Balance the workload of parallel processes Minimize the time spent for communication

Discussion on Programming Models Objectives Discussion on Programming Models MapReduce Message Passing Interface (MPI) Examples of parallel processing Traditional models of parallel programming Parallel computer architectures Parallel computer architectures Why parallelism?

Parallel Computer Architectures We can categorize the architecture of parallel computers in terms of two aspects: Whether the memory is physically centralized or distributed Whether or not the address space is shared Memory Address Space Shared Individual Centralized SMP (Symmetric Multiprocessor) N/A Distributed NUMA (Non-Uniform Memory Access) MPP (Massively Parallel Processors) Memory Address Space Shared Individual Centralized UMA – SMP (Symmetric Multiprocessor) N/A Distributed NUMA (Non-Uniform Memory Access) MPP (Massively Parallel Processors) Memory Address Space Shared Individual Centralized SMP (Symmetric Multiprocessor) N/A Distributed NUMA (Non-Uniform Memory Access) MPP (Massively Parallel Processors) Memory Address Space Shared Individual Centralized SMP (Symmetric Multiprocessor) N/A Distributed NUMA (Non-Uniform Memory Access) MPP (Massively Parallel Processors) Memory Address Space Shared Individual Centralized SMP (Symmetric Multiprocessor) N/A Distributed NUMA (Non-Uniform Memory Access) MPP (Massively Parallel Processors)

Symmetric Multiprocessor Symmetric Multiprocessor (SMP) architecture uses shared system resources that can be accessed equally from all processors A single OS controls the SMP machine and it schedules processes and threads on processors so that the load is balanced Processor Processor Processor Processor Cache Cache Cache Cache Bus or Crossbar Switch Memory I/O

Massively Parallel Processors Massively Parallel Processors (MPP) architecture consists of nodes with each having its own processor, memory and I/O subsystem An independent OS runs at each node Interconnection Network Processor Processor Processor Processor Cache Cache Cache Cache Bus Bus Bus Bus Memory I/O Memory I/O Memory I/O Memory I/O

Non-Uniform Memory Access Non-Uniform Memory Access (NUMA) architecture machines are built on a similar hardware model as MPP NUMA typically provides a shared address space to applications using a hardware/software directory-based coherence protocol The memory latency varies according to whether you access memory directly (local) or through the interconnect (remote). Thus the name non-uniform memory access As in an SMP machine, a single OS controls the whole system

Discussion on Programming Models Objectives Discussion on Programming Models MapReduce Message Passing Interface (MPI) Examples of parallel processing Traditional Models of parallel programming Traditional Models of parallel programming Parallel computer architectures Why parallelizing our programs?

Models of Parallel Programming What is a parallel programming model? A programming model is an abstraction provided by the hardware to programmers It determines how easily programmers can specify their algorithms into parallel unit of computations (i.e., tasks) that the hardware understands It determines how efficiently parallel tasks can be executed on the hardware Main Goal: utilize all the processors of the underlying architecture (e.g., SMP, MPP, NUMA) and minimize the elapsed time of your program

Traditional Parallel Programming Models Shared Memory Message Passing Message Passing

Shared Memory Model In the shared memory programming model, the abstraction is that parallel tasks can access any location of the memory Parallel tasks can communicate through reading and writing common memory locations This is similar to threads from a single process which share a single address space Multi-threaded programs (e.g., OpenMP programs) are the best fit with shared memory programming model

Shared Memory Model Si = Serial Pj = Parallel Single Thread Multi-Thread S1 S1 Time Time Spawn P1 P1 P2 P3 P3 P2 P3 Join S2 Shared Address Space P4 S2 Process Process

Shared Memory Example Sequential Parallel begin parallel // spawn a child thread private int start_iter, end_iter, i; shared int local_iter=4, sum=0; shared double sum=0.0, a[], b[], c[]; shared lock_type mylock; start_iter = getid() * local_iter; end_iter = start_iter + local_iter; for (i=start_iter; i<end_iter; i++) a[i] = b[i] + c[i]; barrier; if (a[i] > 0) { lock(mylock); sum = sum + a[i]; unlock(mylock); } barrier; // necessary end parallel // kill the child thread Print sum; for (i=0; i<8; i++) a[i] = b[i] + c[i]; sum = 0; if (a[i] > 0) sum = sum + a[i]; Print sum; Sequential Parallel

Traditional Parallel Programming Models Shared Memory Shared Memory Message Passing

Message Passing Model In message passing, parallel tasks have their own local memories One task cannot access another task’s memory Hence, to communicate data they have to rely on explicit messages sent to each other This is similar to the abstraction of processes which do not share an address space MPI programs are the best fit with message passing programming model

Message Passing Model Single Thread Message Passing S = Serial P = Parallel S1 S1 S1 S1 S1 Time Time P1 P1 P1 P1 P1 P2 S2 S2 S2 S2 P3 P4 Process 0 Process 1 Process 2 Process 3 S2 Node 1 Node 2 Node 3 Node 4 Data transmission over the Network Process

Message Passing Example id = getpid(); local_iter = 4; start_iter = id * local_iter; end_iter = start_iter + local_iter; if (id == 0) send_msg (P1, b[4..7], c[4..7]); else recv_msg (P0, b[4..7], c[4..7]); for (i=start_iter; i<end_iter; i++) a[i] = b[i] + c[i]; local_sum = 0; if (a[i] > 0) local_sum = local_sum + a[i]; if (id == 0) { recv_msg (P1, &local_sum1); sum = local_sum + local_sum1; Print sum; } send_msg (P0, local_sum); for (i=0; i<8; i++) a[i] = b[i] + c[i]; sum = 0; if (a[i] > 0) sum = sum + a[i]; Print sum; Sequential Parallel

Shared Memory Vs. Message Passing Comparison between shared memory and message passing programming models: Aspect Shared Memory Message Passing Communication Implicit (via loads/stores) Explicit Messages Synchronization Explicit Implicit (Via Messages) Hardware Support Typically Required None Development Effort Lower Higher Tuning Effort Aspect Shared Memory Message Passing Communication Implicit (via loads/stores) Explicit Messages Synchronization Explicit Implicit (Via Messages) Hardware Support Typically Required None Development Effort Lower Higher Tuning Effort Aspect Shared Memory Message Passing Communication Implicit (via loads/stores) Explicit Messages Synchronization Explicit Implicit (Via Messages) Hardware Support Typically Required None Development Effort Lower Higher Tuning Effort Aspect Shared Memory Message Passing Communication Implicit (via loads/stores) Explicit Messages Synchronization Explicit Implicit (Via Messages) Hardware Support Typically Required None Development Effort Lower Higher Tuning Effort Aspect Shared Memory Message Passing Communication Implicit (via loads/stores) Explicit Messages Synchronization Explicit Implicit (Via Messages) Hardware Support Typically Required None Development Effort Lower Higher Tuning Effort

Discussion on Programming Models Objectives Discussion on Programming Models MapReduce Message Passing Interface (MPI) Examples of parallel processing Examples of parallel processing Traditional Models of parallel programming Parallel computer architectures Why parallelizing our programs?

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: Single Program Multiple Data (SPMD) model Multiple Programs Multiple Data (MPMP) model

SPMD In the SPMD model, there is only one program and each process uses the same executable working on different sets of data 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 b.out a.out b.out c.out a.out= Structural Analysis, b.out = fluid analysis and c.out = thermal analysis Example Node 1 Node 2 Node 3 Node 1 Node 2 Node 3 1. MPMD: Master/Slave 2. MPMD: Coupled Analysis

3 Key Points To summarize, keep the following 3 points in mind: The purpose of parallelization is to reduce the time spent for computation 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

Discussion on Programming Models Objectives Discussion on Programming Models MapReduce Message Passing Interface (MPI) Message Passing Interface (MPI) Examples of parallel processing Traditional Models of parallel programming Parallel computer architectures Why parallelizing our programs?

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

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

Reasons for using MPI Reason Description Reason Description Reason 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 Reason Description 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 Reason Description 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 Reason Description Standardization MPI is the only message passing library which can be considered a standard. It is supported on virtually all HPC platforms Reason Description 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

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

Communicators and Groups MPI uses objects called communicators and 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

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)

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

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 MPI_COMM_WORLD Comm_Fluid Comm_Struct Rank=0 Rank=1 Rank=0 Rank=4 Rank=1 Rank=5 - In the first bullet, this happens most of the time. Not all times. Rank=2 Rank=3 Rank=2 Rank=6 Rank=3 Rank=7 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

Next Class Discussion on Programming Models MapReduce Message Passing Interface (MPI) Message Passing Interface (MPI) Examples of parallel processing Traditional Models of parallel programming Parallel computer architectures Programming Models- Part II Why parallelizing our programs?

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

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 Processor1 Processor2 Network sendA recvA

Two Cases Consider the following two cases: A send operation occurs 5 seconds before the receive is ready - where is the message stored while the receive is pending? 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"?

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

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 Safe to modify sendbuf Rank 0 Rank 1 sendbuf recvbuf Network recvbuf sendbuf

Blocking Send and Receive A blocking send can be: Synchronous: Means there is a handshaking occurring with the receive task to confirm a safe send 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

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 - For the last bullet, there are "wait" routines used to do this

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

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

MPI Point-To-Point Communication Routines Signature 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 ) Routine Signature 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 ) Routine Signature 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 ) Routine Signature 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 ) Routine Signature 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

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 Task 0 Task 1 Msg A 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 Blocking send and blocking receive Non-blocking send and blocking receive Blocking send and non-blocking receive Non-blocking send and non-blocking receive Rank 0 Rank 1 sendbuf recvbuf 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 Rank 0 Rank 1 sendbuf recvbuf 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: Either due to the incorrect order of send and receive Or due to the limited size of the system buffer Rank 0 Rank 1 sendbuf recvbuf 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 IF (myrank==0) THEN CALL MPI_SEND(sendbuf, …) CALL MPI_RECV(recvbuf, …) ELSEIF (myrank==1) THEN ENDIF IF (myrank==0) THEN CALL MPI_ISEND(sendbuf, …, ireq, …) CALL MPI_WAIT(ireq, …) CALL MPI_RECV(recvbuf, …) ELSEIF (myrank==1) THEN 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 not larger than the send buffer? DEADLOCK! Rank 0 Rank 1 Rank 0 Rank 1 sendbuf sendbuf sendbuf sendbuf Network Network sysbuf sysbuf sysbuf sysbuf recvbuf recvbuf recvbuf recvbuf

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 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? Deadlock still occurs IF (myrank==0) THEN CALL MPI_RECV(recvbuf, …) CALL MPI_SEND(sendbuf, …) ELSEIF (myrank==1) THEN CALL MPI_ISEND(sendbuf, …) ENDIF The following pseudo-code leads to a deadlock regardless of how much system buffer we have

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 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 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 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

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

Patterns of Collective Communication There are several patterns of collective communication: Broadcast Scatter Gather Allgather Alltoall Reduce Allreduce Scan Reducescatter

1. Broadcast Broadcast sends a message from the process with rank root to all other processes in the group Data Data P0 A P0 A Process Broadcast Process P1 P1 P2 P2 P3 P3 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 Data Data P0 A B C D P0 A B C D Scatter Process Process P1 P1 P2 P2 P3 Gather P3 int MPI_Scatter ( void *sendbuf, int sendcnt, MPI_Datatype sendtype, void *recvbuf, int recvcnt, MPI_Datatype recvtype, int root, MPI_Comm comm ) 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 Data Data P0 A B C D P0 A B C D Process allgather Process P1 P1 P2 P2 P3 P3 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 Data Data P0 A0 A1 A2 A3 B0 B1 B2 B3 C0 C1 C2 C3 D0 D1 D2 D3 P0 A0 B0 C0 D0 A1 B1 C1 D1 A2 B2 C2 D2 A3 B3 C3 D3 Alltoall Process Process P1 P1 P2 P2 P3 P3 int MPI_Alltoall( void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcnt, MPI_Datatype recvtype, MPI_Comm comm )

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 Data Data Data Data P0 A B C D P0 A*B*C*D P0 A B C D P0 A*B*C*D Reduce Allreduce Process Process Process Process P1 P1 P1 P1 P2 P2 P2 P2 P3 P3 P3 P3 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 )

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

9. Reduce Scatter Reduce Scatter combines values and scatters the results. It is equivalent to an MPI_Reduce followed by an MPI_Scatter operation.  Data Data P0 A0 A1 A2 A3 B0 B1 B2 B3 C0 C1 C2 C3 D0 D1 D2 D3 P0 A0*B0*C0*D0 A1*B1*C1*D1 A2*B2*C2*D2 A3*B3*C3*D3 Process Reduce Scatter Process P1 P1 P2 P2 P3 P3 int MPI_Reduce_scatter ( void *sendbuf, void *recvbuf, int *recvcnts, MPI_Datatype datatype, MPI_Op op, MPI_Comm comm )

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