Debian Codegraph @

src/atlas/array/gridtools/GridToolsMakeView.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/GridToolsMakeView.h"
12
13 #include <sstream>
14 #include <vector>
15
16 #include "atlas/array/Array.h"
17 #include "atlas/array/ArrayView.h"
18 #include "atlas/array/ArrayViewDefs.h"
19 #include "atlas/array/IndexView.h"
20 #include "atlas/array/gridtools/GridToolsTraits.h"
21 #include "atlas/library/config.h"
22 #include "atlas/runtime/Exception.h"
23
24 //------------------------------------------------------------------------------
25
26 namespace atlas {
27 namespace array {
28
29 namespace {
30 template <typename Value, unsigned Rank>
31 static void check_metadata(const Array& array) {
32 if (array.rank() != Rank) {
33 std::stringstream err;
34 err << "Number of dimensions do not match: template argument " << Rank << " expected to be " << array.rank();
35 throw_Exception(err.str(), Here());
36 }
37 if (array.datatype() != array::DataType::create<Value>()) {
38 std::stringstream err;
39 err << "Data Type does not match: template argument expected to be " << array.datatype().str();
40 throw_Exception(err.str(), Here());
41 }
42 }
43 } // namespace
44
45 namespace gridtools {
46
47
48 template <typename Value, unsigned int Rank>
49 typename gt_view<Value, Rank>::type make_gt_host_view(const Array& array) {
50 using value_t = typename std::remove_const<Value>::type;
51 using storage_info_ty = storage_traits::storage_info_t<0, Rank>;
52 using data_store_t = storage_traits::data_store_t<value_t, storage_info_ty>;
53
54 data_store_t* ds = reinterpret_cast<data_store_t*>(const_cast<void*>(array.storage()));
55 return ::gridtools::make_host_view<get_access_mode<Value>()>(*ds);
56 }
57
58 template <typename Value, unsigned int Rank>
59 typename gt_view<Value, Rank>::type make_gt_device_view(const Array& array) {
60 using value_t = typename std::remove_const<Value>::type;
61 using storage_info_ty = storage_traits::storage_info_t<0, Rank>;
62 using data_store_t = storage_traits::data_store_t<value_t, storage_info_ty>;
63
64 data_store_t* ds = reinterpret_cast<data_store_t*>(const_cast<void*>(array.storage()));
65 #if ATLAS_GRIDTOOLS_STORAGE_BACKEND_CUDA
66 return ::gridtools::make_device_view<get_access_mode<Value>()>(*ds);
67 #else
68 return ::gridtools::make_host_view<get_access_mode<Value>()>(*ds);
69 #endif
70 }
71
72 } // namespace gridtools
73
74
75 constexpr bool host_view = false;
76 constexpr bool device_view = true;
77
78
79 template <typename Value, int Rank>
80 ArrayView<Value, Rank> make_host_view(Array& array) {
81 check_metadata<Value, Rank>(array);
82 return ArrayView<Value, Rank>(array, host_view);
83 }
84
85 template <typename Value, int Rank>
86 ArrayView<const Value, Rank> make_host_view(const Array& array) {
87 check_metadata<Value, Rank>(array);
88 return ArrayView<const Value, Rank>(array, host_view);
89 }
90
91 template <typename Value, int Rank>
92 ArrayView<Value, Rank> make_device_view(Array& array) {
93 check_metadata<Value, Rank>(array);
94 return ArrayView<Value, Rank>(array, device_view);
95 }
96
97 template <typename Value, int Rank>
98 ArrayView<const Value, Rank> make_device_view(const Array& array) {
99 check_metadata<Value, Rank>(array);
100 return ArrayView<const Value, Rank>(array, device_view);
101 }
102
103 template <typename Value, int Rank>
104 ArrayView<Value, Rank> make_view(Array& array) {
105 return make_host_view<Value, Rank>(array);
106 }
107
108 template <typename Value, int Rank>
109 ArrayView<const Value, Rank> make_view(const Array& array) {
110 return make_host_view<Value, Rank>(array);
111 }
112
113 //template <typename Value, int Rank>
114 //IndexView<Value, Rank> make_host_indexview( Array& array ) {
115 // return IndexView<Value, Rank>( (Value*)( array.storage() ), array.shape().data() );
116 //}
117
118 //template <typename Value, int Rank>
119 //IndexView<const Value, Rank> make_host_indexview( const Array& array ) {
120 // return IndexView<const Value, Rank>( (const Value*)( array.storage() ), array.shape().data() );
121 //}
122
123 //template <typename Value, int Rank>
124 //IndexView<Value, Rank> make_indexview( Array& array ) {
125 // check_metadata<Value, Rank>( array );
126 // return make_host_indexview<Value, Rank>( array );
127 //}
128
129 //template <typename Value, int Rank>
130 //IndexView<const Value, Rank> make_indexview( const Array& array ) {
131 // check_metadata<Value, Rank>( array );
132 // return make_host_indexview<Value, Rank>( array );
133 //}
134
135
136 template <typename Value, int Rank>
137 IndexView<Value, Rank> make_host_indexview(Array& array) {
138 // using value_t = typename std::remove_const<Value>::type;
139 // using storage_info_ty = gridtools::storage_traits::storage_info_t<0, Rank>;
140 // using data_store_t = gridtools::storage_traits::data_store_t<value_t, storage_info_ty>;
141
142 // data_store_t* ds = reinterpret_cast<data_store_t*>(const_cast<void*>(array.storage()));
143
144 // return IndexView<Value, Rank>(::gridtools::make_host_view<gridtools::get_access_mode<Value>()>(*ds));
145 check_metadata<Value, Rank>(array);
146 constexpr bool device_view = false;
147 return IndexView<Value, Rank>(array, device_view);
148 }
149
150 template <typename Value, int Rank>
151 IndexView<const Value, Rank> make_host_indexview(const Array& array) {
152 // using value_t = typename std::remove_const<Value>::type;
153 // using storage_info_ty = gridtools::storage_traits::storage_info_t<0, Rank>;
154 // using data_store_t = gridtools::storage_traits::data_store_t<value_t, storage_info_ty>;
155
156 // data_store_t* ds = reinterpret_cast<data_store_t*>(const_cast<void*>(array.storage()));
157
158 // return IndexView<const Value, Rank>(::gridtools::make_host_view<gridtools::get_access_mode<const Value>()>(*ds));
159 check_metadata<Value, Rank>(array);
160 constexpr bool device_view = false;
161 return IndexView<const Value, Rank>(array, device_view);
162 }
163
164
165 // --------------------------------------------------------------------------------------------
166
167 template <typename Value, int Rank>
168 IndexView<Value, Rank> make_indexview(Array& array) {
169 // check_metadata<Value, Rank>(array);
170 // constexpr bool device_view = false;
171 // return IndexView<cValue, Rank>(array, device_view);
172 return make_host_indexview<Value,Rank>(array);
173
174 }
175
176 template <typename Value, int Rank>
177 IndexView<const Value, Rank> make_indexview(const Array& array) {
178 return make_host_indexview<const Value, Rank>(array);
179 }
180
181 } // namespace array
182 } // namespace atlas
183
184 //-----------------------------------------------------------------------
185 // Explicit instantiation
186 namespace atlas {
187 namespace array {
188
189 //#define EXPLICIT_TEMPLATE_INSTANTIATION( RANK ) \
190 // namespace gridtools { \
191 // template typename gt_view<long unsigned, RANK, Intent::ReadOnly>::type \
192 // make_gt_host_view<long unsigned, RANK, Intent::ReadOnly>( const Array& array ); \
193 // template typename gt_view<long unsigned, RANK, Intent::ReadWrite>::type \
194 // make_gt_host_view<long unsigned, RANK, Intent::ReadWrite>( const Array& array ); \
195 // template typename gt_view<float, RANK, Intent::ReadOnly>::type make_gt_host_view<float, RANK, Intent::ReadOnly>( \
196 // const Array& array ); \
197 // template typename gt_view<float, RANK, Intent::ReadWrite>::type make_gt_host_view<float, RANK, Intent::ReadWrite>( \
198 // const Array& array ); \
199 // template typename gt_view<double, RANK, Intent::ReadOnly>::type make_gt_host_view<double, RANK, Intent::ReadOnly>( \
200 // const Array& array ); \
201 // template typename gt_view<double, RANK, Intent::ReadWrite>::type \
202 // make_gt_host_view<double, RANK, Intent::ReadWrite>( const Array& array ); \
203 // \
204 // template typename gt_view<int, RANK, Intent::ReadOnly>::type make_gt_device_view<int, RANK, Intent::ReadOnly>( \
205 // const Array& array ); \
206 // template typename gt_view<int, RANK, Intent::ReadWrite>::type make_gt_device_view<int, RANK, Intent::ReadWrite>( \
207 // const Array& array ); \
208 // template typename gt_view<long, RANK, Intent::ReadOnly>::type make_gt_device_view<long, RANK, Intent::ReadOnly>( \
209 // const Array& array ); \
210 // template typename gt_view<long, RANK, Intent::ReadWrite>::type make_gt_device_view<long, RANK, Intent::ReadWrite>( \
211 // const Array& array ); \
212 // template typename gt_view<long unsigned, RANK, Intent::ReadOnly>::type \
213 // make_gt_device_view<long unsigned, RANK, Intent::ReadOnly>( const Array& array ); \
214 // template typename gt_view<long unsigned, RANK, Intent::ReadWrite>::type \
215 // make_gt_device_view<long unsigned, RANK, Intent::ReadWrite>( const Array& array ); \
216 // template typename gt_view<float, RANK, Intent::ReadOnly>::type make_gt_device_view<float, RANK, Intent::ReadOnly>( \
217 // const Array& array ); \
218 // template typename gt_view<float, RANK, Intent::ReadWrite>::type \
219 // make_gt_device_view<float, RANK, Intent::ReadWrite>( const Array& array ); \
220 // template typename gt_view<double, RANK, Intent::ReadOnly>::type \
221 // make_gt_device_view<double, RANK, Intent::ReadOnly>( const Array& array ); \
222 // template typename gt_view<double, RANK, Intent::ReadWrite>::type \
223 // make_gt_device_view<double, RANK, Intent::ReadWrite>( const Array& array ); \
224 // }
225
226 //// For each Rank in [1..9]
227 //EXPLICIT_TEMPLATE_INSTANTIATION( 1 )
228 //EXPLICIT_TEMPLATE_INSTANTIATION( 2 )
229 //EXPLICIT_TEMPLATE_INSTANTIATION( 3 )
230 //EXPLICIT_TEMPLATE_INSTANTIATION( 4 )
231 //EXPLICIT_TEMPLATE_INSTANTIATION( 5 )
232 //EXPLICIT_TEMPLATE_INSTANTIATION( 6 )
233 //EXPLICIT_TEMPLATE_INSTANTIATION( 7 )
234 //EXPLICIT_TEMPLATE_INSTANTIATION( 8 )
235 //EXPLICIT_TEMPLATE_INSTANTIATION( 9 )
236
237 //#undef EXPLICIT_TEMPLATE_INSTANTIATION
238
239
240 #define EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(TYPE, RANK) \
241 template ArrayView<TYPE, RANK> make_view<TYPE, RANK>(Array&); \
242 template ArrayView<const TYPE, RANK> make_view<const TYPE, RANK>(Array&); \
243 template ArrayView<const TYPE, RANK> make_view<TYPE, RANK>(const Array&); \
244 template ArrayView<const TYPE, RANK> make_view<const TYPE, RANK>(const Array&); \
245 \
246 template ArrayView<TYPE, RANK> make_host_view<TYPE, RANK>(Array&); \
247 template ArrayView<const TYPE, RANK> make_host_view<const TYPE, RANK>(Array&); \
248 template ArrayView<const TYPE, RANK> make_host_view<TYPE, RANK>(const Array&); \
249 template ArrayView<const TYPE, RANK> make_host_view<const TYPE, RANK>(const Array&); \
250 \
251 template ArrayView<TYPE, RANK> make_device_view<TYPE, RANK>(Array&); \
252 template ArrayView<const TYPE, RANK> make_device_view<const TYPE, RANK>(Array&); \
253 template ArrayView<const TYPE, RANK> make_device_view<TYPE, RANK>(const Array&); \
254 template ArrayView<const TYPE, RANK> make_device_view<const TYPE, RANK>(const Array&); \
255 \
256 namespace gridtools { \
257 template typename gt_view<TYPE, RANK>::type make_gt_host_view<TYPE, RANK>(const Array& array); \
258 template typename gt_view<const TYPE, RANK>::type make_gt_host_view<const TYPE, RANK>(const Array& array); \
259 template typename gt_view<TYPE, RANK>::type make_gt_device_view<TYPE, RANK>(const Array& array); \
260 template typename gt_view<const TYPE, RANK>::type make_gt_device_view<const TYPE, RANK>(const Array& array); \
261 }
262
263
264 #define EXPLICIT_TEMPLATE_INSTATIATION(RANK) \
265 EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(int, RANK) \
266 EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(long, RANK) \
267 EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(float, RANK) \
268 EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(double, RANK) \
269 EXPLICIT_TEMPLATE_INSTANTIATION_TYPE_RANK(unsigned long, RANK)
270
271
272 EXPLICIT_TEMPLATE_INSTATIATION(1)
273 EXPLICIT_TEMPLATE_INSTATIATION(2)
274 EXPLICIT_TEMPLATE_INSTATIATION(3)
275 EXPLICIT_TEMPLATE_INSTATIATION(4)
276 EXPLICIT_TEMPLATE_INSTATIATION(5)
277 EXPLICIT_TEMPLATE_INSTATIATION(6)
278 EXPLICIT_TEMPLATE_INSTATIATION(7)
279 EXPLICIT_TEMPLATE_INSTATIATION(8)
280 EXPLICIT_TEMPLATE_INSTATIATION(9)
281
282 #undef EXPLICIT_TEMPLATE_INSTATIATION_TYPE_RANK
283 #undef EXPLICIT_TEMPLATE_INSTATIATION
284
285 #define EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(TYPE, RANK) \
286 template IndexView<TYPE, RANK> make_host_indexview<TYPE, RANK>(Array&); \
287 template IndexView<const TYPE, RANK> make_host_indexview<const TYPE, RANK>(Array&); \
288 template IndexView<const TYPE, RANK> make_host_indexview<TYPE, RANK>(const Array&); \
289 template IndexView<const TYPE, RANK> make_host_indexview<const TYPE, RANK>(const Array&); \
290 \
291 template IndexView<TYPE, RANK> make_indexview<TYPE, RANK>(Array&); \
292 template IndexView<const TYPE, RANK> make_indexview<const TYPE, RANK>(Array&); \
293 template IndexView<const TYPE, RANK> make_indexview<TYPE, RANK>(const Array&); \
294 template IndexView<const TYPE, RANK> make_indexview<const TYPE, RANK>(const Array&);
295
296 #define EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(RANK) \
297 EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(int, RANK) \
298 EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK(long, RANK)
299
300 EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(1)
301 EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW(2)
302
303 #undef EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW
304 #undef EXPLICIT_TEMPLATE_INSTANTIATION_INDEXVIEW_TYPE_RANK
305
306
307 } // namespace array
308 } // namespace atlas