Backends/SiaNet.Backend.TensorSharp/Cuda/KernelOps/CopyOps.cs
/// <summary>
/// Copies the gpu to cpu.
/// </summary>
/// <param name="result">The result.</param>
/// <param name="src">The source.</param>
/// <param name="totalElements">The total elements.</param>
/// <exception cref="CudaException"></exception>
Copies the gpu to cpu.
Copies the gpu to cpu .
public void CopyGpuToCpu(NDArray result, NDArray src, long totalElements)
        {
            var context = CudaHelpers.TSContextForTensor(src);
            var srcContext = context.CudaContextForTensor(src);
            using (var srcContig = Ops.AsContiguous(src))
            using (var resultContig = AsTypeCpu(result, src.ElementType, true))
            {
                var resultContigPtr = ((Cpu.CpuStorage)resultContig.Storage).PtrAtElement(resultContig.StorageOffset);
                var srcContigPtr = ((CudaStorage)srcContig.Storage).DevicePtrAtElement(srcContig.StorageOffset);
                var totalBytes = totalElements * srcContig.ElementType.Size();
                var res = DriverAPINativeMethods.SynchronousMemcpy_v2.cuMemcpyDtoH_v2(resultContigPtr, srcContigPtr, totalBytes);
                if (res != CUResult.Success) throw new CudaException(res);
                if (result.Storage != resultContig.Storage)
                {
                    Ops.Copy(result, resultContig); 
                }
            }
        }
public void CopyGpuToCpu ( NDArray result , NDArray src , long totalElements ) { var context = CudaHelpers . TSContextForTensor ( src ) ; var srcContext = context . CudaContextForTensor ( src ) ; using ( var srcContig = Ops . AsContiguous ( src ) ) using ( var resultContig = AsTypeCpu ( result , src . ElementType , true ) ) { var resultContigPtr = ( ( Cpu . CpuStorage ) resultContig . Storage ) . PtrAtElement ( resultContig . StorageOffset ) ; var srcContigPtr = ( ( CudaStorage ) srcContig . Storage ) . DevicePtrAtElement ( srcContig . StorageOffset ) ; var totalBytes = totalElements * srcContig . ElementType . Size ( ) ; var res = DriverAPINativeMethods . SynchronousMemcpy_v2 . cuMemcpyDtoH_v2 ( resultContigPtr , srcContigPtr , totalBytes ) ; if ( res != CUResult . Success ) throw new CudaException ( res ) ; if ( result . Storage != resultContig . Storage ) { Ops . Copy ( result , resultContig ) ; } } }
Copies the gpu to cpu.
Copies the gpu to cpu .
// Use DriverAPINativeMethods directly here instead of CudaContext.CopyToHost, because CopyToHost only has an overload // for specifying totalBytes as a uint, but we may exceed the range of a uint here. // copy on CPU
None
result
NDArray
None
src
NDArray
None
totalElements
long
The total elements.
The total elements .