NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RunParameters.cpp
Go to the documentation of this file.
1 
2 #include "../Eval/Eval.hpp"
3 #include "../Math/RNG.hpp"
4 #include "../Param/RunParameters.hpp"
5 
6 #include "../nomad_version.hpp"
7 
8 
9 /*----------------------------------------*/
10 /* initializations (private) */
11 /*----------------------------------------*/
12 void NOMAD::RunParameters::init()
13 {
14  _typeName = "Run";
15 
16  try
17  {
18  #include "../Attribute/runAttributesDefinition.hpp"
19  registerAttributes( _definition );
20 
21  // Registered attributes using defined keywords (not in preprocessed special header file)
22  registerAttribute<NOMAD::Double>("EPSILON", NOMAD::DEFAULT_EPSILON, false,
23  " NOMAD precision for comparison of values ",
24  " \n \n . NOMAD precision for comparison of values \n "," advanced precision double(s) value(s) number(s) ");
25  registerAttribute<std::string>("UNDEF_STR", NOMAD::DEFAULT_UNDEF_STR, false,
26  "String for undefined values ",
27  " \n \n String for undefined values \n "," advanced undef(ined) value(s) string(s) ");
28  registerAttribute<std::string>("NOMAD_VERSION", NOMAD_VERSION_NUMBER, true,
29  " NOMAD version number (for runner) ",
30  " \n \n . NOMAD version number (optional) \n . If not compatible with current version will trigger exception \n " ," advanced nomad version(s) release(s) revision(s) " );
31  registerAttribute<std::string>("INF_STR", NOMAD::DEFAULT_INF_STR, false,
32  "String for infinite values",
33  " \n \n . String for infinite values \n "," advanced string(s) inf(inite) value(s) ");
34  registerAttribute<std::string>("PROBLEM_DIR", std::string(".") + NOMAD::DIR_SEP , false,
35  "Problem directory " ,
36  "\n \n . Problem directory \n . To complete \n "," problem dir(ectory) folder(s) ");
37  // Note: we cannot call checkAndComply() here, the default values
38  // are not valid.
39  }
40  catch (NOMAD::Exception & e)
41  {
42  std::string errorMsg = "Attribute registration failed: ";
43  errorMsg += e.what();
44  throw NOMAD::Exception(__FILE__,__LINE__, errorMsg);
45  }
46 }
47 
48 
49 /*----------------------------------------*/
50 /* check the parameters */
51 /*----------------------------------------*/
52 void NOMAD::RunParameters::checkAndComply(
53  const std::shared_ptr<NOMAD::EvaluatorControlParameters>& evaluatorControlParams)
54 {
55 
56  checkInfo();
57 
58  // VRM TODO implement the remove arguments.
59  // Should the remove be done before checking the toBeChecked flag, after it, or in
60  // a different function ?
61  if (!toBeChecked())
62  {
63  // Early out
64  return;
65  }
66 
67  // check the non-interpreted parameters:
68  const std::shared_ptr<NOMAD::ParameterEntry> pe = getNonInterpretedParamEntry();
69  if (pe)
70  {
71  string err = "File " + pe->getParamFile() + " line " + std::to_string(pe->getLine());
72  err += ": Unrecognized parameter: " + pe->getName() + ".";
73  if (getAttributeValueProtected<bool>("REJECT_UNKNOWN_PARAMETERS", false))
74  {
75  throw NOMAD::Exception(__FILE__,__LINE__, err);
76  }
77  else
78  {
79  std::cerr << err << " Ignore unknown parameter and continue." << std::endl;
80  }
81  }
82 
83  auto problemDir = getAttributeValueProtected<std::string>("PROBLEM_DIR", false);
84 
85  auto seed = getAttributeValueProtected<int>("SEED" ,false);
86  if ( seed < 0)
87  {
88  throw NOMAD::Exception(__FILE__,__LINE__, "Parameters check: SEED must be non-negative" );
89  }
90 
91  // CT todo modify to manage retro-compatiblity
92  auto version_number = getAttributeValueProtected<std::string>("NOMAD_VERSION", false);
93  if ( version_number != NOMAD_VERSION_NUMBER )
94  {
95  throw NOMAD::Exception(__FILE__,__LINE__, "Parameters check: VERSION_NUMBER is not compatible" );
96  }
97 
98  auto anisotropyFactor = getAttributeValueProtected<NOMAD::Double>("ANISOTROPY_FACTOR", false);
99  if ( anisotropyFactor <= 0)
100  {
101  throw NOMAD::Exception(__FILE__,__LINE__, "Parameters check: ANISOTROPY_FACTOR must be positive" );
102  }
103 
104  setStaticParameters();
105 
106  /*--------------------------------*/
107  /* Parallelism related parameters */
108  /*--------------------------------*/
109  // Ensure we can get value for BB_MAX_BLOCK_SIZE without throwing an exception.
110  if (evaluatorControlParams->toBeChecked())
111  {
112  evaluatorControlParams->checkAndComply();
113  }
114  auto blockSize = evaluatorControlParams->getAttributeValue<size_t>("BB_MAX_BLOCK_SIZE");
115  if (0 == blockSize)
116  {
117  throw NOMAD::Exception(__FILE__, __LINE__, "Parameter BB_MAX_BLOCK_SIZE must be positive");
118  }
119 #ifdef _OPENMP
120  else if (blockSize > 1)
121  {
122  if (getAttributeValueProtected<int>("NB_THREADS_OPENMP", false) != 1)
123  {
124  std::cerr << "Warning: Parallelism management: BB_MAX_BLOCK_SIZE is ";
125  std::cerr << "larger than 1 (value is " << blockSize << "). ";
126  std::cerr << "Setting parameter NB_THREADS_OPENMP to 1." << std::endl;
127  }
128  setAttributeValue("NB_THREADS_OPENMP", 1);
129  }
130 #endif
131 
132  /*------------------------*/
133  /* Hot restart parameters */
134  /* Update file names */
135  /*------------------------*/
136  std::string hotRestartFileName = getAttributeValueProtected<std::string>("HOT_RESTART_FILE",false);
137  if (!hotRestartFileName.empty())
138  {
139  NOMAD::completeFileName(hotRestartFileName, problemDir);
140  setAttributeValue("HOT_RESTART_FILE", hotRestartFileName);
141  }
142 
143 
144  auto hMax = getAttributeValueProtected<NOMAD::Double>("H_MAX_0", false);
145  if (hMax <= 0)
146  {
147  throw NOMAD::Exception(__FILE__,__LINE__, "Parameters check: H_MAX_0 must be positive");
148  }
149 
150  _toBeChecked = false;
151 
152 }
153 // End checkAndComply()
154 
155 
156 void NOMAD::RunParameters::setStaticParameters()
157 {
158  // Sub-method of checkAndComply() to set static variables of some classes.
159  NOMAD::RNG::setSeed ( getAttributeValueProtected<int>("SEED",false) );
160  NOMAD::Double::setEpsilon ( getAttributeValueProtected<NOMAD::Double>("EPSILON",false).todouble() );
161  NOMAD::Double::setUndefStr ( getAttributeValueProtected<std::string>("UNDEF_STR",false) );
162  NOMAD::Double::setInfStr ( getAttributeValueProtected<std::string>("INF_STR",false) );
163 
164  // Reset parameter values from these static values, to ensure coherence.
165  // This is bad because we have twice the same value for some parameters.
166  setAttributeValue ( "SEED", NOMAD::RNG::getSeed() );
167  setAttributeValue ( "EPSILON", NOMAD::Double(NOMAD::Double::getEpsilon()) );
168  setAttributeValue ( "UNDEF_STR", NOMAD::Double::getUndefStr() );
169  setAttributeValue ( "INF_STR", NOMAD::Double::getInfStr() );
170 }
171 
#define NOMAD_VERSION_NUMBER
Define version number.
const std::string DEFAULT_UNDEF_STR
Default undefined value string used by Double.
Definition: defines.hpp:75
const char DIR_SEP
Directory separator.
Definition: defines.hpp:53
const std::string DEFAULT_INF_STR
Default infinity string used by Double.
Definition: defines.hpp:69
void completeFileName(std::string &filename, const std::string &problemDir, bool addSeed=false, int seed=0)
Definition: fileutils.cpp:254
const double DEFAULT_EPSILON
Default epsilon used by Double.
Definition: defines.hpp:63