12 #ifndef SST_CORE_PARAM_H 
   13 #define SST_CORE_PARAM_H 
   15 #include "sst/core/from_string.h" 
   16 #include "sst/core/output.h" 
   17 #include "sst/core/serialization/serializable.h" 
   18 #include "sst/core/serialization/serializer.h" 
   19 #include "sst/core/threadsafe.h" 
   31 int main(
int argc, 
char* argv[]);
 
   36 class ConfigComponent;
 
   37 class SSTModelDescription;
 
   40 class ConfigGraphOutput;
 
   58     struct KeyCompare : std::binary_function<std::string, std::string, bool>
 
   60         bool operator()(
const std::string& X, 
const std::string& Y)
 const 
   62             const char* x = X.c_str();
 
   63             const char* y = Y.c_str();
 
   65 #define EAT_VAR(A, B)                                              \ 
   67         if ( *x == '%' && (*(x + 1) == '(' || *(x + 1) == 'd') ) { \ 
   73                 } while ( *x && *x != ')' );                       \ 
   76             if ( *x != 'd' ) goto NO_VARIABLE;                     \ 
   79             while ( *y && isdigit(*y) )                            \ 
   89                     if ( 
'\0' == *x ) 
return false;
 
   94                     if ( *x < *y ) 
return true;
 
   98             if ( !(*x) && (*y) ) 
