Array¶
-
template<typename ElemType, DataLocation Location>
class Array¶ A plain contiguous array allocated in either the
Host
orDevice
according to theLocation
parameter. The memory allocated for the Array will be aligned to the SIMD width of the target system (e.g.,256-bit
when usingAVX2
instruction set).Similar to the
std::vector
, the Array has an allocation capacity and a size. The capacity stores the size of the current memory allocation, while the size stores the number of elements in the container. In this way, it can be resized at will without altering the memory allocation as long as the new size is not greater than the currentcapacity()
. Otherwise, HighFM will allocate a new block in memory and move the content of the container to this new block. To avoid constant reallocations when modifying the size of the container, its allocation capacity is always a multiple of theHIGHFM_MEMORY_BLOCK
(default: 1024 elements).- Template Parameters:
ElemType – Type of the elements in the container.
Location – Where the array will be allocated. Options:
Host
orDevice
.
Constructor
-
Array(index_t n)¶
Creates an Array with size
n
. The elements in the container will be initialized to its default value.- Parameters:
n – [in] container size.
- Throws:
LengthError – if `n < 0`.
-
Array(index_t n, ElemType val)¶
Creates an Array and then fills the container with
val
.- Parameters:
n – [in] container size.
val – [in] value to initialize the Array with.
- Throws:
LengthError – if `size < 0`.
-
template<DataLocation OtherLoc>
Array(const DataMap<ElemType, OtherLoc> &data_map)¶ Creates an Array and then copies the content from a
data_map
. Thedata_map
can be located in another memory space (e.g., in the GPU).- Parameters:
data_map – [in] a DataMap to use as the data source.
-
Array(ElemType *ptr, index_t n)¶
Creates an Array and then copies the data from the container pointed by
ptr
. The container must be stored contiguously in memory and be in the same memory space.- Parameters:
ptr – [in] pointer to the beginning of a container.
n – [in] container size.
-
Array(const Array &other)¶
Creates an Array and then copies the content from an
other
Array.- Parameters:
other – [in] another Array to use as the data source.
Destructor
-
virtual ~Array()¶
Deallocates the container and set its size to zero.
Assignment
-
Array &operator=(const Array &other)¶
Copies the content from an
other
Array to this container. This routine will automatically reallocate the Array if needed.- Parameters:
other – [in] another Array to use as the data source.
- Returns:
*this
.
-
template<DataLocation OtherLoc>
Array &operator=(const Array<ElemType, OtherLoc> &other)¶ Copies the content from an
other
Array to this container. Theother
array can be located in another memory space (e.g., in the GPU). This routine will automatically reallocate the Array if needed.- Parameters:
other – [in] another Array to use as the data source.
- Returns:
*this
.
-
template<DataLocation OtherLoc>
Array &operator=(const DataMap<ElemType, OtherLoc> &data_map)¶ Copies the content from a DataMap to this container. The
data_map
can be located in another memory space (e.g., in the GPU). This routine will automatically reallocate the Array if needed.- Parameters:
data_map – [in] a DataMap to use as the data source.
- Returns:
*this
.
-
Array &operator=(Array &&other) noexcept¶
Replaces the container’s content with the contents of an
other
Array, following the move semantics. Theother
array is left in an unspecified state afterwards.- Parameters:
other – [in] another Array to use as the data source.
- Returns:
*this
.
Iterators
-
iterator begin() noexcept¶
- Returns:
an iterator to the beginning of the container.
-
iterator end() noexcept¶
- Returns:
an iterator to
past-the-end
element of the container.
-
const_iterator cbegin() const noexcept¶
- Returns:
a
const_iterator
to the beginning of the container.
-
const_iterator cend() const noexcept¶
- Returns:
a
const_iterator
to thepast-the-end
element of the container.
Element Access
-
ElemType *data() noexcept¶
Note
If
empty() = true
,data()
returns anullptr
.- Returns:
A raw pointer to the beginning of the container.
-
ElemType *data() const noexcept¶
Note
If
empty() = true
,data()
returns anullptr
.- Returns:
A raw pointer to the beginning of the container.
Capacity
-
bool is_empty() const noexcept¶
- Returns:
true
if the container is empty,false
otherwise.
-
void reserve(index_t new_cap)¶
Reallocates the container to
new_cap
ifnew_cap > capacity()
. Otherwise, this routine does nothing.Warning
During the reallocation, this routine will allocate additional memory equal to
size()
to preserve the array’s content. If this behaviour is undesirable, useclear()
before calling this routine.- Parameters:
new_cap – [in] new capacity.
-
void shrink_to_fit()¶
Reallocates the container based on the number of elements within. This routine will reduce the
capacity()
accordingly.Warning
During the reallocation, this routine will allocate additional memory equal to
size()
to preserve the array’s content.
Modifiers
-
void deallocate() noexcept¶
Deallocates the container from memory.
-
void resize(index_t new_size)¶
Resizes the container to
new_size
. Ifnew_size > capacity()
, this routine automatically reallocates the object to contain the additional elements.Note
See
reserve()
in case of reallocation.- Parameters:
new_size – [in] new size of the container.
- Throws:
LengthError – if `new_size < 0`.
-
void push_back(ElemType value)¶
Inserts
value
at the end of the Array. If the resulting size is greater than the currentcapacity()
, this routine automatically reallocates the container.Note
See
reserve()
in case of reallocation.- Parameters:
value – [in] value to be inserted at the end of the Array.