Microsoft Accelerator - Pi Calc port in C#

Wednesday, 15 December 2010 13:48 animaonline

I'm posting the quick and dirty port of Microsoft's Accelerator π (pi calculation) sample.
The code has been ported from C++ to C# by me, a couple of years ago.

Accelerator is a high-level data parallel library which uses parallel processors such as the GPU or multicore CPU to accelerate execution.
Find out more at Microsoft Research

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Research.DataParallelArrays;

namespace piSharp.cs
{
    class Program
    {
        static void Main(string[] args)
        {
            Random r = new Random();
            // create randomly generated samples in the unit square centered on (0,0)

            int iSize = 1500;
            float[,] x = new float[iSize + 1, iSize + 1];
            // x coordinate
            float[,] y = new float[iSize + 1, iSize + 1];
            // y coordinate
            int i = 0;
            int j = 0;

            // create an x and y arrays of random numbers between 0 and 1
            for (i = 0; i <= iSize - 1; i += 1)
            {
                for (j = 0; j <= iSize - 1; j += 1)
                {
                    x[i, j] = (float)r.NextDouble();
                    y[i, j] = (float)r.NextDouble();
                }
            }
            ParallelArrays.InitGPU();

            // make x and y data parallel arrays
            DisposableFloatParallelArray parallelX = new DisposableFloatParallelArray(x);
            DisposableFloatParallelArray parallelY = new DisposableFloatParallelArray(y);

            // center them about (0, 0) with range (-1, 1)
            FloatParallelArray parallelXCentered = default(FloatParallelArray);
            FloatParallelArray parallelYCentered = default(FloatParallelArray);
            parallelXCentered = ParallelArrays.Multiply(ParallelArrays.Subtract(parallelX, 0.5f), 2f);
            parallelYCentered = ParallelArrays.Multiply(ParallelArrays.Subtract(parallelY, 0.5f), 2f);

            // calculate distance of (x,y) from 0, ie. Sqrt(x^2 + y^2)
            FloatParallelArray parallelXSquare = default(FloatParallelArray);
            FloatParallelArray parallelYSquare = default(FloatParallelArray);
            FloatParallelArray parallelDistance = default(FloatParallelArray);
            parallelXSquare = ParallelArrays.Multiply(parallelXCentered, parallelXCentered);
            parallelYSquare = ParallelArrays.Multiply(parallelYCentered, parallelYCentered);
            parallelDistance = ParallelArrays.Sqrt(ParallelArrays.Add(parallelXSquare, parallelYSquare));
            float[,] test = new float[iSize + 1, iSize + 1];
            ParallelArrays.ToArray(parallelDistance, out test);

            // create an array of 1's if distance <> 1
            FloatParallelArray parallelOne = new FloatParallelArray(1f, parallelX.Shape);
            FloatParallelArray parallelZero = new FloatParallelArray(0f, parallelX.Shape);
            FloatParallelArray parallelInCircle = default(FloatParallelArray);

            parallelInCircle = ParallelArrays.Select(ParallelArrays.Subtract(parallelOne, parallelDistance), parallelOne, parallelZero);

            // the number inside the circle is the sum of the entire InCircle array
            FloatParallelArray parallelCountInCircle = default(FloatParallelArray);
            parallelCountInCircle = ParallelArrays.Sum(parallelInCircle);

            float[] inCircle = new float[2];
            ParallelArrays.ToArray(parallelCountInCircle, out inCircle);
            parallelX.Dispose();
            parallelY.Dispose();

            // approximate pi--area of unit circle is pi
            //                 area of square from -1,1 is 4
            // inCircle/(total number of points) == pi/4
            float pi = 0;
            pi = 4 * inCircle[0] / (iSize * iSize);
            System.Console.WriteLine("Pi is approximately " + pi.ToString());
            ParallelArrays.UnInit();

            // Prompt user for exit for running in VS IDE
            System.Console.WriteLine("Press Enter to Exit");
            System.Console.ReadLine();
        }
    }
}

Comments (0)Add Comment

Write comment
smaller | bigger

busy

Latest tweets

about 6 hours ago Was out walking 2.96 km with #Endomondo . See it here: http://t.co/c9k3s4mV
about 5 days ago @Lordovos yeah, it's a great game. What do you think about #dqix ?
about 5 days ago #dqviii time http://t.co/fH8wUtQV
about 5 days ago My Top 3 #lastfm Artists: Eminem (52), Dieselboy (32) & Disturbed (11) http://t.co/YPJlaiyy
about 6 days ago Was out walking 1.83 km with #Endomondo . See it here: http://t.co/rLVd9mFS
about 6 days ago Was out walking 0.00 km with #Endomondo . See it here: http://t.co/E7m2A2aQ
about 6 days ago Can't believe so few have heard of #OSI , check them out on @Spotify They are awesome! http://t.co/mayiR94r #progressiverock
about 7 days ago Playing #DragonQuestVIII #dqviii
about 9 days ago Was out walking 2.64 km with #Endomondo . See it here: http://t.co/qpusEsHW
about 9 days ago Off to #work #tgif :-D
about 10 days ago &#9829; Hellbound by Eminem #lastfm : http://t.co/7uxCWRlu
about 11 days ago Was out walking 1.60 km with #Endomondo . See it here: http://t.co/u3ZlyiRB
about 11 days ago #Stackoverflow is kinda fun, I like!! #dev
about 12 days ago @YngveNilsen really? Congrats!! :-D

Tag Cloud