TRIQS/nda 2.0.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
basic_array.hpp
Go to the documentation of this file.
1// Copyright (c) 2018--present, The Simons Foundation
2// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
3// SPDX-License-Identifier: Apache-2.0
4// See LICENSE in the root of this distribution for details.
5
10
11#pragma once
12
13#include "./accessors.hpp"
15#include "./basic_functions.hpp"
16#include "./concepts.hpp"
17#include "./iterators.hpp"
18#include "./layout/for_each.hpp"
20#include "./layout/range.hpp"
23#include "./macros.hpp"
26#include "./mem/fill.hpp"
27#include "./mem/memcpy.hpp"
28#include "./mem/policies.hpp"
29#include "./stdutil/array.hpp"
30#include "./traits.hpp"
31
32#include <algorithm>
33#include <array>
34#include <complex>
35#include <concepts>
36#include <exception>
37#include <initializer_list>
38#include <iostream>
39#include <random>
40#include <ranges>
41#include <type_traits>
42#include <utility>
43
44namespace nda {
45
90 template <typename ValueType, int Rank, typename LayoutPolicy, char Algebra, typename ContainerPolicy>
91 class basic_array {
92 // Compile-time checks.
93 static_assert(!std::is_const_v<ValueType>, "Error in nda::basic_array: ValueType cannot be const");
94 static_assert((Algebra != 'N'), "Internal error in nda::basic_array: Algebra 'N' not supported");
95 static_assert((Algebra != 'M') or (Rank == 2), "Internal error in nda::basic_array: Algebra 'M' requires a rank 2 array");
96 static_assert((Algebra != 'V') or (Rank == 1), "Internal error in nda::basic_array: Algebra 'V' requires a rank 1 array");
97
98 public:
100 using value_type = ValueType;
101
103 using layout_policy_t = LayoutPolicy;
104
106 using layout_t = typename LayoutPolicy::template mapping<Rank>;
107
109 using container_policy_t = ContainerPolicy;
110
112 using storage_t = typename ContainerPolicy::template handle<ValueType>;
113
115 using regular_type = basic_array;
116
118 static constexpr int rank = Rank;
119
120 // Compile-time check.
121 static_assert(has_contiguous(layout_t::layout_prop), "Error in nda::basic_array: Memory layout has to be contiguous");
122
123 private:
124 // Type of the array itself.
125 using self_t = basic_array;
126
127 // Type of the accessor policy for views (no no_alias_accessor).
128 using AccessorPolicy = default_accessor;
129
130 // Type of the owning policy for views.
131 using OwningPolicy = borrowed<storage_t::address_space>;
132
133 // Constexpr variable that is true if the value_type is const (never for basic_array).
134 static constexpr bool is_const = false;
135
136 // Constexpr variable that is true if the array is a view (never for basic_array).
137 static constexpr bool is_view = false;
138
139 // Memory layout of the array, i.e. the nda::idx_map.
140 layout_t lay;
141
142 // Memory handle of the array.
143 storage_t sto;
144
145 // Construct an array with a given shape and initialize the memory with zeros.
146 template <std::integral Int = long>
147 basic_array(std::array<Int, Rank> const &shape, mem::init_zero_t) : lay{shape}, sto{lay.size(), mem::init_zero} {}
148
149 public:
154 auto as_array_view() { return basic_array_view<ValueType, Rank, LayoutPolicy, 'A', AccessorPolicy, OwningPolicy>{*this}; };
155
160 auto as_array_view() const { return basic_array_view<const ValueType, Rank, LayoutPolicy, 'A', AccessorPolicy, OwningPolicy>{*this}; };
161
163 [[deprecated]] auto transpose()
164 requires(Rank == 2)
165 {
166 return permuted_indices_view<encode(std::array<int, 2>{1, 0})>(*this);
167 }
168
170 [[deprecated]] auto transpose() const
171 requires(Rank == 2)
172 {
173 return permuted_indices_view<encode(std::array<int, 2>{1, 0})>(*this);
174 }
175
177 basic_array() {}; // NOLINT (user-defined constructor to avoid value initialization of the sso buffer)
178
180 basic_array(basic_array &&) = default;
181
183 explicit basic_array(basic_array const &a) = default;
184
194 template <char A, typename CP>
195 explicit basic_array(basic_array<ValueType, Rank, LayoutPolicy, A, CP> a) noexcept : lay(a.indexmap()), sto(std::move(a.storage())) {}
196
206 template <std::integral... Ints>
207 requires(sizeof...(Ints) == Rank)
208 explicit basic_array(Ints... is) {
209 // setting the layout and storage in the constructor body improves error messages for wrong # of args
210 lay = layout_t{std::array{long(is)...}}; // NOLINT (for better error messages)
211 sto = storage_t{lay.size()}; // NOLINT (for better error messages)
212 }
213
222 template <std::integral Int, typename RHS>
223 explicit basic_array(Int sz, RHS const &val)
224 requires((Rank == 1 and is_scalar_for_v<RHS, basic_array>))
225 : lay(layout_t{std::array{long(sz)}}), sto{lay.size()} {
226 assign_from_scalar(val);
227 }
228
237 template <std::integral Int = long>
238 explicit basic_array(std::array<Int, Rank> const &shape)
239 requires(std::is_default_constructible_v<ValueType>)
240 : lay(shape), sto(lay.size()) {}
241
249 explicit basic_array(layout_t const &layout)
250 requires(std::is_default_constructible_v<ValueType>)
251 : lay{layout}, sto{lay.size()} {}
252
261 explicit basic_array(layout_t const &layout, storage_t &&storage) noexcept : lay{layout}, sto{std::move(storage)} {}
262
269 template <ArrayOfRank<Rank> A>
270 requires(HasValueTypeConstructibleFrom<A, ValueType>)
271 basic_array(A const &a) : lay(a.shape()), sto{lay.size(), mem::do_not_initialize} {
272 static_assert(std::is_constructible_v<ValueType, get_value_t<A>>, "Error in nda::basic_array: Incompatible value types in constructor");
273 if constexpr (std::is_trivial_v<ValueType> or is_complex_v<ValueType>) {
274 // trivial and complex value types can use the optimized assign_from_ndarray
275 if constexpr (std::is_same_v<ValueType, get_value_t<A>>)
276 assign_from_ndarray(a);
277 else
278 assign_from_ndarray(nda::map([](auto const &val) { return ValueType(val); })(a));
279 } else {
280 // general value types may not be default constructible -> use placement new
281 nda::for_each(lay.lengths(), [&](auto const &...is) { new (sto.data() + lay(is...)) ValueType{a(is...)}; });
282 }
283 }
284
294 template <ArrayInitializer<basic_array> Initializer> // can not be explicit
295 basic_array(Initializer const &initializer) : basic_array{initializer.shape()} {
296 initializer.invoke(*this);
297 }
298
299 private:
300 // Get the corresponding shape from an initializer list.
301 static std::array<long, 1> shape_from_init_list(std::initializer_list<ValueType> const &l) noexcept { return {long(l.size())}; }
302
303 // Get the corresponding shape from a nested initializer list.
304 template <typename L>
305 static auto shape_from_init_list(std::initializer_list<L> const &l) noexcept {
306 const auto [min, max] = std::minmax_element(std::begin(l), std::end(l), [](auto &&x, auto &&y) { return x.size() < y.size(); });
307 EXPECTS_WITH_MESSAGE(min->size() == max->size(),
308 "Error in nda::basic_array: Arrays can only be initialized with rectangular initializer lists");
309 return stdutil::front_append(shape_from_init_list(*max), long(l.size()));
310 }
311
312 public:
317 basic_array(std::initializer_list<ValueType> const &l)
318 requires(Rank == 1)
319 : lay(std::array<long, 1>{long(l.size())}), sto{lay.size(), mem::do_not_initialize} {
320 long i = 0;
321 for (auto const &x : l) { new (sto.data() + lay(i++)) ValueType{x}; }
322 }
323
328 basic_array(std::initializer_list<std::initializer_list<ValueType>> const &l2)
329 requires(Rank == 2)
330 : lay(shape_from_init_list(l2)), sto{lay.size(), mem::do_not_initialize} {
331 long i = 0, j = 0;
332 for (auto const &l1 : l2) {
333 for (auto const &x : l1) { new (sto.data() + lay(i, j++)) ValueType{x}; }
334 j = 0;
335 ++i;
336 }
337 }
338
343 basic_array(std::initializer_list<std::initializer_list<std::initializer_list<ValueType>>> const &l3)
344 requires(Rank == 3)
345 : lay(shape_from_init_list(l3)), sto{lay.size(), mem::do_not_initialize} {
346 long i = 0, j = 0, k = 0;
347 for (auto const &l2 : l3) {
348 for (auto const &l1 : l2) {
349 for (auto const &x : l1) { new (sto.data() + lay(i, j, k++)) ValueType{x}; }
350 k = 0;
351 ++j;
352 }
353 j = 0;
354 ++i;
355 }
356 }
357
367 template <char A2>
368 explicit basic_array(basic_array<ValueType, 2, LayoutPolicy, A2, ContainerPolicy> &&a) noexcept
369 requires(Rank == 2)
370 : basic_array{a.indexmap(), std::move(a).storage()} {}
371
379 template <std::integral Int = long>
380 static basic_array zeros(std::array<Int, Rank> const &shape)
381 requires(std::is_standard_layout_v<ValueType> && std::is_trivially_copyable_v<ValueType>)
382 {
384 }
385
393 template <std::integral... Ints>
394 static basic_array zeros(Ints... is)
395 requires(sizeof...(Ints) == Rank)
396 {
397 return zeros(std::array<long, Rank>{is...});
398 }
399
407 template <std::integral Int = long>
408 static basic_array ones(std::array<Int, Rank> const &shape)
410 {
411 auto res = basic_array{stdutil::make_std_array<long>(shape)};
412 res() = ValueType{1};
413 return res;
414 }
415
423 template <std::integral... Ints>
424 static basic_array ones(Ints... is)
425 requires(sizeof...(Ints) == Rank)
426 {
427 return ones(std::array<long, Rank>{is...});
428 }
429
440 template <std::integral Int = long>
441 static basic_array rand(std::array<Int, Rank> const &shape)
442 requires(std::is_floating_point_v<ValueType> or nda::is_complex_v<ValueType>)
443 {
444 auto static gen = std::mt19937_64{};
445 auto res = basic_array{shape};
446 if constexpr (nda::is_complex_v<ValueType>) {
447 auto static dist = std::uniform_real_distribution<typename ValueType::value_type>(0.0, 1.0);
448 using namespace std::complex_literals;
449 for (auto &x : res) x = dist(gen) + 1i * dist(gen);
450 } else {
451 auto static dist = std::uniform_real_distribution<ValueType>(0.0, 1.0);
452 for (auto &x : res) x = dist(gen);
453 }
454 return res;
455 }
456
467 template <std::integral... Ints>
468 static basic_array rand(Ints... is)
469 requires(sizeof...(Ints) == Rank)
470 {
471 return rand(std::array<long, Rank>{is...});
472 }
473
475 basic_array &operator=(basic_array &&) = default;
476
478 basic_array &operator=(basic_array const &) = default;
479
490 template <char A, typename CP>
491 basic_array &operator=(basic_array<ValueType, Rank, LayoutPolicy, A, CP> const &rhs) {
492 *this = basic_array{rhs};
493 return *this;
494 }
495
505 template <ArrayOfRank<Rank> RHS>
506 basic_array &operator=(RHS const &rhs) {
507 resize(rhs.shape());
508 assign_from_ndarray(rhs);
509 return *this;
510 }
511
522 template <typename RHS>
523 basic_array &operator=(RHS const &rhs) noexcept
525 {
526 assign_from_scalar(rhs);
527 return *this;
528 }
529
539 template <ArrayInitializer<basic_array> Initializer>
540 basic_array &operator=(Initializer const &initializer) {
541 resize(initializer.shape());
542 initializer.invoke(*this);
543 return *this;
544 }
545
556 template <std::integral... Ints>
557 void resize(Ints const &...is) {
558 static_assert(std::is_copy_constructible_v<ValueType>, "Error in nda::basic_array: Resizing requires the value_type to be copy constructible");
559 static_assert(sizeof...(is) == Rank, "Error in nda::basic_array: Resizing requires exactly Rank arguments");
560 resize(std::array<long, Rank>{long(is)...});
561 }
562
572 [[gnu::noinline]] void resize(std::array<long, Rank> const &shape) {
573 lay = layout_t(shape);
574 if (sto.is_null() or (sto.size() != lay.size())) sto = storage_t{lay.size()};
575 }
576
577// include common functionality of arrays and views
578// Copyright (c) 2019--present, The Simons Foundation
579// This file is part of TRIQS/nda and is licensed under the Apache License, Version 2.0.
580// SPDX-License-Identifier: Apache-2.0
581// See LICENSE in the root of this distribution for details.
582
587[[nodiscard]] constexpr auto const &indexmap() const noexcept { return lay; }
588
593[[nodiscard]] storage_t const &storage() const & noexcept { return sto; }
594
599[[nodiscard]] storage_t &storage() & noexcept { return sto; }
600
605[[nodiscard]] storage_t storage() && noexcept { return std::move(sto); }
606
613[[nodiscard]] constexpr auto stride_order() const noexcept { return lay.stride_order; }
614
619[[nodiscard]] ValueType const *data() const noexcept { return sto.data(); }
620
625[[nodiscard]] ValueType *data() noexcept { return sto.data(); }
626
631[[nodiscard]] auto const &shape() const noexcept { return lay.lengths(); }
632
637[[nodiscard]] auto const &strides() const noexcept { return lay.strides(); }
638
643[[nodiscard]] long size() const noexcept { return lay.size(); }
644
649[[nodiscard]] long is_contiguous() const noexcept { return lay.is_contiguous(); }
650
655[[nodiscard]] long has_positive_strides() const noexcept { return lay.has_positive_strides(); }
656
661[[nodiscard]] bool empty() const { return sto.is_null(); }
662
664[[nodiscard]] bool is_empty() const noexcept { return sto.is_null(); }
665
670[[nodiscard]] long extent(int i) const noexcept {
671#ifdef NDA_ENFORCE_BOUNDCHECK
672 if (i < 0 || i >= rank) {
673 std::cerr << "Error in extent: Dimension " << i << " is incompatible with array of rank " << rank << std::endl;
674 std::terminate();
675 }
676#endif
677 return lay.lengths()[i];
678}
679
681[[nodiscard]] long shape(int i) const noexcept { return extent(i); }
682
687[[nodiscard]] auto indices() const noexcept { return itertools::product_range(shape()); }
688
693static constexpr bool is_stride_order_C() noexcept { return layout_t::is_stride_order_C(); }
694
699static constexpr bool is_stride_order_Fortran() noexcept { return layout_t::is_stride_order_Fortran(); }
700
710decltype(auto) operator()(_linear_index_t idx) const noexcept {
711 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
712 return sto[idx.value * lay.min_stride()];
713 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
714 return sto[idx.value];
715 else
716 static_assert(always_false<layout_t>, "Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
717}
718
720decltype(auto) operator()(_linear_index_t idx) noexcept {
721 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
722 return sto[idx.value * lay.min_stride()];
723 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
724 return sto[idx.value];
725 else
726 static_assert(always_false<layout_t>, "Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
727}
728
729private:
730// Constexpr variable that is true if bounds checking is disabled.
731#ifdef NDA_ENFORCE_BOUNDCHECK
732static constexpr bool has_no_boundcheck = false;
733#else
734static constexpr bool has_no_boundcheck = true;
735#endif
736
737public:
754template <char ResultAlgebra, bool SelfIsRvalue, typename Self, typename... Ts>
755FORCEINLINE static decltype(auto) call(Self &&self, Ts const &...idxs) noexcept(has_no_boundcheck) {
756 // resulting value type
757 using r_v_t = std::conditional_t<std::is_const_v<std::remove_reference_t<Self>>, ValueType const, ValueType>;
758
759 // behavior depends on the given arguments
760 if constexpr (clef::is_any_lazy<Ts...>) {
761 // if there are lazy arguments, e.g. as in A(i_) << i_, a lazy expression is returned
762 return clef::make_expr_call(std::forward<Self>(self), idxs...);
763 } else if constexpr (sizeof...(Ts) == 0) {
764 // if no arguments are given, a full view is returned
766 } else {
767 // otherwise we check the arguments and either access a single element or make a slice
768 static_assert(((layout_t::template argument_is_allowed_for_call_or_slice<Ts> + ...) > 0),
769 "Error in array/view: Slice arguments must be convertible to range, ellipsis, or long (or string if the layout permits it)");
770
771 // number of arguments convertible to long
772 static constexpr int n_args_long = (layout_t::template argument_is_allowed_for_call<Ts> + ...);
773
774 if constexpr (n_args_long == rank) {
775 // access a single element
776 long offset = self.lay(idxs...);
777 if constexpr (is_view or not SelfIsRvalue) {
778 // if the calling object is a view or an lvalue, we return a reference
779 return AccessorPolicy::template accessor<r_v_t>::access(self.sto.data(), offset);
780 } else {
781 // otherwise, we return a copy of the value
782 return ValueType{self.sto[offset]};
783 }
784 } else {
785 // access a slice of the view/array
786 auto const [offset, idxm] = self.lay.slice(idxs...);
787 static constexpr auto res_rank = decltype(idxm)::rank();
788 // resulting algebra
789 static constexpr char newAlgebra = (ResultAlgebra == 'M' and (res_rank == 1) ? 'V' : ResultAlgebra);
790 // resulting layout policy
791 using r_layout_p = typename detail::layout_to_policy<std::decay_t<decltype(idxm)>>::type;
793 }
794 }
795}
796
797public:
819template <typename... Ts>
820FORCEINLINE decltype(auto) operator()(Ts const &...idxs) const & noexcept(has_no_boundcheck) {
821 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
822 "Error in array/view: Incorrect number of parameters in call operator");
823 return call<Algebra, false>(*this, idxs...);
824}
825
827template <typename... Ts>
828FORCEINLINE decltype(auto) operator()(Ts const &...idxs) & noexcept(has_no_boundcheck) {
829 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
830 "Error in array/view: Incorrect number of parameters in call operator");
831 return call<Algebra, false>(*this, idxs...);
832}
833
835template <typename... Ts>
836FORCEINLINE decltype(auto) operator()(Ts const &...idxs) && noexcept(has_no_boundcheck) {
837 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
838 "Error in array/view: Incorrect number of parameters in call operator");
839 return call<Algebra, true>(*this, idxs...);
840}
841
859template <typename T>
860decltype(auto) operator[](T const &idx) const & noexcept(has_no_boundcheck) {
861 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
862 return call<Algebra, false>(*this, idx);
863}
864
866template <typename T>
867decltype(auto) operator[](T const &x) & noexcept(has_no_boundcheck) {
868 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
869 return call<Algebra, false>(*this, x);
870}
871
873template <typename T>
874decltype(auto) operator[](T const &x) && noexcept(has_no_boundcheck) {
875 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
876 return call<Algebra, true>(*this, x);
877}
878
880static constexpr int iterator_rank = (has_strided_1d(layout_t::layout_prop) ? 1 : Rank);
881
884
887
888private:
889// Make an iterator for the view/array depending on its type.
890template <typename Iterator>
891[[nodiscard]] auto make_iterator(bool at_end) const noexcept {
892 if constexpr (iterator_rank == Rank) {
893 // multi-dimensional iterator
894 if constexpr (layout_t::is_stride_order_C()) {
895 // C-order case (array_iterator already traverses the data in C-order)
896 return Iterator{indexmap().lengths(), indexmap().strides(), sto.data(), at_end};
897 } else {
898 // general case (we need to permute the shape and the strides according to the stride order of the layout)
899 return Iterator{nda::permutations::apply(layout_t::stride_order, indexmap().lengths()),
900 nda::permutations::apply(layout_t::stride_order, indexmap().strides()), sto.data(), at_end};
901 }
902 } else {
903 // 1-dimensional iterator
904 return Iterator{std::array<long, 1>{size()}, std::array<long, 1>{indexmap().min_stride()}, sto.data(), at_end};
905 }
906}
907
908public:
910[[nodiscard]] const_iterator begin() const noexcept { return make_iterator<const_iterator>(false); }
911
913[[nodiscard]] const_iterator cbegin() const noexcept { return make_iterator<const_iterator>(false); }
914
916iterator begin() noexcept { return make_iterator<iterator>(false); }
917
919[[nodiscard]] const_iterator end() const noexcept { return make_iterator<const_iterator>(true); }
920
922[[nodiscard]] const_iterator cend() const noexcept { return make_iterator<const_iterator>(true); }
923
925iterator end() noexcept { return make_iterator<iterator>(true); }
926
939template <typename RHS>
940auto &operator+=(RHS const &rhs) noexcept {
941 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
942 return operator=(*this + rhs);
943}
944
957template <typename RHS>
958auto &operator-=(RHS const &rhs) noexcept {
959 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
960 return operator=(*this - rhs);
961}
962
975template <typename RHS>
976auto &operator*=(RHS const &rhs) noexcept {
977 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
978 return operator=((*this) * rhs);
979}
980
993template <typename RHS>
994auto &operator/=(RHS const &rhs) noexcept {
995 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
996 return operator=(*this / rhs);
997}
998
1007template <std::ranges::contiguous_range R>
1008auto &operator=(R const &rhs) noexcept
1009 requires(Rank == 1 and not MemoryArray<R> and not is_scalar_for_v<R, self_t>)
1010{
1012 return *this;
1013}
1014
1015private:
1016// Implementation of the assignment from an n-dimensional array type.
1017template <typename RHS>
1018void assign_from_ndarray(RHS const &rhs) {
1019#ifdef NDA_ENFORCE_BOUNDCHECK
1020 if (this->shape() != rhs.shape()) {
1021 NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Size mismatch:"
1022 << "\n LHS.shape() = " << this->shape() << "\n RHS.shape() = " << rhs.shape();
1023 }
1024#endif
1025 // compile-time check if assignment is possible
1026 static_assert(std::is_assignable_v<value_type &, get_value_t<RHS>>, "Error in assign_from_ndarray: Incompatible value types");
1027
1028 // are both operands nda::MemoryArray types?
1029 static constexpr bool both_in_memory = MemoryArray<self_t> and MemoryArray<RHS>;
1030
1031 // do both operands have the same stride order?
1032 static constexpr bool same_stride_order = get_layout_info<self_t>.stride_order == get_layout_info<RHS>.stride_order;
1033
1034 // compile-time check for device arrays to avoid runtime errors
1035 static_assert(!(mem::on_device<self_t> or mem::on_device<RHS>) or (both_in_memory and same_stride_order and have_same_value_type_v<self_t, RHS>),
1036 "Error in assign_from_ndarray: Assignment to/from device arrays is not supported for the given types.");
1037
1038 // prefer optimized options if possible
1039 if constexpr (both_in_memory and same_stride_order) {
1040 if (rhs.empty()) return;
1041 // are both operands strided in 1d?
1042 static constexpr bool both_1d_strided = has_layout_strided_1d<self_t> and has_layout_strided_1d<RHS>;
1043 if constexpr (mem::on_host<self_t, RHS> and both_1d_strided) {
1044 // vectorizable copy on host
1045 for (long i = 0; i < size(); ++i) (*this)(_linear_index_t{i}) = rhs(_linear_index_t{i});
1046 return;
1048 // check for block-layout and use mem::memcpy2D if possible
1049 auto bl_layout_dst = get_block_layout(*this);
1050 auto bl_layout_src = get_block_layout(rhs);
1051 if (bl_layout_dst && bl_layout_src) {
1052 auto [n_bl_dst, bl_size_dst, bl_str_dst] = *bl_layout_dst;
1053 auto [n_bl_src, bl_size_src, bl_str_src] = *bl_layout_src;
1054 // check that the total memory size is the same
1055 if (n_bl_dst * bl_size_dst != n_bl_src * bl_size_src) NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Incompatible block sizes";
1056 // if either destination or source consists of a single block, we can chunk it up to make the layouts compatible
1057 if (n_bl_dst == 1 && n_bl_src > 1) {
1058 n_bl_dst = n_bl_src;
1059 bl_size_dst /= n_bl_src;
1060 bl_str_dst = bl_size_dst;
1061 }
1062 if (n_bl_src == 1 && n_bl_dst > 1) {
1063 n_bl_src = n_bl_dst;
1064 bl_size_src /= n_bl_dst;
1065 bl_str_src = bl_size_src;
1066 }
1067 // copy only if block-layouts are compatible, otherwise continue to fallback
1068 if (n_bl_dst == n_bl_src && bl_size_dst == bl_size_src) {
1069 mem::memcpy2D<mem::get_addr_space<self_t>, mem::get_addr_space<RHS>>((void *)data(), bl_str_dst * sizeof(value_type), (void *)rhs.data(),
1070 bl_str_src * sizeof(value_type), bl_size_src * sizeof(value_type),
1071 n_bl_src);
1072 return;
1073 }
1074 }
1075 }
1076 }
1077 // otherwise fallback to elementwise assignment
1079 NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Fallback to elementwise assignment not implemented for arrays/views on the GPU";
1080 }
1081 nda::for_each(shape(), [this, &rhs](auto const &...args) { (*this)(args...) = rhs(args...); });
1082}
1083
1084// Implementation to fill a view/array with a constant scalar value.
1085template <typename Scalar>
1086void fill_with_scalar(Scalar const &scalar) {
1087 if constexpr (mem::on_host<self_t>) {
1088 if constexpr (has_layout_strided_1d<self_t>) {
1089 const long L = size();
1090 auto *__restrict const p = data(); // no alias possible here!
1091 if constexpr (has_contiguous_layout<self_t>) {
1092 for (long i = 0; i < L; ++i) p[i] = scalar;
1093 } else {
1094 const long stri = indexmap().min_stride();
1095 const long Lstri = L * stri;
1096 for (long i = 0; i != Lstri; i += stri) p[i] = scalar;
1097 }
1098 } else {
1099 for (auto &x : *this) x = scalar;
1100 }
1101 } else if constexpr (mem::on_device<self_t> or mem::on_unified<self_t>) {
1102 if constexpr (has_layout_strided_1d<self_t>) {
1103 if constexpr (has_contiguous_layout<self_t>) {
1104 mem::fill_n<mem::get_addr_space<self_t>>(data(), size(), value_type(scalar));
1105 } else {
1106 const long stri = indexmap().min_stride();
1107 mem::fill2D_n<mem::get_addr_space<self_t>>(data(), stri, 1, size(), value_type(scalar));
1108 }
1109 } else {
1110 auto bl_layout = get_block_layout(*this);
1111 if (bl_layout) {
1112 auto [n_bl, bl_size, bl_str] = *bl_layout;
1113 mem::fill2D_n<mem::get_addr_space<self_t>>(data(), bl_str, bl_size, n_bl, value_type(scalar));
1114 } else {
1115 // MAM: implement recursive call to fill_with_scalar on (i,nda::ellipsis{})
1116 NDA_RUNTIME_ERROR << "fill_with_scalar: Not implemented on device for generic (non-blocked) layout.";
1117 }
1118 }
1119 }
1120}
1121
1122// Implementation of the assignment from a scalar value.
1123template <typename Scalar>
1124void assign_from_scalar(Scalar const &scalar) noexcept {
1125 static_assert(!is_const, "Error in assign_from_ndarray: Cannot assign to a const view");
1126 if constexpr (Algebra != 'M') {
1127 // element-wise assignment for non-matrix algebras
1128 fill_with_scalar(scalar);
1129 } else {
1130 // a scalar has to be interpreted as a unit matrix for matrix algebras (the scalar in the shortest diagonal)
1131 // FIXME : A priori faster to put 0 everywhere and then change the diag to avoid the if.
1132 // FIXME : Benchmark and confirm.
1134 fill_with_scalar(0);
1135 else
1136 fill_with_scalar(Scalar{0 * scalar}); // FIXME : improve this
1137 diagonal(*this).fill_with_scalar(scalar);
1138 }
1139}
1140 };
1141
1142 // Class template argument deduction guides.
1143 template <MemoryArray A>
1144 basic_array(A &&a) -> basic_array<get_value_t<A>, get_rank<A>, get_contiguous_layout_policy<get_rank<A>, get_layout_info<A>.stride_order>,
1145 get_algebra<A>, heap<mem::get_addr_space<A>>>;
1146
1147 template <Array A>
1148 basic_array(A &&a) -> basic_array<get_value_t<A>, get_rank<A>, C_layout, get_algebra<A>, heap<mem::get_addr_space<A>>>;
1149
1150} // 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.
Provides utility functions for std::array.
Provides the generic class for views.
Provides basic functions to create and manipulate arrays and views.
Iterator for nda::basic_array and nda::basic_array_view types.
A generic view of a multi-dimensional array.
auto & operator+=(RHS const &rhs) noexcept
Addition assignment operator.
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.
typename ContainerPolicy::template handle< ValueType > storage_t
Type of the memory handle (see Handles).
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 the 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.
basic_array(Initializer const &initializer)
Construct an array from an nda::ArrayInitializer object.
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.
typename LayoutPolicy::template mapping< Rank > layout_t
Type of the memory layout (an nda::idx_map).
basic_array(A const &a)
Construct an array from an nda::ArrayOfRank object with the same rank by copying each element.
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.
basic_array & operator=(Initializer const &initializer)
Assignment operator uses an nda::ArrayInitializer to assign to the array.
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.
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.
basic_array regular_type
The associated regular type.
bool empty() const
Is the view/array empty?
void resize(std::array< long, Rank > const &shape)
Resize the array to a new shape.
long has_positive_strides() const noexcept
Are all the strides of the memory layout of the view/array positive?
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.
LayoutPolicy layout_policy_t
Type of the memory layout policy (see Layout policies).
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 & operator=(R const &rhs) noexcept
Assignment operator makes a deep copy of a general contiguous range and assigns it to the 1-dimension...
array_iterator< iterator_rank, ValueType, typename AccessorPolicy::template accessor< ValueType >::pointer > iterator
Iterator type of the view/array.
auto as_array_view()
Convert the current array to a view with an 'A' (array) algebra.
auto transpose() const
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.
ContainerPolicy container_policy_t
Type of the container policy (see Memory policies).
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
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=(RHS const &rhs) noexcept
Assignment operator assigns a scalar to the 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.
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.
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 & 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.
array_iterator< iterator_rank, ValueType const, typename AccessorPolicy::template accessor< ValueType >::pointer > const_iterator
Const iterator type of the view/array.
iterator end() noexcept
Get an iterator to the end of the view/array.
ValueType value_type
Type of the values in the array (can not be const).
static basic_array rand(std::array< Int, Rank > const &shape)
Make a random-initialized array with the given shape.
Check if a given type satisfies the memory array concept.
Definition concepts.hpp:223
Check if a given type is either an arithmetic or complex type.
Definition concepts.hpp:83
Provides concepts for the nda library.
Provides for_each functions for multi-dimensional arrays/views.
auto permuted_indices_view(A &&a)
Permute the indices/dimensions of an nda::basic_array or nda::basic_array_view.
auto zeros(std::array< Int, Rank > const &shape)
Make an array of the given shape on the given address space and zero-initialize it.
ArrayOfRank< 1 > auto diagonal(M &&m)
Get a view of the diagonal of a 2-dimensional array/view.
auto ones(std::array< Int, Rank > const &shape)
Make an array of the given shape and one-initialize it.
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...
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:206
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.
constexpr bool have_same_value_type_v
Constexpr variable that is true if all types in As have the same value type as A0.
Definition traits.hpp:195
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:63
constexpr bool is_any_lazy
Constexpr variable that is true if any of the given types is lazy.
Definition utils.hpp:144
constexpr bool ellipsis_is_present
Constexpr variable that is true if the parameter pack Args contains an nda::ellipsis.
Definition range.hpp:56
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:281
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
Definition traits.hpp:265
__inline__ void 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.
Definition for_each.hpp:116
constexpr bool has_layout_strided_1d
Constexpr variable that is true if type A has the strided_1d nda::layout_prop_e guarantee.
Definition traits.hpp:333
auto get_block_layout(A const &a)
Check if a given nda::MemoryArray has a block-strided layout.
constexpr layout_info_t get_layout_info
Constexpr variable that specifies the nda::layout_info_t of type A.
Definition traits.hpp:320
constexpr bool has_contiguous_layout
Constexpr variable that is true if type A has the contiguous nda::layout_prop_e guarantee.
Definition traits.hpp:329
static constexpr bool on_device
Constexpr variable that is true if all given types have a Device address space.
static constexpr bool on_unified
Constexpr variable that is true if all given types have a Unified address space.
static constexpr AddressSpace get_addr_space
Variable template providing the address space for different types.
static constexpr bool on_host
Constexpr variable that is true if all given types have a Host address space.
void memcpy2D(void *dest, size_t dpitch, const void *src, size_t spitch, size_t width, size_t height)
Call CUDA's cudaMemcpy2D function or simulate its behavior on the Host based on the given address spa...
Definition memcpy.hpp:73
static constexpr do_not_initialize_t do_not_initialize
Instance of nda::mem::do_not_initialize_t.
Definition handle.hpp:56
static constexpr init_zero_t init_zero
Instance of nda::mem::init_zero_t.
Definition handle.hpp:62
constexpr uint64_t encode(std::array< int, N > const &a)
Encode a std::array<int, N> in a uint64_t.
constexpr std::array< T, N > apply(std::array< Int, N > const &p, std::array< T, N > const &a)
Apply a permutation to a std::array.
constexpr std::array< T, R+1 > front_append(std::array< T, R > const &a, U const &x)
Make a new std::array by prepending one element at the front to an existing std::array.
Definition array.hpp:222
constexpr std::array< T, R > make_std_array(std::array< U, R > const &a)
Convert a std::array with value type U to a std::array with value type T.
Definition array.hpp:171
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:65
static constexpr bool always_false
Constexpr variable that is always false regardless of the types in Ts (used to trigger static_assert)...
Definition traits.hpp:61
constexpr bool is_scalar_for_v
Constexpr variable used to check requirements when initializing an nda::basic_array or nda::basic_arr...
Definition traits.hpp:83
constexpr bool is_scalar_v
Constexpr variable that is true if type S is a scalar type, i.e. arithmetic or complex.
Definition traits.hpp:69
constexpr bool is_scalar_or_convertible_v
Constexpr variable that is true if type S is a scalar type (see nda::is_scalar_v) or if a std::comple...
Definition traits.hpp:76
Provides an iterator for nda::basic_array and nda::basic_array_view types.
Provides functions to transform the memory layout of an nda::basic_array or nda::basic_array_view.
Macros used in the nda library.
Provides functions to create and manipulate matrices, i.e. arrays/view with 'M' algebra.
Defines various memory handling policies.
Provides a generic memcpy and memcpy2D function for different address spaces.
Provides utilities to work with permutations and to compactly encode/decode std::array objects.
Includes the itertools header and provides some additional utilities.
Provides utilities that determine the resulting nda::idx_map when taking a slice of an nda::idx_map.
A small wrapper around a single long integer to be used as a linear index.
Definition traits.hpp:342
Memory policy using an nda::mem::handle_borrowed.
Definition policies.hpp:97
Default accessor for various array and view types.
Definition accessors.hpp:25
uint64_t stride_order
Stride order of the array/view.
Definition traits.hpp:296
Tag used in constructors to indicate that the memory should be initialized to zero.
Definition handle.hpp:59
Provides type traits for the nda library.