I'm very new to MPI and I'm just writing a basic send and receive module in which I'm sending 12 months to n number of processors and receiving each Month and printing its values. So I'm able to send the values correctly and also able to receive all of them but my program is stuck i.e It is not printing "After program is complete" at the last. Can you please help.
#include <stdio.h>
#include <string.h>
#include "mpi.h"
#include<math.h>
int main(int argc, char* argv[]){
int my_rank; /* rank of process */
int p; /* number of processes */
int tag=0; /* tag for messages */
MPI_Status status ; /* return status for receive */
int i;
int pro;
/* start up MPI */
MPI_Init(&argc, &argv);
// find out process rank
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
//find out number of processes
MPI_Comm_size(MPI_COMM_WORLD, &p);
if (my_rank==0)
{
for(i=1;i<=12;i++)
{
pro = (i-1)%p;
MPI_Send(&i, 1, MPI_INT,pro, tag, MPI_COMM_WORLD);
printf("Value of Processor is %d Month %d\n",pro,i);
}
}
//else{
for(int n=0;n<=p;n++)
{
MPI_Recv(&i, 1, MPI_INT, 0, tag, MPI_COMM_WORLD, &status);
printf("My Month is %d and rank is %d\n",i,my_rank);
}
//}
MPI_Barrier(MPI_COMM_WORLD);
if(my_rank==0)
{
printf("After program is complete\n");
}
/* shut down MPI */
MPI_Finalize();
return 0;
}
Below is the output:
Value of Processor is 0 Month 1
Value of Processor is 1 Month 2
Value of Processor is 2 Month 3
Value of Processor is 3 Month 4
Value of Processor is 4 Month 5
Value of Processor is 0 Month 6
Value of Processor is 1 Month 7
Value of Processor is 2 Month 8
Value of Processor is 3 Month 9
Value of Processor is 4 Month 10
Value of Processor is 0 Month 11
My Month is 2 and rank is 1
My Month is 7 and rank is 1
My Month is 3 and rank is 2
My Month is 8 and rank is 2
Value of Processor is 1 Month 12
My Month is 1 and rank is 0
My Month is 6 and rank is 0
My Month is 11 and rank is 0
My Month is 12 and rank is 1
My Month is 4 and rank is 3
My Month is 9 and rank is 3
My Month is 5 and rank is 4
My Month is 10 and rank is 4