java.lang.Object
uk.ac.manchester.tornado.api.types.arrays.TornadoNativeArray
uk.ac.manchester.tornado.api.types.arrays.HalfFloatArray
HalfFloatArray relies on preview features of the Java platform:
HalfFloatArrayrefers to one or more preview APIs:MemorySegment.
Preview features may be removed in a future release, or upgraded to permanent features of the Java platform.
This class represents an array of half floats (float16 types) stored in native memory.
The half float data is stored in a
MemorySegmentPREVIEW, which represents a contiguous region of off-heap memory.
The class also encapsulates methods for setting and getting half float values,
for initializing the half float array, and for converting the array to and from different representations.-
Field Summary
Fields inherited from class uk.ac.manchester.tornado.api.types.arrays.TornadoNativeArray
ARRAY_HEADER -
Constructor Summary
ConstructorsConstructorDescriptionHalfFloatArray(int numberOfElements) Constructs a new instance of theHalfFloatArraythat will store a user-specified number of elements.HalfFloatArray(HalfFloatArray... arrays) Constructs a newHalfFloatArrayinstance by concatenating the contents of the given array ofHalfFloatArrayinstances. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Sets all the values of theHalfFloatArrayinstance to zero.static HalfFloatArrayconcat(HalfFloatArray... arrays) Concatenates multipleHalfFloatArrayinstances into a singleHalfFloatArray.static HalfFloatArrayCreates a new instance of theHalfFloatArrayclass from an on-heapHalfFloat.static HalfFloatArrayfromElements(HalfFloat... values) Creates a new instance of theHalfFloatArrayclass from a set ofHalfFloatvalues.static HalfFloatArrayfromSegment(MemorySegmentPREVIEW segment) static HalfFloatArrayfromSegmentShallow(MemorySegmentPREVIEW segment) Creates a new instance of theHalfFloatArrayclass by wrapping an existingMemorySegmentPREVIEW without copying its contents.get(int index) Gets theHalfFloatvalue stored at the specified index of theHalfFloatArrayinstance.intlongReturns the number of bytes of theMemorySegmentPREVIEW that is associated with theHalfFloatArrayinstance, excluding the header bytes.longReturns the total number of bytes that theMemorySegmentPREVIEW, associated with theHalfFloatArrayinstance, occupies.intgetSize()Returns the number of half float elements stored in theHalfFloatArrayinstance.voidInitializes all the elements of theHalfFloatArrayinstance with a specified value.static voidinitialize(HalfFloatArray array, HalfFloat value) Factory method to initialize aHalfFloatArray.voidSets theHalfFloatvalue at a specified index of theHalfFloatArrayinstance.slice(int offset, int length) Extracts a slice of elements from a given , creating a new instance.Converts theHalfFloatdata from off-heap to on-heap, by copying the values of aHalfFloatArrayinstance into a new on-heapHalfFloat.short[]Converts theHalfFloatdata from off-heap to an on-heap short representation, by getting the values of aHalfFloatArrayinstance as short and coping them into a new on-heap short array.
-
Constructor Details
-
HalfFloatArray
public HalfFloatArray(int numberOfElements) Constructs a new instance of theHalfFloatArraythat will store a user-specified number of elements.- Parameters:
numberOfElements- The number of elements in the array.
-
HalfFloatArray
Constructs a newHalfFloatArrayinstance by concatenating the contents of the given array ofHalfFloatArrayinstances.- Parameters:
arrays- An array ofHalfFloatArrayinstances to be concatenated into the new instance.
-
-
Method Details
-
fromArray
Creates a new instance of theHalfFloatArrayclass from an on-heapHalfFloat.- Parameters:
values- The on-heapHalfFloatarray to create the instance from.- Returns:
- A new
HalfFloatArrayinstance, initialized with values of the on-heapHalfFloatarray.
-
fromElements
Creates a new instance of theHalfFloatArrayclass from a set ofHalfFloatvalues.- Parameters:
values- TheHalfFloatvalues to initialize the array with.- Returns:
- A new instance, initialized with the given values.
-
fromSegment
- Parameters:
segment- TheMemorySegmentPREVIEW containing the off-heap half float data.- Returns:
- A new
HalfFloatArrayinstance, initialized with the segment data.
-
fromSegmentShallow
Creates a new instance of theHalfFloatArrayclass by wrapping an existingMemorySegmentPREVIEW without copying its contents.- Parameters:
segment- TheMemorySegmentPREVIEW containing *both* the off-heap half-float *header* and *data*.- Returns:
- A new
HalfFloatArrayinstance that wraps the given segment.
-
toHeapArray
Converts theHalfFloatdata from off-heap to on-heap, by copying the values of aHalfFloatArrayinstance into a new on-heapHalfFloat.- Returns:
- A new on-heap
HalfFloatarray, initialized with the values stored in theHalfFloatArrayinstance.
-
toShortArray
public short[] toShortArray()Converts theHalfFloatdata from off-heap to an on-heap short representation, by getting the values of aHalfFloatArrayinstance as short and coping them into a new on-heap short array.- Returns:
- A new on-heap short array, initialized with the values stored in the
HalfFloatArrayinstance.
-
set
Sets theHalfFloatvalue at a specified index of theHalfFloatArrayinstance. -
get
Gets theHalfFloatvalue stored at the specified index of theHalfFloatArrayinstance.- Parameters:
index- The index of which to retrieve theHalfFloatvalue.- Returns:
-
clear
public void clear()Sets all the values of theHalfFloatArrayinstance to zero.- Specified by:
clearin classTornadoNativeArray
-
getElementSize
public int getElementSize()- Specified by:
getElementSizein classTornadoNativeArray
-
init
Initializes all the elements of theHalfFloatArrayinstance with a specified value.- Parameters:
value- TheHalfFloatvalue to initialize theHalfFloatArrayinstance with.
-
getSize
public int getSize()Returns the number of half float elements stored in theHalfFloatArrayinstance.- Specified by:
getSizein classTornadoNativeArray- Returns:
-
getSegment
- Specified by:
getSegmentin classTornadoNativeArray- Returns:
- The
MemorySegmentPREVIEW associated with theHalfFloatArrayinstance.
-
getSegmentWithHeader
- Specified by:
getSegmentWithHeaderin classTornadoNativeArray- Returns:
- The
MemorySegmentPREVIEW associated with theHalfFloatArrayinstance.
-
getNumBytesOfSegmentWithHeader
public long getNumBytesOfSegmentWithHeader()Returns the total number of bytes that theMemorySegmentPREVIEW, associated with theHalfFloatArrayinstance, occupies.- Specified by:
getNumBytesOfSegmentWithHeaderin classTornadoNativeArray- Returns:
- The total number of bytes of the
MemorySegmentPREVIEW.
-
getNumBytesOfSegment
public long getNumBytesOfSegment()Returns the number of bytes of theMemorySegmentPREVIEW that is associated with theHalfFloatArrayinstance, excluding the header bytes.- Specified by:
getNumBytesOfSegmentin classTornadoNativeArray- Returns:
- The number of bytes of the raw data in the
MemorySegmentPREVIEW.
-
initialize
Factory method to initialize aHalfFloatArray. This method can be invoked from a Task-Graph.- Parameters:
array- Input Array.value- The float value to initialize theHalfFloatArrayinstance with.
-
concat
Concatenates multipleHalfFloatArrayinstances into a singleHalfFloatArray.- Parameters:
arrays- Variable number ofHalfFloatArrayobjects to be concatenated.- Returns:
- A new
HalfFloatArrayinstance containing all the elements of the input arrays, concatenated in the order they were provided.
-
slice
Extracts a slice of elements from a given , creating a new instance.- Parameters:
offset- The starting index from which to begin the slice, inclusive.length- The number of elements to include in the slice.- Returns:
- A new instance representing the specified slice of the original array.
- Throws:
IllegalArgumentException- if the specified slice is out of the bounds of the original array.
-
HalfFloatArraywhen preview features are enabled.