I am looking at utilising the GPU for crunching some equations but cannot figure out how I can access it from C#. I know that the XNA and DirectX frameworks allow you to use shaders in order to access the GPU, but how would I go about accessing it without these frameworks?
-
NVidia, ATI, Intel or generic? – Bobby May 05 '11 at 08:30
-
@Bobby: I was looking for something generic, ideally. – Neil Knight May 05 '11 at 08:31
-
8It would be interesting to see an answer that shows how to detect the graphic card and either run the code in it or fallback to the CPU. – tiagoboldt May 05 '11 at 08:31
-
1@tiagoboldt - Theano has the ability to use a CPU or GPU, though it doesn't detect the existence of the card automatically. You'd have to link Theano with a BLAS library (eg, ATLAS), but it nevertheless can use CPU *or* GPU. – Brian Vandenberg May 05 '11 at 08:36
-
2Not an answer, but maybe helpful. [BarsWF](http://3.14.by/en/md5), a very fast MD5 cracker which is utilizing either SSE2, CUDA or ATI has been [released under the MIT License](http://3.14.by/forum/viewtopic.php?f=8&t=1333&p=8907). You might want to have a look at that. – Bobby May 05 '11 at 09:10
-
@tiagoboldt OpenCL runs on Intel, AMD and ARM CPUs, GPUs (NVidia, AMD/ATI and Mali) and Cell processors, from a single source. There are several C# OpenCL libraries available. – 3Dave Mar 18 '13 at 15:06
6 Answers
I haven't done it from C#, but basically you use the CUDA (assuming you're using an nVidia card here, of course) SDK and CUDA toolkit to pull it off.
nVidia has ported (or written?) a BLAS implementation for use on CUDA-capable devices. They've provided plenty of examples for how to do number crunching, although you'll have to figure out how you're going to pull it off from C#. My bet is, you're going to have to write some stuff in un-managed C or C++ and link with it.
If you're not hung-up on using C#, take a look at Theano. It might be a bit overkill for your needs, since they're building a framework for doing machine learning on GPUs from Python, but ... it works, and works very well.

- 4,011
- 2
- 37
- 53
How about Brahma (LINQ to GPU)?
Gotta love LINQ!

- 1,709
- 3
- 17
- 27

- 31,770
- 9
- 95
- 162
-
1Oops, just found out i mucked up the link. Thanks for the edit @Bobby, (still learning SO). – George Duckett May 05 '11 at 09:18
If your GPU is NVidia, you can use CUDA.
There is an example here, that explain all the chain, including some C/C++ code: CUDA integration with C#
And there is a library called CUDA.NET available here: CUDA.NET
If your GPU is ATI, then there is ATI Stream. .NET support is less clear to me on this. Maybe the Open Toolkit Library has it, through OpenCL support.
And finally, there is an Microsoft Research project called "Accelerator" which has a managed wrapper which should work on any hardware (provided it supports DirectX 9).

- 132,049
- 21
- 248
- 298
I have done it in C# by leveraging NVIDIA's CUDA libraries and .NET's P/invoke. This requires some careful memory management and a good detailed understanding of the CUDA libraries. This technique can be used in conjunction with any custom GPU/CUDA kernels you would like to create in C, so it's a very powerful flexible approach.
If you would like to save yourself a lot of effort you could buy NMath Premium from CenterSpace software (who I work for) and you can be running large problems on your NVIDIA GPU in minutes from C#. NMath Premium a large C#/.NET math library that can run much of LAPACK and FFT's on the GPU, but falls back to the CPU if the hardware isn't available or the problem size doesn't justify a round trip to the GPU.

- 5,376
- 1
- 20
- 19
I'm afraid that my knowledge of using the GPU is rather theoretical beyond writing shaders for DirectX / XNA and dabbling a little bit with CUDA (NVidia specific). However, I have heard quite a lot about OpenCL (Open Computing Language) which allows you to run algorithms which OpenCL will intelligently push out to your graphics cards, or run on the CPU if you don't have a compatible GPU.
The code you run on the GPU will have to be written specifically in OpenCL's subset of C99 (apologies if this does not meet your reqiurements as you've asked how to use it from C#), but beyond your number crunching algorithms, you can write the rest of your application in C# and have it all work together nicely by using The Open Toolkit;

- 2,591
- 1
- 20
- 24
There are two options if you don't want to mess with P/Invoke stuff and unmanaged code:
- Use mentioned CUDA.NET library. It works very well, but it's targeting CUDA, so only nVidia cards. If you'd like to solve more complex problems you'd have to learn CUDA, write your own kernel (in C...), compile it with nvcc and execute from C# via this library.
- Use Microsoft Research Accelerator. It's a nice library build by MS Research that runs your code on anything that has lots of cores (many-core nVidia/ATI GPUs and multi-core processors). It's completely platform independent. Used it and I'm pretty impressed with the results. There is also a very good tutorial on using Accelerator in C#.
The second option is that I'd recommend, but if you have no problem with sticking to nVidia GPUs only - the first would probably be faster.

- 13,371
- 6
- 44
- 61