MPI (Message Passing Interface) Basics

Slides:



Advertisements
Similar presentations
MPI Message Passing Interface Portable Parallel Programs.
Advertisements

CS 140: Models of parallel programming: Distributed memory and MPI.
MPI Program Structure Self Test with solution. Self Test 1.How would you modify "Hello World" so that only even-numbered processors print the greeting.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd Edition, by B. Wilkinson & M. Allen, ©
Introduction to MPI. What is Message Passing Interface (MPI)?  Portable standard for communication  Processes can communicate through messages.  Each.
CS 240A: Models of parallel programming: Distributed memory and MPI.
Message-Passing Programming and MPI CS 524 – High-Performance Computing.
Distributed Memory Programming with MPI. What is MPI? Message Passing Interface (MPI) is an industry standard message passing system designed to be both.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd Edition, by B. Wilkinson & M. Allen, ©
Computational Grids.
1 July 29, 2005 Distributed Computing 1:00 pm - 2:00 pm Introduction to MPI Barry Wilkinson Department of Computer Science UNC-Charlotte Consortium for.
EECC756 - Shaaban #1 lec # 7 Spring Message Passing Interface (MPI) MPI, the Message Passing Interface, is a library, and a software standard.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd Edition, by B. Wilkinson & M. Allen, ©
Basics of Message-passing Mechanics of message-passing –A means of creating separate processes on different computers –A way to send and receive messages.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Chapter 3 Distributed Memory Programming with MPI An Introduction to Parallel Programming Peter Pacheco.
Parallel & Cluster Computing MPI Basics Paul Gray, University of Northern Iowa David Joiner, Shodor Education Foundation Tom Murphy, Contra Costa College.
2.1 Message-Passing Computing ITCS 4/5145 Parallel Computing, UNC-Charlotte, B. Wilkinson, Jan 17, 2012.
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.
2.1 Message-Passing Computing ITCS 4/5145 Parallel Computing, UNC-Charlotte, B. Wilkinson, Jan 14, 2013.
9-2.1 “Grid-enabling” applications Part 2 Using Multiple Grid Computers to Solve a Single Problem MPI © 2010 B. Wilkinson/Clayton Ferner. Spring 2010 Grid.
Part I MPI from scratch. Part I By: Camilo A. SilvaBIOinformatics Summer 2008 PIRE :: REU :: Cyberbridges.
CS 240A Models of parallel programming: Distributed memory and MPI.
Parallel Computing A task is broken down into tasks, performed by separate workers or processes Processes interact by exchanging information What do we.
CS 484. Message Passing Based on multi-processor Set of independent processors Connected via some communication net All communication between processes.
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.
Message Passing Programming Model AMANO, Hideharu Textbook pp. 140-147.
MPI Introduction to MPI Commands. Basics – Send and Receive MPI is a message passing environment. The processors’ method of sharing information is NOT.
Distributed-Memory (Message-Passing) Paradigm FDI 2004 Track M Day 2 – Morning Session #1 C. J. Ribbens.
12.1 Computational Grids ITCS 4010 Grid Computing, 2005, UNC-Charlotte, B. Wilkinson.
2.1 Message-Passing Computing Cluster Computing, UNC B. Wilkinson, 2007.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd Edition, by B. Wilkinson & M. Allen, ©
CSCI-455/522 Introduction to High Performance Computing Lecture 4.
1 Message Passing Models CEG 4131 Computer Architecture III Miodrag Bolic.
Message-Passing Computing Chapter 2. Programming Multicomputer Design special parallel programming language –Occam Extend existing language to handle.
1 BİL 542 Parallel Computing. 2 Message Passing Chapter 2.
12.1 Parallel Programming Types of Parallel Computers Two principal types: 1.Single computer containing multiple processors - main memory is shared,
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.
2.1 Collective Communication Involves set of processes, defined by an intra-communicator. Message tags not present. Principal collective operations: MPI_BCAST()
Chapter 5. Nonblocking Communication MPI_Send, MPI_Recv are blocking operations Will not return until the arguments to the functions can be safely modified.
3/12/2013Computer Engg, IIT(BHU)1 MPI-1. MESSAGE PASSING INTERFACE A message passing library specification Extended message-passing model Not a language.
-1.1- MPI Lectured by: Nguyễn Đức Thái Prepared by: Thoại Nam.
1 Parallel and Distributed Processing Lecture 5: Message-Passing Computing Chapter 2, Wilkinson & Allen, “Parallel Programming”, 2 nd Ed.
Slides for Parallel Programming Techniques & Applications Using Networked Workstations & Parallel Computers 2nd Edition, by B. Wilkinson & M. Allen, ©
Message Passing Interface Using resources from
1 Programming distributed memory systems Clusters Distributed computers ITCS 4/5145 Parallel Computing, UNC-Charlotte, B. Wilkinson, Jan 6, 2015.
ITCS 4/5145 Parallel Computing, UNC-Charlotte, B
Message Passing Computing
Chapter 4.
Message-Passing Computing
CS4402 – Parallel Computing
MPI Message Passing Interface
Send and Receive.
CS 584.
Send and Receive.
ITCS 4/5145 Parallel Computing, UNC-Charlotte, B
Message Passing Models
Lecture 14: Inter-process Communication
Message-Passing Computing More MPI routines: Collective routines Synchronous routines Non-blocking routines ITCS 4/5145 Parallel Computing, UNC-Charlotte,
Message-Passing Computing
ITCS 4/5145 Parallel Computing, UNC-Charlotte, B
Programming with Shared Memory
Programming with Shared Memory
Message-Passing Computing Message Passing Interface (MPI)
Hello, world in MPI #include <stdio.h> #include "mpi.h"
Hello, world in MPI #include <stdio.h> #include "mpi.h"
MPI Message Passing Interface
CS 584 Lecture 8 Assignment?.
Presentation transcript:

