SST/macro
sim_parameters.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SPROCKIT_COMMON_SIM_PARAMETERS_H_INCLUDED
13 #define SPROCKIT_COMMON_SIM_PARAMETERS_H_INCLUDED
14 
15 #include <sprockit/debug.h>
16 #include <sprockit/unordered.h>
17 
18 #include <iostream>
19 #include <list>
20 #include <vector>
21 #include <set>
22 
23 
25 DeclareDebugSlot(read_params)
26 DeclareDebugSlot(write_params)
27 
28 namespace sprockit {
29 
30 class param_assign {
31  public:
32  param_assign(std::string& p, const std::string& k) :
33  param_(p), key_(k)
34  {
35  }
36 
37  void operator=(int a);
38  void operator=(double x);
39  void operator=(const std::string& str){
40  param_ = str;
41  }
42 
43  const std::string& setByteLength(long x, const char* units);
44  const std::string& setBandwidth(double x, const char* units);
45  const std::string& setFrequency(double x, const char* units);
46  const std::string& setTime(double x, const char* units);
47  const std::string& setValue(double x, const char* units);
48  const std::string& set(const char* str);
49  const std::string& set(const std::string& str);
50 
51  long getByteLength() const;
52  double getBandwidth() const;
53  double getTime() const;
54  double getFrequency() const;
55 
56  operator int() const;
57 
58  operator double() const;
59 
60  operator std::string() const {
61  return param_;
62  }
63 
64  private:
65  std::string& param_;
66  const std::string& key_;
67 
68 };
69 
70 class param_bcaster {
71  public:
72  virtual void
73  bcast(void* buf, int size, int me, int root) = 0;
74 
75  void
76  bcast_string(std::string& str, int me, int root);
77 };
78 
79 class sim_parameters {
80 
81  public:
82  struct parameter_entry
83  {
84  parameter_entry() : read(false) {}
85  std::string value;
86  bool read;
87  };
88 
89  typedef spkt_unordered_map<std::string, parameter_entry> key_value_map;
90 
91  sim_parameters();
92 
93  sim_parameters(const key_value_map& p);
94 
95  sim_parameters(const std::string& filename);
96 
97  /**
98  * In a parallel environment (abstracted through a param_bcaster object),
99  * have rank 0 read params from a file and bcast result to all other ranks
100  * @param params A parameter object (already allocated)
101  * @param me The rank of the calling process
102  * @param nproc The total number of ranks
103  * @param filename
104  * @param bcaster
105  */
106  static void
107  parallel_build_params(sprockit::sim_parameters* params, int me, int nproc, const std::string& filename, param_bcaster* bcaster);
108 
109  virtual ~sim_parameters();
110 
111  void
112  remove_param(const std::string &key);
113 
114  std::string
115  get_param(const std::string& key, bool throw_on_error = true);
116 
117  std::string
118  get_scoped_param(const std::string& key, bool throw_on_error = true);
119 
120  std::string
121  reread_param(const std::string& key) {
122  return get_param(key);
123  }
124 
125  std::string
126  reread_optional_param(const std::string& key, const std::string& def) {
127  return get_optional_param(key, def);
128  }
129 
130  /// Return the value of the keyword if it exists. Otherwise return
131  /// a default value.
132  /// @param key gives the keyword
133  /// @param def gives the default value (used if has_param(key) is false)
134  /// @return the value if it exists, otherwise the default
135  std::string
136  get_optional_param(const std::string &key, const std::string &def);
137 
138  std::string
139  deprecated_optional_param(const std::string &key, const std::string &def);
140 
141  std::string
142  deprecated_param(const std::string& key);
143 
144  void
145  add_param(const std::string& key, const std::string& val);
146 
147  void
148  copy_param(const std::string& oldname, const std::string& newname);
149 
150  void
151  copy_optional_param(const std::string& oldname, const std::string& newname);
152 
153  void
154  add_param_override(const std::string& key, const std::string& val);
155 
156  void
157  add_param_override(const std::string &key, double val);
158 
159  void
160  add_param_override(const std::string& key, double val, const char* units);
161 
162  void
163  add_param_override(const std::string& key, int val);
164 
165  void
166  combine_into(sim_parameters* sp,
167  bool fail_on_existing = false,
168  bool override_existing = true,
169  bool mark_as_read = true);
170 
171  void
172  print_params(std::ostream& os, bool pretty_print, std::list<std::string>& namespaces) const;
173 
174  void
175  print_params(std::ostream& os = std::cerr) const {
176  std::list<std::string> ns;
177  print_params(os, false, ns);
178  }
179 
180  void
181  pretty_print_params(std::ostream& os = std::cerr) const {
182  std::list<std::string> ns;
183  print_params(os, true, ns);
184  }
185 
186  bool
187  print_unread_params(std::ostream& os = std::cerr) const;
188 
189  bool
190  has_param(const std::string& key) const;
191 
192  bool
193  has_scoped_param(const std::string& key) const;
194 
195  int
196  get_int_param(const std::string& key);
197 
198  int
199  deprecated_int_param(const std::string& key);
200 
201  int
202  deprecated_optional_int_param(const std::string &key, int def);
203 
204  int
205  reread_int_param(const std::string& key);
206 
207  int
208  reread_optional_int_param(const std::string& key, int def);
209 
210  /// Return the value of the keyword if it exists. Otherwise return
211  /// a default value.
212  /// @param key gives the keyword
213  /// @param def gives the default value (used if has_param(key) is false)
214  /// @return the value if it exists, otherwise the default
215  int
216  get_optional_int_param(const std::string &key, int def);
217 
218  /// Returns the value of the key as a boolean.
219  bool
220  get_bool_param(const std::string &key);
221 
222  bool
223  reread_bool_param(const std::string& key);
224 
225  bool
226  deprecated_bool_param(const std::string& key);
227 
228  bool
229  deprecated_optional_bool_param(const std::string& key, bool def);
230 
231  bool
232  reread_optional_bool_param(const std::string& key, bool def);
233 
234  /// Return the value of the keyword if it exists. Otherwise return
235  /// a default value.
236  /// @param key gives the keyword
237  /// @param def gives the default value (used if has_param(key) is false)
238  /// @return the value if it exists, otherwise the default
239  bool
240  get_optional_bool_param(const std::string &key, int def);
241 
242  double
243  get_bandwidth_param(const std::string& key);
244 
245  /**
246  @param key The parameter name
247  @return A value with units. This loops through bandwidth, frequency, time, etc
248  to return any value that can have units. Everything is normalized to baseslines
249  of B/s, s, Hz, etc
250  */
251  double
252  get_quantity(const std::string& key);
253 
254  double
255  get_optional_quantity(const std::string& key, double def);
256 
257  double
258  deprecated_bandwidth_param(const std::string& key);
259 
260  double
261  reread_bandwidth_param(const std::string& key);
262 
263  double
264  reread_optional_bandwidth_param(const std::string& key, double def);
265 
266  double
267  deprecated_optional_bandwidth_param(const std::string& key, double def);
268 
269  double
270  get_optional_bandwidth_param(const std::string &key, double def);
271 
272  double
273  get_optional_bandwidth_param(
274  const std::string& key,
275  const std::string& def);
276 
277  long
278  get_byte_length_param(const std::string& key);
279 
280  long
281  get_optional_byte_length_param(const std::string& key, long def);
282 
283  long
284  deprecated_byte_length_param(const std::string& key);
285 
286  long
287  deprecated_optional_byte_length_param(const std::string& key, long def);
288 
289  double
290  get_optional_freq_param(const std::string& key, double def);
291 
292  double
293  get_freq_param(const std::string& key);
294 
295  double
296  deprecated_freq_param(const std::string& key);
297 
298  double
299  deprecated_optional_freq_param(const std::string& key, double def);
300 
301  /// Return the value of the keyword if it exists. Otherwise return
302  /// a default value.
303  /// @param key gives the keyword
304  /// @param def gives the default value (used if has_param(key) is false)
305  /// @return the value if it exists, otherwise the default
306  long
307  get_optional_long_param(const std::string &key, long def);
308 
309  long
310  get_long_param(const std::string& key);
311 
312  long
313  deprecated_long_param(const std::string& key);
314 
315  long
316  deprecated_optional_long_param(const std::string& key, long def);
317 
318  long
319  reread_long_param(const std::string& key);
320 
321  void
322  reread_optional_long_param(const std::string& key);
323 
324  double
325  get_double_param(const std::string& key);
326 
327  /// Return the value of the keyword if it exists. Otherwise return
328  /// a default value.
329  /// @param key gives the keyword
330  /// @param def gives the default value (used if has_param(key) is false)
331  /// @return the value if it exists, otherwise the default
332  double
333  get_optional_double_param(const std::string &key, double def);
334 
335  double
336  reread_double_param(const std::string& key);
337 
338  double
339  reread_optional_double_param(const std::string &key, double def);
340 
341  double
342  deprecated_double_param(const std::string& key);
343 
344  double
345  deprecated_optional_double_param(const std::string& key, double def);
346 
347  double
348  get_time_param(const std::string& key);
349 
350  double
351  get_optional_time_param(const std::string &key, double def);
352 
353  double
354  deprecated_optional_time_param(const std::string &key, double def);
355 
356  double
357  deprecated_time_param(const std::string& key);
358 
359  void
360  get_vector_param(const std::string& key, std::vector<int>& vals);
361 
362  void
363  get_vector_param(const std::string& key, std::vector<double>& vals);
364 
365  void
366  get_vector_param(const std::string& key, std::vector<std::string>& vals);
367 
368  sim_parameters*
369  get_namespace(const std::string& ns);
370 
371  sim_parameters*
372  get_optional_namespace(const std::string& ns);
373 
374  typedef std::set<int>::const_iterator id_iterator;
375 
376  bool
377  has_namespace(const std::string& ns) const;
378 
379  void
380  parse_file(const std::string& fname, bool fail_on_existing, bool override_existing);
381 
382  void
383  parse_stream(std::istream& in, bool fail_on_existing, bool override_existing);
384 
385  void
386  parse_line(const std::string& line, bool fail_on_existing, bool override_existing);
387 
388  /**
389  @param key
390  @param value
391  @param fail_on_existing Fail if the parameter named by key already exists
392  */
393  void
394  parse_keyval(const std::string& key,
395  const std::string& value,
396  bool fail_on_existing,
397  bool override_existing,
398  bool mark_as_read);
399 
400  param_assign
401  operator[](const std::string& key);
402 
403  sim_parameters* top_parent();
404 
405  key_value_map::iterator begin() { return params_.begin(); }
406  key_value_map::const_iterator begin() const { return params_.begin(); }
407 
408  key_value_map::iterator end() { return params_.end(); }
409  key_value_map::const_iterator end() const { return params_.end(); }
410 
411  typedef std::map<std::string, sim_parameters*>::iterator namespace_iterator;
412  typedef std::map<std::string, sim_parameters*>::const_iterator const_namespace_iterator;
413  namespace_iterator ns_begin() { return subspaces_.begin(); }
414  const_namespace_iterator ns_begin() const { return subspaces_.begin(); }
415  namespace_iterator ns_end() { return subspaces_.end(); }
416  const_namespace_iterator ns_end() const { return subspaces_.end(); }
417 
418  private:
419  std::map<std::string, sim_parameters*> subspaces_;
420  std::map<std::string, std::string> variables_;
421 
422  sim_parameters* parent_;
423 
424  static sim_parameters* empty_ns_params_;
425 
426  std::string namespace_;
427 
428  key_value_map params_;
429 
430  void
431  throw_key_error(const std::string& key) const;
432 
433  void
434  set_parent(sim_parameters* p) {
435  parent_ = p;
436  }
437 
438  void
439  set_namespace(const std::string& str) {
440  namespace_ = str;
441  }
442 
443  bool
444  local_has_namespace(const std::string& ns) const {
445  return subspaces_.find(ns) != subspaces_.end();
446  }
447 
448  void
449  split_line(const std::string& line, std::pair<std::string, std::string>& p);
450 
451  void
452  try_to_parse(const std::string& fname, bool fail_on_existing, bool override_existing);
453 
454  void
455  print_params(const key_value_map& pmap, std::ostream& os, bool pretty_print, std::list<std::string>& ns) const;
456 
457  void
458  do_add_param(const std::string& key,
459  const std::string& val,
460  bool fail_on_existing,
461  bool override_existing,
462  bool mark_as_read);
463 
464 
465  sim_parameters*
466  get_scope_and_key(const std::string& key, std::string& final_key);
467 
468  bool
469  get_param(std::string& inout, const std::string& key);
470 
471  bool
472  get_scoped_param(std::string& inout, const std::string& key);
473 
474 };
475 
476 }
477 
478 
479 
480 
481 #endif
482 
DeclareDebugSlot(params) DeclareDebugSlot(read_params) DeclareDebugSlot(write_params) namespace sprockit
const char * get_param(char *str)
double get_double_param(char *str)
bool get_bool_param(char *str)
int get_optional_int_param(char *str, int val)
int get_int_param(char *str)
long get_long_param(char *str)
long get_optional_long_param(char *str, long val)
const char * get_optional_param(char *str, char *val)
bool get_optional_bool_param(char *str, bool val)
double get_optional_double_param(char *str, double val)