kunzmi / managedCuda

ManagedCUDA aims an easy integration of NVidia's CUDA in .net applications written in C#, Visual Basic or any other .net language.
Other
440 stars 79 forks source link

Support for cudaMemcpy2DToArrayAsync #49

Closed dennisai closed 7 years ago

dennisai commented 7 years ago

Hi,

I am using multiple streams w/ 2D textures in my application, and would like to asynchronously copy from a CudaPitchedDeviceVariable to a CudaTextureArray2D/CudaArray2D. Now, I see from the NVIDIA documentation here that there is a method called cudaMemcpy2DToArrayAsync, which might suit my purposes. However, when I looked through the API for CudaArray2D, I was only able to find synchronous copy methods (I checked the source). Is there any way to asynchronously copy to a CudaArray2D using a stream, using the current ManagedCuda API?

If not, is that a feature we can add? I'd be happy to help and make a pull request if someone would be able to help point me in the right direction!

Thanks, Dennis

dennisai commented 7 years ago

I tried writing the following code, and it seems to work!

private void CopyToArray2D(
    CudaPitchedDeviceVariable<float> src,
    CudaArray2D dst,
    CudaStream stream)
{
    CUDAMemCpy2D copyParams = new CUDAMemCpy2D();
    CUResult copyResult;

    copyParams.srcDevice = src.DevicePointer;
    copyParams.srcMemoryType = CUMemoryType.Device;
    copyParams.srcPitch = src.Pitch;
    copyParams.dstArray = dst.CUArray;
    copyParams.dstMemoryType = CUMemoryType.Array;
    copyParams.Height = src.Height;
    copyParams.WidthInBytes = src.WidthInBytes;

    if (stream != null)
    {
        copyResult = DriverAPINativeMethods.AsynchronousMemcpy_v2
            .cuMemcpy2DAsync_v2(ref copyParams, stream.Stream);
    }
    else
    {
        copyResult = DriverAPINativeMethods.SynchronousMemcpy_v2
            .cuMemcpy2D_v2(ref copyParams);
    }

    if (copyResult != CUResult.Success)
    {
        throw new CudaException(copyResult);
    }
}