MPI (Message Passing Interface) Basics Grid Computing, B. Wilkinson, 2004

MPI (Message Passing Interface) Message passing library standard developed by group of academics and industrial partners to foster more widespread use and portability. Defines routines, not implementation. Several free implementations exist. Grid Computing, B. Wilkinson, 2004

MPI designed: To address some problems with earlier message-passing system such as PVM. To provide powerful message-passing mechanism and routines - over 126 routines (although it is said that one can write reasonable MPI programs with just 6 MPI routines). Grid Computing, B. Wilkinson, 2004

Unsafe Message Passing Example Intended behavior: Process 0 Process 1 Destination send(…,1,…); send(…,1,…); lib() Source recv(…,0,…); lib() recv(…,0,…); Grid Computing, B. Wilkinson, 2004

Unsafe Message Passing Example Possible behavior: Process 0 Process 1 Destination send(…,1,…); send(…,1,…); lib() Source recv(…,0,…); lib() recv(…,0,…); Grid Computing, B. Wilkinson, 2004

Message tags not sufficient to deal with such situations -- library routines might use same tags. MPI introduces concept of a “communicator” - which defines the scope of communication. Grid Computing, B. Wilkinson, 2004

MPI Communicators Defines a communication domain - a set of processes that are allowed to communicate between themselves. Communication domains of libraries can be separated from that of a user program. Used in all point-to-point and collective MPI message-passing communications. Grid Computing, B. Wilkinson, 2004

Default Communicator MPI_COMM_WORLD Exists as first communicator for all processes existing in the application. A set of MPI routines exists for forming communicators. Processes have a “rank” in a communicator. Grid Computing, B. Wilkinson, 2004

MPI Process Creation and Execution Purposely not defined - Will depend upon implementation. Only static process creation supported in MPI version 1. All processes must be defined prior to execution and started together. Originally SPMD model of computation. Grid Computing, B. Wilkinson, 2004

SPMD Computational Model main (int argc, char *argv[]) { MPI_Init(&argc, &argv); . MPI_Comm_rank(MPI_COMM_WORLD, &myrank); /*get rank*/ if (myrank == 0) master(); /* routine for master to execute */ else slave(); /* routine for slaves to execute */ MPI_Finalize(); } Grid Computing, B. Wilkinson, 2004

