TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
Layout utilities

Detailed Description

Enums, type traits, bounds checking, looping over multi-dimensional indices, structs that mimic Python's : and ... syntax and more.

Classes

struct  nda::_linear_index_t
 A small wrapper around a single long integer to be used as a linear index. More...
 
struct  nda::ellipsis
 Mimics Python's ... syntax. More...
 
struct  nda::idx_group_t< Is >
 A group of indices. More...
 
struct  nda::layout_info_t
 Stores information about the memory layout and the stride order of an array/view. More...
 

Enumerations

enum class  nda::layout_prop_e : uint64_t
 Compile-time guarantees of the memory layout of an array/view. More...
 

Functions

template<typename... Args>
void nda::assert_in_bounds (int rank, long const *lengths, Args const &...args)
 Check if the given indices/arguments are within the bounds of an array/view.
 
template<typename F , auto R, std::integral Int = long>
__inline__ void nda::for_each (std::array< Int, R > const &shape, F &&f)
 Loop over all possible index values of a given shape and apply a function to them.
 
template<uint64_t StaticExtents, uint64_t StrideOrder, typename F , auto R, std::integral Int = long>
__inline__ void nda::for_each_static (std::array< Int, R > const &shape, F &&f)
 Loop over all possible index values of a given shape and apply a function to them.
 
template<MemoryArray A>
auto nda::get_block_layout (A const &a)
 Check if a given nda::MemoryArray has a block-strided layout.
 
template<int Rank, uint64_t StaticExtents, uint64_t StrideOrder, layout_prop_e LayoutProp, typename... IdxGrps>
auto nda::group_indices_layout (idx_map< Rank, StaticExtents, StrideOrder, LayoutProp > const &idxm, IdxGrps...)
 Given an nda::idx_map and a partition of its indices, return a new nda::idx_map with the grouped indices.
 
constexpr bool nda::has_contiguous (layout_prop_e lp)
 Checks if a layout property has the contiguous property.
 
constexpr bool nda::has_smallest_stride_is_one (layout_prop_e lp)
 Checks if a layout property has the smallest_stride_is_one property.
 
constexpr bool nda::has_strided_1d (layout_prop_e lp)
 Checks if a layout property has the strided_1d property.
 
constexpr bool nda::layout_property_compatible (layout_prop_e from, layout_prop_e to)
 Checks if two layout properties are compatible with each other.
 
constexpr layout_info_t nda::operator& (layout_info_t lhs, layout_info_t rhs)
 Bitwise AND operator for layout infos.
 
constexpr layout_prop_e nda::operator& (layout_prop_e lhs, layout_prop_e rhs)
 Bitwise AND operator for two layout properties.
 
std::ostream & nda::operator<< (std::ostream &os, ellipsis) noexcept
 Write nda::ellipsis to a std::ostream as ___.
 
std::ostream & nda::operator<< (std::ostream &os, range::all_t) noexcept
 Write nda::range::all_t to a std::ostream as _.
 
template<int Rank, uint64_t StaticExtents, uint64_t StrideOrder, layout_prop_e LayoutProp>
std::ostream & nda::operator<< (std::ostream &sout, idx_map< Rank, StaticExtents, StrideOrder, LayoutProp > const &idxm)
 Write an nda::idx_map to a std::ostream.
 
std::ostream & nda::operator<< (std::ostream &sout, layout_prop_e p)
 Write an nda::layout_prop_e to a std::ostream.
 
constexpr layout_prop_e nda::operator| (layout_prop_e lhs, layout_prop_e rhs)
 Bitwise OR operator for two layout properties.
 

Variables

template<typename... Args>
constexpr bool nda::ellipsis_is_present = is_any_of<ellipsis, std::remove_cvref_t<Args>...>
 Constexpr variable that is true if the parameter pack Args contains an nda::ellipsis.
 
template<typename A >
constexpr layout_info_t nda::get_layout_info = layout_info_t{}
 Constexpr variable that specifies the nda::layout_info_t of type A.
 
