You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hama.apache.org by mi...@apache.org on 2014/05/22 11:07:25 UTC

svn commit: r1596782 - in /hama/trunk: CHANGES.txt examples/src/main/java/org/apache/hama/examples/PiEstimator.java

Author: millecker
Date: Thu May 22 09:07:24 2014
New Revision: 1596782

URL: http://svn.apache.org/r1596782
Log:
HAMA-905: Fix Pi Estimator Example

Modified:
    hama/trunk/CHANGES.txt
    hama/trunk/examples/src/main/java/org/apache/hama/examples/PiEstimator.java

Modified: hama/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hama/trunk/CHANGES.txt?rev=1596782&r1=1596781&r2=1596782&view=diff
==============================================================================
--- hama/trunk/CHANGES.txt (original)
+++ hama/trunk/CHANGES.txt Thu May 22 09:07:24 2014
@@ -8,6 +8,7 @@ Release 0.7.0 (unreleased changes)
 
   BUG FIXES
   
+   HAMA-905: Fix Pi Estimator Example (Martin Illecker)
    HAMA-889: NonDefaultIterator of DenseDoubleVector never reaches the end (Yexi Jiang)
    HAMA-888: Add more test cases for DenseDoubleVector (Yexi Jiang)
    HAMA-885: Semi-Clustering is not producing expected output (Renil J via edwardyoon)

Modified: hama/trunk/examples/src/main/java/org/apache/hama/examples/PiEstimator.java
URL: http://svn.apache.org/viewvc/hama/trunk/examples/src/main/java/org/apache/hama/examples/PiEstimator.java?rev=1596782&r1=1596781&r2=1596782&view=diff
==============================================================================
--- hama/trunk/examples/src/main/java/org/apache/hama/examples/PiEstimator.java (original)
+++ hama/trunk/examples/src/main/java/org/apache/hama/examples/PiEstimator.java Thu May 22 09:07:24 2014
@@ -27,6 +27,7 @@ import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.io.DoubleWritable;
 import org.apache.hadoop.io.IOUtils;
+import org.apache.hadoop.io.LongWritable;
 import org.apache.hadoop.io.NullWritable;
 import org.apache.hadoop.io.Text;
 import org.apache.hama.HamaConfiguration;
@@ -46,33 +47,32 @@ public class PiEstimator {
       + System.currentTimeMillis());
 
   public static class MyEstimator extends
-      BSP<NullWritable, NullWritable, Text, DoubleWritable, DoubleWritable> {
+      BSP<NullWritable, NullWritable, Text, DoubleWritable, LongWritable> {
     public static final Log LOG = LogFactory.getLog(MyEstimator.class);
     private String masterTask;
     private static final int iterations = 10000;
 
     @Override
     public void bsp(
-        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable, DoubleWritable> peer)
+        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable, LongWritable> peer)
         throws IOException, SyncException, InterruptedException {
 
-      int in = 0;
+      long in = 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)) {
+        double x = 2.0 * Math.random() - 1.0; // [-1..1]
+        double y = 2.0 * Math.random() - 1.0; // [-1..1]
+        if ((x * x + y * y) <= 1.0) {
           in++;
         }
       }
 
-      double data = 4.0 * in / iterations;
-
-      peer.send(masterTask, new DoubleWritable(data));
+      peer.send(masterTask, new LongWritable(in));
       peer.sync();
     }
 
     @Override
     public void setup(
-        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable, DoubleWritable> peer)
+        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable, LongWritable> peer)
         throws IOException {
       // Choose one as a master
       this.masterTask = peer.getPeerName(peer.getNumPeers() / 2);
@@ -80,17 +80,17 @@ public class PiEstimator {
 
     @Override
     public void cleanup(
-        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable, DoubleWritable> peer)
+        BSPPeer<NullWritable, NullWritable, Text, DoubleWritable, LongWritable> peer)
         throws IOException {
       if (peer.getPeerName().equals(masterTask)) {
-        double pi = 0.0;
+        long in = 0;
         int numPeers = peer.getNumCurrentMessages();
-        DoubleWritable received;
+        LongWritable received;
         while ((received = peer.getCurrentMessage()) != null) {
-          pi += received.get();
+          in += received.get();
         }
+        double pi = 4.0 * in / (iterations * numPeers);
 
-        pi = pi / numPeers;
         peer.write(new Text("Estimated value of PI is"), new DoubleWritable(pi));
       }
     }
@@ -119,7 +119,7 @@ public class PiEstimator {
     BSPJob bsp = new BSPJob(conf, PiEstimator.class);
     bsp.setCompressionCodec(SnappyCompressor.class);
     bsp.setCompressionThreshold(40);
-    
+
     // Set the job name
     bsp.setJobName("Pi Estimation Example");
     bsp.setBspClass(MyEstimator.class);