0

I have an IntPtr hData that points to the beginning of an array of data that is stored in unmanaged memory. When I try to pass it using the CUDA kernel described below, I get a System.Exception: i64 is not a struct type. How should I be passing in a pointer to an array in unmanaged memory using an Alea CUDA kernel?

unsafe private static void CopyDataToDeviceMemory(
    IntPtr hData,
    deviceptr<float> dData,
    int dataLength)
{
    int start = blockIdx.x * blockDim.x + threadIdx.x;
    int stride = gridDim.x * blockDim.x;

    for (int i = start; i < dataLength; i += stride)
    {
        dData[i] = DeviceFunction.Convert<ushort, float>(
            *((ushort*)(hData + i * 2)));
    }
}
ArKi
  • 681
  • 1
  • 10
  • 22
  • I'm not sure I understand the question completely but maybe you're trying to use the IntPtr as a value instead of a pointer. Try marshaling the value back and using that like: var hDataValue = Marshal.ReadInt64(hData); and then using the hDataValue. – Michael Puckett II Apr 06 '17 at 03:34
  • I do see you're using ushort* and converting it to * for pointer but wouldn't hData + 1 * 2 be doing work on a pointer and not the value? If I'm wrong I apologize; it's late here. – Michael Puckett II Apr 06 '17 at 03:36
  • @MichaelPuckettII I am trying to do pointer arithmetic. So `hData` points to the beginning of the array, then `hData + 1` should point to the next byte, and so on. I am trying to basically copy the array that `hData` points to into `dData`. – ArKi Apr 06 '17 at 03:43
  • gotcha, I see that now. :/ – Michael Puckett II Apr 06 '17 at 03:49
  • 1
    Where does hData locate? I guess it is on host, right? if so, you cannot directly use it in a kernel. You must first transfer the hData into device, using functions such as `Alea.CUDAInterop.cuMemcpyHtoD`. – Xiang Zhang Apr 06 '17 at 07:42
  • Ah I see, thank you @XiangZhang. However, does this mean I can't get CUDA to do a `short` to `float` copy? I read somewhere that you can do it with textures... – ArKi Apr 06 '17 at 16:05
  • @XiangZhang thanks for sharing about `Alea.CUDAInterop`, however, I was unable to find `cuMemcpyHtoD` in the 3.0.2 documentation? Is there any way to do this using the C# library? – ArKi Apr 07 '17 at 00:20
  • @ArKi Alea.CUDAInterop is just a P/Invoke interface for CUDA Driver API (http://docs.nvidia.com/cuda/cuda-driver-api/group__CUDA__MEM.html#group__CUDA__MEM), and since you are using unmanaged data, so there is no wrapping on them yet. The doc of this P/Invoke interface is actually the NVIDIA documentation. I try to create you an example these days when I found some time. – Xiang Zhang Apr 07 '17 at 09:03

0 Answers0