Bridge++  Ver. 1.3.x
parameterManager_YAML.cpp
Go to the documentation of this file.
1 
14 #include "parameterManager_YAML.h"
15 
16 #include <sstream>
17 #include <stack>
18 
19 #include "communicator.h"
20 #include "evalexpr.h"
21 
22 using std::string;
23 using Bridge::vout;
24 
25 const std::string ParameterManager_YAML::class_name = "ParameterManager_YAML";
26 
27 //====================================================================
28 void ParameterManager_YAML::read_params(const std::string& params_file, Parameters *params)
29 {
30  const int io_node = 0; // node id for file i/o.
31 
32  int filesize = 0;
33  char *buf = 0;
34 
35  if (Communicator::nodeid() == io_node) {
36  // load and distribute
37  std::ifstream fin(params_file.c_str());
38  if (!fin) {
39  vout.crucial(m_vl, "%s: unable to read parameter file: %s.\n", class_name.c_str(), params_file.c_str());
40 
42  }
43 
44  fin.seekg(0, std::ios::end);
45  filesize = fin.tellg();
46  fin.seekg(0, std::ios::beg);
47 
48  int padding = 8 - (filesize % 8);
49 
50  vout.paranoiac(m_vl, "%s::read_params: filesize = %d, padding = %d\n", class_name.c_str(), filesize, padding);
51 
52  filesize += padding;
53 
54  Communicator::broadcast(1, &filesize, io_node);
55 
56  buf = new char [filesize];
57  memset(buf, 0, filesize);
58 
59  fin.read(buf, filesize - padding);
60 
61  Communicator::Base::broadcast(filesize, buf, io_node);
62  } else {
63  // receive from io_node
64 
65  Communicator::broadcast(1, &filesize, io_node);
66 
67  buf = new char [filesize];
68  memset(buf, 0, filesize);
69 
70  Communicator::Base::broadcast(filesize, buf, io_node);
71  }
72 
73  std::istringstream iss(buf);
74  read_params(iss, params);
75 
76  delete [] buf;
77 }
78 
79 
80 //====================================================================
81 void ParameterManager_YAML::read_params(std::istream& fin, Parameters *params_top)
82 {
83  typedef std::pair<int, Parameters *> Level;
84 
85  int linesize = 256;
86  char *ch = new char[linesize];
87 
88  std::stack<Level> level_reserve;
89 
90  Level top_level(-1, params_top);
91  level_reserve.push(top_level);
92 
93  Parameters *params = params_top;
94 
95  int indent = 0;
96  int indent_prev = indent;
97 
98  while (fin.getline(ch, linesize))
99  {
100  indent_prev = indent;
101 
102  string line(ch);
103  string keystr, valstr;
104 
105  indent = set_key_and_value(keystr, valstr, line);
106  // extract key and value from line.
107 
108  vout.paranoiac(m_vl, "\nline: \"%s\" (%u)\n", line.c_str(), line.length());
109  vout.paranoiac(m_vl, " key = \"%s\"\n", keystr.c_str());
110  vout.paranoiac(m_vl, " val = \"%s\"\n", valstr.c_str());
111  vout.paranoiac(m_vl, " indent = %d\n", indent);
112  vout.paranoiac(m_vl, " val length = %u\n", valstr.length());
113 
114  if (indent == -1) {
115  indent = indent_prev;
116  continue;
117  }
118 
119  if (indent < indent_prev) {
120  // go to upper levels until appropriate indent level found.
121  while (!level_reserve.empty())
122  {
123  Level level = level_reserve.top();
124 
125  if (level.first < indent) {
126  params = level.second;
127  break;
128  }
129 
130  level_reserve.pop();
131  }
132 
133  vout.paranoiac(m_vl, " go back to parent parameters.\n");
134  }
135 
136  if (valstr.length() == 0) { // map key found.
137  // parameter entry for next level. skip if unused.
138  params = params ? params->get_Parameters(keystr) : 0;
139 
140  // preserve parameter entry and associated indent level.
141  Level level(indent, params);
142  level_reserve.push(level);
143 
144  vout.paranoiac(m_vl, " go to subparameters.\n");
145 
146  continue;
147  }
148 
149  if (params && params->find_int(keystr)) {
150  int val = atoi(valstr.c_str());
151  params->set_int(keystr, val);
152 
153  vout.paranoiac(m_vl, " int entry found for key = \"%s\", value = %d\n", keystr.c_str(), val);
154 
155  continue;
156  }
157 
158  if (params && params->find_double(keystr)) {
159 // double val = (valstr.c_str());
160  double val = EvalExpr(valstr).parse();
161  params->set_double(keystr, val);
162 
163  vout.paranoiac(m_vl, " double entry found for key = \"%s\", value = %f\n", keystr.c_str(), val);
164 
165  continue;
166  }
167 
168  if (params && params->find_int_vector(keystr)) {
169  std::vector<int> vec;
170  convert_int_vector(vec, valstr);
171  params->set_int_vector(keystr, vec);
172 
173  vout.paranoiac(m_vl, " int_vector entry found for key = \"%s\"\n", keystr.c_str());
174 
175  continue;
176  }
177 
178  if (params && params->find_double_vector(keystr)) {
179  std::vector<double> vec;
180  convert_double_vector(vec, valstr);
181  params->set_double_vector(keystr, vec);
182 
183  vout.paranoiac(m_vl, " double_vector entry found for key = \"%s\"\n", keystr.c_str());
184 
185  continue;
186  }
187 
188  if (params && params->find_string(keystr)) {
189  params->set_string(keystr, valstr);
190 
191  vout.paranoiac(m_vl, " string entry found for key = \"%s\", value = \"%s\"\n", keystr.c_str(), valstr.c_str());
192 
193  continue;
194  }
195 
196  // verbosity entry
197  if (params && (keystr == "verbose_level")) {
199  params->set_VerboseLevel(vl);
200 
201  vout.paranoiac(m_vl, " verbosity entry found, value = %s(%d)\n", valstr.c_str(), vl);
202 
203  continue;
204  }
205 
206  // vout.crucial(m_vl, "ParameterManager_YAML: entry not found.\n");
207  // Communicator::abort();
208  }
209 
210  delete [] ch;
211 }
212 
213 
214 //====================================================================
215 int ParameterManager_YAML::set_key_and_value(string& keystr, string& valstr, string& line)
216 {
217  string::size_type i2 = line.find("#");
218 
219  if (i2 != string::npos) {
220  line.erase(i2, line.length() - i2);
221  }
222 
223  string::size_type i1 = line.find(":");
224  if (i1 == string::npos) {
225  keystr = "";
226  valstr = "";
227  return -1; // no ":" in the line: regarded as a blank line
228  }
229 
230  keystr = line.substr(0, i1);
231  valstr = line.substr(i1 + 1, line.length() - i1 - 1);
232 
233  int indent = remove_space(keystr);
234  remove_space(valstr);
235 
236  return indent;
237 }
238 
239 
240 //====================================================================
242 {
243  int indent = 0;
244 
245  while (str.length() > 0 && str[0] == ' ')
246  {
247  str.erase(0, 1);
248  ++indent;
249  }
250 
251  while (str.length() > 0 && str[str.length() - 1] == ' ')
252  {
253  str.erase(str.length() - 1, 1);
254  }
255 
256  return indent;
257 }
258 
259 
260 //====================================================================
261 void ParameterManager_YAML::convert_int_vector(std::vector<int>& vec,
262  string& valstr)
263 {
264  int size_max = 20;
265 
266  std::vector<int> vec_tmp(size_max);
267  int size = 0;
268 
269 
270  if (valstr[0] != '[') {
271  vout.crucial(m_vl, "%s: wrong format of vector.\n", class_name.c_str());
273  }
274  valstr.erase(0, 1);
275 
276  if (valstr[valstr.length() - 1] != ']') {
277  vout.crucial(m_vl, "%s: wrong format of vector.\n", class_name.c_str());
279  }
280  valstr.erase(valstr.length() - 1, 1);
281 
282 
283 
284  while (valstr.length() > 0)
285  {
286  string::size_type i1 = valstr.find(",");
287  if (i1 != string::npos) {
288  string elem = valstr.substr(0, i1);
289  valstr.erase(0, i1 + 1);
290  remove_space(elem);
291  vec_tmp[size] = atoi(elem.c_str());
292  ++size;
293  } else {
294  string elem = valstr;
295  valstr.erase(0, elem.length());
296  remove_space(elem);
297  vec_tmp[size] = atoi(elem.c_str());
298  ++size;
299  }
300 
301  if (size > size_max) {
302  vout.crucial(m_vl, "%s: too large vector size.\n", class_name.c_str());
304  }
305  }
306 
307  vec.resize(size);
308  for (int i = 0; i < size; ++i) {
309  vec[i] = vec_tmp[i];
310  }
311 }
312 
313 
314 //====================================================================
315 void ParameterManager_YAML::convert_double_vector(std::vector<double>& vec,
316  string& valstr)
317 {
318  int size_max = 20;
319 
320  std::vector<double> vec_tmp(size_max);
321  int size = 0;
322 
323 
324  if (valstr[0] != '[') {
325  vout.crucial(m_vl, "%s: wrong format of vector.\n", class_name.c_str());
327  }
328  valstr.erase(0, 1);
329 
330  if (valstr[valstr.length() - 1] != ']') {
331  vout.crucial(m_vl, "%s: wrong format of vector.\n", class_name.c_str());
333  }
334  valstr.erase(valstr.length() - 1, 1);
335 
336 
337 
338  while (valstr.length() > 0)
339  {
340  string::size_type i1 = valstr.find(",");
341  if (i1 != string::npos) {
342  string elem = valstr.substr(0, i1);
343  valstr.erase(0, i1 + 1);
344  remove_space(elem);
345  // vec_tmp[size] = atof(elem.c_str());
346  vec_tmp[size] = EvalExpr(elem).parse();
347  ++size;
348  } else {
349  string elem = valstr;
350  valstr.erase(0, elem.length());
351  remove_space(elem);
352  // vec_tmp[size] = atof(elem.c_str());
353  vec_tmp[size] = EvalExpr(elem).parse();
354  ++size;
355  }
356 
357  if (size > size_max) {
358  vout.crucial(m_vl, "%s: too large vector size.\n", class_name.c_str());
360  }
361  }
362 
363  vec.resize(size);
364  for (int i = 0; i < size; ++i) {
365  vec[i] = vec_tmp[i];
366  }
367 }
368 
369 
370 //====================================================================
371 //============================================================END=====
bool find_int(const string &) const
Definition: parameters.cpp:220
BridgeIO vout
Definition: bridgeIO.cpp:278
void read_params(const std::string &params_file, Parameters *params)
read parameters from file.
static void abort()
terminate communicator
void set_int(const string &key, const int value)
Definition: parameters.cpp:262
void set_double_vector(const string &key, const std::vector< double > &value)
Definition: parameters.cpp:289
bool find_double(const string &) const
Definition: parameters.cpp:227
double parse()
Definition: evalexpr.cpp:191
int remove_space(std::string &)
remove spaces from both side of a string.
void convert_double_vector(std::vector< double > &vec, std::string &valstr)
convert from string to double vector.
static int broadcast(size_t size, void *data, int sender)
Class for parameters.
Definition: parameters.h:38
static const std::string class_name
void set_VerboseLevel(Bridge::VerboseLevel value)
Definition: parameters.cpp:316
Parameters * get_Parameters(const string &key) const
Definition: parameters.cpp:102
void set_string(const string &key, const string &value)
Definition: parameters.cpp:298
bool find_string(const string &) const
Definition: parameters.cpp:248
void convert_int_vector(std::vector< int > &vec, std::string &valstr)
convert from string to int vector.
int set_key_and_value(std::string &keystr, std::string &valstr, std::string &line)
extract key and value from a given line.
EvalExpr class for algebraic expression in parameter strings.
Definition: evalexpr.h:39
bool find_double_vector(const string &) const
Definition: parameters.cpp:241
void paranoiac(const char *format,...)
Definition: bridgeIO.cpp:99
void crucial(const char *format,...)
Definition: bridgeIO.cpp:48
void set_double(const string &key, const double value)
Definition: parameters.cpp:271
Bridge::VerboseLevel vl
Definition: checker.cpp:18
VerboseLevel
Definition: bridgeIO.h:39
Bridge::VerboseLevel m_vl
static int broadcast(int count, double *data, int sender)
broadcast array of double from sender.
static int nodeid()
alternative name for self().
Definition: communicator.h:92
void set_int_vector(const string &key, const std::vector< int > &value)
Definition: parameters.cpp:280
bool find_int_vector(const string &) const
Definition: parameters.cpp:234
static VerboseLevel set_verbose_level(const std::string &str)
Definition: bridgeIO.cpp:28