| 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 |
/// @file LocalView.h |
| 12 |
/// This file contains the LocalView class, a class that allows to wrap any |
| 13 |
/// contiguous raw data into |
| 14 |
/// a view which is accessible with multiple indices. |
| 15 |
|
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include <cstddef> |
| 19 |
#include <type_traits> |
| 20 |
#include <array> |
| 21 |
|
| 22 |
#include "atlas/array/ArrayDataStore.h" |
| 23 |
#include "atlas/array/ArrayViewDefs.h" |
| 24 |
#include "atlas/array/helpers/ArraySlicer.h" |
| 25 |
#include "atlas/library/config.h" |
| 26 |
#include "atlas/mdspan.h" |
| 27 |
|
| 28 |
//------------------------------------------------------------------------------------------------------ |
| 29 |
|
| 30 |
namespace atlas { |
| 31 |
namespace array { |
| 32 |
|
| 33 |
/// @brief Multi-dimensional access existing POD array pointer. |
| 34 |
/// |
| 35 |
/// A LocalView is a wrapper around data that enables multidimensional access, and has the exact |
| 36 |
/// same API as ArrayView. |
| 37 |
/// |
| 38 |
/// The data may be strided. |
| 39 |
/// |
| 40 |
/// ### Example 1: |
| 41 |
/// |
| 42 |
/// @code{.cpp} |
| 43 |
/// int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 44 |
/// int[2] strides = { 3, 1 }; |
| 45 |
/// int[2] shape = { 3, 3 }; |
| 46 |
/// LocalView<int,2> matrix( array, shape, strides ); |
| 47 |
/// for( idx_t i=0; i<matrix.shape(0); ++i ) { |
| 48 |
/// for( idx_t j=0; j<matrix.shape(1); ++j ) { |
| 49 |
/// matrix(i,j) *= 10; |
| 50 |
/// } |
| 51 |
/// } |
| 52 |
/// @endcode |
| 53 |
/// |
| 54 |
/// Strides can also be omitted as for most common cases it can be inferred |
| 55 |
/// from the shape. |
| 56 |
/// |
| 57 |
/// ### Example 2: |
| 58 |
/// |
| 59 |
/// @code{.cpp} |
| 60 |
/// int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 61 |
/// int[2] shape = { 3, 3 }; |
| 62 |
/// LocalView<int,2> matrix( array, shape ); |
| 63 |
/// which is identical for this matrix to previous Example 1 |
| 64 |
/// @endcode |
| 65 |
|
| 66 |
|
| 67 |
template <typename Value, int Rank> |
| 68 |
class LocalView { |
| 69 |
template <typename T> |
| 70 |
using is_non_const_value_type = typename std::is_same<T, typename std::remove_const<Value>::type>; |
| 71 |
|
| 72 |
#define ENABLE_IF_NON_CONST \ |
| 73 |
template <bool EnableBool = true, \ |
| 74 |
typename std::enable_if<(!std::is_const<Value>::value && EnableBool), int>::type* = nullptr> |
| 75 |
|
| 76 |
#define ENABLE_IF_CONST_WITH_NON_CONST(T) \ |
| 77 |
template <typename T, typename std::enable_if<(std::is_const<Value>::value && is_non_const_value_type<T>::value), \ |
| 78 |
int>::type* = nullptr> |
| 79 |
|
| 80 |
|
| 81 |
public: |
| 82 |
// -- Type definitions |
| 83 |
using value_type = Value; |
| 84 |
using return_type = value_type; |
| 85 |
|
| 86 |
static constexpr int RANK{Rank}; |
| 87 |
|
| 88 |
private: |
| 89 |
using slicer_t = typename helpers::ArraySlicer<LocalView<Value, Rank>>; |
| 90 |
using const_slicer_t = typename helpers::ArraySlicer<const LocalView<const Value, Rank>>; |
| 91 |
|
| 92 |
template <typename... Args> |
| 93 |
struct slice_t { |
| 94 |
using type = typename slicer_t::template Slice<Args...>::type; |
| 95 |
}; |
| 96 |
|
| 97 |
template <typename... Args> |
| 98 |
struct const_slice_t { |
| 99 |
using type = typename const_slicer_t::template Slice<Args...>::type; |
| 100 |
}; |
| 101 |
|
| 102 |
using mdspan_extents_type = dextents<size_t,RANK>; |
| 103 |
using mdspan_strides_type = std::array<size_t,RANK>; |
| 104 |
using mdspan_type = mdspan<value_type, mdspan_extents_type, layout_stride>; |
| 105 |
using const_mdspan_type = mdspan<const value_type, mdspan_extents_type, layout_stride>; |
| 106 |
|
| 107 |
public: |
| 108 |
// -- Constructors |
| 109 |
|
| 110 |
|
| 111 |
template <typename ValueTp, typename = std::enable_if_t<std::is_convertible_v<ValueTp*, Value*>>> |
| 112 |
LocalView(const LocalView<ValueTp,Rank>& other): data_(other.data_), size_(other.size_), shape_(other.shape_), strides_(other.strides_) {} |
| 113 |
|
| 114 |
template <typename ValueTp, typename Int1, typename Int2, typename = std::enable_if_t<std::is_convertible_v<ValueTp*, Value*> && std::is_integral_v<Int1> && std::is_integral_v<Int2>>> |
| 115 |
LocalView(ValueTp* data, const Int1 shape[], const Int2 strides[]): data_(data) { |
| 116 |
size_ = 1; |
| 117 |
for (idx_t j = 0; j < Rank; ++j) { |
| 118 |
shape_[j] = shape[j]; |
| 119 |
strides_[j] = strides[j]; |
| 120 |
size_ *= shape_[j]; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
template <typename ValueTp, typename Int, typename = std::enable_if_t<std::is_convertible_v<ValueTp*, Value*> && std::is_integral_v<Int>>> |
| 125 |
LocalView(ValueTp* data, const Int shape[]): data_(data) { |
| 126 |
size_ = 1; |
| 127 |
for (int j = Rank - 1; j >= 0; --j) { |
| 128 |
shape_[j] = shape[j]; |
| 129 |
strides_[j] = size_; |
| 130 |
size_ *= shape_[j]; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
template <typename ValueTp, typename ArrayShape, typename = std::enable_if_t<std::is_convertible_v<ValueTp*, Value*>>> |
| 135 |
LocalView(ValueTp* data, const ArrayShape& shape) : LocalView(data,shape.data()) {} |
| 136 |
|
| 137 |
|
| 138 |
template <typename T, typename E, typename L, typename A, typename = std::enable_if_t<std::is_convertible_v<typename A::data_handle_type, Value*> && E::rank() == Rank>> |
| 139 |
LocalView(mdspan<T,E,L,A>& other) : |
| 140 |
data_(other.data_handle()), size_(other.size()) { |
| 141 |
for (int j = 0; j < Rank; ++j) { |
| 142 |
shape_[j] = other.extent(j); |
| 143 |
strides_[j] = other.stride(j); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
ENABLE_IF_CONST_WITH_NON_CONST(value_type) |
| 148 |
operator const LocalView<value_type, Rank>&() const { |
| 149 |
static_assert(std::is_const<Value>::value, "must be const"); |
| 150 |
static_assert(!std::is_const<value_type>::value, "must be non-const"); |
| 151 |
return (const LocalView<value_type, Rank>&)(*this); |
| 152 |
} |
| 153 |
|
| 154 |
const_mdspan_type as_mdspan() const { |
| 155 |
return const_mdspan_type{this->data(), {mdspan_extents(), mdspan_strides()}}; |
| 156 |
} |
| 157 |
|
| 158 |
mdspan_type as_mdspan() { |
| 159 |
return mdspan_type{this->data(), {mdspan_extents(), mdspan_strides()}}; |
| 160 |
} |
| 161 |
|
| 162 |
// -- Access methods |
| 163 |
|
| 164 |
template <typename... Idx, int Rank_ = Rank, typename = std::enable_if_t<sizeof...(Idx) == Rank_>> |
| 165 |
value_type& operator()(Idx... idx) { |
| 166 |
check_bounds(idx...); |
| 167 |
return data_[index(idx...)]; |
| 168 |
} |
| 169 |
|
| 170 |
template <typename... Idx, int Rank_ = Rank, typename = std::enable_if_t<sizeof...(Idx) == Rank_>> |
| 171 |
const value_type& operator()(Idx... idx) const { |
| 172 |
check_bounds(idx...); |
| 173 |
return data_[index(idx...)]; |
| 174 |
} |
| 175 |
|
| 176 |
template <typename Idx, int Rank_ = Rank, typename = std::enable_if_t<Rank_ == 1>> |
| 177 |
const value_type& operator[](Idx idx) const { |
| 178 |
check_bounds(idx); |
| 179 |
return data_[index(idx)]; |
| 180 |
} |
| 181 |
|
| 182 |
template <typename Idx, int Rank_ = Rank, typename = std::enable_if_t<Rank_ == 1>> |
| 183 |
value_type& operator[](Idx idx) { |
| 184 |
check_bounds(idx); |
| 185 |
return data_[index(idx)]; |
| 186 |
} |
| 187 |
|
| 188 |
idx_t size() const { return size_; } |
| 189 |
|
| 190 |
template <typename Int> |
| 191 |
idx_t shape(Int idx) const { |
| 192 |
return shape_[idx]; |
| 193 |
} |
| 194 |
|
| 195 |
template <typename Int> |
| 196 |
idx_t stride(Int idx) const { |
| 197 |
return strides_[idx]; |
| 198 |
} |
| 199 |
|
| 200 |
const idx_t* shape() const { return shape_.data(); } |
| 201 |
|
| 202 |
const idx_t* strides() const { return strides_.data(); } |
| 203 |
|
| 204 |
value_type const* data() const { return data_; } |
| 205 |
|
| 206 |
value_type* data() { return data_; } |
| 207 |
|
| 208 |
bool contiguous() const { return (size_ == shape_[0] * strides_[0] ? true : false); } |
| 209 |
|
| 210 |
ENABLE_IF_NON_CONST |
| 211 |
void assign(const value_type& value); |
| 212 |
|
| 213 |
void dump(std::ostream& os) const; |
| 214 |
|
| 215 |
static constexpr idx_t rank() { return Rank; } |
| 216 |
|
| 217 |
template <typename... Args> |
| 218 |
typename slice_t<Args...>::type slice(Args... args) { |
| 219 |
return slicer_t(*this).apply(args...); |
| 220 |
} |
| 221 |
|
| 222 |
template <typename... Args> |
| 223 |
typename const_slice_t<Args...>::type slice(Args... args) const { |
| 224 |
return const_slicer_t(*this).apply(args...); |
| 225 |
} |
| 226 |
|
| 227 |
friend std::ostream& operator<<(std::ostream& out, const LocalView& x) { |
| 228 |
x.dump(out); |
| 229 |
return out; |
| 230 |
} |
| 231 |
|
| 232 |
private: |
| 233 |
// -- Private methods |
| 234 |
|
| 235 |
template <int Dim, typename Int, typename... Ints> |
| 236 |
constexpr idx_t index_part(Int idx, Ints... next_idx) const { |
| 237 |
return idx * strides_[Dim] + index_part<Dim + 1>(next_idx...); |
| 238 |
} |
| 239 |
|
| 240 |
template <int Dim, typename Int> |
| 241 |
constexpr idx_t index_part(Int last_idx) const { |
| 242 |
return last_idx * strides_[Dim]; |
| 243 |
} |
| 244 |
|
| 245 |
template <typename... Ints> |
| 246 |
constexpr idx_t index(Ints... idx) const { |
| 247 |
return index_part<0>(idx...); |
| 248 |
} |
| 249 |
|
| 250 |
#if ATLAS_ARRAYVIEW_BOUNDS_CHECKING |
| 251 |
template <typename... Ints> |
| 252 |
void check_bounds(Ints... idx) const { |
| 253 |
static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); |
| 254 |
return check_bounds_part<0>(idx...); |
| 255 |
} |
| 256 |
#else |
| 257 |
template <typename... Ints> |
| 258 |
void check_bounds(Ints... idx) const { |
| 259 |
static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); |
| 260 |
} |
| 261 |
#endif |
| 262 |
|
| 263 |
template <typename... Ints> |
| 264 |
void check_bounds_force(Ints... idx) const { |
| 265 |
static_assert(sizeof...(idx) == Rank, "Expected number of indices is different from rank of array"); |
| 266 |
return check_bounds_part<0>(idx...); |
| 267 |
} |
| 268 |
|
| 269 |
template <int Dim, typename Int, typename... Ints> |
| 270 |
void check_bounds_part(Int idx, Ints... next_idx) const { |
| 271 |
if (idx_t(idx) >= shape_[Dim]) { |
| 272 |
throw_OutOfRange("LocalView", array_dim<Dim>(), idx, shape_[Dim]); |
| 273 |
} |
| 274 |
check_bounds_part<Dim + 1>(next_idx...); |
| 275 |
} |
| 276 |
|
| 277 |
template <int Dim, typename Int> |
| 278 |
void check_bounds_part(Int last_idx) const { |
| 279 |
if (idx_t(last_idx) >= shape_[Dim]) { |
| 280 |
throw_OutOfRange("LocalView", array_dim<Dim>(), last_idx, shape_[Dim]); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
template<int... i> |
| 285 |
ATLAS_HOST_DEVICE |
| 286 |
mdspan_extents_type _get_mdspan_extents(std::integer_sequence<int, i...> = {}) const { |
| 287 |
return mdspan_extents_type{(shape_[i])...}; |
| 288 |
} |
| 289 |
|
| 290 |
ATLAS_HOST_DEVICE |
| 291 |
mdspan_extents_type mdspan_extents() const { |
| 292 |
return _get_mdspan_extents(std::make_integer_sequence<int,RANK>{}); |
| 293 |
} |
| 294 |
|
| 295 |
template<int... i> |
| 296 |
ATLAS_HOST_DEVICE |
| 297 |
mdspan_strides_type _get_mdspan_strides(std::integer_sequence<int, i...> = {}) const { |
| 298 |
return mdspan_strides_type{(static_cast<typename mdspan_strides_type::value_type>(strides_[i]))...}; |
| 299 |
} |
| 300 |
|
| 301 |
ATLAS_HOST_DEVICE |
| 302 |
mdspan_strides_type mdspan_strides() const { |
| 303 |
return _get_mdspan_strides(std::make_integer_sequence<int,RANK>{}); |
| 304 |
} |
| 305 |
|
| 306 |
private: |
| 307 |
// -- Private data |
| 308 |
template<typename,int> friend class LocalView; |
| 309 |
|
| 310 |
value_type* data_; |
| 311 |
idx_t size_; |
| 312 |
std::array<idx_t,Rank> shape_; |
| 313 |
std::array<idx_t,Rank> strides_; |
| 314 |
|
| 315 |
#undef ENABLE_IF_NON_CONST |
| 316 |
#undef ENABLE_IF_CONST_WITH_NON_CONST |
| 317 |
}; |
| 318 |
|
| 319 |
template<typename Value, int Rank> |
| 320 |
using View = LocalView<Value,Rank>; |
| 321 |
|
| 322 |
} // namespace array |
| 323 |
} // namespace atlas |