A non-standard-conforming implementation using MPI_Scatterv
:
int arr[10];
const int counts[] = {4, 4, 4};
const int displs[] = {0, 3, 6};
int recv_buff[4];
MPI_Scatterv(arr, counts, displs, MPI_INT, recv_buff,
4, MPI_INT, 0, MPI_COMM_WORLD);
displs
are just simple offsets and are specified as follows:
Original array arr[10]:
[ 0 1 2 3 4 5 6 7 8 9 ]
^ displs[0]
^ displs[1]
^ displs[2]
This is not guaranteed to work because subarrays overlap:
The specification of counts, types, and displacements should not cause any location on the root
to be read more than once.
As Gilles Gouaillardet noted in comments, to avoid overlaps you can call MPI_Scatterv
twice:
int arr[10];
const int counts1[] = {4, 0, 4};
const int counts2[] = {0, 4, 0};
const int displs[] = {0, 3, 6};
int recv_buff[4];
MPI_Scatterv(arr, counts1, displs, MPI_INT, recv_buff,
counts1[rank], MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatterv(arr, counts2, displs, MPI_INT, recv_buff,
counts2[rank], MPI_INT, 0, MPI_COMM_WORLD);
Alternatively, you can use plain MPI_Send
s/MPI_Get
s.