Debian Codegraph @

src/atlas/util/Metadata.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 "atlas/util/Metadata.h"
12
13 #include <iostream>
14 #include <sstream>
15 #include <stdexcept>
16
17 #include "eckit/log/JSON.h"
18 #include "eckit/parser/JSONParser.h"
19 #include "eckit/utils/Hash.h"
20
21
22 #include "atlas/parallel/mpi/mpi.h"
23 #include "atlas/runtime/Exception.h"
24
25 using std::string;
26
27 namespace atlas {
28 namespace util {
29
30 void Metadata::throw_not_found(const std::string& name) const {
31 std::stringstream msg;
32 msg << "Could not find metadata \"" << name << "\"";
33 throw_Exception(msg.str(), Here());
34 }
35
36 size_t Metadata::footprint() const {
37 // TODO
38 size_t size = sizeof(*this);
39 return size;
40 }
41
42 void Metadata::broadcast() {
43 idx_t root = 0;
44 get("owner", root);
45 broadcast(*this, root);
46 }
47
48 void Metadata::broadcast(idx_t root) {
49 broadcast(*this, root);
50 }
51
52 void Metadata::broadcast(Metadata& dest) {
53 idx_t root = 0;
54 get("owner", root);
55 broadcast(dest, root);
56 }
57
58 void Metadata::broadcast(Metadata& dest, idx_t root) {
59 std::string buffer;
60 int buffer_size{0};
61 if (mpi::rank() == root) {
62 std::stringstream s;
63 eckit::JSON json(s);
64 json.precision(17);
65 json << *this;
66 buffer = s.str();
67 buffer_size = static_cast<int>(buffer.size());
68 }
69
70 ATLAS_TRACE_MPI(BROADCAST) { atlas::mpi::comm().broadcast(buffer_size, root); }
71
72 if (mpi::rank() != root) {
73 buffer.resize(buffer_size);
74 }
75
76 ATLAS_TRACE_MPI(BROADCAST) { mpi::comm().broadcast(buffer.begin(), buffer.end(), root); }
77
78 if (not(&dest == this && mpi::rank() == root)) {
79 std::stringstream s;
80 s << buffer;
81 eckit::JSONParser parser(s);
82 dest = Metadata(parser.parse());
83 }
84 }
85
86 void Metadata::broadcast(Metadata& dest) const {
87 idx_t root = 0;
88 get("owner", root);
89 broadcast(dest, root);
90 }
91
92 void Metadata::broadcast(Metadata& dest, idx_t root) const {
93 std::string buffer;
94 int buffer_size{0};
95 if (mpi::rank() == root) {
96 std::stringstream s;
97 eckit::JSON json(s);
98 json.precision(17);
99 json << *this;
100 buffer = s.str();
101 buffer_size = static_cast<int>(buffer.size());
102 }
103
104 ATLAS_TRACE_MPI(BROADCAST) { mpi::comm().broadcast(buffer_size, root); }
105
106 if (mpi::rank() != root) {
107 buffer.resize(buffer_size);
108 }
109
110 ATLAS_TRACE_MPI(BROADCAST) { mpi::comm().broadcast(buffer.begin(), buffer.end(), root); }
111
112 // Fill in dest
113 {
114 std::stringstream s;
115 s << buffer;
116 eckit::JSONParser parser(s);
117 dest = Metadata(parser.parse());
118 }
119 }
120
121
122 Metadata& Metadata::set(const eckit::Configuration& other) {
123 #if ATLAS_ECKIT_VERSION_AT_LEAST(1, 26, 0) || ATLAS_ECKIT_DEVELOP
124 LocalConfiguration::set(other);
125 #else
126 eckit::Value& root = const_cast<eckit::Value&>(get());
127 auto& other_root = other.get();
128 std::vector<string> other_keys;
129 eckit::fromValue(other_keys, other_root.keys());
130 for (auto& key : other_keys) {
131 root[key] = other_root[key];
132 }
133 #endif
134 return *this;
135 }
136
137 Metadata::Metadata(const eckit::Value& value): eckit::LocalConfiguration(value) {}
138
139 // ------------------------------------------------------------------
140 // C wrapper interfaces to C++ routines
141
142 Metadata* atlas__Metadata__new() {
143 return new Metadata();
144 }
145
146 void atlas__Metadata__delete(Metadata* This) {
147 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
148 delete This;
149 }
150
151 void atlas__Metadata__set_int(Metadata* This, const char* name, int value) {
152 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
153 This->set(std::string(name), long(value));
154 }
155 void atlas__Metadata__set_long(Metadata* This, const char* name, long value) {
156 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
157 This->set(std::string(name), value);
158 }
159 void atlas__Metadata__set_float(Metadata* This, const char* name, float value) {
160 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
161 This->set(std::string(name), double(value));
162 }
163 void atlas__Metadata__set_double(Metadata* This, const char* name, double value) {
164 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
165 This->set(std::string(name), value);
166 }
167 void atlas__Metadata__set_string(Metadata* This, const char* name, const char* value) {
168 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
169 This->set(std::string(name), std::string(value));
170 }
171 void atlas__Metadata__set_array_int(Metadata* This, const char* name, int value[], int size) {
172 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
173 std::vector<int> v;
174 v.assign(value, value + size);
175 This->set(std::string(name), v);
176 }
177 void atlas__Metadata__set_array_long(Metadata* This, const char* name, long value[], int size) {
178 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
179 std::vector<long> v;
180 v.assign(value, value + size);
181 This->set(std::string(name), v);
182 }
183 void atlas__Metadata__set_array_float(Metadata* This, const char* name, float value[], int size) {
184 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
185 std::vector<float> v;
186 v.assign(value, value + size);
187 This->set(std::string(name), v);
188 }
189 void atlas__Metadata__set_array_double(Metadata* This, const char* name, double value[], int size) {
190 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
191 std::vector<double> v;
192 v.assign(value, value + size);
193 This->set(std::string(name), v);
194 }
195 int atlas__Metadata__get_int(Metadata* This, const char* name) {
196 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
197 return This->get<int>(std::string(name));
198 }
199 long atlas__Metadata__get_long(Metadata* This, const char* name) {
200 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
201 return This->get<long>(std::string(name));
202 }
203 float atlas__Metadata__get_float(Metadata* This, const char* name) {
204 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
205 return This->get<float>(std::string(name));
206 }
207 double atlas__Metadata__get_double(Metadata* This, const char* name) {
208 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
209 return This->get<double>(std::string(name));
210 }
211 void atlas__Metadata__get_string(Metadata* This, const char* name, char* output_str, int max_len) {
212 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
213 std::string s = This->get<std::string>(std::string(name));
214 if (s.size() + 1 > size_t(max_len)) {
215 std::stringstream msg;
216 msg << "Cannot copy string `" << s << "` of metadata `" << name
217 << "`"
218 "in buffer of length "
219 << max_len;
220 throw_Exception(msg.str(), Here());
221 }
222 std::strncpy(output_str, s.c_str(), max_len);
223 }
224 void atlas__Metadata__get_array_int(Metadata* This, const char* name, int*& value, int& size, int& allocated) {
225 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
226 std::vector<int> v = This->get<std::vector<int>>(std::string(name));
227 size = static_cast<int>(v.size());
228 value = new int[size];
229 for (size_t j = 0; j < v.size(); ++j) {
230 value[j] = v[j];
231 }
232 allocated = true;
233 }
234 void atlas__Metadata__get_array_long(Metadata* This, const char* name, long*& value, int& size, int& allocated) {
235 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
236 std::vector<long> v = This->get<std::vector<long>>(std::string(name));
237 size = static_cast<int>(v.size());
238 value = new long[size];
239 for (size_t j = 0; j < v.size(); ++j) {
240 value[j] = v[j];
241 }
242 allocated = true;
243 }
244 void atlas__Metadata__get_array_float(Metadata* This, const char* name, float*& value, int& size, int& allocated) {
245 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
246 std::vector<float> v = This->get<std::vector<float>>(std::string(name));
247 size = static_cast<int>(v.size());
248 value = new float[size];
249 for (size_t j = 0; j < v.size(); ++j) {
250 value[j] = v[j];
251 }
252 allocated = true;
253 }
254 void atlas__Metadata__get_array_double(Metadata* This, const char* name, double*& value, int& size, int& allocated) {
255 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
256 std::vector<double> v = This->get<std::vector<double>>(std::string(name));
257 size = static_cast<int>(v.size());
258 value = new double[size];
259 for (size_t j = 0; j < v.size(); ++j) {
260 value[j] = v[j];
261 }
262 allocated = true;
263 }
264
265 int atlas__Metadata__has(Metadata* This, const char* name) {
266 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
267 return This->has(std::string(name));
268 }
269
270 void atlas__Metadata__print(Metadata* This, std::ostream* channel) {
271 ATLAS_ASSERT(This != nullptr, "Cannot access uninitialised atlas_Metadata");
272 ATLAS_ASSERT(channel != nullptr);
273 *channel << *This;
274 }
275
276 void atlas__Metadata__json(Metadata* This, char*& json, int& size, int& allocated) {
277 std::stringstream s;
278 eckit::JSON j(s);
279 j.precision(16);
280 j << *This;
281 std::string json_str = s.str();
282 size = static_cast<int>(json_str.size());
283 json = new char[size + 1];
284 allocated = true;
285 std::strncpy(json, json_str.c_str(), size + 1);
286 allocated = true;
287 }
288
289 // ------------------------------------------------------------------
290
291 } // namespace util
292 } // namespace atlas