MPI Point-to-Point Communication Uses send and receive routines with message tags (and communicator) Wild card message tags available Grid Computing, B. Wilkinson, 2004

MPI Blocking Routines Return when “locally complete” when location used to hold message can be used again or altered without affecting message being sent. Blocking send will send message and return does not mean that message has been received, just that process free to move on without adversely affecting message. Grid Computing, B. Wilkinson, 2004

Parameters of blocking send Grid Computing, B. Wilkinson, 2004

Parameters of blocking receive Grid Computing, B. Wilkinson, 2004

Example To send an integer x from process 0 to process 1, int x; MPI_Comm_rank(MPI_COMM_WORLD,&myrank); /* find rank */ if (myrank == 0) { MPI_Send(&x, 1, MPI_INT, 1, msgtag, MPI_COMM_WORLD); } else if (myrank == 1) { MPI_Recv(&x,1,MPI_INT,0,msgtag,MPI_COMM_WORLD,status); } Grid Computing, B. Wilkinson, 2004

MPI Nonblocking Routines Nonblocking send - MPI_Isend() will return “immediately” even before source location is safe to be altered. Nonblocking receive - MPI_Irecv() will return even if no message to accept. Grid Computing, B. Wilkinson, 2004

Nonblocking Routine Formats MPI_Isend(buf,count,datatype,dest,tag,comm,request) MPI_Irecv(buf,count,datatype,source,tag,comm, request) Completion detected by MPI_Wait() and MPI_Test(). MPI_Wait() waits until operation completed, returns then. MPI_Test() returns with flag set indicating whether operation completed at that time. Need to know whether particular operation completed. Determined by accessing request parameter. Grid Computing, B. Wilkinson, 2004

Example To send an integer x from process 0 to process 1 and allow process 0 to continue, MPI_Comm_rank(MPI_COMM_WORLD, &myrank);/* find rank */ if (myrank == 0) { MPI_Isend(&x,1,MPI_INT,1,msgtag,MPI_COMM_WORLD, req1); compute(); MPI_Wait(req1, status); } else if (myrank == 1) { MPI_Recv(&x,1,MPI_INT,0,msgtag,MPI_COMM_WORLD,status); } Grid Computing, B. Wilkinson, 2004

Send Communication Modes Standard Mode Send - Not assumed that corresponding receive routine has started. Amount of buffering not defined by MPI. If buffering provided, send could complete before receive reached. Buffered Mode - Send may start and return before a matching receive. Necessary to specify buffer space via routine MPI_Buffer_attach(). Synchronous Mode - Send and receive can start before each other but can only complete together. Ready Mode - Send can only start if matching receive already reached, otherwise error. Use with care. Grid Computing, B. Wilkinson, 2004

Any type of send routine can be used with any type of receive routine. Each of the four modes can be applied to both blocking and nonblocking send routines. Only the standard mode is available for the blocking and nonblocking receive routines. Any type of send routine can be used with any type of receive routine. Grid Computing, B. Wilkinson, 2004

Collective Communication Involves set of processes, defined by an “intra-communicator.” Message tags not present. Principal collective operations: MPI_Bcast() - Broadcast from root to all other processes MPI_Gather() - Gather values for group of processes MPI_Scatter() - Scatters buffer in parts to group of processes MPI_Alltoall() - Sends data from all processes to all processes MPI_Reduce() - Combine values on all processes to single value MPI_Reduce_scatter() - Combine values and scatter results MPI_Scan() - Compute prefix reductions of data on processes Grid Computing, B. Wilkinson, 2004

Example To gather items from group of processes into process 0, using dynamically allocated memory in root process: int data[10]; /*data to be gathered*/ MPI_Comm_rank(MPI_COMM_WORLD, &myrank); /* find rank */ if (myrank == 0) { MPI_Comm_size(MPI_COMM_WORLD, &grp_size); /*find group size*/ buf = (int *)malloc(grp_size*10*sizeof(int)); /*allocate memory*/ } MPI_Gather(data,10,MPI_INT,buf,grp_size*10,MPI_INT,0,MPI_COMM_WORLD); MPI_Gather() gathers from all processes, including root. Grid Computing, B. Wilkinson, 2004

