public void GLUnmapBufferObject(UInt32 bufferId) { CudaUtil.Call(CudaMem.cuGLUnmapBufferObject(bufferId)); ptr = DevicePtr2D.Zero; widthInBytes = 0; height = 0; }
/// <summary> /// Creates a new buffer and allocates the specified ammount of memory. /// </summary> /// <param name="sizeInBytes">The number of bytes to allocate for the buffer</param> /// <returns>A pointer to the allocated buffer</returns> public static IntPtr Alloc(uint sizeInBytes) { IntPtr result = IntPtr.Zero; CudaUtil.Call(CudaMem.cuMemAllocHost(out result, (UInt32)sizeInBytes)); return(result); }
/// <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> /// Maps a buffer to an OpenGL buffer object. /// </summary> /// <param name="bufferId">The unsigned integer name of the OpenGL buffer</param> /// <returns>A pointer mapped to the OpenGL buffer</returns> public static DeviceBuffer GLMapBufferObject(UInt32 bufferId) { DeviceBuffer result = new DeviceBuffer(); CudaUtil.Call(CudaMem.cuGLMapBufferObject(out result.ptr.ptr, out result.sizeInBytes, bufferId)); return(result); }
/// <summary> /// Fills bytes in this buffer with the given value. /// </summary> /// <param name="value">The value to fill the bytes with</param> /// <param name="widthInBytes">The number of bytes in each row</param> /// <param name="height">The number of rows</param> public void MemSet(byte value) { if (ptr.ptr == 0) { throw new NullPointerException(); } CudaUtil.Call(CudaMem.cuMemsetD2D8(ptr.ptr, ptr.pitch, value, widthInBytes, height)); }
/// <summary> /// Releases any memory used by this buffer, and sets it to null. This method may be /// called on a null pointer, but will have no effect. /// </summary> /// <param name="ptr">A pointer to the buffer to deallocate</param> public static void Free(ref IntPtr ptr) { if (ptr != IntPtr.Zero) { CudaUtil.Call(CudaMem.cuMemFreeHost(ptr)); ptr = IntPtr.Zero; } }
/// <summary> /// Allocates a block of memory sized to hold a 1D array and returns a pointer to it. /// </summary> /// <param name="elemSizeBytes">The size of each array element in bytes</param> /// <param name="width">The number of elements to allocate memory for</param> /// <returns>A pointer to the array</returns> public static DeviceBuffer Alloc(int elemSizeBytes, int width) { DeviceBuffer result = new DeviceBuffer(); result.sizeInBytes = (uint)elemSizeBytes * (uint)width; CudaUtil.Call(CudaMem.cuMemAlloc(out result.ptr.ptr, result.sizeInBytes)); return(result); }
/// <summary> /// Releases memory pointed to by this pointer. This method may be called on a null /// pointer, but will have no effect. /// </summary> public void Free() { if (ptr != 0) { CudaUtil.Call(CudaMem.cuMemFree(ptr)); ptr = 0; } }
public static HostBuffer1D <T> Alloc(int length) { HostBuffer1D <T> result = new HostBuffer1D <T>(); uint elemSize = (uint)Marshal.SizeOf(typeof(T)); CudaUtil.Call(CudaMem.cuMemAllocHost(out result.ptr, elemSize * (uint)length)); result.length = length; return(result); }
/// <summary> /// Allocates a 2D block of memory, and returns a pointer pointing to it. Rows are NOT padded, /// and may not meet the hardware's alignment requirements. /// </summary> /// <param name="widthInBytes">The number of bytes to allocate for each row.</param> /// <param name="height">The number of rows to allocate</param> /// <returns>A pointer to the new block</returns> public static DevicePtr2D AllocRaw(uint widthInBytes, uint height) { DevicePtr2D result = new DevicePtr2D(); result.pitch = widthInBytes; CudaUtil.Call(CudaMem.cuMemAlloc(out result.ptr, widthInBytes * height)); return(result); }
/// <summary> /// Releases any memory used by this buffer. This method may be called on a null /// buffer, but will have no effect. /// </summary> public void Free() { if (ptr != IntPtr.Zero) { CudaUtil.Call(CudaMem.cuMemFreeHost(ptr)); ptr = IntPtr.Zero; length = 0; } }
/// <summary> /// Releases any memory used by this buffer. This method may be called on a null /// buffer, but will have no effect. /// </summary> public void Free() { if (ptr != IntPtr.Zero) { CudaMem.cuMemFreeHost(ptr); ptr = IntPtr.Zero; width = 0; height = 0; } }
/// <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 host memory to device 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 buffer</param> /// <param name="dest">The destination pointer</param> /// <param name="widthInBytes">The size of each row in bytes</param> /// <param name="height">The number of rows</param> public static void CopyRaw2D(IntPtr srcPtr, DevicePtr2D dest, IntPtr widthInBytes, IntPtr height) { if (srcPtr == IntPtr.Zero || dest == DevicePtr2D.Zero) { throw new NullPointerException(); } CudaUtil.Call(CudaMem.memcpy2DHelper( MemoryType.Host, 0, 0, srcPtr, (uint)widthInBytes, MemoryType.Device, 0, 0, (IntPtr)dest.ptr, (uint)dest.Pitch, (uint)widthInBytes, (uint)height) ); }
/// <summary> /// Copies data from host memory to a cuda array 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 buffer</param> /// <param name="dest">The destination pointer</param> /// <param name="stream">The stream in which to queue the copy operation</param> public static void CopyAsync <T>(HostBuffer2D <T> src, CudaArray dest, Stream stream) where T : struct { if (src.IsNull() || dest.IsNull()) { throw new NullPointerException(); } CudaUtil.Call(CudaMem.memcpy2DAsyncHelper( MemoryType.Host, 0, 0, src.ptr, (uint)src.RowSizeBytes, MemoryType.Array, 0, 0, dest.handle, 0, (uint)src.RowSizeBytes, (uint)src.height, stream.Handle )); }
/// <summary> /// Copies data from host memory to device memory asynchronously. /// </summary> /// <param name="src">The source buffer</param> /// <param name="dest">The destination buffer</param> /// <param name="stream">The stream in which to queue the copy operation</param> public static void Copy <T>(HostBuffer2D <T> src, DeviceBuffer2D dest, Stream stream) 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.memcpy2DAsyncHelper( 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, stream.Handle )); }
public void ReadConstant <T>(out T value, string name) where T : struct { DeviceBuffer buffer = GetConstantBuffer(name); CudaMem.CopyStruct(buffer, out value); }
/// <summary> /// Removes a mapping to an OpenGL buffer and nullifies this buffer's pointer. /// </summary> /// <param name="bufferId">The unsigned integer name of the OpenGL buffer</param> public void GLUnmapBufferObject(UInt32 bufferId) { CudaUtil.Call(CudaMem.cuGLUnmapBufferObject(bufferId)); ptr.ptr = 0; sizeInBytes = 0; }
public void WriteConstant <T>(string name, T[] value) where T : struct { DeviceBuffer buffer = GetConstantBuffer(name); CudaMem.Copy(value, buffer); }