OpenM++ runtime library (libopenm)
modelRunState.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 MODEL_RUN_STATE_H
9#define MODEL_RUN_STATE_H
10
11#include <chrono>
12#include <list>
13#include <mutex>
14#include <forward_list>
15using namespace std;
16
17#include "libopenm/omModel.h"
18#include "helper.h"
19
20namespace openm
21{
23 class ModelRunState : public virtual IModelRunState, private RunState
24 {
25 public:
27 ModelRunState(void) : RunState() { };
28
30 ModelStatus status(void) override;
31
33 bool isFinal(void) override;
34
36 bool isError(void) override;
37
39 bool isShutdownOrFinal(void) override;
40
42 RunState get(void) override;
43
45 ModelStatus updateStatus(ModelStatus i_status) override;
46
48 void updateProgress(int i_count, double i_value = 0.0) override;
49
50 private:
51 recursive_mutex theMutex; // mutex to lock status upadte operations
52
53 private:
54 ModelRunState(const ModelRunState & i_state) = delete;
55 ModelRunState & operator=(const ModelRunState & i_state) = delete;
56
57 };
58
61 {
62 public:
63 RunStateHolder(void) { }
64
66 tuple<bool, RunState> get(int i_runId, int i_subId);
67
69 void add(int i_runId, int i_subId) { add(i_runId, i_subId, RunState()); };
70
72 void add(int i_runId, int i_subId, RunState i_state);
73
75 ModelStatus updateStatus(int i_runId, int i_subId, ModelStatus i_status, bool i_isFinalUpdate = true);
76
78 bool updateProgress(int i_runId, int i_subId, int i_count, double i_value);
79
83 const map<pair<int, int>, RunState> saveUpdated(bool i_isNow = false);
84
86 IRowBaseVec saveToRowVector(int i_runId);
87
89 const RunState fromRowVector(const IRowBaseVec & i_src);
90
91 private:
92 recursive_mutex theMutex; // mutex to lock access operations
93 map<pair<int, int>, RunState> stateMap; // store of current runs state for all sub-values, key: (run id, sub-value id) pair
94 map<pair<int, int>, RunState> updateStateMap; // run states updated since last save call
95 chrono::system_clock::time_point lastSaveTime = chrono::system_clock::time_point::min(); // last time of save
96
97 private:
98 RunStateHolder(const RunStateHolder & i_state) = delete;
99 RunStateHolder & operator=(const RunStateHolder & i_state) = delete;
100 };
101}
102
103#endif // MODEL_RUN_STATE_H
model run state: thread safe
Definition: modelRunState.h:24
RunState get(void) override
return model run state data
Definition: modelRunState.cpp:134
ModelStatus status(void) override
get model status
Definition: modelRunState.cpp:106
bool isError(void) override
return true if status is an error
Definition: modelRunState.cpp:120
ModelRunState(void)
initialize model run state
Definition: modelRunState.h:27
ModelStatus updateStatus(ModelStatus i_status) override
set model status if not already set as one of final status values
Definition: modelRunState.cpp:141
bool isShutdownOrFinal(void) override
return true if model in shutdown state: modeling completed or one of exiting
Definition: modelRunState.cpp:127
void updateProgress(int i_count, double i_value=0.0) override
set modeling progress count and value
Definition: modelRunState.cpp:149
bool isFinal(void) override
return true if status is one of exiting: ie done, exit, error
Definition: modelRunState.cpp:113
sub-value run state for all modeling threads, identified by (run id, sub-value id) pair
Definition: modelRunState.h:61
tuple< bool, RunState > get(int i_runId, int i_subId)
find sub-value run state, return false and empty sub-value run state if not exist
Definition: modelRunState.cpp:156
ModelStatus updateStatus(int i_runId, int i_subId, ModelStatus i_status, bool i_isFinalUpdate=true)
update sub-value status if not already set as one of final status values, return actual status or und...
Definition: modelRunState.cpp:174
bool updateProgress(int i_runId, int i_subId, int i_count, double i_value)
set modeling progress count and value, return false if not exist
Definition: modelRunState.cpp:192
void add(int i_runId, int i_subId)
add new or replace existing sub-value run state
Definition: modelRunState.h:69
const map< pair< int, int >, RunState > saveUpdated(bool i_isNow=false)
return updated sub-values run state since previous call.
Definition: modelRunState.cpp:207
IRowBaseVec saveToRowVector(int i_runId)
copy updated run states into output vector of (run id, sub-value id, run state), last element is proc...
Definition: modelRunState.cpp:231
const RunState fromRowVector(const IRowBaseVec &i_src)
append or replace existing run states from received vector of (run id, sub-value id,...
Definition: modelRunState.cpp:253
OpenM++ common helper utilities.
openM++ namespace
Definition: log.h:32
ModelStatus
modeling job status
Definition: omModelRunState.h:43
std::vector< IRowBaseUptr > IRowBaseVec
db rows: vector of unique pointers to db row
Definition: omHelper.h:239
OpenM++ modeling library: public interface.
model run state public interface: thread safe
Definition: omModelRunState.h:139
model run state data: status, progress, update times
Definition: omModelRunState.h:93