Barrier As in all message-passing systems, MPI provides a means of synchronizing processes by stopping each one until they all have reached a specific “barrier” routine. Grid Computing, B. Wilkinson, 2004

Barrier Concept Grid Computing, B. Wilkinson, 2004

Using Library Routines Grid Computing, B. Wilkinson, 2004

Grid Computing, B. Wilkinson, 2004

Measuring Execution Time To measure execution time between point L1 and point L2 in the code, might have: . L1: time(&t1); /* start timer */ L2: time(&t2); /* stop timer */ elapsed_t = difftime(t2, t1); /*t2 - t1*/ printf(“Elapsed time = %5.2f”,elapsed_t); MPI provides MPI_Wtime() for returning time (in seconds). Grid Computing, B. Wilkinson, 2004

Executing MPI programs MPI version 1 standard does not address implementation and did not specify how programs are to be started and each implementation has its own way. Grid Computing, B. Wilkinson, 2004

Several MPI implementations, such as MPICH and LAM MPI, use command: mpirun -np # prog where # is the number of processes and prog is the program. Additional argument specify computers (see later). Grid Computing, B. Wilkinson, 2004

MPI-2 The MPI standard, version 2 does recommend a command for starting MPI programs, namely: mpiexec -n # prog where # is the number of processes and prog is the program. Grid Computing, B. Wilkinson, 2004

Sample MPI Programs Grid Computing, B. Wilkinson, 2004

Hello World #include "mpi.h" #include <stdio.h> int main(int argc,char *argv[]) { MPI_Init(&argc, &argv); printf("Hello World\n"); MPI_Finalize(); return 0; } Grid Computing, B. Wilkinson, 2004

Hello World Printing out rank of process #include "mpi.h" #include <stdio.h> int main(int argc,char *argv[]) { int myrank, numprocs; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&myrank); MPI_Comm_size(MPI_COMM_WORLD,&numprocs) printf("Hello World from process %d of %d\n", myrank, numprocs); MPI_Finalize(); return 0; } Grid Computing, B. Wilkinson, 2004

Question Suppose this program is compiled as helloworld and is executed on a single computer with the command: mpirun -np 4 helloworld What would the output be? Grid Computing, B. Wilkinson, 2004

Answer Several possible outputs depending upon order processes are executed. Example Hello World from process 2 of 4 Hello World from process 0 of 4 Hello World form process 1 of 4 Hello World form process 3 of 4 Grid Computing, B. Wilkinson, 2004

Adding communication to get process 0 to print all messages: #include "mpi.h" #include <stdio.h> int main(int argc,char *argv[]) { int myrank, numprocs; char greeting[80]; /* message sent from slaves to master */ MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD,&myrank); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); sprintf(greeting,"Hello World from process %d of %d\n",rank,size); if (myrank == 0 ) { /* I am going print out everything */ printf("s\n",greeting); /* print greeting from proc 0 */ for (i = 1; i < numprocs; i++) { /* greetings in order */ MPI_Recv(geeting,sizeof(greeting),MPI_CHAR,i,1,MPI_COMM_WORLD, &status); printf(%s\n", greeting); } } else { MPI_Send(greeting,strlen(greeting)+1,MPI_CHAR,0,1, MPI_COMM_WORLD); MPI_Finalize(); return 0; Grid Computing, B. Wilkinson, 2004

MPI_Get_processor_name() Return name of processor executing code (and length of string). Arguments: MPI_Get_processor_name(char *name,int *resultlen) Example int namelen; char procname[MPI_MAX_PROCESSOR_NAME]; MPI_Get_processor_name(procname,&namelen); returned in here Grid Computing, B. Wilkinson, 2004

Easy then to add name in greeting with: sprintf(greeting,"Hello World from process %d of %d on $s\n", rank, size, procname); Grid Computing, B. Wilkinson, 2004

