Template Numerical Library version\ main:d08c4dec
Loading...
Searching...
No Matches
TNL::Containers Namespace Reference

Namespace for TNL containers. More...

Namespaces

namespace  NDArraySyncPatterns
 Namespace where synchronization patterns for distributed N-dimensional arrays are defined.

Classes

class  Array
 Array is responsible for memory management, access to array elements, and general array operations. More...
class  ArrayView
 ArrayView is a simple data structure which provides a non-owning encapsulation of array data. That is, ArrayView is like Array without memory management. More...
struct  Block
 Minimal class representing a block of an equidistant D-dimensional lattice. More...
class  ByteArraySynchronizer
class  ConstStaticSizesHolder
class  DistributedArray
 Distributed array. More...
class  DistributedArraySynchronizer
class  DistributedArrayView
 Distributed array view. More...
class  DistributedNDArray
 Distributed N-dimensional array. More...
class  DistributedNDArraySynchronizer
 Synchronizer for DistributedNDArray. More...
class  DistributedNDArrayView
 Distributed N-dimensional array view. More...
class  DistributedVector
 DistributedVector extends DistributedArray with algebraic operations. More...
class  DistributedVectorView
 DistributedVectorView extends DistributedArrayView with algebraic operations. More...
class  GinkgoVector
 Wrapper for representing vectors using Ginkgo data structure. More...
class  HypreParVector
 Wrapper for Hypre's parallel vector. More...
class  HypreVector
 Wrapper for Hypre's sequential vector. More...
class  IndexedMap
class  IndexedSet
struct  LocalBeginsHolder
class  NDArray
 Dynamic N-dimensional array. More...
class  NDArrayIndexer
 Indexer for N-dimensional arrays. It does not store any data, only the sizes of each dimension. More...
class  NDArrayStorage
 Base storage class for NDArray and StaticNDArray. More...
class  NDArrayView
 Simple data structure which provides a non-owning encapsulation of N-dimensional array data. More...
class  SizesHolder
 Holds static and dynamic sizes of an N-dimensional array. More...
class  StaticArray
 Array with constant size. More...
class  StaticNDArray
 Static N-dimensional array. More...
class  StaticSizesHolder
 Holds static sizes of an N-dimensional array. More...
class  StaticVector
 Vector with constant size. More...
class  Subrange
class  UnorderedIndexedSet
class  Vector
 Vector extends Array with algebraic operations. More...
class  VectorView
 VectorView extends ArrayView with algebraic operations. More...

Typedefs

template<typename Index, std::size_t dimension, Index value = 0>
using make_sizes_holder
 Alias template which simplifies creating a SizesHolder type with a constant value for each dimension.

Enumerations

enum class  SyncDirection : std::uint8_t {
  SyncDirection::All = 0xff , SyncDirection::None = 0 , SyncDirection::Right = 1 << 0 , SyncDirection::Left = 1 << 1 ,
  SyncDirection::Top = 1 << 2 , SyncDirection::Bottom = 1 << 3 , SyncDirection::Front = 1 << 4 , SyncDirection::Back = 1 << 5 ,
  SyncDirection::TopRight = Top | Right , SyncDirection::TopLeft = Top | Left , SyncDirection::BottomRight = Bottom | Right , SyncDirection::BottomLeft = Bottom | Left ,
  SyncDirection::BackRight = Back | Right , SyncDirection::BackLeft = Back | Left , SyncDirection::FrontRight = Front | Right , SyncDirection::FrontLeft = Front | Left ,
  SyncDirection::BackTop = Back | Top , SyncDirection::BackBottom = Back | Bottom , SyncDirection::FrontTop = Front | Top , SyncDirection::FrontBottom = Front | Bottom ,
  SyncDirection::BackTopRight = Back | Top | Right , SyncDirection::BackTopLeft = Back | Top | Left , SyncDirection::BackBottomRight = Back | Bottom | Right , SyncDirection::BackBottomLeft = Back | Bottom | Left ,
  SyncDirection::FrontTopRight = Front | Top | Right , SyncDirection::FrontTopLeft = Front | Top | Left , SyncDirection::FrontBottomRight = Front | Bottom | Right , SyncDirection::FrontBottomLeft = Front | Bottom | Left
}
 Directions for data synchronization in a distributed N-dimensional array. More...

