CALCULATING PI USING A DARTBOARD #ANEKA

/// <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();}}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store