I’m trying to get an intution about how MPI works. Therefore I started with a little example:
...
MPI_Comm_rank(MPI_COMM_WORLD, &tid);
MPI_Comm_size(MPI_COMM_WORLD, &nthreads);
int message = 2;
if(tid != 0)
{
MPI_Recv(&rec, 1, MPI_INT, tid-1, 0, MPI_COMM_WORLD, &status);
printf("Process %i receive %i\n", tid, rec);
}
if(tid != nthreads-1)
{
message++;
printf("Process %i sends %i\n", tid, message);
MPI_Send(&message, 1, MPI_INT, tid+1, 0, MPI_COMM_WORLD);
}
...
That works fine despite the fact that message does seem to increase above 3. Why is that?
mpirun -np Ncommand startsNMPI processes, not one process withNthreads. In each process you have variablemessageand each process has its own variable set to 2. Next 0th process sends itsmessage == 3to 1st process. After it, 1st process sends his ownmessage == 3and 2nd catches it…