This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:

No Format
    @Override
    public void bsp(BSPPeerProtocol
bspPeer) throws IOException       BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
    KeeperException    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++;
        }
      }

      byte[]double tagNamedata = Bytes.toBytes(bspPeer.getPeerName());
      byte[] myData = Bytes.toBytes(4.0 * (double) in / (double) iterations);
      BSPMessageDoubleMessage estimate = new BSPMessage(tagNameDoubleMessage(peer.getPeerName(), myDatadata);

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

  if (bspPeer  @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 = bspPeerpeer.getNumCurrentMessages();
        BSPMessageDoubleMessage received;
        while ((received = bspPeer(DoubleMessage) peer.getCurrentMessage()) != null) {
          pi += Bytes.toDouble(received.getData());
        }

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