Debian Codegraph @

src/atlas/array/gridtools/GridToolsArrayView.cc

raw
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 #include "atlas/array/gridtools/GridToolsArrayView.h"
12 #include <cassert>
13 #include "atlas/array/gridtools/GridToolsArrayHelpers.h"
14 #include "atlas/array/helpers/ArrayAssigner.h"
15 #include "atlas/array/helpers/ArrayCopier.h"
16 #include "atlas/array/helpers/ArrayInitializer.h"
17 #include "atlas/array/helpers/ArrayWriter.h"
18 #include "atlas/runtime/Exception.h"
19
20 #define ENABLE_IF_NON_CONST \
21 template <bool EnableBool, typename std::enable_if<(!std::is_const<Value>::value && EnableBool), int>::type*>
22
23 #define ENABLE_IF_CONST \
24 template <bool EnableBool, typename std::enable_if<(std::is_const<Value>::value && EnableBool), int>::type*>
25
26 namespace atlas {
27 namespace array {
28
29
30 template <typename T, size_t Rank>
31 struct host_device_array {
32 ATLAS_HOST_DEVICE host_device_array(std::initializer_list<T> list) {
33 size_t i(0);
34 for (const T v : list) {
35 data_[i++] = v;
36 }
37 }
38 ATLAS_HOST_DEVICE ~host_device_array() {}
39
40 ATLAS_HOST_DEVICE const T* data() const { return data_; }
41
42 T operator[](int i) const { return data_[i]; }
43
44 T data_[Rank];
45 };
46
47
48 template <typename StorageInfo, int Rank, typename IS>
49 struct StoragePropBuilder;
50
51 template <typename StorageInfo, int Rank, std::size_t... I>
52 struct StoragePropBuilder<StorageInfo, Rank, std::integer_sequence<std::size_t, I...>> {
53 static host_device_array<ArrayStrides::value_type, Rank> buildStrides(const StorageInfo& storage_info) {
54 return host_device_array<ArrayStrides::value_type, Rank>{
55 (ArrayStrides::value_type)storage_info.template stride<I>()...};
56 }
57 static host_device_array<ArrayShape::value_type, Rank> buildShapes(const StorageInfo& storage_info) {
58 return host_device_array<ArrayShape::value_type, Rank>{storage_info.template total_length<I>()...};
59 }
60 };
61
62 template <typename Value, int Rank>
63 ArrayView<Value, Rank>::ArrayView(const ArrayView& other):
64 gt_data_view_(other.gt_data_view_),
65 data_store_orig_(other.data_store_orig_),
66 array_(other.array_),
67 is_device_view_(other.is_device_view_) {
68 std::memcpy(shape_, other.shape_, sizeof(ArrayShape::value_type) * Rank);
69 std::memcpy(strides_, other.strides_, sizeof(ArrayStrides::value_type) * Rank);
70 size_ = other.size_;
71 // TODO: check compatibility
72 }
73
74 template <typename Value, int Rank>
75 ArrayView<Value, Rank>::ArrayView(const Array& array, bool _device_view):
76 gt_data_view_(_device_view ? gridtools::make_gt_device_view<Value, Rank>(array)
77 : gridtools::make_gt_host_view<Value, Rank>(array)),
78 data_store_orig_(&array.data_store()),
79 array_(&array),
80 is_device_view_(_device_view) {
81 if (gt_data_view_.valid()) {
82 constexpr static unsigned int ndims = data_view_t::data_store_t::storage_info_t::ndims;
83
84 using storage_info_ty = gridtools::storage_traits::storage_info_t<0, ndims>;
85 using data_store_t = gridtools::storage_traits::data_store_t<value_type, storage_info_ty>;
86
87 auto storage_info_ =
88 *((reinterpret_cast<data_store_t*>(const_cast<void*>(array.storage())))->get_storage_info_ptr());
89
90 auto stridest =
91 StoragePropBuilder<storage_info_ty, Rank, std::make_integer_sequence<std::size_t, Rank>>::buildStrides(
92 storage_info_);
93 auto shapet =
94 StoragePropBuilder<storage_info_ty, Rank, std::make_integer_sequence<std::size_t, Rank>>::buildShapes(
95 storage_info_);
96
97
98 for (int i = 0; i < Rank; ++i) {
99 strides_[i] = stridest[i];
100 shape_[i] = shapet[i];
101 }
102
103 size_ = storage_info_.total_length();
104 }
105 else {
106 std::fill_n(shape_, Rank, 0);
107 std::fill_n(strides_, Rank, 0);
108
109 size_ = 0;
110 }
111 }
112
113 template <typename Value, int Rank>
114 bool ArrayView<Value, Rank>::valid() const {
115 return gt_data_view_.valid() && (&array_->data_store() == data_store_orig_);
116 }
117
118
119 template <typename Value, int Rank>
120 ENABLE_IF_NON_CONST void ArrayView<Value, Rank>::assign(const value_type& value) {
121 helpers::array_assigner<Value, Rank>::apply(*this, value);
122 }
123
124 //------------------------------------------------------------------------------------------------------
125
126 template <typename Value, int Rank>
127 ENABLE_IF_NON_CONST void ArrayView<Value, Rank>::assign(const std::initializer_list<value_type>& list) {
128 helpers::array_assigner<Value, Rank>::apply(*this, list);
129 }
130
131 //------------------------------------------------------------------------------------------------------
132
133
134 template <typename Value, int Rank>
135 ENABLE_IF_NON_CONST void ArrayView<Value, Rank>::assign(const ArrayView& other) {
136 helpers::array_copier<Value, Rank>::apply(other, *this);
137 }
138
139 //------------------------------------------------------------------------------------------------------
140
141 template <typename Value, int Rank>
142 void ArrayView<Value, Rank>::dump(std::ostream& os) const {
143 os << "size: " << size() << " , values: ";
144 os << "[ ";
145 helpers::array_writer::apply(*this, os);
146 os << " ]";
147 }
148
149 //------------------------------------------------------------------------------------------------------
150
151 } // namespace array
152 } // namespace atlas
153
154 //-----------------------------------------------------------------------
155 // Explicit instantiation
156 namespace atlas {
157 namespace array {
158 #define EXPLICIT_TEMPLATE_INSTANTIATION(Rank) \
159 template class ArrayView<int, Rank>; \
160 template class ArrayView<int const, Rank>; \
161 template class ArrayView<long, Rank>; \
162 template class ArrayView<long const, Rank>; \
163 template class ArrayView<long unsigned, Rank>; \
164 template class ArrayView<long unsigned const, Rank>; \
165 template class ArrayView<float, Rank>; \
166 template class ArrayView<float const, Rank>; \
167 template class ArrayView<double, Rank>; \
168 template class ArrayView<double const, Rank>; \
169 \
170 template void ArrayView<int, Rank>::assign<true, nullptr>(int const&); \
171 template void ArrayView<long, Rank>::assign<true, nullptr>(long const&); \
172 template void ArrayView<float, Rank>::assign<true, nullptr>(float const&); \
173 template void ArrayView<double, Rank>::assign<true, nullptr>(double const&); \
174 template void ArrayView<long unsigned, Rank>::assign<true, nullptr>(long unsigned const&); \
175 template void ArrayView<int, Rank>::assign<true, nullptr>(std::initializer_list<int> const&); \
176 template void ArrayView<long, Rank>::assign<true, nullptr>(std::initializer_list<long> const&); \
177 template void ArrayView<float, Rank>::assign<true, nullptr>(std::initializer_list<float> const&); \
178 template void ArrayView<double, Rank>::assign<true, nullptr>(std::initializer_list<double> const&); \
179 template void ArrayView<long unsigned, Rank>::assign<true, nullptr>(std::initializer_list<long unsigned> const&); \
180 template void ArrayView<int, Rank>::assign<true, nullptr>(ArrayView<int, Rank> const&); \
181 template void ArrayView<long, Rank>::assign<true, nullptr>(ArrayView<long, Rank> const&); \
182 template void ArrayView<float, Rank>::assign<true, nullptr>(ArrayView<float, Rank> const&); \
183 template void ArrayView<double, Rank>::assign<true, nullptr>(ArrayView<double, Rank> const&);
184
185
186 // For each Rank in [1..9]
187 EXPLICIT_TEMPLATE_INSTANTIATION(1)
188 EXPLICIT_TEMPLATE_INSTANTIATION(2)
189 EXPLICIT_TEMPLATE_INSTANTIATION(3)
190 EXPLICIT_TEMPLATE_INSTANTIATION(4)
191 EXPLICIT_TEMPLATE_INSTANTIATION(5)
192 EXPLICIT_TEMPLATE_INSTANTIATION(6)
193 EXPLICIT_TEMPLATE_INSTANTIATION(7)
194 EXPLICIT_TEMPLATE_INSTANTIATION(8)
195 EXPLICIT_TEMPLATE_INSTANTIATION(9)
196
197 #undef EXPLICIT_TEMPLATE_INSTANTIATION
198 } // namespace array
199 } // namespace atlas