Functions

template<typename idx>
std::set< Block< 3, idx > > createInteriorSides (const std::vector< Block< 3, idx > > &decomposition)
 Creates all unique sides of blocks in a 3D decomposition.
template<typename idx, typename OutputIterator>
void createSides (const Block< 3, idx > &block, OutputIterator output)
 Creates the sides of a 3D block and adds them to the output vector.
template<typename idx>
std::set< Block< 3, idx > > createSides (const std::vector< Block< 3, idx > > &decomposition)
 Creates all unique sides of blocks in a 3D decomposition.
template<typename Index>
std::vector< Block< 3, Index > > decomposeBlock (const Block< 3, Index > &global, Index num_x, Index num_y=1, Index num_z=1)
 Decompose a "global" block into several (sub-)blocks in a 3D manner with given block counts along each axis.
template<typename Index>
std::vector< Block< 3, Index > > decomposeBlockOptimal (const Block< 3, Index > &global, Index num_blocks, const std::function< Index(const std::vector< Block< 3, Index > > &) > &f=[](const std::vector< Block< 3, Index > > &decomposition) -> Index { return getInterfaceArea(decomposition);})
 Decompose a "global" block into several (sub-)blocks in an optimal 3D manner.
template<std::size_t I, int N, class T>
constexpr const T && get (const StaticArray< N, T > &&a) noexcept
template<std::size_t I, int N, class T>
constexpr const T & get (const StaticArray< N, T > &a) noexcept
template<std::size_t I, int N, class T>
constexpr const T && get (const StaticVector< N, T > &&a) noexcept
template<std::size_t I, int N, class T>
constexpr const T & get (const StaticVector< N, T > &a) noexcept
template<std::size_t I, int N, class T>
constexpr T && get (StaticArray< N, T > &&a) noexcept
template<std::size_t I, int N, class T>
constexpr T & get (StaticArray< N, T > &a) noexcept
template<std::size_t I, int N, class T>
constexpr T && get (StaticVector< N, T > &&a) noexcept
template<std::size_t I, int N, class T>
constexpr T & get (StaticVector< N, T > &a) noexcept
template<typename idx>
idx getArea (const Block< 3, idx > &block, const StaticVector< 3, idx > &axes_weights={ 1, 1, 1 })
 Calculates the area of a 2D block in 3D space.
template<int D, typename Index>
Block< D, Index >::CoordinatesType getBlockVertex (const Block< D, Index > &block, SyncDirection direction)
 Selects a vertex of a block based on a direction from its center.
template<typename idx>
idx getInterfaceArea (const std::vector< Block< 3, idx > > &decomposition, const StaticVector< 3, idx > &axes_weights={ 1, 1, 1 })
 Calculates the total area of interior sides in a 3D decomposition.
template<typename idx>
double getMaximumImbalance (const std::vector< Block< 3, idx > > &decomposition, idx global_volume=0)
 Calculates the maximum imbalance of blocks in a decomposition.
template<int D, typename idx>
idx getVolume (const Block< D, idx > &block)
 Calculates the volume of a D-dimensional block.
template<int D, typename idx>
idx getVolume (const std::vector< Block< D, idx > > &decomposition)
 Calculates the volume occupied by blocks in a decomposition.
template<class Coefficients, typename Vector>
constexpr auto linearCombination (const std::array< Vector, Coefficients::getSize() > &vectors) -> typename detail::LinearCombinationReturnType< Coefficients, detail::ConstantVectorTypesWrapper< Vector >, std::integral_constant< std::size_t, 0 > >::type
 Generates an expression for a linear combination of vectors.
template<class Coefficients, typename... Vectors, std::enable_if_t< IsArrayType< decltype(Containers::detail::get_from_pack< 0 >(std::declval< Vectors >()...)) >::value, bool>
constexpr auto linearCombination (const Vectors &... vectors) -> typename detail::LinearCombinationReturnType< Coefficients, detail::VectorTypesWrapper< Vectors... >, std::integral_constant< std::size_t, 0 > >::type
 Generates an expression for a linear combination of vectors.
