Accessing cuda device memory when the cuda kernel is running

后端 未结 4 1641
独厮守ぢ
独厮守ぢ 2021-01-22 19:32

I have allocated memory on device using cudaMalloc and have passed it to a kernel function. Is it possible to access that memory from host before the kernel finishes its executi

4条回答
  •  生来不讨喜
    2021-01-22 20:17

    You can't access GPU memory directly from the host regardless of a kernel is running or not.

    If you're talking about copying that memory back to the host before the kernel is finished writing to it, then the answer depends on the compute capability of your device. But all but the very oldest chips can perform data transfers while the kernel is running.

    It seems unlikely that you would want to copy memory that is still being updated by a kernel though. You would get some random snapshot of partially finished data. Instead, you might want to set up something where you have two buffers on the device. You can copy one of the buffers while the GPU is working on the other.

    Update:

    Based on your clarification, I think the closest you can get is using mapped page-locked host memory, also called zero-copy memory. With this approach, values are copied to the host as they are written by the kernel. There is no way to query the kernel to see how much of the work it has performed, so I think you would have to repeatedly scan the memory for newly written values. See section 3.2.4.3, Mapped Memory, in the CUDA Programming Guide v4.2 for a bit more information.

    I wouldn't recommend this though. Unless you have some very unusual requirements, there is likely to be a better way to accomplish your task.

提交回复
热议问题