0
votes

This may be a trivial thing but:

May the size of an underlying array be longer than the count argument send along with the buffer pointer in a MPI_Send( ... ) call?

As for MPI_Recv( ... ) I've found sources that clearly state that this is legitimate (i.e., the buffer is larger than the message).

Plus, it seems to be OK when I compile and run the below program

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mpi.h>

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

  MPI_Init(&argc, &argv);

  int world_rank;
  MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
  int world_size;
  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
  if( world_size != 2 ) {
    printf("You're supposed to use exactly 2 processes, dude!\n");
    MPI_Abort( MPI_COMM_WORLD, 0 );
  }

  int *ids = (int*)( malloc( 10*sizeof(int) ) );

  if( world_rank == 0 ) {
    for( i=0; i<10; i++)
      ids[i]=i+1;
    MPI_Send( ids, 5, MPI_INT, 1, 0, MPI_COMM_WORLD);
  }else{
    for( i=0; i<10; i++)
      ids[i]=20-i;
    MPI_Recv( ids, 5, MPI_INT, 0, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
  }
  for( i=0; i<10; i++)
    printf("me %d   %d %d\n",world_rank,i,ids[i]);

  free(ids);

  MPI_Finalize();

  return 0;
}

because I get following output with no error messages:

me 0   0 1
me 0   1 2
me 0   2 3
me 0   3 4
me 0   4 5
me 0   5 6
me 0   6 7
me 0   7 8
me 0   8 9
me 0   9 10
me 1   0 1
me 1   1 2
me 1   2 3
me 1   3 4
me 1   4 5
me 1   5 15
me 1   6 14
me 1   7 13
me 1   8 12
me 1   9 11

But I'd like to have reassurance because I'm still a newbie...

1
It's not only safe: it's something that is usual to do all the time. For example, when you have to send/recv ghost data on the boundary of an array. Or you send a row of a 2D array. Furthermore MPI has support to send/recv sparse buffers with MPI datatypes - for example, sending a column of a 2D array. So don't worry - your code is OK.Sigi

1 Answers

3
votes

Since you are passing a buffer to MPI_Send and telling it how big the buffer is, the buffer is whatever size you say it is. If you want to cut a big buffer into smaller buffers, that's your business. MPI_Send has no way to tell how you managed to allocate or create the buffer you're passing to it, and it doesn't care.