Debian Codegraph @

src/atlas/field/FieldSet.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 /// @file FieldSet.h
12 /// @author Willem Deconinck
13 /// @author Pedro Maciel
14 /// @date Jan 2015
15
16 #pragma once
17
18 #include <cstdlib>
19 #include <iterator>
20 #include <map>
21 #include <string>
22 #include <type_traits>
23 #include <vector>
24
25 #include "eckit/deprecated.h"
26
27 #include "atlas/array_fwd.h"
28 #include "atlas/field/Field.h"
29 #include "atlas/library/config.h"
30 #include "atlas/runtime/Exception.h"
31 #include "atlas/util/Metadata.h"
32 #include "atlas/util/Object.h"
33 #include "atlas/util/ObjectHandle.h"
34 #include "atlas/field/detail/FieldImpl.h"
35
36 namespace atlas {
37
38 class FieldSet;
39
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41
42 namespace field {
43
44 /**
45 * @brief Represents a set of fields, where order is preserved
46 */
47 class FieldSetImpl : public util::Object {
48 public: // types
49 using iterator = std::vector<Field>::iterator;
50 using const_iterator = std::vector<Field>::const_iterator;
51
52 template <typename T>
53 static constexpr bool is_index() {
54 return std::is_integral<T>::value or std::is_enum<T>::value;
55 }
56
57 template <bool pred>
58 using enable_if_t = typename std::enable_if<pred, int>::type;
59
60 template <typename T>
61 using enable_if_index_t = enable_if_t<is_index<T>()>;
62
63 private:
64
65 class FieldObserver : public field::FieldObserver {
66 public:
67 FieldObserver(FieldSetImpl& fieldset) : fieldset_(fieldset) {}
68
69 private:
70 void onFieldRename(FieldImpl& field) override;
71
72 private:
73 FieldSetImpl& fieldset_;
74 };
75
76
77 public: // methods
78 /// Constructs an empty FieldSet
79 FieldSetImpl(const std::string& name = "untitled");
80 virtual ~FieldSetImpl();
81
82 idx_t size() const { return static_cast<idx_t>(fields_.size()); }
83 bool empty() const { return !fields_.size(); }
84
85 void clear();
86
87 const std::string& name() const { return name_; }
88 std::string& name() { return name_; }
89
90 template <typename Index, enable_if_index_t<Index> = 0>
91 const Field& operator[](Index i) const {
92 return field(i);
93 }
94
95 template <typename Index, enable_if_index_t<Index> = 0>
96 Field& operator[](Index i) {
97 return field(i);
98 }
99
100 const Field& operator[](const std::string& name) const { return field(name); }
101 Field& operator[](const std::string& name) { return field(name); }
102
103 template <typename Index, enable_if_index_t<Index> = 0>
104 const Field& field(Index i) const {
105 if (i >= size())
106 throw_OutOfRange("fieldset", i, size(), Here());
107 return fields_[i];
108 }
109
110 template <typename Index, enable_if_index_t<Index> = 0>
111 Field& field(Index i) {
112 if (i >= size())
113 throw_OutOfRange("fieldset", i, size(), Here());
114 return fields_[i];
115 }
116
117 const std::vector<std::string>& field_names() const;
118
119 Field add(const Field&);
120 void add(const FieldSet&);
121
122 bool has(const std::string& name) const;
123
124 Field& field(const std::string& name) const;
125
126 iterator begin() { return fields_.begin(); }
127 iterator end() { return fields_.end(); }
128 const_iterator begin() const { return fields_.begin(); }
129 const_iterator end() const { return fields_.end(); }
130 const_iterator cbegin() const { return fields_.begin(); }
131 const_iterator cend() const { return fields_.end(); }
132
133 const util::Metadata& metadata() const { return metadata_; }
134 util::Metadata& metadata() { return metadata_; }
135
136 void haloExchange(bool on_device = false) const;
137 void adjointHaloExchange(bool on_device = false) const;
138 void set_dirty(bool = true) const;
139
140 void updateHost() const;
141 void updateHost(std::initializer_list<std::string> names) const;
142 void updateHost(std::initializer_list<int> indices) const;
143 void updateDevice() const;
144 void updateDevice(std::initializer_list<std::string> names) const;
145 void updateDevice(std::initializer_list<int> indices) const;
146 void allocateDevice() const;
147 void allocateDevice(std::initializer_list<std::string> names) const;
148 void allocateDevice(std::initializer_list<int> indices) const;
149 void deallocateDevice() const;
150 void deallocateDevice(std::initializer_list<std::string> names) const;
151 void deallocateDevice(std::initializer_list<int> indices) const;
152 void setDeviceNeedsUpdate(bool) const;
153 void setDeviceNeedsUpdate(bool, std::initializer_list<std::string> names) const;
154 void setDeviceNeedsUpdate(bool, std::initializer_list<int> indices) const;
155 void setHostNeedsUpdate(bool) const;
156 void setHostNeedsUpdate(bool, std::initializer_list<std::string> names) const;
157 void setHostNeedsUpdate(bool, std::initializer_list<int> indices) const;
158 void setDeviceNeedsUpdate() const;
159 void setDeviceNeedsUpdate(std::initializer_list<std::string> names) const;
160 void setDeviceNeedsUpdate(std::initializer_list<int> indices) const;
161 void setHostNeedsUpdate() const;
162 void setHostNeedsUpdate(std::initializer_list<std::string> names) const;
163 void setHostNeedsUpdate(std::initializer_list<int> indices) const;
164 void syncHost() const;
165 void syncHost(std::initializer_list<std::string> names) const;
166 void syncHost(std::initializer_list<int> indices) const;
167 void syncDevice() const;
168 void syncDevice(std::initializer_list<std::string> names) const;
169 void syncDevice(std::initializer_list<int> indices) const;
170
171
172
173 protected: // data
174 std::vector<Field> fields_; ///< field storage
175 std::string name_; ///< internal name
176 util::Metadata metadata_; ///< metadata associated with the FieldSet
177 std::map<std::string, idx_t> index_; ///< name-to-index map, to refer fields by name
178 std::vector<std::string> field_names_; ///< field names
179 std::map<std::string, idx_t> duplicates_; ///< name-to-duplicates map, to refer fields by name
180
181 friend class FieldObserver;
182 FieldObserver field_observer_;
183 };
184
185 // C wrapper interfaces to C++ routines
186 extern "C" {
187 FieldSetImpl* atlas__FieldSet__new(char* name);
188 void atlas__FieldSet__delete(FieldSetImpl* This);
189 void atlas__FieldSet__add_field(FieldSetImpl* This, FieldImpl* field);
190 void atlas__FieldSet__add_fieldset(FieldSetImpl* This, FieldSetImpl* fieldset);
191 int atlas__FieldSet__has_field(const FieldSetImpl* This, char* name);
192 const char* atlas__FieldSet__name(FieldSetImpl* This);
193 idx_t atlas__FieldSet__size(const FieldSetImpl* This);
194 FieldImpl* atlas__FieldSet__field_by_name(FieldSetImpl* This, char* name);
195 FieldImpl* atlas__FieldSet__field_by_idx(FieldSetImpl* This, idx_t idx);
196 void atlas__FieldSet__data_int_specf(FieldSetImpl* This, char* name, int*& field_data, int& rank, int*& field_shapef,
197 int*& field_stridesf);
198 void atlas__FieldSet__data_long_specf(FieldSetImpl* This, char* name, long*& field_data, int& rank, int*& field_shapef,
199 int*& field_stridesf);
200 void atlas__FieldSet__data_float_specf(FieldSetImpl* This, char* name, float*& field_data, int& rank, int*& field_shapef,
201 int*& field_stridesf);
202 void atlas__FieldSet__data_double_specf(FieldSetImpl* This, char* name, double*& field_data, int& rank, int*& field_shapef,
203 int*& field_stridesf);
204 void atlas__FieldSet__data_int_specf_by_idx(FieldSetImpl* This, int& idx, int*& field_data, int& rank, int*& field_shapef,
205 int*& field_stridesf);
206 void atlas__FieldSet__data_long_specf_by_idx(FieldSetImpl* This, int& idx, long*& field_data, int& rank, int*& field_shapef,
207 int*& field_stridesf);
208 void atlas__FieldSet__data_float_specf_by_idx(FieldSetImpl* This, int& idx, float*& field_data, int& rank, int*& field_shapef,
209 int*& field_stridesf);
210 void atlas__FieldSet__data_double_specf_by_idx(FieldSetImpl* This, int& idx, double*& field_data, int& rank, int*& field_shapef,
211 int*& field_stridesf);
212 void atlas__FieldSet__set_dirty(FieldSetImpl* This, int value);
213 void atlas__FieldSet__halo_exchange(FieldSetImpl* This, int on_device);
214 }
215
216 } // namespace field
217 #endif
218
219 //---------------------------------------------------------------------------------------------------------------------
220
221 /**
222 * @brief Represents a set of fields, where order is preserved
223 */
224 class FieldSet : DOXYGEN_HIDE(public util::ObjectHandle<field::FieldSetImpl>) {
225 public: // types
226 using iterator = Implementation::iterator;
227 using const_iterator = Implementation::const_iterator;
228
229 template <typename T>
230 using enable_if_index_t = Implementation::enable_if_index_t<T>;
231
232 public: // methods
233 using Handle::Handle;
234 FieldSet();
235 FieldSet(const std::string& name);
236 FieldSet(const Field&);
237
238 FieldSet clone(const eckit::Parametrisation& config = util::Config()) const;
239
240 idx_t size() const { return get()->size(); }
241 bool empty() const { return get()->empty(); }
242
243 void clear() { get()->clear(); }
244
245 const std::string& name() const { return get()->name(); }
246 std::string& name() { return get()->name(); }
247
248 template <typename Index, enable_if_index_t<Index> = 0>
249 const Field& operator[](Index i) const {
250 return get()->operator[](i);
251 }
252
253 template <typename Index, enable_if_index_t<Index> = 0>
254 Field& operator[](Index i) {
255 return get()->operator[](i);
256 }
257
258 const Field& operator[](const std::string& name) const { return get()->operator[](name); }
259 Field& operator[](const std::string& name) { return get()->operator[](name); }
260
261 const Field& operator[](const char* name) const { return get()->operator[](name); }
262 Field& operator[](const char* name) { return get()->operator[](name); }
263
264 template <typename Index, enable_if_index_t<Index> = 0>
265 const Field& field(Index i) const {
266 return get()->field(i);
267 }
268
269 template <typename Index, enable_if_index_t<Index> = 0>
270 Field& field(Index i) {
271 return get()->field(i);
272 }
273
274 const std::vector<std::string>& field_names() const { return get()->field_names(); }
275
276 Field add(const Field& field) { return get()->add(field); }
277 void add(const FieldSet& fieldset) { return get()->add(fieldset); }
278
279 bool has(const std::string& name) const { return get()->has(name); }
280
281 Field& field(const std::string& name) const { return get()->field(name); }
282
283 iterator begin() { return get()->begin(); }
284 iterator end() { return get()->end(); }
285 const_iterator begin() const { return get()->begin(); }
286 const_iterator end() const { return get()->end(); }
287 const_iterator cbegin() const { return get()->begin(); }
288 const_iterator cend() const { return get()->end(); }
289
290 const util::Metadata& metadata() const;
291 util::Metadata& metadata();
292
293 void set_dirty(bool = true) const;
294
295 void haloExchange(bool on_device = false) const { get()->haloExchange(on_device); }
296 void adjointHaloExchange(bool on_device = false) const { get()->adjointHaloExchange(on_device); }
297
298 // -- Methods related to host-device synchronisation
299 void updateHost() const { get()->updateHost(); }
300 void updateHost(std::initializer_list<std::string> names) const { get()->updateHost(names); }
301 void updateHost(std::initializer_list<int> indices) const { get()->updateHost(indices); }
302 void updateDevice() const { get()->updateDevice(); }
303 void updateDevice(std::initializer_list<std::string> names) const { get()->updateDevice(names); }
304 void updateDevice(std::initializer_list<int> indices) const{ get()->updateDevice(indices); }
305 void allocateDevice() const { get()->allocateDevice(); }
306 void allocateDevice(std::initializer_list<std::string> names) const { get()->allocateDevice(names); }
307 void allocateDevice(std::initializer_list<int> indices) const { get()->allocateDevice(indices); }
308 void deallocateDevice() const { get()->deallocateDevice(); }
309 void deallocateDevice(std::initializer_list<std::string> names) const { get()->deallocateDevice(names); }
310 void deallocateDevice(std::initializer_list<int> indices) const { get()->deallocateDevice(indices); }
311 void setDeviceNeedsUpdate(bool value) const { get()->setDeviceNeedsUpdate(value); }
312 void setDeviceNeedsUpdate(bool value, std::initializer_list<std::string> names) const { get()->setDeviceNeedsUpdate(value, names); }
313 void setDeviceNeedsUpdate(bool value, std::initializer_list<int> indices) const { get()->setDeviceNeedsUpdate(value, indices); }
314 void setHostNeedsUpdate(bool value) const { get()->setHostNeedsUpdate(value); }
315 void setHostNeedsUpdate(bool value, std::initializer_list<std::string> names) const { get()->setHostNeedsUpdate(value, names); }
316 void setHostNeedsUpdate(bool value, std::initializer_list<int> indices) const { get()->setHostNeedsUpdate(value, indices); }
317 void syncHost() const { get()->syncHost(); }
318 void syncHost(std::initializer_list<std::string> names) const { get()->syncHost(names); }
319 void syncHost(std::initializer_list<int> indices) const { get()->syncHost(indices); }
320 void syncDevice() const { get()->syncDevice(); }
321 void syncDevice(std::initializer_list<std::string> names) const { get()->syncDevice(names); }
322 void syncDevice(std::initializer_list<int> indices) const { get()->syncDevice(indices); }
323
324 // Deprecated API
325 DEPRECATED("use 'has' instead") bool has_field(const std::string& name) const { return get()->has(name); }
326 };
327
328 } // namespace atlas