views:

1047

answers:

6

I'm exploring various options for mapping common C# code constructs to C++ CUDA code for running on a GPU. The structure of the system is as follows (arrows represent method calls):

C# program -> C# GPU lib -> C++ CUDA implementation lib

A method in the GPU library could look something like this:

public static void Map<T>(this ICollection<T> c, Func<T,T> f)
{
   //Call 'f' on each element of 'c'
}

This is an extension method to ICollection<> types which runs a function on each element. However, what I would like it to do is to call the C++ library and make it run the methods on the GPU. This would require the function to be, somehow, translated into C++ code. Is this possible?

To elaborate, if the user of my library executes a method (in C#) with some arbitrary code in it, I would like to translate this code into the C++ equivelant such that I can run it on CUDA. I have the feeling that there are no easy way to do this but I would like to know if there are any way to do it or to achieve some of the same effect.

One thing I was wondering about is capturing the function to translate in an Expression and use this to map it to a C++ equivelant. Anyone has any experience with this?

+7  A: 

There's CUDA.Net if you want some reference how C# can be run on GPU.

Pop Catalin
GREAT link, thank you!!!
endian
I think you'll find that you can't run C# on a GPU like you said, but rather that you can lauch CUDA kernels from C#.
cr333
+2  A: 

To be honest, I'm not sure I fully understand what you are getting at. However, you may be interested in this project which converts .Net applications / libraries into straight C++ w/o any .Net framework required. http://www.codeplex.com/crossnet

torial
A: 

Interesting question. I'm not very expert at C#, but I think an ICollection is a container of objects. If each element of c was, say, a pixel, you'd have to do a lot of marshalling to convert that into a buffer of bytes or floats that CUDA could use. I suspect that would slow everything down enough to negate the advantage of doing anything on the gpu.

Die in Sente
A: 

What you could do would be to write an own IQueryable LINQ provider, as is done for LINQ to SQL to translate LINQ queries to SQL.

However, one problem that I see with this approach is the fact that LINQ queries are usually evaluated lazily. In order to benefit from pipelining, this is probably not a viable solution.

It might also be worth investigating how to implement Google’s MapReduce API for C# and CUDA and then use an approach similar to PyCuda to ship the logic to the GPU. In that context, it might also be useful to take a look at the already existing MapReduce implementation in CUDA.

Konrad Rudolph
A: 

That's a very interesting question and I have no idea how to do this.

However, the Brahma library seems to do something very similar. You can define functions using LINQ which are then compiled to GLSL shaders to run efficiently on a GPU. Have a look at their code and in particular the Game of Life sample.

cr333
+1  A: 

I would recommend the following process to accelerate some of your computation using CUDA from a C# program:

  • First, create an unmanaged C++ library that you P/Invoke for the functions you want to accelerate. This will restrict you more or less to the data types you can easily work with in CUDA.
  • Integrate your unmanaged library with your C# application. If you're doing things correctly, you should already notice some kind of speed up. If not, you should probably give up.
  • Replace the C++ functions inside your library (without changing its interface) to perform the computations on the GPU with CUDA kernels.
Eric