/// <summary> /// Allocates a 2D block of memory, and returns a pointer pointing to it. /// </summary> /// <param name="elemSizeInBytes">The byte size of each element.</param> /// <param name="width">The number of columns</param> /// <param name="height">The number of rows</param> /// <returns>A pointer to the block of new memory</returns> public static DeviceBuffer2D Alloc(int elemSizeInBytes, int width, int height) { DeviceBuffer2D result = new DeviceBuffer2D(); result.widthInBytes = (uint)elemSizeInBytes * (uint)width; result.height = (uint)height; CudaUtil.Call(CudaMem.cuMemAllocPitch(out result.ptr.ptr, out result.ptr.pitch, result.widthInBytes, result.height, (uint)elemSizeInBytes)); return(result); }
public static DeviceBuffer2D GLMapBufferObject(UInt32 bufferId, uint widthInBytes) { DeviceBuffer2D result = new DeviceBuffer2D(); UInt32 size; CudaUtil.Call(CudaMem.cuGLMapBufferObject(out result.ptr.ptr, out size, bufferId)); result.ptr.pitch = widthInBytes; result.widthInBytes = widthInBytes; result.height = size / widthInBytes; return(result); }
/// <summary> /// Copies data from device memory to device memory. /// </summary> /// <param name="src">The source array</param> /// <param name="dest">The destination array</param> public static void Copy(DeviceBuffer2D src, DeviceBuffer2D dest) { if (src.ptr == DevicePtr2D.Zero || dest.ptr == DevicePtr2D.Zero) { throw new NullPointerException(); } CudaUtil.Call(memcpy2DUnalignedHelper( MemoryType.Device, 0, 0, (IntPtr)src.ptr.ptr, src.ptr.pitch, MemoryType.Device, 0, 0, (IntPtr)dest.ptr.ptr, dest.ptr.pitch, src.widthInBytes, src.height) ); }
/// <summary> /// Copies data from device memory to host memory. /// </summary> /// <param name="src">The source array</param> /// <param name="dest">The destination array</param> public static void Copy <T>(DeviceBuffer2D src, T[,] dest) where T : struct { if (src.ptr == DevicePtr2D.Zero || dest == null) { throw new NullPointerException(); } IntPtr destPtr = AllocArrayFor(dest, src.widthInBytes, src.height); CudaUtil.Call(memcpy2DHelper( MemoryType.Device, 0, 0, (IntPtr)src.ptr.ptr, src.ptr.pitch, MemoryType.Host, 0, 0, destPtr, src.widthInBytes, src.widthInBytes, src.height) ); PtrToArray(destPtr, dest); Marshal.FreeHGlobal(destPtr); }
/// <summary> /// Copies data from device memory to host memory asynchronously. /// </summary> /// <param name="src">The source pointer</param> /// <param name="dest">The destination buffer</param> /// <param name="stream">The stream in which to queue the copy operation</param> public static void CopyAsync <T>(DeviceBuffer2D src, HostBuffer2D <T> dest, Stream stream) where T : struct { if (src.ptr == DevicePtr2D.Zero || dest.IsNull()) { throw new NullPointerException(); } if (src.widthInBytes != (uint)dest.RowSizeBytes || src.height != (uint)dest.height) { throw new ArgumentException("The source and destination sizes do not match."); } CudaUtil.Call(memcpy2DAsyncHelper( MemoryType.Device, 0, 0, (IntPtr)src.ptr.ptr, src.ptr.pitch, MemoryType.Host, 0, 0, dest.Ptr, (uint)dest.RowSizeBytes, (uint)dest.RowSizeBytes, (uint)dest.Height, stream.Handle) ); }
/// <summary> /// Copies data from host memory to device memory. /// </summary> /// <param name="src">The source buffer</param> /// <param name="dest">The destination buffer</param> public static void Copy <T>(HostBuffer2D <T> src, DeviceBuffer2D dest) where T : struct { if (src.IsNull() || dest.ptr == DevicePtr2D.Zero) { throw new NullPointerException(); } if ((uint)src.RowSizeBytes != dest.WidthInBytes || (uint)src.Height != dest.Height) { throw new ArgumentException("The source and destination sizes do not match."); } CudaUtil.Call(CudaMem.memcpy2DHelper( MemoryType.Host, 0, 0, src.ptr, (uint)src.RowSizeBytes, MemoryType.Device, 0, 0, (IntPtr)dest.ptr.ptr, dest.ptr.pitch, (uint)src.RowSizeBytes, (uint)src.height) ); }
/// <summary> /// Copies data from host memory to device memory. /// </summary> /// <param name="src">The source array</param> /// <param name="dest">The destination pointer</param> public static void Copy <T>(T[,] src, DeviceBuffer2D dest) where T : struct { if (src == null || dest.ptr == DevicePtr2D.Zero) { throw new NullPointerException(); } IntPtr srcPtr = AllocArrayFor(src, dest.widthInBytes, dest.height); ArrayToPtr(src, srcPtr); CudaUtil.Call(memcpy2DHelper( MemoryType.Host, 0, 0, srcPtr, dest.widthInBytes, MemoryType.Device, 0, 0, (IntPtr)dest.ptr.ptr, dest.ptr.pitch, dest.widthInBytes, dest.height) ); Marshal.FreeHGlobal(srcPtr); }