Versions Compared

Key

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

...

The value of PI can be calculated in a number of ways. Consider the following method of In this example, we are estimating PI

  • Inscribe a circle in a square
  • Randomly generate points in the square
  • Determine the number of points in the square that are also in the circle
  • Let r be the number of points in the circle divided by the number of points in the square
  • PI ~ 4 r

Serial pseudo code for this procedure as below:

...

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

The BSP implementation for Pi

A distributed strategy in HAMA with BSP programming model, is break the loop into portions that can be executed by the tasks.

...

PI = 4.0*circle_count/iterations

...


}}}


  • One task acts as master and collects the results through the BSP communication interface.
No Format
public class PiEstimatorPI {
= 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:

Code Block
languagejava

    @Overrideprivate static String MASTER_TASK = "master.task.";

  public static class MyEstimator extends BSP {
    public static final Log LOG = LogFactory.getLog(MyEstimator.class);void bsp(
    private Configuration conf;
  BSPPeer<NullWritable, NullWritable, privateText, StringDoubleWritable> masterTask;peer)
    private static final int iterations = 10000;

    @Override
    public void bsp(BSPPeer bspPeer) throws IOException, KeeperExceptionSyncException,
        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(getName().toString());
      byte[] myData = Bytes.toBytes(4.0 * (double) in / (double) iterations);
      BSPMessageDoubleMessage estimate = new BSPMessage(tagNameDoubleMessage(peer.getPeerName(), myDatadata);

      bspPeerpeer.send(bspPeer.getAddress(masterTask), estimate);
      bspPeerpeer.sync();

      double pi = 0.0;}

      BSPMessage received;@Override
    public void while setup((received
 = bspPeer.getCurrentMessage()) != null) {
    BSPPeer<NullWritable, NullWritable, Text,  LOG.info("Receives messages:" + Bytes.toDouble(received.getData()));DoubleWritable> peer)
        if(pi == 0.0)throws IOException {
      // Choose one as pi = Bytes.toDouble(received.getData());a master
        } else {
          pi = (pi + Bytes.toDouble(received.getData())this.masterTask = peer.getPeerName(peer.getNumPeers() / 2);
        }
      }

    public void if (pi != 0.0) {cleanup(
        LOG.info("\nEstimated value of PI is " + pi);BSPPeer<NullWritable, NullWritable, Text, DoubleWritable> peer)
      }
  throws IOException }
{
    @Override
    public Configuration getConf()if (peer.getPeerName().equals(masterTask)) {
      return conf;
 double pi = }

0.0;
     @Override
   int publicnumPeers void= setConf(Configuration conf) {peer.getNumCurrentMessages();
      this.conf = conf;
      this.masterTask = conf.get(MASTER_TASK)DoubleMessage received;
    }

  }

  publicwhile static((received void= main(String[] args) throws InterruptedException,
      IOExceptionDoubleMessage) peer.getCurrentMessage()) != null) {
    // BSP job configuration
   pi HamaConfiguration conf += new HamaConfigurationreceived.getData();
   
    BSPJob bsp = new BSPJob(conf, PiEstimator.class); }

    // Set the job name
pi    bsp.setJobName("= pi estimation example");
    bsp.setBspClass(MyEstimator.class);
/ numPeers;
    BSPJobClient jobClient = new BSPJobClient(conf);peer
    ClusterStatus cluster = jobClient.getClusterStatus(true);
    // Choose one as a master
    for (String name : cluster.getActiveGroomNames()) {
      conf.set(MASTER_TASK, name .write(new Text("Estimated value of PI is"), new DoubleWritable(pi));
      break;}
    }

    BSPJobClient.runJob(bsp);
  }
}