how to use MPI_Scatterv() properly

I am having a problem using MPI_Scatterv in a parallel program. Here is how it is defined :

int MPI_Scatterv(const void *sendbuf, const int *sendcounts, const int *displs, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)

The way I understood it , the difference between MPI_Scatterv and MPI_Scatter is the fact that in MPI_Scatterv segments do not have to be of the same length and they don't have to be continuous (gaps in memory are allowed). The part I do not understand is if the recvbuf can be an array of different size for each process then what should be used for recvcount. Let's say I want to send 5 elements of sendbuf to process 0 and 15 to process 1. What should the value of recvcount be?

1 Answer

Each process has to call MPI_Scatterv with the right recvcount. You can pass a variable whose value depends on the rank of each process.

For example:

int recvcount = (rank == 0) ? 5 : 15;
MPI_Scatterv( sendbuf, sendcounts, displs, sendtype, recvbuf, recvcount, recvtype, 0, MPI_COMM_WORLD );

Process with rank 0 calls MPI_Scatterv with a recvcount of 5 whereas process 1 passes a count of 15.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like