Send/Recv

Introduction

The MPI_Send primitive sends data stored in a buffer to another process rank. Depending on the size of the message, the call will be blocking or non-blocking. The syntax is as follows:

int MPI_Send(const void *buf, 
	int count, 
	MPI_Datatype datatype, 
	int dest, 
	int tag,
	MPI_Comm comm)

The MPI_Isend primitive performs a non-blocking send. The syntax is as follows:

int MPI_Isend(const void *buf, 
	int count, 
	MPI_Datatype datatype, 
	int dest, 
	int tag,
	MPI_Comm comm, 
	MPI_Request * request)

The MPI_Recv primitive receives data from another process rank. The call is blocking. The syntax is as follows:

int MPI_Recv(void *buf, 
	int count, 
	MPI_Datatype datatype, 
	int source, 
	int tag,
	MPI_Comm comm, 
	MPI_Status * status)

Connection to Modules

  • This module is particularly useful for the communication module.

Files Required

Part 1: Communication with Send and Recv

In this exercise, we assume that there are 2 process ranks.

  • Rank 0 will store in a buffer a value of 100.
  • Rank 1 will store in a buffer a value of 200.
  • Rank 0 will send its value in its buffer to rank 1.
  • Rank 1 will send its value in its buffer to rank 0.
  • The ranks will print the values they have received.
  • In this exercise, we will only use MPI_Send and MPI_Recv.
  • To get started, use the file send_recv_starter.c.

Figure 1 illustrates communication between rank 0 and 1.

Figure 1: Example of sending the value 100 from rank 0 to rank 1 and sending the value 200 from rank 1 to rank 0.

A code snippet from send_recv_starter.c is shown below. Write the code to exchange the values of myValue between the two ranks.

//Initialize values at each rank
  if(my_rank==0)
  {
    myValue=100;
  }
  else
  {
    myValue=200;
  }

  //Write code here
  
  printf("\n\nRank: %d, my buffer: %d", my_rank, myBuffer);  

Part 2: Replacing Send with Isend

Using send_recv_starter.c, write the same program as outlined above, except use MPI_Isend instead of MPI_Send.

Previous
Next