Using memory constructs from the C++ Standard Template Library (STL) in a Chimera Compute Library (CCL) development context is generally not supported or recommended.
In some cases, std::array may work, but it is highly recommend that developers attempt to use CCL-defined constructs, such as
/src/core/types/ct_containers.hppSymbol: NDArrayNDArrayFile: /src/core/types/ct_containers.hppSymbol: NDArrayNDArray Type
/src/core/types/ct_containers.hppSymbol: NDArrayNDArrayNDArray is an address-space agnostic, fixed-size, aggregate type designed for use in a CCL development context in an embedded environment without support for the C++ Standard Template Library (STL). It is intended to be used in place of std::array objects.
It also has experimental support for multi-dimensional representations and indexing, but usage of these features is not encourage at this time.
NDArray is not inherently a Processing Element (PE) or IMD construct. An NDArray wraps a C-style array of elements, which can be classified as either [PE] or [IMD] to specify the intended usage. Usage of elements within the array will follow same rules as other [PE] and [IMD] types.
/src/core/types/ct_containers.hppLines 111–122 /**
* @brief An address-space agnostic, fixed-size, aggregate type designed for use in a CCL
* development context in an embedded environment without support for the C++ Standard
* Template Library (STL).
*
* It is intended to be used in place of `std::array` objects.
*
* @tparam T Type of elements that make up the NDArray.
* @tparam dimensions Dimensions of the NDArray.
*/
template <typename T, std::size_t... dimensions>
struct NDArray {
Public Members
Raw linear array of items of type T and of size linearElementCount matching the volume of the N-dimension array, i.e. dimN * ... * dim1 * dim0.
/src/core/types/ct_containers.hppLine 229 T data[linearElementCount];
Utility Methods
/src/core/types/ct_containers.hppLines 388–393 /**
* @brief Get the size of the NDArray (which is the size of its leftmost dimension)
*
* @return std::size_t The size of the NDArray.
*/
INLINE static constexpr std::size_t size() noexcept { return DM::getLeftmostDimension(); }
/src/core/types/ct_containers.hppLine 394 INLINE static constexpr std::size_t maxSize() noexcept { return DM::getLeftmostDimension(); }
Element Getter & Setter Methods
The overload of the [] operator is designed to act like raw multidimensional C arrays in row-major order.
When the defined NDArrayis one-dimensional, i.e. sizeof...(dimensions) == 1, the [] operator returns a reference to the element at the index provided.
When the defined NDArrayis multidimensional, i.e. sizeof...(dimensions) > 1, the [] operator returns a reference to the element N * dimN-1 * ... * dim0cast as a type of NDArray<T, dimN-1, ..., dim0>.
/src/core/types/ct_containers.hppLines 231–237 /**
* @brief Access the element of the NDArray at a certain position
*
* @param index the position that will be accessed within the NDArray
* @return NextDimType& the element at that position
*/
INLINE NextDimType& operator[](chimera-compute-library-ccl-api-reference/std::size_t index) { return *(reinterpret_cast<NextDimType*>(this) + index); }
/src/core/types/ct_containers.hppLines 367–377 /**
* @brief Slices an NDArray into a sub-NDArray when given an outerDim (i.e. length of the slice)
* and an offsetIndex (i.e the starting index of the slice). Throws errors when
* the slice would go out of bounds.
*
* @tparam outerDim the length of the outer dimension of the NDArray included in the slice
* @param offsetIndex the first index that the slice begins (offset from the beginning of the NDArray)
* @return a NDArray containing the slice
*/
template <std::size_t outerDim>
INLINE typename SlicedNDArray<outerDim, dimensions...>::type& slice(qVar_t<std::size_t> offsetIndex) {
/src/core/types/ct_containers.hppLines 396–401 /**
* @brief Fills all elements of the NDArray with a certain value
*
* @param val the value that we are filling the NDArray with
*/
INLINE constexpr void fill(const Tbase& val) noexcept {
/src/core/types/ct_containers.hppLines 396–401 /**
* @brief Fills all elements of the NDArray with a certain value
*
* @param val the value that we are filling the NDArray with
*/
INLINE constexpr void fill(const Tbase& val) noexcept {
/src/core/types/ct_containers.hppLines 325–338 /**
* @brief Copies over all values from the current ND Array into
* a new ND Array with different dimensions and different types.
* Note that the values are assigned into the new ND Array, so
* behavior is different than asCast(). The new dimensions must contain
* the same number of elements as the dimensions of the current ND Array.
*
* @tparam TNew the new type for the returned ND Array
* @tparam dims the new dimensions for the returned ND Array
* @return a new ND Array with values copied over from the current ND Array with
* different dimensions and a different type
*/
template <typename TNew, std::size_t... dims, std::enable_if_t<(sizeof...(dims) > 0), int> = 0>
INLINE NDArray<TNew, dims...> into() {
Reshape Methods
All of these operations are only useful for multi-dimensional arrays and are considered experimental.
/src/core/types/ct_containers.hppLines 244–251 /**
* @brief Flattens the current ND Array by returning
* a 1D NDArray that contains all tiles.
* A static_assert verifies that flattening is possible.
*
* @return a new 1D NDArray containing all tiles
*/
INLINE FlatCastType& flattenCast() {
Returns a reference to the array, cast to a different shape with the same number of elements as the original array. TCast may also be provided as an explicit reinterpretation to another data type, in which the number of elements is proportional to the difference in element sizes. In other words, the resulting cast will represent the exact same memory size.
/src/core/types/ct_containers.hppLines 258–267 /**
* @brief Reshapes the current ND Array given
* a set of dimensions. If this reshaping is not possible,
* raise an error.
*
* @tparam newDims the dimensions of the reshaped ND Array
* @return a ND Array reshaped to the parameters needs
*/
template <std::size_t... newDims>
INLINE NDArray<T, newDims...>& reshapeCast() {
/src/core/types/ct_containers.hppLines 325–338 /**
* @brief Copies over all values from the current ND Array into
* a new ND Array with different dimensions and different types.
* Note that the values are assigned into the new ND Array, so
* behavior is different than asCast(). The new dimensions must contain
* the same number of elements as the dimensions of the current ND Array.
*
* @tparam TNew the new type for the returned ND Array
* @tparam dims the new dimensions for the returned ND Array
* @return a new ND Array with values copied over from the current ND Array with
* different dimensions and a different type
*/
template <typename TNew, std::size_t... dims, std::enable_if_t<(sizeof...(dims) > 0), int> = 0>
INLINE NDArray<TNew, dims...> into() {
Data Type Casting Methods
/src/core/types/ct_containers.hppLines 292–303 /**
* @brief Reinterperet casts the current ND Array to a new ND Array containing
* a different type, possibly with different size. Note that this
* type conversion is a reinterpretation of the underlying bit pattern.
* Thus, the size of the new ND Array must match the size of the old ND Array.
*
* @tparam TAs the new type of the returned ND Array
* @tparam newDims the dimensions of the reshaped ND Array
* @return a ND Array reinterpreted to a new type
*/
template <typename TAs, std::size_t... newDims, std::enable_if_t<sizeof...(newDims) != 0, std::int32_t> = 0>
INLINE NDArray<TAs, newDims...>& reinterpretCast() {
/src/core/types/ct_containers.hppLines 309–318 /**
* @brief Reinterpret casts the current ND Array to a new ND Array containing a different type.
* This overload is for the use case where we don't have manual output dimension specification (i.e.
* we keep the same dimensions), we require that the new type is the same width as the old type.
*
* @tparam TAs the new type of the returned ND Array
* @return an ND Array reinterpreted to a new type
*/
template <typename TAs>
INLINE NDArray<TAs, dimensions...>& reinterpretCast() {
Examples
/tests/sweep_tests/ndarray_reinterpret_cast/sweep_ndarray_reinterpret_cast.cppLines 59–62 container::NDArray<qVar_t<sweeps::InType>, dim1 * dim2 * dim3 * dim4> arr = readFlow.read();
// reshape input 1d NDArray to 4d NDArray.
auto reshaped = arr.reshapeCast<dim1, dim2, dim3, dim4>();
/src/core/types/ct_containers.hppLines 325–338 /**
* @brief Copies over all values from the current ND Array into
* a new ND Array with different dimensions and different types.
* Note that the values are assigned into the new ND Array, so
* behavior is different than asCast(). The new dimensions must contain
* the same number of elements as the dimensions of the current ND Array.
*
* @tparam TNew the new type for the returned ND Array
* @tparam dims the new dimensions for the returned ND Array
* @return a new ND Array with values copied over from the current ND Array with
* different dimensions and a different type
*/
template <typename TNew, std::size_t... dims, std::enable_if_t<(sizeof...(dims) > 0), int> = 0>
INLINE NDArray<TNew, dims...> into() {