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 
23 namespace openm
24 {
26  struct RunOptions
27  {
30 
33 
35  bool useSparse;
36 
38  double nullValue;
39 
42 
44  double progressStep;
45 
47  RunOptions(void) :
48  subValueCount(1),
49  subValueId(0),
50  useSparse(false),
51  nullValue(FLT_MIN),
52  progressPercent(0),
53  progressStep(0.0)
54  { }
55  ~RunOptions(void) noexcept { }
56  };
57 
59  struct IRunBase
60  {
61  virtual ~IRunBase(void) noexcept = 0;
62 
64  virtual int parameterIdByName(const char * i_name) const = 0;
65 
67  virtual int parameterSubCount(int i_paramId) const = 0;
68 
70  virtual int parameterSelfSubCount(int i_paramId) const = 0;
71 
73  virtual int parameterSubValueIndex(int i_paramId, int i_subId) const = 0;
74 
76  virtual bool isUseSubValue(int i_subId) const = 0;
77 
79  virtual void readParameter(const char * i_name, int i_subId, const type_info & i_type, size_t i_size, void * io_valueArr) = 0;
80  };
81 
83  struct IModel
84  {
85  virtual ~IModel(void) noexcept = 0;
86 
88  virtual int subValueCount(void) const noexcept = 0;
89 
91  virtual int subValueId(void) const noexcept = 0;
92 
94  virtual const RunOptions * runOptions(void) const = 0;
95 
97  virtual int parameterSubValueIndex(const char * i_name) const = 0;
98 
100  virtual void writeOutputTable(const char * i_name, size_t i_size, forward_list<unique_ptr<double> > & io_accValues) = 0;
101 
103  virtual void updateProgress(int i_count, double i_value = 0.0) = 0;
104  };
105 
108  {
110  const char * name;
111 
113  const type_info & typeOf;
114 
116  const size_t size;
117  };
118 
120  extern const size_t PARAMETER_NAME_ARR_LEN;
121 
124 
126  extern const char modelUnknownErrorMessage[];
127 
130 
132  extern const char simulationUnknownErrorMessage[];
133 
136 
138  template<typename TVal> vector<TVal> read_om_parameter(IRunBase * const i_runBase, const char * i_name)
139  {
140  int paramId = i_runBase->parameterIdByName(i_name);
141  int allCount = i_runBase->parameterSubCount(paramId); // number of parameter sub-values
142  int selfCount = i_runBase->parameterSelfSubCount(paramId); // number of sub-values for current process
143 
144  // storage array: parameter values for current process
145  // extra parameter value for exchange between root and child process
146  vector<TVal> valueVec(selfCount);
147  TVal extraVal;
148  bool isStr = typeid(TVal) == typeid(string); // no default null values for string parameters
149 
150  // read sub-values and place into storage array or send to child process
151  for (int nSub = 0; nSub < allCount; nSub++) {
152 
153  void * pData = &extraVal;
154  if (!isStr) {
155  extraVal = numeric_limits<TVal>::quiet_NaN(); // set default null value
156  }
157  i_runBase->readParameter(i_name, nSub, typeid(TVal), 1, pData);
158 
159  if (allCount <= 1) {
160  valueVec[0] = extraVal;
161  }
162  else {
163  if (i_runBase->isUseSubValue(nSub)) valueVec[i_runBase->parameterSubValueIndex(paramId, nSub)] = extraVal;
164  }
165  }
166  return valueVec;
167  }
168 
170  template<typename TVal> vector<unique_ptr<TVal[]>> read_om_parameter(IRunBase * const i_runBase, const char * i_name, size_t i_size)
171  {
172  int paramId = i_runBase->parameterIdByName(i_name);
173  int allCount = i_runBase->parameterSubCount(paramId); // number of parameter sub-values
174  int selfCount = i_runBase->parameterSelfSubCount(paramId); // number of sub-values for current process
175 
176  if (i_size <= 0) throw ModelException("invalid size: %zd of parameter %s", i_size, i_name);
177 
178  // storage array: parameter values for current process
179  vector<unique_ptr<TVal[]>> valueVec(selfCount);
180  for (auto & p : valueVec) {
181  p.reset(new TVal[i_size]);
182  }
183 
184  unique_ptr<TVal> extraVal; // extra parameter value for exchange between root and child process
185  if (allCount > 1) {
186  extraVal.reset(new TVal[i_size]);
187  }
188  bool isStr = typeid(TVal) == typeid(string); // no default null values for string parameters
189 
190  // read sub-values and place into storage array or send to child process
191  for (int nSub = 0; nSub < allCount; nSub++) {
192 
193  void * pData = valueVec[0].get();
194  if (allCount > 1) {
195  pData =
196  i_runBase->isUseSubValue(nSub) ?
197  valueVec[i_runBase->parameterSubValueIndex(paramId, nSub)].get() :
198  extraVal.get();
199  }
200  // set default null values and read parameter
201  if (!isStr) {
202  fill(static_cast<TVal *>(pData), &(static_cast<TVal *>(pData))[i_size], numeric_limits<TVal>::quiet_NaN());
203  }
204  i_runBase->readParameter(i_name, nSub, typeid(TVal), i_size, pData);
205  }
206  return valueVec;
207  }
208 }
209 
210 //
211 // modeling library import and export
212 //
213 #ifdef __cplusplus
214  extern "C" {
215 #endif
216 
218 extern const char * OM_MODEL_NAME;
219 
221 extern const char * OM_MODEL_DIGEST;
222 
224 int main(int argc, char ** argv);
225 
227 typedef void(*OM_RUN_ONCE_HANDLER)(openm::IRunBase * const i_runBase);
229 
231 typedef void(*OM_RUN_INIT_HANDLER)(openm::IRunBase * const i_runBase);
233 
235 typedef void(*OM_STARTUP_HANDLER)(openm::IModel * const i_model);
237 
239 typedef void (*OM_EVENT_LOOP_HANDLER)(openm::IModel * const i_model);
241 
243 typedef void (*OM_SHUTDOWN_HANDLER)(openm::IModel * const i_model);
245 
248 
249 #ifdef __cplusplus
250  }
251 #endif
252 
253 #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:116
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:243
void(* OM_RUN_INIT_HANDLER)(openm::IRunBase *const i_runBase)
model run initialization: read input parameters
Definition: omModel.h:231
OpenM++: public interface for log and trace support.
virtual int parameterIdByName(const char *i_name) const =0
return index of parameter by name
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:59
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:41
OpenmException< 4000, modelUnknownErrorMessage > ModelException
modeling library exception
Definition: omModel.h:129
const char * name
input parameter name
Definition: omModel.h:110
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:44
const char * OM_MODEL_NAME
model name
model input parameter name, type and size
Definition: omModel.h:107
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:32
void(* OM_EVENT_LOOP_HANDLER)(openm::IModel *const i_model)
model event loop: user code entry point
Definition: omModel.h:239
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:170
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:135
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:29
const char modelUnknownErrorMessage[]
default error message: "unknown model error"
Definition: modelBase.cpp:12
model sub-value run public interface
Definition: omModel.h:83
bool useSparse
if true then use sparse output to database
Definition: omModel.h:35
void(* OM_STARTUP_HANDLER)(openm::IModel *const i_model)
model startup method: initialize sub-value
Definition: omModel.h:235
RunOptions(void)
init run options with default values
Definition: omModel.h:47
const type_info & typeOf
value type
Definition: omModel.h:113
openM++ exceptions
Definition: omError.h:19
double nullValue
if use sparse and abs(value) <= nullValue then value not stored
Definition: omModel.h:38
void(* OM_RUN_ONCE_HANDLER)(openm::IRunBase *const i_runBase)
model one-time initialization
Definition: omModel.h:227
model run options
Definition: omModel.h:26