Hi all
I wrote a code which can sent 10 numbers from first process to another process, then the other process will resent the same data to first process.
It is an example code for understanding MPI Send and MPI Recieve functions
It is an example code for understanding MPI Send and MPI Recieve functions
/*
Name : mpi_sent_recieve.c
Function: Sent ten number to one id ,resent the data from the reciver node to sent node
*/
#include <mpi.h>
#include <stdio.h>
int main(int argc,char *argv[])
{
//Input array
int input[10]={1,2,3,4,5,6,7,8,9,10};
int output1[10];
int output2[10];
int numprocs;
int myid;
int i;
MPI_Status stat;
//MPI Init
MPI_Init(&argc,&argv);
//MPI number of process
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
//Getting ID
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if(myid == 0)
{
//Sent 10 number to 1
printf("Senting data from ID0 to ID1\n");
MPI_Send(input,10,MPI_INT,1,0,MPI_COMM_WORLD);
MPI_Recv(output2,10,MPI_INT,1,1,MPI_COMM_WORLD,&stat);
printf("%d:: ID0 Recive data from ID1 %d %d %d %d %d %d %d %d %d %d\n",myid,output2[0],output2[1],output2[2],output2[3],output2[4],output2[5],output2[6],output2[7],output2[8],output2[9]);
}
else if(myid ==1)
{
MPI_Recv(output1,10,MPI_INT,0,0,MPI_COMM_WORLD,&stat);
printf("%d:: ID1 Recieve data %d %d %d %d %d %d %d %d %d %d\n",myid,output1[0],output1[1],output1[2],output1[3],output1[4],output1[5],output1[6],output1[7],output1[8],output1[9]);
printf("Resending data back to ID0\n");
MPI_Send(output1,10,MPI_INT,0,1,MPI_COMM_WORLD);
}
MPI_Finalize();
return 0;
}
0 comments:
Post a Comment