template<typename Output, typename Input>
void nd_absolute (Output &output, const Input &input)
template<typename Output, typename Input1, typename Input2>
void nd_add (Output &output, const Input1 &input1, const Input2 &input2)
template<typename Output, typename Input>
void nd_assign (Output &output, const Input &input)
template<typename Output, typename Input1, typename Input2>
void nd_divide (Output &output, const Input1 &input1, const Input2 &input2)
template<typename Output, typename Func, typename... Input>
void nd_map (Output &output, Func f, const Input &... input)
template<typename Output, typename Input1, typename Input2>
void nd_maximum (Output &output, const Input1 &input1, const Input2 &input2)
template<typename Output, typename Input1, typename Input2>
void nd_minimum (Output &output, const Input1 &input1, const Input2 &input2)
template<typename Output, typename Input1, typename Input2>
void nd_multiply (Output &output, const Input1 &input1, const Input2 &input2)
template<typename Output, typename Input1, typename Input2>
void nd_pow (Output &output, const Input1 &input1, const Input2 &input2)
template<std::size_t axis = 0, typename Input, typename Reduction>
auto nd_reduce (const Input &input, Reduction reduction, typename Input::ValueType identity)
 nd_reduce implements (parallel) reduction across specified axis of N-dimensional array (NDArray) with automatic output allocation.
template<std::size_t axis = 0, typename Input, typename Reduction, typename Output>
void nd_reduce (const Input &input, Reduction reduction, typename Input::ValueType identity, Output &output)
 nd_reduce implements (parallel) reduction across specified axis of N-dimensional array (NDArray).
template<typename Output, typename Input>
void nd_sign (Output &output, const Input &input)
template<typename Output, typename Input>
void nd_sqrt (Output &output, const Input &input)
template<typename Output, typename Input>
void nd_square (Output &output, const Input &input)
template<typename Output, typename Input1, typename Input2>
void nd_subtract (Output &output, const Input1 &input1, const Input2 &input2)
template<int D, typename idx>
bool operator!= (const Block< D, idx > &left, const Block< D, idx > &right)
 Compares two blocks point-wise.
SyncDirection operator& (SyncDirection a, SyncDirection b)
 Bitwise AND operator for SyncDirection.
template<typename SizesHolder, std::size_t ConstValue, typename OtherHolder>
LocalBeginsHolder< SizesHolder, ConstValue > operator+ (const LocalBeginsHolder< SizesHolder, ConstValue > &lhs, const OtherHolder &rhs)
template<typename Index, std::size_t... sizes, typename OtherHolder>
SizesHolder< Index, sizes... > operator+ (const SizesHolder< Index, sizes... > &lhs, const OtherHolder &rhs)
 Combines the sizes of two instance of SizesHolder with the operator +.
template<typename SizesHolder, std::size_t ConstValue, typename OtherHolder>
LocalBeginsHolder< SizesHolder, ConstValue > operator- (const LocalBeginsHolder< SizesHolder, ConstValue > &lhs, const OtherHolder &rhs)
template<typename Index, std::size_t... sizes, typename OtherHolder>
SizesHolder< Index, sizes... > operator- (const SizesHolder< Index, sizes... > &lhs, const OtherHolder &rhs)
 Combines the sizes of two instance of SizesHolder with the operator -.
SyncDirectionoperator-= (SyncDirection &a, SyncDirection b)
 Bitwise operator which clears all bits from b in a.
template<int D, typename idx>
bool operator< (const Block< D, idx > &left, const Block< D, idx > &right)
 Lexicographically compares two blocks by joining their begin and end points.
template<typename Value, typename Device, typename Index>
Fileoperator<< (File &&file, ArrayView< Value, Device, Index > view)
template<typename Value, typename Device, typename Index, typename Allocator>
Fileoperator<< (File &&file, const Array< Value, Device, Index, Allocator > &array)
template<int Size, typename Value>
Fileoperator<< (File &&file, const StaticArray< Size, Value > &array)
 Serialization of static arrays into binary files.
