Cuda with C# in VS2008

Hello, I am a C# Programmer, and I have some plans to begin a neural network, for use in sensory processing.

This hasn’t been possible up until now because of the lack of affordable processing power, however with CUDA it is possible for an independant experimenter (without funding from a university or employer) to do work of this type.

Unfortunately I would really prefer to use C# (much of my existing code and research already exists in C#, specifically for VS2008 using some of the new constructs available in that version).

Anyway, is there a simple way to wrap CUDA for use in C#? I have seen a few discussions of ways to do this, but nothing concrete…

Also is this evey feasable? like would I simply be making calls to implimened math functions on the cuda architecture, and if so, would my app not suffer from a bottleneck with the IO then? or can I develop custom algorithms to “run” on the cuda framework while being centrally controlled by a C# .net app. (basically I want to adapt my existing neural net code to have a neuron object which can function on the GPU within a stream processor, then the entire net can be easily distributed across all the stream processors available and executed from a central app.

I CAN do this in C++, but I would really rather not, as it means porting a ton of code, and getting back into the C++ mindset again (I use C# for many other projects right now as well, and I don’t do well with more than one “active” programming language at a time lol). Last time I used c++ seriously was like 8 years ago lol.

Thanks in advance for any info or advice you can offer!

Do neural networks take a lot of processing power?

To answer your question, you’re exactly on the right track. You would make a win32 dll that uses CUDA and has your core math code, then use P/Invoke to call into it and handle all the networking, etc from C#.

I’ve never tried this myself, however. Search around on the boards about C# and DLL’s, many people have been down this path.

Neural networks are both processor intensive, and very easily scale into parallel processing. (depending on the specific type of NN your talking about, and what it’s being used for).

Doing any kind of sensory processing can be very cpu intensive. (also depending on how your neural model is setup, ranging from simple dot product, to complex mathematical modeling of biological neurons for example).

Anyway, yeah I just stumbled onto a few things, like CUDA.NET which is apparently released now, which is a .NET wrapper for the CUDA API. That might do what I want to do, but I am still concerned with the overhead of the communication between the cpu/gpu using a wrapper like that…

I guess I’m just being a bit lazy and don’t really want to take the extra time to impliment all my math code in C++ on CUDA in order to build my own custom DLL lol…

Does anyone have any experience with CUDA.NET and it’s performance? (it apparently even comes with samples in C#)


I haven’t tried CUDA.NET – I downloaded it and played with it a bit, then experimented with writing my own bindings using P/Invoke (I’m also a primarily .NET programmer, and didn’t want to have to mess with C too much). In any case, if you’re doing anything more than making calls to CUBLAS or CUFFT (i.e. custom kernels), you’re going to have to write your kernels in C, then compile them to an unmanaged DLL and use P/Invoke on it.

However, I’ve done some preliminary research on extending the .NET JIT compiler to support all-managed code, but I haven’t even gotten close to the implementation part of that one yet.

Alas, to answer your question – unless you can represent your neural network in vector/matrix format and do the computations with CUBLAS, you’re stuck writing the CUDA kernels in C.

One more thing to take a look at…I’ve heard of a company doing neural network stuff with CUDA, called Evolved Machines. You may want to take a look at their stuff…perhaps they at least have some DLL’s that you can just P/Invoke to save you some trouble of writing your own kernels.

That’s a good point. Just making repeated calls out to CUBLAS won’t make for a very effective algorithm (at least not that will effectively use the GPU). I’ll have to look into this further… Perhaps I should just download the SDK and try to start from square one (building sample apps) and then worry about getting a sane dev environment in VS2008. Once I have that I can build my own kernels easily enough I guess…

Yeah I was reading up on Evolved Machines as well, their model is based on 100% accurate simulation of a real biological neuron. (WAY out of my league as far as my project scope is concerned, and EXTREMELY more processor intensive than what I’m working with.) I’m just working with a relatively simple mathematical model of neural function, with a few tricks thrown in of my own. (I already have the basic neural network model figured out, but getting networks of more than a couple thousand neurons will bring my current cpu to a crawl… So I hope that if I can get this thing ported to CUDA I could run significantly larger simulations to try more complex problems… Like sensory data processing).

Also, from what I understand, you could compile your kernels into .cubin files, and load/run them by p/invoking the nvcuda dll from .NET. That may be the most ‘managed’ way to do things at this point.

You don’t have to implement all your math code inside the DLL, you can just put the kernel calls, and maybe some arrays that don’t need to be seen from C#. But porting math code from C# to C++ should be pretty easy.

Oh yeah, it will be easy to port, the languages are very similar already, I’m just lazy ;) lol (more accurately it’s taking the time to get familiar with building CUDA apps so I am comfortable with the platform, which I guess I should do no matter what… )

It’s not so much the time to port the code, it’s the time to familiarize myself with a new dev environment first, then port the code (since from what I understand CUDA doesn’t play so nice in visual studio)

Perhaps I am wrong on that point though. Anyway, won’t have time to play with it until later this week, or even next week unfortunately.

EDIT: Ok now that I’m done swallowing my foot, I downloaded the toolkit and sdk, and turns out it plays just fine with Visual Studio lol… Going to play with it and see where I get using some simple examples before I try to port my NN code into a new custom Kernel.