Debian Codegraph @

src/atlas/field/FieldSet.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 <sstream>
12
13 #include "atlas/field/Field.h"
14 #include "atlas/field/detail/FieldInterface.h"
15 #include "atlas/field/FieldSet.h"
16 #include "atlas/grid/Grid.h"
17 #include "atlas/runtime/Exception.h"
18
19 namespace atlas {
20 namespace field {
21
22 //------------------------------------------------------------------------------------------------------
23
24 void FieldSetImpl::FieldObserver::onFieldRename(FieldImpl& field) {
25
26 for (idx_t idx=0; idx<fieldset_.size(); ++idx) {
27 if (fieldset_[idx].get() == &field) {
28 std::string old_name = fieldset_.field_names_[idx];
29 std::string name = field.name();
30 if (name.empty()) {
31 std::stringstream ss;
32 ss << fieldset_.name_ << "[" << idx << "]";
33 name = ss.str();
34 }
35 fieldset_.index_.erase(old_name);
36 fieldset_.index_[name] = idx;
37 fieldset_.field_names_[idx] = name;
38
39 auto duplicate_exists = [&](const std::string& _name) {
40 return fieldset_.duplicates_.find(_name) != fieldset_.duplicates_.end();
41 };
42
43 if (duplicate_exists(old_name)) {
44 --fieldset_.duplicates_[old_name];
45 if (fieldset_.duplicates_[old_name] == 1) {
46 std::size_t restored_index = std::find(fieldset_.field_names_.begin(), fieldset_.field_names_.end(), old_name) - fieldset_.field_names_.begin();
47 fieldset_.index_[old_name] = restored_index;
48 }
49 }
50 if (duplicate_exists(name)) {
51 ++fieldset_.duplicates_[name];
52 }
53 else {
54 fieldset_.duplicates_[name] = 1;
55 }
56 return;
57 }
58 }
59 }
60
61
62 FieldSetImpl::FieldSetImpl(const std::string& /*name*/): name_(), field_observer_(*this) {}
63 FieldSetImpl::~FieldSetImpl() {
64 clear();
65 }
66
67 void FieldSetImpl::clear() {
68 for( auto& field : fields_ ) {
69 field->detachObserver(field_observer_);
70 }
71 index_.clear();
72 fields_.clear();
73 field_names_.clear();
74 duplicates_.clear();
75 }
76
77 Field FieldSetImpl::add(const Field& field) {
78
79 auto update_duplicates = [&](const std::string& name) {
80 if (duplicates_.find(name) != duplicates_.end()) {
81 ++duplicates_[name];
82 }
83 else {
84 duplicates_[name] = 1;
85 }
86 };
87
88 std::string name;
89 if (field.name().size()) {
90 name = field.name();
91 }
92 else {
93 std::stringstream name_ss;
94 name_ss << name_ << "[" << size() << "]";
95 name = name_ss.str();
96 }
97 index_[name] = size();
98 fields_.push_back(field);
99 field_names_.push_back(name);
100 update_duplicates(name);
101 field.get()->attachObserver(field_observer_);
102 return field;
103 }
104
105 void FieldSetImpl::add(const FieldSet& fieldset) {
106 for( auto& field : fieldset) {
107 add(field);
108 }
109 }
110
111 bool FieldSetImpl::has(const std::string& name) const {
112 return index_.count(name);
113 }
114
115
116 Field& FieldSetImpl::field(const std::string& name) const {
117 if (!has(name)) {
118 std::stringstream msg;
119 msg << "FieldSet" << (name_.length() ? " \"" + name_ + "\"" : "") << ": cannot find field \"" << name << "\"";
120 throw_Exception(msg.str(), Here());
121 }
122 if (duplicates_.at(name) > 1) {
123 std::stringstream msg;
124 msg << "FieldSet" << (name_.length() ? " \"" + name_ + "\"" : "") << ": cannot get field with ambiguous name \n" << name << "\". "
125 << duplicates_.at(name) << " fields are registered with same name. Access field by index or iterator instead.";
126 throw_Exception(msg.str(), Here());
127
128 }
129 return const_cast<Field&>(fields_[index_.at(name)]);
130 }
131
132 void FieldSetImpl::haloExchange(bool on_device) const {
133 for (idx_t i = 0; i < size(); ++i) {
134 field(i).haloExchange(on_device);
135 }
136 }
137
138 void FieldSetImpl::adjointHaloExchange(bool on_device) const {
139 for (idx_t i = 0; i < size(); ++i) {
140 field(i).adjointHaloExchange(on_device);
141 }
142 }
143
144 void FieldSetImpl::set_dirty(bool value) const {
145 for (idx_t i = 0; i < size(); ++i) {
146 field(i).set_dirty(value);
147 }
148 }
149
150 const std::vector<std::string>& FieldSetImpl::field_names() const {
151 return field_names_;
152 }
153
154 void FieldSetImpl::updateHost() const {
155 for (auto field: *this) {
156 field.updateHost();
157 }
158 }
159
160 void FieldSetImpl::updateHost(std::initializer_list<std::string> fnames) const {
161 ATLAS_ASSERT(fnames.size() > 0);
162 for (int i = 0; i < fnames.size(); ++i) {
163 auto field = this->field(fnames.begin()[i]);
164 field.updateHost();
165 }
166 }
167
168 void FieldSetImpl::updateHost(std::initializer_list<int> findices) const {
169 ATLAS_ASSERT(findices.size() > 0);
170 for (int i = 0; i < findices.size(); ++i) {
171 auto field = this->field(findices.begin()[i]);
172 field.updateHost();
173 }
174 }
175
176 void FieldSetImpl::updateDevice() const {
177 for (auto field: *this) {
178 field.updateDevice();
179 }
180 }
181
182 void FieldSetImpl::updateDevice(std::initializer_list<std::string> fnames) const {
183 ATLAS_ASSERT(fnames.size() > 0);
184 for (int i = 0; i < fnames.size(); ++i) {
185 auto field = this->field(fnames.begin()[i]);
186 field.updateDevice();
187 }
188 }
189
190 void FieldSetImpl::updateDevice(std::initializer_list<int> findices) const {
191 ATLAS_ASSERT(findices.size() > 0);
192 for (int i = 0; i < findices.size(); ++i) {
193 auto field = this->field(findices.begin()[i]);
194 field.updateDevice();
195 }
196 }
197
198 void FieldSetImpl::allocateDevice() const {
199 for (auto field: *this) {
200 field.allocateDevice();
201 }
202 }
203
204 void FieldSetImpl::allocateDevice(std::initializer_list<std::string> fnames) const {
205 ATLAS_ASSERT(fnames.size() > 0);
206 for (int i = 0; i < fnames.size(); ++i) {
207 auto field = this->field(fnames.begin()[i]);
208 field.allocateDevice();
209 }
210 }
211
212 void FieldSetImpl::allocateDevice(std::initializer_list<int> findices) const {
213 ATLAS_ASSERT(findices.size() > 0);
214 for (int i = 0; i < findices.size(); ++i) {
215 auto field = this->field(findices.begin()[i]);
216 field.allocateDevice();
217 }
218 }
219
220 void FieldSetImpl::deallocateDevice() const {
221 for (auto field: *this) {
222 field.deallocateDevice();
223 }
224 }
225
226 void FieldSetImpl::deallocateDevice(std::initializer_list<std::string> fnames) const {
227 ATLAS_ASSERT(fnames.size() > 0);
228 for (int i = 0; i < fnames.size(); ++i) {
229 auto field = this->field(fnames.begin()[i]);
230 field.deallocateDevice();
231 }
232 }
233
234 void FieldSetImpl::deallocateDevice(std::initializer_list<int> findices) const {
235 ATLAS_ASSERT(findices.size() > 0);
236 for (int i = 0; i < findices.size(); ++i) {
237 auto field = this->field(findices.begin()[i]);
238 field.deallocateDevice();
239 }
240 }
241
242 void FieldSetImpl::setDeviceNeedsUpdate(bool value) const {
243 for (auto field: *this) {
244 field.setDeviceNeedsUpdate(value);
245 }
246 }
247
248 void FieldSetImpl::setDeviceNeedsUpdate(bool value, std::initializer_list<std::string> fnames) const {
249 ATLAS_ASSERT(fnames.size() > 0);
250 for (int i = 0; i < fnames.size(); ++i) {
251 auto field = this->field(fnames.begin()[i]);
252 field.setDeviceNeedsUpdate(true);
253 }
254 }
255
256 void FieldSetImpl::setDeviceNeedsUpdate(bool value, std::initializer_list<int> findices) const {
257 ATLAS_ASSERT(findices.size() > 0);
258 for (int i = 0; i < findices.size(); ++i) {
259 auto field = this->field(findices.begin()[i]);
260 field.setDeviceNeedsUpdate(value);
261 }
262 }
263
264 void FieldSetImpl::setDeviceNeedsUpdate() const {
265 for (auto field: *this) {
266 field.setDeviceNeedsUpdate();
267 }
268 }
269
270 void FieldSetImpl::setDeviceNeedsUpdate(std::initializer_list<std::string> fnames) const {
271 ATLAS_ASSERT(fnames.size() > 0);
272 for (int i = 0; i < fnames.size(); ++i) {
273 auto field = this->field(fnames.begin()[i]);
274 field.setDeviceNeedsUpdate();
275 }
276 }
277
278 void FieldSetImpl::setDeviceNeedsUpdate(std::initializer_list<int> findices) const {
279 ATLAS_ASSERT(findices.size() > 0);
280 for (int i = 0; i < findices.size(); ++i) {
281 auto field = this->field(findices.begin()[i]);
282 field.setDeviceNeedsUpdate();
283 }
284 }
285
286 void FieldSetImpl::setHostNeedsUpdate(bool value) const {
287 for (auto field: *this) {
288 field.setHostNeedsUpdate(value);
289 }
290 }
291
292 void FieldSetImpl::setHostNeedsUpdate(bool value, std::initializer_list<std::string> fnames) const {
293 ATLAS_ASSERT(fnames.size() > 0);
294 for (int i = 0; i < fnames.size(); ++i) {
295 auto field = this->field(fnames.begin()[i]);
296 field.setHostNeedsUpdate(value);
297 }
298 }
299
300 void FieldSetImpl::setHostNeedsUpdate(bool value, std::initializer_list<int> findices) const {
301 ATLAS_ASSERT(findices.size() > 0);
302 for (int i = 0; i < findices.size(); ++i) {
303 auto field = this->field(findices.begin()[i]);
304 field.setHostNeedsUpdate(value);
305 }
306 }
307
308 void FieldSetImpl::setHostNeedsUpdate() const {
309 for (auto field: *this) {
310 field.setHostNeedsUpdate();
311 }
312 }
313
314 void FieldSetImpl::setHostNeedsUpdate(std::initializer_list<std::string> fnames) const {
315 ATLAS_ASSERT(fnames.size() > 0);
316 for (int i = 0; i < fnames.size(); ++i) {
317 auto field = this->field(fnames.begin()[i]);
318 field.setHostNeedsUpdate();
319 }
320 }
321
322 void FieldSetImpl::setHostNeedsUpdate(std::initializer_list<int> findices) const {
323 ATLAS_ASSERT(findices.size() > 0);
324 for (int i = 0; i < findices.size(); ++i) {
325 auto field = this->field(findices.begin()[i]);
326 field.setHostNeedsUpdate();
327 }
328 }
329
330 void FieldSetImpl::syncHost() const {
331 for (auto field: *this) {
332 field.syncHost();
333 }
334 }
335
336 void FieldSetImpl::syncHost(std::initializer_list<std::string> fnames) const {
337 ATLAS_ASSERT(fnames.size() > 0);
338 for (int i = 0; i < fnames.size(); ++i) {
339 auto field = this->field(fnames.begin()[i]);
340 field.syncHost();
341 }
342 }
343
344 void FieldSetImpl::syncHost(std::initializer_list<int> findices) const {
345 ATLAS_ASSERT(findices.size() > 0);
346 for (int i = 0; i < findices.size(); ++i) {
347 auto field = this->field(findices.begin()[i]);
348 field.syncHost();
349 }
350 }
351
352 void FieldSetImpl::syncDevice() const {
353 for (auto field: *this) {
354 field.syncDevice();
355 }
356 }
357
358 void FieldSetImpl::syncDevice(std::initializer_list<std::string> fnames) const {
359 ATLAS_ASSERT(fnames.size() > 0);
360 for (int i = 0; i < fnames.size(); ++i) {
361 auto field = this->field(fnames.begin()[i]);
362 field.syncDevice();
363 }
364 }
365
366 void FieldSetImpl::syncDevice(std::initializer_list<int> findices) const {
367 ATLAS_ASSERT(findices.size() > 0);
368 for (int i = 0; i < findices.size(); ++i) {
369 auto field = this->field(findices.begin()[i]);
370 field.syncDevice();
371 }
372 }
373
374 //-----------------------------------------------------------------------------
375 // C wrapper interfaces to C++ routines
376 extern "C" {
377
378 void atlas__FieldSet__data_int_specf(FieldSetImpl* This, char* name, int*& data, int& rank, int*& shapef, int*& stridesf) {
379 atlas__Field__data_int_specf(This->field(name).get(), data, rank, shapef, stridesf);
380 }
381
382 void atlas__FieldSet__data_long_specf(FieldSetImpl* This, char* name, long*& data, int& rank, int*& shapef, int*& stridesf) {
383 atlas__Field__data_long_specf(This->field(name).get(), data, rank, shapef, stridesf);
384 }
385
386 void atlas__FieldSet__data_float_specf(FieldSetImpl* This, char* name, float*& data, int& rank, int*& shapef, int*& stridesf) {
387 atlas__Field__data_float_specf(This->field(name).get(), data, rank, shapef, stridesf);
388 }
389
390 void atlas__FieldSet__data_double_specf(FieldSetImpl* This, char* name, double*& data, int& rank, int*& shapef, int*& stridesf) {
391 atlas__Field__data_double_specf(This->field(name).get(), data, rank, shapef, stridesf);
392 }
393
394 void atlas__FieldSet__data_int_specf_by_idx(FieldSetImpl* This, int& idx, int*& data, int& rank, int*& shapef, int*& stridesf) {
395 atlas__Field__data_int_specf(This->operator[](idx).get(), data, rank, shapef, stridesf);
396 }
397
398 void atlas__FieldSet__data_long_specf_by_idx(FieldSetImpl* This, int& idx, long*& data, int& rank, int*& shapef, int*& stridesf) {
399 atlas__Field__data_long_specf(This->operator[](idx).get(), data, rank, shapef, stridesf);
400 }
401
402 void atlas__FieldSet__data_float_specf_by_idx(FieldSetImpl* This, int& idx, float*& data, int& rank, int*& shapef, int*& stridesf) {
403 atlas__Field__data_float_specf(This->operator[](idx).get(), data, rank, shapef, stridesf);
404 }
405
406 void atlas__FieldSet__data_double_specf_by_idx(FieldSetImpl* This, int& idx, double*& data, int& rank, int*& shapef, int*& stridesf) {
407 atlas__Field__data_double_specf(This->operator[](idx).get(), data, rank, shapef, stridesf);
408 }
409
410
411 FieldSetImpl* atlas__FieldSet__new(char* name) {
412 FieldSetImpl* fset = new FieldSetImpl(std::string(name));
413 fset->name() = name;
414 return fset;
415 }
416
417 void atlas__FieldSet__delete(FieldSetImpl* This) {
418 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
419 delete This;
420 }
421
422 const char* atlas__FieldSet__name(FieldSetImpl* This) {
423 ATLAS_ASSERT(This != nullptr, "Cannot access name of uninitialised atlas_FieldSet");
424 return This->name().c_str();
425 }
426
427 void atlas__FieldSet__add_field(FieldSetImpl* This, FieldImpl* field) {
428 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
429 ATLAS_ASSERT(field != nullptr, "Reason: Use of uninitialised atlas_Field");
430 This->add(field);
431 }
432
433 void atlas__FieldSet__add_fieldset(FieldSetImpl* This, FieldSetImpl* fieldset) {
434 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
435 ATLAS_ASSERT(fieldset != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
436 This->add(fieldset);
437 }
438
439 int atlas__FieldSet__has_field(const FieldSetImpl* This, char* name) {
440 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
441 return This->has(std::string(name));
442 }
443
444 idx_t atlas__FieldSet__size(const FieldSetImpl* This) {
445 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
446 return This->size();
447 }
448
449 FieldImpl* atlas__FieldSet__field_by_name(FieldSetImpl* This, char* name) {
450 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
451 return This->field(std::string(name)).get();
452 }
453
454 FieldImpl* atlas__FieldSet__field_by_idx(FieldSetImpl* This, idx_t idx) {
455 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
456 return This->operator[](idx).get();
457 }
458
459 void atlas__FieldSet__set_dirty(FieldSetImpl* This, int value) {
460 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
461 This->set_dirty(value);
462 }
463
464 void atlas__FieldSet__halo_exchange(FieldSetImpl* This, int on_device) {
465 ATLAS_ASSERT(This != nullptr, "Reason: Use of uninitialised atlas_FieldSet");
466 This->haloExchange(on_device);
467 }
468 }
469 //-----------------------------------------------------------------------------
470
471 } // namespace field
472
473 //------------------------------------------------------------------------------------------------------
474
475 FieldSet::FieldSet(): Handle(new Implementation()) {}
476
477 FieldSet::FieldSet(const std::string& name): Handle(new Implementation(name)) {}
478
479 FieldSet::FieldSet(const Field& field): Handle(new Implementation()) {
480 get()->add(field);
481 }
482
483 const util::Metadata& FieldSet::metadata() const {
484 return get()->metadata();
485 }
486
487 util::Metadata& FieldSet::metadata() {
488 return get()->metadata();
489 }
490
491 FieldSet FieldSet::clone(const eckit::Parametrisation& config) const {
492 FieldSet fset;
493 for (idx_t jj = 0; jj < size(); ++jj) {
494 fset.add(field(jj).clone(config));
495 }
496 fset.metadata() = metadata();
497 return fset;
498 }
499
500 void FieldSet::set_dirty(bool value) const {
501 get()->set_dirty(value);
502 }
503
504 //------------------------------------------------------------------------------------------------------
505
506 } // namespace atlas