Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ function(install_inside_build)
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cpptoml
${CMAKE_SOURCE_DIR}/third_party/cpptoml/cpptoml.h
)
add_copy_files(copy_files_bdm
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tomlplusplus
${CMAKE_SOURCE_DIR}/third_party/tomlplusplus/toml.hpp
)

# Simulation and demos
add_copy_directory(copy_files_bdm
Expand Down
2 changes: 1 addition & 1 deletion src/core/param/command_line_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ void CommandLineOptions::HandleCoreOptions() {
Log::Fatal("CommandLineOptions::HandleCoreOptions",
"Specified TOML file (", toml_file, ") does not exist.");
}
auto toml = cpptoml::parse_file(toml_file);
auto toml = toml::parse_file(toml_file);
Param param;
param.AssignFromConfig(toml);
std::cout << param.ToJsonString() << std::endl;
Expand Down
213 changes: 96 additions & 117 deletions src/core/param/param.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

#include "core/multi_simulation/optimization_param.h"
#include "core/param/param.h"
#include "core/util/cpptoml.h"
#include "core/util/log.h"
#include "core/util/toml_config.h"

using nlohmann::json;

Expand Down Expand Up @@ -146,78 +146,67 @@ void Param::MergeJsonPatch(const std::string& patch) {
}

// -----------------------------------------------------------------------------
void AssignThreadSafetyMechanism(const std::shared_ptr<cpptoml::table>& config,
Param* param) {
const std::string config_key = "simulation.thread_safety_mechanism";
if (config->contains_qualified(config_key)) {
auto value = config->get_qualified_as<std::string>(config_key);
if (!value) {
return;
}
auto str_value = *value;
if (str_value == "none") {
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kNone;
} else if (str_value == "user-specified") {
param->thread_safety_mechanism =
Param::ThreadSafetyMechanism::kUserSpecified;
} else if (str_value == "automatic") {
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kAutomatic;
}
void AssignThreadSafetyMechanism(const TomlConfig& config, Param* param) {
auto value =
config.at_path("simulation.thread_safety_mechanism").value<std::string>();
if (!value) {
return;
}
auto str_value = *value;
if (str_value == "none") {
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kNone;
} else if (str_value == "user-specified") {
param->thread_safety_mechanism =
Param::ThreadSafetyMechanism::kUserSpecified;
} else if (str_value == "automatic") {
param->thread_safety_mechanism = Param::ThreadSafetyMechanism::kAutomatic;
}
}

// -----------------------------------------------------------------------------
void AssignMappedDataArrayMode(const std::shared_ptr<cpptoml::table>& config,
Param* param) {
const std::string config_key = "performance.mapped_data_array_mode";
if (config->contains_qualified(config_key)) {
auto value = config->get_qualified_as<std::string>(config_key);
if (!value) {
return;
}
auto str_value = *value;
if (str_value == "zero-copy") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
} else if (str_value == "cache") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
} else if (str_value == "copy") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
} else {
Log::Fatal(
"Param",
Concat(
"Parameter mapped_data_array_mode was set to an invalid value (",
str_value, ")."));
}
void AssignMappedDataArrayMode(const TomlConfig& config, Param* param) {
auto value =
config.at_path("performance.mapped_data_array_mode").value<std::string>();
if (!value) {
return;
}
auto str_value = *value;
if (str_value == "zero-copy") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
} else if (str_value == "cache") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
} else if (str_value == "copy") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
} else {
Log::Fatal(
"Param",
Concat("Parameter mapped_data_array_mode was set to an invalid value (",
str_value, ")."));
}
}

// -----------------------------------------------------------------------------
void AssignBoundSpaceMode(const std::shared_ptr<cpptoml::table>& config,
Param* param) {
const std::string config_key = "simulation.bound_space";
if (config->contains_qualified(config_key)) {
auto value = config->get_qualified_as<std::string>(config_key);
if (!value) {
return;
}
auto str_value = *value;
if (str_value == "open") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
} else if (str_value == "closed") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
} else if (str_value == "torus") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
} else {
Log::Fatal("Param",
Concat("Parameter bound_space was set to an invalid value (",
str_value, ")."));
}
void AssignBoundSpaceMode(const TomlConfig& config, Param* param) {
auto value = config.at_path("simulation.bound_space").value<std::string>();
if (!value) {
return;
}
auto str_value = *value;
if (str_value == "open") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kZeroCopy;
} else if (str_value == "closed") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCache;
} else if (str_value == "torus") {
param->mapped_data_array_mode = Param::MappedDataArrayMode::kCopy;
} else {
Log::Fatal("Param",
Concat("Parameter bound_space was set to an invalid value (",
str_value, ")."));
}
}