template<typename Value, typename Device, typename Index>
Fileoperator<< (File &file, ArrayView< Value, Device, Index > view)
 Serialization of array views into binary files.
template<typename Value, typename Device, typename Index, typename Allocator>
Fileoperator<< (File &file, const Array< Value, Device, Index, Allocator > &array)
 Serialization of arrays into binary files.
template<int Size, typename Value>
Fileoperator<< (File &file, const StaticArray< Size, Value > &array)
 Serialization of static arrays into binary files.
template<typename Value, typename Device, typename Index, typename Allocator>
std::ostreamoperator<< (std::ostream &str, const Array< Value, Device, Index, Allocator > &array)
 Overloaded insertion operator for printing an array to output stream.
template<typename Value, typename Device, typename Index>
std::ostreamoperator<< (std::ostream &str, const ArrayView< Value, Device, Index > &view)
 Overloaded insertion operator for printing an array view to output stream.
template<int D, typename idx>
std::ostreamoperator<< (std::ostream &str, const Block< D, idx > &block)
 Writes a textual representation of the given multiindex into the given stream.
template<typename SizesHolder, std::size_t ConstValue>
std::ostreamoperator<< (std::ostream &str, const LocalBeginsHolder< SizesHolder, ConstValue > &holder)
template<typename Index, std::size_t... sizes>
std::ostreamoperator<< (std::ostream &str, const SizesHolder< Index, sizes... > &holder)
 Prints the sizes contained in an instance of SizesHolder to the given output stream.
template<int Size, typename Value>
std::ostreamoperator<< (std::ostream &str, const StaticArray< Size, Value > &a)
template<typename Index>
std::ostreamoperator<< (std::ostream &str, const Subrange< Index > &range)
template<typename Value, typename Index, typename Key>
std::ostreamoperator<< (std::ostream &str, IndexedMap< Value, Index, Key > &set)
template<typename Element, typename Index>
std::ostreamoperator<< (std::ostream &str, IndexedSet< Element, Index > &set)
template<class Key, class Index, class Compare, class Allocator>
std::ostreamoperator<< (std::ostream &str, IndexedSet< Key, Index, Compare, Allocator > &set)
template<typename Element, typename Index>
std::ostreamoperator<< (std::ostream &str, UnorderedIndexedSet< Element, Index > &set)
template<class Key, class Index, class Hash, class KeyEqual, class Allocator>
std::ostreamoperator<< (std::ostream &str, UnorderedIndexedSet< Key, Index, Hash, KeyEqual, Allocator > &set)
template<int D, typename idx>
bool operator== (const Block< D, idx > &left, const Block< D, idx > &right)
 Compares two blocks point-wise.
template<typename Value, typename Device, typename Index, typename Allocator>
Fileoperator>> (File &&file, Array< Value, Device, Index, Allocator > &array)
template<typename Value, typename Device, typename Index>
Fileoperator>> (File &&file, ArrayView< Value, Device, Index > view)
template<int Size, typename Value>
Fileoperator>> (File &&file, StaticArray< Size, Value > &array)
 Deserialization of static arrays from binary files.
template<typename Value, typename Device, typename Index, typename Allocator>
Fileoperator>> (File &file, Array< Value, Device, Index, Allocator > &array)
 Deserialization of arrays from binary files.
template<typename Value, typename Device, typename Index>
Fileoperator>> (File &file, ArrayView< Value, Device, Index > view)
 Deserialization of array views from binary files.
template<int Size, typename Value>
Fileoperator>> (File &file, StaticArray< Size, Value > &array)
 Deserialization of static arrays from binary files.
SyncDirection operator| (SyncDirection a, SyncDirection b)
 Bitwise OR operator for SyncDirection.
SyncDirection opposite (SyncDirection direction)
 Returns an opposite direction for given direction.