return true;
 
  118     inline T convert_value(
const std::string& key, 
const std::string& val)
 const 
  121             return SST::Core::from_string<T>(val);
 
  123         catch ( 
const std::invalid_argument& e ) {
 
  124             std::string msg = 
"Params::find(): No conversion for value: key = " + key + 
", value =  " + val +
 
  125                               ".  Original error: " + e.what();
 
  126             std::invalid_argument t(msg);
 
  145     inline T find_impl(
const std::string& k, T default_value, 
bool& found)
 const 
  149         const std::string& value = getString(k, found);
 
  150         if ( !found ) { 
return default_value; }
 
  152             return convert_value<T>(k, value);
 
  167     inline T find_impl(
const std::string& k, 
const std::string& default_value, 
bool& found)
 const 
  170         const std::string& value = getString(k, found);
 
  173                 return SST::Core::from_string<T>(default_value);
 
  175             catch ( 
const std::invalid_argument& e ) {
 
  176                 std::string msg = 
"Params::find(): Invalid default value specified: key = " + k +
 
  177                                   ", value =  " + default_value + 
".  Original error: " + e.what();
 
  178                 std::invalid_argument t(msg);
 
  185                 return SST::Core::from_string<T>(value);
 
  187             catch ( 
const std::invalid_argument& e ) {
 
  188                 std::string msg = 
"Params::find(): No conversion for value: key = " + k + 
", value =  " + value +
 
  189                                   ".  Original error: " + e.what();
 
  190                 std::invalid_argument t(msg);
 
  196     typedef std::map<uint32_t, std::string>::const_iterator const_iterator; 
 
  198     const std::string& getString(
const std::string& name, 
bool& found) 
const;
 
  223     void cleanToken(std::string& token) 
const;
 
  229     void getDelimitedTokens(
const std::string& value, 
char delim, std::vector<std::string>& tokens) 
const;
 
  244         bool old       = verify_enabled;
 
  245         verify_enabled = enable;
 
  320     typename std::enable_if<not std::is_same<std::string, T>::value, T>::type
 
  321     find(
const std::string& k, T default_value, 
bool& found)
 const 
  323         return find_impl<T>(k, default_value, found);
 
  337     T 
find(
const std::string& k, 
const std::string& default_value, 
bool& found)
 const 
  339         return find_impl<T>(k, default_value, found);
 
  353     typename std::enable_if<std::is_same<bool, T>::value, T>::type
 
  354     find(
const std::string& k, 
const char* default_value, 
bool& found)
 const 
  356         if ( 
nullptr == default_value ) { 
return find_impl<T>(k, 
static_cast<T
>(0), found); }
 
  357         return find_impl<T>(k, std::string(default_value), found);
 
  369     T 
find(
const std::string& k, T default_value)
 const 
  372         return find_impl<T>(k, default_value, tmp);
 
  385     T 
find(
const std::string& k, 
const std::string& default_value)
 const 
  388         return find_impl<T>(k, default_value, tmp);
 
  402     typename std::enable_if<std::is_same<bool, T>::value, T>::type
 
  403     find(
const std::string& k, 
const char* default_value)
 const 
  406         if ( 
nullptr == default_value ) { 
return find_impl<T>(k, 
static_cast<T
>(0), tmp); }
 
  407         return find_impl<T>(k, std::string(default_value), tmp);
 
  418     T 
find(
const std::string& k)
 const 
  421         T    default_value = T();
 
  422         return find_impl<T>(k, default_value, tmp);
 
  436     typename std::enable_if<not std::is_same<bool, T>::value, T>::type 
find(
const std::string& k, 
bool& found)
 const 
  438         T default_value = T();
 
  439         return find_impl<T>(k, default_value, found);
 
  509         std::string value = getString(k, found);
 
  510         if ( !found ) 
return;
 
  514         if ( value.front() != 
'[' || value.back() != 
']' ) {
 
  515             vec.push_back(convert_value<T>(k, value));
 
  519         value = value.substr(1, value.size() - 2);
 
  522         std::vector<std::string> tokens;
 
  523         getDelimitedTokens(value, 
',', tokens);
 
  526         for ( 
auto& str : tokens ) {
 
  532         for ( 
auto& val : tokens ) {
 
  533             vec.push_back(convert_value<T>(k, val));
 
  604         std::string value = getString(k, found);
 
  605         if ( !found ) 
return;
 
  610         if ( value.find(
"set([") == 0 ) {
 
  615             value                     = value.substr(4, value.length() - 5);
 
  617             value[value.length() - 1] = 
'}';
 
  623         if ( value.front() != 
'{' || value.back() != 
'}' ) {
 
  624             set.insert(convert_value<T>(k, value));
 
  628         value = value.substr(1, value.size() - 2);
 
  631         std::vector<std::string> tokens;
 
  632         getDelimitedTokens(value, 
',', tokens);
 
  635         for ( 
auto& str : tokens ) {
 
  641         for ( 
auto& val : tokens ) {
 
  642             set.insert(convert_value<T>(k, val));
 
  710     template <
class keyT, 
class valT>
 
  716         std::string value = getString(k, found);
 
  717         if ( !found ) 
return;
 
  720         if ( value.front() != 
'{' || value.back() != 
'}' ) {
 
  721             std::string msg = 
"Invalid format for parameter to be parsed as a map: " + value;
 
  724             throw std::invalid_argument(msg);
 
  727         value = value.substr(1, value.size() - 2);
 
  730         std::vector<std::string> tokens;
 
  731         getDelimitedTokens(value, 
',', tokens);
 
  735         std::vector<std::string> kvpair;
 
  736         for ( 
auto& x : tokens ) {
 
  738             getDelimitedTokens(x, 
':', kvpair);
 
  740             if ( kvpair.size() != 2 ) {
 
  741                 std::string msg = 
"Invalid format for map key/value pair: " + x;
 
  744                 throw std::invalid_argument(msg);
 
  748             cleanToken(kvpair[0]);
 
  749             cleanToken(kvpair[1]);
 
  752             map[convert_value<keyT>(k, kvpair[0])] = convert_value<valT>(k, kvpair[1]);
 
  767         std::string value = getString(k, found);
 
  768         if ( !found ) 
return false;
 
  770         if ( (value.find(
"[") == std::string::npos) || (value.find(
"]") == std::string::npos) ) { 
return false; }
 
  775     void print_all_params(std::ostream& os, 
const std::string& prefix = 
"") 
const;
 
  789     void insert(
const std::string& key, 
const std::string& value, 
bool overwrite = 
true);
 
  804     std::set<std::string> 
getKeys() 
const;
 
  850     friend class SST::Core::ConfigGraphOutput;
 
  857     void verifyParam(const 
key_type& k) const;
 
  867     void addGlobalParamSet(const std::
string& set);
 
  882     insert_global(const std::
string& set, const 
key_type& key, const 
key_type& value, 
bool overwrite = true);
 
  892     static std::map<std::
string, std::
string> getGlobalParamSet(const std::
string& name);
 
  901     static std::vector<std::
string> getGlobalParamSetNames();
 
  910     std::vector<std::
string> getLocalKeys() const;
 
  920     std::vector<std::
string> getSubscribedGlobalParamSets() const;
 
  928     void verifyKey(const 
key_type& k) const;
 
  932     std::map<uint32_t, std::
string>               my_data;
 
  933     std::vector<std::map<uint32_t, std::
string>*> data;
 
  936     static 
bool                                   g_verify_enabled;
 
  938     static uint32_t getKey(const std::
string& str);
 
  945     static const std::
string& getParamName(uint32_t 
id);
 
  948     friend 
int ::main(
int argc, 
char* argv[]);
 
  950     static std::map<std::
string, uint32_t> keyMap;
 
  951     static std::vector<std::
string>        keyMapReverse;
 
  952     static SST::Core::ThreadSafe::
Spinlock keyLock;
 
  953     static SST::Core::ThreadSafe::
Spinlock globalLock;
 
  954     static uint32_t                        nextKeyID;
 
  956     static std::map<std::
string, std::map<uint32_t, std::
string>> global_params;
 
  962 #define SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(type)                                          \ 
  964     type Params::find(const std::string& k, type default_value, bool& found) const;               \ 
  966     type Params::find(const std::string& k, const std::string& default_value, bool& found) const; \ 
  968     type Params::find(const std::string& k, type default_value) const;                            \ 
  970     type Params::find(const std::string& k, const std::string& default_value) const;              \ 
  972     type Params::find(const std::string& k) const; 
  975  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int32_t)
 
  976  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint32_t)
 
  977  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(int64_t)
 
  978  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(uint64_t)
 
  979  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
bool)
 
  980  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
float)
 
  981  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
double)
 
  982  SST_PARAMS_DECLARE_TEMPLATE_SPECIALIZATION(
UnitAlgebra)
 
  987  std::
string Params::
find<std::
string>(const std::
string& k, const std::
string& default_value, 
bool &found) const;
 
  992 #endif // SST_CORE_PARAMS_H 
void find_map(const key_type &k, std::map< keyT, valT > &map) const 
Find a Parameter value in the set, and return its value as a map<keyT,valT>. 
Definition: params.h:711
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file...
Definition: output.h:51
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:34
void find_array(const key_type &k, std::vector< T > &vec) const 
Find a Parameter value in the set, and return its value as a vector of T's. 
Definition: params.h:504
T find(const std::string &k, T default_value) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:369
Represents the configuration of a generic component. 
Definition: configGraph.h:217
void pushAllowedKeys(const KeySet_t &keys)
Definition: params.cc:203
bool contains(const key_type &k) const 
Search the container for a particular key. 
Definition: params.cc:194
Params get_scoped_params(const std::string &scope) const 
Returns a new parameter object with parameters that match the specified scoped prefix (scopes are sep...
Definition: params.cc:175
A Configuration Graph A graph representing Components and Links. 
Definition: configGraph.h:389
size_t count(const key_type &k) const 
Finds the number of elements with given key. 
Definition: params.cc:89
T find(const std::string &k) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:418
static void enableVerify()
Enable, on a global scale, parameter verification. 
Definition: params.h:254
Definition: serializable.h:118
bool empty() const 
Returns true if the Params is empty. 
Definition: params.cc:50
void clear()
Erases all elements, including deleting reference to global param sets. 
Definition: params.cc:81
Params()
Create a new, empty Params. 
Definition: params.cc:55
bool enableVerify(bool enable)
Enable or disable parameter verification on an instance of Params. 
Definition: params.h:242
T find(const std::string &k, const std::string &default_value) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:385
std::enable_if< not std::is_same< bool, T >::value, T >::type find(const std::string &k, bool &found) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:436
void find_set(const key_type &k, std::set< T > &set) const 
Find a Parameter value in the set, and return its value as a set of T's. 
Definition: params.h:599
Definition: threadsafe.h:121
std::enable_if< std::is_same< bool, T >::value, T >::type find(const std::string &k, const char *default_value, bool &found) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:354
size_t size() const 
Returns the size of the Params. 
Definition: params.cc:44
Parameter store. 
Definition: params.h:55
void popAllowedKeys()
Removes the most recent set of keys considered allowed. 
Definition: params.cc:209
T find(const std::string &k, const std::string &default_value, bool &found) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:337
Base class for Model Generation. 
Definition: sstmodel.h:25
std::set< key_type, KeyCompare > KeySet_t
Definition: params.h:233
std::string key_type
Definition: params.h:232
std::set< std::string > getKeys() const 
Get all the keys contained in the Params object. 
Definition: params.cc:163
Params & operator=(const Params &old)
Assignment operator. 
Definition: params.cc:70
std::enable_if< not std::is_same< std::string, T >::value, T >::type find(const std::string &k, T default_value, bool &found) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:321
void insert(const std::string &key, const std::string &value, bool overwrite=true)
Add a key/value pair into the param object. 
Definition: params.cc:140
std::enable_if< std::is_same< bool, T >::value, T >::type find(const std::string &k, const char *default_value) const 
Find a Parameter value in the set, and return its value as a type T. 
Definition: params.h:403
Performs Unit math in full precision. 
Definition: unitAlgebra.h:106
bool is_value_array(const key_type &k) const 
Checks to see if the value associated with the given key is considered to be an array. 
Definition: params.h:764
void print_all_params(std::ostream &os, const std::string &prefix="") const 
Print all key/value parameter pairs to specified ostream. 
Definition: params.cc:100