146

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?

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236

13 Answers13

167

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

Adil
  • 146,340
  • 25
  • 209
  • 204
mcmillab
  • 2,752
  • 2
  • 23
  • 37
  • 35
    +1 for updating a useful question in a kind of fast-developing subject. – philologon Feb 19 '14 at 22:22
  • 2
    Alea GPU http://www.quantalea.com/ provides CUDA support for all .NET languages, is fully cross platform and gives best developer experience with debugging and profiling of .NET GPU code. – Daniel Jun 03 '15 at 05:34
  • The OpenCL support in Cudafy is pretty bad - I never managed to even compile when my project grew larger. Hence I will stick with plain OpenCL and make bindings to C#. – Libor Jul 13 '15 at 13:38
  • OpenCL using Cudafy works fine for me, been using it for years now – mcmillab Jul 20 '15 at 22:46
  • Added links to projects to help future visitors. – Dan Atkinson Sep 11 '15 at 17:36
  • [Alea GPU V3](http://www.aleagpu.com) comes with very nice and easy to use GPU paralel for and parallel aggregate. Makes using the GPU a breeze. – Daniel Dec 17 '16 at 05:14
  • Cudafy seems to be the path to follow, it seems to be the most supported, exampled, and well used. – David C Fuchs Aug 15 '18 at 16:27
  • Updated my answer above on C++ AMP - now in "maintenance mode" (bug fixes only) and no longer being developed. – lightw8 Aug 22 '18 at 22:40
  • Latest newcomer is Campy: http://campynet.com/ – lightw8 Sep 13 '18 at 04:14
  • 4
    ILGPU (http://www.ilgpu.net): A modern, lightweight & fast GPU compiler for high-performance .Net programs. It's free! ILGPU is released under the University of Illinois/NCSA Open Source License. ILGPU is a free and non-sponsored project. It is being developed by a professional and passionate compiler, GPU and computer graphics developer. Support the project with contributions or some small donations in order to speed up the development process and to keep the project alive. – DanOver Aug 16 '20 at 23:55
49

Microsoft Research Accelerator was a .NET GP GPU library.

James Parsons
  • 6,097
  • 12
  • 68
  • 108
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • That was a great project with poor licensing policy. Unfortunately, no longer available at MS site... – ForNeVeR Mar 28 '16 at 08:13
27

I found Brahma... It also has a GPGPU provider that allows methods to run on the GPU... Thanks for the question... Learnt something new today. :)

Anderson Green
  • 30,230
  • 67
  • 195
  • 328
Vyas Bharghava
  • 6,372
  • 9
  • 39
  • 59
10

Here's another one: CUDAfy. It sounds like GPU.Net, in that something as simple as a method-attribute can cause the entire method to run on the GPU. But unlike GPU.Net, CUDAfy is free and open-source.

GPU.Net appears to require no boilerplate code, though (According to their docs, it's "injected automatically by the build-tool"), while CUDAfy does.


Here is an example of building an application with CUDAfy.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
9

Could I recommend XNA Game Studio as a possible avenue for exploration? It is obviously geared up for writing games, but gives you managed access to your graphics card and much better access to capability enumeration functions and shader development than was previously available in, say, Managed DirectX. There are also ways of combining WinForms and XNA into hybrid applications:

http://www.ziggyware.com/news.php?readmore=866

You'll have to put some effort into learning shader programming (XNA supports HLSL), but this may be a simpler approach than learning a vendor-specific solution such as nVidia's CUDA. The advantage is that you can program in a 100% managed environment. Here are some HLSL links:

http://www.ziggyware.com/weblinks.php?cat_id=9

The GPGPU site is also a recommended destination for general purpose GPU programming:

http://gpgpu.org/

Best of luck!

Dave R.
  • 7,206
  • 3
  • 30
  • 52
  • 2
    Greetings from the future. Though arguably a fine answer at the time (I dabbled with XNA quite a bit), sadly XNA is now defunct –  May 11 '18 at 14:56
  • @MickyD Great Scott! When I hopped into my DeLorean and travelled forward to 2018, I totally forgot to update this answer! If you're still interested in XNA, the spiritual successor is probably the cross-platform MonoGame: http://www.monogame.net – Dave R. May 14 '18 at 15:09
  • Lol. Thanks I’ll check that out –  May 14 '18 at 23:28
9

How about http://www.tidepowerd.com/ GPU.NET ?

pointernil
  • 608
  • 8
  • 17
  • 2
    I love the idea, but they stopped responding to tech support questions about two years ago, and the site has been more-or-less down for about a year, so I think the project is dead. Apparently the author [is on SO](http://stackoverflow.com/users/1197411/jack-p), though. – BlueRaja - Danny Pflughoeft Apr 08 '13 at 21:34
  • Tidedpowerd stoped the development of GPU.NET and closed their business. – Daniel Dec 17 '16 at 05:16
8

Well this is a pretty old question, and since it's been asked things have changed a lot.
Another option for using .Net to write GPU code, which no one has mentioned in answers in Alea GPU. It covers C#, F# and VB.

Professional GPU software development environment for .NET and Mono. Truly cross-platform

In F# official site, Alea is the first option for using F# in GPGPU programming.
To get to know this framework I suggest take a look at its comprehensive list of examples.

Rsh
  • 7,214
  • 5
  • 36
  • 45
  • 1
    Just saw your answer, will delete my post. See also Channel 9 interview by Seth Juarez [here](https://channel9.msdn.com/Blogs/Seth-Juarez/Radically-Simplified-GPU-Programming-with-C) and SO tag [here](http://stackoverflow.com/questions/tagged/aleagpu)) – lightw8 Dec 16 '16 at 02:36
  • @DavidCuccia Great job on checking your old answers. And also thanks for channel 9 link (Dec of 2016!) – Rsh Dec 16 '16 at 11:15
  • I think you mean this [Channel 9 recording](https://channel9.msdn.com/Blogs/Seth-Juarez/Radically-Simplified-GPU-Programming-with-C) – Daniel Dec 17 '16 at 05:12
  • @Daniel I meant "link to channel 9 recording". Was that ambiguous? Anyway, thanks for pointing out. – Rsh Dec 20 '16 at 14:02
  • @DavidCuccia sorry for confusion, link was hard to see – Daniel Dec 22 '16 at 14:03
7

There's a new Microsoft solution in town - C++ AMP (intro here).

Use from C# would be via P/Invoke, as demoed here for desktop apps, and here for (don't-call-it) Metro apps.

Edit: I should note that C++ AMP has an open specification, which means it's not necessarily just for the MS compiler, or just for Windows.

Edit: Apparently, the technology is now in "maintenance mode," meaning they're fixing bugs, but not actively developing.

lightw8
  • 3,262
  • 2
  • 25
  • 35
7

In addition to Brahma, take a look at C$ (pronounced "C Bucks"). From their CodePlex site:

The aim of [C$] is creating a unified language and system for seamless parallel programming on modern GPU's and CPU's.

It's based on C#, evaluated lazily, and targets multiple accelerator models:

Currently the list of intended architectures includes GPU, Multi-core CPU, Multi-GPU (SLI, CrossFire), and Multi-GPU + Multi-CPU Hybrid Architecture.

lightw8
  • 3,262
  • 2
  • 25
  • 35
2

CenterSpace Software has GPU-powered computation in their NMath libraries you can add to C# project. It's a commercial product.

Pasi Tuomainen
  • 496
  • 3
  • 10
2

If your GPUs are all the same brand, you might be able to get GPGPU support from the vendor, either through Nvidia's CUDA or ATI's Stream. AFAIK, they provide DLLs, which you could use through P/Invoke.

Coderer
  • 25,844
  • 28
  • 99
  • 154
1

Managed DirectX somehow, might work

Greg Dean
  • 29,221
  • 14
  • 67
  • 78
  • 3
    Greetings from the future. Though arguably a fine answer at the time, sadly MDX is now defunct having been replaced with XNA which is also defunct. –  May 11 '18 at 14:57
-1

WPF also uses the GPU and you can add custom shaders using HLSL.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • WPF doesn't have GP GPU computation access, to my knowledge. When talking about WPF System.Windows.Media graphics, it isn't real DirectX. Very slow when compared to lower-level vertex programming with SharpDX or SlimDX. – Pasi Tuomainen Jan 22 '14 at 10:42
  • I added a link to a series of articles on GPU-accelerated custom effects in WPF. – Mark Cidade Jan 22 '14 at 19:07