template<typename DistributedNDArray, std::size_t Q, typename BlockType>
void setNeighbors (DistributedNDArraySynchronizer< DistributedNDArray > &synchronizer, const std::array< SyncDirection, Q > &pattern, int rank, const std::vector< BlockType > &decomposition, const BlockType &global)
 Set neighbors for a synchronizer according to given synchronization pattern and decomposition of a global block.
template<typename Index>
Subrange< Index > splitRange (Index globalSize, const MPI::Comm &communicator)
 A helper function which splits a one-dimensional range.
template<typename Index>
Subrange< Index > splitRange (Index rangeBegin, Index rangeEnd, int rank, int num_subintervals)
 A helper function which splits a one-dimensional range.
template<typename Real>
__cuda_callable__ Real TriangleArea (const StaticVector< 2, Real > &a, const StaticVector< 2, Real > &b, const StaticVector< 2, Real > &c)
template<typename Real>
__cuda_callable__ Real TriangleArea (const StaticVector< 3, Real > &a, const StaticVector< 3, Real > &b, const StaticVector< 3, Real > &c)
template<typename Real>
__cuda_callable__ StaticVector< 3, RealVectorProduct (const StaticVector< 3, Real > &u, const StaticVector< 3, Real > &v)

Detailed Description

Namespace for TNL containers.

Typedef Documentation

◆ make_sizes_holder

template<typename Index, std::size_t dimension, Index value = 0>
using TNL::Containers::make_sizes_holder
Initial value:
typename detail::make_sizes_holder_impl< Index, dimension, value, std::make_index_sequence< dimension > >::type

Alias template which simplifies creating a SizesHolder type with a constant value for each dimension.

Template Parameters
IndexIntegral type used for storing dynamic sizes.
DimensionNumber of values stored in the SizesHolder.
ValueThe value to substitute to the SizesHolder for each dimension.

Function Documentation

◆ decomposeBlock()

template<typename Index>
std::vector< Block< 3, Index > > TNL::Containers::decomposeBlock ( const Block< 3, Index > & global,
Index num_x,
Index num_y = 1,
Index num_z = 1 )

Decompose a "global" block into several (sub-)blocks in a 3D manner with given block counts along each axis.

Parameters
globalThe large block to decompose.
num_xNumber of blocks along the x-axis.
num_yNumber of blocks along the y-axis.
num_zNumber of blocks along the z-axis.
Returns
A vector of the blocks into which the input was decomposed.

◆ decomposeBlockOptimal()

template<typename Index>
std::vector< Block< 3, Index > > TNL::Containers::decomposeBlockOptimal ( const Block< 3, Index > & global,
Index num_blocks,
const std::function< Index(const std::vector< Block< 3, Index > > &) > & f = []( const std::vectorBlock< 3, Index > >& decomposition ) -> Index { return getInterfaceArea( decomposition ); } )

Decompose a "global" block into several (sub-)blocks in an optimal 3D manner.

Parameters
globalThe large block to decompose.
num_blocksNumber of blocks.
fThe objective function that should be minimized.
Returns
A vector of the blocks into which the input was decomposed.

◆ getArea()

template<typename idx>
idx TNL::Containers::getArea ( const Block< 3, idx > & block,
const StaticVector< 3, idx > & axes_weights = { 1, 1, 1 } )

Calculates the area of a 2D block in 3D space.

Parameters
blockThe block whose area will be calculated.
axes_weightsOptional weights that modify the area based on the block orientation. axes_weights.x() multiplies the area of blocks normal to the x-axis, axes_weights.y() multiplies the area of blocks normal to the y-axis, and axes_weights.z() multiplies the area of blocks normal to the z-axis.

◆ getBlockVertex()

template<int D, typename Index>
Block< D, Index >::CoordinatesType TNL::Containers::getBlockVertex ( const Block< D, Index > & block,
SyncDirection direction )

Selects a vertex of a block based on a direction from its center.

Parameters
blockis the block.
directionis the direction from the center of the block to one of its vertices.
Returns
coordinates of the block vertex.
Exceptions
std::invalid_argumentwhen the direction does not point to a vertex. E.g., SyncDirection::BackBottomLeft is valid, but SyncDirection::TopRight or SyncDirection::Left are not.

