|
20 | 20 |
|
21 | 21 | #include "core/multi_simulation/optimization_param.h" |
22 | 22 | #include "core/param/param.h" |
23 | | -#include "core/util/cpptoml.h" |
24 | 23 | #include "core/util/log.h" |
| 24 | +#include "core/util/toml_config.h" |
25 | 25 |
|
26 | 26 | using nlohmann::json; |
27 | 27 |
|
@@ -146,78 +146,67 @@ void Param::MergeJsonPatch(const std::string& patch) { |
146 | 146 | } |
147 | 147 |
|
148 | 148 | // ----------------------------------------------------------------------------- |
149 | | -void AssignThreadSafetyMechanism(const std::shared_ptr<cpptoml::table>& config, |
150 | | - Param* param) { |
151 | | - const std::string config_key = "simulation.thread_safety_mechanism"; |
152 | | - if (config->contains_qualified(config_key)) { |
153 | | - auto value = config->get_qualified_as<std::string>(config_key); |
154 | | - if (!value) { |
155 | | - return; |
156 | | - } |
157 | | - auto str_value = *value; |
158 | | - if (str_value == "none") { |
159 | | - param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kNone; |
160 | | - } else if (str_value == "user-specified") { |
161 | | - param->thread_safety_mechanism = |
162 | | - Param::ThreadSafetyMechanism::kUserSpecified; |
163 | | - } else if (str_value == "automatic") { |
164 | | - param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kAutomatic; |
165 | | - } |
| 149 | +void AssignThreadSafetyMechanism(const TomlConfig& config, Param* param) { |
| 150 | + auto value = |
| 151 | + config.at_path("simulation.thread_safety_mechanism").value<std::string>(); |
| 152 | + if (!value) { |
| 153 | + return; |
| 154 | + } |
| 155 | + auto str_value = *value; |
| 156 | + if (str_value == "none") { |
| 157 | + param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kNone; |
| 158 | + } else if (str_value == "user-specified") { |
| 159 | + param->thread_safety_mechanism = |
| 160 | + Param::ThreadSafetyMechanism::kUserSpecified; |
| 161 | + } else if (str_value == "automatic") { |
| 162 | + param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kAutomatic; |
166 | 163 | } |
167 | 164 | } |
168 | 165 |
|
169 | 166 | // ----------------------------------------------------------------------------- |
170 | | -void AssignMappedDataArrayMode(const std::shared_ptr<cpptoml::table>& config, |
171 | | - Param* param) { |
172 | | - const std::string config_key = "performance.mapped_data_array_mode"; |
173 | | - if (config->contains_qualified(config_key)) { |
174 | | - auto value = config->get_qualified_as<std::string>(config_key); |
175 | | - if (!value) { |
176 | | - return; |
177 | | - } |
178 | | - auto str_value = *value; |
179 | | - if (str_value == "zero-copy") { |
180 | | - param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy; |
181 | | - } else if (str_value == "cache") { |
182 | | - param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache; |
183 | | - } else if (str_value == "copy") { |
184 | | - param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy; |
185 | | - } else { |
186 | | - Log::Fatal( |
187 | | - "Param", |
188 | | - Concat( |
189 | | - "Parameter mapped_data_array_mode was set to an invalid value (", |
190 | | - str_value, ").")); |
191 | | - } |
| 167 | +void AssignMappedDataArrayMode(const TomlConfig& config, Param* param) { |
| 168 | + auto value = |
| 169 | + config.at_path("performance.mapped_data_array_mode").value<std::string>(); |
| 170 | + if (!value) { |
| 171 | + return; |
| 172 | + } |
| 173 | + auto str_value = *value; |
| 174 | + if (str_value == "zero-copy") { |
| 175 | + param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy; |
| 176 | + } else if (str_value == "cache") { |
| 177 | + param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache; |
| 178 | + } else if (str_value == "copy") { |
| 179 | + param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy; |
| 180 | + } else { |
| 181 | + Log::Fatal( |
| 182 | + "Param", |
| 183 | + Concat("Parameter mapped_data_array_mode was set to an invalid value (", |
| 184 | + str_value, ").")); |
192 | 185 | } |
193 | 186 | } |
194 | 187 |
|
195 | 188 | // ----------------------------------------------------------------------------- |
196 | | -void AssignBoundSpaceMode(const std::shared_ptr<cpptoml::table>& config, |
197 | | - Param* param) { |
198 | | - const std::string config_key = "simulation.bound_space"; |
199 | | - if (config->contains_qualified(config_key)) { |
200 | | - auto value = config->get_qualified_as<std::string>(config_key); |
201 | | - if (!value) { |
202 | | - return; |
203 | | - } |
204 | | - auto str_value = *value; |
205 | | - if (str_value == "open") { |
206 | | - param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy; |
207 | | - } else if (str_value == "closed") { |
208 | | - param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache; |
209 | | - } else if (str_value == "torus") { |
210 | | - param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy; |
211 | | - } else { |
212 | | - Log::Fatal("Param", |
213 | | - Concat("Parameter bound_space was set to an invalid value (", |
214 | | - str_value, ").")); |
215 | | - } |
| 189 | +void AssignBoundSpaceMode(const TomlConfig& config, Param* param) { |
| 190 | + auto value = config.at_path("simulation.bound_space").value<std::string>(); |
| 191 | + if (!value) { |
| 192 | + return; |
| 193 | + } |
| 194 | + auto str_value = *value; |
| 195 | + if (str_value == "open") { |
| 196 | + param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy; |
| 197 | + } else if (str_value == "closed") { |
| 198 | + param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache; |
| 199 | + } else if (str_value == "torus") { |
| 200 | + param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy; |
| 201 | + } else { |
| 202 | + Log::Fatal("Param", |
| 203 | + Concat("Parameter bound_space was set to an invalid value (", |
| 204 | + str_value, ").")); |
216 | 205 | } |
217 | 206 | } |
218 | 207 |
|
219 | 208 | // ----------------------------------------------------------------------------- |
220 | | -void Param::AssignFromConfig(const std::shared_ptr<cpptoml::table>& config) { |
| 209 | +void Param::AssignFromConfig(const TomlConfig& config) { |
221 | 210 | // group parameters |
222 | 211 | for (auto& el : groups_) { |
223 | 212 | el.second->AssignFromConfig(config); |
@@ -261,77 +250,67 @@ void Param::AssignFromConfig(const std::shared_ptr<cpptoml::table>& config) { |
261 | 250 | "visualization.compress_pv_files"); |
262 | 251 |
|
263 | 252 | // visualize_agents |
264 | | - auto visualize_agentstarr = config->get_table_array("visualize_agent"); |
265 | | - if (visualize_agentstarr) { |
266 | | - for (const auto& table : *visualize_agentstarr) { |
267 | | - // We do a 'redundant' check here, because `get_as` on Mac OS does not |
268 | | - // catch the exception when the "name" is not defined in the bdm.toml |
269 | | - // Same goes for all the other redundant checks |
270 | | - if (table->contains("name")) { |
271 | | - auto name = table->get_as<std::string>("name"); |
272 | | - if (!name) { |
273 | | - Log::Warning("AssignFromConfig", |
274 | | - "Missing name for attribute visualize_agent"); |
275 | | - continue; |
276 | | - } |
277 | | - |
278 | | - if (table->contains("additional_data_members")) { |
279 | | - auto dm_option = |
280 | | - table->get_array_of<std::string>("additional_data_members"); |
| 253 | + if (auto visualize_agentstarr = config["visualize_agent"].as_array()) { |
| 254 | + for (auto& elem : *visualize_agentstarr) { |
| 255 | + auto* table = elem.as_table(); |
| 256 | + if (!table) { |
| 257 | + continue; |
| 258 | + } |
| 259 | + auto name = (*table)["name"].value<std::string>(); |
| 260 | + if (!name) { |
| 261 | + Log::Warning("AssignFromConfig", |
| 262 | + "Missing name for attribute visualize_agent"); |
| 263 | + continue; |
| 264 | + } |
281 | 265 |
|
282 | | - std::set<std::string> data_members; |
283 | | - for (const auto& val : *dm_option) { |
284 | | - data_members.insert(val); |
| 266 | + std::set<std::string> data_members; |
| 267 | + if (auto dm_arr = (*table)["additional_data_members"].as_array()) { |
| 268 | + for (auto& dm : *dm_arr) { |
| 269 | + if (auto s = dm.value<std::string>()) { |
| 270 | + data_members.insert(*s); |
285 | 271 | } |
286 | | - visualize_agents[*name] = data_members; |
287 | | - } else { |
288 | | - std::set<std::string> data_members; |
289 | | - visualize_agents[*name] = data_members; |
290 | 272 | } |
291 | 273 | } |
| 274 | + visualize_agents[*name] = data_members; |
292 | 275 | } |
293 | 276 | } |
294 | 277 |
|
295 | 278 | // visualize_diffusion |
296 | | - auto visualize_diffusiontarr = config->get_table_array("visualize_diffusion"); |
297 | | - if (visualize_diffusiontarr) { |
298 | | - for (const auto& table : *visualize_diffusiontarr) { |
299 | | - if (table->contains("name")) { |
300 | | - auto name = table->get_as<std::string>("name"); |
301 | | - if (!name) { |
302 | | - Log::Warning("AssignFromConfig", |
303 | | - "Missing name for attribute visualize_diffusion"); |
304 | | - continue; |
305 | | - } |
306 | | - |
307 | | - VisualizeDiffusion vd; |
308 | | - vd.name = *name; |
| 279 | + if (auto visualize_diffusiontarr = config["visualize_diffusion"].as_array()) { |
| 280 | + for (auto& elem : *visualize_diffusiontarr) { |
| 281 | + auto* table = elem.as_table(); |
| 282 | + if (!table) { |
| 283 | + continue; |
| 284 | + } |
| 285 | + auto name = (*table)["name"].value<std::string>(); |
| 286 | + if (!name) { |
| 287 | + Log::Warning("AssignFromConfig", |
| 288 | + "Missing name for attribute visualize_diffusion"); |
| 289 | + continue; |
| 290 | + } |
309 | 291 |
|
310 | | - if (table->contains("concentration")) { |
311 | | - auto concentration = table->get_as<bool>("concentration"); |
312 | | - if (concentration) { |
313 | | - vd.concentration = *concentration; |
314 | | - } |
315 | | - } |
316 | | - if (table->contains("gradient")) { |
317 | | - auto gradient = table->get_as<bool>("gradient"); |
318 | | - if (gradient) { |
319 | | - vd.gradient = *gradient; |
320 | | - } |
321 | | - } |
| 292 | + VisualizeDiffusion vd; |
| 293 | + vd.name = *name; |
322 | 294 |
|
323 | | - visualize_diffusion.push_back(vd); |
| 295 | + if (auto concentration = (*table)["concentration"].value<bool>()) { |
| 296 | + vd.concentration = *concentration; |
| 297 | + } |
| 298 | + if (auto gradient = (*table)["gradient"].value<bool>()) { |
| 299 | + vd.gradient = *gradient; |
324 | 300 | } |
| 301 | + |
| 302 | + visualize_diffusion.push_back(vd); |
325 | 303 | } |
326 | 304 | } |
327 | 305 |
|
328 | 306 | // unschedule_default_operations |
329 | | - if (config->get_table("simulation")) { |
330 | | - auto disabled_ops = |
331 | | - config->get_table("simulation") |
332 | | - ->get_array_of<std::string>("unschedule_default_operations"); |
333 | | - for (const auto& op : *disabled_ops) { |
334 | | - unschedule_default_operations.push_back(op); |
| 307 | + if (auto sim_tbl = config["simulation"].as_table()) { |
| 308 | + if (auto ops_arr = (*sim_tbl)["unschedule_default_operations"].as_array()) { |
| 309 | + for (auto& elem : *ops_arr) { |
| 310 | + if (auto op = elem.value<std::string>()) { |
| 311 | + unschedule_default_operations.push_back(*op); |
| 312 | + } |
| 313 | + } |
335 | 314 | } |
336 | 315 | } |
337 | 316 |
|
|
0 commit comments