21[[nodiscard]]
constexpr auto const &indexmap() const noexcept {
return lay; }
27[[nodiscard]] storage_t
const &storage() const & noexcept {
return sto; }
33[[nodiscard]] storage_t &storage() &
noexcept {
return sto; }
39[[nodiscard]] storage_t storage() &&
noexcept {
return std::move(sto); }
47[[nodiscard]]
constexpr auto stride_order() const noexcept {
return lay.stride_order; }
53[[nodiscard]] ValueType
const *data() const noexcept {
return sto.data(); }
59[[nodiscard]] ValueType *data() noexcept {
return sto.data(); }
65[[nodiscard]]
auto const &shape() const noexcept {
return lay.lengths(); }
71[[nodiscard]]
auto const &strides() const noexcept {
return lay.strides(); }
77[[nodiscard]]
long size() const noexcept {
return lay.size(); }
83[[nodiscard]]
long is_contiguous() const noexcept {
return lay.is_contiguous(); }
89[[nodiscard]]
long has_positive_strides() const noexcept {
return lay.has_positive_strides(); }
95[[nodiscard]]
bool empty()
const {
return sto.is_null(); }
98[[nodiscard]]
bool is_empty() const noexcept {
return sto.is_null(); }
104[[nodiscard]]
long extent(
int i)
const noexcept {
105#ifdef NDA_ENFORCE_BOUNDCHECK
106 if (i < 0 || i >= rank) {
107 std::cerr <<
"Error in extent: Dimension " << i <<
" is incompatible with array of rank " << rank << std::endl;
111 return lay.lengths()[i];
115[[nodiscard]]
long shape(
int i)
const noexcept {
return extent(i); }
121[[nodiscard]]
auto indices() const noexcept {
return itertools::product_range(shape()); }
127static constexpr bool is_stride_order_C() noexcept {
return layout_t::is_stride_order_C(); }
133static constexpr bool is_stride_order_Fortran() noexcept {
return layout_t::is_stride_order_Fortran(); }
144decltype(
auto)
operator()(_linear_index_t idx)
const noexcept {
145 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
146 return sto[idx.value * lay.min_stride()];
147 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
148 return sto[idx.value];
150 static_assert(always_false<layout_t>,
"Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
154decltype(
auto)
operator()(_linear_index_t idx)
noexcept {
155 if constexpr (layout_t::layout_prop == layout_prop_e::strided_1d)
156 return sto[idx.value * lay.min_stride()];
157 else if constexpr (layout_t::layout_prop == layout_prop_e::contiguous)
158 return sto[idx.value];
160 static_assert(always_false<layout_t>,
"Internal error in array/view: Calling this type with a _linear_index_t is not allowed");
165#ifdef NDA_ENFORCE_BOUNDCHECK
166static constexpr bool has_no_boundcheck =
false;
168static constexpr bool has_no_boundcheck =
true;
188template <
char ResultAlgebra,
bool SelfIsRvalue,
typename Self,
typename... Ts>
189FORCEINLINE
static decltype(
auto) call(Self &&self, Ts
const &...idxs)
noexcept(has_no_boundcheck) {
191 using r_v_t = std::conditional_t<std::is_const_v<std::remove_reference_t<Self>>, ValueType
const, ValueType>;
194 if constexpr (clef::is_any_lazy<Ts...>) {
196 return clef::make_expr_call(std::forward<Self>(self), idxs...);
197 }
else if constexpr (
sizeof...(Ts) == 0) {
199 return basic_array_view<r_v_t, Rank, LayoutPolicy, Algebra, AccessorPolicy, OwningPolicy>{self.lay, self.sto};
202 static_assert(((layout_t::template argument_is_allowed_for_call_or_slice<Ts> + ...) > 0),
203 "Error in array/view: Slice arguments must be convertible to range, ellipsis, or long (or string if the layout permits it)");
206 static constexpr int n_args_long = (layout_t::template argument_is_allowed_for_call<Ts> + ...);
208 if constexpr (n_args_long == rank) {
210 long offset = self.lay(idxs...);
211 if constexpr (is_view or not SelfIsRvalue) {
213 return AccessorPolicy::template accessor<r_v_t>::access(self.sto.data(), offset);
216 return ValueType{self.sto[offset]};
220 auto const [offset, idxm] = self.lay.slice(idxs...);
221 static constexpr auto res_rank =
decltype(idxm)::rank();
223 static constexpr char newAlgebra = (ResultAlgebra ==
'M' and (res_rank == 1) ?
'V' : ResultAlgebra);
225 using r_layout_p =
typename detail::layout_to_policy<std::decay_t<
decltype(idxm)>>::type;
226 return basic_array_view<r_v_t, res_rank, r_layout_p, newAlgebra, AccessorPolicy, OwningPolicy>{std::move(idxm), {self.sto, offset}};
253template <
typename... Ts>
254FORCEINLINE
decltype(
auto)
operator()(Ts
const &...idxs)
const &
noexcept(has_no_boundcheck) {
255 static_assert((rank == -1) or (
sizeof...(Ts) == rank) or (
sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
256 "Error in array/view: Incorrect number of parameters in call operator");
257 return call<Algebra, false>(*this, idxs...);
261template <typename... Ts>
262FORCEINLINE decltype(auto) operator()(Ts const &...idxs) & noexcept(has_no_boundcheck) {
263 static_assert((rank == -1) or (
sizeof...(Ts) == rank) or (
sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
264 "Error in array/view: Incorrect number of parameters in call operator");
265 return call<Algebra, false>(*this, idxs...);
269template <typename... Ts>
270FORCEINLINE decltype(auto) operator()(Ts const &...idxs) && noexcept(has_no_boundcheck) {
271 static_assert((rank == -1) or (
sizeof...(Ts) == rank) or (
sizeof...(Ts) == 0) or (ellipsis_is_present<Ts...> and (sizeof...(Ts) <= rank + 1)),
272 "Error in array/view: Incorrect number of parameters in call operator");
273 return call<Algebra, true>(*this, idxs...);
294decltype(auto) operator[](T const &idx) const & noexcept(has_no_boundcheck) {
295 static_assert((rank == 1),
"Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
296 return call<Algebra, false>(*
this, idx);
301decltype(
auto)
operator[](T
const &x) &
noexcept(has_no_boundcheck) {
302 static_assert((rank == 1),
"Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
303 return call<Algebra, false>(*
this, x);
308decltype(
auto)
operator[](T
const &x) &&
noexcept(has_no_boundcheck) {
309 static_assert((rank == 1),
"Error in array/view: Subscript operator is only available for rank 1 views/arrays in C++17/20");
310 return call<Algebra, true>(*
this, x);
314static constexpr int iterator_rank = (
has_strided_1d(layout_t::layout_prop) ? 1 : Rank);
317using const_iterator = array_iterator<iterator_rank, ValueType const, typename AccessorPolicy::template accessor<ValueType>::pointer>;
320using iterator = array_iterator<iterator_rank, ValueType, typename AccessorPolicy::template accessor<ValueType>::pointer>;
324template <
typename Iterator>
325[[nodiscard]]
auto make_iterator(
bool at_end)
const noexcept {
326 if constexpr (iterator_rank == Rank) {
328 if constexpr (layout_t::is_stride_order_C()) {
330 return Iterator{indexmap().lengths(), indexmap().strides(), sto.data(), at_end};
338 return Iterator{std::array<long, 1>{size()}, std::array<long, 1>{indexmap().min_stride()}, sto.data(), at_end};
344[[nodiscard]] const_iterator begin() const noexcept {
return make_iterator<const_iterator>(
false); }
347[[nodiscard]] const_iterator cbegin() const noexcept {
return make_iterator<const_iterator>(
false); }
350iterator begin() noexcept {
return make_iterator<iterator>(
false); }
353[[nodiscard]] const_iterator end() const noexcept {
return make_iterator<const_iterator>(
true); }
356[[nodiscard]] const_iterator cend() const noexcept {
return make_iterator<const_iterator>(
true); }
359iterator end() noexcept {
return make_iterator<iterator>(
true); }
373template <
typename RHS>
374auto &operator+=(RHS
const &rhs)
noexcept {
375 static_assert(not is_const,
"Error in array/view: Can not assign to a const view");
376 return operator=(*
this + rhs);
391template <
typename RHS>
392auto &operator-=(RHS
const &rhs)
noexcept {
393 static_assert(not is_const,
"Error in array/view: Can not assign to a const view");
394 return operator=(*
this - rhs);
409template <
typename RHS>
410auto &operator*=(RHS
const &rhs)
noexcept {
411 static_assert(not is_const,
"Error in array/view: Can not assign to a const view");
412 return operator=((*
this) * rhs);
427template <
typename RHS>
428auto &operator/=(RHS
const &rhs)
noexcept {
429 static_assert(not is_const,
"Error in array/view: Can not assign to a const view");
430 return operator=(*
this / rhs);
441template <std::ranges::contiguous_range R>
442auto &operator=(R
const &rhs)
noexcept
443 requires(Rank == 1 and not MemoryArray<R> and not is_scalar_for_v<R, self_t>)
445 *
this = array_const_view<std::ranges::range_value_t<R>, 1>{rhs};
451template <
typename RHS>
452void assign_from_ndarray(RHS
const &rhs) {
453#ifdef NDA_ENFORCE_BOUNDCHECK
454 if (this->shape() != rhs.shape())
455 NDA_RUNTIME_ERROR <<
"Error in assign_from_ndarray: Size mismatch:"
456 <<
"\n LHS.shape() = " << this->shape() <<
"\n RHS.shape() = " << rhs.shape();
459 static_assert(std::is_assignable_v<value_type &, get_value_t<RHS>>,
"Error in assign_from_ndarray: Incompatible value types");
462 static constexpr bool both_in_memory = MemoryArray<self_t> and MemoryArray<RHS>;
465 static constexpr bool same_stride_order = get_layout_info<self_t>.stride_order == get_layout_info<RHS>.stride_order;
468 if constexpr (both_in_memory and same_stride_order) {
469 if (rhs.empty())
return;
471 static constexpr bool both_1d_strided = has_layout_strided_1d<self_t> and has_layout_strided_1d<RHS>;
472 if constexpr (mem::on_host<self_t, RHS> and both_1d_strided) {
474 for (
long i = 0; i < size(); ++i) (*
this)(_linear_index_t{i}) = rhs(_linear_index_t{i});
476 }
else if constexpr (!mem::on_host<self_t, RHS> and have_same_value_type_v<self_t, RHS>) {
480 if (bl_layout_dst && bl_layout_src) {
481 auto [n_bl_dst, bl_size_dst, bl_str_dst] = *bl_layout_dst;
482 auto [n_bl_src, bl_size_src, bl_str_src] = *bl_layout_src;
484 if (n_bl_dst * bl_size_dst != n_bl_src * bl_size_src) NDA_RUNTIME_ERROR <<
"Error in assign_from_ndarray: Incompatible block sizes";
486 if (n_bl_dst == 1 && n_bl_src > 1) {
488 bl_size_dst /= n_bl_src;
489 bl_str_dst = bl_size_dst;
491 if (n_bl_src == 1 && n_bl_dst > 1) {
493 bl_size_src /= n_bl_dst;
494 bl_str_src = bl_size_src;
497 if (n_bl_dst == n_bl_src && bl_size_dst == bl_size_src) {
498 mem::memcpy2D<mem::get_addr_space<self_t>, mem::get_addr_space<RHS>>((
void *)data(), bl_str_dst *
sizeof(value_type), (
void *)rhs.data(),
499 bl_str_src *
sizeof(value_type), bl_size_src *
sizeof(value_type),
507 if constexpr (mem::on_device<self_t> || mem::on_device<RHS>) {
508 NDA_RUNTIME_ERROR <<
"Error in assign_from_ndarray: Fallback to elementwise assignment not implemented for arrays/views on the GPU";
510 nda::for_each(shape(), [
this, &rhs](
auto const &...args) { (*this)(args...) = rhs(args...); });
514template <
typename Scalar>
515void fill_with_scalar(Scalar
const &scalar)
noexcept {
517 if constexpr (has_layout_strided_1d<self_t>) {
518 const long L = size();
519 auto *__restrict
const p = data();
520 if constexpr (has_contiguous_layout<self_t>) {
521 for (
long i = 0; i < L; ++i) p[i] = scalar;
523 const long stri = indexmap().min_stride();
524 const long Lstri = L * stri;
525 for (
long i = 0; i != Lstri; i += stri) p[i] = scalar;
529 for (
auto &x : *
this) x = scalar;
534template <
typename Scalar>
535void assign_from_scalar(Scalar
const &scalar)
noexcept {
536 static_assert(!is_const,
"Error in assign_from_ndarray: Cannot assign to a const view");
537 if constexpr (Algebra !=
'M') {
539 fill_with_scalar(scalar);
544 if constexpr (is_scalar_or_convertible_v<Scalar>)
547 fill_with_scalar(Scalar{0 * scalar});
548 const long imax = std::min(extent(0), extent(1));
549 for (
long i = 0; i < imax; ++i)
operator()(i, i) = scalar;
constexpr bool has_strided_1d(layout_prop_e lp)
Checks if a layout property has the strided_1d property.
__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.
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.