HostMemoryBuffer#

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.

Method Summary

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.

asByteBuffer()

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.

copyFromDeviceBuffer(BaseDeviceMemoryBuffer deviceMemoryBuffer)

Synchronously copy from a DeviceMemoryBuffer to a HostMemoryBuffer.

copyFromDeviceBuffer(BaseDeviceMemoryBuffer deviceMemoryBuffer, Cuda.Stream stream)

Copy from a DeviceMemoryBuffer to a HostMemoryBuffer using the specified stream.

copyFromDeviceBufferAsync(BaseDeviceMemoryBuffer deviceMemoryBuffer, Cuda.Stream stream)

Copy from a DeviceMemoryBuffer to a HostMemoryBuffer using the specified stream.

copyFromHostBuffer(long destOffset, HostMemoryBuffer srcData, long srcOffset, long length)

Copy the contents of the given buffer to this buffer.

copyFromStream(long destOffset, InputStream in, long byteLength)

Copy len bytes from in to this buffer.

getBoolean(long offset)

Returns the Boolean value at that offset.

getByte(long offset)

Returns the byte value at that offset.

getBytes(byte[] dst, long dstOffset, long srcOffset, long len)

Copy a set of bytes to an array from the buffer starting at offset.

getDouble(long offset)

Returns the Double value at that offset.

getFloat(long offset)

Returns the Float value at that offset.

getInt(long offset)

Returns the Integer value at that offset.

getInts(int[] dst, long dstIndex, long srcOffset, int count)

Copy a set of ints to an array from the buffer starting at offset.

getLong(long offset)

Returns the Long value at that offset.

getLongs(long[] dst, long dstIndex, long srcOffset, int count)

Copy a set of longs to an array from the buffer starting at offset.

getShort(long offset)

Returns the Short value at that offset.

mapFile(File path, MapMode mode, long offset, long length)

Create a host buffer that is memory-mapped to a file.

printBuffer()

WARNING: Debug only method to print buffer.

printBuffer(int wordsPerRow)

WARNING: Debug only method to print buffer.

setBoolean(long offset, boolean value)

Sets the Boolean value at that offset.

setByte(long offset, byte value)

Sets the byte value at that offset.

setBytes(long offset, byte[] data, long srcOffset, long len)

Copy a set of bytes from an array into the buffer at offset.

setDouble(long offset, double value)

Sets the Double value at that offset.

setDoubles(long offset, double[] data, long srcOffset, long len)

Copy a set of doubles from an array into the buffer at offset.

setFloat(long offset, float value)

Sets the Float value at that offset.

setFloats(long offset, float[] data, long srcOffset, long len)

Copy a set of floats from an array into the buffer at offset.

setInt(long offset, int value)

Sets the Integer value at that offset.

setInts(long offset, int[] data, long srcOffset, long len)

Copy a set of ints from an array into the buffer at offset.

setLong(long offset, long value)

Sets the Long value at that offset.

setLongs(long offset, long[] data, long srcOffset, long len)

Copy a set of longs from an array into the buffer at offset.

setMemory(long offset, long length, byte value)

Sets the values in this buffer repeatedly.

setShort(long offset, short value)

Sets the Short value at that offset.

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.

Fields inherited from class ai.rapids.cudf.MemoryBuffer: address, cleaner, closed, id, length, refCount

Methods inherited from class ai.rapids.cudf.MemoryBuffer: addressOutOfBoundsCheck, close, copyFromMemoryBuffer, copyFromMemoryBufferAsync, getAddress, getEventHandler, getLength, getRefCount, incRefCount, noWarnLeakExpected, setEventHandler, toString

Methods

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.

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.

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

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.

public final byte getByte(long offset)#

Returns the byte value at that offset.

Parameters:

offset – - offset from the address

Returns:

- value

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

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

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.

public final short getShort(long offset)#

Returns the Short value at that offset.

Parameters:

offset – - offset from the address

Returns:

- value

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

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.

public final int getInt(long offset)#

Returns the Integer value at that offset.

Parameters:

offset – - offset from the address

Returns:

- value

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

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

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

public final long getLong(long offset)#

Returns the Long value at that offset.

Parameters:

offset – - offset from the address

Returns:

- value

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

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

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.

public final float getFloat(long offset)#

Returns the Float value at that offset.

Parameters:

offset – - offset from the address

Returns:

- value

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

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

public final double getDouble(long offset)#

Returns the Double value at that offset.

Parameters:

offset – - offset from the address

Returns:

- value

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

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

public final boolean getBoolean(long offset)#

Returns the Boolean value at that offset.

Parameters:

offset – - offset from the address

Returns:

- value

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

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

public final void copyFromDeviceBuffer(
BaseDeviceMemoryBuffer deviceMemoryBuffer
)#

Synchronously copy from a DeviceMemoryBuffer to a HostMemoryBuffer.

Parameters:

deviceMemoryBuffer – buffer to copy data from

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

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

public final HostMemoryBuffer slice(long offset, long len)#

Slice off a part of the host buffer.

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.

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.

public void printBuffer()#

WARNING: Debug only method to print buffer.

Does not work for buffers over 2GB.

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.

Static Methods

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

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

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

public static HostMemoryBuffer mapFile(
File path,
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