Class HostMemoryBuffer

java.lang.Object
ai.rapids.cudf.MemoryBuffer
ai.rapids.cudf.HostMemoryBuffer
All Implemented Interfaces:
AutoCloseable

public class HostMemoryBuffer extends MemoryBuffer
This class holds an off-heap buffer in the host/CPU memory. Please note that instances must be explicitly closed or native memory will be leaked! Internally this class will try to use PinnedMemoryPool to allocate and free the memory it uses by default. To avoid using the pinned memory pool for allocations by default set the Java system property ai.rapids.cudf.prefer-pinned to false. Be aware that the off heap memory limits set by Java do not apply to these buffers.
  • Nested Class Summary

    Nested classes/interfaces inherited from class ai.rapids.cudf.MemoryBuffer

    MemoryBuffer.EventHandler, MemoryBuffer.MemoryBufferCleaner
  • Field Summary

    Fields inherited from class ai.rapids.cudf.MemoryBuffer

    address, cleaner, closed, id, length, refCount
  • Method Summary

    Modifier and Type
    Method
    Description
    allocate(long bytes)
    Allocate memory, but be sure to close the returned buffer to avoid memory leaks.
    allocate(long bytes, boolean preferPinned)
    Allocate memory, but be sure to close the returned buffer to avoid memory leaks.
    allocateRaw(long bytes)
    Allocate host memory bypassing the default allocator.
    Return a ByteBuffer that provides access to the underlying memory.
    asByteBuffer(long offset, int length)
    Return a ByteBuffer that provides access to the underlying memory.
    final void
    Synchronously copy from a DeviceMemoryBuffer to a HostMemoryBuffer
    final void
    Copy from a DeviceMemoryBuffer to a HostMemoryBuffer using the specified stream.
    final void
    Copy from a DeviceMemoryBuffer to a HostMemoryBuffer using the specified stream.
    final void
    copyFromHostBuffer(long destOffset, HostMemoryBuffer srcData, long srcOffset, long length)
    Copy the contents of the given buffer to this buffer
    final void
    copyFromStream(long destOffset, InputStream in, long byteLength)
    Copy len bytes from in to this buffer.
    final boolean
    getBoolean(long offset)
    Returns the Boolean value at that offset
    final byte
    getByte(long offset)
    Returns the byte value at that offset
    final void
    getBytes(byte[] dst, long dstOffset, long srcOffset, long len)
    Copy a set of bytes to an array from the buffer starting at offset.
    final double
    getDouble(long offset)
    Returns the Double value at that offset
    final float
    getFloat(long offset)
    Returns the Float value at that offset
    final int
    getInt(long offset)
    Returns the Integer value at that offset
    final void
    getInts(int[] dst, long dstIndex, long srcOffset, int count)
    Copy a set of ints to an array from the buffer starting at offset.
    final long
    getLong(long offset)
    Returns the Long value at that offset
    final void
    getLongs(long[] dst, long dstIndex, long srcOffset, int count)
    Copy a set of longs to an array from the buffer starting at offset.
    final short
    getShort(long offset)
    Returns the Short value at that offset
    mapFile(File path, FileChannel.MapMode mode, long offset, long length)
    Create a host buffer that is memory-mapped to a file.
    void
    WARNING: Debug only method to print buffer.
    void
    printBuffer(int wordsPerRow)
    WARNING: Debug only method to print buffer.
    final void
    setBoolean(long offset, boolean value)
    Sets the Boolean value at that offset
    final void
    setByte(long offset, byte value)
    Sets the byte value at that offset
    final void
    setBytes(long offset, byte[] data, long srcOffset, long len)
    Copy a set of bytes from an array into the buffer at offset.
    final void
    setDouble(long offset, double value)
    Sets the Double value at that offset
    final void
    setDoubles(long offset, double[] data, long srcOffset, long len)
    Copy a set of doubles from an array into the buffer at offset.
    final void
    setFloat(long offset, float value)
    Sets the Float value at that offset
    final void
    setFloats(long offset, float[] data, long srcOffset, long len)
    Copy a set of floats from an array into the buffer at offset.
    final void
    setInt(long offset, int value)
    Sets the Integer value at that offset
    final void
    setInts(long offset, int[] data, long srcOffset, long len)
    Copy a set of ints from an array into the buffer at offset.
    final void
    setLong(long offset, long value)
    Sets the Long value at that offset
    final void
    setLongs(long offset, long[] data, long srcOffset, long len)
    Copy a set of longs from an array into the buffer at offset.
    final void
    setMemory(long offset, long length, byte value)
    Sets the values in this buffer repeatedly
    final void
    setShort(long offset, short value)
    Sets the Short value at that offset
    final void
    setShorts(long offset, short[] data, long srcOffset, long len)
    Copy a set of shorts from an array into the buffer at offset.
    slice(long offset, long len)
    Slice off a part of the host buffer.
    sliceWithCopy(long offset, long len)
    Slice off a part of the host buffer, actually making a copy of the data.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • allocate

      public static HostMemoryBuffer allocate(long bytes, boolean preferPinned)
      Allocate memory, but be sure to close the returned buffer to avoid memory leaks.
      Parameters:
      bytes - size in bytes to allocate
      preferPinned - If set to true, the pinned memory pool will be used if possible with a fallback to off-heap memory. If set to false, the allocation will always be from off-heap memory.
      Returns:
      the newly created buffer
    • allocate

      public static HostMemoryBuffer allocate(long bytes)
      Allocate memory, but be sure to close the returned buffer to avoid memory leaks. Pinned memory will be preferred for allocations if the java system property ai.rapids.cudf.prefer-pinned is set to true.
      Parameters:
      bytes - size in bytes to allocate
      Returns:
      the newly created buffer
    • allocateRaw

      public static HostMemoryBuffer allocateRaw(long bytes)
      Allocate host memory bypassing the default allocator. This is intended to only be used by other allocators. Pinned memory will not be used for these allocations.
      Parameters:
      bytes - size in bytes to allocate
      Returns:
      the newly created buffer
    • mapFile

      public static HostMemoryBuffer mapFile(File path, FileChannel.MapMode mode, long offset, long length) throws IOException
      Create a host buffer that is memory-mapped to a file.
      Parameters:
      path - path to the file to map into host memory
      mode - mapping type
      offset - file offset where the map will start
      length - the number of bytes to map
      Returns:
      file-mapped buffer
      Throws:
      IOException
    • asByteBuffer

      public final ByteBuffer asByteBuffer()
      Return a ByteBuffer that provides access to the underlying memory. Please note: if the buffer is larger than a ByteBuffer can handle (2GB) an exception will be thrown. Also be aware that the ByteBuffer will be in native endian order, which is different from regular ByteBuffers that are big endian by default.
    • asByteBuffer

      public final ByteBuffer asByteBuffer(long offset, int length)
      Return a ByteBuffer that provides access to the underlying memory. Be aware that the ByteBuffer will be in native endian order, which is different from regular ByteBuffers that are big endian by default.
      Parameters:
      offset - the offset to start at
      length - how many bytes to include.
    • copyFromHostBuffer

      public final void copyFromHostBuffer(long destOffset, HostMemoryBuffer srcData, long srcOffset, long length)
      Copy the contents of the given buffer to this buffer
      Parameters:
      destOffset - offset in bytes in this buffer to start copying to
      srcData - Buffer to be copied from
      srcOffset - offset in bytes to start copying from in srcData
      length - number of bytes to copy
    • copyFromStream

      public final void copyFromStream(long destOffset, InputStream in, long byteLength) throws IOException
      Copy len bytes from in to this buffer.
      Parameters:
      destOffset - offset in bytes in this buffer to start copying to
      in - input stream to copy bytes from
      byteLength - number of bytes to copy
      Throws:
      EOFException - If there are not enough bytes in the stream to copy.
      IOException - If there is an error reading from the stream.
    • getByte

      public final byte getByte(long offset)
      Returns the byte value at that offset
      Parameters:
      offset - - offset from the address
      Returns:
      - value
    • setByte

      public final void setByte(long offset, byte value)
      Sets the byte value at that offset
      Parameters:
      offset - - offset from the address
      value - - value to be set
    • getBytes

      public final void getBytes(byte[] dst, long dstOffset, long srcOffset, long len)
      Copy a set of bytes to an array from the buffer starting at offset.
      Parameters:
      dst - destination byte array
      dstOffset - starting offset within the destination array
      srcOffset - starting offset within this buffer
      len - number of bytes to copy
    • setBytes

      public final void setBytes(long offset, byte[] data, long srcOffset, long len)
      Copy a set of bytes from an array into the buffer at offset.
      Parameters:
      offset - the offset from the address to start copying to
      data - the data to be copied.
    • getShort

      public final short getShort(long offset)
      Returns the Short value at that offset
      Parameters:
      offset - - offset from the address
      Returns:
      - value
    • setShort

      public final void setShort(long offset, short value)
      Sets the Short value at that offset
      Parameters:
      offset - - offset from the address
      value - - value to be set
    • setShorts

      public final void setShorts(long offset, short[] data, long srcOffset, long len)
      Copy a set of shorts from an array into the buffer at offset.
      Parameters:
      offset - the offset from the address to start copying to
      data - the data to be copied.
      srcOffset - index in data to start at.
    • getInt

      public final int getInt(long offset)
      Returns the Integer value at that offset
      Parameters:
      offset - - offset from the address
      Returns:
      - value
    • getInts

      public final void getInts(int[] dst, long dstIndex, long srcOffset, int count)
      Copy a set of ints to an array from the buffer starting at offset.
      Parameters:
      dst - destination int array
      dstIndex - starting index within the destination array
      srcOffset - starting offset within this buffer
      count - number of ints to copy
    • setInt

      public final void setInt(long offset, int value)
      Sets the Integer value at that offset
      Parameters:
      offset - - offset from the address
      value - - value to be set
    • setInts

      public final void setInts(long offset, int[] data, long srcOffset, long len)
      Copy a set of ints from an array into the buffer at offset.
      Parameters:
      offset - the offset from the address to start copying to
      data - the data to be copied.
      srcOffset - index into data to start at
    • getLong

      public final long getLong(long offset)
      Returns the Long value at that offset
      Parameters:
      offset - - offset from the address
      Returns:
      - value
    • setLong

      public final void setLong(long offset, long value)
      Sets the Long value at that offset
      Parameters:
      offset - - offset from the address
      value - - value to be set
    • getLongs

      public final void getLongs(long[] dst, long dstIndex, long srcOffset, int count)
      Copy a set of longs to an array from the buffer starting at offset.
      Parameters:
      dst - destination long array
      dstIndex - starting index within the destination array
      srcOffset - starting offset within this buffer
      count - number of longs to copy
    • setLongs

      public final void setLongs(long offset, long[] data, long srcOffset, long len)
      Copy a set of longs from an array into the buffer at offset.
      Parameters:
      offset - the offset from the address to start copying to
      data - the data to be copied.
      srcOffset - index into data to start at.
    • getFloat

      public final float getFloat(long offset)
      Returns the Float value at that offset
      Parameters:
      offset - - offset from the address
      Returns:
      - value
    • setFloat

      public final void setFloat(long offset, float value)
      Sets the Float value at that offset
      Parameters:
      offset - - offset from the address
      value - - value to be set
    • setFloats

      public final void setFloats(long offset, float[] data, long srcOffset, long len)
      Copy a set of floats from an array into the buffer at offset.
      Parameters:
      offset - the offset from the address to start copying to
      data - the data to be copied.
      srcOffset - index into data to start at
    • getDouble

      public final double getDouble(long offset)
      Returns the Double value at that offset
      Parameters:
      offset - - offset from the address
      Returns:
      - value
    • setDouble

      public final void setDouble(long offset, double value)
      Sets the Double value at that offset
      Parameters:
      offset - - offset from the address
      value - - value to be set
    • setDoubles

      public final void setDoubles(long offset, double[] data, long srcOffset, long len)
      Copy a set of doubles from an array into the buffer at offset.
      Parameters:
      offset - the offset from the address to start copying to
      data - the data to be copied.
      srcOffset - index into data to start at
    • getBoolean

      public final boolean getBoolean(long offset)
      Returns the Boolean value at that offset
      Parameters:
      offset - - offset from the address
      Returns:
      - value
    • setBoolean

      public final void setBoolean(long offset, boolean value)
      Sets the Boolean value at that offset
      Parameters:
      offset - - offset from the address
      value - - value to be set
    • setMemory

      public final void setMemory(long offset, long length, byte value)
      Sets the values in this buffer repeatedly
      Parameters:
      offset - - offset from the address
      length - - number of bytes to set
      value - - value to be set
    • copyFromDeviceBuffer

      public final void copyFromDeviceBuffer(BaseDeviceMemoryBuffer deviceMemoryBuffer)
      Synchronously copy from a DeviceMemoryBuffer to a HostMemoryBuffer
      Parameters:
      deviceMemoryBuffer - buffer to copy data from
    • copyFromDeviceBuffer

      public final void copyFromDeviceBuffer(BaseDeviceMemoryBuffer deviceMemoryBuffer, Cuda.Stream stream)
      Copy from a DeviceMemoryBuffer to a HostMemoryBuffer using the specified stream. The copy has completed when this returns, but the memory copy could overlap with operations occurring on other streams.
      Parameters:
      deviceMemoryBuffer - buffer to copy data from
      stream - CUDA stream to use
    • copyFromDeviceBufferAsync

      public final void copyFromDeviceBufferAsync(BaseDeviceMemoryBuffer deviceMemoryBuffer, Cuda.Stream stream)
      Copy from a DeviceMemoryBuffer to a HostMemoryBuffer using the specified stream. The copy is async and may not have completed when this returns.
      Parameters:
      deviceMemoryBuffer - buffer to copy data from
      stream - CUDA stream to use
    • slice

      public final HostMemoryBuffer slice(long offset, long len)
      Slice off a part of the host buffer.
      Specified by:
      slice in class MemoryBuffer
      Parameters:
      offset - where to start the slice at.
      len - how many bytes to slice
      Returns:
      a host buffer that will need to be closed independently from this buffer.
    • sliceWithCopy

      public final HostMemoryBuffer sliceWithCopy(long offset, long len)
      Slice off a part of the host buffer, actually making a copy of the data.
      Parameters:
      offset - where to start the slice at.
      len - how many bytes to slice
      Returns:
      a host buffer that will need to be closed independently from this buffer.
    • printBuffer

      public void printBuffer()
      WARNING: Debug only method to print buffer. Does not work for buffers over 2GB.
    • printBuffer

      public void printBuffer(int wordsPerRow)
      WARNING: Debug only method to print buffer. Does not work for buffers over 2GB.
      Parameters:
      wordsPerRow - the number of 32 bit works to print per row.