◆ getInterfaceArea()

template<typename idx>
idx TNL::Containers::getInterfaceArea ( const std::vector< Block< 3, idx > > & decomposition,
const StaticVector< 3, idx > & axes_weights = { 1, 1, 1 } )

Calculates the total area of interior sides in a 3D decomposition.

Parameters
decompositionA vector of blocks that form a 3D decomposition of a large block.
axes_weightsOptional weights that modify interface areas based on the interior side orientation. axes_weights.x() multiplies the area of sides normal to the x-axis, axes_weights.y() multiplies the area of sides normal to the y-axis, and axes_weights.z() multiplies the area of sides normal to the z-axis.

◆ getMaximumImbalance()

template<typename idx>
double TNL::Containers::getMaximumImbalance ( const std::vector< Block< 3, idx > > & decomposition,
idx global_volume = 0 )

Calculates the maximum imbalance of blocks in a decomposition.

Imbalance is a non-negative quantity that measures how much the block's volume differs from the ideal, average volume. If all blocks have the ideal volume, then all blocks have zero imbalance.

Parameters
decompositionVector of blocks in the decomposition.
global_volumeOptional pre-computed volume of all blocks.

◆ linearCombination() [1/2]

template<class Coefficients, typename Vector>
auto TNL::Containers::linearCombination ( const std::array< Vector, Coefficients::getSize() > & vectors) -> typename detail::LinearCombinationReturnType< Coefficients, detail::ConstantVectorTypesWrapper< Vector >, std::integral_constant< std::size_t, 0 > >::type
constexpr

Generates an expression for a linear combination of vectors.

This function creates an expression for a linear combination of vectors, i.e.

\[ \vec ET = \alpha_1 \vec v_1 + \alpha_2 \vec v_2 + \dots + \alpha_n \vec v_n \]

The coefficients \( \alpha_i \) are given as a Coefficients class, which must provide a static method getValue( int i ) that returns the i-th coefficient. The vectors are given as arguments to the function. The transformation to the expression template is done at compile time and so the coefficients must be static.

Template Parameters
CoefficientsClass with static parameters of the linear combination. It must provide a static method getValue( int i ) that returns the i-th coefficient.
VectorType of the vectors in the linear combination. It can be any vector type that can be combined with expression templates, i.e. TNL::Containers::StaticVector, TNL::Containers::Vector or TNL::Containers::DistributedVector.
Parameters
vectorsInput vectors that will be combined with Coefficients.
Returns
An expression object representing the linear combination.

◆ linearCombination() [2/2]

template<class Coefficients, typename... Vectors, std::enable_if_t< IsArrayType< decltype(Containers::detail::get_from_pack< 0 >(std::declval< Vectors >()...)) >::value, bool>
auto TNL::Containers::linearCombination ( const Vectors &... vectors) -> typename detail::LinearCombinationReturnType< Coefficients, detail::VectorTypesWrapper< Vectors... >, std::integral_constant< std::size_t, 0 > >::type
constexpr

Generates an expression for a linear combination of vectors.

This function creates an expression for a linear combination of vectors, i.e.

\[ \vec ET = \alpha_1 \vec v_1 + \alpha_2 \vec v_2 + \dots + \alpha_n \vec v_n \]

The coefficients \( \alpha_i \) are given as a Coefficients class, which must provide a static method getValue( int i ) that returns the i-th coefficient. The vectors are given as arguments to the function. The transformation to the expression template is done at compile time and so the coefficients must be static.

Template Parameters
CoefficientsClass with static parameters of the linear combination. It must provide a static method getValue( int i ) that returns the i-th coefficient.
VectorsA variadic pack of the vector types in the linear combination. Each pack can contain any vector types that can be combined with expression templates, i.e. TNL::Containers::StaticVector, TNL::Containers::Vector or TNL::Containers::DistributedVector.
Parameters
vectorsInput vectors that will be combined with Coefficients.
Returns
An expression object representing the linear combination.

◆ nd_reduce() [1/2]

