Debian Codegraph @

src/utilities/aslParametersManager.cxx

raw
1 /*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include "aslParametersManager.h"
24 #include "../aslUtilities.h"
25 #include "../acl/aclHardware.h"
26 #include "math/aslVectorsDynamicLength.h"
27 #include <iostream>
28 #include <fstream>
29 #include <boost/filesystem.hpp>
30 #include "acl/aclTypesList.h"
31 #include <map>
32 #include <memory>
33
34 using namespace std;
35 using namespace boost::filesystem;
36 using namespace boost::program_options;
37
38 namespace asl
39 {
40
41 // Auxiliary infrastructure in order to enable
42 // usage of "*" wildcard in options key and
43 // map population.
44 // This should be removed once program_options
45 // enables population of a map<string, T>.
46 //
47 //
48 // see also (add after the implementation bellow):
49 // http://stackoverflow.com/questions/15554842/boostprogram-options-parameters-with-a-fixed-and-a-variable-token
50 //
51 class PrefixStore
52 {
53 public:
54 PrefixStore(const string prefix_);
55 virtual void store(variables_map & vm) = 0;
56
57 protected:
58 string prefix;
59 };
60
61
62 template <typename T> class PrefixMapStore : public PrefixStore
63 {
64 public:
65 PrefixMapStore(const string prefix_,
66 shared_ptr<map<string, T>> destinationMap_);
67 virtual void store(variables_map & vm);
68
69 private:
70 shared_ptr<map<string, T>> destinationMap;
71 };
72
73
74 PrefixStore::PrefixStore(const string prefix_):
75 prefix(prefix_)
76 {
77 }
78
79
80 template <typename T>
81 PrefixMapStore<T>::PrefixMapStore(const string prefix_,
82 shared_ptr<map<string, T>> destinationMap_ ):
83 PrefixStore(prefix_),
84 destinationMap(destinationMap_)
85 {
86 }
87
88
89 template <typename T>
90 void PrefixMapStore<T>::store(variables_map & vm)
91 {
92 variables_map::iterator it;
93 for (it = vm.begin(); it != vm.end(); ++it)
94 {
95 if ((*it).first.find(prefix) != string::npos)
96 {
97 pair<string, T> p(it->first, it->second.as<T>());
98 destinationMap->insert(p);
99 }
100 }
101 }
102 // End of auxiliary infrastructure.
103
104
105 template <typename T> class WildcardCheck
106 {
107 public:
108 static void check(string key, shared_ptr<T> dummyPointer)
109 {
110 if (*key.rbegin() == '*')
111 errorMessage("Parameter<T>::Parameter() - attempt to use \"*\" wildcard in the option key without providing corresponding map");
112 }
113 };
114
115
116 template <typename T> class WildcardCheck<map<string, T>>
117 {
118 public:
119 static void check(string key, shared_ptr<map<string, T>> destinationMap)
120 {
121 if (*key.rbegin() != '*')
122 errorMessage("Parameter<map<string, T>>::Parameter() - no \"*\" wildcard in the option key");
123
124 // generate prefix by cutting option key's last char - "*"
125 ParametersManager::current->addPrefix(key.substr(0, key.size() - 1),
126 destinationMap);
127 }
128 };
129
130
131 template <typename T>
132 Parameter<T>::Parameter(const char* key_,
133 const char* description_,
134 const char* units_):
135 key(key_),
136 description(description_),
137 units(units_)
138 {
139 // Adds itself to current ParametersManager
140 if (ParametersManager::current == NULL)
141 errorMessage("ParametersManager was not instantiated and is not available");
142
143 WildcardCheck<T>::check(key, parameter.p);
144
145 ParametersManager::current->add(parameter,
146 key.c_str(),
147 description.c_str(),
148 units.c_str());
149 }
150
151 template Parameter<string>::Parameter(const char* key,
152 const char* description,
153 const char* units);
154
155 #define BOOST_TT_rep_expression(r, data, t) \
156 template Parameter<t>::Parameter(const char* key, \
157 const char* description, \
158 const char* units); \
159 template Parameter<AVec<t>>::Parameter(const char* key, \
160 const char* description, \
161 const char* units); \
162 template Parameter<map<string, t>>::Parameter(const char* key, \
163 const char* description, \
164 const char* units); \
165 template Parameter<map<string, AVec<t>>>::Parameter(const char* key, \
166 const char* description, \
167 const char* units);
168 BOOST_PP_SEQ_FOR_EACH(BOOST_TT_rep_expression, ~, BOOST_TT_acl_types)
169 #undef BOOST_TT_rep_expression
170
171
172 template <typename T> Parameter<T>::Parameter(T defaultValue,
173 const char* key_,
174 const char* description_,
175 const char* units_):
176 key(key_),
177 description(description_),
178 units(units_)
179 {
180 // Adds itself to current ParametersManager
181 if (ParametersManager::current == NULL)
182 errorMessage("ParametersManager was not instantiated and is not available");
183
184 ParametersManager::current->add(parameter,
185 defaultValue,
186 key.c_str(),
187 description.c_str(),
188 units.c_str());
189 }
190
191 template Parameter<string>::Parameter(string defaultValue,
192 const char* key,
193 const char* description,
194 const char* units);
195
196 #define BOOST_TT_rep_expression(r, data, t) \
197 template Parameter<t>::Parameter(t defaultValue, \
198 const char* key, \
199 const char* description, \
200 const char* units); \
201 template Parameter<AVec<t>>::Parameter(AVec<t> defaultValue, \
202 const char* key, \
203 const char* description, \
204 const char* units);
205 BOOST_PP_SEQ_FOR_EACH(BOOST_TT_rep_expression, ~, BOOST_TT_acl_types)
206 #undef BOOST_TT_rep_expression
207
208
209 ParametersManager * ParametersManager::current(NULL);
210
211 ParametersManager::ParametersManager():
212 parametersOptions("Parameters options"),
213 parametersFileStr("# Parameters file\n"),
214 parametersFileDirectory("")
215 {
216 enable();
217 }
218
219
220 ParametersManager::~ParametersManager()
221 {
222 // Deactivates this instance of ParametersManager
223 if (current == this)
224 current = NULL;
225 }
226
227
228 void ParametersManager::enable()
229 {
230 // Activates this instance of ParametersManager
231 current = this;
232 }
233
234
235 // Function that teaches boost::program_options how to deal with AVec<>
236 template <typename T> void validate(boost::any& v,
237 const std::vector<std::string>& values,
238 AVec<T> *, int)
239 {
240 // Make sure no previous assignment to 'v' was made.
241 validators::check_first_occurrence(v);
242
243 AVec<T> vec;
244 // Extract tokens from values string vector and populate AVec
245 if (values[0] == "")
246 {
247 errorMessage("ParametersManager - no value provided for a variable of type AVec<...>");
248 }
249
250 vector<T> converted;
251 stringstream strStream(values[0]);
252 string token;
253 while (!strStream.eof())
254 {
255 strStream >> token;
256 converted.push_back(strToNum<T>(token));
257 }
258
259 vec = converted;
260 v = vec;
261 }
262
263
264 template <typename T> void ParametersManager::add(UValue<T> parameter,
265 const char* key,
266 const char* description,
267 const char* units)
268 {
269 string fullDescription = units[0] == '\0' ? description
270 : string(description) + ", [" + units + "]";
271 parametersOptions.add_options()
272 (key, value<T>(&parameter.v())->required(), fullDescription.c_str());
273
274 // Add the parameter to the default parameters file
275 parametersFileStr += "\n# " + fullDescription + "\n" + key + " = \n";
276 }
277
278
279 template<typename T> void ParametersManager::add(UValue<map<string, T>> parameter,
280 const char* key,
281 const char* description,
282 const char* units)
283 {
284 string fullDescription = units[0] == '\0' ? description
285 : string(description) + ", [" + units + "]";
286 parametersOptions.add_options()
287 (key, value<T>()->required(), fullDescription.c_str());
288 // Add the parameter to the default parameters file
289 parametersFileStr += "\n# " + fullDescription + "; replace '*' by any suffix to provide a set of related parameters\n"
290 + key + " = \n";
291 }
292
293
294 template<typename T> void ParametersManager::add(UValue<T> parameter,
295 T defaultValue,
296 const char* key,
297 const char* description,
298 const char* units)
299 {
300 string fullDescription = units[0] == '\0' ? description
301 : string(description) + ", [" + units + "]";
302 parametersOptions.add_options()
303 (key, value<T>(&parameter.v())->default_value(defaultValue),
304 fullDescription.c_str());
305
306 // Add the parameter to the default parameters file
307 parametersFileStr += "\n# " + fullDescription + "\n"
308 + key + " = " + numToStr(defaultValue) + "\n";
309 }
310
311
312 template<typename T>
313 void ParametersManager::addPrefix(string prefix,
314 shared_ptr<map<string, T>> destinationMap)
315 {
316 prefixes.push_back(std::make_shared<PrefixMapStore<T>>(prefix, destinationMap));
317 }
318
319
320 void ParametersManager::populateMaps(variables_map & vm)
321 {
322 for (unsigned int i = 0; i < prefixes.size(); ++i)
323 {
324 prefixes[i]->store(vm);
325 }
326 }
327
328
329 void ParametersManager::load(string paramFile)
330 {
331 variables_map vm;
332
333 try
334 {
335 std::ifstream ifs(paramFile);
336 if (!ifs.good())
337 errorMessage("Can not open parameters file: " + paramFile);
338
339 parsed_options parsed = parse_config_file(ifs, parametersOptions,
340 true);
341 store(parsed, vm);
342 notify(vm);
343 populateMaps(vm);
344 }
345 catch(exception& e)
346 {
347 errorMessage(string("ParametersManager::load() - ") + e.what());
348 }
349 }
350
351
352 string ParametersManager::getDir()
353 {
354 return parametersFileDirectory;
355 }
356
357
358 void ParametersManager::writeParametersFile(const std::string fileName)
359 {
360 std::ofstream fo(fileName);
361 if (!fo.good())
362 errorMessage("ParametersManager::writeParametersFile() - can not open file: " + fileName);
363
364 fo << parametersFileStr;
365 fo.close();
366 }
367
368
369 ApplicationParametersManager::ApplicationParametersManager(const char* applicationName_,
370 const char* applicationVersion_):
371 platform(acl::getPlatformVendor(acl::hardware.defaultQueue)),
372 device(acl::getDeviceName(acl::hardware.defaultQueue)),
373 applicationName(applicationName_),
374 applicationVersion(applicationVersion_)
375 {
376 enable();
377 // Prepend informative header
378 parametersFileStr += "# Generated by " + applicationName + " version " + applicationVersion + "\n\n"
379 + "# Get the list of all available computation devices by running:\n"
380 + "# `" + applicationName + " -d`\n";
381
382 /* Add platform and device parameters already initialized
383 with their default values */
384 add(platform, platform.v(),
385 "platform", "Default computation platform", "");
386 add(device, device.v(),
387 "device", "Default computation device", "");
388 }
389
390
391 void ApplicationParametersManager::load(int argc, char * argv[])
392 {
393 variables_map vm;
394 // Set default parameters file path
395 path p("./");
396
397 options_description genericOptions("Generic options");
398
399 genericOptions.add_options()
400 ("help,h", "Display this help and exit")
401 ("version,v", "Display version and exit")
402 ("devices,d", "Display all available devices and exit")
403 ("parameters,p", value<string>(), "Path to the parameters file")
404 ("generate,g", value<string>(),
405 "Generate default parameters file, write it and exit")
406 ("check,c", "Check parameters for consistency and exit");
407
408 positional_options_description positional;
409
410 options_description allOptions;
411 positional.add("parameters", 1);
412
413 allOptions.add(genericOptions).add(parametersOptions);
414
415 try
416 {
417 store(command_line_parser(argc, argv).options(allOptions).positional(positional).run(), vm);
418
419 if (vm.count("help"))
420 {
421 cout << "Usage: " + applicationName + " [PARAMETERS_FILE] [OPTION]...\n"
422 << allOptions
423 << endl;
424 exit(EXIT_SUCCESS);
425 }
426
427 if (vm.count("version"))
428 {
429 cout << applicationName + " " + applicationVersion
430 << endl;
431 exit(EXIT_SUCCESS);
432 }
433
434 if (vm.count("devices"))
435 {
436 cout << applicationName + " " + applicationVersion + "\n\n"
437 << "Default computation device:\n"
438 << acl::hardware.getDefaultDeviceInfo() << "\n\n"
439 << "List of all available platforms and their devices:\n"
440 << acl::hardware.getDevicesInfo()
441 << endl;
442 exit(EXIT_SUCCESS);
443 }
444
445 if (vm.count("generate"))
446 {
447 path gp(vm["generate"].as<string>());
448 cout << "Writing default parameters file to: "
449 << gp.string() << endl;
450
451 writeParametersFile(gp.string());
452 exit(EXIT_SUCCESS);
453 }
454
455 if (vm.count("parameters"))
456 {
457 p = vm["parameters"].as<string>();
458 if (is_directory(p))
459 {
460 // Only warn, since all options might have default values, or required values
461 // provided through the command line - so no parameters file is required
462 warningMessage("ApplicationParametersManager::load() - no parameters file provided, " + p.string() + " is a directory. Using default and/or command line values");
463 }
464 else
465 {
466 std::ifstream ifs(p.string());
467 if (ifs.good())
468 {
469 parsed_options parsed = parse_config_file(ifs, allOptions, true);
470 store(parsed, vm);
471 // Get directory, cutting file name
472 p = p.parent_path();
473 }
474 else
475 {
476 warningMessage("ApplicationParametersManager::load() - can not open parameters file: " + p.string() + " . Using default and/or command line values");
477 // Get directory, cutting file name, after using p for warning
478 p = p.parent_path();
479 }
480 }
481 }
482 else
483 {
484 warningMessage("ApplicationParametersManager::load() - no parameters file provided. Using default and/or command line values");
485 }
486
487 // Generate `parametersFileDirectory`
488 // Get absolute, canonical (no symbolic links, . or ..) path first
489 p = canonical(p);
490 // Append slash
491 p /= "/";
492 parametersFileDirectory = p.string();
493
494 // Run error notification only after obtaining
495 // all options and dealing with "--help"
496 notify(vm);
497
498 populateMaps(vm);
499
500 // Set hardware default queue as required through provided options
501 acl::hardware.setDefaultQueue(vm["platform"].as<string>(),
502 vm["device"].as<string>());
503
504 // Place it after(!) notify(vm);
505 if (vm.count("check"))
506 {
507 cout << "Parameters consistency check - successful." << endl;
508 exit(EXIT_SUCCESS);
509 }
510 }
511 catch(exception& e)
512 {
513 errorMessage(string("ApplicationParametersManager::load() - ") + e.what());
514 }
515 }
516
517 } //namespace asl