C# – Utilizing the GPU with c#


I am trying to get more processing power out of my grid.

I am using all cpus/cores, is it possible to utilize the GPU with C#.

Anyone know any libraries or got any sample code?

Best Solution

[Edit OCT 2017 as even this answer gets quite old]

Most of these answers are quite old, so I thought I'd give an updated summary of where I think each project is:

  • GPU.Net (TidePowerd) - I tried this 6 months ago or so, and did get it working though it took a little bit of work. Converts C# kernel code to cuda at compile time. Unfortunately their website has been down and their github hasn't been updated for a couple of years, which might indicate the project is dead....

  • Cudafy - Open source and very easy to use. Converts C# kernel code to cuda at runtime (with ability to serialize and cache). Can easily run the same kernel code on the CPU (mostly for debugging). Supports multiple GPUs. More examples available than others here. The boilerplate code referred to by other answers is minimal, and in my case at least helped with my understanding of how the code works. Cuda/Nvidia only though. Unfortunately, it seems that they didn't update their solutions for a couple of years too (latest commit in 2015 -- support of cuda 7.0).

  • Hybridizer. Commercial solution compiling C# to CUDA. Provides a free community edition on visual studio marketplace and samples on github.

  • AleaGPU Commercial solution with a free community edition for consumer GPUS. See Daniel's comments for details.

  • Brahma - runs LINQ expressions via OpenCL (so supports AMD too). Not much documentation / examples. Last update in 2011.

  • C$ - last development was over 10 years ago...

  • Microsoft Accelerator - similarly doesn't look like it is being actively developed any longer.

  • some others (C++ AMP, OpenTK -- dead/Cloo) - many of these are just bindings - ie enable you to call the GPU from C#, but your kernel code (code which is actually run on the GPU) needs to be written in C or OpenCL, meaning you must use (and learn) another language.

As I said, I would recommend Cudafy over all the others - if it could run on OpenCL as well as Cuda it would be perfect.

EDIT SEP 2013 Cudafy now allows you to compile for both CUDA and OpenCL, so will run the same C# code on on all GPUs. This sounds fantastic, though I haven't tested the OpenCL compiling yet.