kunzmi / managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.
Other
440 stars 79 forks source link

It all started as a hobby project to easily access CUDA from C# - at that time CUDA was available in version 3. Now more than 10 years later, managedCuda is still alive and is updated regularly by me to the latest versions of CUDA. In order to support further developments, I switched from the LGPL license to a dual-license GPLv3 / commercial license starting with managedCuda for Cuda version 12 onwards. In case you plan to use managedCuda 12 for a commercial project, please contact me by mail: managedcuda@articimaging.eu. If you use the open-source license and want to contribute to future development, you can donate me a beer here: Support via PayPal

Official nuget packages

One can find multiple packages for managedCuda on nuget, but the official packages are:

managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.

For this it includes:

//init some ramdom values double[] randoms = new double[length]; for (int i = 0; i < length; i++) { randoms[i] = rand.NextDouble(); }

//Alloc device memory CudaDeviceVariable a = randoms; CudaDeviceVariable b = new CudaDeviceVariable(length); b.Set(10.0); //NPPs method int size = a.MeanGetBufferSize(); //NPPs method //Alloc temporary memory for NPPs mean method CudaDeviceVariable buffer = new CudaDeviceVariable(size); CudaDeviceVariable mean = new CudaDeviceVariable(1);

a.Mul(b); //NPPs method a.DivC(10.0); //NPPs method a.Mean(mean, buffer); //NPPs method

//Copy data back to host double m = mean; double[] res = a;

//Clean up mean.Dispose(); buffer.Dispose(); b.Dispose(); a.Dispose();


- The new feature 'per thread default stream' is available as a compiler directive of the managedCuda main library: Compile the library with the option "_PerThreadDefaultStream" to enable it.

# Note about Cuda context
Nvidia changed the cuda context behavior in the cuda libraries (NPP, Cufft, etc.) why it is highly recommended to use a ```PrimaryContext``` instead of a ```CudaContext``` when using ManagedCUDA together with Cuda libraries. To create a ```PrimaryContext``` in ManagedCUDA, use the following lines of code:
```C#
int deviceID = 0;
PrimaryContext ctx = new PrimaryContext(deviceID);
// Set current to CPU thread, mandatory for a PrimaryContext
ctx.SetCurrent();

NppStreamContext

In order to use the NppStreamContext-API of NPP, initialize a NppStreamContext like this:

CudaStream cudaStream = new CudaStream();          //optional, 
NPPNativeMethods.NPPCore.nppSetStream(cudaStream); //if not set, NPP will work on the default null-stream
NppStreamContext nppCtx = NPPNativeMethods.NPPCore.nppGetStreamContext();