![]() ![]() MonteCarloPiApproximationParallellForSimulation() Ĭonsole.WriteLine("Parallell simulation using Parallel.For: (ms)" + sw.ElapsedMilliseconds) MonteCarloPiApproximationSerialSimulation() Ĭonsole.WriteLine("Serial simulation: (ms)" + sw.ElapsedMilliseconds) Static int iterations = 10000000 * numberOfCores Ĭonsole.WriteLine("Monte Carlo numeric simulation of PI") Ĭonsole.WriteLine("Iteration limit: " + iterations) Ĭonsole.WriteLine("Number of processor cores on system: " + Environment.ProcessorCount) Ĭonsole.WriteLine("\nMONTE CARLO SIMULATION") Static int numberOfCores = Environment.ProcessorCount The iterations I used in the demo was 80 million. The conclusion I got after testing showed after several runs shows that the sequential version runs in about 3.5 seconds on myĮight core system with about half the time, about 1.8 seconds using Parallel.For - The last version using Tasks and Tasks.WaitAll give about 1.7 seconds and the quickest compuation, about twice as fast. I have included a screen shot at the end. Let's move on the code sample, review the code. This expression (b) is directly from the previous expression (a) The ratio to be expected between the areas of the unit circle and unit rectangle therefore gives the formula above. The unit circle got a radius ofġ, hence its area is PI * 1^2 = PI. Why? Because the square has got an area equal to four, remember that the unit square got sides equal to the number 2 and its area is therefore 2 * 2 = 4. We know that the following can then be expected: The number PI can be defined as generating random numbers and looking at the ratio of the numbers inside the circle M divided upon the total numbers generated N. We consider the unit circle inscribed in a square around origo with corners at coordinates (-1,-1), (-1, 1), (1,1) and (1,-1). ![]() Then the Parallel.For construct in TPL is used. The following code sample shows numeric compuation of the number PI using Monte-Carlo simulation. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |