use of MPI_Scatter if the set is not divisible amo

2019-06-21 19:39发布

问题:

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.

回答1:

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



标签: c mpi