| 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 |
|
| 13 |
#include <cassert> |
| 14 |
#include <cstddef> |
| 15 |
#include <cstring> |
| 16 |
#include <type_traits> |
| 17 |
|
| 18 |
#include "atlas/array/Array.h" |
| 19 |
#include "atlas/array/ArrayDataStore.h" |
| 20 |
#include "atlas/array/ArrayViewDefs.h" |
| 21 |
#include "atlas/array/LocalView.h" |
| 22 |
#include "atlas/array/gridtools/GridToolsMakeView.h" |
| 23 |
#include "atlas/array/gridtools/GridToolsTraits.h" |
| 24 |
#include "atlas/library/config.h" |
| 25 |
|
| 26 |
//------------------------------------------------------------------------------------------------------ |
| 27 |
|
| 28 |
namespace atlas { |
| 29 |
namespace array { |
| 30 |
|
| 31 |
template <typename Value, int Rank> |
| 32 |
class ArrayView { |
| 33 |
template <typename T> |
| 34 |
using is_non_const_value_type = typename std::is_same<T, typename std::remove_const<Value>::type>; |
| 35 |
|
| 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> |
| 39 |
|
| 40 |
#define ENABLE_IF_CONST_WITH_NON_CONST(T) \ |
| 41 |
template <typename T, typename std::enable_if<(std::is_const<Value>::value && is_non_const_value_type<T>::value), \ |
| 42 |
int>::type* = nullptr> |
| 43 |
|
| 44 |
public: |
| 45 |
// -- Type definitions |
| 46 |
using value_type = Value; |
| 47 |
using non_const_value_type = typename std::remove_const<Value>::type; |
| 48 |
static constexpr bool is_const = std::is_const<Value>::value; |
| 49 |
static constexpr bool is_non_const = !std::is_const<Value>::value; |
| 50 |
|
| 51 |
// static constexpr Intent ACCESS{AccessMode}; |
| 52 |
static constexpr int RANK{Rank}; |
| 53 |
|
| 54 |
|
| 55 |
using data_view_t = gridtools::data_view_tt<Value, Rank, gridtools::get_access_mode<Value>()>; |
| 56 |
|
| 57 |
private: |
| 58 |
using slicer_t = typename helpers::ArraySlicer<ArrayView<Value, Rank>>; |
| 59 |
using const_slicer_t = typename helpers::ArraySlicer<const ArrayView<const Value, Rank>>; |
| 60 |
|
| 61 |
template <typename... Args> |
| 62 |
struct slice_t { |
| 63 |
using type = typename slicer_t::template Slice<Args...>::type; |
| 64 |
}; |
| 65 |
|
| 66 |
template <typename... Args> |
| 67 |
struct const_slice_t { |
| 68 |
using type = typename const_slicer_t::template Slice<Args...>::type; |
| 69 |
}; |
| 70 |
|
| 71 |
public: |
| 72 |
ATLAS_HOST_DEVICE |
| 73 |
ArrayView(const ArrayView& other); |
| 74 |
ArrayView(const Array& array, bool device_view); |
| 75 |
|
| 76 |
ENABLE_IF_CONST_WITH_NON_CONST(value_type) |
| 77 |
ArrayView(const ArrayView<value_type, Rank>& other): |
| 78 |
gt_data_view_(other.is_device_view_ ? gridtools::make_gt_device_view<Value, Rank>(*other.array_) |
| 79 |
: gridtools::make_gt_host_view<Value, Rank>(*other.array_)), |
| 80 |
data_store_orig_(other.data_store_orig_), |
| 81 |
array_(other.array_), |
| 82 |
is_device_view_(other.is_device_view_) { |
| 83 |
std::memcpy(shape_, other.shape_, sizeof(ArrayShape::value_type) * Rank); |
| 84 |
std::memcpy(strides_, other.strides_, sizeof(ArrayStrides::value_type) * Rank); |
| 85 |
size_ = other.size_; |
| 86 |
// TODO: check compatibility |
| 87 |
} |
| 88 |
|
| 89 |
ENABLE_IF_CONST_WITH_NON_CONST(value_type) |
| 90 |
operator const ArrayView<value_type, Rank>&() const { return *(const ArrayView<value_type, Rank>*)(this); } |
| 91 |
|
| 92 |
value_type* data() { return gt_data_view_.data(); } |
| 93 |
value_type const* data() const { return gt_data_view_.data(); } |
| 94 |
|
| 95 |
template <typename... Ints, typename = typename std::enable_if<(sizeof...(Ints) == Rank), int>::type> |
| 96 |
ATLAS_HOST_DEVICE value_type& operator()(Ints... c) { |
| 97 |
assert(sizeof...(Ints) == Rank); |
| 98 |
using common_type = typename std::common_type<Ints...>::type; |
| 99 |
return gt_data_view_(static_cast<common_type>(c)...); |
| 100 |
} |
| 101 |
|
| 102 |
template <typename... Ints, typename = typename std::enable_if<(sizeof...(Ints) == Rank), int>::type> |
| 103 |
ATLAS_HOST_DEVICE value_type const& operator()(Ints... c) const { |
| 104 |
assert(sizeof...(Ints) == Rank); |
| 105 |
using common_type = typename std::common_type<Ints...>::type; |
| 106 |
return gt_data_view_(static_cast<common_type>(c)...); |
| 107 |
} |
| 108 |
|
| 109 |
template <typename Int, bool EnableBool = true> |
| 110 |
ATLAS_HOST_DEVICE typename std::enable_if<(Rank == 1 && EnableBool), const value_type&>::type operator[]( |
| 111 |
Int idx) const { |
| 112 |
return gt_data_view_(idx); |
| 113 |
} |
| 114 |
|
| 115 |
template <typename Int, bool EnableBool = true> |
| 116 |
ATLAS_HOST_DEVICE typename std::enable_if<(Rank == 1 && EnableBool), value_type&>::type operator[](Int idx) { |
| 117 |
return gt_data_view_(idx); |
| 118 |
} |
| 119 |
|
| 120 |
template <unsigned int Dim> |
| 121 |
ATLAS_HOST_DEVICE idx_t shape() const { |
| 122 |
return gt_data_view_.template length<Dim>(); |
| 123 |
} |
| 124 |
|
| 125 |
ATLAS_HOST_DEVICE |
| 126 |
data_view_t& data_view() { return gt_data_view_; } |
| 127 |
ATLAS_HOST_DEVICE |
| 128 |
data_view_t const& data_view() const { return gt_data_view_; } |
| 129 |
|
| 130 |
template <unsigned int Dim> |
| 131 |
ATLAS_HOST_DEVICE idx_t stride() const { |
| 132 |
return gt_data_view_.storage_info().template stride<Dim>(); |
| 133 |
} |
| 134 |
|
| 135 |
static constexpr idx_t rank() { return Rank; } |
| 136 |
|
| 137 |
ATLAS_HOST_DEVICE size_t size() const { return size_; } |
| 138 |
bool valid() const; |
| 139 |
|
| 140 |
bool contiguous() const { return (size_ == size_t(shape_[0]) * size_t(strides_[0]) ? true : false); } |
| 141 |
|
| 142 |
void dump(std::ostream& os) const; |
| 143 |
|
| 144 |
ENABLE_IF_NON_CONST |
| 145 |
void assign(const value_type& value); |
| 146 |
|
| 147 |
ENABLE_IF_NON_CONST |
| 148 |
void assign(const std::initializer_list<value_type>& list); |
| 149 |
|
| 150 |
ENABLE_IF_NON_CONST |
| 151 |
void assign(const ArrayView<Value, Rank>& other); |
| 152 |
|
| 153 |
const idx_t* strides() const { return strides_; } |
| 154 |
|
| 155 |
const idx_t* shape() const { return shape_; } |
| 156 |
|
| 157 |
template <typename Int> |
| 158 |
ATLAS_HOST_DEVICE |
| 159 |
idx_t shape(Int idx) const { |
| 160 |
return shape_[idx]; |
| 161 |
} |
| 162 |
|
| 163 |
template <typename Int> |
| 164 |
ATLAS_HOST_DEVICE |
| 165 |
idx_t stride(Int idx) const { |
| 166 |
return strides_[idx]; |
| 167 |
} |
| 168 |
|
| 169 |
template <typename... Args> |
| 170 |
typename slice_t<Args...>::type slice(Args... args) { |
| 171 |
return slicer_t(*this).apply(args...); |
| 172 |
} |
| 173 |
|
| 174 |
template <typename... Args> |
| 175 |
typename const_slice_t<Args...>::type slice(Args... args) const { |
| 176 |
return const_slicer_t(*this).apply(args...); |
| 177 |
} |
| 178 |
|
| 179 |
bool isDeviceView() const { return is_device_view_; } |
| 180 |
|
| 181 |
// Befriend all template variations |
| 182 |
template <typename friendValue, int friendRank> |
| 183 |
friend class ArrayView; |
| 184 |
|
| 185 |
private: |
| 186 |
data_view_t gt_data_view_; |
| 187 |
idx_t shape_[Rank]; |
| 188 |
idx_t strides_[Rank]; |
| 189 |
size_t size_; |
| 190 |
ArrayDataStore const* data_store_orig_; |
| 191 |
Array const* array_; |
| 192 |
bool is_device_view_{false}; |
| 193 |
#undef ENABLE_IF_NON_CONST |
| 194 |
#undef ENABLE_IF_CONST_WITH_NON_CONST |
| 195 |
}; |
| 196 |
|
| 197 |
//------------------------------------------------------------------------------------------------------ |
| 198 |
|
| 199 |
} // namespace array |
| 200 |
} // namespace atlas |