ComputeSharp, Run C# on the GPU
github.com
github.com
https://github.com/nick-funk/radium
It uses Cloo, so OpenCL if that's what you like. Includes examples for basic array operations as well as a full blown ray tracer so you can see how complex objects are passed to and from the GPU.
Caveat: you're writing C-esque compute kernels, not C# though.
Your variables would have to be accessed via pointers, but C# has robust pointer support and the compiler could probably rewrite them (JSIL did this)
I'm currently writing some updated docs to go over all these details, since the ones currently on GitHub only refer to the previous v1 release of the library.
Frankly, unless licensing is a problem, I think ComputeSharp author should just implement a DX12-based accelerator for ILGPU.
Looks like ComputeSharp is at a higher level of abstraction, or closer to what I expect with my background.
Anyway its definitely a valuable project and I'd love to give it a run.
ILGPU provides both Linux and MacOS.
If you wanted to try, you can clone the repository (make sure to be in the dev branch!) and then run the Benchmark project, which will show you some baseline difference in speed between the CPU and the GPU for some example algorithms. Let me know how it goes!
ILGPU's main brilliancy though is support for most interesting platforms: Windows, Linux, MacOS, and maybe even Android (via opencl). Also, hopefully, Nvidia-based IoT boards (via CUDA and OCL).
DX12 coverage is way less, and only really brings XBox to the table.
I am not saying your project is bad. It is also brilliant! I would love to work on it too, if days had just over 32-36 hours instead of 24 :-) But as a consumer of .NET open source package ecosystem, it would be MUCH better not to write GPGPU code twice using one library first for Mac, Linux and Android, and then again using another library for XBox (either works on Windows).
So I am asking you to be open minded here and seriously consider joining ILGPU project.
P.S. personally, I found ILGPU a few months ago, and I am in no way part of the project (yet).
And there is a practical example implementing raytracing [1].
I was looking for something a little simpler... I have two large arrays and I want to do simple mathematical operations on them.
Super simple, complete examples of a shader that just multiplies all items in a buffer by 2: https://github.com/Sergio0694/ComputeSharp/blob/dev/samples/...
Simple naive matrix-multiply-add shader: https://github.com/Sergio0694/ComputeSharp/blob/ada133aacd29...
Let me know if those help, and feel free to ping me on GitHub or in the C# Discord server if you have any issues!
- ComputeSharp uses exclusively DirectX as the backend, ILGPU has backends for CUDA/OpenCL/CPU. This means that ComputeSharp only works on Windows 10, but it has the benefit of not needing any other dependencies (no need to install CUDA or OpenCL to get GPU support). ILGPU instead should work on Linux/Mac too, but requires you to install those frameworks if you want your code to run on the GPU. - ComputeSharp is particularly tied to DX12 compute shaders and the HLSL language. This means it exposes many HLSL intrinsics and features, which makes it particularly easy to write/port existing shaders to it (from either HLSL or GLSL). I have some samples in the repo with some shaders ported from https://www.shadertoy.com/ to showcase this. ILGPU instead lets you write more "abstract" code in a way. - Since ComputeSharp is heavily tied to DirectX 12 APIs, it allows you to very easily to interop with DirectX and Win32 APIs if you wanted to. For instance, you can run a shader with ComputeSharp and then copy the results to some other DX resource manually, eg. to render that in a window. - ComputeSharp also exposes a number of additional DirectX types and features, such as 2D and 3D textures, and allows using custom pixel formats for your textures, which makes it particularly easy to write shaders that process images, as the GPU can take care of all the pixel format processing for you automatically. - ComputeSharp rewrites your C# code to HLSl at build time, whereas ILGPU has a runtime JIT to process the kernels. This makes ComputeSharp likely faster during the first run (as most of the work is done at build time) and with no need for dynamic code generation, but the C# code needs to follow some rules to be supported. ILGPU can be a bit more forgiving in the way it lets you write code, and it might be able to add some extra optimizations for you, like using CUDA streams.
Overall the two libraries are very different and I believe there's space for both of them in the ecosystem. The more open source projects there are for people to choose from, the better! Plus it's still a way for developers to learn more by also looking at what others are doing in any given area, and what approaches they're using to solve similar problems.
If you try ComputeSharp 2.0 out (eg. by running the samples), let me know how it goes!
I fully agree that both libraries use different approaches to realize .Net code execution on the GPU. Correct me if I'm wrong: ComputeSharp is a source->source translator for C# to HLSL (as you said) and ILGPU is an IL code->assembly just-in-time compiler for .Net programs (similar to the .Net runtime itself; except for the current implementation of the OpenCL backend...). ILGPU is designed to implement GPGPU programs highly efficiently on GPUs while having the ability to emulate all functions on the CPU for debugging (use case: HPC-inspired GPGPU computing workloads). It also includes a variety of different compiler optimizations specific to different GPU architectures. The primary focus has been to achieve high performance on NVIDIA GPUs that is comparable to "native" Cuda programs.
However, I also think there is definitely enough room for both libraries in the ecosystem (as they also target different groups of developers/users... :) ).
ILGPU basically does more things to just make your code well optimized from what I can see (with its full JIT compiler, like you mentioned), whereas ComputeSharp is more about letting you write a "C#-ified HLSL shader" that is then run as is. So you could say it's less forgiving but might give you more control (as in, you can basically port GLSL/HLSL shaders directly to C# and run them in ComputeSharp with almost no code changes (see https://twitter.com/SergioPedri/status/1363869460793335811 or https://twitter.com/SergioPedri/status/1364210592760872960).
Also as I mentioned before, which is one of the key differences (which is cool, I love that the two libraries have a very different structure and key features!), ComputeSharp is tightly coupled with DX12 APIs, which gives it some extra features like textures and automatic pixel format conversion, and makes it very easy to interop with other DX stuff (eg. to use a swap chain panel to render a shader to a window, like I do in one of my samples).
Goes without saying, I think ILGPU is a really cool project! I will definitely need to find the time to have a more in depth look at it, I'm sure there's plenty of cool things I could learn from that.
Cheers!