/home/runner/work/charm/charm/src/ck-ldb/TreeLevel.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Author: Juan Galvez <jjgalvez@illinois.edu> |
2 | | |
3 | | #ifndef TREELEVEL_H |
4 | | #define TREELEVEL_H |
5 | | |
6 | | #include "TopoManager.h" |
7 | | #include "TreeLB.h" |
8 | | #include "TreeStrategyBase.h" |
9 | | #include "TreeStrategyFactory.h" |
10 | | #include <cmath> |
11 | | #include <limits> // std::numeric_limits |
12 | | |
13 | 4 | #define FLOAT_TO_INT_MULT 10000 |
14 | | |
15 | | // ----------------------- msgs ----------------------- |
16 | | |
17 | | #include "TreeLevel.decl.h" |
18 | | |
19 | | class LLBMigrateMsg : public TreeLBMessage, public CMessage_LLBMigrateMsg |
20 | | { |
21 | | public: |
22 | | // NOTE: currently this message allocates space for all PEs, even if it doesn't include |
23 | | // info for all of them |
24 | | int n_moves; // number of moves |
25 | | int* num_incoming; // pe -> number of objects incoming |
26 | | int* obj_start; // pe -> idx to first object in to_pes |
27 | | int* to_pes; // obj -> to_pe |
28 | | }; |
29 | | |
30 | | class LBStatsMsg_1 : public TreeLBMessage, public CMessage_LBStatsMsg_1 |
31 | | { |
32 | | public: |
33 | | unsigned int nObjs; // num objs in this msg |
34 | | unsigned int nPes; // num pes in this msg |
35 | | |
36 | | int* pe_ids; // IDs of the pes in this msg |
37 | | float* bgloads; // bgloads[i] is background load of i-th pe in this msg |
38 | | float* speeds; // speeds[i] is speed of i-th pe |
39 | | unsigned int* obj_start; // obj_start[i] points to where loads of objects of i-th pe |
40 | | // start in this msg (array oloads) |
41 | | |
42 | | float* oloads; // array of obj loads (grouped by pe), i-th obj in the array is |
43 | | // considered to have ID i |
44 | | unsigned int* |
45 | | order; // list of obj ids sorted by load (ids are determined by position in oloads) |
46 | | |
47 | | static TreeLBMessage* merge(std::vector<TreeLBMessage*>& msgs) |
48 | 645 | { |
49 | | // TODO ideally have option of sorting objects |
50 | | |
51 | 645 | bool rateAware = false; |
52 | 645 | LBStatsMsg_1* mm = (LBStatsMsg_1*)msgs[0]; |
53 | 645 | if ((void*)mm->speeds != (void*)mm->obj_start) rateAware = true; |
54 | | |
55 | | // could pass n and m as parameters to this method, but don't think it would really |
56 | | // matter |
57 | 645 | unsigned int nObjs = 0; |
58 | 645 | unsigned int nPes = 0; |
59 | 1.94k | for (int i = 0; i < msgs.size(); i++) |
60 | 1.30k | { |
61 | 1.30k | LBStatsMsg_1* msg = (LBStatsMsg_1*)msgs[i]; |
62 | 1.30k | nObjs += msg->nObjs; |
63 | 1.30k | nPes += msg->nPes; |
64 | 1.30k | } |
65 | | |
66 | 645 | LBStatsMsg_1* newMsg; |
67 | 645 | if (rateAware) |
68 | 0 | newMsg = new (nPes, nPes, nPes, nPes + 1, nObjs, nObjs, 0) LBStatsMsg_1; |
69 | 645 | else |
70 | 645 | newMsg = new (nPes, nPes, 0, nPes + 1, nObjs, nObjs, 0) LBStatsMsg_1; |
71 | 645 | newMsg->nObjs = nObjs; |
72 | 645 | newMsg->nPes = nPes; |
73 | 645 | int pe_cnt = 0; |
74 | 645 | int obj_cnt = 0; |
75 | 1.94k | for (int i = 0; i < msgs.size(); i++) |
76 | 1.30k | { |
77 | 1.30k | LBStatsMsg_1* msg = (LBStatsMsg_1*)msgs[i]; |
78 | 1.30k | const int msg_npes = msg->nPes; |
79 | 1.30k | memcpy(newMsg->pe_ids + pe_cnt, msg->pe_ids, sizeof(int) * msg_npes); |
80 | 1.30k | memcpy(newMsg->bgloads + pe_cnt, msg->bgloads, sizeof(float) * msg_npes); |
81 | 1.30k | if (rateAware) |
82 | 0 | memcpy(newMsg->speeds + pe_cnt, msg->speeds, sizeof(float) * msg_npes); |
83 | | // memcpy(newMsg->obj_start + pe_cnt, msg->obj_start, sizeof(int)*msg_npes); |
84 | 2.60k | for (int j = 0; j < msg_npes; j++) |
85 | 1.30k | newMsg->obj_start[pe_cnt + j] = msg->obj_start[j] + obj_cnt; |
86 | 1.30k | memcpy(newMsg->oloads + obj_cnt, msg->oloads, sizeof(float) * (msg->nObjs)); |
87 | | |
88 | 1.30k | obj_cnt += msg->nObjs; |
89 | 1.30k | pe_cnt += msg_npes; |
90 | 1.30k | } |
91 | 645 | newMsg->obj_start[pe_cnt] = obj_cnt; |
92 | | |
93 | 645 | return newMsg; |
94 | 645 | } |
95 | | |
96 | | template <typename O, typename P> |
97 | | static float fill(std::vector<TreeLBMessage*> msgs, std::vector<O>& objs, |
98 | | std::vector<P>& procs, LLBMigrateMsg* migMsg, |
99 | | std::vector<int>& obj_local_ids) |
100 | 1.70k | { |
101 | 1.70k | int pe_cnt = 0; |
102 | 1.70k | int obj_cnt = 0; |
103 | 1.70k | float total_load = 0; |
104 | 6.99k | for (int i = 0; i < msgs.size(); i++) |
105 | 5.28k | { |
106 | 5.28k | LBStatsMsg_1* msg = (LBStatsMsg_1*)msgs[i]; |
107 | 11.2k | for (int j = 0; j < msg->nPes; j++) |
108 | 5.94k | { |
109 | 5.94k | int pe = msg->pe_ids[j]; |
110 | 5.94k | CkAssert(pe >= 0 && pe < CkNumPes()); |
111 | 5.94k | procs[pe_cnt].populate(pe, msg->bgloads + j, msg->speeds + j); |
112 | 5.94k | procs[pe_cnt++].resetLoad(); |
113 | 5.94k | migMsg->obj_start[pe] = obj_cnt; |
114 | 5.94k | int local_id = 0; |
115 | 97.5k | for (int k = msg->obj_start[j]; k < msg->obj_start[j + 1]; |
116 | 91.5k | k++, obj_cnt++, local_id++) |
117 | 91.5k | { |
118 | 91.5k | objs[obj_cnt].populate(obj_cnt, msg->oloads + k, pe); |
119 | 91.5k | total_load += objs[obj_cnt].getLoad(); |
120 | 91.5k | migMsg->to_pes[obj_cnt] = pe; |
121 | | // if obj_local_ids.size() > 0: |
122 | 91.5k | obj_local_ids[obj_cnt] = local_id; |
123 | 91.5k | } |
124 | 5.94k | } |
125 | 5.28k | } |
126 | 1.70k | CkAssert(obj_cnt == objs.size()); |
127 | 1.70k | CkAssert(pe_cnt == procs.size()); |
128 | 1.70k | return total_load; |
129 | 1.70k | } Unexecuted instantiation: _ZN12LBStatsMsg_14fillIN12TreeStrategy3ObjILi1ELb0EEENS1_4ProcILi1ELb1ELb0EEEEEfSt6vectorIP13TreeLBMessageSaIS8_EERS6_IT_SaISB_EERS6_IT0_SaISF_EEP13LLBMigrateMsgRS6_IiSaIiEE _ZN12LBStatsMsg_14fillIN12TreeStrategy3ObjILi1ELb0EEENS1_4ProcILi1ELb0ELb0EEEEEfSt6vectorIP13TreeLBMessageSaIS8_EERS6_IT_SaISB_EERS6_IT0_SaISF_EEP13LLBMigrateMsgRS6_IiSaIiEE Line | Count | Source | 100 | 1.70k | { | 101 | 1.70k | int pe_cnt = 0; | 102 | 1.70k | int obj_cnt = 0; | 103 | 1.70k | float total_load = 0; | 104 | 6.99k | for (int i = 0; i < msgs.size(); i++) | 105 | 5.28k | { | 106 | 5.28k | LBStatsMsg_1* msg = (LBStatsMsg_1*)msgs[i]; | 107 | 11.2k | for (int j = 0; j < msg->nPes; j++) | 108 | 5.94k | { | 109 | 5.94k | int pe = msg->pe_ids[j]; | 110 | 5.94k | CkAssert(pe >= 0 && pe < CkNumPes()); | 111 | 5.94k | procs[pe_cnt].populate(pe, msg->bgloads + j, msg->speeds + j); | 112 | 5.94k | procs[pe_cnt++].resetLoad(); | 113 | 5.94k | migMsg->obj_start[pe] = obj_cnt; | 114 | 5.94k | int local_id = 0; | 115 | 97.5k | for (int k = msg->obj_start[j]; k < msg->obj_start[j + 1]; | 116 | 91.5k | k++, obj_cnt++, local_id++) | 117 | 91.5k | { | 118 | 91.5k | objs[obj_cnt].populate(obj_cnt, msg->oloads + k, pe); | 119 | 91.5k | total_load += objs[obj_cnt].getLoad(); | 120 | 91.5k | migMsg->to_pes[obj_cnt] = pe; | 121 | | // if obj_local_ids.size() > 0: | 122 | 91.5k | obj_local_ids[obj_cnt] = local_id; | 123 | 91.5k | } | 124 | 5.94k | } | 125 | 5.28k | } | 126 | 1.70k | CkAssert(obj_cnt == objs.size()); | 127 | 1.70k | CkAssert(pe_cnt == procs.size()); | 128 | 1.70k | return total_load; | 129 | 1.70k | } |
|
130 | | }; |
131 | | |
132 | | class SubtreeLoadMsg : public TreeLBMessage, public CMessage_SubtreeLoadMsg |
133 | | { |
134 | | public: |
135 | | int pe; |
136 | | float load; |
137 | | }; |
138 | | |
139 | | class SubtreeMigrateDecisionMsg : public TreeLBMessage, |
140 | | public CMessage_SubtreeMigrateDecisionMsg |
141 | | { |
142 | | public: |
143 | | int num_moves; |
144 | | int* src_groups; |
145 | | int* dest_groups; |
146 | | int* loads; |
147 | | }; |
148 | | |
149 | | class TokenListMsg : public TreeLBMessage, public CMessage_TokenListMsg |
150 | | { |
151 | | public: |
152 | | // int dest; |
153 | | int load; |
154 | | int num_tokens; |
155 | | int* local_ids; |
156 | | int* oldPes; |
157 | | float* loads; |
158 | | }; |
159 | | |
160 | | #include "TreeLevel.def.h" |
161 | | |
162 | | // ----------------------- StrategyWrapper ----------------------- |
163 | | |
164 | | class IStrategyWrapper |
165 | | { |
166 | | public: |
167 | 277 | virtual ~IStrategyWrapper() {} |
168 | | |
169 | | virtual float prepStrategy(unsigned int nobjs, unsigned int nprocs, |
170 | | std::vector<TreeLBMessage*>& msgs, |
171 | | LLBMigrateMsg* mig_msg) = 0; |
172 | | |
173 | | virtual void runStrategy(LLBMigrateMsg* migMsg, IDM* idm = nullptr) = 0; |
174 | | |
175 | | virtual void removeObj(int& local_id, int& oldPe, float& load) = 0; |
176 | | |
177 | | virtual void addForeignObject(int local_id, int oldPe, float load) = 0; |
178 | | }; |
179 | | |
180 | | // This wrapper allocates mem for objects and processors. to the lb algorithm, |
181 | | // it passes vectors of objects and processors |
182 | | template <typename O, typename P> |
183 | | class StrategyWrapper : public IStrategyWrapper |
184 | | { |
185 | | public: |
186 | | // TODO make a separate Solution class to deal with IDM scenario? |
187 | | class Solution |
188 | | { |
189 | | public: |
190 | | Solution(int& nmoves, int* num_incoming, int* loc, int foreign_obj_id_start, |
191 | | std::vector<int>& obj_local_ids) |
192 | 1.70k | : n_moves(nmoves), |
193 | 1.70k | num_incoming(num_incoming), |
194 | 1.70k | loc(loc), |
195 | 1.70k | obj_local_ids(obj_local_ids), |
196 | 1.70k | foreign_obj_id_start(foreign_obj_id_start) |
197 | 1.70k | { |
198 | 1.70k | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE8SolutionC2ERiPiS8_iRSt6vectorIiSaIiEE _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE8SolutionC2ERiPiS8_iRSt6vectorIiSaIiEE Line | Count | Source | 192 | 1.70k | : n_moves(nmoves), | 193 | 1.70k | num_incoming(num_incoming), | 194 | 1.70k | loc(loc), | 195 | 1.70k | obj_local_ids(obj_local_ids), | 196 | 1.70k | foreign_obj_id_start(foreign_obj_id_start) | 197 | 1.70k | { | 198 | 1.70k | } |
|
199 | | |
200 | | inline void assign(const O* o, P* p) |
201 | 90.8k | { |
202 | | #if DEBUG__TREE_LB_L3 |
203 | | CkPrintf("[%d] Moving object %d from processor %d to %d foreign_obj_id_start=%d\n", |
204 | | CkMyPe(), o->id, o->oldPe, p->id, foreign_obj_id_start); |
205 | | #endif |
206 | 90.8k | #if CMK_ERROR_CHECKING |
207 | 90.8k | CkAssert(p->id >= 0 && p->id < CkNumPes()); |
208 | 90.8k | CkAssert(procMap[p->id] >= 0); |
209 | 90.8k | CkAssert(o->id >= 0 && o->id < num_objs); |
210 | 90.8k | CkAssert(o->oldPe >= 0 && o->oldPe < CkNumPes()); |
211 | 90.8k | CkAssert(objs_assigned.count(o->id) == |
212 | 90.8k | 0); // check that object hasn't been assigned already |
213 | 90.8k | #endif |
214 | 90.8k | p->assign(o); |
215 | 90.8k | if (o->oldPe != p->id) |
216 | 71.9k | { |
217 | 71.9k | n_moves += 1; |
218 | 71.9k | num_incoming[p->id] += 1; |
219 | 71.9k | } |
220 | 90.8k | if (o->id < foreign_obj_id_start) |
221 | 90.8k | { |
222 | | // this object is in my subtree |
223 | 90.8k | loc[o->id] = p->id; |
224 | 90.8k | } |
225 | 58 | else |
226 | 58 | { |
227 | 58 | (*idm)[o->oldPe].emplace_back(obj_local_ids[o->id], p->id); |
228 | 58 | } |
229 | 90.8k | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE8Solution6assignEPKS2_PS4_ _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE8Solution6assignEPKS2_PS4_ Line | Count | Source | 201 | 90.8k | { | 202 | | #if DEBUG__TREE_LB_L3 | 203 | | CkPrintf("[%d] Moving object %d from processor %d to %d foreign_obj_id_start=%d\n", | 204 | | CkMyPe(), o->id, o->oldPe, p->id, foreign_obj_id_start); | 205 | | #endif | 206 | 90.8k | #if CMK_ERROR_CHECKING | 207 | 90.8k | CkAssert(p->id >= 0 && p->id < CkNumPes()); | 208 | 90.8k | CkAssert(procMap[p->id] >= 0); | 209 | 90.8k | CkAssert(o->id >= 0 && o->id < num_objs); | 210 | 90.8k | CkAssert(o->oldPe >= 0 && o->oldPe < CkNumPes()); | 211 | 90.8k | CkAssert(objs_assigned.count(o->id) == | 212 | 90.8k | 0); // check that object hasn't been assigned already | 213 | 90.8k | #endif | 214 | 90.8k | p->assign(o); | 215 | 90.8k | if (o->oldPe != p->id) | 216 | 71.9k | { | 217 | 71.9k | n_moves += 1; | 218 | 71.9k | num_incoming[p->id] += 1; | 219 | 71.9k | } | 220 | 90.8k | if (o->id < foreign_obj_id_start) | 221 | 90.8k | { | 222 | | // this object is in my subtree | 223 | 90.8k | loc[o->id] = p->id; | 224 | 90.8k | } | 225 | 58 | else | 226 | 58 | { | 227 | 58 | (*idm)[o->oldPe].emplace_back(obj_local_ids[o->id], p->id); | 228 | 58 | } | 229 | 90.8k | } |
|
230 | | |
231 | 90.8k | inline void assign(const O& o, P& p) { assign(&o, &p); }Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE8Solution6assignERKS2_RS4_ _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE8Solution6assignERKS2_RS4_ Line | Count | Source | 231 | 90.8k | inline void assign(const O& o, P& p) { assign(&o, &p); } |
|
232 | | |
233 | 1.70k | void setIDM(IDM* idm) { this->idm = idm; }Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE8Solution6setIDMEPSt13unordered_mapIiSt6vectorISt4pairIiiESaISA_EESt4hashIiESt8equal_toIiESaIS9_IKiSC_EEE _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE8Solution6setIDMEPSt13unordered_mapIiSt6vectorISt4pairIiiESaISA_EESt4hashIiESt8equal_toIiESaIS9_IKiSC_EEE Line | Count | Source | 233 | 1.70k | void setIDM(IDM* idm) { this->idm = idm; } |
|
234 | | |
235 | | #if CMK_ERROR_CHECKING |
236 | | void setErrorChecking(std::vector<O>& objs, std::vector<P>& procs) |
237 | 1.70k | { |
238 | 1.70k | num_objs = objs.size(); |
239 | 1.70k | procMap.resize(CkNumPes(), -1); |
240 | 7.64k | for (int i = 0; i < procs.size(); i++) procMap[procs[i].id] = i; |
241 | 1.70k | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE8Solution16setErrorCheckingERSt6vectorIS2_SaIS2_EERS7_IS4_SaIS4_EE _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE8Solution16setErrorCheckingERSt6vectorIS2_SaIS2_EERS7_IS4_SaIS4_EE Line | Count | Source | 237 | 1.70k | { | 238 | 1.70k | num_objs = objs.size(); | 239 | 1.70k | procMap.resize(CkNumPes(), -1); | 240 | 7.64k | for (int i = 0; i < procs.size(); i++) procMap[procs[i].id] = i; | 241 | 1.70k | } |
|
242 | | #endif |
243 | | |
244 | | private: |
245 | | friend StrategyWrapper; |
246 | | int& n_moves; |
247 | | int* num_incoming; |
248 | | int* loc; // store solution of strategy here: loc[i] = newPe for object i |
249 | | std::vector<int>& obj_local_ids; |
250 | | const int foreign_obj_id_start; |
251 | | IDM* idm = nullptr; |
252 | | #if CMK_ERROR_CHECKING |
253 | | size_t num_objs; |
254 | | std::vector<int> procMap; |
255 | | std::unordered_set<unsigned int> objs_assigned; |
256 | | #endif |
257 | | }; |
258 | | |
259 | | StrategyWrapper(const std::string& _strategy_name, bool _isTreeRoot, json& config) |
260 | 526 | { |
261 | 526 | strategy_name = _strategy_name; |
262 | 526 | isTreeRoot = _isTreeRoot; |
263 | 526 | strategy = TreeStrategy::Factory::makeStrategy<O, P, Solution>(strategy_name, config); |
264 | 526 | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEEC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbRN8nlohmann10basic_jsonISt3mapSt6vectorSB_blmdSaNSE_14adl_serializerESH_IhSaIhEEEE _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEEC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEbRN8nlohmann10basic_jsonISt3mapSt6vectorSB_blmdSaNSE_14adl_serializerESH_IhSaIhEEEE Line | Count | Source | 260 | 526 | { | 261 | 526 | strategy_name = _strategy_name; | 262 | 526 | isTreeRoot = _isTreeRoot; | 263 | 526 | strategy = TreeStrategy::Factory::makeStrategy<O, P, Solution>(strategy_name, config); | 264 | 526 | } |
|
265 | | |
266 | | virtual ~StrategyWrapper() |
267 | 277 | { |
268 | 277 | delete strategy; |
269 | 277 | delete sol; |
270 | 277 | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEED2Ev _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEED2Ev Line | Count | Source | 267 | 277 | { | 268 | 277 | delete strategy; | 269 | 277 | delete sol; | 270 | 277 | } |
|
271 | | |
272 | | float prepStrategy(unsigned int nobjs, unsigned int nprocs, |
273 | | std::vector<TreeLBMessage*>& msgs, LLBMigrateMsg* migMsg) |
274 | 1.70k | { |
275 | 1.70k | CkAssert(foreign_objs.size() == 0 && sol == nullptr); |
276 | 1.70k | objs.resize(nobjs); |
277 | 1.70k | procs.resize(nprocs); |
278 | | // if (subtree_migrations) |
279 | 1.70k | obj_local_ids.resize(nobjs); |
280 | 1.70k | foreign_obj_id = nobjs; |
281 | 1.70k | sol = new Solution(migMsg->n_moves, migMsg->num_incoming, migMsg->to_pes, |
282 | 1.70k | foreign_obj_id, obj_local_ids); |
283 | 1.70k | return LBStatsMsg_1::fill(msgs, objs, procs, migMsg, obj_local_ids); |
284 | 1.70k | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE12prepStrategyEjjRSt6vectorIP13TreeLBMessageSaIS8_EEP13LLBMigrateMsg _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE12prepStrategyEjjRSt6vectorIP13TreeLBMessageSaIS8_EEP13LLBMigrateMsg Line | Count | Source | 274 | 1.70k | { | 275 | 1.70k | CkAssert(foreign_objs.size() == 0 && sol == nullptr); | 276 | 1.70k | objs.resize(nobjs); | 277 | 1.70k | procs.resize(nprocs); | 278 | | // if (subtree_migrations) | 279 | 1.70k | obj_local_ids.resize(nobjs); | 280 | 1.70k | foreign_obj_id = nobjs; | 281 | 1.70k | sol = new Solution(migMsg->n_moves, migMsg->num_incoming, migMsg->to_pes, | 282 | 1.70k | foreign_obj_id, obj_local_ids); | 283 | 1.70k | return LBStatsMsg_1::fill(msgs, objs, procs, migMsg, obj_local_ids); | 284 | 1.70k | } |
|
285 | | |
286 | | // remove object because it is moving to a different subtree |
287 | | void removeObj(int& local_id, int& oldPe, float& load) |
288 | 58 | { |
289 | 58 | O& o = objs.back(); |
290 | 58 | local_id = obj_local_ids[o.id]; |
291 | 58 | oldPe = o.oldPe; |
292 | 58 | load = o.getLoad(); |
293 | | // indicating that the object will migrate but not yet known where |
294 | 58 | sol->loc[o.id] = -1; |
295 | 58 | objs.pop_back(); |
296 | 58 | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE9removeObjERiS6_Rf _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE9removeObjERiS6_Rf Line | Count | Source | 288 | 58 | { | 289 | 58 | O& o = objs.back(); | 290 | 58 | local_id = obj_local_ids[o.id]; | 291 | 58 | oldPe = o.oldPe; | 292 | 58 | load = o.getLoad(); | 293 | | // indicating that the object will migrate but not yet known where | 294 | 58 | sol->loc[o.id] = -1; | 295 | 58 | objs.pop_back(); | 296 | 58 | } |
|
297 | | |
298 | | void addForeignObject(int local_id, int oldPe, float load) |
299 | 58 | { |
300 | 58 | CkAssert(oldPe >= 0 && oldPe < CkNumPes()); |
301 | 58 | foreign_objs.emplace_back(); |
302 | 58 | foreign_objs.back().populate(foreign_obj_id++, &load, oldPe); |
303 | 58 | obj_local_ids.push_back(local_id); |
304 | 58 | CkAssert(obj_local_ids.size() == foreign_obj_id); |
305 | 58 | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE16addForeignObjectEiif _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE16addForeignObjectEiif Line | Count | Source | 299 | 58 | { | 300 | 58 | CkAssert(oldPe >= 0 && oldPe < CkNumPes()); | 301 | 58 | foreign_objs.emplace_back(); | 302 | 58 | foreign_objs.back().populate(foreign_obj_id++, &load, oldPe); | 303 | 58 | obj_local_ids.push_back(local_id); | 304 | 58 | CkAssert(obj_local_ids.size() == foreign_obj_id); | 305 | 58 | } |
|
306 | | |
307 | | void runStrategy(LLBMigrateMsg* migMsg, IDM* idm = nullptr) |
308 | 1.70k | { |
309 | 1.70k | CkAssert(sol != nullptr); |
310 | 1.70k | sol->setIDM(idm); |
311 | | |
312 | 1.70k | if (foreign_objs.size() > 0) |
313 | 2 | { |
314 | 2 | objs.insert(objs.end(), foreign_objs.begin(), foreign_objs.end()); |
315 | 2 | foreign_objs.clear(); |
316 | 2 | } |
317 | | |
318 | 1.70k | std::vector<int> procMap; |
319 | 1.70k | #if CMK_ERROR_CHECKING |
320 | 1.70k | { |
321 | | #else |
322 | | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) |
323 | | { |
324 | | #endif |
325 | 1.70k | procMap.resize(CkNumPes(), -1); |
326 | 7.64k | for (int i = 0; i < procs.size(); i++) procMap[procs[i].id] = i; |
327 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) |
328 | 176 | CkPrintf("[%d] num_procs=%zu num_objs=%zu\n", CkMyPe(), procs.size(), |
329 | 176 | objs.size()); |
330 | 1.70k | if (objs.size() > 0) |
331 | 1.70k | { |
332 | 1.70k | float objMinLoad = std::numeric_limits<float>::max(); |
333 | 1.70k | float objMaxLoad = 0; |
334 | 1.70k | float objTotalLoad = 0; |
335 | 1.70k | for (const auto& o : objs) |
336 | 91.5k | { |
337 | 91.5k | float oload = o.getLoad(); |
338 | 91.5k | objMinLoad = std::min(objMinLoad, oload); |
339 | 91.5k | objMaxLoad = std::max(objMaxLoad, oload); |
340 | 91.5k | objTotalLoad += oload; |
341 | 91.5k | if (o.id < sol->foreign_obj_id_start) |
342 | 91.5k | { |
343 | 91.5k | CkAssert(procMap[o.oldPe] >= 0); |
344 | 91.5k | procs[procMap[o.oldPe]].assign(o); |
345 | 91.5k | } |
346 | 91.5k | } |
347 | 1.70k | #if CMK_ERROR_CHECKING |
348 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) |
349 | 176 | #endif |
350 | 176 | CkPrintf("[%d] obj loads: min=%f mean=%f max=%f\n", CkMyPe(), objMinLoad, |
351 | 176 | objTotalLoad / objs.size(), objMaxLoad); |
352 | 1.70k | } |
353 | | |
354 | 1.70k | float procMinLoad = std::numeric_limits<float>::max(); |
355 | 1.70k | float procMaxLoad = 0; |
356 | 1.70k | float procTotalLoad = 0; |
357 | 1.70k | for (const auto& p : procs) |
358 | 5.94k | { |
359 | 5.94k | float pload = p.getLoad(); |
360 | 5.94k | procMinLoad = std::min(procMinLoad, pload); |
361 | 5.94k | procMaxLoad = std::max(procMaxLoad, pload); |
362 | 5.94k | procTotalLoad += pload; |
363 | 5.94k | } |
364 | 1.70k | #if CMK_ERROR_CHECKING |
365 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) |
366 | 176 | #endif |
367 | 176 | CkPrintf("[%d] proc loads: min=%f mean=%f max=%f\n", CkMyPe(), procMinLoad, |
368 | 176 | procTotalLoad / procs.size(), procMaxLoad); |
369 | 1.70k | if (objs.size() > 0) |
370 | 5.93k | for (auto& p : procs) p.resetLoad(); |
371 | 1.70k | } |
372 | | |
373 | 1.70k | #if CMK_ERROR_CHECKING |
374 | 1.70k | sol->setErrorChecking(objs, procs); |
375 | 1.70k | #endif |
376 | | |
377 | 1.70k | double t0 = CkWallTimer(); |
378 | 1.70k | strategy->solve(objs, procs, *sol, false); |
379 | | |
380 | 1.70k | #if CMK_ERROR_CHECKING |
381 | 1.70k | { |
382 | | #else |
383 | | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) |
384 | | { |
385 | | #endif |
386 | 1.70k | double strategy_time = CkWallTimer() - t0; |
387 | 1.70k | TopoManager* tmgr = TopoManager::getTopoManager(); |
388 | 1.70k | float maxLoad = 0; |
389 | 1.70k | unsigned int migrations_sum_hops = 0; |
390 | 5.94k | for (auto& p : procs) p.resetLoad(); |
391 | 1.70k | for (const auto& o : objs) |
392 | 91.5k | { |
393 | 91.5k | int dest = -1; |
394 | 91.5k | if (o.id < sol->foreign_obj_id_start) |
395 | 91.5k | { |
396 | 91.5k | dest = migMsg->to_pes[o.id]; |
397 | 91.5k | } |
398 | 58 | else |
399 | 58 | { |
400 | 58 | for (auto& idm_move : (*idm)[o.oldPe]) |
401 | 870 | { |
402 | 870 | if (idm_move.first == obj_local_ids[o.id]) |
403 | 58 | { |
404 | 58 | dest = idm_move.second; |
405 | 58 | break; |
406 | 58 | } |
407 | 870 | } |
408 | 58 | } |
409 | 91.5k | if (dest == -1) |
410 | 0 | { |
411 | 0 | if (o.id >= sol->foreign_obj_id_start) |
412 | 0 | CkPrintf("[%d] Error: strategy %s might not support foreign objects\n", |
413 | 0 | CkMyPe(), strategy_name.c_str()); |
414 | 0 | CkAbort("Object was not assigned to any processor\n"); |
415 | 0 | } |
416 | 91.5k | if (procMap[dest] < 0) CkAbort("Strategy assigned object to invalid processor\n"); |
417 | 91.5k | if (dest != o.oldPe) |
418 | 71.9k | migrations_sum_hops += tmgr->getHopsBetweenRanks(o.oldPe, dest); |
419 | 91.5k | P& p = procs[procMap[dest]]; |
420 | 91.5k | p.assign(o); |
421 | 91.5k | maxLoad = std::max(maxLoad, p.getLoad()); |
422 | 91.5k | } |
423 | 1.70k | #if CMK_ERROR_CHECKING |
424 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) |
425 | 176 | #endif |
426 | 176 | { |
427 | 176 | CkPrintf( |
428 | 176 | "[%d] strategy %s time=%f secs, maxLoad after strategy=%f, num_migrations=%d " |
429 | 176 | "migrations_sum_hops=%u\n", |
430 | 176 | CkMyPe(), strategy_name.c_str(), strategy_time, maxLoad, migMsg->n_moves, |
431 | 176 | migrations_sum_hops); |
432 | 176 | } |
433 | 1.70k | } |
434 | | |
435 | 1.70k | delete sol; |
436 | 1.70k | sol = nullptr; |
437 | 1.70k | } Unexecuted instantiation: _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb1ELb0EEEE11runStrategyEP13LLBMigrateMsgPSt13unordered_mapIiSt6vectorISt4pairIiiESaISB_EESt4hashIiESt8equal_toIiESaISA_IKiSD_EEE _ZN15StrategyWrapperIN12TreeStrategy3ObjILi1ELb0EEENS0_4ProcILi1ELb0ELb0EEEE11runStrategyEP13LLBMigrateMsgPSt13unordered_mapIiSt6vectorISt4pairIiiESaISB_EESt4hashIiESt8equal_toIiESaISA_IKiSD_EEE Line | Count | Source | 308 | 1.70k | { | 309 | 1.70k | CkAssert(sol != nullptr); | 310 | 1.70k | sol->setIDM(idm); | 311 | | | 312 | 1.70k | if (foreign_objs.size() > 0) | 313 | 2 | { | 314 | 2 | objs.insert(objs.end(), foreign_objs.begin(), foreign_objs.end()); | 315 | 2 | foreign_objs.clear(); | 316 | 2 | } | 317 | | | 318 | 1.70k | std::vector<int> procMap; | 319 | 1.70k | #if CMK_ERROR_CHECKING | 320 | 1.70k | { | 321 | | #else | 322 | | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) | 323 | | { | 324 | | #endif | 325 | 1.70k | procMap.resize(CkNumPes(), -1); | 326 | 7.64k | for (int i = 0; i < procs.size(); i++) procMap[procs[i].id] = i; | 327 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) | 328 | 176 | CkPrintf("[%d] num_procs=%zu num_objs=%zu\n", CkMyPe(), procs.size(), | 329 | 176 | objs.size()); | 330 | 1.70k | if (objs.size() > 0) | 331 | 1.70k | { | 332 | 1.70k | float objMinLoad = std::numeric_limits<float>::max(); | 333 | 1.70k | float objMaxLoad = 0; | 334 | 1.70k | float objTotalLoad = 0; | 335 | 1.70k | for (const auto& o : objs) | 336 | 91.5k | { | 337 | 91.5k | float oload = o.getLoad(); | 338 | 91.5k | objMinLoad = std::min(objMinLoad, oload); | 339 | 91.5k | objMaxLoad = std::max(objMaxLoad, oload); | 340 | 91.5k | objTotalLoad += oload; | 341 | 91.5k | if (o.id < sol->foreign_obj_id_start) | 342 | 91.5k | { | 343 | 91.5k | CkAssert(procMap[o.oldPe] >= 0); | 344 | 91.5k | procs[procMap[o.oldPe]].assign(o); | 345 | 91.5k | } | 346 | 91.5k | } | 347 | 1.70k | #if CMK_ERROR_CHECKING | 348 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) | 349 | 176 | #endif | 350 | 176 | CkPrintf("[%d] obj loads: min=%f mean=%f max=%f\n", CkMyPe(), objMinLoad, | 351 | 176 | objTotalLoad / objs.size(), objMaxLoad); | 352 | 1.70k | } | 353 | | | 354 | 1.70k | float procMinLoad = std::numeric_limits<float>::max(); | 355 | 1.70k | float procMaxLoad = 0; | 356 | 1.70k | float procTotalLoad = 0; | 357 | 1.70k | for (const auto& p : procs) | 358 | 5.94k | { | 359 | 5.94k | float pload = p.getLoad(); | 360 | 5.94k | procMinLoad = std::min(procMinLoad, pload); | 361 | 5.94k | procMaxLoad = std::max(procMaxLoad, pload); | 362 | 5.94k | procTotalLoad += pload; | 363 | 5.94k | } | 364 | 1.70k | #if CMK_ERROR_CHECKING | 365 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) | 366 | 176 | #endif | 367 | 176 | CkPrintf("[%d] proc loads: min=%f mean=%f max=%f\n", CkMyPe(), procMinLoad, | 368 | 176 | procTotalLoad / procs.size(), procMaxLoad); | 369 | 1.70k | if (objs.size() > 0) | 370 | 5.93k | for (auto& p : procs) p.resetLoad(); | 371 | 1.70k | } | 372 | | | 373 | 1.70k | #if CMK_ERROR_CHECKING | 374 | 1.70k | sol->setErrorChecking(objs, procs); | 375 | 1.70k | #endif | 376 | | | 377 | 1.70k | double t0 = CkWallTimer(); | 378 | 1.70k | strategy->solve(objs, procs, *sol, false); | 379 | | | 380 | 1.70k | #if CMK_ERROR_CHECKING | 381 | 1.70k | { | 382 | | #else | 383 | | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) | 384 | | { | 385 | | #endif | 386 | 1.70k | double strategy_time = CkWallTimer() - t0; | 387 | 1.70k | TopoManager* tmgr = TopoManager::getTopoManager(); | 388 | 1.70k | float maxLoad = 0; | 389 | 1.70k | unsigned int migrations_sum_hops = 0; | 390 | 5.94k | for (auto& p : procs) p.resetLoad(); | 391 | 1.70k | for (const auto& o : objs) | 392 | 91.5k | { | 393 | 91.5k | int dest = -1; | 394 | 91.5k | if (o.id < sol->foreign_obj_id_start) | 395 | 91.5k | { | 396 | 91.5k | dest = migMsg->to_pes[o.id]; | 397 | 91.5k | } | 398 | 58 | else | 399 | 58 | { | 400 | 58 | for (auto& idm_move : (*idm)[o.oldPe]) | 401 | 870 | { | 402 | 870 | if (idm_move.first == obj_local_ids[o.id]) | 403 | 58 | { | 404 | 58 | dest = idm_move.second; | 405 | 58 | break; | 406 | 58 | } | 407 | 870 | } | 408 | 58 | } | 409 | 91.5k | if (dest == -1) | 410 | 0 | { | 411 | 0 | if (o.id >= sol->foreign_obj_id_start) | 412 | 0 | CkPrintf("[%d] Error: strategy %s might not support foreign objects\n", | 413 | 0 | CkMyPe(), strategy_name.c_str()); | 414 | 0 | CkAbort("Object was not assigned to any processor\n"); | 415 | 0 | } | 416 | 91.5k | if (procMap[dest] < 0) CkAbort("Strategy assigned object to invalid processor\n"); | 417 | 91.5k | if (dest != o.oldPe) | 418 | 71.9k | migrations_sum_hops += tmgr->getHopsBetweenRanks(o.oldPe, dest); | 419 | 91.5k | P& p = procs[procMap[dest]]; | 420 | 91.5k | p.assign(o); | 421 | 91.5k | maxLoad = std::max(maxLoad, p.getLoad()); | 422 | 91.5k | } | 423 | 1.70k | #if CMK_ERROR_CHECKING | 424 | 1.70k | if ((CkMyPe() == 0 || isTreeRoot) && _lb_args.debug() > 0) | 425 | 176 | #endif | 426 | 176 | { | 427 | 176 | CkPrintf( | 428 | 176 | "[%d] strategy %s time=%f secs, maxLoad after strategy=%f, num_migrations=%d " | 429 | 176 | "migrations_sum_hops=%u\n", | 430 | 176 | CkMyPe(), strategy_name.c_str(), strategy_time, maxLoad, migMsg->n_moves, | 431 | 176 | migrations_sum_hops); | 432 | 176 | } | 433 | 1.70k | } | 434 | | | 435 | 1.70k | delete sol; | 436 | 1.70k | sol = nullptr; | 437 | 1.70k | } |
|
438 | | |
439 | | private: |
440 | | std::string strategy_name; |
441 | | bool isTreeRoot; |
442 | | std::vector<O> objs; |
443 | | std::vector<P> procs; |
444 | | Solution* sol = nullptr; |
445 | | std::vector<int> obj_local_ids; |
446 | | std::vector<O> foreign_objs; |
447 | | int foreign_obj_id; |
448 | | TreeStrategy::Strategy<O, P, Solution>* strategy; |
449 | | }; |
450 | | |
451 | | // -------------------------------------------------------------- |
452 | | |
453 | | // ---------------- RootLevel ---------------- |
454 | | |
455 | | class RootLevel : public LevelLogic |
456 | | { |
457 | | public: |
458 | 352 | RootLevel(int _num_groups = -1) : num_groups(_num_groups) {} |
459 | | |
460 | | virtual ~RootLevel() |
461 | 122 | { |
462 | 122 | for (auto w : wrappers) delete w; |
463 | 122 | } |
464 | | |
465 | | /** |
466 | | * mode 0: receive obj stats |
467 | | * mode 1: receive aggregated group load |
468 | | */ |
469 | | virtual void configure(bool rateAware, std::vector<std::string> strategies, |
470 | | json& config, bool repeat_strategies = false, |
471 | | bool token_passing = true) |
472 | 352 | { |
473 | 352 | using namespace TreeStrategy; |
474 | 352 | for (auto w : wrappers) delete w; |
475 | 352 | wrappers.clear(); |
476 | 352 | if (num_groups == -1) |
477 | 348 | { |
478 | 348 | current_strategy = 0; |
479 | 348 | for (const std::string& strategy_name : strategies) |
480 | 352 | { |
481 | 352 | if (rateAware) |
482 | 0 | { |
483 | 0 | wrappers.push_back(new StrategyWrapper<Obj<1>, Proc<1, true>>( |
484 | 0 | strategy_name, true, config[strategy_name])); |
485 | 0 | } |
486 | 352 | else |
487 | 352 | { |
488 | 352 | wrappers.push_back(new StrategyWrapper<Obj<1>, Proc<1, false>>( |
489 | 352 | strategy_name, true, config[strategy_name])); |
490 | 352 | } |
491 | 352 | } |
492 | 348 | this->repeat_strategies = repeat_strategies; |
493 | 348 | } |
494 | 4 | else |
495 | 4 | { |
496 | 4 | group_strategy_dummy = !token_passing; |
497 | 4 | } |
498 | 352 | } |
499 | | |
500 | | virtual void depositStats(TreeLBMessage* stats) |
501 | 5.25k | { |
502 | 5.25k | stats_msgs.push_back(stats); |
503 | 5.25k | if (num_groups > 0) |
504 | 6 | { |
505 | 6 | total_load += ((SubtreeLoadMsg*)stats)->load; |
506 | 6 | } |
507 | 5.25k | else |
508 | 5.25k | { |
509 | 5.25k | nPes += ((LBStatsMsg_1*)stats)->nPes; |
510 | 5.25k | nObjs += ((LBStatsMsg_1*)stats)->nObjs; |
511 | 5.25k | } |
512 | 5.25k | } |
513 | | |
514 | | TreeLBMessage* loadBalance(IDM& idm) |
515 | 1.68k | { |
516 | | #if DEBUG__TREE_LB_L1 |
517 | | // print('[' + str(charm.myPe()) + ']', self.__class__, 'loadBalance') |
518 | | #endif |
519 | | |
520 | 1.68k | const int num_children = stats_msgs.size(); |
521 | 1.68k | CkAssert(num_children > 0); |
522 | | #if DEBUG__TREE_LB_L1 |
523 | | CkPrintf("[%d] RootLevel::loadBalance, num_children=%d nPes=%d nObjs=%d\n", CkMyPe(), |
524 | | num_children, nPes, nObjs); |
525 | | #endif |
526 | | |
527 | 1.68k | if (num_groups == -1) |
528 | 1.68k | { |
529 | | // msg has object loads |
530 | 1.68k | CkAssert(wrappers.size() > current_strategy); |
531 | 1.68k | IStrategyWrapper* wrapper = wrappers[current_strategy]; |
532 | 1.68k | CkAssert(wrapper != nullptr); |
533 | 1.68k | CkAssert(nPes == CkNumPes()); |
534 | 1.68k | LLBMigrateMsg* migMsg = new (nPes, nPes, nObjs, 0) LLBMigrateMsg; |
535 | 1.68k | migMsg->n_moves = 0; |
536 | 1.68k | std::fill(migMsg->num_incoming, migMsg->num_incoming + nPes, 0); |
537 | | |
538 | | #if DEBUG__TREE_LB_L1 |
539 | | double t0 = CkWallTimer(); |
540 | | #endif |
541 | 1.68k | wrapper->prepStrategy(nObjs, nPes, stats_msgs, migMsg); |
542 | 1.68k | wrapper->runStrategy(migMsg); |
543 | 1.68k | if (current_strategy == wrappers.size() - 1) |
544 | 1.67k | { |
545 | 1.67k | if (repeat_strategies) current_strategy = 0; |
546 | 1.67k | } |
547 | 4 | else |
548 | 4 | { |
549 | 4 | current_strategy++; |
550 | 4 | } |
551 | | #if DEBUG__TREE_LB_L1 |
552 | | CkPrintf("[%d] RootLevel::loadBalance - strategy took %f secs\n", CkMyPe(), |
553 | | CkWallTimer() - t0); |
554 | | #endif |
555 | | // need to cast pointer to ensure delete of CMessage_LBStatsMsg_1 is called |
556 | 5.25k | for (auto msg : stats_msgs) delete (LBStatsMsg_1*)msg; |
557 | 1.68k | stats_msgs.clear(); |
558 | 1.68k | nPes = nObjs = 0; |
559 | 1.68k | return migMsg; |
560 | 1.68k | } |
561 | 4 | else |
562 | 4 | { |
563 | 4 | CkAssert(num_groups >= 1); |
564 | 4 | CkAssert(wrappers.size() == 0); |
565 | 4 | if (_lb_args.debug() > 0) |
566 | 2 | CkPrintf("[%d] ROOT: RECEIVED STATS, Total load is %f\n", CkMyPe(), total_load); |
567 | | |
568 | 4 | std::vector<GroupMigration> solution; |
569 | 4 | if (num_groups > 1 && !group_strategy_dummy) |
570 | 2 | { |
571 | 2 | std::vector<std::pair<int, float>> underloaded; |
572 | 2 | std::vector<std::pair<int, float>> overloaded; |
573 | 2 | underloaded.reserve(num_groups); |
574 | 2 | overloaded.reserve(num_groups); |
575 | 2 | float avg_grp_load = total_load / num_groups; |
576 | 2 | float epsilon = avg_grp_load * 0.02; |
577 | 2 | for (auto* sm : stats_msgs) |
578 | 4 | { |
579 | 4 | SubtreeLoadMsg* msg = (SubtreeLoadMsg*)sm; |
580 | 4 | int& grp = msg->pe; |
581 | 4 | float& load = msg->load; |
582 | | //#if DEBUG__TREE_LB_L1 |
583 | 4 | if (_lb_args.debug() > 1) |
584 | 0 | CkPrintf("[%d] PE %d load = %f\n", CkMyPe(), grp, load); |
585 | | //#endif |
586 | 4 | if (load < avg_grp_load - epsilon) |
587 | 2 | { |
588 | 2 | underloaded.emplace_back(grp, load); |
589 | 2 | } |
590 | 2 | else if (load > avg_grp_load + epsilon) |
591 | 2 | { |
592 | 2 | overloaded.emplace_back(grp, load); |
593 | 2 | } |
594 | 4 | delete msg; |
595 | 4 | } |
596 | 2 | stats_msgs.clear(); |
597 | 2 | size_t underloaded_idx = 0; |
598 | 2 | for (auto& ov : overloaded) |
599 | 2 | { |
600 | 2 | int& g1 = ov.first; |
601 | 2 | float& l1 = ov.second; |
602 | 4 | while ((l1 - avg_grp_load > epsilon) && (underloaded_idx < underloaded.size())) |
603 | 2 | { |
604 | 2 | int& g2 = underloaded[underloaded_idx].first; |
605 | 2 | float& l2 = underloaded[underloaded_idx].second; |
606 | 2 | float transfer = std::min(l1 - avg_grp_load, avg_grp_load - l2); |
607 | 2 | solution.emplace_back(g1, g2, roundf(FLOAT_TO_INT_MULT * transfer)); |
608 | | //#if DEBUG__TREE_LB_L1 |
609 | 2 | if (_lb_args.debug() > 0) |
610 | 2 | CkPrintf("[%d] Root: moving %f load from %d to %d\n", CkMyPe(), transfer, |
611 | 2 | g1, g2); |
612 | | //#endif |
613 | 2 | l2 += transfer; |
614 | | /// underloaded[underloaded_idx].second += transfer; |
615 | 2 | if (l2 >= avg_grp_load - epsilon) underloaded_idx += 1; |
616 | 2 | l1 -= transfer; |
617 | 2 | } |
618 | 2 | } |
619 | 2 | } |
620 | 2 | else |
621 | 2 | { |
622 | 2 | for (auto* sm : stats_msgs) delete (SubtreeLoadMsg*)sm; |
623 | 2 | stats_msgs.clear(); |
624 | 2 | } |
625 | | |
626 | 4 | total_load = 0.0; |
627 | | |
628 | 4 | int nmoves = int(solution.size()); |
629 | 4 | SubtreeMigrateDecisionMsg* migMsg = |
630 | 4 | new (nmoves, nmoves, nmoves, 0) SubtreeMigrateDecisionMsg; |
631 | 4 | migMsg->num_moves = nmoves; |
632 | 6 | for (int i = 0; i < solution.size(); i++) |
633 | 2 | { |
634 | 2 | auto& mig = solution[i]; |
635 | 2 | migMsg->src_groups[i] = mig.src_group; |
636 | 2 | migMsg->dest_groups[i] = mig.dst_group; |
637 | 2 | migMsg->loads[i] = mig.load; |
638 | 2 | } |
639 | 4 | return migMsg; |
640 | 4 | } |
641 | 1.68k | } |
642 | | |
643 | | protected: |
644 | | struct GroupMigration |
645 | | { |
646 | | GroupMigration(int src, int dst, float _load) |
647 | 2 | : src_group(src), dst_group(dst), load(_load) |
648 | 2 | { |
649 | 2 | } |
650 | | int src_group; |
651 | | int dst_group; |
652 | | int load; |
653 | | }; |
654 | | |
655 | | int num_groups; |
656 | | bool repeat_strategies; |
657 | | size_t current_strategy = 0; |
658 | | bool group_strategy_dummy = false; // if true, don't balance load between groups |
659 | | unsigned int nPes = 0; // total number of processors in msgs I am processing |
660 | | unsigned int nObjs = 0; // total number of objects in msgs I am processing |
661 | | float total_load = 0; |
662 | | std::vector<IStrategyWrapper*> wrappers; |
663 | | }; |
664 | | |
665 | | // ---------------- NodeSetLevel ---------------- |
666 | | |
667 | | class NodeSetLevel : public LevelLogic |
668 | | { |
669 | | public: |
670 | 6 | NodeSetLevel(LBManager* _lbmgr, std::vector<int>& _pes) : lbmgr(_lbmgr), pes(_pes) {} |
671 | | |
672 | | virtual ~NodeSetLevel() |
673 | 0 | { |
674 | 0 | for (auto w : wrappers) delete w; |
675 | 0 | } |
676 | | |
677 | | virtual void configure(bool rateAware, std::vector<std::string> strategies, |
678 | | json& config, bool repeat_strategies = false, |
679 | | int _cutoff_freq = 1) |
680 | 6 | { |
681 | 6 | using namespace TreeStrategy; |
682 | 6 | for (auto w : wrappers) delete w; |
683 | 6 | wrappers.clear(); |
684 | 6 | current_strategy = 0; |
685 | 6 | for (const std::string& strategy_name : strategies) |
686 | 6 | { |
687 | 6 | if (rateAware) |
688 | 0 | { |
689 | 0 | wrappers.push_back(new StrategyWrapper<Obj<1>, Proc<1, true>>( |
690 | 0 | strategy_name, false, config[strategy_name])); |
691 | 0 | } |
692 | 6 | else |
693 | 6 | { |
694 | 6 | wrappers.push_back(new StrategyWrapper<Obj<1>, Proc<1, false>>( |
695 | 6 | strategy_name, false, config[strategy_name])); |
696 | 6 | } |
697 | 6 | } |
698 | 6 | this->repeat_strategies = repeat_strategies;; |
699 | 6 | cutoff_freq = _cutoff_freq; |
700 | 6 | CkAssert(cutoff_freq > 0); |
701 | 6 | } |
702 | | |
703 | | virtual void depositStats(TreeLBMessage* stats) |
704 | 21 | { |
705 | 21 | stats_msgs.push_back(stats); |
706 | 21 | nPes += ((LBStatsMsg_1*)stats)->nPes; |
707 | 21 | nObjs += ((LBStatsMsg_1*)stats)->nObjs; |
708 | 21 | } |
709 | | |
710 | 36 | virtual bool cutoff() { return (lbmgr->step() + 1) % cutoff_freq != 0; } |
711 | | |
712 | | virtual TreeLBMessage* mergeStats() |
713 | 6 | { |
714 | 6 | CkAssert(wrappers.size() > current_strategy); |
715 | 6 | IStrategyWrapper* wrapper = wrappers[current_strategy]; |
716 | 6 | CkAssert(wrapper != nullptr); |
717 | | |
718 | 6 | num_children = stats_msgs.size(); |
719 | 6 | CkAssert(num_children > 0); |
720 | | #if DEBUG__TREE_LB_L2 |
721 | | CkPrintf("[%d] NodeSetLevel::mergeStats, num_children=%d nPes=%d nObjs=%d\n", |
722 | | CkMyPe(), num_children, nPes, nObjs); |
723 | | #endif |
724 | | |
725 | 6 | CkAssert(migMsg == nullptr); |
726 | 6 | int total_npes = CkNumPes(); |
727 | 6 | migMsg = new (total_npes, total_npes, nObjs, 0) LLBMigrateMsg; |
728 | 6 | migMsg->n_moves = 0; |
729 | 6 | std::fill(migMsg->num_incoming, migMsg->num_incoming + total_npes, 0); |
730 | | |
731 | 6 | float subtree_load = wrapper->prepStrategy(nObjs, nPes, stats_msgs, migMsg); |
732 | | // need to cast pointer to ensure delete of CMessage_LBStatsMsg_1 is called |
733 | 7 | for (auto msg : stats_msgs) delete (LBStatsMsg_1*)msg; |
734 | 6 | stats_msgs.clear(); |
735 | 6 | nPes = nObjs = 0; |
736 | | |
737 | 6 | SubtreeLoadMsg* newMsg = new SubtreeLoadMsg; |
738 | 6 | newMsg->pe = CkMyPe(); |
739 | 6 | newMsg->load = subtree_load; |
740 | 6 | return newMsg; |
741 | 6 | } |
742 | | |
743 | | virtual void processDecision(TreeLBMessage* decision, int& incoming, int& outgoing) |
744 | 6 | { |
745 | 6 | SubtreeMigrateDecisionMsg* d = (SubtreeMigrateDecisionMsg*)decision; |
746 | 6 | incoming = outgoing = 0; |
747 | 10 | for (int i = 0; i < d->num_moves; i++) |
748 | 4 | { |
749 | 4 | int& src_group = d->src_groups[i]; |
750 | 4 | int& dest_group = d->dest_groups[i]; |
751 | 4 | int& load = d->loads[i]; |
752 | 4 | if (src_group == CkMyPe()) |
753 | 2 | outgoing += load; |
754 | 2 | else if (dest_group == CkMyPe()) |
755 | 2 | incoming += load; |
756 | 4 | CkAssert(src_group != dest_group); |
757 | 4 | } |
758 | | #if DEBUG__TREE_LEVELS_L2 |
759 | | CkPrintf("[%d] NodeSetLevel: incoming=%d outgoing=%d\n", CkMyPe(), incoming, |
760 | | outgoing); |
761 | | #else |
762 | 6 | if (CkMyPe() == 0 && _lb_args.debug() > 1) |
763 | 0 | CkPrintf("[%d] NodeSetLevel: incoming=%d outgoing=%d\n", CkMyPe(), incoming, |
764 | 0 | outgoing); |
765 | 6 | #endif |
766 | 6 | } |
767 | | |
768 | 2 | virtual bool makesTokens() { return true; } |
769 | | |
770 | | virtual int getTokenSets(TreeLBMessage* transferMsg, |
771 | | std::vector<TreeLBMessage*>& token_sets, |
772 | | std::vector<int>& destinations) |
773 | 2 | { |
774 | 2 | IStrategyWrapper* wrapper = wrappers[current_strategy]; |
775 | | |
776 | | // tokens will be list of local object id, obj load, and current PE |
777 | | |
778 | | // TODO need a good algorithm to find a subset of objects whose aggregate load |
779 | | // closely matches the load that is supposed to be sent to each destination subtree. |
780 | | // this is NOT efficient |
781 | 2 | SubtreeMigrateDecisionMsg* d = (SubtreeMigrateDecisionMsg*)transferMsg; |
782 | 2 | int outgoing_nominal_load = 0; |
783 | 2 | std::vector<Token> tokens; |
784 | 4 | for (int i = 0; i < d->num_moves; i++) |
785 | 2 | { |
786 | 2 | int& src = d->src_groups[i]; |
787 | 2 | if (src == CkMyPe()) |
788 | 2 | { |
789 | 2 | int& dest = d->dest_groups[i]; |
790 | 2 | float load = float(d->loads[i]) / FLOAT_TO_INT_MULT; |
791 | 2 | int nominal_load = d->loads[i]; |
792 | 2 | outgoing_nominal_load += nominal_load; |
793 | 2 | tokens.clear(); |
794 | | #if DEBUG__TREE_LB_L1 |
795 | | CkPrintf("[%d] NodeSetLevel: I have to transfer %f load to %d\n", CkMyPe(), load, |
796 | | dest); |
797 | | #endif |
798 | 2 | destinations.push_back(dest); |
799 | 2 | float transferred = 0; |
800 | 2 | int local_id, oldPe; |
801 | 2 | float oload; |
802 | 60 | while (transferred < load) |
803 | 58 | { |
804 | 58 | wrapper->removeObj(local_id, oldPe, oload); |
805 | 58 | transferred += oload; |
806 | 58 | tokens.emplace_back(local_id, oldPe, oload); |
807 | | #if DEBUG__TREE_LB_L2 |
808 | | CkPrintf("[%d] Sending obj with local_obj_id=%d oldPe=%d load=%f TO %d\n", |
809 | | CkMyPe(), local_id, oldPe, oload, dest); |
810 | | #endif |
811 | 58 | } |
812 | 2 | int ntokens = tokens.size(); |
813 | 2 | TokenListMsg* token_set_msg = new (ntokens, ntokens, ntokens, 0) TokenListMsg; |
814 | 2 | token_set_msg->load = nominal_load; |
815 | 2 | token_set_msg->num_tokens = ntokens; |
816 | 60 | for (int j = 0; j < ntokens; j++) |
817 | 58 | { |
818 | 58 | auto& token = tokens[j]; |
819 | 58 | token_set_msg->local_ids[j] = token.obj_local_id; |
820 | 58 | token_set_msg->oldPes[j] = token.oldPe; |
821 | 58 | token_set_msg->loads[j] = token.load; |
822 | 58 | } |
823 | 2 | token_sets.push_back(token_set_msg); |
824 | 2 | } |
825 | 2 | } |
826 | 2 | return outgoing_nominal_load; |
827 | 2 | } |
828 | | |
829 | | virtual int tokensReceived(TreeLBMessage* msg) |
830 | 2 | { |
831 | 2 | IStrategyWrapper* wrapper = wrappers[current_strategy]; |
832 | 2 | TokenListMsg* token_set = (TokenListMsg*)msg; |
833 | 60 | for (int i = 0; i < token_set->num_tokens; i++) |
834 | 58 | { |
835 | | #if DEBUG__TREE_LB_L2 |
836 | | CkPrintf("[%d] Adding object with local_id=%d from oldPe=%d with load %f\n", |
837 | | CkMyPe(), token_set->local_ids[i], token_set->oldPes[i], |
838 | | token_set->loads[i]); |
839 | | #endif |
840 | 58 | wrapper->addForeignObject(token_set->local_ids[i], token_set->oldPes[i], |
841 | 58 | token_set->loads[i]); |
842 | 58 | } |
843 | 2 | int load = token_set->load; |
844 | | #if DEBUG__TREE_LB_L2 |
845 | | CkPrintf("[%d] Total nominal load in token set is %d\n", CkMyPe(), load); |
846 | | #endif |
847 | 2 | delete token_set; |
848 | 2 | return load; |
849 | 2 | } |
850 | | |
851 | | virtual TreeLBMessage* loadBalance(IDM& idm) |
852 | 18 | { |
853 | 18 | CkAssert(wrappers.size() > current_strategy); |
854 | 18 | IStrategyWrapper* wrapper = wrappers[current_strategy]; |
855 | 18 | CkAssert(wrapper != nullptr); |
856 | | |
857 | 18 | if (cutoff()) |
858 | 12 | { |
859 | 12 | num_children = stats_msgs.size(); |
860 | 12 | CkAssert(num_children > 0); |
861 | | #if DEBUG__TREE_LB_L2 |
862 | | CkPrintf( |
863 | | "[%d] NodeSetLevel::loadBalance (w cutoff), num_children=%d nPes=%d nObjs=%d\n", |
864 | | CkMyPe(), num_children, nPes, nObjs); |
865 | | #endif |
866 | | |
867 | 12 | CkAssert(migMsg == nullptr); |
868 | 12 | int total_npes = CkNumPes(); |
869 | 12 | migMsg = new (total_npes, total_npes, nObjs, 0) LLBMigrateMsg; |
870 | 12 | migMsg->n_moves = 0; |
871 | 12 | std::fill(migMsg->num_incoming, migMsg->num_incoming + total_npes, 0); |
872 | | |
873 | 12 | wrapper->prepStrategy(nObjs, nPes, stats_msgs, migMsg); |
874 | | // need to cast pointer to ensure delete of CMessage_LBStatsMsg_1 is called |
875 | 14 | for (auto msg : stats_msgs) delete (LBStatsMsg_1*)msg; |
876 | 12 | stats_msgs.clear(); |
877 | 12 | nPes = nObjs = 0; |
878 | 12 | } |
879 | 18 | wrapper->runStrategy(migMsg, &idm); |
880 | 18 | if (current_strategy == wrappers.size() - 1) |
881 | 18 | { |
882 | 18 | if (repeat_strategies) current_strategy = 0; |
883 | 18 | } |
884 | 0 | else |
885 | 0 | { |
886 | 0 | current_strategy++; |
887 | 0 | } |
888 | 18 | TreeLBMessage* decision = migMsg; |
889 | 18 | migMsg = nullptr; |
890 | 18 | return decision; |
891 | 18 | } |
892 | | |
893 | | protected: |
894 | | struct Token |
895 | | { |
896 | | Token(int obj_local_id, int oldPe, float load) |
897 | 58 | : obj_local_id(obj_local_id), oldPe(oldPe), load(load) |
898 | 58 | { |
899 | 58 | } |
900 | | int obj_local_id; |
901 | | int oldPe; |
902 | | float load; |
903 | | }; |
904 | | |
905 | | LBManager* lbmgr; |
906 | | bool repeat_strategies; |
907 | | size_t current_strategy = 0; |
908 | | std::vector<IStrategyWrapper*> wrappers; |
909 | | LLBMigrateMsg* migMsg = nullptr; |
910 | | std::vector<int> pes; |
911 | | unsigned int num_children = 0; |
912 | | unsigned int nPes = |
913 | | 0; // total number of processors in msgs I am processing (from my subtree) |
914 | | unsigned int nObjs = |
915 | | 0; // total number of objects in msgs I am processing (from my subtree) |
916 | | int cutoff_freq = 0; |
917 | | }; |
918 | | |
919 | | // ---------------- NodeLevel ---------------- |
920 | | |
921 | | class NodeLevel : public LevelLogic |
922 | | { |
923 | | public: |
924 | 168 | NodeLevel(LBManager* _lbmgr, std::vector<int>& _pes) : lbmgr(_lbmgr), pes(_pes) {} |
925 | | |
926 | | virtual ~NodeLevel() |
927 | 155 | { |
928 | 155 | for (auto w : wrappers) delete w; |
929 | 155 | } |
930 | | |
931 | | virtual void configure(bool rateAware, std::vector<std::string> strategies, |
932 | | json& config, bool repeat_strategies = false, |
933 | | int _cutoff_freq = 1) |
934 | 168 | { |
935 | 168 | using namespace TreeStrategy; |
936 | 168 | for (auto w : wrappers) delete w; |
937 | 168 | wrappers.clear(); |
938 | 168 | current_strategy = 0; |
939 | 168 | for (const std::string& strategy_name : strategies) |
940 | 168 | { |
941 | 168 | if (rateAware) |
942 | 0 | { |
943 | 0 | wrappers.push_back(new StrategyWrapper<Obj<1>, Proc<1, true>>( |
944 | 0 | strategy_name, false, config[strategy_name])); |
945 | 0 | } |
946 | 168 | else |
947 | 168 | { |
948 | 168 | wrappers.push_back(new StrategyWrapper<Obj<1>, Proc<1, false>>( |
949 | 168 | strategy_name, false, config[strategy_name])); |
950 | 168 | } |
951 | 168 | } |
952 | 168 | this->repeat_strategies = repeat_strategies; |
953 | 168 | cutoff_freq = _cutoff_freq; |
954 | 168 | CkAssert(cutoff_freq > 0); |
955 | 168 | } |
956 | | |
957 | 60 | virtual bool cutoff() { return (lbmgr->step() + 1) % cutoff_freq != 0; } |
958 | | |
959 | | virtual TreeLBMessage* mergeStats() |
960 | 24 | { |
961 | | // send obj loads up |
962 | 24 | TreeLBMessage* newMsg = LBStatsMsg_1::merge(stats_msgs); |
963 | | // need to cast pointer to ensure delete of CMessage_LBStatsMsg_1 is called |
964 | 56 | for (auto m : stats_msgs) delete (LBStatsMsg_1*)m; |
965 | 24 | stats_msgs.clear(); |
966 | 24 | return newMsg; |
967 | 24 | } |
968 | | |
969 | | virtual void processDecision(TreeLBMessage* decision, int& incoming, int& outgoing) |
970 | 24 | { |
971 | | // will just forward the decision from the root |
972 | 24 | CkReferenceMsg(decision); // Add a reference since caller deletes this message |
973 | 24 | this->decision = decision; |
974 | 24 | incoming = outgoing = 0; |
975 | 24 | } |
976 | | |
977 | | virtual TreeLBMessage* loadBalance(IDM& idm) |
978 | 30 | { |
979 | 30 | if (cutoff()) |
980 | 6 | { |
981 | 6 | return withinNodeLoadBalance(); |
982 | 6 | } |
983 | 24 | else |
984 | 24 | { |
985 | | // just forward decision from root to children |
986 | 24 | return decision; |
987 | 24 | } |
988 | 30 | } |
989 | | |
990 | | protected: |
991 | | LLBMigrateMsg* withinNodeLoadBalance() |
992 | 6 | { |
993 | 6 | CkAssert(wrappers.size() > current_strategy); |
994 | 6 | IStrategyWrapper* wrapper = wrappers[current_strategy]; |
995 | 6 | CkAssert(wrapper != nullptr); |
996 | 6 | CkAssert(pes.size() > 0); |
997 | | |
998 | 6 | unsigned int nObjs = 0; |
999 | 6 | unsigned int nPes = 0; |
1000 | 6 | for (auto msg : stats_msgs) |
1001 | 16 | { |
1002 | 16 | nObjs += ((LBStatsMsg_1*)msg)->nObjs; |
1003 | 16 | nPes += ((LBStatsMsg_1*)msg)->nPes; |
1004 | 16 | } |
1005 | 6 | CkAssert(nPes == pes.size()); |
1006 | | #if DEBUG__TREE_LB_L1 |
1007 | | if (CkMyPe() == 0) |
1008 | | CkPrintf("[%d] NodeLevel::withinNodeLoadBalance - nPes=%d nObjs=%d\n", CkMyPe(), |
1009 | | nPes, nObjs); |
1010 | | #endif |
1011 | | |
1012 | 6 | int total_npes = CkNumPes(); |
1013 | 6 | LLBMigrateMsg* migMsg = new (total_npes, total_npes, nObjs, 0) LLBMigrateMsg; |
1014 | 6 | migMsg->n_moves = 0; |
1015 | 6 | std::fill(migMsg->num_incoming, migMsg->num_incoming + total_npes, 0); |
1016 | | |
1017 | | #if DEBUG__TREE_LB_L2 |
1018 | | double t0 = CkWallTimer(); |
1019 | | #endif |
1020 | 6 | wrapper->prepStrategy(nObjs, nPes, stats_msgs, migMsg); |
1021 | 6 | wrapper->runStrategy(migMsg); |
1022 | | #if DEBUG__TREE_LB_L2 |
1023 | | CkPrintf("[%d] NodeLevel::withinNodeLoadBalance - strategy took %f secs\n", CkMyPe(), |
1024 | | CkWallTimer() - t0); |
1025 | | #endif |
1026 | 6 | if (current_strategy == wrappers.size() - 1) |
1027 | 6 | { |
1028 | 6 | if (repeat_strategies) current_strategy = 0; |
1029 | 6 | } |
1030 | 0 | else |
1031 | 0 | { |
1032 | 0 | current_strategy++; |
1033 | 0 | } |
1034 | | // need to cast pointer to ensure delete of CMessage_LBStatsMsg_1 is called |
1035 | 16 | for (auto msg : stats_msgs) delete (LBStatsMsg_1*)msg; |
1036 | 6 | stats_msgs.clear(); |
1037 | 6 | return migMsg; |
1038 | 6 | } |
1039 | | |
1040 | | LBManager* lbmgr; |
1041 | | bool repeat_strategies; |
1042 | | size_t current_strategy = 0; |
1043 | | std::vector<IStrategyWrapper*> wrappers; |
1044 | | TreeLBMessage* decision = nullptr; |
1045 | | std::vector<int> pes; |
1046 | | int cutoff_freq = 0; |
1047 | | }; |
1048 | | |
1049 | | // ---------------- PELevel ---------------- |
1050 | | |
1051 | | class PELevel : public LevelLogic |
1052 | | { |
1053 | | public: |
1054 | | struct LDObjLoadGreater |
1055 | | { |
1056 | | inline bool operator()(const LDObjData& o1, const LDObjData& o2) const |
1057 | 0 | { |
1058 | 0 | return (o1.wallTime > o2.wallTime); |
1059 | 0 | } |
1060 | | }; |
1061 | | |
1062 | 1.26k | PELevel(LBManager* _lbmgr) : lbmgr(_lbmgr), rateAware(_lb_args.testPeSpeed()) {} |
1063 | | |
1064 | 479 | virtual ~PELevel() {} |
1065 | | |
1066 | | virtual TreeLBMessage* getStats() |
1067 | 5.91k | { |
1068 | 5.91k | const int mype = CkMyPe(); |
1069 | 5.91k | int nobjs = lbmgr->GetObjDataSz(); |
1070 | 5.91k | std::vector<LDObjData> allLocalObjs(nobjs); |
1071 | 5.91k | if (nobjs > 0) lbmgr->GetObjData(allLocalObjs.data()); // populate allLocalObjs |
1072 | 5.91k | myObjs.clear(); |
1073 | 5.91k | LBRealType nonMigratableLoad = 0; |
1074 | 94.7k | for (int i = 0; i < nobjs; i++) |
1075 | 88.8k | { |
1076 | 88.8k | if (allLocalObjs[i].migratable) |
1077 | 88.6k | { |
1078 | 88.6k | myObjs.emplace_back(allLocalObjs[i]); |
1079 | 88.6k | } |
1080 | 139 | else |
1081 | 139 | { |
1082 | 139 | nonMigratableLoad += allLocalObjs[i].wallTime; |
1083 | 139 | } |
1084 | 88.8k | } |
1085 | 5.91k | nobjs = myObjs.size(); |
1086 | | |
1087 | | // TODO verify that non-migratable objects are not added to msg and are only counted |
1088 | | // as background load |
1089 | | |
1090 | | #if DEBUG__TREE_LB_L3 |
1091 | | float total_obj_load = 0; |
1092 | | for (int i = 0; i < nobjs; i++) total_obj_load += myObjs[i].wallTime; |
1093 | | CkPrintf("[%d] PELevel::getStats, myObjs=%d, aggregate_obj_load=%f\n", mype, |
1094 | | int(myObjs.size()), total_obj_load); |
1095 | | #endif |
1096 | | |
1097 | | // TODO sending comm info: only send if needed by an active strategy: |
1098 | | // this could be tricky for trees with more than 2 levels and each level is |
1099 | | // cycling through multiple strategies |
1100 | | |
1101 | | // std::sort(myObjs.begin(), myObjs.end(), PELevel::LDObjLoadGreater()); // sort |
1102 | | // descending order of load |
1103 | | |
1104 | | // allocate and populate stats msg |
1105 | 5.91k | LBStatsMsg_1* msg; |
1106 | 5.91k | if (rateAware) |
1107 | 0 | { |
1108 | 0 | msg = new (1, 1, 1, 2, nobjs, nobjs, 0) LBStatsMsg_1; |
1109 | 0 | msg->speeds[0] = float(lbmgr->ProcessorSpeed()); |
1110 | 0 | } |
1111 | 5.91k | else |
1112 | 5.91k | msg = new (1, 1, 0, 2, nobjs, nobjs, 0) LBStatsMsg_1; |
1113 | 5.91k | msg->nObjs = nobjs; |
1114 | 5.91k | msg->nPes = 1; |
1115 | 5.91k | msg->pe_ids[0] = mype; |
1116 | 5.91k | msg->obj_start[0] = 0; |
1117 | 5.91k | msg->obj_start[1] = nobjs; |
1118 | 95.4k | for (int i = 0; i < nobjs; i++) |
1119 | 89.5k | { |
1120 | | // If rateAware, convert object loads by multiplying by processor speed |
1121 | | // Note this conversion isn't done for bgloads because they never leave the PE |
1122 | 89.5k | if (rateAware) |
1123 | 0 | msg->oloads[i] = float(myObjs[i].wallTime) * msg->speeds[0]; |
1124 | 89.5k | else |
1125 | 89.5k | msg->oloads[i] = float(myObjs[i].wallTime); |
1126 | 89.5k | msg->order[i] = i; |
1127 | 89.5k | } |
1128 | | |
1129 | 5.91k | LBRealType t1, t2, t3, bg_walltime; |
1130 | | #if CMK_LB_CPUTIMER |
1131 | | LBRealType t4; |
1132 | | lbmgr->GetTime(&t1, &t2, &t3, &bg_walltime, &t4); |
1133 | | #else |
1134 | 5.91k | lbmgr->GetTime(&t1, &t2, &t3, &bg_walltime, &bg_walltime); |
1135 | 5.91k | #endif |
1136 | 5.91k | bg_walltime += nonMigratableLoad; |
1137 | 5.91k | if (_lb_args.ignoreBgLoad()) // TODO I think the LBDatabase should return |
1138 | | // bg_walltime=0 if ignoreBGLoad=True |
1139 | 173 | msg->bgloads[0] = 0; |
1140 | 5.73k | else |
1141 | 5.73k | msg->bgloads[0] = float(bg_walltime); |
1142 | | // fprintf(stderr, "[%d] my bgload is %f %f\n", mype, msg->bgloads[0], bg_walltime); |
1143 | | |
1144 | 5.91k | return msg; |
1145 | 5.91k | } |
1146 | | |
1147 | | virtual void processDecision(TreeLBMessage* decision_msg, int& incoming, int& outgoing) |
1148 | 5.92k | { |
1149 | 5.92k | const int mype = CkMyPe(); |
1150 | 5.92k | LLBMigrateMsg* decision = (LLBMigrateMsg*)decision_msg; |
1151 | 5.92k | incoming = decision->num_incoming[mype]; |
1152 | 5.92k | CkAssert(incoming >= 0); |
1153 | 5.92k | outgoing = 0; |
1154 | 5.92k | int obj_start = decision->obj_start[mype]; |
1155 | 5.92k | int obj_end = obj_start + int(myObjs.size()); |
1156 | 5.92k | int j = 0; |
1157 | 97.3k | for (int i = obj_start; i < obj_end; i++, j++) |
1158 | 91.4k | { |
1159 | 91.4k | int dest = decision->to_pes[i]; |
1160 | 91.4k | if (dest != mype) |
1161 | 71.8k | { |
1162 | 71.8k | if (dest >= 0) |
1163 | 71.8k | { |
1164 | | #if DEBUG__TREE_LB_L3 |
1165 | | CkPrintf("[%d] (processDecision) My obj %d (abs=%d) moving to %d\n", CkMyPe(), |
1166 | | j, i, dest); |
1167 | | #endif |
1168 | 71.8k | if (lbmgr->Migrate(myObjs[j].handle, dest) == 0) |
1169 | 0 | { |
1170 | 0 | CkAbort("PELevel: Migrate call returned 0\n"); |
1171 | | // missed.push_back(dest); |
1172 | 0 | } |
1173 | 71.8k | } |
1174 | 36 | else |
1175 | 36 | { |
1176 | | // dest can be < 0, this can happen in some trees, if moving objects |
1177 | | // between subtrees and I don't yet know the final destination PE |
1178 | 36 | outgoing += 1; |
1179 | 36 | } |
1180 | 71.8k | } |
1181 | 91.4k | } |
1182 | | #if DEBUG__TREE_LB_L2 |
1183 | | CkPrintf("[%d] PELevel::processDecision, incoming=%d outgoing=%d\n", CkMyPe(), |
1184 | | incoming, outgoing); |
1185 | | #endif |
1186 | 5.92k | } |
1187 | | |
1188 | | int migrateObjects(const std::vector<std::pair<int, int>>& mig_order) |
1189 | 2 | { |
1190 | 2 | for (auto& move : mig_order) |
1191 | 58 | { |
1192 | 58 | int obj_local_id = move.first; |
1193 | 58 | int toPe = move.second; |
1194 | | #if DEBUG__TREE_LB_L3 |
1195 | | CkPrintf("[%d] (migrateObjects) migrating object with local ID %d to PE %d\n", |
1196 | | CkMyPe(), obj_local_id, toPe); |
1197 | | #endif |
1198 | | // import random |
1199 | | // toPe = random.randint(0, charm.numPes() - 1) # this is to verify that |
1200 | | // verification framework works :) |
1201 | 58 | if (lbmgr->Migrate(myObjs[obj_local_id].handle, toPe) == 0) |
1202 | 0 | { |
1203 | 0 | CkAbort("PELevel: Migrate call returned 0\n"); |
1204 | 0 | } |
1205 | 58 | } |
1206 | 2 | return mig_order.size(); |
1207 | 2 | } |
1208 | | |
1209 | | protected: |
1210 | | LBManager* lbmgr; |
1211 | | bool rateAware; |
1212 | | std::vector<LDObjData> myObjs; |
1213 | | }; |
1214 | | |
1215 | | // ---------------- MsgAggregator ---------------- |
1216 | | |
1217 | | /** |
1218 | | * Currently only understands one msg type (LBStatsMsg_1) but could be made to |
1219 | | * understand multiple |
1220 | | */ |
1221 | | class MsgAggregator : public LevelLogic |
1222 | | { |
1223 | | public: |
1224 | 554 | MsgAggregator() {} |
1225 | | |
1226 | 235 | virtual ~MsgAggregator() {} |
1227 | | |
1228 | | virtual TreeLBMessage* mergeStats() |
1229 | 621 | { |
1230 | 621 | TreeLBMessage* newMsg = LBStatsMsg_1::merge(stats_msgs); |
1231 | | // need to cast pointer to ensure delete of CMessage_LBStatsMsg_1 is called |
1232 | 1.24k | for (auto m : stats_msgs) delete (LBStatsMsg_1*)m; |
1233 | 621 | stats_msgs.clear(); |
1234 | 621 | return newMsg; |
1235 | 621 | } |
1236 | | |
1237 | | virtual std::vector<TreeLBMessage*> splitDecision(TreeLBMessage* decision, |
1238 | | std::vector<int>& children) |
1239 | 2.32k | { |
1240 | 2.32k | const int myNode = CkMyNode(); |
1241 | 2.32k | std::vector<TreeLBMessage*> decisions(children.size() + 1); |
1242 | | |
1243 | | // The first element is always for the local PE, the caller must delete it manually if |
1244 | | // it is not used |
1245 | 2.32k | decisions[0] = decision; |
1246 | 6.56k | for (int i = 1; i < decisions.size(); ++i) |
1247 | 4.24k | { |
1248 | 4.24k | if (CkNodeOf(children[i-1]) == myNode) |
1249 | 3.43k | { |
1250 | 3.43k | CkReferenceMsg(decision); |
1251 | 3.43k | decisions[i] = decision; |
1252 | 3.43k | } |
1253 | | // Each child in another process gets its own copy: the send packs the message |
1254 | | // and writes the destination into its header, and the transport may still be |
1255 | | // reading the buffer after the send returns. |
1256 | 810 | else |
1257 | 810 | { |
1258 | 810 | decisions[i] = (TreeLBMessage*)CkCopyMsg((void**)&decision); |
1259 | 810 | } |
1260 | 4.24k | } |
1261 | | |
1262 | 2.32k | return decisions; |
1263 | 2.32k | } |
1264 | | }; |
1265 | | |
1266 | | #endif /* TREELEVEL_H */ |