12 #include <type_traits> 14 #include "atlas/array/gridtools/GridToolsTraits.h" 15 #include "atlas/library/config.h" 28 template <
typename Value>
40 void set(
const Value& value ) { *( idx_ ) = value + BASE; }
42 Value
get()
const {
return *(idx_)-BASE; }
44 void operator=(
const Value& value ) {
set( value ); }
63 operator Value()
const {
return get(); }
73 template <
typename Value,
int Rank>
77 #if ATLAS_HAVE_FORTRAN 79 #define INDEX_REF Index 80 #define FROM_FORTRAN -1 89 gridtools::data_view_tt<typename std::remove_const<Value>::type, Rank, gridtools::get_access_mode<Value>()>;
92 IndexView( data_view_t data_view ) : gt_data_view_( data_view ) {
93 if ( data_view.valid() )
94 size_ = gt_data_view_.storage_info().total_length();
99 template <
typename... Coords,
typename =
typename std::enable_if<(
sizeof...( Coords ) == Rank ),
int>::type>
100 Index ATLAS_HOST_DEVICE operator()( Coords... c ) {
101 assert(
sizeof...( Coords ) == Rank );
102 return INDEX_REF( >_data_view_( c... ) );
105 template <
typename... Coords,
typename =
typename std::enable_if<(
sizeof...( Coords ) == Rank ),
int>::type>
106 ATLAS_HOST_DEVICE Value
const operator()( Coords... c )
const {
107 return gt_data_view_( c... ) FROM_FORTRAN;
110 idx_t size()
const {
return size_; }
112 void dump( std::ostream& os )
const;
115 data_view_t gt_data_view_;
124 #if ATLAS_HAVE_FORTRAN 125 #define INDEX_REF Index 126 #define FROM_FORTRAN -1 127 #define TO_FORTRAN +1 135 template <
typename Value,
int Rank>
138 using value_type =
typename remove_const<Value>::type;
140 #if ATLAS_HAVE_FORTRAN 143 typedef Value& Index;
153 template <
typename... Ints,
typename =
typename std::enable_if<(
sizeof...( Ints ) == Rank ),
int>::type>
154 Index operator()( Ints... idx ) {
155 check_bounds( idx... );
156 return INDEX_REF( &data_[index( idx... )] );
159 template <
typename... Ints,
typename =
typename std::enable_if<(
sizeof...( Ints ) == Rank ),
int>::type>
160 const value_type operator()( Ints... idx )
const {
161 return data_[index( idx... )] FROM_FORTRAN;
167 template <
int Dim,
typename Int,
typename... Ints>
168 constexpr
idx_t index_part( Int idx, Ints... next_idx )
const {
169 return idx * strides_[Dim] + index_part<Dim + 1>( next_idx... );
172 template <
int Dim,
typename Int>
173 constexpr
idx_t index_part( Int last_idx )
const {
174 return last_idx * strides_[Dim];
177 template <
typename... Ints>
178 constexpr
idx_t index( Ints... idx )
const {
179 return index_part<0>( idx... );
182 #if ATLAS_INDEXVIEW_BOUNDS_CHECKING 183 template <
typename... Ints>
184 void check_bounds( Ints... idx )
const {
185 static_assert(
sizeof...( idx ) == Rank,
"Expected number of indices is different from rank of array" );
186 return check_bounds_part<0>( idx... );
189 template <
typename... Ints>
190 void check_bounds( Ints... )
const {}
193 template <
typename... Ints>
194 void check_bounds_force( Ints... idx )
const {
195 static_assert(
sizeof...( idx ) == Rank,
"Expected number of indices is different from rank of array" );
196 return check_bounds_part<0>( idx... );
199 template <
int Dim,
typename Int,
typename... Ints>
200 void check_bounds_part( Int idx, Ints... next_idx )
const {
201 if (
idx_t( idx ) >= shape_[Dim] ) {
202 throw_OutOfRange(
"IndexView", array_dim<Dim>(), idx, shape_[Dim] );
204 check_bounds_part<Dim + 1>( next_idx... );
207 template <
int Dim,
typename Int>
208 void check_bounds_part( Int last_idx )
const {
209 if (
idx_t( last_idx ) >= shape_[Dim] ) {
210 throw_OutOfRange(
"IndexView", array_dim<Dim>(), last_idx, shape_[Dim] );
214 idx_t size()
const {
return shape_[0]; }
216 void dump( std::ostream& os )
const;
220 idx_t strides_[Rank];
Definition: GridToolsIndexView.h:29
Definition: GridToolsIndexView.h:136
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
Multidimensional access to Array or Field objects containing index fields that are compatible with Fo...
Definition: GridToolsIndexView.h:74
long idx_t
Integer type for indices in connectivity tables.
Definition: config.h:42
Definition: ArrayViewDefs.h:20