Running .NET Code on the GPU

Using Alea GPU to take advantage of GPU computing from .NET.

Published on January 4, 2018

We all know the GPU is exceptional at graphics processing but did you know the GPU also excels at highly parallel compute workloads? For certain operations, the GPU can offer a 100x speedup against the CPU. This sort of power has traditionally been accessible exclusively from C and C++ but has recently been made available from .NET.

Unlike the CPU, the GPU is made up of thousands of weaker cores which are extremely effective at working in parallel. In 2007 NVIDIA released CUDA which is a platform that enables developers to tap into the extraordinary compute power of the GPU. Leveraging the GPU for high performance compute workloads is known as GPU acceleration and serves as the basis for all modern artificial intelligence. Alea GPU is a commercial solution with free offerings that enables .NET code to be JIT compiled into CUDA binary which runs just as fast as CUDA code compiled from C or C++.

public static void Kernel<T>(T[] result, T[] arg1, T[] arg2, Func<T, T, T> operation)
    var start = blockIdx.x * blockDim.x + threadIdx.x;
    var stride = gridDim.x * blockDim.x;
    for (var i = start; i < result.Length; i += stride)
        result[i] = operation(arg1[i], arg2[i]);

Alea GPU offers several luxuries that even CUDA within C++ lacks such as automatic memory management and transfer. An obvious advantage for .NET developers is the ability to write GPU accelerated code alongside the rest of the application. Alea GPU offers bindings to many of the underlying CUDA APIs including cuBLAS and cuDNN. I have written GPU accelerated code in both C++ and C# so I can personally vouch for the .NET solution.