PetaVision  Alpha
Configuration.cpp
1 #include "Configuration.hpp"
2 #include "utils/PVAssert.hpp"
3 #include "utils/PVLog.hpp"
4 
5 namespace PV {
6 
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");
25 }
26 
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);
31 }
32 
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));
37  FatalIf(
38  !insertion.second, "failed to register boolean configuration argument %s\n", name.c_str());
39 }
40 
41 void Configuration::registerIntegerArgument(std::string const &name) {
42  registerArgument(name, CONFIG_INT);
43  int defaultValue = 0;
44  auto insertion = mIntegerConfigMap.insert(std::make_pair(name, defaultValue));
45  FatalIf(
46  !insertion.second, "failed to register integer configuration argument %s\n", name.c_str());
47 }
48 
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));
53  FatalIf(
54  !insertion.second,
55  "failed to register unsigned int configuration argument %s\n",
56  name.c_str());
57 }
58 
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));
63  FatalIf(
64  !insertion.second, "failed to register string configuration argument %s\n", name.c_str());
65 }
66 
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));
71  FatalIf(
72  !insertion.second,
73  "failed to register optional int configuration argument %s\n",
74  name.c_str());
75 }
76 
77 Configuration::ConfigurationType Configuration::getType(std::string const &name) const {
78  auto location = mConfigTypeMap.find(name);
79  return (location == mConfigTypeMap.end()) ? CONFIG_UNRECOGNIZED : location->second;
80 }
81 
82 bool const &Configuration::getBooleanArgument(std::string const &name) const {
83  if (getType(name) != CONFIG_BOOL) {
84  throw std::invalid_argument("getBooleanArgument");
85  }
86  auto location = mBooleanConfigMap.find(name);
87  pvAssert(location != mBooleanConfigMap.end());
88  return location->second;
89 }
90 
91 int const &Configuration::getIntegerArgument(std::string const &name) const {
92  if (getType(name) != CONFIG_INT) {
93  throw std::invalid_argument("getIntegerArgument");
94  }
95  auto location = mIntegerConfigMap.find(name);
96  pvAssert(location != mIntegerConfigMap.end());
97  return location->second;
98 }
99 
100 unsigned int const &Configuration::getUnsignedIntArgument(std::string const &name) const {
101  if (getType(name) != CONFIG_UNSIGNED) {
102  throw std::invalid_argument("getUnsignedIntArgument");
103  }
104  auto location = mUnsignedIntConfigMap.find(name);
105  pvAssert(location != mUnsignedIntConfigMap.end());
106  return location->second;
107 }
108 
109 std::string const &Configuration::getStringArgument(std::string const &name) const {
110  if (getType(name) != CONFIG_STRING) {
111  throw std::invalid_argument("getStringArgument");
112  }
113  auto location = mStringConfigMap.find(name);
114  pvAssert(location != mStringConfigMap.end());
115  return location->second;
116 }
117 
119 Configuration::getIntOptionalArgument(std::string const &name) const {
120  if (getType(name) != CONFIG_INT_OPTIONAL) {
121  throw std::invalid_argument("getIntOptionalArgument");
122  }
123  auto location = mIntOptionalConfigMap.find(name);
124  pvAssert(location != mIntOptionalConfigMap.end());
125  return location->second;
126 }
127 
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;
132 }
133 
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;
138 }
139 
140 std::string
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;
145 }
146 
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;
151 }
152 
153 std::string
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("-");
159  }
160  else {
161  returnedString.append(std::to_string(value.mValue));
162  }
163  return returnedString;
164 }
165 
166 std::string Configuration::printArgument(std::string const &name) const {
167  ConfigurationType type = getType(name);
168  std::string returnString;
169  bool status; // Used to verify the result of calling the get*Argument method.
170  switch (type) {
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));
176  break;
177  case CONFIG_STRING: returnString = printStringArgument(name, getStringArgument(name)); break;
178  case CONFIG_INT_OPTIONAL:
179  returnString = printIntOptionalArgument(name, getIntOptionalArgument(name));
180  break;
181  default: pvAssert(0); break;
182  }
183  return returnString;
184 }
185 
186 std::string Configuration::printConfig() const {
187  std::string configString;
188  for (auto &s : mConfigArguments) {
189  configString.append(printArgument(s)).append("\n");
190  }
191  return configString;
192 }
193 
194 bool Configuration::setArgumentUsingString(std::string const &name, std::string const &value) {
195  bool found;
196  switch (getType(name)) {
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);
204  }
205  return true;
206 }
207 
208 bool Configuration::parseBoolean(std::string const &valueString) const {
209  bool value;
210  if (valueString == "true" || valueString == "T" || valueString == "1") {
211  value = true;
212  }
213  else if (valueString == "false" || valueString == "F" || valueString == "0") {
214  value = false;
215  }
216  else {
217  throw std::invalid_argument("parseBoolean");
218  }
219  return value;
220 }
221 
222 int Configuration::parseInteger(std::string const &valueString) const {
223  int value = std::stoi(valueString);
224  return value;
225 }
226 
227 unsigned int Configuration::parseUnsignedInt(std::string const &valueString) const {
228  unsigned int value = (unsigned int)std::stoul(valueString);
229  return value;
230 }
231 
232 std::string Configuration::parseString(std::string const &valueString) const { return valueString; }
233 
234 Configuration::IntOptional Configuration::parseIntOptional(std::string const &valueString) const {
235  IntOptional value;
236  if (valueString.empty() || valueString == "-") {
237  value.mUseDefault = true;
238  value.mValue = -1;
239  }
240  else {
241  value.mUseDefault = false;
242  value.mValue = std::stoi(valueString);
243  }
244  return value;
245 }
246 
247 bool Configuration::setBooleanArgument(std::string const &name, bool const &value) {
248  bool found = getType(name) == CONFIG_BOOL;
249  if (found) {
250  auto location = mBooleanConfigMap.find(name);
251  pvAssert(location != mBooleanConfigMap.end());
252  location->second = value;
253  }
254  return found;
255 }
256 
257 bool Configuration::setIntegerArgument(std::string const &name, int const &value) {
258  bool found = getType(name) == CONFIG_INT;
259  if (found) {
260  auto location = mIntegerConfigMap.find(name);
261  pvAssert(location != mIntegerConfigMap.end());
262  location->second = value;
263  }
264  return found;
265 }
266 
267 bool Configuration::setUnsignedIntArgument(std::string const &name, unsigned int const &value) {
268  bool found = getType(name) == CONFIG_UNSIGNED;
269  if (found) {
270  auto location = mUnsignedIntConfigMap.find(name);
271  pvAssert(location != mUnsignedIntConfigMap.end());
272  location->second = value;
273  }
274  return found;
275 }
276 
277 bool Configuration::setStringArgument(std::string const &name, std::string const &value) {
278  bool found = getType(name) == CONFIG_STRING;
279  if (found) {
280  auto location = mStringConfigMap.find(name);
281  pvAssert(location != mStringConfigMap.end());
282  location->second = value;
283  }
284  return found;
285 }
286 
287 bool Configuration::setIntOptionalArgument(std::string const &name, IntOptional const &value) {
288  bool found = getType(name) == CONFIG_INT_OPTIONAL;
289  if (found) {
290  auto location = mIntOptionalConfigMap.find(name);
291  pvAssert(location != mIntOptionalConfigMap.end());
292  location->second = value;
293  }
294  return found;
295 }
296 
297 } // end namespace PV
ConfigurationType getType(std::string const &name) const