MPI Source and Dest arguments

Hi,
I am using a simple program which is using MPI_Send(...) and MPI_Recv(....) commands:

1
2
3
4
5
6
7
8

// task 0 sends to task 1 and waits to receive a return message
   if (rank == 0) {
     dest = 1;//‘dest’ means rank of destination
     source = 1;
     MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
     MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
     } 

The comment says that:

// task 0 sends to task 1 and waits to receive a return message

but in MPI_Recv(...), source is "1".

Should the source is not "0" if task 0 is sending.

Somebody please guide me.

Zulfi.
Last edited on
Hi,

I understood that code. I was not looking at the "else" part.

The complete if statement is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (rank == 0) {
     dest = 1;//‘dest’ means rank of destination
     source = 1;
     MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
     MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
     } 

   // task 1 waits for task 0 message then returns a message
   else if (rank == 1) {
     dest = 0;
     source = 0;
     MPI_Recv(&inmsg, 1, MPI_CHAR, source, tag, MPI_COMM_WORLD, &Stat);
     MPI_Send(&outmsg, 1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
     }


Zulfi.
@zak100,
Did you know that you can use MPI_Sendrecv rather than having separate calls?

Like so ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "mpi.h"
#include <iostream>
#include <cstring>
using namespace std;

int main( int argc, char* argv[] )
{
   const int MAXSIZE = 30;
   char sendbuffer[MAXSIZE];
   char recvbuffer[MAXSIZE];
   int rank, size;
   MPI_Status stat;
 
   MPI_Init( &argc, &argv );
   MPI_Comm_rank( MPI_COMM_WORLD, &rank );
   MPI_Comm_size( MPI_COMM_WORLD, &size );

   if ( rank == 0 ) strcpy( sendbuffer, "Root processor awake" );
   if ( rank == 1 ) strcpy( sendbuffer, "Slave processor waiting" );
   MPI_Sendrecv( &sendbuffer, MAXSIZE, MPI_CHAR, 1 - rank, 1,
                 &recvbuffer, MAXSIZE, MPI_CHAR, 1 - rank, 1,
                 MPI_COMM_WORLD, &stat );                  
 
   cout << "Processor " << rank << " sends: " << sendbuffer << '\n';
   cout << "Processor " << rank << " receives: " << recvbuffer << '\n';
   MPI_Finalize();
}
Last edited on
Topic archived. No new replies allowed.