-2

对于数组{ 24 48 27 1 5 30 40 27 31 36 29 45 45 43 10 15 50 35 40 47 }

index  0- 3 ---> processor 0,  
index  4- 7 ---> processor 1,
index  8-11 ---> processor 2,
index 12-15 ---> processor 3,
index 16-19 ---> processor 4,

并将总和返回给主总和。

我如何对其进行编码以便可以轻松完成???

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

public class Aznin {

    public static void main(String args[]) throws Exception {
            Random randomNumbers = new Random();            
            MPI.Init(args);
            int my_rank = MPI.COMM_WORLD.Rank();
            int size = MPI.COMM_WORLD.Size();
            MPI.COMM_WORLD.Barrier();
            int[] n = new int[100];
            int[] my_sum = new int[1];
            int blockNum, start, end;
            int finalSum = 0;

            // master initialize random integers and stores them
            if (my_rank == 0)   {
                System.out.println("");
                    //for 20 values produced by Random number function; but you can declare an array of 20 values; other option can read values from the file--- OK Dear Students :)                

            //how many valus you want to distribute e.g. 20 
            for (int i = 0; i < 20; i++)    {
                    n[i] = 1 + randomNumbers.nextInt(50);
                    System.out.printf("%d| ", n[i]);
                }
                System.out.printf("\n");
            }

            //Broadcast n from master processor to all available processors
            MPI.COMM_WORLD.Bcast(n, 0, 20, MPI.INT, 0);

            // Each processor calculates the result
            my_sum[0] = 0;
            blockNum = 20/size;     // calculates number of data blocks--- using how many CPUs i.e. Size
            start = my_rank * blockNum; // calculates for starting index for particular processor
            end = start + blockNum; // calculates for ending index for particular processor

            //Keep in mind; your approach can vary from my approach; you can create lists at run time as well --OK
            for (int i = start; i < end; i++)   {
                my_sum[0] = my_sum[0] + n[i];
            }
            System.out.println("Result from processor " +my_rank+ " = " +my_sum[0]);

            if (my_rank == 0)   {   // master processor waits for result from workers
                finalSum = my_sum[0];
                //master processor recieves result from all available worker prosessors
                for (int wproc = 1; wproc < size; wproc++)  {
                    MPI.COMM_WORLD.Recv(my_sum, 0, 1, MPI.INT, MPI.ANY_SOURCE,99);
                    finalSum = finalSum + my_sum[0];
                }
            }
            else    {   // worker processors
                // send result to master processor
                MPI.COMM_WORLD.Send(my_sum, 0, 1, MPI.INT, 0, 99);
            }

            // master processor prints final result
            if (my_rank == 0)   {
                System.out.println("Result = " + finalSum);
            }

            MPI.Finalize();
    }
}
4

2 回答 2

0

您可以使用 MapReduce 以多线程方式对数组求和。为此,网上有很多代码。你只需要采用它们:

http://pages.cs.wisc.edu/~gibson/mapreduceexample/main.C.html

于 2012-04-20T06:48:12.800 回答
0

我不清楚您的要求,不知道为什么要同时使用 C++ 和 javascript。无论如何,这里是如何在 javscript 中做类似的事情。

var data = [ 24, 48, 27, 1, 5, 30, 40, 27, 31, 36,
             29, 45, 45, 43, 10, 15, 50, 35, 40, 47 ];

function totalInChunks(d, n) {
  var result = [];
  var i = 0, j, len = d.length, t;
  do {
    for (t=0, j=0; j<n; j++) {
      t += d[i++];
    }
    result.push(t);
  } while (i<len)

  return result;
}

totalInChunks(data, 4); // 100,102,141,113,172

这是否有用我无法判断。我想在哪里t计算你可以将数据或对象分组并将它们发送到另一个处理器,然后在最后收集结果以整理最终结果。但是 javascript 是单线程的,因此没有太多意义或设施来异步执行它,尽管它可以使用setTimeout进行设置。

除了作为协调异步处理的练习之外,很难看出这一点。

于 2012-04-20T07:07:26.153 回答