/// <summary> /// Allocates a block of memory and returns a pointer to it. /// </summary> /// <param name="sizeInBytes">The ammount of memory to allocate</param> /// <returns>A pointer to the new memory</returns> public static DevicePtr AllocRaw(uint sizeInBytes) { DevicePtr result = new DevicePtr(); CudaUtil.Call(CudaMem.cuMemAlloc(out result.ptr, sizeInBytes)); return(result); }
/// <summary> /// Copies data from device memory to host memory asynchronously. No size checks are performed, it is /// up to the programmer to ensure the destination is large enough to hold the data. /// </summary> /// <param name="src">The source pointer</param> /// <param name="dest">The destination pointer, must be to page locked memory</param> /// <param name="sizeInBytes">The number of bytes to copy</param> /// <param name="stream">The stream in which to queue the copy operation</param> public static void CopyRawAsync(DevicePtr src, IntPtr destPtr, IntPtr sizeInBytes, Stream stream) { if (src == DevicePtr.Zero || destPtr == IntPtr.Zero) { throw new NullPointerException(); } CudaUtil.Call(cuMemcpyDtoHAsync(destPtr, src.ptr, (uint)sizeInBytes, stream.Handle)); }
/// <summary> /// Copies data from device memory to host memory. No size checks are performed, it is /// up to the programmer to ensure the destination is large enough to hold the data. /// </summary> /// <param name="src">The source pointer</param> /// <param name="dest">The destination buffer</param> /// <param name="sizeInBytes">The number of bytes to copy</param> public static void CopyRaw(DevicePtr src, IntPtr destPtr, IntPtr sizeInBytes) { if (src == DevicePtr.Zero || destPtr == IntPtr.Zero) { throw new NullPointerException(); } CudaUtil.Call(cuMemcpyDtoH(destPtr, src.ptr, (uint)sizeInBytes)); }
/// <summary> /// Copies data from device memory to a host struct. No size checking is performed. /// </summary> /// <param name="src">The source buffer</param> /// <param name="dest">The destination struct</param> public static void CopyStructRaw <T>(DevicePtr src, out T dest) where T : struct { if (src == DevicePtr.Zero) { throw new NullPointerException(); } uint sizeInBytes = (uint)Marshal.SizeOf(typeof(T)); IntPtr destPtr = Marshal.AllocHGlobal((IntPtr)sizeInBytes); CudaUtil.Call(cuMemcpyDtoH(destPtr, src.ptr, sizeInBytes)); dest = (T)Marshal.PtrToStructure(destPtr, typeof(T)); Marshal.FreeHGlobal(destPtr); }
internal uint sizeInBytes; //The buffer size in bytes internal DeviceBuffer(DevicePtr ptr, uint sizeInBytes) { this.ptr = ptr; this.sizeInBytes = sizeInBytes; }