70 likes | 128 Views
PP Lab. MPI programming III. Program 1. Write a program in which every process sends a number to process 0 and process 0 prints the numbers. 1. 2. 0. n-1. Function to be used. MPI_Send : Performs a basic send Synopsis: # include " mpi.h "
E N D
PP Lab MPI programming III
Program 1 Write a program in which every process sends a number to process 0 and process 0 prints the numbers. 1 2 0 n-1
Function to be used • MPI_Send: Performs a basic send • Synopsis: #include "mpi.h" intMPI_Send( void *buf, int count, MPI_Datatypedatatype, intdest, int tag, MPI_Commcomm ) • Input Parameters: • buf: initial address of send buffer (choice) • count: number of elements in send buffer (nonnegative integer) • datatype: datatype of each send buffer element (handle) • dest: rank of destination (integer) • tag: message tag (integer) • comm:communicator(handle)
Function to be used • MPI_Recv: Basic receive • Synopsis: #include "mpi.h" intMPI_Recv( void *buf, int count, MPI_Datatypedatatype, int source, int tag, MPI_Commcomm, MPI_Status *status ) • Output Parameters: • buf initial address of receive buffer (choice) • status status object (Status) • Input Parameters: • count maximum number of elements in receive buffer (integer) • datatypedatatype of each receive buffer element (handle) • source rank of source (integer) • tag message tag (integer) • commcommunicator (handle)
MPI_Status structure • Public Attributes: • intMPI_ERROR • intMPI_SOURCE • intMPI_TAG • intreserved [2] • intsize
Code #include <stdio.h> #include "mpi.h” main(intargc, char **argv) {inti, myrank, size, message, recv_buf, dest, tag, count; MPI_Status status; MPI_Init(&argc,&argv); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); //tell me my rank MPI_Comm_size(MPI_COMM_WORLD,&size); //tell me the size of cluster message=myrank*8; dest=0; count=1; tag=0; if(myrank!= 0) MPI_Send(&message,count,MPI_INT,dest,tag,MPI_COMM_WORLD); else {for (i=1;i<size;i++) {MPI_Recv(&recv_buf,count,MPI_INT,i,tag,MPI_COMM_WORLD, &status); printf( “process %d sent %d\n",i,recv_buf);} } MPI_Finalize();}
Assignment Write a program in which process 0 sends an array of five numbers to process 1.