Debian Codegraph @

src/atlas/field/Field.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 <iostream>
12 #include <string>
13
14 #include "atlas/library/config.h"
15
16 #include "atlas/field/Field.h"
17 #include "atlas/field/detail/FieldImpl.h"
18 #include "atlas/runtime/Exception.h"
19
20 #if ATLAS_HAVE_FUNCTIONSPACE
21 #include "atlas/functionspace/FunctionSpace.h"
22 #endif
23
24 namespace atlas {
25
26 // ------------------------------------------------------------------
27
28 std::ostream& operator<<(std::ostream& os, const Field& f) {
29 os << (*f.get());
30 return os;
31 }
32
33 Field::Field(const eckit::Parametrisation& config): Handle(Implementation::create(config)) {}
34
35 Field::Field(const std::string& name, array::DataType datatype, const array::ArrayShape& shape):
36 Handle(Implementation::create(name, datatype, shape)) {}
37
38 Field::Field(const std::string& name, array::DataType datatype, array::ArraySpec&& spec):
39 Handle(Implementation::create(name, datatype, std::move(spec))) {}
40
41 Field::Field(const std::string& name, array::Array* array): Handle(Implementation::create(name, array)) {}
42
43 template <typename DATATYPE>
44 Field::Field(const std::string& name, DATATYPE* data, const array::ArraySpec& spec):
45 Handle(Implementation::wrap(name, data, spec)) {}
46
47 template <typename DATATYPE>
48 Field::Field(const std::string& name, DATATYPE* data, const array::ArrayShape& shape):
49 Handle(Implementation::wrap(name, data, shape)) {}
50
51 /// @brief Implicit conversion to Array
52 Field::operator const array::Array&() const {
53 return get()->array();
54 }
55 Field::operator array::Array&() {
56 return get()->array();
57 }
58
59 const array::Array& Field::array() const {
60 return get()->array();
61 }
62 array::Array& Field::array() {
63 return get()->array();
64 }
65
66 /// @brief Clone
67 Field Field::clone(const eckit::Parametrisation& config) const {
68 Field tmp(get()->name(), get()->datatype(), get()->shape());
69 tmp.metadata() = this->metadata();
70 tmp.set_functionspace(this->functionspace());
71 array::Array::CopyPolicy cp;
72 // To be set up via config. For now use default, as Array does not yet implement it.
73 tmp.array().copy(this->array(),cp);
74 return tmp;
75 }
76
77 // -- Accessors
78
79 /// @brief Access to raw data
80 void* Field::storage() {
81 return get()->storage();
82 }
83
84 /// @brief Internal data type of field
85 array::DataType Field::datatype() const {
86 return get()->datatype();
87 }
88
89 /// @brief Name associated to this field
90 const std::string& Field::name() const {
91 return get()->name();
92 }
93
94 /// @brief Rename this field
95 void Field::rename(const std::string& name) {
96 get()->rename(name);
97 }
98
99 /// @brief Access to metadata associated to this field
100 const util::Metadata& Field::metadata() const {
101 return get()->metadata();
102 }
103 util::Metadata& Field::metadata() {
104 return get()->metadata();
105 }
106
107 field::Halo& Field::halo() const {
108 return get()->halo();
109 }
110
111 /// @brief Resize field to given shape
112 void Field::resize(const array::ArrayShape& shape) {
113 get()->resize(shape);
114 }
115
116 void Field::insert(idx_t idx1, idx_t size1) {
117 get()->insert(idx1, size1);
118 }
119
120 /// @brief Shape of this field in Fortran style (reverse order of C style)
121 const std::vector<int>& Field::shapef() const {
122 return get()->shapef();
123 }
124
125 /// @brief Strides of this field in Fortran style (reverse order of C style)
126 const std::vector<int>& Field::stridesf() const {
127 return get()->stridesf();
128 }
129
130 /// @brief Shape of this field (reverse order of Fortran style)
131 const array::ArrayShape& Field::shape() const {
132 return get()->shape();
133 }
134
135 /// @brief Strides of this field
136 const array::ArrayStrides& Field::strides() const {
137 return get()->strides();
138 }
139
140 /// @brief Shape of this field associated to index 'i'
141 idx_t Field::shape(idx_t i) const {
142 return get()->shape(i);
143 }
144
145 /// @brief Stride of this field associated to index 'i'
146 idx_t Field::stride(idx_t i) const {
147 return get()->stride(i);
148 }
149
150 /// @brief Number of values stored in this field
151 size_t Field::size() const {
152 return get()->size();
153 }
154
155 /// @brief Rank of field
156 idx_t Field::rank() const {
157 return get()->rank();
158 }
159
160 /// @brief Number of bytes occupied by the values of this field
161 size_t Field::bytes() const {
162 return get()->bytes();
163 }
164
165 bool Field::contiguous() const {
166 return array().contiguous();
167 }
168
169 /// @brief Output information of field plus raw data
170 void Field::dump(std::ostream& os) const {
171 get()->dump(os);
172 }
173
174 /// Metadata that is more intrinsic to the Field, and queried often
175 void Field::set_levels(idx_t n) {
176 get()->set_levels(n);
177 }
178 idx_t Field::levels() const {
179 return get()->levels();
180 }
181
182 /// Metadata that is more intrinsic to the Field, and queried often
183 void Field::set_variables(idx_t n) {
184 get()->set_variables(n);
185 }
186 idx_t Field::variables() const {
187 return get()->variables();
188 }
189
190 void Field::set_horizontal_dimension(const std::vector<idx_t>& h_dim) {
191 get()->set_horizontal_dimension(h_dim);
192 }
193
194 std::vector<idx_t> Field::horizontal_dimension() const {
195 return get()->horizontal_dimension();
196 }
197
198 void Field::set_functionspace(const FunctionSpace& functionspace) {
199 #if ATLAS_HAVE_FUNCTIONSPACE
200 get()->set_functionspace(functionspace);
201 #else
202 throw_Exception("Atlas has been compiled without FunctionSpace support",Here());
203 #endif
204 }
205 const FunctionSpace& Field::functionspace() const {
206 #if ATLAS_HAVE_FUNCTIONSPACE
207 return get()->functionspace();
208 #else
209 throw_Exception("Atlas has been compiled without FunctionSpace support",Here());
210 #endif
211 }
212
213 /// @brief Return the memory footprint of the Field
214 size_t Field::footprint() const {
215 return get()->footprint();
216 }
217
218 bool Field::dirty() const {
219 return get()->dirty();
220 }
221
222 void Field::set_dirty(bool value) const {
223 get()->set_dirty(value);
224 }
225
226 void Field::haloExchange(bool on_device) const {
227 get()->haloExchange(on_device);
228 }
229
230 void Field::adjointHaloExchange(bool on_device) const {
231 get()->adjointHaloExchange(on_device);
232 }
233
234 // -- Methods related to host-device synchronisation, requires gridtools_storage
235
236 void Field::updateHost() const {
237 get()->updateHost();
238 }
239 void Field::updateDevice() const {
240 get()->updateDevice();
241 }
242 void Field::syncHostDevice() const {
243 get()->syncHostDevice();
244 }
245 void Field::syncHost() const {
246 get()->syncHost();
247 }
248 void Field::syncDevice() const {
249 get()->syncDevice();
250 }
251 bool Field::hostNeedsUpdate() const {
252 return get()->hostNeedsUpdate();
253 }
254 bool Field::deviceNeedsUpdate() const {
255 return get()->deviceNeedsUpdate();
256 }
257 void Field::setHostNeedsUpdate(bool v) const {
258 return get()->setHostNeedsUpdate(v);
259 }
260 void Field::setHostNeedsUpdate() const {
261 return get()->setHostNeedsUpdate(true);
262 }
263 void Field::setDeviceNeedsUpdate(bool v) const {
264 return get()->setDeviceNeedsUpdate(v);
265 }
266 void Field::setDeviceNeedsUpdate() const {
267 return get()->setDeviceNeedsUpdate(true);
268 }
269 bool Field::deviceAllocated() const {
270 return get()->deviceAllocated();
271 }
272 void Field::allocateDevice() {
273 get()->allocateDevice();
274 }
275 void Field::deallocateDevice() {
276 get()->deallocateDevice();
277 }
278 void Field::reactivateDeviceWriteViews() const {
279 get()->reactivateDeviceWriteViews();
280 }
281 void Field::reactivateHostWriteViews() const {
282 get()->reactivateHostWriteViews();
283 }
284
285 // ------------------------------------------------------------------
286
287 template Field::Field(const std::string&, float*, const array::ArraySpec&);
288 template Field::Field(const std::string&, float*, const array::ArrayShape&);
289 template Field::Field(const std::string&, double*, const array::ArraySpec&);
290 template Field::Field(const std::string&, double*, const array::ArrayShape&);
291 template Field::Field(const std::string&, long*, const array::ArraySpec&);
292 template Field::Field(const std::string&, long*, const array::ArrayShape&);
293 template Field::Field(const std::string&, int*, const array::ArraySpec&);
294 template Field::Field(const std::string&, int*, const array::ArrayShape&);
295
296 // ------------------------------------------------------------------
297
298
299 } // namespace atlas