template<std::size_t axis = 0, typename Input, typename Reduction>
auto TNL::Containers::nd_reduce ( const Input & input,
Reduction reduction,
typename Input::ValueType identity )

nd_reduce implements (parallel) reduction across specified axis of N-dimensional array (NDArray) with automatic output allocation.

This overloaded version automatically creates and returns a new NDArray with dimensions reduced by removing the specified axis. It wraps the original nd_reduce function that requires an output array parameter.

Template Parameters
axisis the dimension index to reduce over (compile-time constant)
Inputis the NDArray type providing access to data and dimensions
Reductionis a binary function object for reduction operation
Parameters
inputis the input NDArray to be reduced
reductionis a binary function object defining the reduction operation This can be a user-defined lambda function or an instance of some Function objects for reduction operations.
identityis the identity element for the reduction operation
Returns
auto-deduced NDArray with dimensions reduced by removing the specified axis

The function creates an output array with dimensions determined by removing the specified axis from the input. For example, reducing a 3D array with dimensions \((X,Y,Z)\) along axis 1 will produce a 2D array with dimensions \((X,Z)\).

The reduction operation is applied across all elements along the specified axis using the same logic as the original nd_reduce function.

Example (2D Reduction)
// Create 2D array
Array array;
array.setSizes( 3, 4 );
// Initialize array with values...
// Perform reduction along axis 0 (returns 1D array of size 4)
auto result = TNL::Containers::nd_reduce< 0 >( array, std::plus<>(), 0.0 );
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:64
Dynamic N-dimensional array.
Definition NDArray.h:538
Holds static and dynamic sizes of an N-dimensional array.
Definition SizesHolder.h:30
Definition Host.h:19
void nd_reduce(const Input &input, Reduction reduction, typename Input::ValueType identity, Output &output)
nd_reduce implements (parallel) reduction across specified axis of N-dimensional array (NDArray).
Definition Reduce.h:137
Example (3D Reduction)
// Create 3D array
Array array;
array.setSizes( 3, 4, 5 );
// Perform maximum reduction along axis 1 (returns 2D array of size `(3, 5)`)
auto result = TNL::Containers::nd_reduce< 1 >( array, TNL::Max(), -INFINITY );
Function object implementing max(x, y).
Definition Functional.h:272
Note
This overload provides more convenient usage by automatically creating the output array with appropriate dimensions. The output array type is deduced based on input dimensionality.
The returned array uses the same device type and memory layout as the input array.
For 1D inputs, this function returns a scalar value rather than an array.
See also
nd_reduce for the original function with explicit output parameter

◆ nd_reduce() [2/2]

template<std::size_t axis = 0, typename Input, typename Reduction, typename Output>
void TNL::Containers::nd_reduce ( const Input & input,
Reduction reduction,
typename Input::ValueType identity,
Output & output )

nd_reduce implements (parallel) reduction across specified axis of N-dimensional array (NDArray).

This function reduces an N-dimensional array to a lower-dimensional array by applying a binary operation along the specified axis. The reduction operation must be associative and commutative for correct results in parallel execution. The function handles different array dimensions (1D to 6D) with customizable axis configuration.

Template Parameters
axisis the dimension index to reduce over (compile-time constant)
Inputis the NDArray type providing access to data and dimensions
Reductionis a binary function object for reduction operation
Outputis the resulting array type with reduced dimensions
Parameters
inputis the input NDArray to be reduced
reductionis a binary function object defining the reduction operation This can be a user-defined lambda function or an instance of some Function objects for reduction operations.
identityis the identity element for the reduction operation
outputis the result array with dimensions reduced by removing the specified axis

The function handles different dimensions through template specialization:

  • 1D: Direct scalar reduction
  • 2D: Reduction along specified axis using 2D reduction kernel
  • 3D: Reduction along specified axis using 3D reduction kernel
  • 4D-6D: Uses permutation sequences to handle axis ordering with parallel execution

The reduction operation is applied across all elements along the specified axis. For higher dimensions (4D+), the implementation uses parallelFor with a kernel that processes sub-ranges of the output dimensions.

