Differences between revisions 11 and 12
Revision 11 as of 2011-02-15 23:56:43
Size: 1981
Editor: edwardyoon
Comment:
Revision 12 as of 2011-12-21 00:54:17
Size: 2430
Editor: edwardyoon
Comment:
Deletions are marked like this. Additions are marked like this.
Line 31: Line 31:
    public void bsp(BSPPeerProtocol bspPeer) throws IOException,
       KeeperException, InterruptedException {
    @Override
public void bsp(
        
BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
       
throws IOException, SyncException, InterruptedException {
Line 43: Line 46:
      byte[] tagName = Bytes.toBytes(bspPeer.getPeerName());
      byte[] myD
ata = Bytes.toBytes(4.0 * (double) in / (double) iterations);
      BSPMessage estimate = new BSPMessage(tagName, myData);
      double data = 4.0 * (double) in / (double) iterations;
      DoubleMessage estimate = new DoubleMessage(peer.getPeerName(), data);
Line 47: Line 49:
      bspPeer.send(masterTask, estimate);
      bspPeer.sync();
      peer.send(masterTask, estimate);
      peer.sync();
    }
Line 50: Line 53:
    @Override
    public void setup(
        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
        throws IOException {
      // Choose one as a master
      this.masterTask = peer.getPeerName(peer.getNumPeers() / 2);
    }
Line 51: Line 61:
      if (bspPeer.getPeerName().equals(masterTask)) {     public void cleanup(
        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
        throws IOException {
      if (peer.getPeerName().equals(masterTask)) {
Line 53: Line 66:
        int numPeers = bspPeer.getNumCurrentMessages();
        BSPMessage received;
        while ((received = bspPeer.getCurrentMessage()) != null) {
          pi += Bytes.toDouble(received.getData());
        int numPeers = peer.getNumCurrentMessages();
        DoubleMessage received;
        while ((received = (DoubleMessage) peer.getCurrentMessage()) != null) {
          pi += received.getData();
Line 60: Line 73:
        writeResult(pi);         peer
            .write(new Text("Estimated value of PI is"), new DoubleWritable(pi));
Line 63: Line 77:
  }

Pi Estimator

The value of PI can be calculated in a number of ways. In this example, we are estimating PI using following way:

  • Each task executes locally its portion of the loop a number of times.

  iterations = 10000
  circle_count = 0

  do j = 1,iterations
  generate 2 random numbers between 0 and 1
  xcoordinate = random1
  ycoordinate = random2
  if (xcoordinate, ycoordinate) inside circle
  then circle_count = circle_count + 1
  end do

  PI = 4.0*circle_count/iterations
  • One task acts as master and collects the results through the BSP communication interface.

  PI = pi_sum / n_processes

1) Each process computes the value of Pi locally, and 2) sends it to master task using send() function. Then, 3) the master task can recieve the messages using sync() function. Finally, we can calculate the average value of sum of PI values from each peers as below:

    @Override
    public void bsp(
        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
        throws IOException, SyncException, InterruptedException {

      int in = 0, out = 0;
      for (int i = 0; i < iterations; i++) {
        double x = 2.0 * Math.random() - 1.0, y = 2.0 * Math.random() - 1.0;
        if ((Math.sqrt(x * x + y * y) < 1.0)) {
          in++;
        } else {
          out++;
        }
      }

      double data = 4.0 * (double) in / (double) iterations;
      DoubleMessage estimate = new DoubleMessage(peer.getPeerName(), data);

      peer.send(masterTask, estimate);
      peer.sync();
    }

    @Override
    public void setup(
        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
        throws IOException {
      // Choose one as a master
      this.masterTask = peer.getPeerName(peer.getNumPeers() / 2);
    }

    public void cleanup(
        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
        throws IOException {
      if (peer.getPeerName().equals(masterTask)) {
        double pi = 0.0;
        int numPeers = peer.getNumCurrentMessages();
        DoubleMessage received;
        while ((received = (DoubleMessage) peer.getCurrentMessage()) != null) {
          pi += received.getData();
        }

        pi = pi / numPeers;
        peer
            .write(new Text("Estimated value of PI is"), new DoubleWritable(pi));
      }
    }
  }

PiEstimator (last edited 2013-04-17 10:50:29 by edwardyoon)