Turning C# methods into C++ methods

Turning C# methods into C++ methods

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?

Passing “const” variable to method in Java


Should I wrap all my c++ code in its own namespace?
There's CUDA.Net if you want some reference how C# can be run on GPU..
How to use swig to generate php interface for c++ so

Are missing invariants for an object always a sign of bad design? [closed]

Unmanaged thread running Managed Code


Consequences of these Pointer Errors in C++ vs Managed
To be honest, I'm not sure I fully understand what you are getting at.

What is the best Framework to use for developing a skinned application?
However, you may be interested in this project which converts .Net applications / libraries into straight C++ w/o any .Net framework required.

Decrypting RijndaelManaged Encrypted strings with CryptDecrypt


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.


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..


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..


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..

62 out of 100 based on 57 user ratings 382 reviews