Debian Codegraph @

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