Skip to content

Getting number of total MPI bytes transferred by a program

An answer to this question on Stack Overflow.

Question

Is there any way in MPI to get the total number of bytes transferred by my entire MPI program in C?

Answer

Not that I know of directly, but you could adapt the following code to your purposes:

uint64_t bytes_recv = 0;
void CommRecv(MyObject* a){
  MPI_Status status;
  MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
  int msg_size;
  MPI_Get_count(&status, MPI_BYTE, &msg_size);
  bytes_recv += msg_size;
  // Allocate a buffer to hold the incoming data
  char* buf = (char*)malloc(msg_size);
  assert(buf!=NULL);
  MPI_Recv(buf, msg_size, MPI_BYTE, from, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  //Do stuff
  free(buf);
}

The point here is to wrap the standard MPI communication functions with functions that keep track of data transfer statistics. Internally, these functions use MPI_Get_count() to retrieve the size of the incoming message. This is then added to a global variable that tracks communication over all of the wrapped MPI functions.

At the end of the program you can accumulate each instance's global variables on the master process.