/* * (C) Copyright 2013 ECMWF. * * This software is licensed under the terms of the Apache Licence Version 2.0 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. * In applying this licence, ECMWF does not waive the privileges and immunities * granted to it by virtue of its status as an intergovernmental organisation * nor does it submit to any jurisdiction. */ /// @file LocalView.h /// This file contains the LocalView class, a class that allows to wrap any /// contiguous raw data into /// a view which is accessible with multiple indices. #pragma once #include #include #include #include "atlas/array/ArrayDataStore.h" #include "atlas/array/ArrayViewDefs.h" #include "atlas/array/helpers/ArraySlicer.h" #include "atlas/library/config.h" #include "atlas/mdspan.h" //------------------------------------------------------------------------------------------------------ namespace atlas { namespace array { /// @brief Multi-dimensional access existing POD array pointer. /// /// A LocalView is a wrapper around data that enables multidimensional access, and has the exact /// same API as ArrayView. /// /// The data may be strided. /// /// ### Example 1: /// /// @code{.cpp} /// int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; /// int[2] strides = { 3, 1 }; /// int[2] shape = { 3, 3 }; /// LocalView matrix( array, shape, strides ); /// for( idx_t i=0; i matrix( array, shape ); /// which is identical for this matrix to previous Example 1 /// @endcode template class LocalView { template using is_non_const_value_type = typename std::is_same::type>; #define ENABLE_IF_NON_CONST \ template ::value && EnableBool), int>::type* = nullptr> #define ENABLE_IF_CONST_WITH_NON_CONST(T) \ template ::value && is_non_const_value_type::value), \ int>::type* = nullptr> public: // -- Type definitions using value_type = Value; using return_type = value_type; static constexpr int RANK{Rank}; private: using slicer_t = typename helpers::ArraySlicer>; using const_slicer_t = typename helpers::ArraySlicer>; template struct slice_t { using type = typename slicer_t::template Slice::type; }; template struct const_slice_t { using type = typename const_slicer_t::template Slice::type; }; using mdspan_extents_type = dextents; using mdspan_strides_type = std::array; using mdspan_type = mdspan; using const_mdspan_type = mdspan; public: // -- Constructors template >> LocalView(const LocalView& other): data_(other.data_), size_(other.size_), shape_(other.shape_), strides_(other.strides_) {} template && std::is_integral_v && std::is_integral_v>> LocalView(ValueTp* data, const Int1 shape[], const Int2 strides[]): data_(data) { size_ = 1; for (idx_t j = 0; j < Rank; ++j) { shape_[j] = shape[j]; strides_[j] = strides[j]; size_ *= shape_[j]; } } template && std::is_integral_v>> LocalView(ValueTp* data, const Int shape[]): data_(data) { size_ = 1; for (int j = Rank - 1; j >= 0; --j) { shape_[j] = shape[j]; strides_[j] = size_; size_ *= shape_[j]; } } template >> LocalView(ValueTp* data, const ArrayShape& shape) : LocalView(data,shape.data()) {} template && E::rank() == Rank>> LocalView(mdspan& other) : data_(other.data_handle()), size_(other.size()) { for (int j = 0; j < Rank; ++j) { shape_[j] = other.extent(j); strides_[j] = other.stride(j); } } ENABLE_IF_CONST_WITH_NON_CONST(value_type) operator const LocalView&() const { static_assert(std::is_const::value, "must be const"); static_assert(!std::is_const::value, "must be non-const"); return (const LocalView&)(*this); } const_mdspan_type as_mdspan() const { return const_mdspan_type{this->data(), {mdspan_extents(), mdspan_strides()}}; } mdspan_type as_mdspan() { return mdspan_type{this->data(), {mdspan_extents(), mdspan_strides()}}; } // -- Access methods template > value_type& operator()(Idx... idx) { check_bounds(idx...); return data_[index(idx...)]; } template > const value_type& operator()(Idx... idx) const { check_bounds(idx...); return data_[index(idx...)]; } template > const value_type& operator[](Idx idx) const { check_bounds(idx); return data_[index(idx)]; } template > value_type& operator[](Idx idx) { check_bounds(idx); return data_[index(idx)]; } idx_t size() const { return size_; } template idx_t shape(Int idx) const { return shape_[idx]; } template idx_t stride(Int idx) const { return strides_[idx]; } const idx_t* shape() const { return shape_.data(); } const idx_t* strides() const { return strides_.data(); } value_type const* data() const { return data_; } value_type* data() { return data_; } bool contiguous() const { return (size_ == shape_[0] * strides_[0] ? true : false); } ENABLE_IF_NON_CONST void assign(const value_type& value); void dump(std::ostream& os) const; static constexpr idx_t rank() { return Rank; } template typename slice_t::type slice(Args... args) { return slicer_t(*this).apply(args...); } template typename const_slice_t::type slice(Args... args) const { return const_slicer_t(*this).apply(args...); } friend std::ostream& operator<<(std::ostream& out, const LocalView& x) { x.dump(out); return out; } private: // -- Private methods template constexpr idx_t index_part(Int idx, Ints... next_idx) const { return idx * strides_[Dim] + index_part(next_idx...); } template constexpr idx_t index_part(Int last_idx) const { return last_idx * strides_[Dim]; } template constexpr idx_t index(Ints... idx) const { return index_part<0>(idx...); } #if ATLAS_ARRAYVIEW_BOUNDS_CHECKING template void check_bounds(Ints... idx) const { static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); return check_bounds_part<0>(idx...); } #else template void check_bounds(Ints... idx) const { static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); } #endif template void check_bounds_force(Ints... idx) const { static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); return check_bounds_part<0>(idx...); } template void check_bounds_part(Int idx, Ints... next_idx) const { if (idx_t(idx) >= shape_[Dim]) { throw_OutOfRange("LocalView", array_dim(), idx, shape_[Dim]); } check_bounds_part(next_idx...); } template void check_bounds_part(Int last_idx) const { if (idx_t(last_idx) >= shape_[Dim]) { throw_OutOfRange("LocalView", array_dim(), last_idx, shape_[Dim]); } } template ATLAS_HOST_DEVICE mdspan_extents_type _get_mdspan_extents(std::integer_sequence = {}) const { return mdspan_extents_type{(shape_[i])...}; } ATLAS_HOST_DEVICE mdspan_extents_type mdspan_extents() const { return _get_mdspan_extents(std::make_integer_sequence{}); } template ATLAS_HOST_DEVICE mdspan_strides_type _get_mdspan_strides(std::integer_sequence = {}) const { return mdspan_strides_type{(static_cast(strides_[i]))...}; } ATLAS_HOST_DEVICE mdspan_strides_type mdspan_strides() const { return _get_mdspan_strides(std::make_integer_sequence{}); } private: // -- Private data template friend class LocalView; value_type* data_; idx_t size_; std::array shape_; std::array strides_; #undef ENABLE_IF_NON_CONST #undef ENABLE_IF_CONST_WITH_NON_CONST }; template using View = LocalView; } // namespace array } // namespace atlas