1 #include "Configuration.hpp" 2 #include "utils/PVAssert.hpp" 3 #include "utils/PVLog.hpp" 7 Configuration::Configuration() {
8 registerBooleanArgument(
"RequireReturn");
9 registerStringArgument(
"OutputPath");
10 registerStringArgument(
"ParamsFile");
11 registerStringArgument(
"LogFile");
12 registerStringArgument(
"GPUDevices");
13 registerUnsignedIntArgument(
"RandomSeed");
14 registerStringArgument(
"WorkingDirectory");
15 registerBooleanArgument(
"Restart");
16 registerStringArgument(
"CheckpointReadDirectory");
17 registerIntOptionalArgument(
"NumThreads");
18 registerIntegerArgument(
"NumRows");
19 registerIntegerArgument(
"NumColumns");
20 registerIntegerArgument(
"BatchWidth");
21 registerIntegerArgument(
"CheckpointCellNumRows");
22 registerIntegerArgument(
"CheckpointCellNumColumns");
23 registerIntegerArgument(
"CheckpointCellBatchDimension");
24 registerBooleanArgument(
"DryRun");
27 void Configuration::registerArgument(std::string
const &name, ConfigurationType type) {
28 auto insertion = mConfigTypeMap.insert(std::make_pair(name, type));
29 FatalIf(!insertion.second,
"failed to register configuration argument %s\n", name.c_str());
30 mConfigArguments.push_back(name);
33 void Configuration::registerBooleanArgument(std::string
const &name) {
34 registerArgument(name, CONFIG_BOOL);
35 bool defaultValue =
false;
36 auto insertion = mBooleanConfigMap.insert(std::make_pair(name, defaultValue));
38 !insertion.second,
"failed to register boolean configuration argument %s\n", name.c_str());
41 void Configuration::registerIntegerArgument(std::string
const &name) {
42 registerArgument(name, CONFIG_INT);
44 auto insertion = mIntegerConfigMap.insert(std::make_pair(name, defaultValue));
46 !insertion.second,
"failed to register integer configuration argument %s\n", name.c_str());
49 void Configuration::registerUnsignedIntArgument(std::string
const &name) {
50 registerArgument(name, CONFIG_UNSIGNED);
51 unsigned int defaultValue = 0U;
52 auto insertion = mUnsignedIntConfigMap.insert(std::make_pair(name, defaultValue));
55 "failed to register unsigned int configuration argument %s\n",
59 void Configuration::registerStringArgument(std::string
const &name) {
60 registerArgument(name, CONFIG_STRING);
61 std::string defaultValue{};
62 auto insertion = mStringConfigMap.insert(std::make_pair(name, defaultValue));
64 !insertion.second,
"failed to register string configuration argument %s\n", name.c_str());
67 void Configuration::registerIntOptionalArgument(std::string
const &name) {
68 registerArgument(name, CONFIG_INT_OPTIONAL);
69 IntOptional defaultValue;
70 auto insertion = mIntOptionalConfigMap.insert(std::make_pair(name, defaultValue));
73 "failed to register optional int configuration argument %s\n",
78 auto location = mConfigTypeMap.find(name);
79 return (location == mConfigTypeMap.end()) ? CONFIG_UNRECOGNIZED : location->second;
82 bool const &Configuration::getBooleanArgument(std::string
const &name)
const {
83 if (
getType(name) != CONFIG_BOOL) {
84 throw std::invalid_argument(
"getBooleanArgument");
86 auto location = mBooleanConfigMap.find(name);
87 pvAssert(location != mBooleanConfigMap.end());
88 return location->second;
91 int const &Configuration::getIntegerArgument(std::string
const &name)
const {
92 if (
getType(name) != CONFIG_INT) {
93 throw std::invalid_argument(
"getIntegerArgument");
95 auto location = mIntegerConfigMap.find(name);
96 pvAssert(location != mIntegerConfigMap.end());
97 return location->second;
100 unsigned int const &Configuration::getUnsignedIntArgument(std::string
const &name)
const {
101 if (
getType(name) != CONFIG_UNSIGNED) {
102 throw std::invalid_argument(
"getUnsignedIntArgument");
104 auto location = mUnsignedIntConfigMap.find(name);
105 pvAssert(location != mUnsignedIntConfigMap.end());
106 return location->second;
109 std::string
const &Configuration::getStringArgument(std::string
const &name)
const {
110 if (
getType(name) != CONFIG_STRING) {
111 throw std::invalid_argument(
"getStringArgument");
113 auto location = mStringConfigMap.find(name);
114 pvAssert(location != mStringConfigMap.end());
115 return location->second;
119 Configuration::getIntOptionalArgument(std::string
const &name)
const {
120 if (
getType(name) != CONFIG_INT_OPTIONAL) {
121 throw std::invalid_argument(
"getIntOptionalArgument");
123 auto location = mIntOptionalConfigMap.find(name);
124 pvAssert(location != mIntOptionalConfigMap.end());
125 return location->second;
128 std::string Configuration::printBooleanArgument(std::string
const &name,
bool const &value) {
129 std::string returnedString(name);
130 returnedString.append(
":").append(value ?
"true" :
"false");
131 return returnedString;
134 std::string Configuration::printIntegerArgument(std::string
const &name,
int const &value) {
135 std::string returnedString(name);
136 returnedString.append(
":").append(std::to_string(value));
137 return returnedString;
141 Configuration::printUnsignedArgument(std::string
const &name,
unsigned int const &value) {
142 std::string returnedString(name);
143 returnedString.append(
":").append(std::to_string(value));
144 return returnedString;
147 std::string Configuration::printStringArgument(std::string
const &name, std::string
const &value) {
148 std::string returnedString(name);
149 returnedString.append(
":").append(value);
150 return returnedString;
154 Configuration::printIntOptionalArgument(std::string
const &name, IntOptional
const &value) {
155 std::string returnedString(name);
156 returnedString.append(
":");
157 if (value.mUseDefault) {
158 returnedString.append(
"-");
161 returnedString.append(std::to_string(value.mValue));
163 return returnedString;
166 std::string Configuration::printArgument(std::string
const &name)
const {
167 ConfigurationType type =
getType(name);
168 std::string returnString;
171 case CONFIG_UNRECOGNIZED:
break;
172 case CONFIG_BOOL: returnString = printBooleanArgument(name, getBooleanArgument(name));
break;
173 case CONFIG_INT: returnString = printIntegerArgument(name, getIntegerArgument(name));
break;
174 case CONFIG_UNSIGNED:
175 returnString = printUnsignedArgument(name, getUnsignedIntArgument(name));
177 case CONFIG_STRING: returnString = printStringArgument(name, getStringArgument(name));
break;
178 case CONFIG_INT_OPTIONAL:
179 returnString = printIntOptionalArgument(name, getIntOptionalArgument(name));
181 default: pvAssert(0);
break;
186 std::string Configuration::printConfig()
const {
187 std::string configString;
188 for (
auto &s : mConfigArguments) {
189 configString.append(printArgument(s)).append(
"\n");
194 bool Configuration::setArgumentUsingString(std::string
const &name, std::string
const &value) {
197 case CONFIG_UNRECOGNIZED:
return false;
break;
198 case CONFIG_BOOL: setBooleanArgument(name, parseBoolean(value));
break;
199 case CONFIG_INT: setIntegerArgument(name, parseInteger(value));
break;
200 case CONFIG_UNSIGNED: setUnsignedIntArgument(name, parseUnsignedInt(value));
break;
201 case CONFIG_STRING: setStringArgument(name, parseString(value));
break;
202 case CONFIG_INT_OPTIONAL: setIntOptionalArgument(name, parseIntOptional(value));
break;
203 default: pvAssert(0);
208 bool Configuration::parseBoolean(std::string
const &valueString)
const {
210 if (valueString ==
"true" || valueString ==
"T" || valueString ==
"1") {
213 else if (valueString ==
"false" || valueString ==
"F" || valueString ==
"0") {
217 throw std::invalid_argument(
"parseBoolean");
222 int Configuration::parseInteger(std::string
const &valueString)
const {
223 int value = std::stoi(valueString);
227 unsigned int Configuration::parseUnsignedInt(std::string
const &valueString)
const {
228 unsigned int value = (
unsigned int)std::stoul(valueString);
232 std::string Configuration::parseString(std::string
const &valueString)
const {
return valueString; }
236 if (valueString.empty() || valueString ==
"-") {
237 value.mUseDefault =
true;
241 value.mUseDefault =
false;
242 value.mValue = std::stoi(valueString);
247 bool Configuration::setBooleanArgument(std::string
const &name,
bool const &value) {
248 bool found =
getType(name) == CONFIG_BOOL;
250 auto location = mBooleanConfigMap.find(name);
251 pvAssert(location != mBooleanConfigMap.end());
252 location->second = value;
257 bool Configuration::setIntegerArgument(std::string
const &name,
int const &value) {
258 bool found =
getType(name) == CONFIG_INT;
260 auto location = mIntegerConfigMap.find(name);
261 pvAssert(location != mIntegerConfigMap.end());
262 location->second = value;
267 bool Configuration::setUnsignedIntArgument(std::string
const &name,
unsigned int const &value) {
268 bool found =
getType(name) == CONFIG_UNSIGNED;
270 auto location = mUnsignedIntConfigMap.find(name);
271 pvAssert(location != mUnsignedIntConfigMap.end());
272 location->second = value;
277 bool Configuration::setStringArgument(std::string
const &name, std::string
const &value) {
278 bool found =
getType(name) == CONFIG_STRING;
280 auto location = mStringConfigMap.find(name);
281 pvAssert(location != mStringConfigMap.end());
282 location->second = value;
287 bool Configuration::setIntOptionalArgument(std::string
const &name, IntOptional
const &value) {
288 bool found =
getType(name) == CONFIG_INT_OPTIONAL;
290 auto location = mIntOptionalConfigMap.find(name);
291 pvAssert(location != mIntOptionalConfigMap.end());
292 location->second = value;
ConfigurationType getType(std::string const &name) const