PDC 5 PDF
PDC 5 PDF
REGN NO-18BCE2015
SLOT-L49+50
EX 5 ( MPI-II)
SCENARIO – I
Implement a MPI program to demonstrate a simple MPI broadcast.
MPI is a message passing interface system which facilitates the processors
which have distributed memory architecture to communicate and send &
receive messages.
Use MPI_Bcast(buffer,count,MPI_INT,source,MPI_COMM_WORLD) method.
ALGORITHM:
• Broadcast the message using MPI_Bcast from the process with rank
"root" to all other processes of the communicator.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
source=0;
count=4;
if(myid == source){
for(i=0;i<count;i++) buffer[i]=i;
}
MPI_Bcast(buffer,count,MPI_INT,source,MPI_COMM_WORLD);
for(i=0;i<count;i++)
printf("%d ",buffer[i]);
printf("\n");
MPI_Finalize();
}
RESULTS:
SCENARIO – II
Implement a MPI program to demonstrate a simple Send and Receive.
MPI is a message passing interface system which facilitates the processors
which have distributed memory architecture to communicate and send &
receive messages.
Use the following methods:
MPI_Send(&buffer,count,MPI_INT,destination,tag,MPI_COMM_WORLD);
MPI_Recv(&buffer,count,MPI_INT,source,tag,MPI_COMM_WORLD,&status
);
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
NAME-SHIVAM AHUJA
REGN NO-18BCE2015
SLOT-L49+50
#include <math.h>
int main(argc,argv)
int argc;
char *argv[];
{
int myid, numprocs;
int tag,source,destination,count;
int buffer;
MPI_Status status;
MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
tag=1234;
source=0;
destination=1;
count=1;
if(myid == source){
buffer=5678;
MPI_Send(&buffer,count,MPI_INT,destination,tag,MPI_COMM_WORLD);
printf("processor %d sent %d\n",myid,buffer);
}
if(myid == destination){
MPI_Recv(&buffer,count,MPI_INT,source,tag,MPI_COMM_WORLD,&status
);
printf("processor %d got %d\n",myid,buffer);
}
MPI_Finalize();
}
NAME-SHIVAM AHUJA
REGN NO-18BCE2015
SLOT-L49+50
OUTPUT:
RESULTS:
SCENARIO – III
ALGORITHM:
arguments.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
printf(" Hello from c process: %d Numprocs is %d\n",myid,numprocs);
mytag=123;
if(myid == 0) {
j=200;
icount=1;
NAME-SHIVAM AHUJA
REGN NO-18BCE2015
SLOT-L49+50
ierr=MPI_Send(&j,icount,MPI_INT,1,mytag,MPI_COMM_WORLD);
}
if(myid == 1)
{
ierr=MPI_Probe(0,mytag,MPI_COMM_WORLD,&status);
ierr=MPI_Get_count(&status,MPI_INT,&icount);
i=(int*)malloc(icount*sizeof(int));
printf("getting %d\n",icount);
ierr =
MPI_Recv(i,icount,MPI_INT,0,mytag,MPI_COMM_WORLD,&status);
printf("i= "); for(j=0;j<icount;j++)
printf("%d ",i[j]); printf("\n");
}
MPI_Finalize();
}
EXECUTION:
RESULTS:
The rank and size of the processor along with the message size is printed.