How to make a CUDA dll that can be used in a C # application?

It would be nice if you could give me a short guide, and not a few words.

My CUDA application works the way I wanted. Now the problem is how to export the CUDA code in C #, since I would like to do the interface and everything else in C #.

From this link:

http://www.codeproject.com/Articles/9826/How-to-create-a-DLL-library-in-C-and-then-use-it-w

I know how to create a C library that can be imported into a C # application as a Win32 DLL.

But my question is: how to make a CUDA application dll (or another extension) that can be sent to C # and used from a C # application? It would be nice if there was a tutorial for CUDA somewhere, for example, for the C library for the C # application (link above).

I am using Win7 64 bit, Visual Studio 2010 Ultimate, Cuda Toolikt 5.0 and NSight 2.2.012313

+4
source share
1 answer

ManagedCUDA is ideal for this type of thing. First you need to follow the instructions in the documentation to set up your Visual Studio project.

Here is an example solution:

test.cu (compiled in test.ptx)

#if !defined(__CUDACC__) #define __CUDACC__ #include <host_config.h> #include <device_launch_parameters.h> #include <device_functions.h> #include <math_functions.h> #endif extern "C" { __global__ void test(float * data) { float a = data[0]; float b = data[1]; float c = data[2]; data[0] = max(a, max(b, c)); } } 

and here is the C # code:

 private static void Test() { using (CudaContext ctx = new CudaContext()) { CudaDeviceVariable<float> d = new CudaDeviceVariable<float>(3); CUmodule module = ctx.LoadModulePTX("test.ptx"); CudaKernel kernel = new CudaKernel("test", module, ctx) { GridDimensions = new dim3(1, 1), BlockDimensions = new dim3(1, 1) }; kernel.Run(d.DevicePointer); } } 

This is just a proof of concept, the deviceโ€™s memory is not even initialized, and the result is not readable, but enough to illustrate how to do this.

You have several options for distributing your application. In this case, I decided to collect the .cu file in PTX and load it inside the C # project from the file system.
You can also embed PTX as a resource directly in your C # application.
You can also compile the cube and load or paste it instead of PTX.

+3
source

All Articles