incubator-hama-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Hama Wiki] Trivial Update of "PiEstimator" by edwardyoon
Date Wed, 21 Dec 2011 00:54:18 GMT
Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Hama Wiki" for change notification.

The "PiEstimator" page has been changed by edwardyoon:
http://wiki.apache.org/hama/PiEstimator?action=diff&rev1=11&rev2=12

  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:
  
  {{{
-     public void bsp(BSPPeerProtocol bspPeer) throws IOException,
+     @Override
+     public void bsp(
+         BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
-         KeeperException, InterruptedException {
+         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;
@@ -42, +45 @@

          }
        }
  
-       byte[] tagName = Bytes.toBytes(bspPeer.getPeerName());
-       byte[] myData = Bytes.toBytes(4.0 * (double) in / (double) iterations);
+       double data = 4.0 * (double) in / (double) iterations;
-       BSPMessage estimate = new BSPMessage(tagName, myData);
+       DoubleMessage estimate = new DoubleMessage(peer.getPeerName(), data);
  
-       bspPeer.send(masterTask, estimate);
+       peer.send(masterTask, estimate);
-       bspPeer.sync();
+       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 (bspPeer.getPeerName().equals(masterTask)) {
+       if (peer.getPeerName().equals(masterTask)) {
          double pi = 0.0;
-         int numPeers = bspPeer.getNumCurrentMessages();
+         int numPeers = peer.getNumCurrentMessages();
-         BSPMessage received;
+         DoubleMessage received;
-         while ((received = bspPeer.getCurrentMessage()) != null) {
+         while ((received = (DoubleMessage) peer.getCurrentMessage()) != null) {
-           pi += Bytes.toDouble(received.getData());
+           pi += received.getData();
          }
  
          pi = pi / numPeers;
-         writeResult(pi);
+         peer
+             .write(new Text("Estimated value of PI is"), new DoubleWritable(pi));
        }
      }
+   }
  }}}
  

Mime
View raw message