0

I am trying to let one process handle all the printtf operations so the printing happens in the order i want. I am trying to store the data that process 1 generate and let process 0 print the data that both process 0 and process 1 generate but i only get what process 0 is generating.

Here is the relevant part of the code

 char str3[33]; char str4[66]; MPI_Barrier(MPI_COMM_WORLD); while (count<5){ MPI_Recv(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD,&status); char str[30]; if(my_rank==0) sprintf(str,"Process %d received the count\n",my_rank); if(my_rank==1) sprintf(str3,"Process %d received the count\n",my_rank); count++; char str2[66]; if (my_rank==0) sprintf(str2,"Process %d incremented the count(%d) and sent it back to process %d\n",my_rank,count,(my_rank+1)%2); if (my_rank==1) sprintf(str4,"Process %d incremented the count(%d) and sent it back to process %d\n",my_rank,count,(my_rank+1)%2); if(my_rank==0){ printf(str3); printf (str4); printf(str); printf(str2); memset(str3,'\0',sizeof(str3)); memset(str4,'\0',sizeof(str4)); memset(str,'\0',sizeof(str)); memset(str2,'\0',sizeof(str2)); } MPI_Send(&count,1,MPI_INT,(my_rank+1)%2,tag,MPI_COMM_WORLD); } 
2
  • Why is the MPI_Recv() operation before the MPI_Send() ? Does your program terminate properly ? Because i would expect a deadlock since MPI_Recv() is blocking until it receives a message that will never be sent... Commented Apr 13, 2014 at 14:41
  • yes it does terminate properly. Commented Apr 13, 2014 at 15:00

1 Answer 1

1

First, note that, as pointed out by @grancis, your code appears to be flawed owing to a deadlock, since both processes 0 and 1 block in MPI_recv before sending. May be you are sending data before entering the code snippet shown in the question, allowing the code to continue.

Anyway, the problem is that the buffers str3 and str4 are modified by process 1, so that when process 0 tries to print them it can not obviously print anything different from what these buffers originally contained (which is uninitialized memory in your code before the first iteration, and zero in the subsequent iterations since you used memset). Remember that in MPI processes DO NOT share memory.

If you do not want process 1 to print information, then process 1 must send its information to process 0 (through ordinary MPI_Send/MPI_Recv in MPI1 or one sided communication in MPI2 or MPI3), and only then process 0 can print that information.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks i got what you said and it worked. You're also correct, i'm sending data the first time before the loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.