Package ai.rapids.cudf
Class HostMemoryBuffer
java.lang.Object
ai.rapids.cudf.MemoryBuffer
ai.rapids.cudf.HostMemoryBuffer
- All Implemented Interfaces:
AutoCloseable
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic HostMemoryBuffer
allocate
(long bytes) Allocate memory, but be sure to close the returned buffer to avoid memory leaks.static HostMemoryBuffer
allocate
(long bytes, boolean preferPinned) Allocate memory, but be sure to close the returned buffer to avoid memory leaks.static HostMemoryBuffer
allocateRaw
(long bytes) Allocate host memory bypassing the default allocator.final ByteBuffer
Return a ByteBuffer that provides access to the underlying memory.final ByteBuffer
asByteBuffer
(long offset, int length) Return a ByteBuffer that provides access to the underlying memory.final void
copyFromDeviceBuffer
(BaseDeviceMemoryBuffer deviceMemoryBuffer) Synchronously copy from a DeviceMemoryBuffer to a HostMemoryBufferfinal void
copyFromDeviceBuffer
(BaseDeviceMemoryBuffer deviceMemoryBuffer, Cuda.Stream stream) Copy from a DeviceMemoryBuffer to a HostMemoryBuffer using the specified stream.final void
copyFromDeviceBufferAsync
(BaseDeviceMemoryBuffer deviceMemoryBuffer, Cuda.Stream stream) 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 bufferfinal 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 offsetfinal byte
getByte
(long offset) Returns the byte value at that offsetfinal 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 offsetfinal float
getFloat
(long offset) Returns the Float value at that offsetfinal int
getInt
(long offset) Returns the Integer value at that offsetfinal 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 offsetfinal 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 offsetstatic HostMemoryBuffer
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 offsetfinal void
setByte
(long offset, byte value) Sets the byte value at that offsetfinal 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 offsetfinal 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 offsetfinal 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 offsetfinal 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 offsetfinal 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 repeatedlyfinal void
setShort
(long offset, short value) Sets the Short value at that offsetfinal void
setShorts
(long offset, short[] data, long srcOffset, long len) Copy a set of shorts from an array into the buffer at offset.final HostMemoryBuffer
slice
(long offset, long len) Slice off a part of the host buffer.final HostMemoryBuffer
sliceWithCopy
(long offset, long len) Slice off a part of the host buffer, actually making a copy of the data.Methods inherited from class ai.rapids.cudf.MemoryBuffer
addressOutOfBoundsCheck, close, copyFromMemoryBuffer, copyFromMemoryBufferAsync, getAddress, getEventHandler, getLength, getRefCount, incRefCount, noWarnLeakExpected, setEventHandler, toString
-
Method Details
-
allocate
Allocate memory, but be sure to close the returned buffer to avoid memory leaks.- Parameters:
bytes
- size in bytes to allocatepreferPinned
- 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
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
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 memorymode
- mapping typeoffset
- file offset where the map will startlength
- the number of bytes to map- Returns:
- file-mapped buffer
- Throws:
IOException
-
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
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 atlength
- 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 tosrcData
- Buffer to be copied fromsrcOffset
- offset in bytes to start copying from in srcDatalength
- 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 toin
- input stream to copy bytes frombyteLength
- 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 addressvalue
- - 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 arraydstOffset
- starting offset within the destination arraysrcOffset
- starting offset within this bufferlen
- 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 todata
- 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 addressvalue
- - 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 todata
- 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 arraydstIndex
- starting index within the destination arraysrcOffset
- starting offset within this buffercount
- 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 addressvalue
- - 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 todata
- 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 addressvalue
- - 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 arraydstIndex
- starting index within the destination arraysrcOffset
- starting offset within this buffercount
- 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 todata
- 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 addressvalue
- - 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 todata
- 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 addressvalue
- - 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 todata
- 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 addressvalue
- - 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 addresslength
- - number of bytes to setvalue
- - value to be set
-
copyFromDeviceBuffer
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 fromstream
- 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 fromstream
- CUDA stream to use
-
slice
Slice off a part of the host buffer.- Specified by:
slice
in classMemoryBuffer
- 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
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.
-