Debian Codegraph @

src/atlas/array/gridtools/GridToolsArrayHelpers.h

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 #pragma once
12
13 #include <array>
14 #include <type_traits>
15 #include <utility>
16 #include <vector>
17
18 #include "atlas/array.h"
19 #include "atlas/array/ArrayDataStore.h"
20 #include "atlas/array/DataType.h"
21 #include "atlas/array/gridtools/GridToolsTraits.h"
22 #include "atlas/array_fwd.h"
23 #include "atlas/library/config.h"
24 #include "atlas/runtime/Exception.h"
25 #include "atlas/runtime/Log.h"
26
27 //------------------------------------------------------------------------------
28
29 namespace atlas {
30 namespace array {
31 namespace gridtools {
32
33 template <unsigned int Rank>
34 std::array<idx_t, Rank> get_array_from_vector(std::vector<idx_t> const& values) {
35 std::array<idx_t, Rank> array;
36 std::copy(values.begin(), values.end(), array.begin());
37 return array;
38 }
39
40 template <unsigned int TotalDims, unsigned int Dim, typename = void>
41 struct check_dimension_lengths_impl {
42 template <typename FirstDim, typename... Dims>
43 static void apply(ArrayShape const& shape, FirstDim first_dim, Dims... d) {
44 if (first_dim < shape[Dim]) {
45 std::stringstream err;
46 err << "Attempt to resize array with original size for dimension " << Dim << " of " << shape[Dim] << " by "
47 << first_dim << std::endl;
48 throw_Exception(err.str(), Here());
49 }
50 check_dimension_lengths_impl<TotalDims, Dim + 1>::apply(shape, d...);
51 }
52 };
53
54 template <unsigned int TotalDims, unsigned int Dim>
55 struct check_dimension_lengths_impl<TotalDims, Dim,
56 typename std::enable_if<(Dim == TotalDims - 1) && (TotalDims > 1)>::type> {
57 template <typename FirstDim>
58 static void apply(ArrayShape const& shape, FirstDim first_dim) {
59 if (first_dim < shape[Dim]) {
60 std::stringstream err;
61 err << "Attempt to resize array with original size for dimension " << Dim - 1 << " of " << shape[Dim - 1]
62 << " by " << first_dim << std::endl;
63 throw_Exception(err.str(), Here());
64 }
65 }
66 };
67
68 template <unsigned int TotalDims, unsigned int Dim>
69 struct check_dimension_lengths_impl<TotalDims, Dim,
70 typename std::enable_if<(Dim == TotalDims - 1) && (TotalDims == 1)>::type> {
71 template <typename FirstDim>
72 static void apply(ArrayShape const& shape, FirstDim first_dim) {
73 if (first_dim < shape[Dim]) {
74 std::stringstream err;
75 err << "Attempt to resize array with original size for dimension " << Dim << " of " << shape[Dim] << " by "
76 << first_dim << std::endl;
77 throw_Exception(err.str(), Here());
78 }
79 }
80 };
81
82 template <typename... Dims>
83 void check_dimension_lengths(ArrayShape const& shape, Dims... d) {
84 check_dimension_lengths_impl<sizeof...(d), 0>::apply(shape, d...);
85 }
86
87 template <unsigned int Rank>
88 struct default_layout_t {
89 template <typename T>
90 struct get_layout;
91
92 template <typename UInt, UInt... Indices>
93 struct get_layout<std::integer_sequence<UInt, Indices...>> {
94 using type = ::gridtools::layout_map<Indices...>;
95 };
96
97 using type = typename get_layout<std::make_integer_sequence<::gridtools::uint_t, Rank>>::type;
98 };
99
100
101 template <typename Value, typename LayoutMap>
102 struct get_layout_map_component {
103 // TODO: static_assert( ::gridtools::is_layout_map<LayoutMap>(), "Error: not a
104 // layout_map" );
105 template <int Idx>
106 struct get_component {
107 ATLAS_HOST_DEVICE
108 constexpr get_component() {}
109
110 ATLAS_HOST_DEVICE constexpr static Value apply() { return LayoutMap::template at<Idx>(); }
111 };
112 };
113
114 template <typename Value>
115 struct get_stride_component {
116 template <int Idx>
117 struct get_component {
118 ATLAS_HOST_DEVICE
119 constexpr get_component() {}
120
121 template <typename StorageInfoPtr>
122 ATLAS_HOST_DEVICE constexpr static Value apply(StorageInfoPtr a) {
123 static_assert((::gridtools::is_storage_info<typename std::remove_pointer<StorageInfoPtr>::type>::value),
124 "Error: not a storage_info");
125 return a->template stride<Idx>();
126 }
127 };
128 };
129
130 template <typename Value>
131 struct get_shape_component {
132 template <int Idx>
133 struct get_component {
134 ATLAS_HOST_DEVICE
135 constexpr get_component() {}
136
137 template <typename StorageInfoPtr>
138 ATLAS_HOST_DEVICE constexpr static Value apply(StorageInfoPtr a) {
139 static_assert((::gridtools::is_storage_info<typename std::remove_pointer<StorageInfoPtr>::type>::value),
140 "Error: not a storage_info");
141 return a->template total_length<Idx>();
142 }
143 };
144 };
145
146 // indirection around C++11 sizeof... since it is buggy for nvcc and cray
147 template <typename... T>
148 struct get_pack_size {
149 using type = ::gridtools::static_uint<sizeof...(T)>;
150 };
151
152 template <typename Value, typename LayoutMap, typename Alignment, size_t Rank>
153 struct gt_storage_t {
154 using type =
155 gridtools::storage_traits::data_store_t<Value, gridtools::storage_traits::custom_layout_storage_info_align_t<
156 0, LayoutMap, ::gridtools::zero_halo<Rank>, Alignment>>;
157 };
158
159 template <typename Value, typename LayoutMap, typename Alignment, typename... UInts>
160 typename gt_storage_t<Value, LayoutMap, Alignment, get_pack_size<UInts...>::type::value>::type* create_gt_storage(
161 UInts... dims) {
162 static_assert((sizeof...(dims) > 0), "Error: can not create storages without any dimension");
163 constexpr static unsigned int rank = get_pack_size<UInts...>::type::value;
164 typedef gridtools::storage_traits::custom_layout_storage_info_align_t<0, LayoutMap, ::gridtools::zero_halo<rank>,
165 Alignment>
166 storage_info_ty;
167 typedef gridtools::storage_traits::data_store_t<Value, storage_info_ty> data_store_t;
168
169 data_store_t* ds;
170 if (::gridtools::accumulate(::gridtools::multiplies(), dims...) == 0) {
171 ds = new data_store_t();
172 }
173 else {
174 storage_info_ty si(dims...);
175 ds = new data_store_t(si);
176 }
177 return ds;
178 }
179
180 template <typename Value, typename LayoutMap, typename... UInts>
181 typename gt_storage_t<Value, LayoutMap, ::gridtools::alignment<1>, get_pack_size<UInts...>::type::value>::type*
182 create_gt_storage(UInts... dims) {
183 return create_gt_storage<Value, LayoutMap, ::gridtools::alignment<1>>(dims...);
184 }
185
186
187 template <typename Value, size_t Rank>
188 struct gt_wrap_storage_t {
189 using type = gridtools::storage_traits::data_store_t<Value, gridtools::storage_traits::storage_info_t<0, Rank>>;
190 };
191
192 template <typename Value, unsigned int Rank>
193 static typename gt_wrap_storage_t<Value, Rank>::type* wrap_gt_storage(Value* data, std::array<idx_t, Rank>&& shape,
194 std::array<idx_t, Rank>&& strides) {
195 static_assert((Rank > 0), "Error: can not create storages without any dimension");
196 typedef gridtools::storage_traits::storage_info_t<0, Rank, ::gridtools::zero_halo<Rank>> storage_info_ty;
197 typedef gridtools::storage_traits::data_store_t<Value, storage_info_ty> data_store_t;
198 ::gridtools::array<::gridtools::uint_t, Rank> _shape;
199 ::gridtools::array<::gridtools::uint_t, Rank> _strides;
200 for (unsigned int i = 0; i < Rank; ++i) {
201 _shape[i] = shape[i];
202 _strides[i] = strides[i];
203 }
204 storage_info_ty si(_shape, _strides);
205 data_store_t* ds = new data_store_t(si, data);
206
207 return ds;
208 }
209
210 constexpr idx_t zero(idx_t) {
211 return 0;
212 }
213
214 template <idx_t... Is>
215 ArrayStrides make_null_strides(std::integer_sequence<idx_t, Is...>) {
216 return make_strides(zero(Is)...);
217 }
218
219 template <typename UInt>
220 struct my_apply_gt_integer_sequence {
221 template <typename Container, template <UInt T> class Lambda, typename... ExtraTypes>
222 ATLAS_HOST_DEVICE static constexpr Container apply(ExtraTypes const&... args_) {
223 static_assert((std::is_same<Container, Container>::value),
224 "ERROR: my_apply_gt_integer_sequence only accepts a "
225 "std::integer_sequence type. Check the call");
226 return Container(args_...);
227 }
228 };
229
230 template <typename UInt, UInt... Indices>
231 struct my_apply_gt_integer_sequence<std::integer_sequence<UInt, Indices...>> {
232 /**
233 @brief duplicated interface for the case in which the container is an
234 aggregator
235 */
236 template <typename Container, template <UInt T> class Lambda, typename... ExtraTypes>
237 ATLAS_HOST_DEVICE static constexpr Container apply(ExtraTypes const&... args_) {
238 return Container{Lambda<Indices>::apply(args_...)...};
239 }
240 };
241
242 #ifndef __CUDACC__
243
244 template <typename DataStore, typename... Dims>
245 ArraySpec ATLAS_HOST make_spec(DataStore* gt_data_store_ptr, Dims... dims) {
246 static_assert((::gridtools::is_data_store<DataStore>::value), "Internal Error: passing a non GT data store");
247
248 if (gt_data_store_ptr->valid()) {
249 auto storage_info_ptr = gt_data_store_ptr->get_storage_info_ptr().get();
250 using Layout = typename DataStore::storage_info_t::layout_t;
251 using Alignment = typename DataStore::storage_info_t::alignment_t;
252
253 using seq = my_apply_gt_integer_sequence<std::make_integer_sequence<int, sizeof...(dims)>>;
254
255 ArraySpec spec(
256 ArrayShape{(idx_t)dims...},
257 seq::template apply<ArrayStrides, get_stride_component<idx_t>::template get_component>(storage_info_ptr),
258 seq::template apply<ArrayLayout, get_layout_map_component<idx_t, Layout>::template get_component>(),
259 ArrayAlignment(Alignment::value));
260 ATLAS_ASSERT(spec.allocatedSize() == storage_info_ptr->padded_total_length());
261 return spec;
262 }
263 else {
264 return ArraySpec(make_shape({dims...}),
265 make_null_strides(std::make_integer_sequence<idx_t, sizeof...(dims)>()));
266 }
267 }
268 #endif
269
270 //------------------------------------------------------------------------------
271
272 } // namespace gridtools
273 } // namespace array
274 } // namespace atlas