OpenM++ runtime library (libopenm)
modelHelper.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_HELPER_H
9#define MODEL_HELPER_H
10
11using namespace std;
12
13namespace openm
14{
15 // atomic bool vector to store sub-value done status
17 {
18 public:
19 DoneVector(size_t i_size) { init(i_size); };
20
21 // create new vector of bool values
22 void init(size_t i_size);
23
24 // clear all values: set it to false
25 void reset(void);
26
27 // return true if all values are set to true
28 bool isAll(void);
29
30 // set to true value at specified position
31 void setAt(size_t i_pos);
32
33 // return count of contiguous true values staring from zero position
34 int countFirst(void);
35
36 private:
37 recursive_mutex theMutex; // mutex to lock access operations
38 vector<bool> isDoneVec; // if true then all sub-value accumulators saved in database
39
40 private:
41 DoneVector(const DoneVector & i_doneVec) = delete;
42 DoneVector & operator=(const DoneVector & i_doneVec) = delete;
43 };
44
45 // helper struct to define modeling groups size and count: groups of processes for parallel run of modeling task
47 {
48 int groupSize; // size of modeling group
49 int groupCount; // number of modeling groups
50 int activeRank; // active rank in group: process index among other modeling processes in the group
51 int groupOne; // current process modeling group number, one based, not a zero based
52 bool isRootActive; // if true then root process used for modeling else dedicated for data exchange
53 int subPerProcess; // number of sub-values per modeling process, except of last process where rest is calculated
54 int selfSubCount; // number of sub-values for current process
55
56 static const int all = 0; // worldwide group, all processes
57
58 ProcessGroupDef(void) :
59 groupSize(1), groupCount(0), activeRank(0), groupOne(0), isRootActive(true), subPerProcess(1), selfSubCount(1)
60 { }
61
62 ProcessGroupDef(int i_subValueCount, int i_threadCount, bool i_isRootIdle, int i_worldSize, int i_worldRank);
63 };
64
65 // helper struct to hold modeling group run id and run state
66 struct RunGroup
67 {
68 int groupOne; // modeling group number, one based, not a zero based
69 int runId; // if > 0 then model run id
70 int setId; // if > 0 then set id of source input parametes
71 int firstChildRank; // world rank of first child process
72 int childCount; // number of child processes in group
73 bool isUseRoot; // if true then root process used for modeling else dedicated for data exchange
74 int groupSize; // size of modeling group
75 int subPerProcess; // number of sub-values per modeling process, except of last process where rest is calculated
76 ModelRunState state; // group status and modeling progress
77 DoneVector isSubDone; // size of [subValue count], if true then all sub-value accumulators saved in database
78 vector<ModelRunState> childState; // size of [childCount] run state for all group child processes
79
80 // set initial run group size, assign process ranks and initial state state
81 RunGroup(int i_groupOne, int i_subValueCount, const ProcessGroupDef & i_rootGroupDef);
82
83 // set group state for next run
84 void nextRun(int i_runId, int i_setId, ModelStatus i_status);
85
86 // clear group state after run
87 void reset(void) { nextRun(0, 0, ModelStatus::init); }
88
89 // return child world rank where sub-value is calculated
90 int rankBySubValueId(int i_subId) const;
91
92 private:
93 RunGroup(const RunGroup & i_runGroup) = delete;
94 RunGroup & operator=(const RunGroup & i_runGroup) = delete;
95 };
96
97 // helper struct to receive output table values for each accumulator
99 {
100 int runId; // run id to receive
101 int subValueId; // sub-value index to receive
102 int tableId; // output table id
103 int accId; // accumulator id
104 size_t valueSize; // size of accumulator data
105 bool isReceived; // if true then data received
106 int senderRank; // sender rank: process where accumulator calculated
107 int msgTag; // accumulator message tag
108
110 int i_runId,
111 int i_subId,
112 int i_subValueCount,
113 int i_senderRank,
114 int i_tableId,
115 int i_accId,
116 int i_accIndex,
117 size_t i_valueSize
118 ) :
119 runId(i_runId),
120 subValueId(i_subId),
121 tableId(i_tableId),
122 accId(i_accId),
123 valueSize(i_valueSize),
124 isReceived(false),
125 senderRank(i_senderRank),
126 msgTag(accMsgTag(i_subId, i_subValueCount, i_accIndex))
127 { }
128
130 static int accMsgTag(int i_subId, int i_subValueCount, int i_accIndex)
131 {
132 return ((int)MsgTag::outSubValueBase + i_accIndex) * i_subValueCount + i_subId;
133 }
134 };
135}
136
137#endif // MODEL_HELPER_H
Definition: modelHelper.h:17
model run state: thread safe
Definition: modelRunState.h:24
openM++ namespace
Definition: log.h:32
@ outSubValueBase
input parameter
ModelStatus
modeling job status
Definition: omModelRunState.h:43
@ init
initial status
Definition: modelHelper.h:99
static int accMsgTag(int i_subId, int i_subValueCount, int i_accIndex)
return accumulator message tag
Definition: modelHelper.h:130
Definition: modelHelper.h:47
Definition: modelHelper.h:67