TRIQS/nda 1.3.0
Multi-dimensional array library for C++
Loading...
Searching...
No Matches
_impl_basic_array_view_common.hpp
1// Copyright (c) 2019-2024 Simons Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0.txt
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// Authors: Thomas Hahn, Miguel Morales, Olivier Parcollet, Nils Wentzell
16
21[[nodiscard]] constexpr auto const &indexmap() const noexcept { return lay; }
22
27[[nodiscard]] storage_t const &storage() const & noexcept { return sto; }
28
33[[nodiscard]] storage_t &storage() & noexcept { return sto; }
34
39[[nodiscard]] storage_t storage() && noexcept { return std::move(sto); }
40
47[[nodiscard]] constexpr auto stride_order() const noexcept { return lay.stride_order; }
48
53[[nodiscard]] ValueType const *data() const noexcept { return sto.data(); }
54
59[[nodiscard]] ValueType *data() noexcept { return sto.data(); }
60
65[[nodiscard]] auto const &shape() const noexcept { return lay.lengths(); }
66
71[[nodiscard]] auto const &strides() const noexcept { return lay.strides(); }
72
77[[nodiscard]] long size() const noexcept { return lay.size(); }
78
83[[nodiscard]] long is_contiguous() const noexcept { return lay.is_contiguous(); }
84
89[[nodiscard]] bool empty() const { return sto.is_null(); }
90
92[[nodiscard]] bool is_empty() const noexcept { return sto.is_null(); }
93
98[[nodiscard]] long extent(int i) const noexcept {
99#ifdef NDA_ENFORCE_BOUNDCHECK
100 if (i < 0 || i >= rank) {
101 std::cerr << "Error in extent: Dimension " << i << " is incompatible with array of rank " << rank << std::endl;
102 std::terminate();
103 }
104#endif
105 return lay.lengths()[i];
106}
107
109[[nodiscard]] long shape(int i) const noexcept { return extent(i); }
110
115[[nodiscard]] auto indices() const noexcept { return itertools::product_range(shape()); }
116
121static constexpr bool is_stride_order_C() noexcept { return layout_t::is_stride_order_C(); }
122
127static constexpr bool is_stride_order_Fortran() noexcept { return layout_t::is_stride_order_Fortran(); }
128
138decltype(auto) operator()(_linear_index_t idx) const noexcept {
139 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
140 return sto[idx.value * lay.min_stride()];
141 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
142 return sto[idx.value];
143 else
144 static_assert(always_false<layout_t>, "Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
145}
146
148decltype(auto) operator()(_linear_index_t idx) noexcept {
149 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
150 return sto[idx.value * lay.min_stride()];
151 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
152 return sto[idx.value];
153 else
154 static_assert(always_false<layout_t>, "Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
155}
156
157private:
158// Constexpr variable that is true if bounds checking is disabled.
159#ifdef NDA_ENFORCE_BOUNDCHECK
160static constexpr bool has_no_boundcheck = false;
161#else
162static constexpr bool has_no_boundcheck = true;
163#endif
164
165public:
182template <char ResultAlgebra, bool SelfIsRvalue, typename Self, typename... Ts>
183FORCEINLINE static decltype(auto) call(Self &&self, Ts const &...idxs) noexcept(has_no_boundcheck) {
184 // resulting value type
185 using r_v_t = std::conditional_t<std::is_const_v<std::remove_reference_t<Self>>, ValueType const, ValueType>;
186
187 // behavior depends on the given arguments
188 if constexpr (clef::is_any_lazy<Ts...>) {
189 // if there are lazy arguments, e.g. as in A(i_) << i_, a lazy expression is returned
190 return clef::make_expr_call(std::forward<Self>(self), idxs...);
191 } else if constexpr (sizeof...(Ts) == 0) {
192 // if no arguments are given, a full view is returned
193 return basic_array_view<r_v_t, Rank, LayoutPolicy, Algebra, AccessorPolicy, OwningPolicy>{self.lay, self.sto};
194 } else {
195 // otherwise we check the arguments and either access a single element or make a slice
196 static_assert(((layout_t::template argument_is_allowed_for_call_or_slice<Ts> + ...) > 0),
197 "Error in array/view: Slice arguments must be convertible to range, ellipsis, or long (or string if the layout permits it)");
198
199 // number of arguments convertible to long
200 static constexpr int n_args_long = (layout_t::template argument_is_allowed_for_call<Ts> + ...);
201
202 if constexpr (n_args_long == rank) {
203 // access a single element
204 long offset = self.lay(idxs...);
205 if constexpr (is_view or not SelfIsRvalue) {
206 // if the calling object is a view or an lvalue, we return a reference
207 return AccessorPolicy::template accessor<ValueType>::access(self.sto.data(), offset);
208 } else {
209 // otherwise, we return a copy of the value
210 return ValueType{self.sto[offset]};
211 }
212 } else {
213 // access a slice of the view/array
214 auto const [offset, idxm] = self.lay.slice(idxs...);
215 static constexpr auto res_rank = decltype(idxm)::rank();
216 // resulting algebra
217 static constexpr char newAlgebra = (ResultAlgebra == 'M' and (res_rank == 1) ? 'V' : ResultAlgebra);
218 // resulting layout policy
219 using r_layout_p = typename detail::layout_to_policy<std::decay_t<decltype(idxm)>>::type;
220 return basic_array_view<ValueType, res_rank, r_layout_p, newAlgebra, AccessorPolicy, OwningPolicy>{std::move(idxm), {self.sto, offset}};
221 }
222 }
223}
224
225public:
247template <typename... Ts>
248FORCEINLINE decltype(auto) operator()(Ts const &...idxs) const & noexcept(has_no_boundcheck) {
249 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
250 "Error in array/view: Incorrect number of parameters in call operator");
251 return call<Algebra, false>(*this, idxs...);
252}
253
255template <typename... Ts>
256FORCEINLINE decltype(auto) operator()(Ts const &...idxs) & noexcept(has_no_boundcheck) {
257 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
258 "Error in array/view: Incorrect number of parameters in call operator");
259 return call<Algebra, false>(*this, idxs...);
260}
261
263template <typename... Ts>
264FORCEINLINE decltype(auto) operator()(Ts const &...idxs) && noexcept(has_no_boundcheck) {
265 static_assert((rank == -1) or (sizeof...(Ts) == rank) or (sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
266 "Error in array/view: Incorrect number of parameters in call operator");
267 return call<Algebra, true>(*this, idxs...);
268}
269
287template <typename T>
288decltype(auto) operator[](T const &idx) const & noexcept(has_no_boundcheck) {
289 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
290 return call<Algebra, false>(*this, idx);
291}
292
294template <typename T>
295decltype(auto) operator[](T const &x) & noexcept(has_no_boundcheck) {
296 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
297 return call<Algebra, false>(*this, x);
298}
299
301template <typename T>
302decltype(auto) operator[](T const &x) && noexcept(has_no_boundcheck) {
303 static_assert((rank == 1), "Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
304 return call<Algebra, true>(*this, x);
305}
306
308static constexpr int iterator_rank = (has_strided_1d(layout_t::layout_prop) ? 1 : Rank);
309
311using const_iterator = array_iterator<iterator_rank, ValueType const, typename AccessorPolicy::template accessor<ValueType>::pointer>;
312
314using iterator = array_iterator<iterator_rank, ValueType, typename AccessorPolicy::template accessor<ValueType>::pointer>;
315
316private:
317// Make an iterator for the view/array depending on its type.
318template <typename Iterator>
319[[nodiscard]] auto make_iterator(bool at_end) const noexcept {
320 if constexpr (iterator_rank == Rank) {
321 // multi-dimensional iterator
322 if constexpr (layout_t::is_stride_order_C()) {
323 // C-order case (array_iterator already traverses the data in C-order)
324 return Iterator{indexmap().lengths(), indexmap().strides(), sto.data(), at_end};
325 } else {
326 // general case (we need to permute the shape and the strides according to the stride order of the layout)
327 return Iterator{nda::permutations::apply(layout_t::stride_order, indexmap().lengths()),
328 nda::permutations::apply(layout_t::stride_order, indexmap().strides()), sto.data(), at_end};
329 }
330 } else {
331 // 1-dimensional iterator
332 return Iterator{std::array<long, 1>{size()}, std::array<long, 1>{indexmap().min_stride()}, sto.data(), at_end};
333 }
334}
335
336public:
338[[nodiscard]] const_iterator begin() const noexcept { return make_iterator<const_iterator>(false); }
339
341[[nodiscard]] const_iterator cbegin() const noexcept { return make_iterator<const_iterator>(false); }
342
344iterator begin() noexcept { return make_iterator<iterator>(false); }
345
347[[nodiscard]] const_iterator end() const noexcept { return make_iterator<const_iterator>(true); }
348
350[[nodiscard]] const_iterator cend() const noexcept { return make_iterator<const_iterator>(true); }
351
353iterator end() noexcept { return make_iterator<iterator>(true); }
354
367template <typename RHS>
368auto &operator+=(RHS const &rhs) noexcept {
369 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
370 return operator=(*this + rhs);
371}
372
385template <typename RHS>
386auto &operator-=(RHS const &rhs) noexcept {
387 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
388 return operator=(*this - rhs);
389}
390
403template <typename RHS>
404auto &operator*=(RHS const &rhs) noexcept {
405 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
406 return operator=((*this) * rhs);
407}
408
421template <typename RHS>
422auto &operator/=(RHS const &rhs) noexcept {
423 static_assert(not is_const, "Error in array/view: Can not assign to a const view");
424 return operator=(*this / rhs);
425}
426
435template <std::ranges::contiguous_range R>
436auto &operator=(R const &rhs) noexcept
437 requires(Rank == 1 and not MemoryArray<R>)
438{
439 *this = basic_array_view{rhs};
440 return *this;
441}
442
443private:
444// Implementation of the assignment from an n-dimensional array type.
445template <typename RHS>
446void assign_from_ndarray(RHS const &rhs) { // FIXME noexcept {
447#ifdef NDA_ENFORCE_BOUNDCHECK
448 if (this->shape() != rhs.shape())
449 NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Size mismatch:"
450 << "\n LHS.shape() = " << this->shape() << "\n RHS.shape() = " << rhs.shape();
451#endif
452 // compile-time check if assignment is possible
453 static_assert(std::is_assignable_v<value_type &, get_value_t<RHS>>, "Error in assign_from_ndarray: Incompatible value types");
454
455 // are both operands nda::MemoryArray types?
456 static constexpr bool both_in_memory = MemoryArray<self_t> and MemoryArray<RHS>;
457
458 // do both operands have the same stride order?
459 static constexpr bool same_stride_order = get_layout_info<self_t>.stride_order == get_layout_info<RHS>.stride_order;
460
461 // prefer optimized options if possible
462 if constexpr (both_in_memory and same_stride_order) {
463 if (rhs.empty()) return;
464 // are both operands strided in 1d?
465 static constexpr bool both_1d_strided = has_layout_strided_1d<self_t> and has_layout_strided_1d<RHS>;
466 if constexpr (mem::on_host<self_t, RHS> and both_1d_strided) {
467 // vectorizable copy on host
468 for (long i = 0; i < size(); ++i) (*this)(_linear_index_t{i}) = rhs(_linear_index_t{i});
469 return;
470 } else if constexpr (!mem::on_host<self_t, RHS> and have_same_value_type_v<self_t, RHS>) {
471 // check for block-layout and use mem::memcpy2D if possible
472 auto bl_layout_dst = get_block_layout(*this);
473 auto bl_layout_src = get_block_layout(rhs);
474 if (bl_layout_dst && bl_layout_src) {
475 auto [n_bl_dst, bl_size_dst, bl_str_dst] = *bl_layout_dst;
476 auto [n_bl_src, bl_size_src, bl_str_src] = *bl_layout_src;
477 // check that the total memory size is the same
478 if (n_bl_dst * bl_size_dst != n_bl_src * bl_size_src) NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Incompatible block sizes";
479 // if either destination or source consists of a single block, we can chunk it up to make the layouts compatible
480 if (n_bl_dst == 1 && n_bl_src > 1) {
481 n_bl_dst = n_bl_src;
482 bl_size_dst /= n_bl_src;
483 bl_str_dst = bl_size_dst;
484 }
485 if (n_bl_src == 1 && n_bl_dst > 1) {
486 n_bl_src = n_bl_dst;
487 bl_size_src /= n_bl_dst;
488 bl_str_src = bl_size_src;
489 }
490 // copy only if block-layouts are compatible, otherwise continue to fallback
491 if (n_bl_dst == n_bl_src && bl_size_dst == bl_size_src) {
492 mem::memcpy2D<mem::get_addr_space<self_t>, mem::get_addr_space<RHS>>((void *)data(), bl_str_dst * sizeof(value_type), (void *)rhs.data(),
493 bl_str_src * sizeof(value_type), bl_size_src * sizeof(value_type),
494 n_bl_src);
495 return;
496 }
497 }
498 }
499 }
500 // otherwise fallback to elementwise assignment
501 if constexpr (mem::on_device<self_t> || mem::on_device<RHS>) {
502 NDA_RUNTIME_ERROR << "Error in assign_from_ndarray: Fallback to elementwise assignment not implemented for arrays/views on the GPU";
503 }
504 nda::for_each(shape(), [this, &rhs](auto const &...args) { (*this)(args...) = rhs(args...); });
505}
506
507// Implementation to fill a view/array with a constant scalar value.
508template <typename Scalar>
509void fill_with_scalar(Scalar const &scalar) noexcept {
510 // we make a special implementation if the array is strided in 1d or contiguous
511 if constexpr (has_layout_strided_1d<self_t>) {
512 const long L = size();
513 auto *__restrict const p = data(); // no alias possible here!
514 if constexpr (has_contiguous_layout<self_t>) {
515 for (long i = 0; i < L; ++i) p[i] = scalar;
516 } else {
517 const long stri = indexmap().min_stride();
518 const long Lstri = L * stri;
519 for (long i = 0; i < Lstri; i += stri) p[i] = scalar;
520 }
521 } else {
522 // no compile-time memory layout guarantees
523 for (auto &x : *this) x = scalar;
524 }
525}
526
527// Implementation of the assignment from a scalar value.
528template <typename Scalar>
529void assign_from_scalar(Scalar const &scalar) noexcept {
530 static_assert(!is_const, "Error in assign_from_ndarray: Cannot assign to a const view");
531 if constexpr (Algebra != 'M') {
532 // element-wise assignment for non-matrix algebras
533 fill_with_scalar(scalar);
534 } else {
535 // a scalar has to be interpreted as a unit matrix for matrix algebras (the scalar in the shortest diagonal)
536 // FIXME : A priori faster to put 0 everywhere and then change the diag to avoid the if.
537 // FIXME : Benchmark and confirm.
538 if constexpr (is_scalar_or_convertible_v<Scalar>)
539 fill_with_scalar(0);
540 else
541 fill_with_scalar(Scalar{0 * scalar}); // FIXME : improve this
542 const long imax = std::min(extent(0), extent(1));
543 for (long i = 0; i < imax; ++i) operator()(i, i) = scalar;
544 }
545}
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
auto get_block_layout(A const &a)
Check if a given nda::MemoryArray has a block-strided layout.
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.