Debian Codegraph @

src/atlas/array/Array.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 <memory>
14 #include <vector>
15
16 #include "atlas/library/config.h"
17
18 #include "atlas/util/Object.h"
19
20 #include "atlas/array/ArrayDataStore.h"
21 #include "atlas/array/DataType.h"
22 #include "atlas/array_fwd.h"
23
24 namespace atlas {
25 namespace array {
26
27 // --------------------------------------------------------------------------------------------
28 // Forward declarations
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 template <typename Value>
31 class ArrayT;
32 template <typename Value>
33 class ArrayT_impl;
34 #endif
35
36 // --------------------------------------------------------------------------------------------
37
38 class Array : public util::Object {
39 public:
40 Array() = default;
41 virtual ~Array();
42
43 static Array* create(array::DataType, const ArrayShape&);
44
45 static Array* create(array::DataType, const ArrayShape&, const ArrayLayout&);
46
47 static Array* create(array::DataType, ArraySpec&&);
48
49 static Array* create(ArraySpec&&);
50
51 virtual size_t footprint() const = 0;
52
53 template <typename Value>
54 static Array* create(idx_t size0);
55 template <typename Value>
56 static Array* create(idx_t size0, idx_t size1);
57 template <typename Value>
58 static Array* create(idx_t size0, idx_t size1, idx_t size2);
59 template <typename Value>
60 static Array* create(idx_t size0, idx_t size1, idx_t size2, idx_t size3);
61 template <typename Value>
62 static Array* create(idx_t size0, idx_t size1, idx_t size2, idx_t size3, idx_t size4);
63
64 template <typename Value>
65 static Array* create(const ArrayShape& shape);
66
67 template <typename Value>
68 static Array* create(const ArrayShape& shape, const ArrayLayout& layout);
69
70 template <typename Value>
71 static Array* wrap(Value* data, const ArrayShape& shape);
72
73 template <typename Value>
74 static Array* wrap(Value* data, const ArraySpec& spec);
75
76 idx_t bytes() const { return datatype().size() * spec().allocatedSize(); }
77
78 size_t size() const { return spec_.size(); }
79
80 idx_t rank() const { return spec_.rank(); }
81
82 idx_t stride(idx_t i) const { return spec_.strides()[i]; }
83
84 idx_t shape(idx_t i) const { return spec_.shape()[i]; }
85
86 const ArrayStrides& strides() const { return spec_.strides(); }
87
88 const ArrayStrides& device_strides() const { return spec_.device_strides(); }
89
90 const ArrayShape& shape() const { return spec_.shape(); }
91
92 const std::vector<int>& shapef() const { return spec_.shapef(); }
93
94 const std::vector<int>& stridesf() const { return spec_.stridesf(); }
95
96 const std::vector<int>& device_stridesf() const { return spec_.device_stridesf(); }
97
98 bool contiguous() const { return spec_.contiguous(); }
99
100 bool hasDefaultLayout() const { return spec_.hasDefaultLayout(); }
101
102 virtual array::DataType datatype() const = 0;
103
104 virtual void resize(const ArrayShape& shape) = 0;
105
106 virtual void resize(idx_t size0) = 0;
107 virtual void resize(idx_t size0, idx_t size1) = 0;
108 virtual void resize(idx_t size0, idx_t size1, idx_t size2) = 0;
109 virtual void resize(idx_t size0, idx_t size1, idx_t size2, idx_t size3) = 0;
110 virtual void resize(idx_t size0, idx_t size1, idx_t size2, idx_t size3, idx_t size4) = 0;
111
112 virtual void insert(idx_t idx1, idx_t size1) = 0;
113
114 virtual void dump(std::ostream& os) const = 0;
115
116 virtual void accMap() const = 0;
117 virtual void accUnmap() const = 0;
118 virtual bool accMapped() const = 0;
119
120 virtual void* storage() { return data_store_->voidDataStore(); }
121
122 virtual const void* storage() const { return data_store_->voidDataStore(); }
123
124 bool valid() const { return data_store_->valid(); }
125
126 void updateDevice() const { data_store_->updateDevice(); }
127
128 void updateHost() const { data_store_->updateHost(); }
129
130 void syncHostDevice() const { data_store_->syncHostDevice(); }
131
132 void syncHost() const { data_store_->syncHost(); }
133
134 void syncDevice() const { data_store_->syncDevice(); }
135
136 bool hostNeedsUpdate() const { return data_store_->hostNeedsUpdate(); }
137
138 bool deviceNeedsUpdate() const { return data_store_->deviceNeedsUpdate(); }
139
140 void setHostNeedsUpdate(bool v) const { return data_store_->setHostNeedsUpdate(v); }
141
142 void setDeviceNeedsUpdate(bool v) const { return data_store_->setDeviceNeedsUpdate(v); }
143
144 bool deviceAllocated() const { return data_store_->deviceAllocated(); }
145
146 void allocateDevice() { data_store_->allocateDevice(); }
147
148 void deallocateDevice() { data_store_->deallocateDevice(); }
149
150 void reactivateDeviceWriteViews() const { data_store_->reactivateDeviceWriteViews(); }
151
152 void reactivateHostWriteViews() const { data_store_->reactivateHostWriteViews(); }
153
154 const ArraySpec& spec() const { return spec_; }
155
156 struct CopyPolicy {
157 enum class Execution {
158 SERIAL=0,
159 OMP=1
160 };
161 bool on_device = false;
162 Execution execution {Execution::SERIAL};
163 };
164 virtual void copy(const Array&, const CopyPolicy&) = 0;
165 void copy(const Array& other) { return copy(other,CopyPolicy{}); }
166
167 // -- dangerous methods... You're on your own interpreting the raw data
168 template <typename DATATYPE>
169 DATATYPE const* host_data() const {
170 return data_store_->hostData<DATATYPE>();
171 }
172 template <typename DATATYPE>
173 DATATYPE* host_data() {
174 return data_store_->hostData<DATATYPE>();
175 }
176 template <typename DATATYPE>
177 DATATYPE const* device_data() const {
178 return data_store_->deviceData<DATATYPE>();
179 }
180 template <typename DATATYPE>
181 DATATYPE* device_data() {
182 return data_store_->deviceData<DATATYPE>();
183 }
184 template <typename DATATYPE>
185 DATATYPE const* data() const {
186 return data_store_->hostData<DATATYPE>();
187 }
188 template <typename DATATYPE>
189 DATATYPE* data() {
190 return data_store_->hostData<DATATYPE>();
191 }
192 void const* data() const { return data<void>(); }
193 void* data() { return data<void>(); }
194
195 const ArrayDataStore& data_store() const { return *data_store_; }
196
197 protected:
198 Array(ArraySpec&& spec): spec_(std::move(spec)) {}
199 ArraySpec spec_;
200 std::unique_ptr<ArrayDataStore> data_store_;
201
202 void replace(Array& array) {
203 data_store_.swap(array.data_store_);
204 spec_ = array.spec_;
205 }
206 };
207
208 // --------------------------------------------------------------------------------------------
209
210 template <typename Value>
211 class ArrayT : public Array {
212 public:
213 ArrayT(idx_t size0);
214 ArrayT(idx_t size0, idx_t size1);
215 ArrayT(idx_t size0, idx_t size1, idx_t size2);
216 ArrayT(idx_t size0, idx_t size1, idx_t size2, idx_t size3);
217 ArrayT(idx_t size0, idx_t size1, idx_t size2, idx_t size3, idx_t size4);
218
219 ArrayT(ArraySpec&&);
220
221 ArrayT(const ArrayShape&);
222
223 ArrayT(const ArrayShape&, const ArrayAlignment&);
224
225 ArrayT(const ArrayShape&, const ArrayLayout&);
226
227 virtual void insert(idx_t idx1, idx_t size1);
228
229 virtual void resize(const ArrayShape&);
230
231 virtual void resize(idx_t size0);
232 virtual void resize(idx_t size0, idx_t size1);
233 virtual void resize(idx_t size0, idx_t size1, idx_t size2);
234 virtual void resize(idx_t size0, idx_t size1, idx_t size2, idx_t size3);
235 virtual void resize(idx_t size0, idx_t size1, idx_t size2, idx_t size3, idx_t size4);
236
237 virtual void copy(const Array&, const CopyPolicy&);
238
239 virtual array::DataType datatype() const { return array::DataType::create<Value>(); }
240
241 virtual void dump(std::ostream& os) const;
242
243 // This constructor is used through the Array::create() or the Array::wrap()
244 // methods
245 ArrayT(ArrayDataStore*, const ArraySpec&);
246
247 virtual size_t footprint() const;
248
249 virtual void accMap() const;
250 virtual void accUnmap() const;
251 virtual bool accMapped() const;
252
253 using Array::host_data;
254 using Array::device_data;
255 using Array::data;
256
257 Value const* host_data() const {
258 return data_store_->hostData<Value>();
259 }
260 Value* host_data() {
261 return data_store_->hostData<Value>();
262 }
263 Value const* device_data() const {
264 return data_store_->deviceData<Value>();
265 }
266 Value* device_data() {
267 return data_store_->deviceData<Value>();
268 }
269 Value const* data() const {
270 return data_store_->hostData<Value>();
271 }
272 Value* data() {
273 return data_store_->hostData<Value>();
274 }
275 private:
276 template <typename T>
277 friend class ArrayT_impl;
278 };
279
280 extern template class ArrayT<float>;
281 extern template class ArrayT<double>;
282 extern template class ArrayT<int>;
283 extern template class ArrayT<long>;
284
285 } // namespace array
286 } // namespace atlas