2013-03-15 165 views
0

我试图找到我的整数数组的最小数目,但是,它返回0。MPI分钟功能降低

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的整数,因此它包含在最小/最大比较中?

谢谢。

回答

1

MIN还原总是导致0因为localTotal总是0中排名ROOT这的确是最小值。

在拨打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]; 
+0

非常感谢您!非常感激! – user1861861 2013-03-15 17:39:38