如果不能在进程之间将集合整除,则使用MPI_Scatter [英] use of MPI_Scatter if the set is not divisible among processes

查看:189
本文介绍了如果不能在进程之间将集合整除,则使用MPI_Scatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MPI_Scatter()和MPI_Gather()的程序.该程序将整数N作为输入,并返回从2到N的质数.我创建了一个从2到N的数字的数组,并使用MPI_Scatter将数组拆分为N/(procs数)个元素,然后将它们赋给流程.如果我插入一个数字N,该数字对于进程数(大小")是可分割的,那么一切正常,但是当我输入一个对大小"不可分割的N时,会出现一些错误. 例如:N = 16并且size = 4 => 16/4 = 4,所以N%size == 0,但是当N%size!= 0时,我将遇到错误. 我尝试添加:

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++;

其中rank是当前进程的等级,为根进程增加一个元素.但是它仍然无法正常工作.我怎么解决这个问题? 预先谢谢你.

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.

推荐答案

如果必须分配数字数组,并且不能将其均等分配给所有进程,请使用

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

这篇关于如果不能在进程之间将集合整除,则使用MPI_Scatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