TRIQS/nda 1.3.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 Commissariat à l'énergie atomique et aux énergies alternatives (CEA)
2// Copyright (c) 2018 Centre national de la recherche scientifique (CNRS)
3// Copyright (c) 2018-2024 Simons Foundation
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0.txt
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17// Authors: Thomas Hahn, Olivier Parcollet, Nils Wentzell
18
24#pragma once
25
26#include "./accessors.hpp"
28#include "./basic_functions.hpp"
29#include "./concepts.hpp"
30#include "./exceptions.hpp"
31#include "./iterators.hpp"
32#include "./layout/for_each.hpp"
34#include "./layout/range.hpp"
36#include "./macros.hpp"
38#include "./mem/memcpy.hpp"
39#include "./mem/policies.hpp"
40#include "./stdutil/array.hpp"
41#include "./traits.hpp"
42
43#include <algorithm>
44#include <array>
45#include <complex>
46#include <concepts>
47#include <initializer_list>
48#include <random>
49#include <ranges>
50#include <type_traits>
51#include <utility>
52
53#ifdef NDA_ENFORCE_BOUNDCHECK
54#include <exception>
55#include <iostream>
56#endif // NDA_ENFORCE_BOUNDCHECK
57
58namespace nda {
59
112 template <typename ValueType, int Rank, typename LayoutPolicy, char Algebra, typename ContainerPolicy>
114 // Compile-time checks.
115 static_assert(!std::is_const_v<ValueType>, "Error in nda::basic_array: ValueType cannot be const");
116 static_assert((Algebra != 'N'), "Internal error in nda::basic_array: Algebra 'N' not supported");
117 static_assert((Algebra != 'M') or (Rank == 2), "Internal error in nda::basic_array: Algebra 'M' requires a rank 2 array");
118 static_assert((Algebra != 'V') or (Rank == 1), "Internal error in nda::basic_array: Algebra 'V' requires a rank 1 array");
119
120 public:
122 using value_type = ValueType;
123
125 using layout_policy_t = LayoutPolicy;
126
128 using layout_t = typename LayoutPolicy::template mapping<Rank>;
129
131 using container_policy_t = ContainerPolicy;
132
134 using storage_t = typename ContainerPolicy::template handle<ValueType>;
135
138
140 static constexpr int rank = Rank;
141
142 // Compile-time check.
143 static_assert(has_contiguous(layout_t::layout_prop), "Error in nda::basic_array: Memory layout has to be contiguous");
144
145 private:
146 // Type of the array itself.
147 using self_t = basic_array;
148
149 // Type of the accessor policy for views (no no_alias_accessor).
151
152 // Type of the owning policy for views.
154
155 // Constexpr variable that is true if the value_type is const (never for basic_array).
156 static constexpr bool is_const = false;
157
158 // Constexpr variable that is true if the array is a view (never for basic_array).
159 static constexpr bool is_view = false;
160
161 // Memory layout of the array, i.e. the nda::idx_map.
162 layout_t lay;
163
164 // Memory handle of the array.
165 storage_t sto;
166
167 // Construct an array with a given shape and initialize the memory with zeros.
168 template <std::integral Int = long>
169 basic_array(std::array<Int, Rank> const &shape, mem::init_zero_t) : lay{shape}, sto{lay.size(), mem::init_zero} {}
170
171 public:
176 auto as_array_view() { return basic_array_view<ValueType, Rank, LayoutPolicy, 'A', AccessorPolicy, OwningPolicy>{*this}; };
177
182 auto as_array_view() const { return basic_array_view<const ValueType, Rank, LayoutPolicy, 'A', AccessorPolicy, OwningPolicy>{*this}; };
183
185 [[deprecated]] auto transpose()
186 requires(Rank == 2)
187 {
188 return permuted_indices_view<encode(std::array<int, 2>{1, 0})>(*this);
189 }
190
192 [[deprecated]] auto transpose() const
193 requires(Rank == 2)
194 {
195 return permuted_indices_view<encode(std::array<int, 2>{1, 0})>(*this);
196 }
197
199 basic_array() {}; // NOLINT (user-defined constructor to avoid value initialization of the sso buffer)
200
203
205 explicit basic_array(basic_array const &a) = default;
206
216 template <char A, typename CP>
217 explicit basic_array(basic_array<ValueType, Rank, LayoutPolicy, A, CP> a) noexcept : lay(a.indexmap()), sto(std::move(a.storage())) {}
218
228 template <std::integral... Ints>
229 requires(sizeof...(Ints) == Rank)
230 explicit basic_array(Ints... is) {
231 // setting the layout and storage in the constructor body improves error messages for wrong # of args
232 lay = layout_t{std::array{long(is)...}}; // NOLINT (for better error messages)
233 sto = storage_t{lay.size()}; // NOLINT (for better error messages)
234 }
235
244 template <std::integral Int, typename RHS>
245 explicit basic_array(Int sz, RHS const &val)
246 requires((Rank == 1 and is_scalar_for_v<RHS, basic_array>))
247 : lay(layout_t{std::array{long(sz)}}), sto{lay.size()} {
248 assign_from_scalar(val);
249 }
250
259 template <std::integral Int = long>
260 explicit basic_array(std::array<Int, Rank> const &shape)
261 requires(std::is_default_constructible_v<ValueType>)
262 : lay(shape), sto(lay.size()) {}
263
271 explicit basic_array(layout_t const &layout)
272 requires(std::is_default_constructible_v<ValueType>)
273 : lay{layout}, sto{lay.size()} {}
274
283 explicit basic_array(layout_t const &layout, storage_t &&storage) noexcept : lay{layout}, sto{std::move(storage)} {}
284
291 template <ArrayOfRank<Rank> A>
292 requires(HasValueTypeConstructibleFrom<A, ValueType>)
293 basic_array(A const &a) : lay(a.shape()), sto{lay.size(), mem::do_not_initialize} {
294 static_assert(std::is_constructible_v<ValueType, get_value_t<A>>, "Error in nda::basic_array: Incompatible value types in constructor");
295 if constexpr (std::is_trivial_v<ValueType> or is_complex_v<ValueType>) {
296 // trivial and complex value types can use the optimized assign_from_ndarray
297 if constexpr (std::is_same_v<ValueType, get_value_t<A>>)
298 assign_from_ndarray(a);
299 else
300 assign_from_ndarray(nda::map([](auto const &val) { return ValueType(val); })(a));
301 } else {
302 // general value types may not be default constructible -> use placement new
303 nda::for_each(lay.lengths(), [&](auto const &...is) { new (sto.data() + lay(is...)) ValueType{a(is...)}; });
304 }
305 }
306
316 template <ArrayInitializer<basic_array> Initializer> // can not be explicit
317 basic_array(Initializer const &initializer) : basic_array{initializer.shape()} {
318 initializer.invoke(*this);
319 }
320
321 private:
322 // Get the corresponding shape from an initializer list.
323 static std::array<long, 1> shape_from_init_list(std::initializer_list<ValueType> const &l) noexcept { return {long(l.size())}; }
324
325 // Get the corresponding shape from a nested initializer list.
326 template <typename L>
327 static auto shape_from_init_list(std::initializer_list<L> const &l) noexcept {
328 const auto [min, max] = std::minmax_element(std::begin(l), std::end(l), [](auto &&x, auto &&y) { return x.size() < y.size(); });
329 EXPECTS_WITH_MESSAGE(min->size() == max->size(),
330 "Error in nda::basic_array: Arrays can only be initialized with rectangular initializer lists");
331 return stdutil::front_append(shape_from_init_list(*max), long(l.size()));
332 }
333
334 public:
339 basic_array(std::initializer_list<ValueType> const &l)
340 requires(Rank == 1)
341 : lay(std::array<long, 1>{long(l.size())}), sto{lay.size(), mem::do_not_initialize} {
342 long i = 0;
343 for (auto const &x : l) { new (sto.data() + lay(i++)) ValueType{x}; }
344 }
345
350 basic_array(std::initializer_list<std::initializer_list<ValueType>> const &l2)
351 requires(Rank == 2)
352 : lay(shape_from_init_list(l2)), sto{lay.size(), mem::do_not_initialize} {
353 long i = 0, j = 0;
354 for (auto const &l1 : l2) {
355 for (auto const &x : l1) { new (sto.data() + lay(i, j++)) ValueType{x}; }
356 j = 0;
357 ++i;
358 }
359 }
360
365 basic_array(std::initializer_list<std::initializer_list<std::initializer_list<ValueType>>> const &l3)
366 requires(Rank == 3)
367 : lay(shape_from_init_list(l3)), sto{lay.size(), mem::do_not_initialize} {
368 long i = 0, j = 0, k = 0;
369 for (auto const &l2 : l3) {
370 for (auto const &l1 : l2) {
371 for (auto const &x : l1) { new (sto.data() + lay(i, j, k++)) ValueType{x}; }
372 k = 0;
373 ++j;
374 }
375 j = 0;
376 ++i;
377 }
378 }
379
389 template <char A2>
391 requires(Rank == 2)
392 : basic_array{a.indexmap(), std::move(a).storage()} {}
393
401 template <std::integral Int = long>
402 static basic_array zeros(std::array<Int, Rank> const &shape)
403 requires(std::is_standard_layout_v<ValueType> && std::is_trivially_copyable_v<ValueType>)
404 {
406 }
407
415 template <std::integral... Ints>
416 static basic_array zeros(Ints... is)
417 requires(sizeof...(Ints) == Rank)
418 {
419 return zeros(std::array<long, Rank>{is...});
420 }
421
429 template <std::integral Int = long>
430 static basic_array ones(std::array<Int, Rank> const &shape)
432 {
434 res() = ValueType{1};
435 return res;
436 }
437
445 template <std::integral... Ints>
446 static basic_array ones(Ints... is)
447 requires(sizeof...(Ints) == Rank)
448 {
449 return ones(std::array<long, Rank>{is...});
450 }
451
462 template <std::integral Int = long>
463 static basic_array rand(std::array<Int, Rank> const &shape)
464 requires(std::is_floating_point_v<ValueType> or nda::is_complex_v<ValueType>)
465 {
466 using namespace std::complex_literals;
467 auto static gen = std::mt19937(std::random_device{}());
468 auto res = basic_array{shape};
469 if constexpr (nda::is_complex_v<ValueType>) {
470 auto static dist = std::uniform_real_distribution<typename ValueType::value_type>(0.0, 1.0);
471 for (auto &x : res) x = dist(gen) + 1i * dist(gen);
472 } else {
473 auto static dist = std::uniform_real_distribution<ValueType>(0.0, 1.0);
474 for (auto &x : res) x = dist(gen);
475 }
476 return res;
477 }
478
489 template <std::integral... Ints>
490 static basic_array rand(Ints... is)
491 requires(sizeof...(Ints) == Rank)
492 {
493 return rand(std::array<long, Rank>{is...});
494 }
495
498
500 basic_array &operator=(basic_array const &) = default;
501
512 template <char A, typename CP>
514 *this = basic_array{rhs};
515 return *this;
516 }
517
527 template <ArrayOfRank<Rank> RHS>
528 basic_array &operator=(RHS const &rhs) {
529 resize(rhs.shape());
530 assign_from_ndarray(rhs);
531 return *this;
532 }
533
544 template <typename RHS>
545 basic_array &operator=(RHS const &rhs) noexcept
547 {
548 assign_from_scalar(rhs);
549 return *this;
550 }
551
561 template <ArrayInitializer<basic_array> Initializer>
562 basic_array &operator=(Initializer const &initializer) {
563 resize(initializer.shape());
564 initializer.invoke(*this);
565 return *this;
566 }
567
578 template <std::integral... Ints>
579 void resize(Ints const &...is) {
580 static_assert(std::is_copy_constructible_v<ValueType>, "Error in nda::basic_array: Resizing requires the value_type to be copy constructible");
581 static_assert(sizeof...(is) == Rank, "Error in nda::basic_array: Resizing requires exactly Rank arguments");
582 resize(std::array<long, Rank>{long(is)...});
583 }
584
594 [[gnu::noinline]] void resize(std::array<long, Rank> const &shape) {
595 lay = layout_t(shape);
596 if (sto.is_null() or (sto.size() != lay.size())) sto = storage_t{lay.size()};
597 }
598
599// include common functionality of arrays and views
600// Copyright (c) 2019-2024 Simons Foundation
601//
602// Licensed under the Apache License, Version 2.0 (the "License");
603// you may not use this file except in compliance with the License.
604// You may obtain a copy of the License at
605//
606// http://www.apache.org/licenses/LICENSE-2.0.txt
607//
608// Unless required by applicable law or agreed to in writing, software
609// distributed under the License is distributed on an "AS IS" BASIS,
610// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
611// See the License for the specific language governing permissions and
612// limitations under the License.
613//
614// Authors: Thomas Hahn, Miguel Morales, Olivier Parcollet, Nils Wentzell
615
620[[nodiscard]] constexpr auto const &indexmap() const noexcept { return lay; }
621
626[[nodiscard]] storage_t const &storage() const & noexcept { return sto; }
627
632[[nodiscard]] storage_t &storage() & noexcept { return sto; }
633
638[[nodiscard]] storage_t storage() && noexcept { return std::move(sto); }
639
646[[nodiscard]] constexpr auto stride_order() const noexcept { return lay.stride_order; }
647
652[[nodiscard]] ValueType const *data() const noexcept { return sto.data(); }
653
658[[nodiscard]] ValueType *data() noexcept { return sto.data(); }
659
664[[nodiscard]] auto const &shape() const noexcept { return lay.lengths(); }
665
670[[nodiscard]] auto const &strides() const noexcept { return lay.strides(); }
671
676[[nodiscard]] long size() const noexcept { return lay.size(); }
677
682[[nodiscard]] long is_contiguous() const noexcept { return lay.is_contiguous(); }
683
688[[nodiscard]] long has_positive_strides() const noexcept { return lay.has_positive_strides(); }
689
694[[nodiscard]] bool empty() const { return sto.is_null(); }
695
697[[nodiscard]] bool is_empty() const noexcept { return sto.is_null(); }
698
703[[nodiscard]] long extent(int i) const noexcept {
704#ifdef NDA_ENFORCE_BOUNDCHECK
705 if (i < 0 || i >= rank) {
706 std::cerr << "Error in extent: Dimension " << i << " is incompatible with array of rank " << rank << std::endl;
707 std::terminate();
708 }
709#endif
710 return lay.lengths()[i];
711}
712
714[[nodiscard]] long shape(int i) const noexcept { return extent(i); }
715
720[[nodiscard]] auto indices() const noexcept { return itertools::product_range(shape()); }
721
726static constexpr bool is_stride_order_C() noexcept { return layout_t::is_stride_order_C(); }
727
732static constexpr bool is_stride_order_Fortran() noexcept { return layout_t::is_stride_order_Fortran(); }
733
743decltype(auto) operator()(_linear_index_t idx) const noexcept {
744 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
745 return sto[idx.value * lay.min_stride()];
746 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
747 return sto[idx.value];
748 else
749 static_assert(always_false<layout_t>, "Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
750}
751
753decltype(auto) operator()(_linear_index_t idx) noexcept {
754 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
755 return sto[idx.value * lay.min_stride()];
756 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
757 return sto[idx.value];
758 else
759 static_assert(always_false<layout_t>, "Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
760}
761
762private:
763// Constexpr variable that is true if bounds checking is disabled.
764#ifdef NDA_ENFORCE_BOUNDCHECK
765static constexpr bool has_no_boundcheck = false;
766#else
767static constexpr bool has_no_boundcheck = true;
768#endif
769
770public:
787template <char ResultAlgebra, bool SelfIsRvalue, typename Self, typename... Ts>
788FORCEINLINE static decltype(auto) call(Self &&self, Ts const &...idxs) noexcept(has_no_boundcheck) {
789 // resulting value type
790 using r_v_t = std::conditional_t<std::is_const_v<std::remove_reference_t<Self>>, ValueType const, ValueType>;
791
792 // behavior depends on the given arguments
793 if constexpr (clef::is_any_lazy<Ts...>) {
794 // if there are lazy arguments, e.g. as in A(i_) << i_, a lazy expression is returned
795 return clef::make_expr_call(std::forward<Self>(self), idxs...);
796 } else if constexpr (sizeof...(Ts) == 0) {
797 // if no arguments are given, a full view is returned
799 } else {
800 // otherwise we check the arguments and either access a single element or make a slice
801 static_assert(((layout_t::template argument_is_allowed_for_call_or_slice<Ts> + ...) > 0),
802 "Error in array/view: Slice arguments must be convertible to range, ellipsis, or long (or string if the layout permits it)");
803
804 // number of arguments convertible to long
805 static constexpr int n_args_long = (layout_t::template argument_is_allowed_for_call<Ts> + ...);
806
807 if constexpr (n_args_long == rank) {
808 // access a single element
809 long offset = self.lay(idxs...);
810 if constexpr (is_view or not SelfIsRvalue) {
811 // if the calling object is a view or an lvalue, we return a reference
812 return AccessorPolicy::template accessor<r_v_t>::access(self.sto.data(), offset);
813 } else {
814 // otherwise, we return a copy of the value
815 return ValueType{self.sto[offset]};
816 }
817 } else {
818 // access a slice of the view/array
819 auto const [offset, idxm] = self.lay.slice(idxs...);
820 static constexpr auto res_rank = decltype(idxm)::rank();
821 // resulting algebra
822 static constexpr char newAlgebra = (ResultAlgebra == 'M' and (res_rank == 1) ? 'V' : ResultAlgebra);
823 // resulting layout policy
824 using r_layout_p = typename detail::layout_to_policy<std::decay_t<decltype(idxm)>>::type;
826 }
827 }
828}
829
830public:
852template <typename... Ts>
853FORCEINLINE decltype(auto) operator()(Ts const &...idxs) const & noexcept(has_no_boundcheck) {
854 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
855 "Error in array/view: Incorrect number of parameters in call operator");
856 return call<Algebra, false>(*this, idxs...);
857}
858
860template <typename... Ts>
861FORCEINLINE decltype(auto) operator()(Ts const &...idxs) & noexcept(has_no_boundcheck) {
862 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
863 "Error in array/view: Incorrect number of parameters in call operator");
864 return call<Algebra, false>(*this, idxs...);
865}
866
868template <typename... Ts>
869FORCEINLINE decltype(auto) operator()(Ts const &...idxs) && noexcept(has_no_boundcheck) {
870 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
871 "Error in array/view: Incorrect number of parameters in call operator");
872 return call<Algebra, true>(*this, idxs...);
873}
874
892template <typename T>
893decltype(auto) operator[](T const &idx) const & noexcept(has_no_boundcheck) {
894 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
895 return call<Algebra, false>(*this, idx);
896}
897
899template <typename T>
900decltype(auto) operator[](T const &x) & noexcept(has_no_boundcheck) {
901 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
902 return call<Algebra, false>(*this, x);
903}
904
906template <typename T>
907decltype(auto) operator[](T const &x) && noexcept(has_no_boundcheck) {
908 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
909 return call<Algebra, true>(*this, x);
910}
911
913static constexpr int iterator_rank = (has_strided_1d(layout_t::layout_prop) ? 1 : Rank);
914
917
920
921private:
922// Make an iterator for the view/array depending on its type.
923template <typename Iterator>
924[[nodiscard]] auto make_iterator(bool at_end) const noexcept {
925 if constexpr (iterator_rank == Rank) {
926 // multi-dimensional iterator
927 if constexpr (layout_t::is_stride_order_C()) {
928 // C-order case (array_iterator already traverses the data in C-order)
929 return Iterator{indexmap().lengths(), indexmap().strides(), sto.data(), at_end};
930 } else {
931 // general case (we need to permute the shape and the strides according to the stride order of the layout)
932 return Iterator{nda::permutations::apply(layout_t::stride_order, indexmap().lengths()),
933 nda::permutations::apply(layout_t::stride_order, indexmap().strides()), sto.data(), at_end};
934 }
935 } else {
936 // 1-dimensional iterator
937 return Iterator{std::array<long, 1>{size()}, std::array<long, 1>{indexmap().min_stride()}, sto.data(), at_end};
938 }
939}
940
941public:
943[[nodiscard]] const_iterator begin() const noexcept { return make_iterator<const_iterator>(false); }
944
946[[nodiscard]] const_iterator cbegin() const noexcept { return make_iterator<const_iterator>(false); }
947
949iterator begin() noexcept { return make_iterator<iterator>(false); }
950
952[[nodiscard]] const_iterator end() const noexcept { return make_iterator<const_iterator>(true); }
953
955[[nodiscard]] const_iterator cend() const noexcept { return make_iterator<const_iterator>(true); }
956
958iterator end() noexcept { return make_iterator<iterator>(true); }
959
972template <typename RHS>
973auto &operator+=(RHS const &rhs) noexcept {
974 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
975 return operator=(*this + rhs);
976}
977
990template <typename RHS>
991auto &operator-=(RHS const &rhs) noexcept {
992 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
993 return operator=(*this - rhs);
994}
995
1008template <typename RHS>
1009auto &operator*=(RHS const &rhs) noexcept {
1010 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
1011 return operator=((*this) * rhs);
1012}
1013
1026template <typename RHS>
1027auto &operator/=(RHS const &rhs) noexcept {
1028 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
1029 return operator=(*this / rhs);
1030}
1031
1040template <std::ranges::contiguous_range R>
1041auto &operator=(R const &rhs) noexcept
1042 requires(Rank == 1 and not MemoryArray<R> and not is_scalar_for_v<R, self_t>)
1043{
1045 return *this;
1046}
1047
1048private:
1049// Implementation of the assignment from an n-dimensional array type.
1050template <typename RHS>
1051void assign_from_ndarray(RHS const &rhs) { // FIXME noexcept {
1052#ifdef NDA_ENFORCE_BOUNDCHECK
1053 if (this->shape() != rhs.shape())
1054 NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Size mismatch:"
1055 << "\n LHS.shape() = " << this->shape() << "\n RHS.shape() = " << rhs.shape();
1056#endif
1057 // compile-time check if assignment is possible
1058 static_assert(std::is_assignable_v<value_type &, get_value_t<RHS>>, "Error in assign_from_ndarray: Incompatible value types");
1059
1060 // are both operands nda::MemoryArray types?
1061 static constexpr bool both_in_memory = MemoryArray<self_t> and MemoryArray<RHS>;
1062
1063 // do both operands have the same stride order?
1064 static constexpr bool same_stride_order = get_layout_info<self_t>.stride_order == get_layout_info<RHS>.stride_order;
1065
1066 // prefer optimized options if possible
1067 if constexpr (both_in_memory and same_stride_order) {
1068 if (rhs.empty()) return;
1069 // are both operands strided in 1d?
1070 static constexpr bool both_1d_strided = has_layout_strided_1d<self_t> and has_layout_strided_1d<RHS>;
1071 if constexpr (mem::on_host<self_t, RHS> and both_1d_strided) {
1072 // vectorizable copy on host
1073 for (long i = 0; i < size(); ++i) (*this)(_linear_index_t{i}) = rhs(_linear_index_t{i});
1074 return;
1076 // check for block-layout and use mem::memcpy2D if possible
1077 auto bl_layout_dst = get_block_layout(*this);
1078 auto bl_layout_src = get_block_layout(rhs);
1079 if (bl_layout_dst && bl_layout_src) {
1080 auto [n_bl_dst, bl_size_dst, bl_str_dst] = *bl_layout_dst;
1081 auto [n_bl_src, bl_size_src, bl_str_src] = *bl_layout_src;
1082 // check that the total memory size is the same
1083 if (n_bl_dst * bl_size_dst != n_bl_src * bl_size_src) NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Incompatible block sizes";
1084 // if either destination or source consists of a single block, we can chunk it up to make the layouts compatible
1085 if (n_bl_dst == 1 && n_bl_src > 1) {
1086 n_bl_dst = n_bl_src;
1087 bl_size_dst /= n_bl_src;
1088 bl_str_dst = bl_size_dst;
1089 }
1090 if (n_bl_src == 1 && n_bl_dst > 1) {
1091 n_bl_src = n_bl_dst;
1092 bl_size_src /= n_bl_dst;
1093 bl_str_src = bl_size_src;
1094 }
1095 // copy only if block-layouts are compatible, otherwise continue to fallback
1096 if (n_bl_dst == n_bl_src && bl_size_dst == bl_size_src) {
1097 mem::memcpy2D<mem::get_addr_space<self_t>, mem::get_addr_space<RHS>>((void *)data(), bl_str_dst * sizeof(value_type), (void *)rhs.data(),
1098 bl_str_src * sizeof(value_type), bl_size_src * sizeof(value_type),
1099 n_bl_src);
1100 return;
1101 }
1102 }
1103 }
1104 }
1105 // otherwise fallback to elementwise assignment
1107 NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Fallback to elementwise assignment not implemented for arrays/views on the GPU";
1108 }
1109 nda::for_each(shape(), [this, &rhs](auto const &...args) { (*this)(args...) = rhs(args...); });
1110}
1111
1112// Implementation to fill a view/array with a constant scalar value.
1113template <typename Scalar>
1114void fill_with_scalar(Scalar const &scalar) noexcept {
1115 // we make a special implementation if the array is strided in 1d or contiguous
1116 if constexpr (has_layout_strided_1d<self_t>) {
1117 const long L = size();
1118 auto *__restrict const p = data(); // no alias possible here!
1119 if constexpr (has_contiguous_layout<self_t>) {
1120 for (long i = 0; i < L; ++i) p[i] = scalar;
1121 } else {
1122 const long stri = indexmap().min_stride();
1123 const long Lstri = L * stri;
1124 for (long i = 0; i != Lstri; i += stri) p[i] = scalar;
1125 }
1126 } else {
1127 // no compile-time memory layout guarantees
1128 for (auto &x : *this) x = scalar;
1129 }
1130}
1131
1132// Implementation of the assignment from a scalar value.
1133template <typename Scalar>
1134void assign_from_scalar(Scalar const &scalar) noexcept {
1135 static_assert(!is_const, "Error in assign_from_ndarray: Cannot assign to a const view");
1136 if constexpr (Algebra != 'M') {
1137 // element-wise assignment for non-matrix algebras
1138 fill_with_scalar(scalar);
1139 } else {
1140 // a scalar has to be interpreted as a unit matrix for matrix algebras (the scalar in the shortest diagonal)
1141 // FIXME : A priori faster to put 0 everywhere and then change the diag to avoid the if.
1142 // FIXME : Benchmark and confirm.
1144 fill_with_scalar(0);
1145 else
1146 fill_with_scalar(Scalar{0 * scalar}); // FIXME : improve this
1147 const long imax = std::min(extent(0), extent(1));
1148 for (long i = 0; i < imax; ++i) operator()(i, i) = scalar;
1149 }
1150}
1151 };
1152
1153 // Class template argument deduction guides.
1154 template <MemoryArray A>
1155 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>,
1156 get_algebra<A>, heap<mem::get_addr_space<A>>>;
1157
1158 template <Array A>
1159 basic_array(A &&a) -> basic_array<get_value_t<A>, get_rank<A>, C_layout, get_algebra<A>, heap<>>;
1160
1161} // 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.
A generic 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 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.
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.
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...
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.
void resize(Ints const &...is)
Resize the array to a new shape.
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
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=(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 const & shape() const noexcept
Get the shape of the view/array.
long size() const noexcept
Get the total size 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.
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.
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:248
Provides concepts for the nda library.
Provides a custom runtime error class and macros to assert conditions and throw exceptions.
Provides for_each functions for multi-dimensional arrays/views.
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...
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.
auto ones(std::array< Int, Rank > const &shape)
Make an array of the given shape and one-initialize it.
mapped< F > map(F f)
Create a lazy function call expression on arrays/views.
Definition map.hpp:213
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:196
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
constexpr bool is_any_lazy
Constexpr variable that is true if any of the given types is lazy.
Definition utils.hpp:156
constexpr bool ellipsis_is_present
Constexpr variable that is true if the parameter pack Args contains an nda::ellipsis.
Definition range.hpp:69
constexpr bool has_contiguous(layout_prop_e lp)
Checks if a layout property has the contiguous property.
Definition traits.hpp:282
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
Definition traits.hpp:266
__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:129
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:334
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:321
constexpr bool has_contiguous_layout
Constexpr variable that is true if type A has the contiguous nda::layout_prop_e guarantee.
Definition traits.hpp:330
static constexpr bool on_device
Constexpr variable that is true if all given types have a Device 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:84
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.
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:235
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:184
constexpr bool is_complex_v
Constexpr variable that is true if type T is a std::complex type.
Definition traits.hpp:75
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:71
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:93
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:79
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:86
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.
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.
A small wrapper around a single long integer to be used as a linear index.
Definition traits.hpp:343
Memory policy using an nda::mem::handle_borrowed.
Definition policies.hpp:108
Default accessor for various array and view types.
Definition accessors.hpp:36
uint64_t stride_order
Stride order of the array/view.
Definition traits.hpp:297
Tag used in constructors to indicate that the memory should be initialized to zero.
Definition handle.hpp:72
Provides type traits for the nda library.