
CudaSharp - Making C# run on the GPU - codereflection
http://khyperia.blogspot.co.uk/2014/01/making-c-run-on-gpu.html
======
khyperia
Oh, wow, author of that library here, thanks for posting this! I originally
intended it to be a little fun experiment, but if there's real interest, then
I can certainly clean it up and make it usable.

Last night I changed it to not depend on llc.exe and instead use the llvm api
(had to compile llvm-3.4... that was not fun), which was one of the biggest
roadblocks I foresaw with respect to redistributability.

~~~
codereflection
Hey, hope you don't mind that I posted it here. I came across this on The
Morning Brew, and thought that a broader audience might appreciate your
efforts.

~~~
khyperia
I certainly don't mind! I've actually never had someone re-post a project of
mine, quite the surprise I got just now!

I've never heard of The Morning Brew, what exactly is it?

~~~
mkchandler
It's a daily blog post that rounds up some of the more popular .NET related
posts around the web: [http://blog.cwa.me.uk/](http://blog.cwa.me.uk/)

~~~
sn0v
Funny they'd call it that when they're not covering the rival (Java) :P

------
IanDrake
If you're looking for something more immediately usable, try Cudafy.NET.

~~~
j_s
lgpl - [http://cudafy.codeplex.com/](http://cudafy.codeplex.com/)

commercial -
[http://www.hybriddsp.com/Downloads.aspx](http://www.hybriddsp.com/Downloads.aspx)

------
VikingCoder
For the curious, you could also check out Brahma, which translates LINQ to
GPU.

[http://www.infoq.com/news/2010/05/Brahma](http://www.infoq.com/news/2010/05/Brahma)

~~~
voltagex_
or
[http://brahma.codeplex.com/SourceControl/latest](http://brahma.codeplex.com/SourceControl/latest)

------
m_mueller
There's also a commercial product called Quant Alea (targeted at the financial
market) that gives you CUDA on F#, including live Excel adaptors.

~~~
profquail
In fact, there are several options for running F# code on a GPU, some
commercial and some free/open-source:

[http://fsharp.org/use/gpu/](http://fsharp.org/use/gpu/)

------
bluej4ack
What advantages does it have over [http://research.microsoft.com/en-
us/projects/Accelerator/](http://research.microsoft.com/en-
us/projects/Accelerator/) ?

~~~
khyperia
Not sure. It's a quick research project, created by a bored high school
student (me). Edit: Although judging by the reaction it's getting, I very well
might try and release it with a stable version and really make it shine.

I haven't looked into Accelerator that much, so I'm not entirely sure how to
compare them. Please note, however, that the CudaSharp project was created no
more than four days ago, so doing a direct featureset comparison might give
some skewed results.

A quick skim of that link makes it look like Accelerator is using DirectX for
its GPU work. CudaSharp is using CUDA (via LLVM), so there's one major
difference. (I plan on eventually porting it to OpenCL as well - but there's
not much documentation on the OpenCL LLVM backend, I'm not sure if it even
exists)

~~~
bluej4ack
My understanding is that it supports both, but I may be mistaken

------
blt
This is cool, but I wonder if compiled C# code will be too branchy to run fast
on the GPU. So many bounds checks, exceptions, ...

~~~
khyperia
Right now, not a lot of C# code can actually be compiled to run on the GPU.
It's using a custom JIT compiler that I made, I've only spent about two days
working on it so far, so naturally it's nowhere near feature-complete.

Examples of things that it doesn't support are exceptions, dynamic allocation
of memory, the like. Essentially, you are writing CUDA kernel code, but in C#
instead. Whatever CUDA C can't do, you can't do (even if normal C# can) - for
example, exceptions aren't in CUDA C, so "throw" (and try/catch) won't ever be
supported.

Actually on topic to your point, it's being compiled through LLVM, which is a
very awesome compiler backend that does a bunch of optimizations on code.

------
Nilzor
What practical applications would this have?

~~~
_random_
Using a high-level mainstream language to increase math calculations
performance in an affordable way.

------
_random_
"Calling the CudaSharp.Translate method reads the CIL, translates to LLVM IR"

Does it open a door to transpiling C# to asm.js (sorry for off-topic)?

~~~
khyperia
I've never heard of asm.js before, but doing a bit of googling looks like it's
sort of like an LLVM backend.

Theoretically, yes. However, that translation from CIL to LLVM IR isn't the
easiest thing in the world. Right now I'm only supporting a subset of the
instructions that roughly correspond to the things available on the GPU, and
adding support for the _entire_ C# language is an extreme task. There's a
reason there's a huge team at MS dedicated to writing the JIT for .net.

But yes, it would be possible, it would just be a lot of work and probably
wouldn't support much of C#.

------
aashishkoirala
I like what you've done. I hope to see you take it all the way through. Nice
job!

