OpenM++ runtime library (libopenm)
omModel.h
Go to the documentation of this file.
1 
5 // Copyright (c) 2013-2015 OpenM++
6 // This code is licensed under the MIT license (see LICENSE.txt for details)
7 
8 #ifndef OM_H_MODEL_H
9 #define OM_H_MODEL_H
10 
11 #include <memory>
12 #include <cfloat>
13 #include <forward_list>
14 #include <type_traits>
15 #include <typeinfo>
16 #include <vector>
17 using namespace std;
18 
19 #include "omModelRunState.h"
20 #include "omLog.h"
21 #include "omError.h"
22 #include "omVersion.h"
23 
24 namespace openm
25 {
27  struct RunOptions
28  {
31 
34 
36  bool useSparse;
37 
39  double nullValue;
40 
43 
45  double progressStep;
46 
48  RunOptions(void) :
49  subValueCount(1),
50  subValueId(0),
51  useSparse(false),
52  nullValue(FLT_MIN),
53  progressPercent(0),
54  progressStep(0.0)
55  { }
56  ~RunOptions(void) noexcept { }
57  };
58 
60  struct IRunBase
61  {
62  virtual ~IRunBase(void) noexcept = 0;
63 
65  virtual int parameterIdByName(const char * i_name) const = 0;
66 
68  virtual int parameterSubCount(int i_paramId) const = 0;
69 
71  virtual int parameterSelfSubCount(int i_paramId) const = 0;
72 
74  virtual int parameterSubValueIndex(int i_paramId, int i_subId) const = 0;
75 
77  virtual bool isUseSubValue(int i_subId) const = 0;
78 
80  virtual void readParameter(const char * i_name, int i_subId, const type_info & i_type, size_t i_size, void * io_valueArr) = 0;
81  };
82 
84  struct IModel
85  {
86  virtual ~IModel(void) noexcept = 0;
87 
89  virtual int subValueCount(void) const noexcept = 0;
90 
92  virtual int subValueId(void) const noexcept = 0;
93 
95  virtual const RunOptions * runOptions(void) const = 0;
96 
98  virtual int parameterSubValueIndex(const char * i_name) const = 0;
99 
101  virtual void writeOutputTable(const char * i_name, size_t i_size, forward_list<unique_ptr<double> > & io_accValues) = 0;
102 
104  virtual void updateProgress(int i_count, double i_value = 0.0) = 0;
105  };
106 
109  {
111  const char * name;
112 
114  const type_info & typeOf;
115 
117  const size_t size;
118  };
119 
121  extern const size_t PARAMETER_NAME_ARR_LEN;
122 
125 
127  extern const char modelUnknownErrorMessage[];
128 
131 
133  extern const char simulationUnknownErrorMessage[];
134 
137 
139  template<typename TVal> vector<TVal> read_om_parameter(IRunBase * const i_runBase, const char * i_name)
140  {
141  int paramId = i_runBase->parameterIdByName(i_name);
142  int allCount = i_runBase->parameterSubCount(paramId); // number of parameter sub-values
143  int selfCount = i_runBase->parameterSelfSubCount(paramId); // number of sub-values for current process
144 
145  // storage array: parameter values for current process
146  // extra parameter value for exchange between root and child process
147  vector<TVal> valueVec(selfCount);
148  TVal extraVal;
149  bool isStr = typeid(TVal) == typeid(string); // no default null values for string parameters
150 
151  // read sub-values and place into storage array or send to child process
152  for (int nSub = 0; nSub < allCount; nSub++) {
153 
154  void * pData = &extraVal;
155  if (!isStr) {
156  extraVal = numeric_limits<TVal>::quiet_NaN(); // set default null value
157  }
158  i_runBase->readParameter(i_name, nSub, typeid(TVal), 1, pData);
159 
160  if (allCount <= 1) {
161  valueVec[0] = extraVal;
162  }
163  else {
164  if (i_runBase->isUseSubValue(nSub)) valueVec[i_runBase->parameterSubValueIndex(paramId, nSub)] = extraVal;
165  }
166  }
167  return valueVec;
168  }
169 
171  template<typename TVal> vector<unique_ptr<TVal[]>> read_om_parameter(IRunBase * const i_runBase, const char * i_name, size_t i_size)
172  {
173  int paramId = i_runBase->parameterIdByName(i_name);
174  int allCount = i_runBase->parameterSubCount(paramId); // number of parameter sub-values
175  int selfCount = i_runBase->parameterSelfSubCount(paramId); // number of sub-values for current process
176 
177  if (i_size <= 0) throw ModelException("invalid size: %zd of parameter %s", i_size, i_name);
178 
179  // storage array: parameter values for current process
180  vector<unique_ptr<TVal[]>> valueVec(selfCount);
181  for (auto & p : valueVec) {
182  p.reset(new TVal[i_size]);
183  }
184 
185  unique_ptr<TVal> extraVal; // extra parameter value for exchange between root and child process
186  if (allCount > 1) {
187  extraVal.reset(new TVal[i_size]);
188  }
189  bool isStr = typeid(TVal) == typeid(string); // no default null values for string parameters
190 
191  // read sub-values and place into storage array or send to child process
192  for (int nSub = 0; nSub < allCount; nSub++) {
193 
194  void * pData = valueVec[0].get();
195  if (allCount > 1) {
196  pData =
197  i_runBase->isUseSubValue(nSub) ?
198  valueVec[i_runBase->parameterSubValueIndex(paramId, nSub)].get() :
199  extraVal.get();
200  }
201  // set default null values and read parameter
202  if (!isStr) {
203  fill(static_cast<TVal *>(pData), &(static_cast<TVal *>(pData))[i_size], numeric_limits<TVal>::quiet_NaN());
204  }
205  i_runBase->readParameter(i_name, nSub, typeid(TVal), i_size, pData);
206  }
207  return valueVec;
208  }
209 }
210 
211 //
212 // modeling library import and export
213 //
214 #ifdef __cplusplus
215  extern "C" {
216 #endif
217 
219 extern const char * OM_MODEL_NAME;
220 
222 extern const char * OM_MODEL_DIGEST;
223 
225 int main(int argc, char ** argv);
226 
228 typedef void(*OM_RUN_ONCE_HANDLER)(openm::IRunBase * const i_runBase);
230 
232 typedef void(*OM_RUN_INIT_HANDLER)(openm::IRunBase * const i_runBase);
234 
236 typedef void(*OM_STARTUP_HANDLER)(openm::IModel * const i_model);
238 
240 typedef void (*OM_EVENT_LOOP_HANDLER)(openm::IModel * const i_model);
242 
244 typedef void (*OM_SHUTDOWN_HANDLER)(openm::IModel * const i_model);
246 
249 
250 #ifdef __cplusplus
251  }
252 #endif
253 
254 #endif // OM_H_MODEL_H
model run state public interface: thread safe
Definition: omModelRunState.h:140
const size_t size
parameter size (number of parameter values)
Definition: omModel.h:117
int main(int argc, char **argv)
main entry point
Definition: main.cpp:75
void(* OM_SHUTDOWN_HANDLER)(openm::IModel *const i_model)
model shutdown method: save output results
Definition: omModel.h:244
void(* OM_RUN_INIT_HANDLER)(openm::IRunBase *const i_runBase)
model run initialization: read input parameters
Definition: omModel.h:232
OpenM++: public interface for log and trace support.
virtual int parameterIdByName(const char *i_name) const =0
return index of parameter by name
OpenM++: runtime version.
OM_SHUTDOWN_HANDLER ModelShutdownHandler
model shutdown method: save output results
Definition: main.cpp:63
OM_RUN_ONCE_HANDLER RunOnceHandler
model one-time initialization
Definition: main.cpp:51
public interface to initialize model run and input parameters
Definition: omModel.h:60
openm::IModelRunState * theModelRunState
public interface of process-wide model run state: status, progress, update times
Definition: modelRunState.cpp:33
const size_t PARAMETER_NAME_ARR_LEN
size of parameters list: number of model input parameters
const ParameterNameSizeItem parameterNameSizeArr[]
model input parameters name, type and size
const char simulationUnknownErrorMessage[]
simulation exception default error message: "unknown error in simulation"
Definition: modelBase.cpp:15
int progressPercent
if positive then used for simulation progress reporting, ex: every 10%
Definition: omModel.h:42
OpenmException< 4000, modelUnknownErrorMessage > ModelException
modeling library exception
Definition: omModel.h:130
const char * name
input parameter name
Definition: omModel.h:111
openM++ namespace
Definition: argReader.cpp:16
double progressStep
if positive then used for simulation progress reporting, ex: every 1000 cases or every 0...
Definition: omModel.h:45
const char * OM_MODEL_NAME
model name
model input parameter name, type and size
Definition: omModel.h:108
virtual void readParameter(const char *i_name, int i_subId, const type_info &i_type, size_t i_size, void *io_valueArr)=0
read model parameter
int subValueId
sub-value index for current modeling thread
Definition: omModel.h:33
void(* OM_EVENT_LOOP_HANDLER)(openm::IModel *const i_model)
model event loop: user code entry point
Definition: omModel.h:240
vector< unique_ptr< TVal[]> > read_om_parameter(IRunBase *const i_runBase, const char *i_name, size_t i_size)
read array parameter value or all sub-values for the current process
Definition: omModel.h:171
OM_STARTUP_HANDLER ModelStartupHandler
model startup method: initialize sub-value
Definition: main.cpp:57
OpenM++ modeling library: model run state public interface.
OpenmException< 4000, simulationUnknownErrorMessage > SimulationException
simulation exception
Definition: omModel.h:136
virtual int parameterSubValueIndex(int i_paramId, int i_subId) const =0
return index of parameter sub-value in the storage array of sub-values
const char * OM_MODEL_DIGEST
model metadata digest: unique model key
OM_RUN_INIT_HANDLER RunInitHandler
model run initialization: read input parameters
Definition: main.cpp:54
virtual bool isUseSubValue(int i_subId) const =0
return true if sub-value used by current process
OM_EVENT_LOOP_HANDLER RunModelHandler
model event loop: user code entry point
Definition: main.cpp:60
virtual int parameterSelfSubCount(int i_paramId) const =0
number of parameter sub-values for current process
OpenM++: public interface for errors and exceptions.
virtual int parameterSubCount(int i_paramId) const =0
number of parameter sub-values
int subValueCount
number of sub-values
Definition: omModel.h:30
const char modelUnknownErrorMessage[]
default error message: "unknown model error"
Definition: modelBase.cpp:12
model sub-value run public interface
Definition: omModel.h:84
bool useSparse
if true then use sparse output to database
Definition: omModel.h:36
void(* OM_STARTUP_HANDLER)(openm::IModel *const i_model)
model startup method: initialize sub-value
Definition: omModel.h:236
RunOptions(void)
init run options with default values
Definition: omModel.h:48
const type_info & typeOf
value type
Definition: omModel.h:114
openM++ exceptions
Definition: omError.h:19
double nullValue
if use sparse and abs(value) <= nullValue then value not stored
Definition: omModel.h:39
void(* OM_RUN_ONCE_HANDLER)(openm::IRunBase *const i_runBase)
model one-time initialization
Definition: omModel.h:228
model run options
Definition: omModel.h:27