Pinging processes and timing Master-slave structure #include <mpi.h> void master(void); void slave(void); int main(int argc, char **argv){ int myrank; printf("This is my ping program\n"); MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); if (myrank == 0) { master(); } else { slave(); } MPI_Finalize(); return 0; Grid Computing, B. Wilkinson, 2004

Master routine void master(void){ int x = 9; double starttime, endtime; MPI_Status status; printf("I am the master - Send me a message when you receive this number %d\n", x); starttime = MPI_Wtime(); MPI_Send(&x,1,MPI_INT,1,1,MPI_COMM_WORLD); MPI_Recv(&x,1,MPI_INT,1,1,MPI_COMM_WORLD,&status); endtime = MPI_Wtime(); printf("I am the master. I got this back %d \n", x); printf("That took %f seconds\n",endtime - starttime); } Grid Computing, B. Wilkinson, 2004

Slave routine void slave(void){ int x; MPI_Status status; printf("I am the slave - working\n"); MPI_Recv(&x,1,MPI_INT,0,1,MPI_COMM_WORLD,&status); printf("I am the slave. I got this %d \n", x); MPI_Send(&x, 1, MPI_INT, 0, 1, MPI_COMM_WORLD); } Grid Computing, B. Wilkinson, 2004

Example using collective routines MPI_Bcast() MPI_Reduce() Adding numbers in a file. Grid Computing, B. Wilkinson, 2004

Grid Computing, B. Wilkinson, 2004 #include “mpi.h” #include <stdio.h> #include <math.h> #define MAXSIZE 1000 void main(int argc, char *argv){ int myid, numprocs; int data[MAXSIZE], i, x, low, high, myresult, result; char fn[255]; char *fp; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myid); if (myid == 0) { /* Open input file and initialize data */ strcpy(fn,getenv(“HOME”)); strcat(fn,”/MPI/rand_data.txt”); if ((fp = fopen(fn,”r”)) == NULL) { printf(“Can’t open the input file: %s\n\n”, fn); exit(1); } for(i = 0; i < MAXSIZE; i++) fscanf(fp,”%d”, &data[i]); MPI_Bcast(data, MAXSIZE, MPI_INT, 0, MPI_COMM_WORLD); /* broadcast data */ x = n/nproc; /* Add my portion Of data */ low = myid * x; high = low + x; for(i = low; i < high; i++) myresult += data[i]; printf(“I got %d from %d\n”, myresult, myid); /* Compute global sum */ MPI_Reduce(&myresult, &result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); if (myid == 0) printf(“The sum is %d.\n”, result); MPI_Finalize(); Grid Computing, B. Wilkinson, 2004

Compiling/Executing MPI Programs Preliminaries Set up paths Create required directory structure Create a file listing machines to be used (“hostfile”) Grid Computing, B. Wilkinson, 2004

Before starting MPI for the first time, need to create a hostfile Sample hostfile terra #venus //Currently not used, commented out leo1 leo2 leo3 leo4 leo5 leo6 leo7 leo8 Grid Computing, B. Wilkinson, 2004

Compiling/executing (SPMD) MPI program For LAM MPI version 6.5.2. At a command line: To start MPI: First time: lamboot -v hostfile Subsequently: lamboot To compile MPI programs: mpicc -o file file.c or mpiCC -o file file.cpp To execute MPI program: mpirun -v -np no_processors file To remove processes for reboot lamclean -v Terminate LAM lamhalt If fails: wipe -v lamhost Grid Computing, B. Wilkinson, 2004

Compiling/Executing Multiple MPI Programs Create a file, say called appfile, specifying programs: Example 1 master and 2 slaves, appfile contains n0 master n0-1 slave To execute: mpirun -v appfile Sample output 3292 master running on n0 (o) 3296 slave running on n0 (o) 412 slave running on n1 Grid Computing, B. Wilkinson, 2004

Parallel Programming Home Page http://www.cs.uncc.edu/par_prog Gives step-by-step instructions for compiling and executing programs, and other information. Grid Computing, B. Wilkinson, 2004