**CALCULATING PI USING A DARTBOARD #ANEKA**

This example is a bit complicated, but it yields a more useful result. It explains how to utilize *AnekaThreads* to calculate the value of **pi**. This is similar to shooting darts at random spots on a dartboard. Using this strategy, each thread individually approximates the value of pi, and the cumulative average derived from all threads is utilized to estimate the value of pi. Increasing the number of AnekaThreads enhances pi accuracy.

*A circle inscribed in a square bounded by the coordinates (1,1), (1,-1), (-1,-1) and (-1,1)*

Throwing darts at random spots within the square is used to calculate pi. A dart may land within or outside the circle, but within the square. A dart that lands inside the circle is called a hit.

The ratio of the area of the circle, πr2, to the area of the square, 4r2, is π/4. The x-y coordinates of the darts are random numbers in the range [-1, 1]. The value of pi is given by the following equation:

π/4 = (number of hits inside the circle) / (total number of throws)

π = 4 * (number of hits inside the circle) / (total number of throws)

Each thread calculates pi by throwing darts a specified number of times. Increasing the number of threads maintains the work done by each thread while improving the overall accuracy of the cumulative average of pi.

/// <summary>/// Class Dart. Represents a dart that is thrown for the given/// number of iterations at random points on a dartboard./// </summary>[Serializable]public class Dart{/// <summary>/// The number of iterations to throw the dart at random points./// </summary>private int iterations;/// <summary>/// The approximation for the value of Pi./// </summary>private double result;/// <summary>/// Gets or sets the approximation for the value of Pi./// </summary>public double Result{get { return result; }set { result = value; }}/// <param name=”iterations”>The number of iterations to throw/// the dart at random points</param>public Dart(int iterations){this.iterations = iterations;}/// <summary>/// Throws the dart at random points for the specified number of iterations./// </summary>public void Fire(){int hit = 0;Random rand = new Random();for (int i = 0; i < this.iterations; i++){double x = rand.NextDouble() * 2–1;double y = rand.NextDouble() * 2–1;if ((x * x) + (y * y) <= 1.0){hit++;}}this.result = 4.0 * ((double)hit / (double)iterations);}}/// <summary>/// Class Dartboard. Represents a dartboard to which a collection of darts/// can be thrown. Each of the darts thrown in encapsulated in an AnekaThread/// instance and executed on the remote runtime environment./// </summary>public class Dartboard{/// <summary>/// The application configuration/// </summary>private Configuration configuration;/// <summary>/// Creates an instance of Dartboard./// </summary>/// <param name=”schedulerUri”>The uri to Aneka’s scheduler</param>public Dartboard(Uri schedulerUri){configuration = new Configuration();configuration.SchedulerUri = schedulerUri;}/// <summary>/// Creates a list of AnekeThread instances for each of the darts/// specified by <paramref name=”noOfDarts”/>, submits them for execution/// on the Aneka runtime, and composes the final result by calculating the/// cumulative average value of pi./// </summary>/// <param name=”noOfDarts”>The number of darts to throw. That is the number/// of AnekaThread instances to execute on the remote runtime/// environment</param>/// <param name=”iterations”>The number of iterations for each of the darts</param>/// <returns>The cumulative average of pi</returns>public double ThrowDarts(int noOfDarts, int iterations){// Create application and computation threadsAnekaApplication<AnekaThread, ThreadManager> application = newAnekaApplication<AnekaThread, ThreadManager>(configuration);IList<AnekaThread> threads = this.CreateComputeThreads(application,noOfDarts, iterations);// execute threads on Anekathis.ExecuteThreads(threads);// calcualate cumulative average of pidouble pi = this.ComposeResult(threads);// stop applicationapplication.StopExecution();return pi;}/// <summary>/// Creates AnekeThread instances for each of the specified number of/// darts. These threads are initalized to execute the Dart.Fire() method on/// the remote node./// </summary>/// <param name=”application”>The AnekaApplication instance/// containing the application configuration</param>/// <param name=”noOfDarts”>The number of darts to throw. That is, the/// number of instances of AnekaThread to create</param>/// <param name=”iterations”>The number of iterations for each of the darts/// thrown</param>/// <returns>A list of AnekaThread instances</returns>private IList<AnekaThread> CreateComputeThreads(AnekaApplication<AnekaThread, ThreadManager> application,int noOfDarts, int iterations){IList<AnekaThread> threads = new List<AnekaThread>();for (int x = 0; x < noOfDarts; x++){Dart dart = new Dart(iterations);AnekaThread thread = new AnekaThread(dart.Fire, application);threads.Add(thread);}return threads;}/// <summary>/// Executes the list of AnekaThread instances/// on the Aneka runtime environment./// </summary>/// <param name=”threads”>The list of AnekaThread/// instances to execute</param>private void ExecuteThreads(IList<AnekaThread> threads){for each (AnekaThread thread in threads){thread.Start();}}/// <summary>/// Composes the resulting value of pi by calculating the cumulative average/// computed by each of the AnekaThread instances./// This method pauses until all threads have completed execution./// </summary>/// <param name=”threads”>The list of instances that were submitted for/// execution</param>/// <returns>The average value of pi</returns>private double ComposeResult(IList<AnekaThread> threads){// wait till all threads complete.foreach (AnekaThread thread in threads){thread.Join();}double total = 0;foreach (AnekaThread thread in threads){Dart dart = (Dart)thread.Target;total += dart.Result;}return total / threads.Count;}/// <summary>/// The main entry point to the application./// </summary>/// <param name=”args”>Currently requires no command line arguments. </param>static void Main(string[] args){Uri uri = new Uri(“tcp://400w-ICT0217–09:9090/Aneka”);Dartboard dboard = new Dartboard(uri);double pi = dboard.ThrowDarts(25, 3000);Console.WriteLine(“Value of pi = “ + pi);Console.ReadKey();}}

The Dartboard class manages the execution of AnekaThread objects on distant nodes. The parameters for the ThrowDarts method are the number of darts and the number of repetitions per dart. For each dart, an AnekaThread is established and performed on a distant node for the specified number of repeats. Take note of how a List is utilized to keep track of the program’s AnekaThread collection. The ExecuteThreads function iterates through this list to start all threads, and the ComposeResult method iterates through the list to connect all threads to the main application thread before computing the cumulative average for pi.

**Output**

*Run 1:*

Value of pi = 3.13797333333333

*Run 2:*

Value of pi = 3.15202666666667

*Run 3:*

Value of pi = 3.13845333333333

ReferenceRajkumar Buyya, Christian Vecchiola, S. ThamaraiSelvi,

Mastering Cloud Computing Foundations and Applications Programming,Morgan Kaufmann (2013).