SlideShare a Scribd company logo
1 of 54
Download to read offline
Parallel Programming
Using MPI
Dr. Ajit K Nayak
SOA University, Odisha, India
Introduction to MPI/Ajit Nayak//2
What is MPI?
 The Message Passing Interface, is a
standardised and portable message passing
system
 It is designed by a group of researchers from
academia and industry to function on a wide
variety of parallel computer
 The standard defines the syntax and semantics
of core library routines useful to a wide range
of users writing portable message-passing
programs in Fortran or C
Introduction to MPI/Ajit Nayak//3
The Environment
 Clustering is used as a Parallel Computing
Environment
 Clustering is nothing but a network (cluster) of
computers configured to work as one computer
 This type of environment is cheaper (no more
than a general network environment!) than buying
an original parallel computer
 A Parallel computer (of this kind) may be
deployed on the fly
Introduction to MPI/Ajit Nayak//4
Building the Environment
 Choosing a Network Topology
• Network of workstations (NOW)
 Choosing an Operating System
• Linux
 Installing and configuring the Environment
• The network setup for master and slaves
 Choosing, Installing and Configuring a MPI
implementation
• mpich-1.2.5 (free downloadable from
Introduction to MPI/Ajit Nayak//5
Network of Workstations
 The cluster should be viewed as a special,
standalone entity.
 It should have its own networking facilities that
are not shared with other systems.
 A cluster should have one Master node that
serves as the central focus of the operational
aspects of the cluster.
 Also it should have some Slave nodes, which
will be used as workstations.
Introduction to MPI/Ajit Nayak//6
A Pictorial view
Slave Nodes
Master Node
Ethernet Hub
Network of Workstations
Introduction to MPI/Ajit Nayak//7
Programming using MPI
 MPI Assumes that the no of processes created
(for a program) are independent of processors
(computers) available (for the cluster).
 The number of computers for a cluster is
decided by the network administrator
 The number of processes for a program is
decided by the parallel programmer
 i.e. no processes are created (or no processors
could be attached) during the execution of a
program.
Introduction to MPI/Ajit Nayak//8
How it works?
 Each process is recognized by a unique integer
id called rank (0 .. p-1).
 Each process is assigned to a computer
(Processor) in a round-robin fashion.
 Process Zero always runs on Master
Process 0
P1
P2
P6
P5
P4 P3
Introduction to MPI/Ajit Nayak//9
Process Distribution
 4 processors and 8 processes
Process 0 Assigned to Master
Process 1 Assigned to Slave 1
Process 2 Assigned to Slave 2
Process 3 Assigned to Slave 3
Process 4 Assigned to Master
Process 5 Assigned to Slave 1
Process 6 Assigned to Slave 2
Process 7 Assigned to Slave 3
Introduction to MPI/Ajit Nayak//10
Process Distribution
Master Node
Process Distribution
(P0, P4)
Slave 1 Slave 2 Slave 3
(P1, P5)
(P2, P6) (P3)
This programs
are called SPMD
programs!
Introduction to MPI/Ajit Nayak//11
MPI Program Structure
#include “mpi.h”
main(int argc, char** argv){
/*. . . Declarations . . . */
/*. . . Sequential Algorithms . . . */
MPI_Init(&argc, &argv);
/*. . . Parallel Algorithm . . . */
MPI_Finalize( );
/* . . . Sequential Algorithm . . . */
} // End of main program !
Introduction to MPI/Ajit Nayak//12
Program Structure contd…
 int MPI_Init( int *argc, char ***argv )
• It initializes the parallel environment.
• It must be called before any other MPI
routine
 int MPI_Finalize( void )
• It cleans-up all MPI state
• After this invocation no MPI routine should
be called.
Introduction to MPI/Ajit Nayak//13
Introduction to MPI/Ajit Nayak//14
Sending information
 int MPI_Send( void *buf, int count, MPI_Datatype
datatype, int dest, int tag, MPI_Comm comm)
• It is used for point-to-point communication, i.e. comm. between a
pair of processes. One side sending and other receiving.
•The send call blocks until the send buffer can be reclaimed
buf initial address of send buffer
count number of elements to send
datatype datatype of each entry
dest rank of destination
tag message tag
comm communicator
Introduction to MPI/Ajit Nayak//15
Receiving information
 int MPI_Recv( void *buf, int count, MPI_Datatype
datatype, int source, int tag, MPI_Comm comm,
MPI_Status *status)
• It performs a blocking receive operation
buf initial address of send buffer
count max number of elements to receive
datatype datatype of each entry
dest rank of destination
tag message tag
comm communicator (defines a communication domain)
status return status (A structure having information
about source, tag and error)
Introduction to MPI/Ajit Nayak//16
A First Program
 Problem:
• Root process receives a greeting message
from all other processes.
• Each process other than Root send a greeting
message to Root process
• Root process prints messages received from
other processes
 We will use blocking send and recv calls to
perform this task
Introduction to MPI/Ajit Nayak//17
The Complete Program
#include<stdio.h>
#include<string.h>
#include “mpi.h”
int main (int argc, char *argv[ ]) {
int myRank, numProcs, dest;
int Root=0, tag=0; int i;
char msg[30];
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
if(myRank !=Root){
strcpy(msg, “Helo World”);
Introduction to MPI/Ajit Nayak//18
Greeting Program contd..
MPI_Send(msg, strlen(msg)+1, MPI_CHAR, Root, tag,
MPI_COMM_WORLD);
} //end if
else{
for(i=1; i < numProcs; i++){
MPI_Recv(msg, 30, MPI_CHAR, i, tag,
MPI_COMM_WORLD, &status);
printf(“%s From process %dn”, msg, i);
}//end for
} // end else
MPI_Finalize( );
} // End of program !
Introduction to MPI/Ajit Nayak//19
MPI Datatypes
 MPI datatype C datatype
 MPI_CHAR signed char
 MPI_SHORT signed short int
 MPI_INT signed int
 MPI_LONG signed long int
 MPI_UNSIGNED_CHAR unsigned char
 MPI_UNSIGNED_SHORT unsigned short int
 MPI_UNSIGNED unsigned int
 MPI_UNSIGNED_LONG unsigned long int
 MPI_FLOAT float
 MPI_DOUBLE double
 MPI_LONG_DOUBLE long double
Introduction to MPI/Ajit Nayak//20
Where /How to get it done?
 Login to a specified user
 Open a file in vi editor (vim greeting.c)
 Write the source code, save and exit
 In command prompt issue following commands
To Compile
mpicc -o <objFileName> <CFileName>
To Execute
mpiexec –n <4> ./<objFileName>
Introduction to MPI/Ajit Nayak//21
A Sum Program
Problem:
• To find the sum of n integers on p processes
• Where p=n and p=2q
Introduction to MPI/Ajit Nayak//22
Problem Understanding
0 1 2 3 4 5 6 7
6+220
1+5 9+130 4
No of processes (p)= 8
q=3 (p=2q)
0+1 2+3 4+5 6+70 2 64
Introduction to MPI/Ajit Nayak//23
Program
#include<stdio.h>
#include<math.h>
#include "mpi.h"
#define IS_INT(X) ( (X) == (int) (X)) ? 1:0
#define LOG2(X) log10(X)/log10(2)
int main(int argc, char *argv[]){
int myRank, numProcs, Root=0;
int source, destination, tag=0;
int iLevel, level, nextLevel , value, sum=0, ans;
float height; //height of the tree
MPI_Status status;
Introduction to MPI/Ajit Nayak//24
Program contd.
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
height=LOG2(numProcs);
Introduction to MPI/Ajit Nayak//25
Program contd.
/* if p!=2q */
if(!(IS_INT(height))){
if(MyRank==Root)
printf("n Error: Number of processes
should be power of 2...n");
MPI_Finalize();
exit(-1);
}//end error checking
sum=myRank;
Introduction to MPI/Ajit Nayak//26
Program contd.
//find sender and receiver in each level
for(ilevel=0; ilevel < height; ilevel++){
Level = pow(2, ilevel);
if((MyRank % level)== 0){
NextLevel = pow(2,(ilevel+1));
if((myRank% nextLevel)==0){ //if receiver
source = myRank+Level;
MPI_Recv(&value, 1, MPI_INT,
Source, tag, MPI_COMM_WORLD, &status);
sum += value;
}
Introduction to MPI/Ajit Nayak//27
Program contd.
else{//if sender
Destination=MyRank - Level;
MPI_Send(&sum, 1, MPI_INT,
destination, tag, MPI_COMM_WORLD);
}//end of else
}
} // end of for loop
if(MyRank==Root)
printf("nnMy Rank is %d and the Final Sum %d
nn",myRank,sum);
MPI_Finalize(); } //end of the program
Introduction to MPI/Ajit Nayak//28
Introduction to MPI/Ajit Nayak//29
Collective Communications
Collective communications transmit data
among all processes in a group.
Synchronizes processes without passing data.
MPI provides the following collective
communication
Global Communication Functions
Broadcast
Gather/Scatter etc.
Global Reduction Functions
Reduce
Introduction to MPI/Ajit Nayak//30
Introduction to MPI/Ajit Nayak//31
Broadcast
 int MPI_Bcast(void *buf, int count, MPI_Datatype
datatype, int root, MPI_Comm comm);
• It broadcasts a message from root to all processes in the
group (including itself)
Broadcast
Processes
Elements
A0 A0
A0
A0
A0
Introduction to MPI/Ajit Nayak//32
Broadcasting How to?
. . .
int array[100];
int Root=0;
MPI_Bcast(array, 100, MPI_INT, Root,
MPI_COMM_WORLD);
. . .
 This call will broadcast an array of 100
integers to all processes
 We don‟t require a corresponding receive call in
all processes to receive this broadcast
Introduction to MPI/Ajit Nayak//33
Gather / Scatter
 int MPI_Gather/Scatter(void *sendbuf, int
sendcount, MPI_Datatype sendtype, void
*recvdbuf, int recvcount, MPI_Datatype
recvtype, int root, MPI_Comm comm);
Processes
Elements
Gather
Scatter
A0
A1
A2
A3
A0 A1 A2 A3
Introduction to MPI/Ajit Nayak//34
Gather / Scatter
• Each process (including Root) sends the
contents of its send buffer to the root
process
• The root process receives the messages and
stores them in rank order
• recvcount argument at the root indicates the
no of items it receives from each process
100
Proc 0
100
Proc 1
100
Proc 2
Proc 0 100 100 100
Gather
Scatter
Introduction to MPI/Ajit Nayak//35
Gather / Scatter contd.
if (myRank==0)
rbuf=(int *) malloc (size*100*sizeof(int));
MPI_Gather(sendArray, 100, MPI_INT, rbuf,
100, MPI_INT, root, MPI_COMM_WORLD);
• Similarly in case of Scatter, Root process
distributes items to all processes in rank order
rbuf = ( int *) malloc (recvCount * sizeof(int));
if (myRank==0)
MPI_Scatter(sendbuf, sendCount, MPI_INT, rbuf,
recvCount, MPI_INT, root, MPI_COMM_WORLD);
Introduction to MPI/Ajit Nayak//36
Gather to All
 This is like gather, except all process receives the
same result.
Processes
Elements
D0
C0
B0
A0
D0C0B0A0
D0C0B0A0
D0C0B0A0
D0C0B0A0
AllGather
 The data received from jth process is placed at jth
block of rbuf.
int MPI_Allgather(void *sbuf, int scount, MPI_Datatype stype, void
*rbuf, int rcount, MPI_Datatype rtype, MPI_COMM_WORLD);
Introduction to MPI/Ajit Nayak//37
All to All
 Each process sends distinct data to each of the
receivers.
Processes
Elements
D3D2D1D0
C3C2C1C0
B3B2B1B0
A3A2A1A0
D3C3B3A3
D2C2B2A2
D1C1B1A1
D0C0B0A0
Alltoall
 The jth block sent from process i is received by
process j and is placed in the ith block of rbuf.
int MPI_Alltoall(void *sbuf, int scount, MPI_Datatype stype, void
*rbuf, int rcount, MPI_Datatype rtype, MPI_COMM_WORLD);
Introduction to MPI/Ajit Nayak//38
Next Program
 Problem
• Matrix Vector Product ( A X = B )
Input: A [ 1..m, 1..n ], X[ 1..n ]
Output: B[ 1..m ]
Algorithm:
1. an 8x8 matrix has been divided into 4 (2x8) sub-matrices.
2. Each sub-matrix is stored in the local memory of each
process.
3. A local matrix-vector product is performed at each
process.
4. Then the results are assembled from each process onto
Root process.
Introduction to MPI/Ajit Nayak//39
The Program
#include <stdio.h>
#include "mpi.h"
int main( int argc, char **argv ){
int a[2][8], b[8], cpart[2], ctotal[8];
int rank, size, i, k;
MPI_Init( &argc, &argv );
MPI_Comm_rank( MPI_COMM_WORLD, &rank);
MPI_Comm_size( MPI_COMM_WORLD, &size );
Introduction to MPI/Ajit Nayak//40
Matrix vector Prog contd..
if (size != 4) {
printf("Error!:# of processors must be equal to 4n");
printf("Programm aborting....n");
MPI_Abort(MPI_COMM_WORLD, 1); }
for (i=0;i<2;i++) // setting values for a[1..2, 1..8]
for (k=0;k<8;k++) a[i][k] = rank*(k+1);
printf("process %d:n",rank); //printing values of „a‟
for (i=0;i<2;i++){
for (k=0;k<8;k++)printf("%dt",a[i][k]);
printf("n");
}
printf("n");
Introduction to MPI/Ajit Nayak//41
Matrix vector prod. Prog. contd..
for (k=0;k<8;k++) b[k] = k+1; // value of „b‟
for (i=0;i<2;i++){
cpart[i] = 0;
for (k=0;k<8;k++) cpart[i] = cpart[i] + a[i][k] * b[k];
}
printf("After multiplication process %d:n",rank);
for (k=0;k<2;k++)
printf("%dt",cpart[k]);
printf("n");
Introduction to MPI/Ajit Nayak//42
Matrix vector prod Prog contd..
MPI_Gather(cpart, 2, MPI_INT, ctotal, 2,
MPI_INT,0, MPI_COMM_WORLD);
if(rank==0){
printf("Vector b:n");
for (k=0;k<8;k++) printf("%dt",b[k]);
printf(“n result isn");
for (k=0;k<8;k++)
printf("%dt",ctotal[k]);
printf("n");
} // end of if
MPI_Finalize();
} // end of main program
Introduction to MPI/Ajit Nayak//43
Matrix vector prod Prog Output
process 0:
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
process 1:
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
process 2:
2 4 6 8 10 12 14 16
2 4 6 8 10 12 14 16
process 3:
3 6 9 12 15 18 21 24
3 6 9 12 15 18 21 24
Introduction to MPI/Ajit Nayak//44
Matrix vector prod Prog Output
After multiplication process 0: 0 0
After multiplication process 1: 204 204
After multiplication process 2: 408 408
After multiplication process 3: 612 612
Vector b:
1 2 3 4 5 6 7 8
result vector:
0 0 204 204 408 408 612 612
Introduction to MPI/Ajit Nayak//45
Introduction to MPI/Ajit Nayak//46
Reduce
 int MPI Reduce(void* sendbuf, void* recvbuf,
int count, MPI Datatype datatype, MPI Op op,
int root, MPI Comm comm)
Reduce
(+)
Processes
Elements
C2A2
B2
C1B1A1
C0B0
A0 C0+ C1+ C2B0+ B1+ B2
A0+ A1+ A2
Introduction to MPI/Ajit Nayak//47
Other Reduce functions
All Reduce
(+)
Processes
Elements
C2A2
B2
C1B1A1
C0B0
A0
C0+ C1+ C2A0+ A1+ A2
B0+ B1+ B2
C0+ C1+ C2B0+ B1+ B2A0+ A1+ A2
C0+ C1+ C2B0+ B1+ B2
A0+ A1+ A2
Reduce
Scatter
(+)
Processes
Elements
C2A2
B2
C1B1A1
C0B0
A0
C0+ C1+ C2
B0+ B1+ B2
A0+ A1+ A2
Introduction to MPI/Ajit Nayak//48
Predefined Operations
 Name Meaning
 MPI_MAX maximum
 MPI_MIN minimum
 MPI_SUM sum
 MPI_PROD product
 MPI_LAND logical and
 MPI_BAND bit-wise and
 MPI_LOR logical or
 MPI_BOR bit-wise or
 MPI_LXOR logical xor
 MPI_BXOR bit-wise xor
 MPI_MAXLOC max value and location
 MPI_MINLOC min value and location
Introduction to MPI/Ajit Nayak//49
Practice Programs
 Find sum of integers in the range(0:500000) using
blocking calls (send, recv)
• Method 1: (simple)
 Divide different ranges in in different processes
 Process with rank greater than 0, sends the value of its
result to root process. Process 0 calculates and prints
final sum
• Method 2: (Linear array)
 Process n-1 sends its sum to process n-2, n-2 adds its
own sum with received sum and sends the resulting sum
to n-3.
 This process continues till it reaches at process 0.
Introduction to MPI/Ajit Nayak//50
Practice Programs
• Method 3: (hypercube) use the hypercube algorithm.
 Find sum of above range of integers using non-
blocking calls (reduce)
 Modify Matrix vector product program to work with
any size Matrix.
Introduction to MPI/Ajit Nayak//51
Other Utility-Calls
 double MPI_Wtime(void)
• returns a double floating point number of
seconds, since some arbitrary point of time in
the past.
• It can be used to find the execution-time of
programs or ‘section of programs’. The time
interval can be measured by calling this
routine at the beginning and at the end of
program/section and subtracting the values
returned.
Introduction to MPI/Ajit Nayak//52
Other Utility-Calls
 int MPI_Barrier (MPI_Comm comm)
• MPI_Barrier blocks the calling process until all processes in
communicator have entered the function.
 int MPI_Sendrecv (void *sendbuf, int sendcount,
MPI_Datatype sendtype, int dest, int sendtag, void
*recvbuf , int recvcount, MPI_Datatype recvtype, int
source, int recvtag, MPI_Comm comm, MPI_Status
*status)
• performs both a send and a receive. Can be matched with
ordinary send and recv.
• No deadlock arises
Introduction to MPI/Ajit Nayak//53
References
The standardization forum:
• http://mpi-forum.org/
Software
• https://www.mpich.org/
Books
 Peter. S. Pacheco – Parallel Programming with MPI,
Morgan Kaufmann Publishers
 M. Snir, S. Otto, S H-Lederman, D Walker, J Dongarra
– MPI: The Complete Reference, The MIT Press,
Cambridge, Massachusetts, London
Introduction to MPI/Ajit Nayak//54
Thank You

More Related Content

What's hot (20)

Transport layer
Transport layerTransport layer
Transport layer
 
Eigrp.ppt
Eigrp.pptEigrp.ppt
Eigrp.ppt
 
Feng’s classification
Feng’s classificationFeng’s classification
Feng’s classification
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
MULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULINGMULTILEVEL QUEUE SCHEDULING
MULTILEVEL QUEUE SCHEDULING
 
Linux scheduler
Linux schedulerLinux scheduler
Linux scheduler
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Networking in linux
Networking in linuxNetworking in linux
Networking in linux
 
03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis03 Analysis of Algorithms: Probabilistic Analysis
03 Analysis of Algorithms: Probabilistic Analysis
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Samba server
Samba serverSamba server
Samba server
 
Distance vector routing
Distance vector routingDistance vector routing
Distance vector routing
 
Programming using MPI and OpenMP
Programming using MPI and OpenMPProgramming using MPI and OpenMP
Programming using MPI and OpenMP
 
Types of grammer - TOC
Types of grammer - TOCTypes of grammer - TOC
Types of grammer - TOC
 
MPI Tutorial
MPI TutorialMPI Tutorial
MPI Tutorial
 
Route Redistribution
Route RedistributionRoute Redistribution
Route Redistribution
 
Introduction to MPI
Introduction to MPI Introduction to MPI
Introduction to MPI
 
Chapter 4 slides
Chapter 4 slidesChapter 4 slides
Chapter 4 slides
 
Scheduling in distributed systems - Andrii Vozniuk
Scheduling in distributed systems - Andrii VozniukScheduling in distributed systems - Andrii Vozniuk
Scheduling in distributed systems - Andrii Vozniuk
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
 

Viewers also liked

Enterprise Architecture for Dummies
Enterprise Architecture for DummiesEnterprise Architecture for Dummies
Enterprise Architecture for DummiesSebastien Juras
 
Ns2: OTCL - PArt II
Ns2: OTCL - PArt IINs2: OTCL - PArt II
Ns2: OTCL - PArt IIAjit Nayak
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIAjit Nayak
 
Ns2: Introduction - Part I
Ns2: Introduction - Part INs2: Introduction - Part I
Ns2: Introduction - Part IAjit Nayak
 
Misiones en Honduras Mayo 2012
Misiones en Honduras Mayo 2012Misiones en Honduras Mayo 2012
Misiones en Honduras Mayo 2012Ricardo Quintero
 
Introduction to database-ER Model
Introduction to database-ER ModelIntroduction to database-ER Model
Introduction to database-ER ModelAjit Nayak
 
03 administracion de requisitos
03 administracion de requisitos03 administracion de requisitos
03 administracion de requisitosRicardo Quintero
 
Object Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IObject Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IAjit Nayak
 
Software Engineering an Introduction
Software Engineering an IntroductionSoftware Engineering an Introduction
Software Engineering an IntroductionAjit Nayak
 
Software Engineering :Behavioral Modelling - II State diagram
Software Engineering :Behavioral Modelling - II State diagramSoftware Engineering :Behavioral Modelling - II State diagram
Software Engineering :Behavioral Modelling - II State diagramAjit Nayak
 
Uml Omg Fundamental Certification 2
Uml Omg Fundamental Certification 2Uml Omg Fundamental Certification 2
Uml Omg Fundamental Certification 2Ricardo Quintero
 
Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5Ricardo Quintero
 
Introduction to database-Formal Query language and Relational calculus
Introduction to database-Formal Query language and Relational calculusIntroduction to database-Formal Query language and Relational calculus
Introduction to database-Formal Query language and Relational calculusAjit Nayak
 
Omg Fundamental Certification 4
Omg Fundamental Certification 4Omg Fundamental Certification 4
Omg Fundamental Certification 4Ricardo Quintero
 
Object Oriented Programming using C++ Part II
Object Oriented Programming using C++ Part IIObject Oriented Programming using C++ Part II
Object Oriented Programming using C++ Part IIAjit Nayak
 
Introduction to database-Transaction Concurrency and Recovery
Introduction to database-Transaction Concurrency and RecoveryIntroduction to database-Transaction Concurrency and Recovery
Introduction to database-Transaction Concurrency and RecoveryAjit Nayak
 
Things to know to improve your willpower
Things to know to improve your willpowerThings to know to improve your willpower
Things to know to improve your willpowerSebastien Juras
 
Computer Networks Module III
Computer Networks Module IIIComputer Networks Module III
Computer Networks Module IIIAjit Nayak
 
Is your company fully engaged towards innovation?
Is your company fully engaged towards innovation?Is your company fully engaged towards innovation?
Is your company fully engaged towards innovation?Sebastien Juras
 

Viewers also liked (20)

01 conceptos de diseño
01 conceptos de diseño01 conceptos de diseño
01 conceptos de diseño
 
Enterprise Architecture for Dummies
Enterprise Architecture for DummiesEnterprise Architecture for Dummies
Enterprise Architecture for Dummies
 
Ns2: OTCL - PArt II
Ns2: OTCL - PArt IINs2: OTCL - PArt II
Ns2: OTCL - PArt II
 
Object Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part IIIObject Oriented Programming using C++ Part III
Object Oriented Programming using C++ Part III
 
Ns2: Introduction - Part I
Ns2: Introduction - Part INs2: Introduction - Part I
Ns2: Introduction - Part I
 
Misiones en Honduras Mayo 2012
Misiones en Honduras Mayo 2012Misiones en Honduras Mayo 2012
Misiones en Honduras Mayo 2012
 
Introduction to database-ER Model
Introduction to database-ER ModelIntroduction to database-ER Model
Introduction to database-ER Model
 
03 administracion de requisitos
03 administracion de requisitos03 administracion de requisitos
03 administracion de requisitos
 
Object Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IObject Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part I
 
Software Engineering an Introduction
Software Engineering an IntroductionSoftware Engineering an Introduction
Software Engineering an Introduction
 
Software Engineering :Behavioral Modelling - II State diagram
Software Engineering :Behavioral Modelling - II State diagramSoftware Engineering :Behavioral Modelling - II State diagram
Software Engineering :Behavioral Modelling - II State diagram
 
Uml Omg Fundamental Certification 2
Uml Omg Fundamental Certification 2Uml Omg Fundamental Certification 2
Uml Omg Fundamental Certification 2
 
Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5Uml Omg Fundamental Certification 5
Uml Omg Fundamental Certification 5
 
Introduction to database-Formal Query language and Relational calculus
Introduction to database-Formal Query language and Relational calculusIntroduction to database-Formal Query language and Relational calculus
Introduction to database-Formal Query language and Relational calculus
 
Omg Fundamental Certification 4
Omg Fundamental Certification 4Omg Fundamental Certification 4
Omg Fundamental Certification 4
 
Object Oriented Programming using C++ Part II
Object Oriented Programming using C++ Part IIObject Oriented Programming using C++ Part II
Object Oriented Programming using C++ Part II
 
Introduction to database-Transaction Concurrency and Recovery
Introduction to database-Transaction Concurrency and RecoveryIntroduction to database-Transaction Concurrency and Recovery
Introduction to database-Transaction Concurrency and Recovery
 
Things to know to improve your willpower
Things to know to improve your willpowerThings to know to improve your willpower
Things to know to improve your willpower
 
Computer Networks Module III
Computer Networks Module IIIComputer Networks Module III
Computer Networks Module III
 
Is your company fully engaged towards innovation?
Is your company fully engaged towards innovation?Is your company fully engaged towards innovation?
Is your company fully engaged towards innovation?
 

Similar to Parallel programming using MPI

Similar to Parallel programming using MPI (20)

Parallel computing(2)
Parallel computing(2)Parallel computing(2)
Parallel computing(2)
 
Lecture9
Lecture9Lecture9
Lecture9
 
MPI Introduction
MPI IntroductionMPI Introduction
MPI Introduction
 
MPI
MPIMPI
MPI
 
Introduction to MPI
Introduction to MPIIntroduction to MPI
Introduction to MPI
 
My ppt hpc u4
My ppt hpc u4My ppt hpc u4
My ppt hpc u4
 
High Performance Computing using MPI
High Performance Computing using MPIHigh Performance Computing using MPI
High Performance Computing using MPI
 
Mpi
Mpi Mpi
Mpi
 
25-MPI-OpenMP.pptx
25-MPI-OpenMP.pptx25-MPI-OpenMP.pptx
25-MPI-OpenMP.pptx
 
Introduction to MPI
Introduction to MPIIntroduction to MPI
Introduction to MPI
 
MPI message passing interface
MPI message passing interfaceMPI message passing interface
MPI message passing interface
 
MPI
MPIMPI
MPI
 
Tutorial on Parallel Computing and Message Passing Model - C2
Tutorial on Parallel Computing and Message Passing Model - C2Tutorial on Parallel Computing and Message Passing Model - C2
Tutorial on Parallel Computing and Message Passing Model - C2
 
Message Passing Interface (MPI)-A means of machine communication
Message Passing Interface (MPI)-A means of machine communicationMessage Passing Interface (MPI)-A means of machine communication
Message Passing Interface (MPI)-A means of machine communication
 
What is [Open] MPI?
What is [Open] MPI?What is [Open] MPI?
What is [Open] MPI?
 
Open MPI
Open MPIOpen MPI
Open MPI
 
MPI Presentation
MPI PresentationMPI Presentation
MPI Presentation
 
Parallel Programming on the ANDC cluster
Parallel Programming on the ANDC clusterParallel Programming on the ANDC cluster
Parallel Programming on the ANDC cluster
 
Parallel and Distributed Computing Chapter 10
Parallel and Distributed Computing Chapter 10Parallel and Distributed Computing Chapter 10
Parallel and Distributed Computing Chapter 10
 
Multicore
MulticoreMulticore
Multicore
 

More from Ajit Nayak

Software Engineering : Software testing
Software Engineering : Software testingSoftware Engineering : Software testing
Software Engineering : Software testingAjit Nayak
 
Software Engineering :Behavioral Modelling - I Sequence diagram
Software Engineering :Behavioral Modelling - I Sequence diagram Software Engineering :Behavioral Modelling - I Sequence diagram
Software Engineering :Behavioral Modelling - I Sequence diagram Ajit Nayak
 
Software Engineering :UML class diagrams
Software Engineering :UML class diagramsSoftware Engineering :UML class diagrams
Software Engineering :UML class diagramsAjit Nayak
 
Software Engineering : OOAD using UML
Software Engineering : OOAD using UMLSoftware Engineering : OOAD using UML
Software Engineering : OOAD using UMLAjit Nayak
 
Software Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & SpecificationSoftware Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & SpecificationAjit Nayak
 
Software Engineering : Process Models
Software Engineering : Process ModelsSoftware Engineering : Process Models
Software Engineering : Process ModelsAjit Nayak
 
Database Programming using SQL
Database Programming using SQLDatabase Programming using SQL
Database Programming using SQLAjit Nayak
 
NS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt IIINS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt IIIAjit Nayak
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using CAjit Nayak
 
Object Oriented Analysis Design using UML
Object Oriented Analysis Design using UMLObject Oriented Analysis Design using UML
Object Oriented Analysis Design using UMLAjit Nayak
 
Operating Systems Part III-Memory Management
Operating Systems Part III-Memory ManagementOperating Systems Part III-Memory Management
Operating Systems Part III-Memory ManagementAjit Nayak
 
Operating Systems Part I-Basics
Operating Systems Part I-BasicsOperating Systems Part I-Basics
Operating Systems Part I-BasicsAjit Nayak
 
Operating Systems Part II-Process Scheduling, Synchronisation & Deadlock
Operating Systems Part II-Process Scheduling, Synchronisation & DeadlockOperating Systems Part II-Process Scheduling, Synchronisation & Deadlock
Operating Systems Part II-Process Scheduling, Synchronisation & DeadlockAjit Nayak
 
Introduction to database-Normalisation
Introduction to database-NormalisationIntroduction to database-Normalisation
Introduction to database-NormalisationAjit Nayak
 
Computer Networks Module II
Computer Networks Module IIComputer Networks Module II
Computer Networks Module IIAjit Nayak
 
Computer Networks Module I
Computer Networks Module IComputer Networks Module I
Computer Networks Module IAjit Nayak
 
Computer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iComputer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iAjit Nayak
 

More from Ajit Nayak (17)

Software Engineering : Software testing
Software Engineering : Software testingSoftware Engineering : Software testing
Software Engineering : Software testing
 
Software Engineering :Behavioral Modelling - I Sequence diagram
Software Engineering :Behavioral Modelling - I Sequence diagram Software Engineering :Behavioral Modelling - I Sequence diagram
Software Engineering :Behavioral Modelling - I Sequence diagram
 
Software Engineering :UML class diagrams
Software Engineering :UML class diagramsSoftware Engineering :UML class diagrams
Software Engineering :UML class diagrams
 
Software Engineering : OOAD using UML
Software Engineering : OOAD using UMLSoftware Engineering : OOAD using UML
Software Engineering : OOAD using UML
 
Software Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & SpecificationSoftware Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & Specification
 
Software Engineering : Process Models
Software Engineering : Process ModelsSoftware Engineering : Process Models
Software Engineering : Process Models
 
Database Programming using SQL
Database Programming using SQLDatabase Programming using SQL
Database Programming using SQL
 
NS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt IIINS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt III
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Object Oriented Analysis Design using UML
Object Oriented Analysis Design using UMLObject Oriented Analysis Design using UML
Object Oriented Analysis Design using UML
 
Operating Systems Part III-Memory Management
Operating Systems Part III-Memory ManagementOperating Systems Part III-Memory Management
Operating Systems Part III-Memory Management
 
Operating Systems Part I-Basics
Operating Systems Part I-BasicsOperating Systems Part I-Basics
Operating Systems Part I-Basics
 
Operating Systems Part II-Process Scheduling, Synchronisation & Deadlock
Operating Systems Part II-Process Scheduling, Synchronisation & DeadlockOperating Systems Part II-Process Scheduling, Synchronisation & Deadlock
Operating Systems Part II-Process Scheduling, Synchronisation & Deadlock
 
Introduction to database-Normalisation
Introduction to database-NormalisationIntroduction to database-Normalisation
Introduction to database-Normalisation
 
Computer Networks Module II
Computer Networks Module IIComputer Networks Module II
Computer Networks Module II
 
Computer Networks Module I
Computer Networks Module IComputer Networks Module I
Computer Networks Module I
 
Computer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module iComputer Fundamentals & Intro to C Programming module i
Computer Fundamentals & Intro to C Programming module i
 

Recently uploaded

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxsomshekarkn64
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 

Recently uploaded (20)

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
lifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptxlifi-technology with integration of IOT.pptx
lifi-technology with integration of IOT.pptx
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 

Parallel programming using MPI

  • 1. Parallel Programming Using MPI Dr. Ajit K Nayak SOA University, Odisha, India
  • 2. Introduction to MPI/Ajit Nayak//2 What is MPI?  The Message Passing Interface, is a standardised and portable message passing system  It is designed by a group of researchers from academia and industry to function on a wide variety of parallel computer  The standard defines the syntax and semantics of core library routines useful to a wide range of users writing portable message-passing programs in Fortran or C
  • 3. Introduction to MPI/Ajit Nayak//3 The Environment  Clustering is used as a Parallel Computing Environment  Clustering is nothing but a network (cluster) of computers configured to work as one computer  This type of environment is cheaper (no more than a general network environment!) than buying an original parallel computer  A Parallel computer (of this kind) may be deployed on the fly
  • 4. Introduction to MPI/Ajit Nayak//4 Building the Environment  Choosing a Network Topology • Network of workstations (NOW)  Choosing an Operating System • Linux  Installing and configuring the Environment • The network setup for master and slaves  Choosing, Installing and Configuring a MPI implementation • mpich-1.2.5 (free downloadable from
  • 5. Introduction to MPI/Ajit Nayak//5 Network of Workstations  The cluster should be viewed as a special, standalone entity.  It should have its own networking facilities that are not shared with other systems.  A cluster should have one Master node that serves as the central focus of the operational aspects of the cluster.  Also it should have some Slave nodes, which will be used as workstations.
  • 6. Introduction to MPI/Ajit Nayak//6 A Pictorial view Slave Nodes Master Node Ethernet Hub Network of Workstations
  • 7. Introduction to MPI/Ajit Nayak//7 Programming using MPI  MPI Assumes that the no of processes created (for a program) are independent of processors (computers) available (for the cluster).  The number of computers for a cluster is decided by the network administrator  The number of processes for a program is decided by the parallel programmer  i.e. no processes are created (or no processors could be attached) during the execution of a program.
  • 8. Introduction to MPI/Ajit Nayak//8 How it works?  Each process is recognized by a unique integer id called rank (0 .. p-1).  Each process is assigned to a computer (Processor) in a round-robin fashion.  Process Zero always runs on Master Process 0 P1 P2 P6 P5 P4 P3
  • 9. Introduction to MPI/Ajit Nayak//9 Process Distribution  4 processors and 8 processes Process 0 Assigned to Master Process 1 Assigned to Slave 1 Process 2 Assigned to Slave 2 Process 3 Assigned to Slave 3 Process 4 Assigned to Master Process 5 Assigned to Slave 1 Process 6 Assigned to Slave 2 Process 7 Assigned to Slave 3
  • 10. Introduction to MPI/Ajit Nayak//10 Process Distribution Master Node Process Distribution (P0, P4) Slave 1 Slave 2 Slave 3 (P1, P5) (P2, P6) (P3) This programs are called SPMD programs!
  • 11. Introduction to MPI/Ajit Nayak//11 MPI Program Structure #include “mpi.h” main(int argc, char** argv){ /*. . . Declarations . . . */ /*. . . Sequential Algorithms . . . */ MPI_Init(&argc, &argv); /*. . . Parallel Algorithm . . . */ MPI_Finalize( ); /* . . . Sequential Algorithm . . . */ } // End of main program !
  • 12. Introduction to MPI/Ajit Nayak//12 Program Structure contd…  int MPI_Init( int *argc, char ***argv ) • It initializes the parallel environment. • It must be called before any other MPI routine  int MPI_Finalize( void ) • It cleans-up all MPI state • After this invocation no MPI routine should be called.
  • 14. Introduction to MPI/Ajit Nayak//14 Sending information  int MPI_Send( void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) • It is used for point-to-point communication, i.e. comm. between a pair of processes. One side sending and other receiving. •The send call blocks until the send buffer can be reclaimed buf initial address of send buffer count number of elements to send datatype datatype of each entry dest rank of destination tag message tag comm communicator
  • 15. Introduction to MPI/Ajit Nayak//15 Receiving information  int MPI_Recv( void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status) • It performs a blocking receive operation buf initial address of send buffer count max number of elements to receive datatype datatype of each entry dest rank of destination tag message tag comm communicator (defines a communication domain) status return status (A structure having information about source, tag and error)
  • 16. Introduction to MPI/Ajit Nayak//16 A First Program  Problem: • Root process receives a greeting message from all other processes. • Each process other than Root send a greeting message to Root process • Root process prints messages received from other processes  We will use blocking send and recv calls to perform this task
  • 17. Introduction to MPI/Ajit Nayak//17 The Complete Program #include<stdio.h> #include<string.h> #include “mpi.h” int main (int argc, char *argv[ ]) { int myRank, numProcs, dest; int Root=0, tag=0; int i; char msg[30]; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &myRank); MPI_Comm_size(MPI_COMM_WORLD, &numProcs); if(myRank !=Root){ strcpy(msg, “Helo World”);
  • 18. Introduction to MPI/Ajit Nayak//18 Greeting Program contd.. MPI_Send(msg, strlen(msg)+1, MPI_CHAR, Root, tag, MPI_COMM_WORLD); } //end if else{ for(i=1; i < numProcs; i++){ MPI_Recv(msg, 30, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status); printf(“%s From process %dn”, msg, i); }//end for } // end else MPI_Finalize( ); } // End of program !
  • 19. Introduction to MPI/Ajit Nayak//19 MPI Datatypes  MPI datatype C datatype  MPI_CHAR signed char  MPI_SHORT signed short int  MPI_INT signed int  MPI_LONG signed long int  MPI_UNSIGNED_CHAR unsigned char  MPI_UNSIGNED_SHORT unsigned short int  MPI_UNSIGNED unsigned int  MPI_UNSIGNED_LONG unsigned long int  MPI_FLOAT float  MPI_DOUBLE double  MPI_LONG_DOUBLE long double
  • 20. Introduction to MPI/Ajit Nayak//20 Where /How to get it done?  Login to a specified user  Open a file in vi editor (vim greeting.c)  Write the source code, save and exit  In command prompt issue following commands To Compile mpicc -o <objFileName> <CFileName> To Execute mpiexec –n <4> ./<objFileName>
  • 21. Introduction to MPI/Ajit Nayak//21 A Sum Program Problem: • To find the sum of n integers on p processes • Where p=n and p=2q
  • 22. Introduction to MPI/Ajit Nayak//22 Problem Understanding 0 1 2 3 4 5 6 7 6+220 1+5 9+130 4 No of processes (p)= 8 q=3 (p=2q) 0+1 2+3 4+5 6+70 2 64
  • 23. Introduction to MPI/Ajit Nayak//23 Program #include<stdio.h> #include<math.h> #include "mpi.h" #define IS_INT(X) ( (X) == (int) (X)) ? 1:0 #define LOG2(X) log10(X)/log10(2) int main(int argc, char *argv[]){ int myRank, numProcs, Root=0; int source, destination, tag=0; int iLevel, level, nextLevel , value, sum=0, ans; float height; //height of the tree MPI_Status status;
  • 24. Introduction to MPI/Ajit Nayak//24 Program contd. MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &myRank); MPI_Comm_size(MPI_COMM_WORLD, &numProcs); height=LOG2(numProcs);
  • 25. Introduction to MPI/Ajit Nayak//25 Program contd. /* if p!=2q */ if(!(IS_INT(height))){ if(MyRank==Root) printf("n Error: Number of processes should be power of 2...n"); MPI_Finalize(); exit(-1); }//end error checking sum=myRank;
  • 26. Introduction to MPI/Ajit Nayak//26 Program contd. //find sender and receiver in each level for(ilevel=0; ilevel < height; ilevel++){ Level = pow(2, ilevel); if((MyRank % level)== 0){ NextLevel = pow(2,(ilevel+1)); if((myRank% nextLevel)==0){ //if receiver source = myRank+Level; MPI_Recv(&value, 1, MPI_INT, Source, tag, MPI_COMM_WORLD, &status); sum += value; }
  • 27. Introduction to MPI/Ajit Nayak//27 Program contd. else{//if sender Destination=MyRank - Level; MPI_Send(&sum, 1, MPI_INT, destination, tag, MPI_COMM_WORLD); }//end of else } } // end of for loop if(MyRank==Root) printf("nnMy Rank is %d and the Final Sum %d nn",myRank,sum); MPI_Finalize(); } //end of the program
  • 29. Introduction to MPI/Ajit Nayak//29 Collective Communications Collective communications transmit data among all processes in a group. Synchronizes processes without passing data. MPI provides the following collective communication Global Communication Functions Broadcast Gather/Scatter etc. Global Reduction Functions Reduce
  • 31. Introduction to MPI/Ajit Nayak//31 Broadcast  int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm); • It broadcasts a message from root to all processes in the group (including itself) Broadcast Processes Elements A0 A0 A0 A0 A0
  • 32. Introduction to MPI/Ajit Nayak//32 Broadcasting How to? . . . int array[100]; int Root=0; MPI_Bcast(array, 100, MPI_INT, Root, MPI_COMM_WORLD); . . .  This call will broadcast an array of 100 integers to all processes  We don‟t require a corresponding receive call in all processes to receive this broadcast
  • 33. Introduction to MPI/Ajit Nayak//33 Gather / Scatter  int MPI_Gather/Scatter(void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvdbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm); Processes Elements Gather Scatter A0 A1 A2 A3 A0 A1 A2 A3
  • 34. Introduction to MPI/Ajit Nayak//34 Gather / Scatter • Each process (including Root) sends the contents of its send buffer to the root process • The root process receives the messages and stores them in rank order • recvcount argument at the root indicates the no of items it receives from each process 100 Proc 0 100 Proc 1 100 Proc 2 Proc 0 100 100 100 Gather Scatter
  • 35. Introduction to MPI/Ajit Nayak//35 Gather / Scatter contd. if (myRank==0) rbuf=(int *) malloc (size*100*sizeof(int)); MPI_Gather(sendArray, 100, MPI_INT, rbuf, 100, MPI_INT, root, MPI_COMM_WORLD); • Similarly in case of Scatter, Root process distributes items to all processes in rank order rbuf = ( int *) malloc (recvCount * sizeof(int)); if (myRank==0) MPI_Scatter(sendbuf, sendCount, MPI_INT, rbuf, recvCount, MPI_INT, root, MPI_COMM_WORLD);
  • 36. Introduction to MPI/Ajit Nayak//36 Gather to All  This is like gather, except all process receives the same result. Processes Elements D0 C0 B0 A0 D0C0B0A0 D0C0B0A0 D0C0B0A0 D0C0B0A0 AllGather  The data received from jth process is placed at jth block of rbuf. int MPI_Allgather(void *sbuf, int scount, MPI_Datatype stype, void *rbuf, int rcount, MPI_Datatype rtype, MPI_COMM_WORLD);
  • 37. Introduction to MPI/Ajit Nayak//37 All to All  Each process sends distinct data to each of the receivers. Processes Elements D3D2D1D0 C3C2C1C0 B3B2B1B0 A3A2A1A0 D3C3B3A3 D2C2B2A2 D1C1B1A1 D0C0B0A0 Alltoall  The jth block sent from process i is received by process j and is placed in the ith block of rbuf. int MPI_Alltoall(void *sbuf, int scount, MPI_Datatype stype, void *rbuf, int rcount, MPI_Datatype rtype, MPI_COMM_WORLD);
  • 38. Introduction to MPI/Ajit Nayak//38 Next Program  Problem • Matrix Vector Product ( A X = B ) Input: A [ 1..m, 1..n ], X[ 1..n ] Output: B[ 1..m ] Algorithm: 1. an 8x8 matrix has been divided into 4 (2x8) sub-matrices. 2. Each sub-matrix is stored in the local memory of each process. 3. A local matrix-vector product is performed at each process. 4. Then the results are assembled from each process onto Root process.
  • 39. Introduction to MPI/Ajit Nayak//39 The Program #include <stdio.h> #include "mpi.h" int main( int argc, char **argv ){ int a[2][8], b[8], cpart[2], ctotal[8]; int rank, size, i, k; MPI_Init( &argc, &argv ); MPI_Comm_rank( MPI_COMM_WORLD, &rank); MPI_Comm_size( MPI_COMM_WORLD, &size );
  • 40. Introduction to MPI/Ajit Nayak//40 Matrix vector Prog contd.. if (size != 4) { printf("Error!:# of processors must be equal to 4n"); printf("Programm aborting....n"); MPI_Abort(MPI_COMM_WORLD, 1); } for (i=0;i<2;i++) // setting values for a[1..2, 1..8] for (k=0;k<8;k++) a[i][k] = rank*(k+1); printf("process %d:n",rank); //printing values of „a‟ for (i=0;i<2;i++){ for (k=0;k<8;k++)printf("%dt",a[i][k]); printf("n"); } printf("n");
  • 41. Introduction to MPI/Ajit Nayak//41 Matrix vector prod. Prog. contd.. for (k=0;k<8;k++) b[k] = k+1; // value of „b‟ for (i=0;i<2;i++){ cpart[i] = 0; for (k=0;k<8;k++) cpart[i] = cpart[i] + a[i][k] * b[k]; } printf("After multiplication process %d:n",rank); for (k=0;k<2;k++) printf("%dt",cpart[k]); printf("n");
  • 42. Introduction to MPI/Ajit Nayak//42 Matrix vector prod Prog contd.. MPI_Gather(cpart, 2, MPI_INT, ctotal, 2, MPI_INT,0, MPI_COMM_WORLD); if(rank==0){ printf("Vector b:n"); for (k=0;k<8;k++) printf("%dt",b[k]); printf(“n result isn"); for (k=0;k<8;k++) printf("%dt",ctotal[k]); printf("n"); } // end of if MPI_Finalize(); } // end of main program
  • 43. Introduction to MPI/Ajit Nayak//43 Matrix vector prod Prog Output process 0: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 process 1: 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 process 2: 2 4 6 8 10 12 14 16 2 4 6 8 10 12 14 16 process 3: 3 6 9 12 15 18 21 24 3 6 9 12 15 18 21 24
  • 44. Introduction to MPI/Ajit Nayak//44 Matrix vector prod Prog Output After multiplication process 0: 0 0 After multiplication process 1: 204 204 After multiplication process 2: 408 408 After multiplication process 3: 612 612 Vector b: 1 2 3 4 5 6 7 8 result vector: 0 0 204 204 408 408 612 612
  • 46. Introduction to MPI/Ajit Nayak//46 Reduce  int MPI Reduce(void* sendbuf, void* recvbuf, int count, MPI Datatype datatype, MPI Op op, int root, MPI Comm comm) Reduce (+) Processes Elements C2A2 B2 C1B1A1 C0B0 A0 C0+ C1+ C2B0+ B1+ B2 A0+ A1+ A2
  • 47. Introduction to MPI/Ajit Nayak//47 Other Reduce functions All Reduce (+) Processes Elements C2A2 B2 C1B1A1 C0B0 A0 C0+ C1+ C2A0+ A1+ A2 B0+ B1+ B2 C0+ C1+ C2B0+ B1+ B2A0+ A1+ A2 C0+ C1+ C2B0+ B1+ B2 A0+ A1+ A2 Reduce Scatter (+) Processes Elements C2A2 B2 C1B1A1 C0B0 A0 C0+ C1+ C2 B0+ B1+ B2 A0+ A1+ A2
  • 48. Introduction to MPI/Ajit Nayak//48 Predefined Operations  Name Meaning  MPI_MAX maximum  MPI_MIN minimum  MPI_SUM sum  MPI_PROD product  MPI_LAND logical and  MPI_BAND bit-wise and  MPI_LOR logical or  MPI_BOR bit-wise or  MPI_LXOR logical xor  MPI_BXOR bit-wise xor  MPI_MAXLOC max value and location  MPI_MINLOC min value and location
  • 49. Introduction to MPI/Ajit Nayak//49 Practice Programs  Find sum of integers in the range(0:500000) using blocking calls (send, recv) • Method 1: (simple)  Divide different ranges in in different processes  Process with rank greater than 0, sends the value of its result to root process. Process 0 calculates and prints final sum • Method 2: (Linear array)  Process n-1 sends its sum to process n-2, n-2 adds its own sum with received sum and sends the resulting sum to n-3.  This process continues till it reaches at process 0.
  • 50. Introduction to MPI/Ajit Nayak//50 Practice Programs • Method 3: (hypercube) use the hypercube algorithm.  Find sum of above range of integers using non- blocking calls (reduce)  Modify Matrix vector product program to work with any size Matrix.
  • 51. Introduction to MPI/Ajit Nayak//51 Other Utility-Calls  double MPI_Wtime(void) • returns a double floating point number of seconds, since some arbitrary point of time in the past. • It can be used to find the execution-time of programs or ‘section of programs’. The time interval can be measured by calling this routine at the beginning and at the end of program/section and subtracting the values returned.
  • 52. Introduction to MPI/Ajit Nayak//52 Other Utility-Calls  int MPI_Barrier (MPI_Comm comm) • MPI_Barrier blocks the calling process until all processes in communicator have entered the function.  int MPI_Sendrecv (void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag, void *recvbuf , int recvcount, MPI_Datatype recvtype, int source, int recvtag, MPI_Comm comm, MPI_Status *status) • performs both a send and a receive. Can be matched with ordinary send and recv. • No deadlock arises
  • 53. Introduction to MPI/Ajit Nayak//53 References The standardization forum: • http://mpi-forum.org/ Software • https://www.mpich.org/ Books  Peter. S. Pacheco – Parallel Programming with MPI, Morgan Kaufmann Publishers  M. Snir, S. Otto, S H-Lederman, D Walker, J Dongarra – MPI: The Complete Reference, The MIT Press, Cambridge, Massachusetts, London
  • 54. Introduction to MPI/Ajit Nayak//54 Thank You