template<typename A >
constexpr bool nda::has_contiguous_layout = (has_contiguous(get_layout_info<A>.prop))
 Constexpr variable that is true if type A has the contiguous nda::layout_prop_e guarantee.
 
template<typename A >
constexpr bool nda::has_layout_smallest_stride_is_one = (has_smallest_stride_is_one(get_layout_info<A>.prop))
 Constexpr variable that is true if type A has the smallest_stride_is_one nda::layout_prop_e guarantee.
 
template<typename A >
constexpr bool nda::has_layout_strided_1d = (has_strided_1d(get_layout_info<A>.prop))
 Constexpr variable that is true if type A has the strided_1d nda::layout_prop_e guarantee.
 
template<int... Is>
idx_group_t< Is... > nda::idx_group = {}
 Variable template for an nda::idx_group_t.
 
template<typename T >
constexpr bool nda::is_range_or_ellipsis = is_any_of<std::remove_cvref_t<T>, range, range::all_t, ellipsis>
 Constexpr variable that is true if the type T is either an nda::range, an nda::range::all_t or an nda::ellipsis.
 

Enumeration Type Documentation

◆ layout_prop_e

enum class nda::layout_prop_e : uint64_t
strong

#include <nda/traits.hpp>

Compile-time guarantees of the memory layout of an array/view.

The possible values are:

  • none: No guarantees.
  • strided_1d: There is a constant overall stride in memory.
  • smallest_stride_is_one: The stride in the fastest dimension is 1.
  • contiguous: The array/view is contiguous in memory, i.e. it is strided_1d and smallest_stride_is_one.

Furthermore, the set of values has a partial order:

  • contiguous > strided_1d > none
  • contiguous > smallest_stride_is_one > none

Definition at line 222 of file traits.hpp.

Function Documentation

◆ assert_in_bounds()

template<typename... Args>
void nda::assert_in_bounds ( int rank,
long const * lengths,
Args const &... args )

#include <nda/layout/bound_check_worker.hpp>

Check if the given indices/arguments are within the bounds of an array/view.

It uses the shape and rank of the array/view to check if the given arguments are within bounds. If an error is detected, it throws a std::runtime_error with the error message.

Template Parameters
ArgsTypes of the arguments to be checked.
Parameters
rankRank of the array/view.
lengthsShape of the array/view.
argsArguments (long indices, nda::range, nda::range::all_t or nda::ellipsis) to be checked.

Definition at line 106 of file bound_check_worker.hpp.

◆ for_each()

template<typename F , auto R, std::integral Int = long>
__inline__ void nda::for_each ( std::array< Int, R > const & shape,
F && f )

#include <nda/layout/for_each.hpp>

Loop over all possible index values of a given shape and apply a function to them.

It traverses all possible indices in C-order, i.e. the last index varies the fastest. The shape is specified at runtime and the given function f must be callable with as many long values as the number of dimensions in the shape array, e.g. for a 3-dimensional shape the function must be callable as f(long, long, long).

Template Parameters
FCallable type.
RNumber of dimensions.
IntInteger type used in the shape array.
Parameters
shapeShape to loop over (index bounds).
fCallable object.

Definition at line 129 of file for_each.hpp.

◆ for_each_static()

template<uint64_t StaticExtents, uint64_t StrideOrder, typename F , auto R, std::integral Int = long>
__inline__ void nda::for_each_static ( std::array< Int, R > const & shape,
F && f )

#include <nda/layout/for_each.hpp>

Loop over all possible index values of a given shape and apply a function to them.

It traverses all possible indices in the order given by the encoded StrideOrder parameter. The shape is either specified at runtime (StaticExtents == 0) or, partially or fully, at compile time (StaticExtents != 0). The given function f must be callable with as many long values as the number of dimensions in the shape array, e.g. for a 3-dimensional shape the function must be callable as f(long, long, long).

Template Parameters
StaticExtentsEncoded static extents.
StrideOrderEncoded stride order.
FCallable type.
RNumber of dimensions.
IntInteger type used in the shape array.
Parameters
shapeShape to loop over (index bounds).
fCallable object.

