有没有人曾经在VB.NET中成功使用过CUDAfy?

时间:2015-11-16 19:11:29

标签: vb.net cudafy.net

我正在尝试使用VS2013在VB.NET中使用CUDAFy 1.29。

我试图从CUDAFy翻译C#的样本,我有两种类型的错误,如下所述:

我的变量

Shared cs_CC As String = "adiciona"
Shared MyGPU As GPGPU = Nothing
Shared Arch As eArchitecture = Nothing

我的代码:

 Shared Executa()

 if Loader = true

        Dim Modulo = CudafyModule.TryDeserialize(cs_cc)
        If IsNothing(Modulo) OrElse (Not Modulo.TryVerifyChecksums) Then
            Modulo = CudafyTranslator.Cudafy(ePlatform.All, Arch, cs_CC.GetType)
            Modulo.Serialize()
        End If

        MyGPU.Loadmodule(Modulo)

        Dim a As Integer() = New Integer(N - 1) {}
        Dim b As Integer() = New Integer(N - 1) {}
        Dim c As Integer() = New Integer(N - 1) {}

        ' allocate the memory on the GPU
        Dim dev_a As Integer() = MyGPU.Allocate(Of Integer)(a)
        Dim dev_b As Integer() = MyGPU.Allocate(Of Integer)(b)
        Dim dev_c As Integer() = MyGPU.Allocate(Of Integer)(c)

        ' fill the arrays 'a' and 'b' on the CPU
        For i As Integer = 0 To N - 1
            a(i) = i
            b(i) = 2 * i
        Next

        ' copy the arrays 'a' and 'b' to the GPU
        MyGPU.CopyToDevice(a, dev_a)
        MyGPU.CopyToDevice(b, dev_b)


        For i As Integer = 0 To 128
            MyGPU.Launch(1, 1).adiciona(dev_a, dev_b, dev_c)
        Next

 end if

End Sub

将在CUDA上运行的ADICIONA函数

 <Cudafy()> _
  Shared Sub adiciona(thread As GThread, a As Integer(), b As Integer(), c As Integer())

    Dim tid As Integer = thread.blockIdx.x
    While tid < N
        c(tid) = a(tid) + b(tid)
        tid += thread.gridDim.x
    End While
   End Sub

LOADER:尝试识别卡和CUDA(成功运行):

Public Shared Function Loader() As Boolean

    DeviceType = eGPUType.Cuda
    CudafyModes.Target = DeviceType
    CudafyTranslator.Language = If(CudafyModes.Target = eGPUType.Cuda, eLanguage.Cuda, eLanguage.OpenCL)
    Dim CompatibleDevice As GPGPUProperties() = CudafyHost.GetDeviceProperties(CudafyModes.Target, True).ToArray

    If Not CompatibleDevice.Any Then        '  não possui um full-CUDA device
        MsgBox("I do not found any OpenCL or CUDA compatible device")
        Return False
    End If

    Dim selectedDevice As GPGPUProperties = CompatibleDevice(0)
    If IsNothing(selectedDevice) Then
        MsgBox("I cannot allocate a compatible device")
        Return False
    End If


    CudafyModes.DeviceId = selectedDevice.DeviceId
    Thread_per_Block = selectedDevice.MaxThreadsPerBlock
    Blocks_per_Grid = selectedDevice.MaxThreadsSize.x
    Shared_Mem_per_Block = selectedDevice.SharedMemoryPerBlock

    MyGPU = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId)
    Arch = MyGPU.GetArchitecture

    Return True

 End Function

问题:

问题1:如果我只使用 dim Modulo作为CudafyModule = CudafyTranslator.Cudafy(),我会收到以下错误:不支持检查语句。< / strong>很奇怪! Cudafy的所有文档都完全按照它显示这一行!

问题2:因此,我尝试检查是否存在已写入的模块(.CDFY),如果不存在,则调用Serialize()函数。问题是该函数在我的文件夹中创建一个名为 STRING.CDFY 的文件,但不是 ADICIONA.CDFY ,也不是 ADD_LONG_LOOP.CDFY 。是对的。由于我想避免在每次运行此代码时编译,如何正确地使CUDAFy编写它?

问题3:当VS运行时,一切正常,直到调用ADICIONA(MyGPU.Launch)! VS通过消息“在模块中找不到'ADICIONA'”来停止执行。

有趣的是注意到:

1-创建两个临时文件(.CU,.PTX)以及.CDFY文件。它证明了NVCC编译器运行良好并创建了CUDA模块。那么,为什么代码没有找到函数ADICIONA?

2-用C#编写的所有样本都在这里运行100%。从C#和VB的转换似乎没问题(我用过TELERIK来做)。我不认为问题可能与此有关,但我可能错了。

3-问题与NVCC编译器和VB.NET的一些引用无关,因为代码编译。

我试着用CODEPLEX写一个答案。根本没有答案...... 我试图在Internet上看到很多样本,但所有这些都是为CUDAFy C#创建的,并且没有一个使用1.29和CUDA 7.5版。

另外,我想了解为什么基本功能(CudafyTranslator.Cudafy())在VB中出错但在C#中没有。

那么,有没有人使用VB.NET成功创建了CUDAFy代码?

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

要解决问题1,请选中项目属性&gt;下的“删除整数溢出检查”。编译&gt;高级编译选项&gt;优化。更多信息:http://forums.asp.net/post/715302.aspx

答案 1 :(得分:0)

尝试以下简单示例:

Imports Cudafy
Imports Cudafy.Host
Imports Cudafy.Translator

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim GPU As GPGPU = CudafyHost.GetDevice(eGPUType.OpenCL, CudafyModes.DeviceId)
        Dim Modulo As CudafyModule = CudafyTranslator.Cudafy(ePlatform.All, GPU.GetArchitecture())

        GPU.LoadModule(Modulo)

        Dim Resultado As Integer
        Dim ResultadoGPU As Integer() = gpu.Allocate(Of Integer)()

        GPU.Launch().Adicionar(2, 7, ResultadoGPU)
        GPU.CopyFromDevice(ResultadoGPU, Resultado)
        MessageBox.Show("2 + 7 = " & Resultado)

        GPU.Launch().Subtrair(2, 7, ResultadoGPU)
        GPU.CopyFromDevice(ResultadoGPU, Resultado)
        MessageBox.Show("2 - 7 = " & Resultado)

        GPU.Free(ResultadoGPU)
    End Sub

    <Cudafy()>
    Private Shared Sub Adicionar(a As Integer, b As Integer, c As Integer())
        c(0) = a + b
    End Sub

    <Cudafy()>
    Private Shared Sub Subtrair(a As Integer, b As Integer, c As Integer())
        c(0) = a - b
    End Sub
End Class