Return-Path: Delivered-To: apmail-incubator-hama-commits-archive@minotaur.apache.org Received: (qmail 14679 invoked from network); 18 Oct 2010 01:26:28 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 18 Oct 2010 01:26:28 -0000 Received: (qmail 75289 invoked by uid 500); 18 Oct 2010 01:26:28 -0000 Delivered-To: apmail-incubator-hama-commits-archive@incubator.apache.org Received: (qmail 75263 invoked by uid 500); 18 Oct 2010 01:26:28 -0000 Mailing-List: contact hama-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hama-dev@incubator.apache.org Delivered-To: mailing list hama-commits@incubator.apache.org Received: (qmail 75255 invoked by uid 99); 18 Oct 2010 01:26:28 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Oct 2010 01:26:28 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.131] (HELO eos.apache.org) (140.211.11.131) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 18 Oct 2010 01:26:26 +0000 Received: from eosnew.apache.org (localhost [127.0.0.1]) by eos.apache.org (Postfix) with ESMTP id 511FF946 for ; Mon, 18 Oct 2010 01:25:55 +0000 (UTC) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Apache Wiki To: Apache Wiki Date: Mon, 18 Oct 2010 01:25:55 -0000 Message-ID: <20101018012555.67784.20720@eosnew.apache.org> Subject: =?utf-8?q?=5BHama_Wiki=5D_Update_of_=22PiEstimator=22_by_edwardyoon?= X-Virus-Checked: Checked by ClamAV on apache.org Dear Wiki user, You have subscribed to a wiki page or wiki category on "Hama Wiki" for chan= ge notification. The "PiEstimator" page has been changed by edwardyoon. http://wiki.apache.org/hama/PiEstimator -------------------------------------------------- New page: =3D=3D Pi Estimator =3D=3D The value of PI can be calculated in a number of ways. Consider the followi= ng 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 poi= nts in the square * PI ~ 4 r Serial pseudo code for this procedure as below: {{{ iterations =3D 10000 circle_count =3D 0 do j =3D 1,iterations generate 2 random numbers between 0 and 1 xcoordinate =3D random1 ycoordinate =3D random2 if (xcoordinate, ycoordinate) inside circle then circle_count =3D circle_count + 1 end do PI =3D 4.0*circle_count/iterations }}} =3D=3D=3D=3D The BSP implementation for Pi =3D=3D=3D=3D Parallel strategy is break the loop into portions that can be executed by t= he tasks. * For the task of estimating PI: * Each task executes its portion of the loop a number of times. * Each task can do its work without requiring any information from the o= ther tasks (there are no data dependencies). * One task acts as master and collects the results. {{{ public class PiEstimator { private static String MASTER_TASK =3D "master.task."; public static class MyEstimator extends BSP { public static final Log LOG =3D LogFactory.getLog(MyEstimator.class); private Configuration conf; private String masterTask; private static final int iterations =3D 10000; @Override public void bsp(BSPPeer bspPeer) throws IOException, KeeperException, InterruptedException { int in =3D 0, out =3D 0; for (int i =3D 0; i < iterations; i++) { double x =3D 2.0 * Math.random() - 1.0, y =3D 2.0 * Math.random() -= 1.0; if ((Math.sqrt(x * x + y * y) < 1.0)) { in++; } else { out++; } } byte[] tagName =3D Bytes.toBytes(getName().toString()); byte[] myData =3D Bytes.toBytes(4.0 * (double) in / (double) iteratio= ns); BSPMessage estimate =3D new BSPMessage(tagName, myData); bspPeer.send(bspPeer.getAddress(masterTask), estimate); bspPeer.sync(); double pi =3D 0.0; BSPMessage received; while ((received =3D bspPeer.getCurrentMessage()) !=3D null) { LOG.info("Receives messages:" + Bytes.toDouble(received.getData())); if(pi =3D=3D 0.0) { pi =3D Bytes.toDouble(received.getData()); } else { pi =3D (pi + Bytes.toDouble(received.getData())) / 2; } } if (pi !=3D 0.0) { LOG.info("\nEstimated value of PI is " + pi); } } @Override public Configuration getConf() { return conf; } @Override public void setConf(Configuration conf) { this.conf =3D conf; this.masterTask =3D conf.get(MASTER_TASK); } } public static void main(String[] args) throws InterruptedException, IOException { // BSP job configuration HamaConfiguration conf =3D new HamaConfiguration(); // Execute locally // conf.set("bsp.master.address", "local"); BSPJob bsp =3D new BSPJob(conf, PiEstimator.class); // Set the job name bsp.setJobName("pi estimation example"); bsp.setBspClass(MyEstimator.class); BSPJobClient jobClient =3D new BSPJobClient(conf); ClusterStatus cluster =3D jobClient.getClusterStatus(true); // Choose one as a master for (String name : cluster.getActiveGroomNames()) { conf.set(MASTER_TASK, name); break; } BSPJobClient.runJob(bsp); } } }}}