1

I have a Dictionary of Circles (random distributed in 2D-space) and a Position in 2D-space. The method should return just ANY circle, where the Position is within the radius of the circle. (There might be even more than one, but I don't care about which one it chooses)

My current implementation looks like this:

int GetAnyCircleWithinRadius(Dictionary<int, Circle>circles, Position position)
{
    int circleIndex = -1;
    Parallel.ForEach(circles.Values, (circle, state) =>
    {
        double deltaX = cirlce.center.x - position.x;
        double deltaY = cirlce.center.x - position.x;
        double distance = Math.Abs(deltaX * deltaX + deltaY * deltaY);
        if (distance < circle.radius)
        {
            state.Break();
            circleIndex = circle.index;
        }
    }
    return circleIndex;
}

Basically it runs through all circles in parallel on the CPU and checks if the distance to the center is smaller than its radius, which would mean that the position is inside the circle.

Now my question is:
Is there a simple way, to run the same routine on the GPU instead of the CPU?

What I tried so far is a bit playing arround with Cloo and I was able to run a prime-searcher (see below) but I have no idea of how I can translate my own "circle-problem"-C# code in this program.

using Cloo.Extensions;

...

static void Main(string[] args)
{
    primes.ClooForEach(IsPrime);
}

static string IsPrime
{
    get
    {
        return
            @"
            kernel void GetIfPrime(global int* message)
            {
                int index = get_global_id(0);
                int upperl = (int)sqrt((float)message[index]);
                for(int i = 2; i <= upperl; i++)
                {
                    if(message[index]%i==0)
                    {
                        message[index]=0;
                        return;
                    }
                }
            }";
    }
}

I'm also glad to receive any other performance hints! :)

Pixel_95
  • 954
  • 2
  • 9
  • 21

0 Answers0