| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
#include <type_traits> |
| 13 |
|
| 14 |
#include "atlas/array/Array.h" |
| 15 |
#include "atlas/array/gridtools/GridToolsTraits.h" |
| 16 |
#include "atlas/library/config.h" |
| 17 |
|
| 18 |
//------------------------------------------------------------------------------------------------------ |
| 19 |
|
| 20 |
namespace atlas { |
| 21 |
namespace array { |
| 22 |
|
| 23 |
//------------------------------------------------------------------------------------------------------ |
| 24 |
|
| 25 |
namespace detail { |
| 26 |
|
| 27 |
// FortranIndex: |
| 28 |
// Helper class that does +1 and -1 operations on stored values |
| 29 |
template <typename Value> |
| 30 |
class FortranIndex { |
| 31 |
public: |
| 32 |
enum |
| 33 |
{ |
| 34 |
BASE = 1 |
| 35 |
}; |
| 36 |
|
| 37 |
public: |
| 38 |
ATLAS_HOST_DEVICE |
| 39 |
FortranIndex(Value* idx): idx_(idx) {} |
| 40 |
ATLAS_HOST_DEVICE |
| 41 |
void set(const Value& value) { *(idx_) = value + BASE; } |
| 42 |
ATLAS_HOST_DEVICE |
| 43 |
Value get() const { return *(idx_)-BASE; } |
| 44 |
ATLAS_HOST_DEVICE |
| 45 |
void operator=(const Value& value) { set(value); } |
| 46 |
ATLAS_HOST_DEVICE |
| 47 |
FortranIndex<Value>& operator=(const FortranIndex<Value>& other) { |
| 48 |
set(other.get()); |
| 49 |
return *this; |
| 50 |
} |
| 51 |
ATLAS_HOST_DEVICE |
| 52 |
FortranIndex<Value>& operator--() { |
| 53 |
--(*(idx_)); |
| 54 |
return *this; |
| 55 |
} |
| 56 |
ATLAS_HOST_DEVICE |
| 57 |
FortranIndex<Value>& operator++() { |
| 58 |
++(*(idx_)); |
| 59 |
return *this; |
| 60 |
} |
| 61 |
|
| 62 |
ATLAS_HOST_DEVICE |
| 63 |
FortranIndex<Value>& operator+=(Value v) { |
| 64 |
*(idx_) += v; |
| 65 |
return *this; |
| 66 |
} |
| 67 |
|
| 68 |
ATLAS_HOST_DEVICE |
| 69 |
FortranIndex<Value>& operator-=(Value v) { |
| 70 |
*(idx_) -= v; |
| 71 |
return *this; |
| 72 |
} |
| 73 |
|
| 74 |
// implicit conversion |
| 75 |
ATLAS_HOST_DEVICE |
| 76 |
operator Value() const { return get(); } |
| 77 |
|
| 78 |
private: |
| 79 |
Value* idx_; |
| 80 |
}; |
| 81 |
|
| 82 |
} // namespace detail |
| 83 |
|
| 84 |
//------------------------------------------------------------------------------------------------------ |
| 85 |
|
| 86 |
template <typename Value, int Rank> |
| 87 |
class IndexView { |
| 88 |
public: |
| 89 |
// -- Type definitions |
| 90 |
#if ATLAS_HAVE_FORTRAN |
| 91 |
typedef detail::FortranIndex<Value> Index; |
| 92 |
#define INDEX_REF Index |
| 93 |
#define FROM_FORTRAN -1 |
| 94 |
#define TO_FORTRAN +1 |
| 95 |
#else |
| 96 |
typedef Value& Index; |
| 97 |
#define INDEX_REF * |
| 98 |
#define FROM_FORTRAN |
| 99 |
#define TO_FORTRAN |
| 100 |
#endif |
| 101 |
using data_view_t = |
| 102 |
gridtools::data_view_tt<typename std::remove_const<Value>::type, Rank, gridtools::get_access_mode<Value>()>; |
| 103 |
|
| 104 |
using value_type = Value; |
| 105 |
|
| 106 |
public: |
| 107 |
IndexView(data_view_t); |
| 108 |
IndexView(const Array&, bool device_view); |
| 109 |
|
| 110 |
template <typename... Coords, typename = typename std::enable_if<(sizeof...(Coords) == Rank), int>::type> |
| 111 |
ATLAS_HOST_DEVICE |
| 112 |
Index operator()(Coords... c) { |
| 113 |
assert(sizeof...(Coords) == Rank); |
| 114 |
return INDEX_REF(>_data_view_(c...)); |
| 115 |
} |
| 116 |
|
| 117 |
template <typename... Coords, typename = typename std::enable_if<(sizeof...(Coords) == Rank), int>::type> |
| 118 |
ATLAS_HOST_DEVICE |
| 119 |
Value const operator()(Coords... c) const { |
| 120 |
return gt_data_view_(c...) FROM_FORTRAN; |
| 121 |
} |
| 122 |
|
| 123 |
ATLAS_HOST_DEVICE |
| 124 |
idx_t size() const { return size_; } |
| 125 |
|
| 126 |
template <typename Int> |
| 127 |
ATLAS_HOST_DEVICE |
| 128 |
idx_t shape(Int idx) const { |
| 129 |
return shape_[idx]; |
| 130 |
} |
| 131 |
|
| 132 |
template <typename Int> |
| 133 |
ATLAS_HOST_DEVICE |
| 134 |
idx_t stride(Int idx) const { |
| 135 |
return strides_[idx]; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
void dump(std::ostream& os) const; |
| 140 |
|
| 141 |
private: |
| 142 |
data_view_t gt_data_view_; |
| 143 |
idx_t size_; |
| 144 |
idx_t shape_[Rank]; |
| 145 |
idx_t strides_[Rank]; |
| 146 |
bool is_device_view_; |
| 147 |
ArrayDataStore const* data_store_orig_; |
| 148 |
Array const* array_; |
| 149 |
|
| 150 |
#undef INDEX_REF |
| 151 |
#undef TO_FORTRAN |
| 152 |
#undef FROM_FORTRAN |
| 153 |
}; |
| 154 |
|
| 155 |
|
| 156 |
#if ATLAS_HAVE_FORTRAN |
| 157 |
#define INDEX_REF Index |
| 158 |
#define FROM_FORTRAN -1 |
| 159 |
#define TO_FORTRAN +1 |
| 160 |
#else |
| 161 |
#define INDEX_REF * |
| 162 |
#define FROM_FORTRAN |
| 163 |
#define TO_FORTRAN |
| 164 |
#endif |
| 165 |
|
| 166 |
|
| 167 |
template <typename Value, int Rank> |
| 168 |
class LocalIndexView { |
| 169 |
public: |
| 170 |
using value_type = typename remove_const<Value>::type; |
| 171 |
|
| 172 |
#if ATLAS_HAVE_FORTRAN |
| 173 |
typedef detail::FortranIndex<Value> Index; |
| 174 |
#else |
| 175 |
typedef Value& Index; |
| 176 |
#endif |
| 177 |
|
| 178 |
public: |
| 179 |
LocalIndexView(Value* data, const idx_t shape[Rank]); |
| 180 |
|
| 181 |
LocalIndexView(Value* data, const idx_t shape[Rank], const idx_t strides[Rank]); |
| 182 |
|
| 183 |
// -- Access methods |
| 184 |
|
| 185 |
template <typename... Ints, typename = typename std::enable_if<(sizeof...(Ints) == Rank), int>::type> |
| 186 |
Index operator()(Ints... idx) { |
| 187 |
check_bounds(idx...); |
| 188 |
return INDEX_REF(&data_[index(idx...)]); |
| 189 |
} |
| 190 |
|
| 191 |
template <typename... Ints, typename = typename std::enable_if<(sizeof...(Ints) == Rank), int>::type> |
| 192 |
const value_type operator()(Ints... idx) const { |
| 193 |
return data_[index(idx...)] FROM_FORTRAN; |
| 194 |
} |
| 195 |
|
| 196 |
private: |
| 197 |
// -- Private methods |
| 198 |
|
| 199 |
template <int Dim, typename Int, typename... Ints> |
| 200 |
constexpr idx_t index_part(Int idx, Ints... next_idx) const { |
| 201 |
return idx * strides_[Dim] + index_part<Dim + 1>(next_idx...); |
| 202 |
} |
| 203 |
|
| 204 |
template <int Dim, typename Int> |
| 205 |
constexpr idx_t index_part(Int last_idx) const { |
| 206 |
return last_idx * strides_[Dim]; |
| 207 |
} |
| 208 |
|
| 209 |
template <typename... Ints> |
| 210 |
constexpr idx_t index(Ints... idx) const { |
| 211 |
return index_part<0>(idx...); |
| 212 |
} |
| 213 |
|
| 214 |
#if ATLAS_INDEXVIEW_BOUNDS_CHECKING |
| 215 |
template <typename... Ints> |
| 216 |
void check_bounds(Ints... idx) const { |
| 217 |
static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); |
| 218 |
return check_bounds_part<0>(idx...); |
| 219 |
} |
| 220 |
#else |
| 221 |
template <typename... Ints> |
| 222 |
void check_bounds(Ints...) const {} |
| 223 |
#endif |
| 224 |
|
| 225 |
template <typename... Ints> |
| 226 |
void check_bounds_force(Ints... idx) const { |
| 227 |
static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); |
| 228 |
return check_bounds_part<0>(idx...); |
| 229 |
} |
| 230 |
|
| 231 |
template <int Dim, typename Int, typename... Ints> |
| 232 |
void check_bounds_part(Int idx, Ints... next_idx) const { |
| 233 |
if (idx_t(idx) >= shape_[Dim]) { |
| 234 |
throw_OutOfRange("IndexView", array_dim<Dim>(), idx, shape_[Dim]); |
| 235 |
} |
| 236 |
check_bounds_part<Dim + 1>(next_idx...); |
| 237 |
} |
| 238 |
|
| 239 |
template <int Dim, typename Int> |
| 240 |
void check_bounds_part(Int last_idx) const { |
| 241 |
if (idx_t(last_idx) >= shape_[Dim]) { |
| 242 |
throw_OutOfRange("IndexView", array_dim<Dim>(), last_idx, shape_[Dim]); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
idx_t size() const { return shape_[0]; } |
| 247 |
|
| 248 |
void dump(std::ostream& os) const; |
| 249 |
|
| 250 |
private: |
| 251 |
Value* data_; |
| 252 |
idx_t strides_[Rank]; |
| 253 |
idx_t shape_[Rank]; |
| 254 |
}; |
| 255 |
|
| 256 |
#undef INDEX_REF |
| 257 |
#undef FROM_FORTRAN |
| 258 |
#undef TO_FORTRAN |
| 259 |
|
| 260 |
//------------------------------------------------------------------------------------------------------ |
| 261 |
|
| 262 |
} // namespace array |
| 263 |
} // namespace atlas |