16 #include <type_traits> 18 #include "atlas/array/Array.h" 19 #include "atlas/array/ArrayUtil.h" 20 #include "atlas/array/ArrayViewDefs.h" 22 #include "atlas/array/gridtools/GridToolsMakeView.h" 23 #include "atlas/array/gridtools/GridToolsTraits.h" 24 #include "atlas/library/config.h" 31 template <
typename Value,
int Rank>
34 using is_non_const_value_type =
typename std::is_same<T, typename std::remove_const<Value>::type>;
36 #define ENABLE_IF_NON_CONST \ 37 template <bool EnableBool = true, \ 38 typename std::enable_if<( !std::is_const<Value>::value && EnableBool ), int>::type* = nullptr> 40 #define ENABLE_IF_CONST_WITH_NON_CONST( T ) \ 41 template <typename T, \ 42 typename std::enable_if<( std::is_const<Value>::value && is_non_const_value_type<T>::value ), \ 43 int>::type* = nullptr> 47 using value_type = Value;
48 using non_const_value_type =
typename std::remove_const<Value>::type;
49 static constexpr
bool is_const = std::is_const<Value>::value;
50 static constexpr
bool is_non_const = !std::is_const<Value>::value;
53 static constexpr
int RANK{Rank};
56 using data_view_t = gridtools::data_view_tt<Value, Rank, gridtools::get_access_mode<Value>()>;
62 template <
typename... Args>
64 using type =
typename slicer_t::template
Slice<Args...>::type;
67 template <
typename... Args>
68 struct const_slice_t {
69 using type =
typename const_slicer_t::template
Slice<Args...>::type;
75 ArrayView(
const Array& array,
bool device_view );
77 ENABLE_IF_CONST_WITH_NON_CONST( value_type )
79 gt_data_view_( other.is_device_view_ ? gridtools::make_gt_device_view<Value, Rank>( *other.array_ )
80 : gridtools::make_gt_host_view<Value, Rank>( *other.array_ ) ),
81 data_store_orig_( other.data_store_orig_ ),
82 array_( other.array_ ),
83 is_device_view_( other.is_device_view_ ) {
84 std::memcpy( shape_, other.shape_,
sizeof( ArrayShape::value_type ) * Rank );
85 std::memcpy( strides_, other.strides_,
sizeof( ArrayStrides::value_type ) * Rank );
90 ENABLE_IF_CONST_WITH_NON_CONST( value_type )
93 value_type* data() {
return gt_data_view_.data(); }
94 value_type
const* data()
const {
return gt_data_view_.data(); }
96 template <
typename... Coords,
typename =
typename std::enable_if<(
sizeof...( Coords ) == Rank ),
int>::type>
97 ATLAS_HOST_DEVICE value_type& operator()( Coords... c ) {
98 assert(
sizeof...( Coords ) == Rank );
99 return gt_data_view_( c... );
102 template <
typename... Coords,
typename =
typename std::enable_if<(
sizeof...( Coords ) == Rank ),
int>::type>
103 ATLAS_HOST_DEVICE value_type
const& operator()( Coords... c )
const {
104 assert(
sizeof...( Coords ) == Rank );
105 return gt_data_view_( c... );
108 template <
typename Int,
bool EnableBool = true>
109 ATLAS_HOST_DEVICE
typename std::enable_if<( Rank == 1 && EnableBool ), const value_type&>::type operator[](
111 return gt_data_view_( idx );
114 template <
typename Int,
bool EnableBool = true>
115 ATLAS_HOST_DEVICE
typename std::enable_if<( Rank == 1 && EnableBool ), value_type&>::type operator[]( Int idx ) {
116 return gt_data_view_( idx );
119 template <
unsigned int Dim>
120 ATLAS_HOST_DEVICE
idx_t shape()
const {
121 return gt_data_view_.template length<Dim>();
125 data_view_t& data_view() {
return gt_data_view_; }
127 data_view_t
const& data_view()
const {
return gt_data_view_; }
129 template <
unsigned int Dim>
130 ATLAS_HOST_DEVICE
idx_t stride()
const {
131 return gt_data_view_.storage_info().template stride<Dim>();
134 static constexpr
idx_t rank() {
return Rank; }
135 size_t size()
const {
return size_; }
138 bool contiguous()
const {
return ( size_ ==
size_t( shape_[0] ) *
size_t( strides_[0] ) ?
true :
false ); }
140 void dump( std::ostream& os )
const;
143 void assign(
const value_type& value );
146 void assign(
const std::initializer_list<value_type>& list );
148 const idx_t* strides()
const {
return strides_; }
150 const idx_t* shape()
const {
return shape_; }
152 template <
typename Int>
153 idx_t shape( Int idx )
const {
157 template <
typename Int>
158 idx_t stride( Int idx )
const {
159 return strides_[idx];
162 template <
typename... Args>
163 typename slice_t<Args...>::type slice( Args... args ) {
164 return slicer_t( *this ).apply( args... );
167 template <
typename... Args>
168 typename const_slice_t<Args...>::type slice( Args... args )
const {
169 return const_slicer_t( *this ).apply( args... );
172 bool isDeviceView()
const {
return is_device_view_; }
175 template <
typename friendValue,
int friendRank>
176 friend class ArrayView;
179 data_view_t gt_data_view_;
181 idx_t strides_[Rank];
185 bool is_device_view_{
false};
186 #undef ENABLE_IF_NON_CONST 187 #undef ENABLE_IF_CONST_WITH_NON_CONST Definition: ArraySlicer.h:110
Multi-dimensional access to a Array object or Field object.
Definition: GridToolsArrayView.h:32
Definition: test_array_slicer.cc:22
Definition: ArrayUtil.h:46
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42
This file contains the LocalView class, a class that allows to wrap any contiguous raw data into a vi...