Definition at line 108 of file for_each.hpp.

◆ get_block_layout()

template<MemoryArray A>
auto nda::get_block_layout ( A const & a)

#include <nda/basic_functions.hpp>

Check if a given nda::MemoryArray has a block-strided layout.

If the array is block-strided, return the number of blocks, their size and the stride.

An array is considered to be block-strided if its data in memory is laid out as contiguous blocks of the same size (> 1) repeated with a single stride in memory.

A block-strided array has at most 1 non-contiguous index m, i.e. strides[order[m]] != strides[order[m+1]] * shape[order[m+1]].

Template Parameters
Anda::MemoryArray type.
Parameters
aArray object.
Returns
An optional tuple (if block-strided) containing the number of blocks, their size and the stride between blocks.

Definition at line 560 of file basic_functions.hpp.

◆ group_indices_layout()

template<int Rank, uint64_t StaticExtents, uint64_t StrideOrder, layout_prop_e LayoutProp, typename... IdxGrps>
auto nda::group_indices_layout ( idx_map< Rank, StaticExtents, StrideOrder, LayoutProp > const & idxm,
IdxGrps...  )

#include <nda/group_indices.hpp>

Given an nda::idx_map and a partition of its indices, return a new nda::idx_map with the grouped indices.

It is used to create an nda::group_indices_view: group_indices_view(A, idx_group<i,j,...>, idx_group<k,l,...>, ...). The resulting index map is of rank sizeof...(IdxGrps) with merged indices, one for each group.

Preconditions:

  • The given index groups correspond to a partition of the set {0, 1, ..., R - 1} where R is the rank.
  • In each group the indices are consecutive in memory .

For example, let's look at the following strider order: S = (2, 1, 0). That means that dimension 0 is the fastest varying index, then dimension 1 and dimension 2 is slowest. We say that dimensions 0 and 1 are consecutive in memory. The same goes for dimensions 2 and 1. However, dimensions 0 and 2 are not consecutive in memory.

To check if a group of indices are consecutive in memory, we look at the inverse permutation of the stride order. The stride order S is a permutation which maps an integer j to a dimension/index i of the array such that S[j] = i is the (j+1)-th slowest varying index, i.e. S[0] is the slowest index and S[R - 1] is the fastest. The inverse permutation, M = S^{-1}, therefore tells us how fast a given index varies, e.g. M[i] = j means that index i varies the (j+1)-th slowest.

Template Parameters
RankRank of the original nda::idx_map.
StaticExtentsStatic extents of the original nda::idx_map.
StrideOrderStride order of the original nda::idx_map.
LayoutPropLayout property of the original nda::idx_map.
IdxGrpsGroups of indices.
Parameters
idxmOriginal nda::idx_map.
Returns
New nda::idx_map with the grouped indices.

Definition at line 157 of file group_indices.hpp.

◆ has_contiguous()

bool nda::has_contiguous ( layout_prop_e lp)
inlineconstexpr

#include <nda/traits.hpp>

Checks if a layout property has the contiguous property.

Parameters
lpnda::layout_prop_e to check.
Returns
True if it has the contiguous property, false otherwise.

Definition at line 282 of file traits.hpp.

◆ has_smallest_stride_is_one()

bool nda::has_smallest_stride_is_one ( layout_prop_e lp)
inlineconstexpr

#include <nda/traits.hpp>

Checks if a layout property has the smallest_stride_is_one property.

Parameters
lpnda::layout_prop_e to check.
Returns
True if it has the smallest_stride_is_one property, false otherwise.

Definition at line 274 of file traits.hpp.

◆ has_strided_1d()

bool nda::has_strided_1d ( layout_prop_e lp)
inlineconstexpr

#include <nda/traits.hpp>

Checks if a layout property has the strided_1d property.

Parameters
lpnda::layout_prop_e to check.
Returns
True if it has the strided_1d property, false otherwise.

Definition at line 266 of file traits.hpp.

◆ layout_property_compatible()

