Differences between revisions 6 and 7
 ⇤ ← Revision 6 as of 2010-10-20 11:32:30 → Size: 3531 Editor: edwardyoon Comment: ← Revision 7 as of 2010-10-25 01:23:43 → ⇥ Size: 3599 Editor: edwardyoon Comment: BSPPeer interface changed. Deletions are marked like this. Additions are marked like this. Line 58: Line 58: byte[] tagName = Bytes.toBytes(getName().toString()); byte[] tagName = Bytes.toBytes(bspPeer.getPeerName()); Line 62: Line 62: bspPeer.send(bspPeer.getAddress(masterTask), estimate); bspPeer.send(masterTask, estimate); Line 98: Line 98: // Execute locally    // conf.set("bsp.master.address", "local"); Line 107: Line 109: for (String name : cluster.getActiveGroomNames()) {      conf.set(MASTER_TASK, name); for (String peerName : cluster.getActiveGroomNames().values()) {      conf.set(MASTER_TASK, peerName);

## Pi Estimator

The value of PI can be calculated in a number of ways. Consider the following method of 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:

```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```

## 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.

• Each task executes locally its portion of the loop a number of times.
• One task acts as master and collects the results through the BSP communication interface.

```public class PiEstimator {

public static class MyEstimator extends BSP {
public static final Log LOG = LogFactory.getLog(MyEstimator.class);
private Configuration conf;
private static final int iterations = 10000;

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

bspPeer.sync();

double pi = 0.0;
while ((received = bspPeer.getCurrentMessage()) != null) {
if(pi == 0.0) {
} else {
pi = (pi + Bytes.toDouble(received.getData())) / 2;
}
}

if (pi != 0.0) {
LOG.info("\nEstimated value of PI is " + pi);
}
}

@Override
public Configuration getConf() {
return conf;
}

@Override
public void setConf(Configuration conf) {
this.conf = conf;
}

}

public static void main(String[] args) throws InterruptedException,
IOException {
// BSP job configuration
HamaConfiguration conf = new HamaConfiguration();
// Execute locally

BSPJob bsp = new BSPJob(conf, PiEstimator.class);
// Set the job name
bsp.setJobName("pi estimation example");
bsp.setBspClass(MyEstimator.class);

BSPJobClient jobClient = new BSPJobClient(conf);
ClusterStatus cluster = jobClient.getClusterStatus(true);
// Choose one as a master
for (String peerName : cluster.getActiveGroomNames().values()) {