Example (2D Reduction)
// Create 2D array
Array array;
array.setSizes( 3, 4 );
// Initialize array with values...
// Create output 1D array
// Perform reduction along axis 0
Example (3D Reduction)
// Create 3D array
Array array;
array.setSizes( 3, 4, 5 );
// Create output 2D array
using OutputArray = TNL::Containers::NDArray< float,
OutputArray result;
// Perform maximum reduction along axis 1 (returns 2D array of size `(3, 5)`)
TNL::Containers::nd_reduce< 1 >( arr, TNL::Max(), -INFINITY, result );
Note
The output array dimensions are automatically set based on the input dimensions with the specified axis removed. For example, reducing a 3D array with dimensions \((X,Y,Z)\) along axis 1 will produce an output array with dimensions \((X,Z)\).
The function uses permutation sequences to handle non-contiguous memory access patterns when the reduction axis is not the first dimension. This ensures correct memory access patterns for performance optimization.
See also
reduce for 1D reduction implementation

◆ operator<<() [1/4]

template<int Size, typename Value>
File & TNL::Containers::operator<< ( File && file,
const StaticArray< Size, Value > & array )

Serialization of static arrays into binary files.

Parameters
fileoutput file
arrayis an array to be written into the output file.

◆ operator<<() [2/4]

template<int Size, typename Value>
File & TNL::Containers::operator<< ( File & file,
const StaticArray< Size, Value > & array )

Serialization of static arrays into binary files.

Parameters
fileoutput file
arrayis an array to be written into the output file.

◆ operator<<() [3/4]

template<typename Value, typename Device, typename Index, typename Allocator>
std::ostream & TNL::Containers::operator<< ( std::ostream & str,
const Array< Value, Device, Index, Allocator > & array )

Overloaded insertion operator for printing an array to output stream.

Template Parameters
Valueis a type of the array elements.
Deviceis a device where the array is allocated.
Indexis a type used for the indexing of the array elements.
Parameters
stris a output stream.
arrayis the array to be printed.
Returns
a reference to the output stream std::ostream.

◆ operator<<() [4/4]

template<typename Value, typename Device, typename Index>
std::ostream & TNL::Containers::operator<< ( std::ostream & str,
const ArrayView< Value, Device, Index > & view )

Overloaded insertion operator for printing an array view to output stream.

Template Parameters
Valueis a type of the array view elements.
Deviceis a device where the array view is allocated.
Indexis a type used for the indexing of the array view elements.
Parameters
stris a output stream.
viewis the array view to be printed.
Returns
a reference to the output stream std::ostream.

◆ operator>>() [1/2]

template<int Size, typename Value>
File & TNL::Containers::operator>> ( File && file,
StaticArray< Size, Value > & array )

Deserialization of static arrays from binary files.

Parameters
fileinput file
arrayis an array to be read from the input file.

◆ operator>>() [2/2]

template<int Size, typename Value>
File & TNL::Containers::operator>> ( File & file,
StaticArray< Size, Value > & array )

Deserialization of static arrays from binary files.

Parameters
fileinput file
arrayis an array to be read from the input file.

◆ splitRange() [1/2]

template<typename Index>
Subrange< Index > TNL::Containers::splitRange ( Index globalSize,
const MPI::Comm & communicator )
nodiscard

A helper function which splits a one-dimensional range.

Parameters
globalSizeSize of the global range [0, globalSize) to be split.
communicatorMPI communicator consisting of ranks for which the range is split.
Returns
A subrange [begin, end) for the calling MPI rank.

◆ splitRange() [2/2]

template<typename Index>
Subrange< Index > TNL::Containers::splitRange ( Index rangeBegin,
Index rangeEnd,
int rank,
int num_subintervals )
nodiscard

A helper function which splits a one-dimensional range.

Parameters
rangeBeginBeginning of the interval [rangeBegin, rangeEnd).
rangeEndEnd of the interval [rangeBegin, rangeEnd).
rankIndex of the subinterval for which the output is calculated.
num_subintervalsTotal number of subintervals.
Returns
A subrange [begin, end) for the specified rank.