【问题标题】:MPI min function in reduce减少中的 MPI min 函数
【发布时间】:2013-03-16 01:06:33
【问题描述】:

我试图在我的整数数组中找到最小的数字,但是它返回 0。

import mpi.*;
import java.util.Random;

class AddIntSR
{
public static void main(String[] params) throws Exception
{
    MPI.Init(params);
    int me = MPI.COMM_WORLD.Rank();
    int size = MPI.COMM_WORLD.Size();
    final int CHUNKSIZE = 1;
    final int ROOT = 0;
    Random rg = new Random();

    int [] bigBuf = new int[CHUNKSIZE *size];
    int [] smallBuf = new int[CHUNKSIZE];
    int [] minBuf = new int[1];

    int localTotal = 0;

    if (me == ROOT)
    {
        for(int i = 0; i< bigBuf.length; i++)
            bigBuf[i] = rg.nextInt(10);

        for(int i = 0; i< bigBuf.length; i++)
            System.out.println("bigBuf "+bigBuf[i]);    
    }

    MPI.COMM_WORLD.Scatter(bigBuf,0,CHUNKSIZE,MPI.INT,smallBuf,0,CHUNKSIZE,MPI.INT,ROOT);

    if(me!= ROOT)
    {
        System.out.println("smallBuf "+me+ ": "+smallBuf[0]);
        for(int i = 0; i < smallBuf.length; i++)
            localTotal += smallBuf[i];
    }

    MPI.COMM_WORLD.Reduce(new int[]{localTotal},0,bigBuf,0,1,MPI.INT,MPI.MAX,ROOT);
    MPI.COMM_WORLD.Reduce(new int[]{localTotal},0,minBuf,0,1,MPI.INT,MPI.MIN,ROOT);

    if(me == ROOT)
    {   
        System.out.println(bigBuf[0]);
        System.out.println(minBuf[0]);
    }
}
}

我不确定为什么它不起作用。最大功能似乎工作正常。 另外,我如何能够访问发送到处理器 0 的整数,以便将其包含在最小/最大比较中?

谢谢。

【问题讨论】:

    标签: mpi min reduce


    【解决方案1】:

    MIN 的减少总是导致0,因为localTotal 在等级ROOT 中总是0,这确实是最小值。

    在MPI.COMM_WORLD.Scatter 调用之后,包括ROOT 在内的所有进程都将在其smallBuf 中拥有一条数据。因此,您应该删除以下条件,即:

    if(me!= ROOT)
    {
        System.out.println("smallBuf "+me+ ": "+smallBuf[0]);
        for(int i = 0; i < smallBuf.length; i++)
            localTotal += smallBuf[i];
    }
    

    应该变成:

    System.out.println("smallBuf "+me+ ": "+smallBuf[0]);
    for(int i = 0; i < smallBuf.length; i++)
        localTotal += smallBuf[i];
    

    【讨论】:

    • 非常感谢!非常感谢!
    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 2021-01-12
    • 2018-12-01
    • 2014-08-04
    • 1970-01-01
    • 2015-01-10
    相关资源
    最近更新 更多