use of MPI_Scatter if the set is not divisible among processes
Asked Answered
B

1

11

I have a program which uses MPI_Scatter() and MPI_Gather(). The program take as input an integer N and return the prime number from 2 to N. I create an array with the number from 2 to N and with the MPI_Scatter split the array into N/(number of procs) elements, then give them to the processes. If I insert a number N which is divisible for the number of processes ('size') everything works fine, but when I input a N not divisible for 'size' I'll have some errors. For example: N=16 and size=4 => 16/4= 4, so N%size==0, but when N%size!=0 I will have errors. I tried to add:

div = N/size;
if (N%size != 0)
    if (rank == 0)
        div++;

where rank is the rank of the current process, to give one more element to the root process. But it is still not working. How can I solve this problem? Thank you in advance.

Basanite answered 24/1, 2014 at 12:8 Comment(0)
A
14

If you have to distribute an array of numbers, that can not be equally distributed to all processes, use MPI_Scatterv instead.

Arnold answered 24/1, 2014 at 12:14 Comment(5)
Since recvcount is an integer rather than an integer array, does this imply that all receiving processes can only receive the same number of recvtype data? @ArnoldKaykaya
@Kaykaya No, the recvcount is specified by the receiving rank (r), it has to match sendcount(r) (if the types are equal). Each rank just receives one part. Thus a scalar integer (that can vary from process to process for the scatterv operation) is sufficient.Arnold
@Arnold So with the standard MPI_Scatter, say MPI_Scatter(sendbuf, 100, MPI_INT, rbuf, 100, MPI_INT, root, comm);, all the processes execute this statement regardless. With the MPI_Scatterv, MPI_Scatterv(sendbuf, scounts, displs, MPI_INT, rbuf, 100, MPI_INT, root, comm);, if all processes execute the same statement, they'd all receive 100 elements and this could potentially be different from scounts[rank], aren't they?Kaykaya
@Arnold Can you please take a look at the two examples here on this page It seems to me that all processes receive the same number of data elements defined with int recvcount.Kaykaya
@Kaykaya the first example uses the same count, but only because each process uses a different datatype with different size for each process. The second example uses the same size and just shows the use with different displacements on the sending side. An example with different recvcounts is offered in https://mcmap.net/q/1017400/-how-to-use-mpi_scatterv-properlyArnold

© 2022 - 2024 — McMap. All rights reserved.