0

I'm using MPI in this code, a ring of pass of messages between processes, process n receives from n-1 and sends to n+1.

#include "mpi.h"
#include <stdio.h>

int main(argc,argv)
int argc;
char **argv;
{

  int MyProc, size,tag=1;
  char msg='A', msg_recpt;
  MPI_Status status;

  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &MyProc);
  MPI_Comm_size(MPI_COMM_WORLD, &size);

  printf("Process # %d started \n", MyProc);
  MPI_Barrier(MPI_COMM_WORLD);


  if(MyProc == 0){
      printf("Sending:  Proc #%d  to Proc #%d\n",MyProc,(MyProc +1 )%size);
      MPI_Send(&msg, 1, MPI_CHAR, (MyProc +1 )%size,tag, MPI_COMM_WORLD);
      MPI_Recv(&msg_recpt, 1, MPI_CHAR, (MyProc + size - 1)%size, tag, MPI_COMM_WORLD, &status);
      printf("Receving: Proc #%d from Proc #%d\n",MyProc,(MyProc + size - 1)%size);
  }else{
    printf("Receving: Proc #%d de Proc #%d\n",MyProc,(MyProc + size - 1)%size);
    MPI_Recv(&msg_recpt, 1, MPI_CHAR, (MyProc + size - 1)%size, tag, MPI_COMM_WORLD, &status);
    MPI_Send(&msg, 1, MPI_CHAR, (MyProc +1 )%size,tag, MPI_COMM_WORLD);
    printf("Sending:  Proc #%d  from Proc #%d\n",MyProc,(MyProc +1 )%size);
  }

  printf("Finishing proc %d\n", MyProc);

  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Finalize();
}

and I get this output:

Process # 0 started 
Process # 1 started 
Process # 2 started 
Sending:  Proc #0  to Proc #1
Receving: Proc #0 from Proc #2
Finishing proc 0
Receving: Proc #1 de Proc #0
Sending:  Proc #1  from Proc #2
Finishing proc 1
Receving: Proc #2 de Proc #1
Sending:  Proc #2  from Proc #0
Finishing proc 2

Something is not correct because the process 0 receiving message appears before the process2 sending message, I thought that process0 should wait until process2 sends but I don't know what is happening.

weatherman
  • 100
  • 1
  • 12
AFS
  • 1,433
  • 6
  • 28
  • 52
  • 1
    How did you run your application? Have you sequenced your outputs? Because the outputs can be interleaved without the additional flag to mpirun. – Matthieu Brucher Nov 17 '18 at 12:05
  • I run the application this way `mpirun -np 3 ./app` – AFS Nov 17 '18 at 12:07
  • Please [edit] such essential info into the question. Also, wow, people are still using even pre-ISO C in new code? Is there any defensible reason for that? I thought it was bad enough that so many can't move on from C89/C90, but... – underscore_d Nov 17 '18 at 12:08
  • Possible duplicate of [Ordering Output in MPI](https://stackoverflow.com/questions/5305061/ordering-output-in-mpi) – Gilles Gouaillardet Nov 17 '18 at 13:33

1 Answers1

0

You can't assume that because stdout gave you an order, that's the order of the operations.

The only thing you can say is that on each rank, the order is respected, but the rest is not. How MPI will agglomerate stdout from each rank is implementation dependent (I think intel MPI has a flag I can't find anymore that will slow down execution).

If you want to make sure of the order of the operations, you have to use something else than stdout, have a look at MPE, vampir...

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
  • Why stdout doesn't give the operations order? – AFS Nov 17 '18 at 12:55
  • Because stdout from mpirun is not stdout from your individual processes. They get agglomerated by mpirun and turned into the stdout. But the order is not specified so that performance is maximized. – Matthieu Brucher Nov 17 '18 at 13:07