I am a newbie in MPI and I have a homework, I am not asking for you to solve it, I only need a hint on why my program is malfunctioning.
Here is the problem
Write a MPI C program that simulates a ping pong table game. 2 processes should only be used. Processes use MPI_Send and MPI_Recv to continually bounce messages off to each other, a specific number of times. The message is formed of an integer count variable that is incremented by each process before being sent. The count variable is initialized to zero prior to starting the game.
Output
A text file called ping_pong_output.txt formed as the following example: if count = 5
Process 0 started the game and initialized the count Process 0 incremented the count ( 1 ) and sent it to process 1
Process received the count Process 0 incremented the count ( 2 ) & sent it back to process 0
Process received the count Process 0 incremented the count ( 3 ) & sent it back to process 1
Process received the count Process 0 incremented the count ( 4 ) & sent it back to process 0
Process received the count Process 0 incremented the count ( 5 ) & sent it back to process 1
Note the following:
- The processes take turns being the sender and receiver (need to use %)
- The program should allow only 2 processes to play the game
The code I wrote
#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
#include <time.h>
int main(int argc, char * argv[]){
int size=0,my_rank=0,tag=1,count;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&size);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
MPI_Barrier(MPI_COMM_WORLD);
if (my_rank==0){
count=0;
count++;
printf("Process 0 started the game and initialized the count\nProcess 0 incremented the count and sent it to process 1\n");
MPI_Send(&count,1,MPI_INT,1,tag,MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
while (count<=5){
MPI_Barrier(MPI_COMM_WORLD);
MPI_Recv(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD,&status);
printf("Process %d received the count\n",my_rank);
count++;
MPI_Send(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD);
printf("process %d incremented the count (%d) & sent it back to process %d\n",my_rank,count,(my_rank+1)%2);
}
MPI_Finalize();
return 0;
}
The output I got is :
Process 1 received the count
process 1 incremented the count (2) & sent it back to process 0
Process 1 received the count
process 1 incremented the count (4) & sent it back to process 0
Process 1 received the count
process 1 incremented the count (6) & sent it back to process 0
Process 0 started the game and initialized the count
Process 0 incremented the count and sent it to process 1
Process 0 received the count
process 0 incremented the count (3) & sent it back to process 1
Process 0 received the count
process 0 incremented the count (5) & sent it back to process 1
Process 0 received the count
process 0 incremented the count (7) & sent it back to process 1
I don't understand why process 1 is running first, the loop is even running before the if statement. All those MPI_Barrier are probably useless but they came out of desperation. Any help is appreciated.