如何使CUDA dll在c#应用程序中使用?

时间:2022-09-02 09:48:47

It would be good if you could give me a brief tutorial instead of a few words.

如果你能给我一个简短的指导,而不是几个字,那就太好了。

My CUDA application is working as I wanted. Now, the problem is how to export CUDA code to C# as I would like to make front end and everything else in C#.

我的CUDA应用程序正在按我的要求工作。现在,问题是如何将CUDA代码导出到c#中,就像我想在c#中创建前端和其他所有东西一样。

From this link:

从这个链接:

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

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

I know how to make a library in C language that can be imported into C# application as Win32 dll.

我知道如何用C语言创建一个库,它可以作为Win32 dll导入到c#应用程序中。

But my question is, how to make CUDA application dll (or some other extension) that can be shipped to C# and used from C# application? It would be good if there is somewhere tutorial for CUDA like the one for C library to C# app(above link).

但是我的问题是,如何使CUDA应用程序dll(或其他扩展)可以发送到c#并从c#应用程序中使用?如果有CUDA的教程,比如C库到c# app的教程(见链接),那就更好了。

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

我使用Win7 64位,Visual Studio 2010 Ultimate, Cuda Toolikt 5.0和NSight 2.2.012313。

1 个解决方案

#1


3  

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

ManagedCUDA非常适合这种类型的东西。首先,您需要按照文档中的说明来设置Visual Studio项目。

Here is an example of a solution:

这里有一个解决方案的例子:

test.cu (compiles to test.ptx)

测试。铜(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:

这里是c#代码:

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 memory is not even initialized and the result is not read but is enough to illustrate how to do it.

这只是概念的证明,设备内存甚至没有初始化,结果也没有读取,但足以说明如何执行。

You have several options how to distribute your application. In this case i opted for compiling the .cu file into PTX and load it inside the C# project from filesystem.
You could also embed the PTX as a resource directly into your C# application.
You could also compile into a cubin and load or embed that instead of PTX.

您可以选择如何分发应用程序。在本例中,我选择将.cu文件编译为PTX,并从文件系统中加载到c#项目中。您还可以将PTX作为资源直接嵌入到c#应用程序中。您还可以编译成cubin和load或嵌入,而不是PTX。

#1


3  

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

ManagedCUDA非常适合这种类型的东西。首先,您需要按照文档中的说明来设置Visual Studio项目。

Here is an example of a solution:

这里有一个解决方案的例子:

test.cu (compiles to test.ptx)

测试。铜(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:

这里是c#代码:

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 memory is not even initialized and the result is not read but is enough to illustrate how to do it.

这只是概念的证明,设备内存甚至没有初始化,结果也没有读取,但足以说明如何执行。

You have several options how to distribute your application. In this case i opted for compiling the .cu file into PTX and load it inside the C# project from filesystem.
You could also embed the PTX as a resource directly into your C# application.
You could also compile into a cubin and load or embed that instead of PTX.

您可以选择如何分发应用程序。在本例中,我选择将.cu文件编译为PTX,并从文件系统中加载到c#项目中。您还可以将PTX作为资源直接嵌入到c#应用程序中。您还可以编译成cubin和load或嵌入,而不是PTX。