// -----------------------------------------------------------------------------
void Param::AssignFromConfig(const std::shared_ptr<cpptoml::table>& config) {
void Param::AssignFromConfig(const TomlConfig& config) {
// group parameters
for (auto& el : groups_) {
el.second->AssignFromConfig(config);
Expand Down Expand Up @@ -261,77 +250,67 @@ void Param::AssignFromConfig(const std::shared_ptr<cpptoml::table>& config) {
"visualization.compress_pv_files");

// visualize_agents
auto visualize_agentstarr = config->get_table_array("visualize_agent");
if (visualize_agentstarr) {
for (const auto& table : *visualize_agentstarr) {
// We do a 'redundant' check here, because `get_as` on Mac OS does not
// catch the exception when the "name" is not defined in the bdm.toml
// Same goes for all the other redundant checks
if (table->contains("name")) {
auto name = table->get_as<std::string>("name");
if (!name) {
Log::Warning("AssignFromConfig",
"Missing name for attribute visualize_agent");
continue;
}

if (table->contains("additional_data_members")) {
auto dm_option =
table->get_array_of<std::string>("additional_data_members");
if (auto visualize_agentstarr = config["visualize_agent"].as_array()) {
for (auto& elem : *visualize_agentstarr) {
auto* table = elem.as_table();
if (!table) {
continue;
}
auto name = (*table)["name"].value<std::string>();
if (!name) {
Log::Warning("AssignFromConfig",
"Missing name for attribute visualize_agent");
continue;
}

std::set<std::string> data_members;
for (const auto& val : *dm_option) {
data_members.insert(val);
std::set<std::string> data_members;
if (auto dm_arr = (*table)["additional_data_members"].as_array()) {
for (auto& dm : *dm_arr) {
if (auto s = dm.value<std::string>()) {
data_members.insert(*s);
}
visualize_agents[*name] = data_members;
} else {
std::set<std::string> data_members;
visualize_agents[*name] = data_members;
}
}
visualize_agents[*name] = data_members;
}
}

// visualize_diffusion
auto visualize_diffusiontarr = config->get_table_array("visualize_diffusion");
if (visualize_diffusiontarr) {
for (const auto& table : *visualize_diffusiontarr) {
if (table->contains("name")) {
auto name = table->get_as<std::string>("name");
if (!name) {
Log::Warning("AssignFromConfig",
"Missing name for attribute visualize_diffusion");
continue;
}

VisualizeDiffusion vd;
vd.name = *name;
if (auto visualize_diffusiontarr = config["visualize_diffusion"].as_array()) {
for (auto& elem : *visualize_diffusiontarr) {
auto* table = elem.as_table();
if (!table) {
continue;
}
auto name = (*table)["name"].value<std::string>();
if (!name) {
Log::Warning("AssignFromConfig",
"Missing name for attribute visualize_diffusion");
continue;
}

if (table->contains("concentration")) {
auto concentration = table->get_as<bool>("concentration");
if (concentration) {
vd.concentration = *concentration;
}
}
if (table->contains("gradient")) {
auto gradient = table->get_as<bool>("gradient");
if (gradient) {
vd.gradient = *gradient;
}
}
VisualizeDiffusion vd;
vd.name = *name;

visualize_diffusion.push_back(vd);
if (auto concentration = (*table)["concentration"].value<bool>()) {
vd.concentration = *concentration;
}
if (auto gradient = (*table)["gradient"].value<bool>()) {
vd.gradient = *gradient;
}

visualize_diffusion.push_back(vd);
}
}

// unschedule_default_operations
if (config->get_table("simulation")) {
auto disabled_ops =
config->get_table("simulation")
->get_array_of<std::string>("unschedule_default_operations");
for (const auto& op : *disabled_ops) {
unschedule_default_operations.push_back(op);
if (auto sim_tbl = config["simulation"].as_table()) {
if (auto ops_arr = (*sim_tbl)["unschedule_default_operations"].as_array()) {
for (auto& elem : *ops_arr) {
if (auto op = elem.value<std::string>()) {
unschedule_default_operations.push_back(*op);
}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/param/param.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ struct Param {
bool plot_memory_layout = false;

/// Assign values from config file to variables
void AssignFromConfig(const std::shared_ptr<cpptoml::table>&);
void AssignFromConfig(const TomlConfig&);

private:
friend class DiffusionTest_CopyOldData_Test;
Expand Down
2 changes: 1 addition & 1 deletion src/core/param/param_group.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ ParamGroupUid ParamGroupUidGenerator::NewUid() { return counter_++; }

ParamGroup::~ParamGroup() = default;

void ParamGroup::AssignFromConfig(const std::shared_ptr<cpptoml::table>&) {}
void ParamGroup::AssignFromConfig(const TomlConfig&) {}

} // namespace bdm
12 changes: 10 additions & 2 deletions src/core/param/param_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@

#include <memory>
#include "core/util/root.h"
#include "cpptoml/cpptoml.h"
#include "tomlplusplus/toml.hpp"

namespace bdm {

/// Typedef for TOML config table, insulating downstream code from the
/// concrete TOML library in use.
/// Migration note: cpptoml has been replaced by tomlplusplus.
/// Update downstream AssignFromConfig overrides:
/// void AssignFromConfig(const cpptoml::table&) // old -- will not compile
/// void AssignFromConfig(const TomlConfig&) // new
using TomlConfig = toml::table;

struct Param;

using ParamGroupUid = uint64_t;
Expand Down Expand Up @@ -52,7 +60,7 @@ struct ParamGroup {
protected:
/// Assign values from a toml config file.\n
/// Can be omitted if toml file support is not required.
virtual void AssignFromConfig(const std::shared_ptr<cpptoml::table>&);
virtual void AssignFromConfig(const TomlConfig&);

private:
friend struct Param;
Expand Down
5 changes: 3 additions & 2 deletions src/core/simulation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#include "core/simulation.h"

#include <cpptoml/cpptoml.h>
#include <omp.h>
#include <algorithm>
#include <cmath>
Expand All @@ -28,6 +27,8 @@
#include <utility>
#include <vector>

#include <tomlplusplus/toml.hpp>

#include "bdm_version.h"
#include "core/agent/agent_uid_generator.h"
#include "core/analysis/time_series.h"
Expand Down Expand Up @@ -507,7 +508,7 @@ void Simulation::LoadConfigFiles(const std::vector<std::string>& ctor_configs,
if (configs.size()) {
for (auto& config : configs) {
if (EndsWith(config, ".toml")) {
auto toml = cpptoml::parse_file(config);
auto toml = toml::parse_file(config);
param_->AssignFromConfig(toml);
} else if (EndsWith(config, ".json")) {
std::ifstream ifs(config);
Expand Down
40 changes: 8 additions & 32 deletions src/core/util/cpptoml.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,12 @@
//
// -----------------------------------------------------------------------------

#ifndef CORE_UTIL_CPPTOML_H_
#define CORE_UTIL_CPPTOML_H_
// Deprecated: cpptoml has been replaced by tomlplusplus.
// Update your includes from "core/util/cpptoml.h" to "core/util/toml_config.h"
// and use TomlConfig (from param_group.h) instead of cpptoml::table.
#if !defined(__ROOTCLING__) && !defined(__CLING__)
#pragma message( \
"cpptoml.h is deprecated. Use #include \"core/util/toml_config.h\" instead.")
#endif

#define BDM_ASSIGN_CONFIG_VALUE(variable, config_key) \
{ \
if (config->contains_qualified(config_key)) { \
auto value = config->get_qualified_as<decltype(variable)>(config_key); \
if (value) { \
variable = *value; \
} \
} \
}

#define BDM_ASSIGN_CONFIG_DOUBLE3_VALUE(variable, config_key) \
{ \
if (config->contains_qualified(config_key)) { \
auto value = config->get_array_of<real_t>(config_key); \
if (value) { \
auto vector = *value; \
if (vector.size() == variable.size()) { \
for (uint64_t i = 0; i < vector.size(); i++) { \
variable[i] = vector[i]; \
} \
} else { \
Log::Fatal("cpptoml parameter parsing", \
"An error occurred during parameter parsing of (", \
config_key, ". Array dimensions do not match"); \
} \
} \
} \
}

#endif // CORE_UTIL_CPPTOML_H_
#include "core/util/toml_config.h"
Loading
Loading