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-2023 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: Olivier Parcollet, Nils Wentzell
16
17/**
18 * @file
19 * @brief Provides various convenient aliases and helper functions for nda::basic_array and nda::basic_array_view.
20 */
21
22#pragma once
23
24#include "./accessors.hpp"
25#include "./concepts.hpp"
26#include "./layout/idx_map.hpp"
27#include "./layout/permutation.hpp"
28#include "./layout/policies.hpp"
29#include "./mem/address_space.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
41 /// @cond
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;
59 /// @endcond
60
61 /**
62 * @addtogroup av_types
63 * @{
64 */
65
66 /**
67 * @brief Alias template of an nda::basic_array with an 'A' algebra.
68 *
69 * @tparam ValueType Value type of the array.
70 * @tparam Rank Rank of the array.
71 * @tparam Layout Layout policy of the array.
72 * @tparam ContainerPolicy Memory policy of the array.
73 */
74 template <typename ValueType, int Rank, typename Layout = C_layout, typename ContainerPolicy = heap<>>
75 using array = basic_array<ValueType, Rank, Layout, 'A', ContainerPolicy>;
76
77 /**
78 * @brief Alias template of an nda::basic_array_view with an 'A' algebra, nda::default_accessor and nda::borrowed
79 * owning policy.
80 *
81 * @tparam ValueType Value type of the view.
82 * @tparam Rank Rank of the view.
83 * @tparam Layout Layout policy of the view.
84 */
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
88 /**
89 * @brief Same as nda::array_view except for const value types.
90 *
91 * @tparam ValueType Value type of the view.
92 * @tparam Rank Rank of the view.
93 * @tparam Layout Layout policy of the view.
94 */
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
98 /**
99 * @brief Same as nda::array_view except for contiguous memory layouts.
100 *
101 * @tparam ValueType Value type of the view.
102 * @tparam Rank Rank of the view.
103 * @tparam Layout Layout policy of the view.
104 */
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
109 /**
110 * @brief Same as nda::array_const_view except for contiguous memory layouts.
111 *
112 * @tparam ValueType Value type of the view.
113 * @tparam Rank Rank of the view.
114 * @tparam Layout Layout policy of the view.
115 */
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
120 /**
121 * @brief Alias template of an nda::basic_array with rank 2 and an 'M' algebra.
122 *
123 * @tparam ValueType Value type of the matrix.
124 * @tparam Layout Layout policy of the matrix.
125 * @tparam ContainerPolicy Memory policy of the matrix.
126 */
127 template <typename ValueType, typename Layout = C_layout, typename ContainerPolicy = heap<>>
128 using matrix = basic_array<ValueType, 2, Layout, 'M', ContainerPolicy>;
129
130 /**
131 * @brief Alias template of an nda::basic_array_view with rank 2, an 'M' algebra, nda::default_accessor and
132 * nda::borrowed owning policy.
133 *
134 * @tparam ValueType Value type of the view.
135 * @tparam Layout Layout policy of the view.
136 */
137 template <typename ValueType, typename Layout = C_stride_layout>
138 using matrix_view = basic_array_view<ValueType, 2, Layout, 'M', default_accessor, borrowed<>>;
139
140 /**
141 * @brief Same as nda::matrix_view except for const value types.
142 *
143 * @tparam ValueType Value type of the view.
144 * @tparam Layout Layout policy of the view.
145 */
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
149 /**
150 * @brief Alias template of an nda::basic_array with rank 1 and a 'V' algebra.
151 *
152 * @tparam ValueType Value type of the vector.
153 * @tparam ContainerPolicy Memory policy of the vector.
154 */
155 template <typename ValueType, typename ContainerPolicy = heap<>>
156 using vector = basic_array<ValueType, 1, C_layout, 'V', ContainerPolicy>;
157
158 /**
159 * @brief Alias template of an nda::basic_array_view with rank 1, a 'V' algebra, nda::default_accessor and
160 * nda::borrowed owning policy.
161 *
162 * @tparam ValueType Value type of the view.
163 * @tparam Layout Layout policy of the view.
164 */
165 template <typename ValueType, typename Layout = C_stride_layout>
166 using vector_view = basic_array_view<ValueType, 1, Layout, 'V', default_accessor, borrowed<>>;
167
168 /**
169 * @brief Same as nda::vector_view except for const value types.
170 *
171 * @tparam ValueType Value type of the view.
172 * @tparam Layout Layout policy of the view.
173 */
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
177 /**
178 * @ingroup av_utils
179 * @brief Encode the given shape into a single integer using the nda::encode function.
180 *
181 * @tparam Is Integer sequence describing the shape.
182 * @param i0 Size of the first dimension.
183 * @param is Sizes of the other dimensions.
184 * @return `uint64_t` encoded shape.
185 */
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
192 /**
193 * @brief Alias template of an nda::basic_array with static extents, contiguous C layout, 'A' algebra and nda::stack
194 * container policy.
195 *
196 * @tparam ValueType Value type of the array.
197 * @tparam N0 Size of the first dimension.
198 * @tparam Ns Sizes of the other dimensions.
199 */
200 template <typename ValueType, int N0, int... Ns>
201 using stack_array =
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
206 /**
207 * @brief Alias template of an nda::basic_array with rank 2, static extents, contiguous C layout, 'M' algebra and
208 * nda::stack memory policy.
209 *
210 * @tparam ValueType Value type of the matrix.
211 * @tparam N Number of rows.
212 * @tparam M Number of columns.
213 */
214 template <typename ValueType, int N, int M>
215 using stack_matrix =
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',
217 nda::stack<static_cast<size_t>(N *M)>>;
218
219 /**
220 * @brief Alias template of an nda::basic_array with rank 1, static extents, contiguous C layout, 'V' algebra and
221 * nda::stack memory policy.
222 *
223 * @tparam ValueType Value type of the vector.
224 * @tparam N Size of the vector.
225 */
226 template <typename ValueType, int N>
227 using stack_vector =
228 nda::basic_array<ValueType, 1, nda::basic_layout<nda::static_extents(N), nda::C_stride_order<1>, nda::layout_prop_e::contiguous>, 'V',
229 nda::stack<N>>;
230
231 /**
232 * @brief Similar to nda::array except the memory is stored on the device.
233 *
234 * @tparam ValueType Value type of the array.
235 * @tparam Rank Rank of the array.
236 * @tparam Layout Layout policy of the array.
237 */
238 template <typename ValueType, int Rank, typename Layout = C_layout>
239 using cuarray = basic_array<ValueType, Rank, Layout, 'A', heap<mem::Device>>;
240
241 /**
242 * @brief Similar to nda::array_view except the memory is stored on the device.
243 *
244 * @tparam ValueType Value type of the view.
245 * @tparam Rank Rank of the view.
246 * @tparam Layout Layout policy of the view.
247 */
248 template <typename ValueType, int Rank, typename Layout = C_stride_layout>
249 using cuarray_view = basic_array_view<ValueType, Rank, Layout, 'A', default_accessor, borrowed<mem::Device>>;
250
251 /**
252 * @brief Similar to nda::array_const_view except the memory is stored on the device.
253 *
254 * @tparam ValueType Value type of the view.
255 * @tparam Rank Rank of the view.
256 * @tparam Layout Layout policy of the view.
257 */
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
261 /**
262 * @brief Similar to nda::matrix except the memory is stored on the device.
263 *
264 * @tparam ValueType Value type of the matrix.
265 * @tparam Layout Layout policy of the matrix.
266 * @tparam ContainerPolicy Memory policy of the matrix.
267 */
268 template <typename ValueType, typename Layout = C_layout, typename ContainerPolicy = heap<mem::Device>>
269 using cumatrix = basic_array<ValueType, 2, Layout, 'M', ContainerPolicy>;
270
271 /**
272 * @brief Similar to nda::matrix_view except the memory is stored on the device.
273 *
274 * @tparam ValueType Value type of the view.
275 * @tparam Layout Layout policy of the view.
276 */
277 template <typename ValueType, typename Layout = C_stride_layout>
278 using cumatrix_view = basic_array_view<ValueType, 2, Layout, 'M', default_accessor, borrowed<mem::Device>>;
279
280 /**
281 * @brief Similar to nda::matrix_const_view except the memory is stored on the device.
282 *
283 * @tparam ValueType Value type of the view.
284 * @tparam Layout Layout policy of the view.
285 */
286 template <typename ValueType, typename Layout = C_stride_layout>
287 using cumatrix_const_view = basic_array_view<ValueType const, 2, Layout, 'M', default_accessor, borrowed<mem::Device>>;
288
289 /**
290 * @brief Similar to nda::vector except the memory is stored on the device.
291 *
292 * @tparam ValueType Value type of the vector.
293 */
294 template <typename ValueType>
295 using cuvector = basic_array<ValueType, 1, C_layout, 'V', heap<mem::Device>>;
296
297 /**
298 * @brief Similar to nda::vector_view except the memory is stored on the device.
299 *
300 * @tparam ValueType Value type of the view.
301 * @tparam Layout Layout policy of the view.
302 */
303 template <typename ValueType, typename Layout = C_stride_layout>
304 using cuvector_view = basic_array_view<ValueType, 1, Layout, 'V', default_accessor, borrowed<mem::Device>>;
305
306 /**
307 * @brief Similar to nda::vector_const_view except the memory is stored on the device.
308 *
309 * @tparam ValueType Value type of the view.
310 * @tparam Layout Layout policy of the view.
311 */
312 template <typename ValueType, typename Layout = C_stride_layout>
313 using cuvector_const_view = basic_array_view<ValueType const, 1, Layout, 'V', default_accessor, borrowed<mem::Device>>;
314
315 /** @} */
316
317 /**
318 * @addtogroup av_utils
319 * @{
320 */
321
322 /// Specialization of nda::is_regular_v for nda::basic_array.
323 template <typename ValueType, int Rank, typename Layout, char Algebra, typename ContainerPolicy>
325
326 /// Specialization of nda::is_view_v for nda::basic_array_view.
327 template <typename ValueType, int Rank, typename Layout, char Algebra, typename AccessorPolicy, typename OwningPolicy>
329
330 /// Specialization of nda::get_algebra for nda::basic_array types.
331 template <typename ValueType, int Rank, typename Layout, char Algebra, typename ContainerPolicy>
332 inline constexpr char get_algebra<basic_array<ValueType, Rank, Layout, Algebra, ContainerPolicy>> = Algebra;
333
334 /// Specialization of nda::get_algebra for nda::basic_array_view types.
335 template <typename ValueType, int Rank, typename Layout, char Algebra, typename AccessorPolicy, typename OwningPolicy>
337
338 /// Specialization of nda::get_layout_info for nda::basic_array types.
339 template <typename ValueType, int Rank, typename Layout, char Algebra, typename ContainerPolicy>
340 inline constexpr layout_info_t get_layout_info<basic_array<ValueType, Rank, Layout, Algebra, ContainerPolicy>> =
341 basic_array<ValueType, Rank, Layout, Algebra, ContainerPolicy>::layout_t::layout_info;
342
343 /// Specialization of nda::get_layout_info for nda::basic_array_view types.
344 template <typename ValueType, int Rank, typename Layout, char Algebra, typename AccessorPolicy, typename OwningPolicy>
345 inline constexpr layout_info_t get_layout_info<basic_array_view<ValueType, Rank, Layout, Algebra, AccessorPolicy, OwningPolicy>> =
346 basic_array_view<ValueType, Rank, Layout, Algebra, AccessorPolicy, OwningPolicy>::layout_t::layout_info;
347
348 /**
349 * @brief Get the type of the nda::basic_array_view that would be obtained by constructing a view from a given type.
350 * @tparam T Type to construct a view from.
351 */
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
355 /**
356 * @brief Get the type of the nda::basic_array that would be obtained by constructing an array from a given type.
357 * @tparam T Type to construct an array from.
358 */
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
362 /**
363 * @brief Get the type of the nda::basic_array that would be obtained by constructing an array on host memory from a
364 * given type.
365 *
366 * @tparam T Type to construct an array from.
367 */
368 template <typename T, typename RT = get_regular_t<T>>
369 using get_regular_host_t =
370 std::conditional_t<mem::on_host<RT>, RT,
371 basic_array<get_value_t<RT>, get_rank<RT>, get_contiguous_layout_policy<get_rank<RT>, get_layout_info<RT>.stride_order>,
372 get_algebra<RT>, heap<mem::Host>>>;
373
374 /**
375 * @brief Get the type of the nda::basic_array that would be obtained by constructing an array on device memory from
376 * a given type.
377 *
378 * @tparam T Type to construct an array from.
379 */
380 template <typename T, typename RT = get_regular_t<T>>
381 using get_regular_device_t =
382 std::conditional_t<mem::on_device<RT>, RT,
383 basic_array<get_value_t<RT>, get_rank<RT>, get_contiguous_layout_policy<get_rank<RT>, get_layout_info<RT>.stride_order>,
384 get_algebra<RT>, heap<mem::Device>>>;
385
386 /**
387 * @brief Get the type of the nda::basic_array that would be obtained by constructing an array on unified memory from
388 * a given type.
389 *
390 * @tparam T Type to construct an array from.
391 */
392 template <typename T, typename RT = get_regular_t<T>>
393 using get_regular_unified_t =
394 std::conditional_t<mem::on_unified<RT>, RT,
395 basic_array<get_value_t<RT>, get_rank<RT>, get_contiguous_layout_policy<get_rank<RT>, get_layout_info<RT>.stride_order>,
396 get_algebra<RT>, heap<mem::Unified>>>;
397
398 /// Specialization of nda::get_algebra for nda::expr_unary types.
399 template <char OP, Array A>
400 inline constexpr char get_algebra<expr_unary<OP, A>> = get_algebra<A>;
401
402 /// Specialization of nda::get_layout_info for nda::expr_unary types.
403 template <char OP, Array A>
404 inline constexpr layout_info_t get_layout_info<expr_unary<OP, A>> = get_layout_info<A>;
405
406 /// Specialization of nda::get_algebra for nda::expr types.
407 template <char OP, typename L, typename R>
408 inline constexpr char get_algebra<expr<OP, L, R>> = expr<OP, L, R>::algebra;
409
410 /// Specialization of nda::get_layout_info for nda::expr types.
411 template <char OP, typename L, typename R>
412 inline constexpr layout_info_t get_layout_info<expr<OP, L, R>> = expr<OP, L, R>::compute_layout_info();
413
414 /** @} */
415
416} // namespace nda
void swap(nda::basic_array_view< V1, R1, LP1, A1, AP1, OP1 > &a, nda::basic_array_view< V2, R2, LP2, A2, AP2, OP2 > &b)=delete
std::swap is deleted for nda::basic_array_view.
Iterator for nda::basic_array and nda::basic_array_view types.
A generic view of a multi-dimensional array.
basic_array_view(A &&a) noexcept
Generic constructor from any nda::MemoryArray type.
const_iterator end() const noexcept
Get a const iterator to the end of the view/array.
ValueType const * data() const noexcept
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view...
static constexpr bool is_stride_order_C() noexcept
Is the stride order of the view/array in C-order?
iterator begin() noexcept
Get an iterator to the beginning of the view/array.
ValueType * data() noexcept
Get a pointer to the actual data (in general this is not the beginning of thr memory block for a view...
basic_array_view & operator=(RHS const &rhs) noexcept
Assignment operator makes a deep copy of the contents of an nda::ArrayOfRank object.
auto & operator/=(RHS const &rhs) noexcept
Division assignment operator.
static constexpr int rank
Number of dimensions of the view.
decltype(auto) operator[](T const &idx) const &noexcept(has_no_boundcheck)
Subscript operator to access the 1-dimensional view/array.
auto const & shape() const noexcept
Get the shape of the view/array.
static __inline__ decltype(auto) call(Self &&self, Ts const &...idxs) noexcept(has_no_boundcheck)
Implementation of the function call operator.
basic_array_view(layout_t const &idxm, ValueType *p) noexcept
Construct a view from a bare pointer to some contiguous data and a memory layout.
__inline__ decltype(auto) operator()(Ts const &...idxs) &noexcept(has_no_boundcheck)
Non-const overload of nda::basic_array_view::operator()(Ts const &...) const &.
decltype(auto) operator[](T const &x) &&noexcept(has_no_boundcheck)
Rvalue overload of nda::basic_array_view::operator[](T const &) const &.
basic_array_view(R &rg) noexcept
Construct a 1-dimensional view of a general contiguous range.
basic_array_view & operator=(basic_array_view const &rhs) noexcept
Copy assignment operator makes a deep copy of the contents of the view.
basic_array_view(std::array< ValueType, N > &a) noexcept
Construct a 1-dimensional view of a std::array.
auto const & strides() const noexcept
Get the strides of the view/array (see nda::idx_map for more details on how we define strides).
long shape(int i) const noexcept
storage_t storage() &&noexcept
Get the data storage of the view/array.
decltype(auto) operator[](T const &x) &noexcept(has_no_boundcheck)
Non-const overload of nda::basic_array_view::operator[](T const &) const &.
decltype(auto) operator()(_linear_index_t idx) noexcept
Non-const overload of nda::basic_array_view::operator()(_linear_index_t) const.
auto & operator+=(RHS const &rhs) noexcept
Addition assignment operator.
auto indices() const noexcept
Get a range that generates all valid index tuples.
basic_array_view()=default
Default constructor constructs an empty view with a default constructed memory handle and layout.
long is_contiguous() const noexcept
Is the memory layout of the view/array contiguous?
auto & operator=(R const &rhs) noexcept
Assignment operator makes a deep copy of a general contiguous range and assigns it to the 1-dimension...
storage_t & storage() &noexcept
Get the data storage of the view/array.
static constexpr bool is_stride_order_Fortran() noexcept
Is the stride order of the view/array in Fortran-order?
decltype(auto) operator()(_linear_index_t idx) const noexcept
Access the element of the view/array at the given nda::_linear_index_t.
__inline__ decltype(auto) operator()(Ts const &...idxs) const &noexcept(has_no_boundcheck)
Function call operator to access the view/array.
auto & operator*=(RHS const &rhs) noexcept
Multiplication assignment operator.
basic_array_view(std::array< std::remove_const_t< ValueType >, N > const &a) noexcept
Construct a 1-dimensional view of a std::array.
friend void deep_swap(basic_array_view a, basic_array_view b) noexcept
Swap two views by swapping their data.
iterator end() noexcept
Get an iterator to the end of the view/array.
long size() const noexcept
Get the total size of the view/array.
auto & operator-=(RHS const &rhs) noexcept
Subtraction assignment operator.
bool empty() const
Is the view/array empty?
basic_array_view(std::array< long, Rank > const &shape, ValueType *p) noexcept
Construct a view from a bare pointer to some contiguous data and a shape.
bool is_empty() const noexcept
friend void swap(basic_array_view &a, basic_array_view &b) noexcept
Swap two views by swapping their memory handles and layouts.
constexpr auto stride_order() const noexcept
Get the stride order of the memory layout of the view/array (see nda::idx_map for more details on how...
const_iterator cbegin() const noexcept
Get a const iterator to the beginning of the view/array.
basic_array_view(layout_t const &idxm, storage_t st)
Construct a view from a given layout and memory handle.
static constexpr int iterator_rank
Rank of the nda::array_iterator for the view/array.
basic_array_view(basic_array_view &&)=default
Default move constructor moves the memory handle and layout.
void rebind(basic_array_view< T, R, LP, A, AP, OP > v) noexcept
Rebind the current view to another view.
basic_array_view(basic_array_view const &)=default
Default copy constructor copies the memory handle and layout.
__inline__ decltype(auto) operator()(Ts const &...idxs) &&noexcept(has_no_boundcheck)
Rvalue overload of nda::basic_array_view::operator()(Ts const &...) const &.
long extent(int i) const noexcept
Get the extent of the ith dimension.
constexpr auto const & indexmap() const noexcept
Get the memory layout of the view/array.
const_iterator begin() const noexcept
Get a const iterator to the beginning of the view/array.
const_iterator cend() const noexcept
Get a const iterator to the end of the view/array.
storage_t const & storage() const &noexcept
Get the data storage of the view/array.
A generic multi-dimensional array.
auto & operator+=(RHS const &rhs) noexcept
Addition assignment operator.
decltype(auto) operator[](T const &idx) const &noexcept(has_no_boundcheck)
Subscript operator to access the 1-dimensional view/array.
basic_array(basic_array< ValueType, 2, LayoutPolicy, A2, ContainerPolicy > &&a) noexcept
Construct a 2-dimensional array from another 2-dimensional array with a different algebra.
basic_array & operator=(RHS const &rhs)
Assignment operator makes a deep copy of an nda::ArrayOfRank object.
static constexpr bool is_stride_order_Fortran() noexcept
Is the stride order of the view/array in Fortran-order?
ValueType const * data() const noexcept
Get a pointer to the actual data (in general this is not the beginning of the memory block for a view...
ValueType * data() noexcept
Get a pointer to the actual data (in general this is not the beginning of thr memory block for a view...
long shape(int i) const noexcept
const_iterator cbegin() const noexcept
Get a const iterator to the beginning of the view/array.
static constexpr int rank
Number of dimensions of the array.
static constexpr bool is_stride_order_C() noexcept
Is the stride order of the view/array in C-order?
storage_t & storage() &noexcept
Get the data storage of the view/array.
basic_array(basic_array< ValueType, Rank, LayoutPolicy, A, CP > a) noexcept
Construct an array from another array with a different algebra and/or container policy.
auto const & strides() const noexcept
Get the strides of the view/array (see nda::idx_map for more details on how we define strides).
basic_array(Ints... is)
Construct an array with the given dimensions.
static basic_array ones(Ints... is)
Make a one-initialized array with the given dimensions.
const_iterator begin() const noexcept
Get a const iterator to the beginning of the view/array.
auto as_array_view() const
Convert the current array to a view with an 'A' (array) algebra.
long extent(int i) const noexcept
Get the extent of the ith dimension.
basic_array(std::initializer_list< ValueType > const &l)
Construct a 1-dimensional array from an initializer list.
decltype(auto) operator[](T const &x) &&noexcept(has_no_boundcheck)
Rvalue overload of nda::basic_array_view::operator[](T const &) const &.
long is_contiguous() const noexcept
Is the memory layout of the view/array contiguous?
basic_array(std::initializer_list< std::initializer_list< ValueType > > const &l2)
Construct a 2-dimensional array from a double nested initializer list.
decltype(auto) operator()(_linear_index_t idx) noexcept
Non-const overload of nda::basic_array_view::operator()(_linear_index_t) const.
__inline__ decltype(auto) operator()(Ts const &...idxs) &noexcept(has_no_boundcheck)
Non-const overload of nda::basic_array_view::operator()(Ts const &...) const &.
bool empty() const
Is the view/array empty?
void resize(std::array< long, Rank > const &shape)
Resize the array to a new shape.
basic_array(Int sz, RHS const &val)
Construct a 1-dimensional array with the given size and initialize each element to the given scalar v...
static basic_array ones(std::array< Int, Rank > const &shape)
Make a one-initialized array with the given shape.
constexpr auto stride_order() const noexcept
Get the stride order of the memory layout of the view/array (see nda::idx_map for more details on how...
auto & operator/=(RHS const &rhs) noexcept
Division assignment operator.
auto as_array_view()
Convert the current array to a view with an 'A' (array) algebra.
basic_array(A const &a)
Construct an array from an nda::ArrayOfRank object with the same rank by copying each element.
static basic_array zeros(Ints... is)
Make a zero-initialized array with the given dimensions.
auto indices() const noexcept
Get a range that generates all valid index tuples.
static __inline__ decltype(auto) call(Self &&self, Ts const &...idxs) noexcept(has_no_boundcheck)
Implementation of the function call operator.
void resize(Ints const &...is)
Resize the array to a new shape.
storage_t storage() &&noexcept
Get the data storage of the view/array.
basic_array()
Default constructor constructs an empty array with a default constructed memory handle and layout.
bool is_empty() const noexcept
auto & operator=(R const &rhs) noexcept
Assignment operator makes a deep copy of a general contiguous range and assigns it to the 1-dimension...
storage_t const & storage() const &noexcept
Get the data storage of the view/array.
basic_array & operator=(basic_array const &)=default
Default copy assignment copies the memory handle and layout from the right hand side array.
basic_array & operator=(basic_array< ValueType, Rank, LayoutPolicy, A, CP > const &rhs)
Assignment operator makes a deep copy of another array with a different algebra and/or container poli...
basic_array(basic_array const &a)=default
Default copy constructor copies the memory handle and layout.
basic_array(std::initializer_list< std::initializer_list< std::initializer_list< ValueType > > > const &l3)
Construct a 3-dimensional array from a triple nested initializer list.
iterator begin() noexcept
Get an iterator to the beginning of the view/array.
basic_array(layout_t const &layout)
Construct an array with the given memory layout.
static basic_array rand(Ints... is)
Make a random-initialized array with the given dimensions.
basic_array(layout_t const &layout, storage_t &&storage) noexcept
Construct an array with the given memory layout and with an existing memory handle/storage.
static constexpr int iterator_rank
Rank of the nda::array_iterator for the view/array.
auto & operator-=(RHS const &rhs) noexcept
Subtraction assignment operator.
const_iterator end() const noexcept
Get a const iterator to the end of the view/array.
basic_array(basic_array &&)=default
Default move constructor moves the memory handle and layout.
__inline__ decltype(auto) operator()(Ts const &...idxs) &&noexcept(has_no_boundcheck)
Rvalue overload of nda::basic_array_view::operator()(Ts const &...) const &.
basic_array(std::array< Int, Rank > const &shape)
Construct an array with the given shape.
const_iterator cend() const noexcept
Get a const iterator to the end of the view/array.
auto const & shape() const noexcept
Get the shape of the view/array.
long size() const noexcept
Get the total size of the view/array.
decltype(auto) operator[](T const &x) &noexcept(has_no_boundcheck)
Non-const overload of nda::basic_array_view::operator[](T const &) const &.
auto & operator*=(RHS const &rhs) noexcept
Multiplication assignment operator.
basic_array & operator=(basic_array &&)=default
Default move assignment moves the memory handle and layout from the right hand side array.
static basic_array zeros(std::array< Int, Rank > const &shape)
Make a zero-initialized array with the given shape.
__inline__ decltype(auto) operator()(Ts const &...idxs) const &noexcept(has_no_boundcheck)
Function call operator to access the view/array.
constexpr auto const & indexmap() const noexcept
Get the memory layout of the view/array.
iterator end() noexcept
Get an iterator to the end of the view/array.
decltype(auto) operator()(_linear_index_t idx) const noexcept
Access the element of the view/array at the given nda::_linear_index_t.
static basic_array rand(std::array< Int, Rank > const &shape)
Make a random-initialized array with the given shape.
Layout that specifies how to map multi-dimensional indices to a linear/flat index.
Definition idx_map.hpp:103
#define CUSOLVER_CHECK(X, info,...)
#define NDA_RUNTIME_ERROR
auto rand(std::array< Int, Rank > const &shape)
Make an array of the given shape and initialize it with random values from the uniform distribution o...
decltype(auto) make_regular(A &&a)
Make a given object regular.
void resize_or_check_if_view(A &a, std::array< long, A::rank > const &sha)
Resize a given regular array to the given shape or check if a given view as the correct shape.
decltype(auto) to_unified(A &&a)
Convert an nda::MemoryArray to its regular type on unified memory.
auto make_const_view(basic_array< T, R, LP, A, CP > const &a)
Make an nda::basic_array_view with a const value type from a given nda::basic_array.
auto zeros(Ints... is)
Make an array of the given shape on the given address space and zero-initialize it.
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
auto rand(Ints... is)
Make an array of the given dimensions and initialize it with random values from the uniform distribut...
auto arange(long first, long last, long step=1)
Make a 1-dimensional integer array and initialize it with values of a given nda::range.
decltype(auto) to_host(A &&a)
Convert an nda::MemoryArray to its regular type on host memory.
auto make_matrix_view(basic_array_view< T, R, LP, A, AP, OP > const &a)
Make an nda::matrix_view of a given nda::basic_array_view.
auto arange(long last)
Make a 1-dimensional integer array and initialize it with values of a given nda::range with a step si...
auto make_matrix_view(basic_array< T, R, LP, A, CP > const &a)
Make an nda::matrix_view of a given nda::basic_array.
auto ones(Ints... is)
Make an array with the given dimensions and one-initialize it.
auto make_const_view(basic_array_view< T, R, LP, A, AP, OP > const &a)
Make an nda::basic_array_view with a const value type from a given nda::basic_array_view.
decltype(auto) to_device(A &&a)
Convert an nda::MemoryArray to its regular type on device memory.
auto make_array_view(basic_array< T, R, LP, A, CP > const &a)
Make an nda::array_view of a given nda::basic_array.
auto make_array_const_view(basic_array< T, R, LP, A, CP > const &a)
Make an nda::array_const_view of a given nda::basic_array.
auto ones(std::array< Int, Rank > const &shape)
Make an array of the given shape and one-initialize it.
auto make_array_const_view(basic_array_view< T, R, LP, A, AP, OP > const &a)
Make an nda::array_const_view of a given nda::basic_array_view.
auto make_array_view(basic_array_view< T, R, LP, A, AP, OP > const &a)
Make an nda::array_view of a given nda::basic_array_view.
auto concatenate(A0 const &a0, As const &...as)
Join a sequence of nda::Array types along an existing axis.
long first_dim(A const &a)
Get the extent of the first dimension of the array.
constexpr bool is_view_v< basic_array_view< ValueType, Rank, Layout, Algebra, AccessorPolicy, OwningPolicy > >
Specialization of nda::is_view_v for nda::basic_array_view.
constexpr bool is_regular_v< basic_array< ValueType, Rank, Layout, Algebra, ContainerPolicy > >
Specialization of nda::is_regular_v for nda::basic_array.
constexpr char get_algebra< basic_array_view< ValueType, Rank, Layout, Algebra, AccessorPolicy, OwningPolicy > >
Specialization of nda::get_algebra for nda::basic_array_view types.
constexpr char get_algebra< basic_array< ValueType, Rank, Layout, Algebra, ContainerPolicy > >
Specialization of nda::get_algebra for nda::basic_array types.
constexpr uint64_t static_extents(int i0, Is... is)
Encode the given shape into a single integer using the nda::encode function.
constexpr char get_algebra< expr_unary< OP, A > >
Specialization of nda::get_algebra for nda::expr_unary types.
bool operator==(LHS const &lhs, RHS const &rhs)
Equal-to comparison operator for two nda::Array objects.
long second_dim(A const &a)
Get the extent of the second dimension of the array.
constexpr char get_algebra< expr< OP, L, R > >
Specialization of nda::get_algebra for nda::expr types.
__inline__ void clef_auto_assign(expr< tags::terminal, T > const &ex, RHS &&rhs)
Overload of clef_auto_assign function for terminal expressions.
__inline__ void clef_auto_assign(std::reference_wrapper< T > wrapper, RHS &&rhs)
Overload of clef_auto_assign function for std::reference_wrapper objects.
__inline__ void operator<<(expr< tags::function, F, placeholder< Is >... > const &ex, RHS &&rhs)
Assign values to the underlying object of a lazy function call expression.
__inline__ void clef_auto_assign_subscript(std::reference_wrapper< T > wrapper, RHS &&rhs)
Overload of clef_auto_assign_subscript function for std::reference_wrapper objects.
__inline__ void clef_auto_assign_subscript(expr< Tag, Childs... > const &ex, RHS const &rhs)
Overload of clef_auto_assign_subscript function for generic expressions.
__inline__ void clef_auto_assign_subscript(expr< tags::terminal, T > const &ex, RHS &&rhs)
Overload of clef_auto_assign_subscript function for terminal expressions.
__inline__ void clef_auto_assign(expr< Tag, Childs... > const &ex, RHS const &rhs)
Overload of clef_auto_assign function for generic expressions.
void clef_auto_assign(A &&a, F &&f)
Overload of nda::clef::clef_auto_assign function for nda::Array objects.
__inline__ void operator<<(expr< tags::subscript, T, placeholder< Is >... > const &ex, RHS &&rhs)
Assign values to the underlying object of a lazy subscript expression.
__inline__ decltype(auto) eval(T const &obj, Pairs &&...pairs)
Generic function to evaluate expressions and other types.
Definition eval.hpp:197
__inline__ auto make_function(T &&obj, Phs...)
Factory function for nda::clef::make_fun_impl objects.
Definition function.hpp:100
__inline__ auto if_else(C &&c, A &&a, B &&b)
Create a lazy ternary (if-else) expression.
#define CLEF_OPERATION(TAG, OP)
auto make_expr(T &&t)
Create a terminal expression node of an object.
Definition make_lazy.hpp:45
__inline__ auto op_dispatch(std::true_type, Args &&...args)
Dispatch operations containing at least one lazy operand.
__inline__ decltype(auto) op_dispatch(std::false_type, Args &&...args)
Dispatch operations containing only non-lazy operands.
auto make_expr_subscript(T &&t, Args &&...args)
Create a subscript expression from an object and a list of arguments.
Definition make_lazy.hpp:93
auto make_expr_call(F &&f, Args &&...args)
Create a function call expression from a callable object and a list of arguments.
Definition make_lazy.hpp:74
auto make_expr_from_clone(T &&t)
Create a terminal expression node of an object.
Definition make_lazy.hpp:57
constexpr bool is_clef_expression
Alias template for nda::clef::is_any_lazy.
Definition utils.hpp:161
constexpr bool is_lazy
Constexpr variable that is true if the type T is a lazy type.
Definition utils.hpp:153
constexpr bool force_copy_in_expr
Constexpr variable that is true if objects of type T should be forced to be copied into an expression...
Definition utils.hpp:137
constexpr bool is_function
Constexpr variable that is true if the type T is an nda::clef::make_fun_impl type.
Definition utils.hpp:165
constexpr bool is_any_lazy
Constexpr variable that is true if any of the given types is lazy.
Definition utils.hpp:157
auto get_block_layout(A const &a)
Check if a given nda::MemoryArray has a block-strided layout.
layout_prop_e
Compile-time guarantees of the memory layout of an array/view.
Definition traits.hpp:222
AddressSpace
Enum providing identifiers for the different memory address spaces.
static constexpr do_not_initialize_t do_not_initialize
Instance of nda::mem::do_not_initialize_t.
Definition handle.hpp:69
static constexpr init_zero_t init_zero
Instance of nda::mem::init_zero_t.
Definition handle.hpp:75
constexpr uint64_t encode(std::array< int, N > const &a)
Encode a std::array<int, N> in a uint64_t.
#define EXPECTS(X)
Definition macros.hpp:59
#define FORCEINLINE
Definition macros.hpp:41
#define EXPECTS_WITH_MESSAGE(X,...)
Definition macros.hpp:75
#define AS_STRING(...)
Definition macros.hpp:31
#define ASSERT(X)
Definition macros.hpp:64
Contiguous layout policy with C-order (row-major order).
Definition policies.hpp:47
Strided (non-contiguous) layout policy with C-order (row-major order).
Definition policies.hpp:79
A small wrapper around a single long integer to be used as a linear index.
Definition traits.hpp:343
long value
Linear index.
Definition traits.hpp:345
Generic layout policy with arbitrary order.
Definition policies.hpp:115
Memory policy using an nda::mem::handle_borrowed.
Definition policies.hpp:108
static constexpr bool is_lazy
Constexpr variable that is true if any of the evaluators of the child nodes is lazy.
Definition eval.hpp:150
__inline__ decltype(auto) operator()(expr< Tag, Childs... > const &ex, Pairs &...pairs) const
Evaluate the given expression by applying the given nda::clef::pair objects.
Definition eval.hpp:170
__inline__ decltype(auto) operator()(make_fun_impl< T, Is... > const &f, Pairs &...pairs) const
Evaluate the nda::clef::make_fun_impl object.
Definition function.hpp:133
static constexpr bool is_lazy
Constexpr variable that is true if all the placeholders are assigned a value.
Definition function.hpp:120
static constexpr bool is_lazy
Constexpr variable that is true if the there is no nda::clef::pair containing an nda::clef::placehold...
Definition eval.hpp:92
__inline__ decltype(auto) operator()(placeholder< N >, pair< Is, Ts > &...pairs) const
Evaluate the placeholder.
Definition eval.hpp:101
static constexpr bool is_lazy
Constexpr variable that is always false.
Definition eval.hpp:126
__inline__ decltype(auto) operator()(std::reference_wrapper< T > const &wrapper, Pairs const &...pairs) const
Evaluate the std::reference_wrapper by redirecting the evaluation to the object contained in the wrap...
Definition eval.hpp:135
Generic evaluator for types which do not have a specialized evaluator.
Definition eval.hpp:55
__inline__ T const & operator()(T const &t, Pairs &...) const
Evaluate the object and ignore all given nda::clef::pair objects.
Definition eval.hpp:65
static constexpr bool is_lazy
Constexpr variable that is true if the type T is lazy.
Definition eval.hpp:57
Single node of the expression tree.
expr & operator=(expr const &)=delete
Copy assignment operator is deleted.
expr(expr &&ex) noexcept
Move constructor simply moves the child nodes from the source expression.
auto operator[](Args &&...args) const
Subscript operator.
expr & operator=(expr &&)=default
Default move assignment operator.
expr(expr const &)=default
Default copy constructor.
expr(Tag, Us &&...us)
Construct an expression node with a given tag and child nodes.
childs_t childs
Child nodes of the current expression node.
auto operator()(Args &&...args) const
Function call operator.
Helper struct to simplify calls to nda::clef::eval.
Definition function.hpp:52
T obj
Object to be evaluated.
Definition function.hpp:54
__inline__ decltype(auto) operator()(Args &&...args) const
Function call operator.
Definition function.hpp:68
__inline__ decltype(auto) operator()(F &&f, Args &&...args) const
Perform a function call operation.
Definition operation.hpp:95
__inline__ A operator()(C const &c, A const &a, B const &b) const
Perform a ternary (if-else) operation.
__inline__ decltype(auto) operator()(F &&f, Args &&...args) const
Perform a subscript operation.
__inline__ L operator()(L &&l) const
Perform a terminal operation.
Definition operation.hpp:77
A pair consisting of a placeholder and its assigned value.
T rhs
Value assigned to the placeholder (can be an lvalue reference).
static constexpr int p
Integer label of the placeholder.
A placeholder is an empty struct, labelled by an int.
static constexpr int index
Integer label.
auto operator[](T &&t) const
Subscript operator.
pair< N, RHS > operator=(RHS &&rhs) const
Assign a value to the placeholder.
auto operator()(Args &&...args) const
Function call operator.
Tag for binary operator expressions.
Tag for function call expressions.
Tag for conditional expressions.
Tag for subscript expressions.
Tag to indicate a terminal node in the expression tree.
Tag for unary operator expressions.
Accessor type of the nda::default_accessor.
Definition accessors.hpp:42
static __inline__ T * offset(pointer p, std::ptrdiff_t i) noexcept
Offset the pointer by a certain number of elements.
Definition accessors.hpp:71
static __inline__ reference access(pointer p, std::ptrdiff_t i) noexcept
Access a specific element of the data.
Definition accessors.hpp:59
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.
Stores information about the memory layout and the stride order of an array/view.
Definition traits.hpp:295
Tag used in constructors to indicate that the memory should be initialized to zero.
Definition handle.hpp:72
Accessor type of the nda::no_alias_accessor.
Definition accessors.hpp:82
static __inline__ reference access(pointer p, std::ptrdiff_t i) noexcept
Access a specific element of the data.
Definition accessors.hpp:99
static __inline__ T * offset(pointer p, std::ptrdiff_t i) noexcept
Offset the pointer by a certain number of elements.
Accessor for array and view types with no aliasing.
Definition accessors.hpp:76
Memory policy using an nda::mem::handle_stack.
Definition policies.hpp:84