Example #1
0
        /// <summary>
        /// Copies data from host memory to device memory.
        /// </summary>
        /// <param name="src">The source array</param>
        /// <param name="dest">The destination CUDA array</param>
        public static void Copy <T>(T[,] src, CudaArray dest) where T : struct
        {
            if (src == null || dest.handle == IntPtr.Zero)
            {
                throw new NullPointerException();
            }

            int elemSize = Marshal.SizeOf(typeof(T));
            int width    = src.GetLength(1);
            int height   = src.GetLength(0);

            CudaArray.Descriptor desc = dest.GetDescriptor();

            if (desc.Width != width)
            {
                throw new ArgumentException("The source and destination width do not match.");
            }
            if (desc.Height != height)
            {
                throw new ArgumentException("The source and destination height do not match.");
            }

            IntPtr srcPtr = Marshal.AllocHGlobal(elemSize * width * height);

            ArrayToPtr(src, srcPtr);
            CudaUtil.Call(memcpy2DHelper(
                              MemoryType.Host, 0, 0, srcPtr, (uint)(elemSize * width),
                              MemoryType.Array, 0, 0, dest.handle, 0,
                              (uint)(elemSize * width), (uint)height)
                          );

            Marshal.FreeHGlobal(srcPtr);
        }
Example #2
0
        public DeviceBuffer GetConstantBuffer(string name)
        {
            DeviceBuffer result = new DeviceBuffer();

            CudaUtil.Call(cuModuleGetGlobal(out result.ptr.ptr, out result.sizeInBytes, handle, name));
            return(result);
        }
Example #3
0
        private TexAddressMode GetTexAddressMode(int dim)
        {
            UInt32 result;

            CudaUtil.Call(cuTexRefGetAddressMode(out result, handle, dim));
            return((TexAddressMode)result);
        }
 public void GLUnmapBufferObject(UInt32 bufferId)
 {
     CudaUtil.Call(CudaMem.cuGLUnmapBufferObject(bufferId));
     ptr          = DevicePtr2D.Zero;
     widthInBytes = 0;
     height       = 0;
 }
Example #5
0
        /// <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);
        }
Example #6
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);
        }
Example #7
0
        public static TexRef Create()
        {
            TexRef result = new TexRef();

            CudaUtil.Call(cuTexRefCreate(out result.handle));
            return(result);
        }
Example #8
0
        public TexRef GetTexRef(string name)
        {
            TexRef result = new TexRef();

            CudaUtil.Call(cuModuleGetTexRef(out result.handle, handle, name));
            return(result);
        }
Example #9
0
        /// <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);
        }
Example #10
0
        private int GetAttr(DeviceAttribute attrib)
        {
            Int32 result;

            CudaUtil.Call(cuDeviceGetAttribute(out result, (Int32)attrib, handle));
            return(result);
        }
Example #11
0
 /// <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));
 }
Example #12
0
 /// <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;
     }
 }
Example #13
0
        /// <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);
        }
Example #14
0
 /// <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;
     }
 }
Example #15
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         CudaUtil.Call(cuModuleUnload(handle));
         handle = IntPtr.Zero;
     }
 }
Example #16
0
 public void Free()
 {
     if (handle != IntPtr.Zero)
     {
         CudaUtil.Call(cuArrayDestroy(handle));
         handle = IntPtr.Zero;
     }
 }
Example #17
0
 /// <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));
 }
Example #18
0
 /// <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));
 }
Example #19
0
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         CudaUtil.Call(cuCtxDestroy(handle));
         handle = IntPtr.Zero;
     }
 }
Example #20
0
        /// <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);
        }
Example #21
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);
        }
Example #22
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)
     {
         CudaUtil.Call(CudaMem.cuMemFreeHost(ptr));
         ptr    = IntPtr.Zero;
         length = 0;
     }
 }
Example #23
0
        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);
        }
Example #24
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);
        }
Example #25
0
 public Descriptor GetDescriptor()
 {
     if (handle == IntPtr.Zero)
     {
         return(new Descriptor());
     }
     else
     {
         Descriptor result;
         CudaUtil.Call(cuArrayGetDescriptor(out result, handle));
         return(result);
     }
 }
Example #26
0
        public static CudaArray Allocate(int width, int height, CudaArrayFormat format, int numChannels)
        {
            CudaArray  result = new CudaArray();
            Descriptor desc   = new Descriptor();

            desc.Width       = (UInt32)width;
            desc.Height      = (UInt32)height;
            desc.Format      = format;
            desc.NumChannels = (UInt32)numChannels;

            CudaUtil.Call(cuArrayCreate(out result.handle, ref desc));
            return(result);
        }
Example #27
0
        /// <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)
                          );
        }
Example #28
0
        /// <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)
                          );
        }
Example #29
0
        /// <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>(DeviceBuffer src, T[] dest) where T : struct
        {
            if (src.ptr == DevicePtr.Zero || dest == null)
            {
                throw new NullPointerException();
            }

            IntPtr destPtr = AllocArrayFor(dest, src.sizeInBytes);

            CudaUtil.Call(cuMemcpyDtoH(destPtr, src.ptr.ptr, src.sizeInBytes));
            PtrToArray(destPtr, dest);
            Marshal.FreeHGlobal(destPtr);
        }
Example #30
0
        /// <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>(DeviceBuffer src, HostBuffer1D <T> dest, Stream stream) where T : struct
        {
            if (src.ptr == DevicePtr.Zero || dest.IsNull())
            {
                throw new NullPointerException();
            }
            if (src.sizeInBytes != (uint)dest.SizeInBytes)
            {
                throw new ArgumentException("The source and destination sizes do not match.");
            }

            CudaUtil.Call(cuMemcpyDtoHAsync(dest.ptr, src.ptr.ptr, (uint)dest.SizeInBytes, stream.Handle));
        }