TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
declarations.hpp
Go to the documentation of this file.
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
16
21
22#pragma once
23
24#include "./accessors.hpp"
25#include "./concepts.hpp"
26#include "./layout/idx_map.hpp"
28#include "./layout/policies.hpp"
30#include "./mem/policies.hpp"
31#include "./traits.hpp"
32
33#include <array>
34#include <cstddef>
35#include <cstdint>
36#include <stdexcept>
37#include <type_traits>
38
39namespace nda {
40
42 // Forward declarations.
43 template <int Rank, uint64_t StaticExtents, uint64_t StrideOrder, layout_prop_e LayoutProp>
44 class idx_map;
45
46 template <typename ValueType, int Rank, typename Layout, char Algebra, typename ContainerPolicy>
47 class basic_array;
48
49 template <typename ValueType, int Rank, typename Layout, char Algebra = 'A',
50 typename AccessorPolicy = nda::default_accessor, //, nda::no_alias_accessor, //,
51 typename OwningPolicy = nda::borrowed<>>
52 class basic_array_view;
53
54 template <char OP, Array A>
55 struct expr_unary;
56
57 template <char OP, ArrayOrScalar L, ArrayOrScalar R>
58 struct expr;
60
65
74 template <typename ValueType, int Rank, typename Layout = C_layout, typename ContainerPolicy = heap<>>
75 using array = basic_array<ValueType, Rank, Layout, 'A', ContainerPolicy>;
76
85 template <typename ValueType, int Rank, typename Layout = C_stride_layout>
86 using array_view = basic_array_view<ValueType, Rank, Layout, 'A', default_accessor, borrowed<>>;
87
95 template <typename ValueType, int Rank, typename Layout = C_stride_layout>
96 using array_const_view = basic_array_view<ValueType const, Rank, Layout, 'A', default_accessor, borrowed<>>;
97
105 template <typename ValueType, int Rank, typename Layout = C_layout>
106 requires(has_contiguous(Layout::template mapping<Rank>::layout_prop))
107 using array_contiguous_view = basic_array_view<ValueType, Rank, Layout, 'A', default_accessor, borrowed<>>;
108
116 template <typename ValueType, int Rank, typename Layout = C_layout>
117 requires(has_contiguous(Layout::template mapping<Rank>::layout_prop))
118 using array_contiguous_const_view = basic_array_view<ValueType const, Rank, Layout, 'A', default_accessor, borrowed<>>;
119
127 template <typename ValueType, typename Layout = C_layout, typename ContainerPolicy = heap<>>
128 using matrix = basic_array<ValueType, 2, Layout, 'M', ContainerPolicy>;
129
137 template <typename ValueType, typename Layout = C_stride_layout>
138 using matrix_view = basic_array_view<ValueType, 2, Layout, 'M', default_accessor, borrowed<>>;
139
146 template <typename ValueType, typename Layout = C_stride_layout>
147 using matrix_const_view = basic_array_view<ValueType const, 2, Layout, 'M', default_accessor, borrowed<>>;
148
155 template <typename ValueType, typename ContainerPolicy = heap<>>
156 using vector = basic_array<ValueType, 1, C_layout, 'V', ContainerPolicy>;
157
165 template <typename ValueType, typename Layout = C_stride_layout>
166 using vector_view = basic_array_view<ValueType, 1, Layout, 'V', default_accessor, borrowed<>>;
167
174 template <typename ValueType, typename Layout = C_stride_layout>
175 using vector_const_view = basic_array_view<ValueType const, 1, Layout, 'V', default_accessor, borrowed<>>;
176
186 template <typename... Is>
187 constexpr uint64_t static_extents(int i0, Is... is) {
188 if (i0 > 15) throw std::runtime_error("Error in nda::static_extents: Only 16 dimensions are supported!");
189 return encode(std::array<int, sizeof...(Is) + 1>{i0, static_cast<int>(is)...});
190 }
191
200 template <typename ValueType, int N0, int... Ns>
202 nda::basic_array<ValueType, 1 + sizeof...(Ns),
203 nda::basic_layout<nda::static_extents(N0, Ns...), nda::C_stride_order<1 + sizeof...(Ns)>, nda::layout_prop_e::contiguous>, 'A',
204 nda::stack<N0 *(Ns *... * 1)>>;
205
214 template <typename ValueType, int N, int M>
216 nda::basic_array<ValueType, 2, nda::basic_layout<nda::static_extents(N, M), nda::C_stride_order<2>, nda::layout_prop_e::contiguous>, 'M',
218
226 template <typename ValueType, int N>
228 nda::basic_array<ValueType, 1, nda::basic_layout<nda::static_extents(N), nda::C_stride_order<1>, nda::layout_prop_e::contiguous>, 'V',
230
238 template <typename ValueType, int Rank, typename Layout = C_layout>
239 using cuarray = basic_array<ValueType, Rank, Layout, 'A', heap<mem::Device>>;
240
248 template <typename ValueType, int Rank, typename Layout = C_stride_layout>
250
258 template <typename ValueType, int Rank, typename Layout = C_stride_layout>
259 using cuarray_const_view = basic_array_view<ValueType const, Rank, Layout, 'A', default_accessor, borrowed<mem::Device>>;
260
268 template <typename ValueType, typename Layout = C_layout, typename ContainerPolicy = heap<mem::Device>>
269 using cumatrix = basic_array<ValueType, 2, Layout, 'M', ContainerPolicy>;
270
277 template <typename ValueType, typename Layout = C_stride_layout>
279
286 template <typename ValueType, typename Layout = C_stride_layout>
288
294 template <typename ValueType>
295 using cuvector = basic_array<ValueType, 1, C_layout, 'V', heap<mem::Device>>;
296
303 template <typename ValueType, typename Layout = C_stride_layout>
305
312 template <typename ValueType, typename Layout = C_stride_layout>
314
316
321
323 template <typename ValueType, int Rank, typename Layout, char Algebra, typename ContainerPolicy>
325
327 template <typename ValueType, int Rank, typename Layout, char Algebra, typename AccessorPolicy, typename OwningPolicy>
329
331 template <typename ValueType, int Rank, typename Layout, char Algebra, typename ContainerPolicy>
333
335 template <typename ValueType, int Rank, typename Layout, char Algebra, typename AccessorPolicy, typename OwningPolicy>
337
339 template <typename ValueType, int Rank, typename Layout, char Algebra, typename ContainerPolicy>
342
344 template <typename ValueType, int Rank, typename Layout, char Algebra, typename AccessorPolicy, typename OwningPolicy>
347
352 template <typename T, typename T2 = std::remove_reference_t<T> /* Keep this: Fix for gcc11 bug */>
353 using get_view_t = std::remove_reference_t<decltype(basic_array_view{std::declval<T>()})>;
354
359 template <typename T, typename T2 = std::remove_reference_t<T> /* Keep this: Fix for gcc11 bug */>
360 using get_regular_t = decltype(basic_array{std::declval<T>()});
361
368 template <typename T, typename RT = get_regular_t<T>>
370 std::conditional_t<mem::on_host<RT>, RT,
373
380 template <typename T, typename RT = get_regular_t<T>>
382 std::conditional_t<mem::on_device<RT>, RT,
385
392 template <typename T, typename RT = get_regular_t<T>>
394 std::conditional_t<mem::on_unified<RT>, RT,
397
399 template <char OP, Array A>
401
403 template <char OP, Array A>
405
407 template <char OP, typename L, typename R>
409
411 template <char OP, typename L, typename R>
413
415
416} // namespace nda
Defines accessors for nda::array objects (cf. std::default_accessor).
Provides definitions and type traits involving the different memory address spaces supported by nda.
A generic view of a multi-dimensional array.
A generic multi-dimensional array.
Layout that specifies how to map multi-dimensional indices to a linear/flat index.
Definition idx_map.hpp:103
Provides concepts for the nda library.
basic_array_view< ValueType, 1, Layout, 'V', default_accessor, borrowed<> > vector_view
Alias template of an nda::basic_array_view with rank 1, a 'V' algebra, nda::default_accessor and nda:...
basic_array_view< ValueType, Rank, Layout, 'A', default_accessor, borrowed<> > array_view
Alias template of an nda::basic_array_view with an 'A' algebra, nda::default_accessor and nda::borrow...
nda::basic_array< ValueType, 2, nda::basic_layout< nda::static_extents(N, M), nda::C_stride_order< 2 >, nda::layout_prop_e::contiguous >, 'M', nda::stack< static_cast< size_t >(N *M)> > stack_matrix
Alias template of an nda::basic_array with rank 2, static extents, contiguous C layout,...
basic_array_view< ValueType, 1, Layout, 'V', default_accessor, borrowed< mem::Device > > cuvector_view
Similar to nda::vector_view except the memory is stored on the device.
basic_array< ValueType, 1, C_layout, 'V', heap< mem::Device > > cuvector
Similar to nda::vector except the memory is stored on the device.
basic_array_view< ValueType const, 2, Layout, 'M', default_accessor, borrowed< mem::Device > > cumatrix_const_view
Similar to nda::matrix_const_view except the memory is stored on the device.
basic_array_view< ValueType, Rank, Layout, 'A', default_accessor, borrowed<> > array_contiguous_view
Same as nda::array_view except for contiguous memory layouts.
basic_array_view< ValueType, 2, Layout, 'M', default_accessor, borrowed<> > matrix_view
Alias template of an nda::basic_array_view with rank 2, an 'M' algebra, nda::default_accessor and nda...
basic_array_view< ValueType const, Rank, Layout, 'A', default_accessor, borrowed<> > array_contiguous_const_view
Same as nda::array_const_view except for contiguous memory layouts.
basic_array_view< ValueType const, Rank, Layout, 'A', default_accessor, borrowed<> > array_const_view
Same as nda::array_view except for const value types.
basic_array< ValueType, Rank, Layout, 'A', ContainerPolicy > array
Alias template of an nda::basic_array with an 'A' algebra.
nda::basic_array< ValueType, 1, nda::basic_layout< nda::static_extents(N), nda::C_stride_order< 1 >, nda::layout_prop_e::contiguous >, 'V', nda::stack< N > > stack_vector
Alias template of an nda::basic_array with rank 1, static extents, contiguous C layout,...
basic_array< ValueType, Rank, Layout, 'A', heap< mem::Device > > cuarray
Similar to nda::array except the memory is stored on the device.
basic_array_view< ValueType const, Rank, Layout, 'A', default_accessor, borrowed< mem::Device > > cuarray_const_view
Similar to nda::array_const_view except the memory is stored on the device.
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > cumatrix
Similar to nda::matrix except the memory is stored on the device.
basic_array_view< ValueType const, 1, Layout, 'V', default_accessor, borrowed<> > vector_const_view
Same as nda::vector_view except for const value types.
basic_array< ValueType, 1, C_layout, 'V', ContainerPolicy > vector
Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
basic_array_view< ValueType const, 2, Layout, 'M', default_accessor, borrowed<> > matrix_const_view
Same as nda::matrix_view except for const value types.
basic_array_view< ValueType, 2, Layout, 'M', default_accessor, borrowed< mem::Device > > cumatrix_view
Similar to nda::matrix_view except the memory is stored on the device.
basic_array< ValueType, 2, Layout, 'M', ContainerPolicy > matrix
Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
basic_array_view< ValueType, Rank, Layout, 'A', default_accessor, borrowed< mem::Device > > cuarray_view
Similar to nda::array_view except the memory is stored on the device.
basic_array_view< ValueType const, 1, Layout, 'V', default_accessor, borrowed< mem::Device > > cuvector_const_view
Similar to nda::vector_const_view except the memory is stored on the device.
nda::basic_array< ValueType, 1+sizeof...(Ns), nda::basic_layout< nda::static_extents(N0, Ns...), nda::C_stride_order< 1+sizeof...(Ns)>, nda::layout_prop_e::contiguous >, 'A', nda::stack< N0 *(Ns *... *1)> > stack_array
Alias template of an nda::basic_array with static extents, contiguous C layout, 'A' algebra and nda::...
constexpr bool is_regular_v
Constexpr variable that is true if type A is a regular array, i.e. an nda::basic_array.
Definition traits.hpp:145
constexpr char get_algebra
Constexpr variable that specifies the algebra of a type.
Definition traits.hpp:126
std::conditional_t< mem::on_device< RT >, RT, basic_array< get_value_t< RT >, get_rank< RT >, get_contiguous_layout_policy< get_rank< RT >, get_layout_info< RT >.stride_order >, get_algebra< RT >, heap< mem::Device > > > get_regular_device_t
Get the type of the nda::basic_array that would be obtained by constructing an array on device memory...
decltype(basic_array{std::declval< T >()}) get_regular_t
Get the type of the nda::basic_array that would be obtained by constructing an array from a given typ...
std::conditional_t< mem::on_unified< RT >, RT, basic_array< get_value_t< RT >, get_rank< RT >, get_contiguous_layout_policy< get_rank< RT >, get_layout_info< RT >.stride_order >, get_algebra< RT >, heap< mem::Unified > > > get_regular_unified_t
Get the type of the nda::basic_array that would be obtained by constructing an array on unified memor...
std::conditional_t< mem::on_host< RT >, RT, basic_array< get_value_t< RT >, get_rank< RT >, get_contiguous_layout_policy< get_rank< RT >, get_layout_info< RT >.stride_order >, get_algebra< RT >, heap< mem::Host > > > get_regular_host_t
Get the type of the nda::basic_array that would be obtained by constructing an array on host memory f...
constexpr uint64_t static_extents(int i0, Is... is)
Encode the given shape into a single integer using the nda::encode function.
constexpr int get_rank
Constexpr variable that specifies the rank of an nda::Array or of a contiguous 1-dimensional range.
Definition traits.hpp:136
std::remove_reference_t< decltype(basic_array_view{std::declval< T >()})> get_view_t
Get the type of the nda::basic_array_view that would be obtained by constructing a view from a given ...
constexpr bool is_view_v
Constexpr variable that is true if type A is a view, i.e. an nda::basic_array_view.
Definition traits.hpp:154
constexpr uint64_t C_stride_order
C/Row-major stride order.
Definition idx_map.hpp:65
std::conditional_t< StrideOrder==C_stride_order< Rank >, C_layout, std::conditional_t< StrideOrder==Fortran_stride_order< Rank >, F_layout, contiguous_layout_with_stride_order< StrideOrder > > > get_contiguous_layout_policy
Get the contiguous layout policy for a given rank and stride order.
Definition policies.hpp:141
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:282
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
Definition traits.hpp:321
heap_basic< mem::mallocator< AdrSp > > heap
Alias template of the nda::heap_basic policy using an nda::mem::mallocator.
Definition policies.hpp:63
constexpr uint64_t encode(std::array< int, N > const &a)
Encode a std::array<int, N> in a uint64_t.
Provides a class that maps multi-dimensional indices to a linear index and vice versa.
Provides definitions of various layout policies.
Defines various memory handling policies.
Provides utilities to work with permutations and to compactly encode/decode std::array objects.
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:47
Generic layout policy with arbitrary order.
Definition policies.hpp:115
Memory policy using an nda::mem::handle_borrowed.
Definition policies.hpp:108
Default accessor for various array and view types.
Definition accessors.hpp:36
Lazy unary expression for nda::Array types.
Lazy binary expression for nda::ArrayOrScalar types.
static constexpr layout_info_t compute_layout_info()
Compute the layout information of the expression.
static constexpr char algebra
Constexpr variable specifying the algebra of one of the non-scalar operands.
Stores information about the memory layout and the stride order of an array/view.
Definition traits.hpp:295
Memory policy using an nda::mem::handle_stack.
Definition policies.hpp:84
Provides type traits for the nda library.