bool nda::layout_property_compatible ( layout_prop_e from,
layout_prop_e to )
inlineconstexpr

#include <nda/traits.hpp>

Checks if two layout properties are compatible with each other.

Parameters
fromSource nda::layout_prop_e.
toTarget nda::layout_prop_e.
Returns
True if the source layout property is greater or equal to the target layout property, otherwise false (see nda::layout_prop_e).

Definition at line 237 of file traits.hpp.

◆ operator&() [1/2]

layout_info_t nda::operator& ( layout_info_t lhs,
layout_info_t rhs )
constexpr

#include <nda/traits.hpp>

Bitwise AND operator for layout infos.

Parameters
lhsLeft hand side nda::layout_info_t operand.
rhsRight hand side nda::layout_info_t operand.
Returns
A new nda::layout_info_t with the same stride order as the two arguments and their compatible nda::layout_prop_e. If the stride orders are different, the stride order is set to -1 (undefined, no permutation) and no memory layout guarantee can be made.

Definition at line 312 of file traits.hpp.

◆ operator&() [2/2]

layout_prop_e nda::operator& ( layout_prop_e lhs,
layout_prop_e rhs )
constexpr

#include <nda/traits.hpp>

Bitwise AND operator for two layout properties.

Parameters
lhsLeft hand side nda::layout_prop_e operand.
rhsRight hand side nda::layout_prop_e operand.
Returns
The bitwise AND of their numerical binary representation.

Definition at line 258 of file traits.hpp.

◆ operator<<() [1/4]

std::ostream & nda::operator<< ( std::ostream & os,
ellipsis  )
inlinenoexcept

#include <nda/layout/range.hpp>

Write nda::ellipsis to a std::ostream as ___.

Parameters
osOutput stream.
Returns
Reference to the output stream.

Definition at line 65 of file range.hpp.

◆ operator<<() [2/4]

std::ostream & nda::operator<< ( std::ostream & os,
range::all_t  )
inlinenoexcept

#include <nda/layout/range.hpp>

Write nda::range::all_t to a std::ostream as _.

Parameters
osOutput stream.
Returns
Reference to the output stream.

Definition at line 57 of file range.hpp.

◆ operator<<() [3/4]

template<int Rank, uint64_t StaticExtents, uint64_t StrideOrder, layout_prop_e LayoutProp>
std::ostream & nda::operator<< ( std::ostream & sout,
idx_map< Rank, StaticExtents, StrideOrder, LayoutProp > const & idxm )

#include <nda/print.hpp>

Write an nda::idx_map to a std::ostream.

Template Parameters
RankRank of the nda::idx_map.
StaticExtentsStaticExtents of the nda::idx_map.
StrideOrderStrideOrder of the nda::idx_map.
LayoutPropLayout property of the nda::idx_map.
Parameters
soutstd::ostream object.
idxmnda::idx_map object.
Returns
Reference to std::ostream object.

Definition at line 66 of file print.hpp.

◆ operator<<() [4/4]

std::ostream & nda::operator<< ( std::ostream & sout,
layout_prop_e p )
inline

#include <nda/print.hpp>

Write an nda::layout_prop_e to a std::ostream.

Parameters
soutstd::ostream object.
pnda::layout_prop_e object.
Returns
Reference to std::ostream object.

Definition at line 49 of file print.hpp.

◆ operator|()

layout_prop_e nda::operator| ( layout_prop_e lhs,
layout_prop_e rhs )
constexpr

#include <nda/traits.hpp>

Bitwise OR operator for two layout properties.

Parameters
lhsLeft hand side nda::layout_prop_e operand.
rhsRight hand side nda::layout_prop_e operand.
Returns
The bitwise OR of their numerical binary representation.

Definition at line 249 of file traits.hpp.

Variable Documentation

◆ idx_group

template<int... Is>
idx_group_t<Is...> nda::idx_group = {}
inline

#include <nda/group_indices.hpp>

Variable template for an nda::idx_group_t.

Template Parameters
IsIndices.

Definition at line 124 of file group_indices.hpp.