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
18#include "omModelRunState.h"
19#include "omLog.h"
20#include "omError.h"
21
22namespace openm
23{
26 {
29
32
35
37 double nullValue;
38
41
44
47
50
53
55 RunOptions(void) :
57 subValueId(0),
58 useSparse(false),
59 nullValue(FLT_MIN),
60 isDbMicrodata(false),
61 isCsvMicrodata(false),
62 isTraceMicrodata(false),
64 progressStep(0.0)
65 { }
66 ~RunOptions(void) noexcept { }
67
69 bool isMicrodata(void) const { return isDbMicrodata || isCsvMicrodata || isTraceMicrodata; }
70
72 bool isTextMicrodata(void) const { return isCsvMicrodata || isTraceMicrodata; }
73 };
74
77 {
78 virtual ~IRunOptions(void) noexcept = 0;
79
81 virtual bool isOptionExist(const char * i_key) const noexcept = 0;
82
84 virtual std::string strOption(const char * i_key, const std::string & i_default = "") const noexcept = 0;
85
87 virtual bool boolOption(const char * i_key) const noexcept = 0;
88
95 virtual int boolOptionToInt(const char * i_key) const noexcept = 0;
96
98 virtual int intOption(const char * i_key, int i_default) const noexcept = 0;
99
101 virtual long long longOption(const char * i_key, long long i_default) const noexcept = 0;
102
104 virtual double doubleOption(const char * i_key, double i_default) const noexcept = 0;
105
107 virtual std::vector<std::pair<std::string, std::string>> allOptions(void) const noexcept = 0;
108 };
109
111 struct IRunBase : public IRunOptions
112 {
113 virtual ~IRunBase(void) noexcept = 0;
114
116 virtual int parameterIdByName(const char * i_name) const = 0;
117
119 virtual int parameterSubCount(int i_paramId) const = 0;
120
122 virtual int parameterSelfSubCount(int i_paramId) const = 0;
123
125 virtual int parameterSubValueIndex(int i_paramId, int i_subId) const = 0;
126
128 virtual bool isUseSubValue(int i_subId) const = 0;
129
131 virtual bool isTableSuppressed(const char * i_name) const = 0;
132
134 virtual bool isTableSuppressed(int i_tableId) const = 0;
135
137 virtual void readParameter(const char * i_name, int i_subId, const std::type_info & i_type, size_t i_size, void * io_valueArr) = 0;
138 };
139
141 struct IModel
142 {
143 virtual ~IModel(void) noexcept = 0;
144
146 virtual int subValueCount(void) const noexcept = 0;
147
149 virtual int subValueId(void) const noexcept = 0;
150
152 virtual int tableIdByName(const char * i_name) const = 0;
153
155 virtual bool isSuppressed(const char * i_name) const = 0;
156
158 virtual const RunOptions * runOptions(void) const = 0;
159
161 virtual int parameterSubValueIndex(const char * i_name) const = 0;
162
164 virtual void writeOutputTable(const char * i_name, size_t i_size, std::forward_list<std::unique_ptr<double[]> > & io_accValues) = 0;
165
167 virtual void updateProgress(int i_count, double i_value = 0.0) = 0;
168
175 virtual void writeDbMicrodata(int i_entityKind, uint64_t i_microdataKey, const void * i_entityThis) = 0;
176
185 virtual void writeCsvMicrodata(int i_entityKind, uint64_t i_microdataKey, int i_eventId, bool i_isSameEntity, const void * i_entityThis) = 0;
186 };
187
190 {
192 const char * name;
193
195 const std::type_info & typeOf;
196
198 const size_t size;
199 };
200
202 extern const size_t PARAMETER_NAME_ARR_LEN;
203
206
209 {
212
214 const char * entity;
215
218
220 const char * attribute;
221
223 const std::type_info & typeOf;
224
226 const size_t size;
227
229 const ptrdiff_t offset;
230
232 static int byName(const char * i_entityName, const char * i_attrName);
233 };
234
236 extern const size_t ENTITY_NAME_SIZE_ARR_LEN;
237
240
241 struct EventIdNameItem; // model events id, name
242
244 extern const EventIdNameItem EventIdNameArr[];
245
247 extern const size_t EVENT_ID_NAME_ARR_LEN;
248
251 {
254
256 const char * eventName;
257
259 static const char * byId(int i_eventId);
260
262 static bool checkId(int i_eventId)
263 {
264 return EVENT_ID_NAME_ARR_LEN > 0 &&
265 (i_eventId == EventIdNameArr[0].eventId ||
266 (0 < i_eventId + 1 && i_eventId + 1 < (int)EVENT_ID_NAME_ARR_LEN));
267 }
268 };
269
271 #define OM_USE_MICRODATA_EVENTS (EVENT_ID_NAME_ARR_LEN > 1)
272
274 extern const char modelUnknownErrorMessage[];
275
278
280 extern const char simulationUnknownErrorMessage[];
281
284
286 template<typename TVal> std::vector<TVal> read_om_parameter(IRunBase * const i_runBase, const char * i_name)
287 {
288 int paramId = i_runBase->parameterIdByName(i_name);
289 int allCount = i_runBase->parameterSubCount(paramId); // number of parameter sub-values
290 int selfCount = i_runBase->parameterSelfSubCount(paramId); // number of sub-values for current process
291
292 // storage array: parameter values for current process
293 // extra parameter value for exchange between root and child process
294 std::vector<TVal> valueVec(selfCount);
295 TVal extraVal;
296 bool isStr = typeid(TVal) == typeid(std::string); // no default null values for string parameters
297
298 // read sub-values and place into storage array or send to child process
299 for (int nSub = 0; nSub < allCount; nSub++) {
300 try {
301 void * pData = &extraVal;
302 if (!isStr) {
303 extraVal = std::numeric_limits<TVal>::quiet_NaN(); // set default null value
304 }
305 i_runBase->readParameter(i_name, nSub, typeid(TVal), 1, pData);
306
307 if (allCount <= 1) {
308 valueVec[0] = extraVal;
309 }
310 else {
311 if (i_runBase->isUseSubValue(nSub)) valueVec[i_runBase->parameterSubValueIndex(paramId, nSub)] = extraVal;
312 }
313 }
314 catch (std::exception & ex) {
315 throw ModelException("Failed to read input parameter: %s sub-value [%d]. %s", i_name, nSub, ex.what());
316 }
317 }
318 return valueVec;
319 }
320
322 template<typename TVal> std::vector<std::unique_ptr<TVal[]>> read_om_parameter(IRunBase * const i_runBase, const char * i_name, size_t i_size)
323 {
324 int paramId = i_runBase->parameterIdByName(i_name);
325 int allCount = i_runBase->parameterSubCount(paramId); // number of parameter sub-values
326 int selfCount = i_runBase->parameterSelfSubCount(paramId); // number of sub-values for current process
327
328 if (i_size <= 0) throw ModelException("invalid size: %zd of parameter %s", i_size, i_name);
329
330 // storage array: parameter values for current process
331 std::vector<std::unique_ptr<TVal[]>> valueVec(selfCount);
332 for (auto & p : valueVec) {
333 p.reset(new TVal[i_size]);
334 }
335
336 std::unique_ptr<TVal> extraVal; // extra parameter value for exchange between root and child process
337 if (allCount > 1) {
338 extraVal.reset(new TVal[i_size]);
339 }
340 bool isStr = typeid(TVal) == typeid(std::string); // no default null values for string parameters
341
342 // read sub-values and place into storage array or send to child process
343 for (int nSub = 0; nSub < allCount; nSub++) {
344 try {
345 void * pData = valueVec[0].get();
346 if (allCount > 1) {
347 pData =
348 i_runBase->isUseSubValue(nSub) ?
349 valueVec[i_runBase->parameterSubValueIndex(paramId, nSub)].get() :
350 extraVal.get();
351 }
352 // set default null values and read parameter
353 if (!isStr) {
354 std::fill(static_cast<TVal *>(pData), &(static_cast<TVal *>(pData))[i_size], std::numeric_limits<TVal>::quiet_NaN());
355 }
356 i_runBase->readParameter(i_name, nSub, typeid(TVal), i_size, pData);
357 }
358 catch (std::exception & ex) {
359 throw ModelException("Failed to read input parameter: %s sub-value [%d]. %s", i_name, nSub, ex.what());
360 }
361 }
362 return valueVec;
363 }
364}
365
366//
367// modeling library import and export
368//
369#ifdef __cplusplus
370 extern "C" {
371#endif
372
374extern const char * OM_MODEL_NAME;
375
377extern const char * OM_MODEL_DIGEST;
378
380int main(int argc, char ** argv);
381
383typedef void(*OM_RUN_ONCE_HANDLER)(openm::IRunBase * const i_runBase);
385
387typedef void(*OM_RUN_INIT_HANDLER)(openm::IRunBase * const i_runBase);
389
391typedef void(*OM_STARTUP_HANDLER)(openm::IModel * const i_model);
393
395typedef void (*OM_EVENT_LOOP_HANDLER)(openm::IModel * const i_model);
397
399typedef void (*OM_SHUTDOWN_HANDLER)(openm::IModel * const i_model);
401
403typedef void(*OM_RUN_SHUTDOWN_HANDLER)(bool i_isError, openm::IRunBase * const i_runBase);
405
408
409#ifdef __cplusplus
410 }
411#endif
412
413#endif // OM_H_MODEL_H
openM++ exceptions
Definition: omError.h:19
openM++ namespace
Definition: log.h:32
OpenmException< 4000, modelUnknownErrorMessage > ModelException
modeling library exception
Definition: omModel.h:277
const ParameterNameSizeItem parameterNameSizeArr[]
model input parameters name, type and size
const EventIdNameItem EventIdNameArr[]
list of events id, name
const char modelUnknownErrorMessage[]
default error message: "unknown model error"
Definition: modelBase.cpp:14
std::vector< TVal > read_om_parameter(IRunBase *const i_runBase, const char *i_name)
read scalar parameter value or all sub-values for the current process
Definition: omModel.h:286
const size_t EVENT_ID_NAME_ARR_LEN
size of event list: all events in all entities
const size_t PARAMETER_NAME_ARR_LEN
size of parameters list: number of model input parameters
const size_t ENTITY_NAME_SIZE_ARR_LEN
size of entity attributes list: all attributes of all entities
const char simulationUnknownErrorMessage[]
simulation exception default error message: "unknown error in simulation"
Definition: modelBase.cpp:17
OpenmException< 4000, simulationUnknownErrorMessage > SimulationException
simulation exception
Definition: omModel.h:283
const EntityNameSizeItem EntityNameSizeArr[]
list of entity attributes name, type, size and member offset
OpenM++: public interface for errors and exceptions.
OpenM++: public interface for log and trace support.
const char * OM_MODEL_NAME
model name
openm::IModelRunState * theModelRunState
public interface of process-wide model run state: status, progress, update times
Definition: modelRunState.cpp:33
void(* OM_STARTUP_HANDLER)(openm::IModel *const i_model)
model startup method: initialize sub-value
Definition: omModel.h:391
OM_SHUTDOWN_HANDLER ModelShutdownHandler
model shutdown method: save output results
Definition: main.cpp:64
int main(int argc, char **argv)
main entry point
Definition: main.cpp:82
OM_STARTUP_HANDLER ModelStartupHandler
model startup method: initialize sub-value
Definition: main.cpp:58
OM_RUN_ONCE_HANDLER RunOnceHandler
model one-time initialization
Definition: main.cpp:52
void(* OM_EVENT_LOOP_HANDLER)(openm::IModel *const i_model)
model event loop: user code entry point
Definition: omModel.h:395
OM_RUN_SHUTDOWN_HANDLER RunShutdownHandler
process shutdown: last entry point from user code before process exit
Definition: main.cpp:67
void(* OM_RUN_SHUTDOWN_HANDLER)(bool i_isError, openm::IRunBase *const i_runBase)
process shutdown: last entry point from user code before process exit
Definition: omModel.h:403
OM_RUN_INIT_HANDLER RunInitHandler
model run initialization: read input parameters
Definition: main.cpp:55
void(* OM_RUN_ONCE_HANDLER)(openm::IRunBase *const i_runBase)
model one-time initialization
Definition: omModel.h:383
void(* OM_SHUTDOWN_HANDLER)(openm::IModel *const i_model)
model shutdown method: save output results
Definition: omModel.h:399
OM_EVENT_LOOP_HANDLER RunModelHandler
model event loop: user code entry point
Definition: main.cpp:61
const char * OM_MODEL_DIGEST
model metadata digest: unique model key
void(* OM_RUN_INIT_HANDLER)(openm::IRunBase *const i_runBase)
model run initialization: read input parameters
Definition: omModel.h:387
OpenM++ modeling library: model run state public interface.
model entity attributes name, type, size and member offset
Definition: omModel.h:209
const ptrdiff_t offset
attribute value offset in entity this
Definition: omModel.h:229
const char * attribute
attribute name
Definition: omModel.h:220
int entityId
entity metadata id in database
Definition: omModel.h:211
int attributeId
attribute metadata id in database
Definition: omModel.h:217
const char * entity
entity name
Definition: omModel.h:214
const std::type_info & typeOf
attribute value type
Definition: omModel.h:223
const size_t size
attribute value bytes size
Definition: omModel.h:226
model events id, name
Definition: omModel.h:251
int eventId
event id
Definition: omModel.h:253
const char * eventName
event name
Definition: omModel.h:256
static bool checkId(int i_eventId)
return true if event id valid
Definition: omModel.h:262
model sub-value run public interface
Definition: omModel.h:142
virtual void updateProgress(int i_count, double i_value=0.0)=0
set modeling progress count and value
virtual void writeCsvMicrodata(int i_entityKind, uint64_t i_microdataKey, int i_eventId, bool i_isSameEntity, const void *i_entityThis)=0
write microdata into CSV file or into trace.
virtual int subValueCount(void) const noexcept=0
number of sub-values
virtual int tableIdByName(const char *i_name) const =0
return id of output table by name
virtual int parameterSubValueIndex(const char *i_name) const =0
return index of parameter sub-value in the storage array for current modeling thread
virtual int subValueId(void) const noexcept=0
sub-value index of current modeling thread
virtual void writeOutputTable(const char *i_name, size_t i_size, std::forward_list< std::unique_ptr< double[]> > &io_accValues)=0
write output result table: sub values
virtual bool isSuppressed(const char *i_name) const =0
check by name if output table suppressed.
virtual void writeDbMicrodata(int i_entityKind, uint64_t i_microdataKey, const void *i_entityThis)=0
write microdata into the database.
virtual const RunOptions * runOptions(void) const =0
return basic model run options
model run state public interface: thread safe
Definition: omModelRunState.h:139
public interface to initialize model run and input parameters
Definition: omModel.h:112
virtual bool isTableSuppressed(int i_tableId) const =0
check by id if output table suppressed.
virtual bool isTableSuppressed(const char *i_name) const =0
check by name if output table suppressed.
virtual int parameterSubValueIndex(int i_paramId, int i_subId) const =0
return index of parameter sub-value in the storage array of sub-values
virtual void readParameter(const char *i_name, int i_subId, const std::type_info &i_type, size_t i_size, void *io_valueArr)=0
read model parameter
virtual int parameterSelfSubCount(int i_paramId) const =0
number of parameter sub-values for current process
virtual int parameterSubCount(int i_paramId) const =0
number of parameter sub-values
virtual bool isUseSubValue(int i_subId) const =0
return true if sub-value used by current process
virtual int parameterIdByName(const char *i_name) const =0
return id of parameter by name
public interface to get model run options
Definition: omModel.h:77
virtual bool isOptionExist(const char *i_key) const noexcept=0
return true if run option found by i_key in run_option table for the current run id.
virtual int boolOptionToInt(const char *i_key) const noexcept=0
search for boolean value of run option by i_key and return one of: return 1 if key found and value i...
virtual double doubleOption(const char *i_key, double i_default) const noexcept=0
return double value of run option by i_key or default if not found or can not be converted to double.
virtual long long longOption(const char *i_key, long long i_default) const noexcept=0
return long value of run option by i_key or default if not found or can not be converted to long.
virtual std::vector< std::pair< std::string, std::string > > allOptions(void) const noexcept=0
return a copy of all run options as [key, value] pairs, ordered by key.
virtual std::string strOption(const char *i_key, const std::string &i_default="") const noexcept=0
return string value of run option by i_key or default value if not found.
virtual int intOption(const char *i_key, int i_default) const noexcept=0
return int value of run option by i_key or default if not found or can not be converted to int.
virtual bool boolOption(const char *i_key) const noexcept=0
return boolean value of run option by i_key or false if not found or value is not "yes",...
model input parameter name, type and size
Definition: omModel.h:190
const size_t size
parameter size (number of parameter values)
Definition: omModel.h:198
const std::type_info & typeOf
value type
Definition: omModel.h:195
const char * name
input parameter name
Definition: omModel.h:192
basic model run options
Definition: omModel.h:26
bool isMicrodata(void) const
retrun true if any microdata output enabled
Definition: omModel.h:69
bool isTraceMicrodata
if true then model run is writing microdata into trace output
Definition: omModel.h:46
double progressStep
if positive then used for simulation progress reporting, ex: every 1000 cases or every 0....
Definition: omModel.h:52
bool isTextMicrodata(void) const
retrun true if microdata output to text enabled: output to CSV or to trace
Definition: omModel.h:72
bool useSparse
OBSOLETE, for backward compatibility only.
Definition: omModel.h:34
int progressPercent
if positive then used for simulation progress reporting, ex: every 10%
Definition: omModel.h:49
bool isCsvMicrodata
if true then model run is writing microdata into CSV
Definition: omModel.h:43
int subValueId
sub-value index for current modeling thread
Definition: omModel.h:31
bool isDbMicrodata
if true then model run is storing microdata in database
Definition: omModel.h:40
int subValueCount
number of sub-values
Definition: omModel.h:28
double nullValue
OBSOLETE, for backward compatibility only.
Definition: omModel.h:37
RunOptions(void)
init run options with default values
Definition: omModel.h:55