Unverified Commit da811d46 authored by jcipar's avatar jcipar Committed by GitHub
Browse files

Config static initialization (#2743)

* Change static map/set to functions that return static variables

* Fixed newline

* testing CI

* removed commented-out code fixed formatting

* return const references to parameter_set and alias_table

* fixup whitespace

* Too many requests error when running CI on docs. Triggering a rebuild.
parent 63c8c036
......@@ -269,15 +269,24 @@ def gen_parameter_code(config_hpp, config_out_cpp):
"""
str_to_write += "#include<LightGBM/config.h>\nnamespace LightGBM {\n"
# alias table
str_to_write += "std::unordered_map<std::string, std::string> Config::alias_table({\n"
str_to_write += "const std::unordered_map<std::string, std::string>& Config::alias_table() {\n"
str_to_write += " static std::unordered_map<std::string, std::string> aliases({\n"
for pair in alias:
str_to_write += " {\"%s\", \"%s\"},\n" % (pair[0], pair[1])
str_to_write += "});\n\n"
str_to_write += " });\n"
str_to_write += " return aliases;\n"
str_to_write += "}\n\n"
# names
str_to_write += "std::unordered_set<std::string> Config::parameter_set({\n"
str_to_write += "const std::unordered_set<std::string>& Config::parameter_set() {\n"
str_to_write += " static std::unordered_set<std::string> params({\n"
for name in names:
str_to_write += " \"%s\",\n" % (name)
str_to_write += "});\n\n"
str_to_write += " });\n"
str_to_write += " return params;\n"
str_to_write += "}\n\n"
# from strings
str_to_write += "void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {\n"
str_to_write += " std::string tmp_str = \"\";\n"
......
......@@ -890,8 +890,8 @@ struct Config {
bool is_parallel = false;
bool is_parallel_find_bin = false;
LIGHTGBM_EXPORT void Set(const std::unordered_map<std::string, std::string>& params);
static std::unordered_map<std::string, std::string> alias_table;
static std::unordered_set<std::string> parameter_set;
static const std::unordered_map<std::string, std::string>& alias_table();
static const std::unordered_set<std::string>& parameter_set();
std::vector<std::vector<double>> auc_mu_weights_matrix;
private:
......@@ -960,8 +960,8 @@ struct ParameterAlias {
static void KeyAliasTransform(std::unordered_map<std::string, std::string>* params) {
std::unordered_map<std::string, std::string> tmp_map;
for (const auto& pair : *params) {
auto alias = Config::alias_table.find(pair.first);
if (alias != Config::alias_table.end()) { // found alias
auto alias = Config::alias_table().find(pair.first);
if (alias != Config::alias_table().end()) { // found alias
auto alias_set = tmp_map.find(alias->second);
if (alias_set != tmp_map.end()) { // alias already set
// set priority by length & alphabetically to ensure reproducible behavior
......@@ -979,7 +979,7 @@ struct ParameterAlias {
} else { // alias not set
tmp_map.emplace(alias->second, pair.first);
}
} else if (Config::parameter_set.find(pair.first) == Config::parameter_set.end()) {
} else if (Config::parameter_set().find(pair.first) == Config::parameter_set().end()) {
Log::Warning("Unknown parameter: %s", pair.first.c_str());
}
}
......
......@@ -7,7 +7,8 @@
*/
#include<LightGBM/config.h>
namespace LightGBM {
std::unordered_map<std::string, std::string> Config::alias_table({
const std::unordered_map<std::string, std::string>& Config::alias_table() {
static std::unordered_map<std::string, std::string> aliases({
{"config_file", "config"},
{"task_type", "task"},
{"objective_type", "objective"},
......@@ -165,9 +166,12 @@ std::unordered_map<std::string, std::string> Config::alias_table({
{"mlist", "machine_list_filename"},
{"workers", "machines"},
{"nodes", "machines"},
});
});
return aliases;
}
std::unordered_set<std::string> Config::parameter_set({
const std::unordered_set<std::string>& Config::parameter_set() {
static std::unordered_set<std::string> params({
"config",
"task",
"objective",
......@@ -287,7 +291,9 @@ std::unordered_set<std::string> Config::parameter_set({
"gpu_platform_id",
"gpu_device_id",
"gpu_use_dp",
});
});
return params;
}
void Config::GetMembersFromString(const std::unordered_map<std::string, std::string>& params) {
std::string tmp_str = "";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment