-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.C
More file actions
485 lines (407 loc) · 14.1 KB
/
scheduler.C
File metadata and controls
485 lines (407 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/**
* BlockScheduler
*
* Plan on a set of trailing updates up to the block (U or L) limit, unless the
* block is local or shared by another trailing update. A block is eligible to
* be planned if its necessary dependencies on other blocks have been
* fulfilled, by that block having been previously planned. To simplify the
* dependencies the block are split into regions (called panels in the code) to
* signify that they have the same number of pending dependencies because they
* are in the same region. This is explained further in a technical report,
* "Exploring Partial Synchrony in an Asynchronous Environment Using Dense LU".
*/
#include "scheduler.h"
#include "lu.decl.h"
#include "messages.h"
#include "lu.h"
#include <algorithm>
using std::min;
using std::find;
#include <utility>
using std::pair;
using std::make_pair;
#include "register.h"
using std::list;
using std::map;
// The limit on the number of concurrent outgoing sends
#ifndef SEND_LIM
#error Please define some value for the macro SEND_LIM appropriate to the machine you are running on
#endif
static const int SEND_LIMIT = SEND_LIM;
inline bool operator==(const CkIndex2D &l, const CkIndex2D &r) {
return l.x == r.x && l.y == r.y;
}
pair<int, int> make_pair(CkIndex2D index) {
return make_pair(index.x, index.y);
}
BlockScheduler::BlockScheduler(CProxy_LUBlk luArr_, LUConfig config, CProxy_LUMgr mgr_)
: luArr(luArr_), mgr(mgr_.ckLocalBranch()), inProgress(false), inPumpMessages(false), numActive(0)
, pendingTriggered(0), reverseSends(CkMyPe() % 2 == 0)
, maxMemory(0), maxMemoryIncreases(0), maxMemoryStep(-1) {
// Calculate the block limit based on the memory threshold
blockLimit = config.memThreshold * 1024 * 1024 /
(config.blockSize * (config.blockSize + 1) * sizeof(double) + sizeof(LUBlk) + sdagOverheadPerBlock);
contribute(CkCallback(CkIndex_LUBlk::schedulerReady(NULL), luArr));
}
void BlockScheduler::scheduleSend(blkMsg *msg, bool onActive) {
list<blkMsg *>::iterator iter = find(scheduledSends.begin(), scheduledSends.end(), msg);
if (iter == scheduledSends.end()) {
scheduledSends.push_back(msg);
}
pumpMessages();
}
void BlockScheduler::releaseActiveColumn(const int y, const int t) {
for (StateList::iterator iter = localBlocks.begin();
iter != localBlocks.end(); ++iter) {
if (iter->iy == y && t >= iter->updatesPlanned &&
iter->pendingDependencies.size() > 0) {
for (list<Panel*>::iterator depPanels = iter->pendingDependencies.begin();
depPanels != iter->pendingDependencies.end(); ++depPanels) {
Panel &panel = **depPanels;
panel.dependents.remove(iter);
}
iter->pendingDependencies.clear();
}
}
}
void BlockScheduler::scheduleSend(CkIndex2D index, bool onActive) {
blkMsg *msg = luArr(index).ckLocal()->LUmsg;
scheduleSend(msg, onActive);
}
// If the processor is idle, have Converse call this function to pump message
// out to ensure progress is made
void pumpOnIdle(void *s) {
BlockScheduler *scheduler = (BlockScheduler *)s;
scheduler->pumpMessages();
}
void BlockScheduler::pumpMessages() {
if (inPumpMessages) return;
inPumpMessages = true;
size_t curMemory = CmiMemoryUsage()/1024;
if (curMemory > maxMemory) {
maxMemory = curMemory;
maxMemoryIncreases++;
if (plannedUpdates.size() > 0)
maxMemoryStep = plannedUpdates.front().t;
}
for (list<blkMsg*>::iterator iter = sendsInFlight.begin();
iter != sendsInFlight.end(); ++iter) {
blkMsg *msg = *iter;
int ref = REFFIELD(UsrToEnv(msg));
CkAssert(ref > 0 && ref <= 2);
if (ref == 1) {
if (!msg->firstHalfSent) {
msg->firstHalfSent = true;
propagateBlkMsg(msg);
} else {
map<pair<int, int>, wantedBlock>::iterator blockIter =
wantedBlocks.find(make_pair(msg->src));
if (blockIter != wantedBlocks.end()) {
wantedBlock &block = blockIter->second;
CkAssert(block.isSending);
block.isSending = false;
if (block.refs.size() == 0) {
delete msg;
wantedBlocks.erase(blockIter);
progress();
}
} else {
// If local, needs to be set back to false, so resetMessage gets called
msg->firstHalfSent = false;
}
iter = sendsInFlight.erase(iter);
}
}
}
for (list<blkMsg*>::iterator iter = scheduledSends.begin();
iter != scheduledSends.end() && sendsInFlight.size() < SEND_LIMIT;
++iter) {
if (find(sendsInFlight.begin(), sendsInFlight.end(), *iter) ==
sendsInFlight.end()) {
sendsInFlight.push_back(*iter);
propagateBlkMsg(*iter);
iter = scheduledSends.erase(iter);
}
}
if (sendsInFlight.size() != 0 || scheduledSends.size() != 0) {
CcdCallOnCondition(CcdPROCESSOR_STILL_IDLE, pumpOnIdle, this);
}
inPumpMessages = false;
}
void BlockScheduler::printBlockLimit() {
CkPrintf("%d: block limit = %d\n", CkMyPe(), blockLimit);
}
// When a LUBlk registers, initialize the dependencies based on the presence of
// this block
void BlockScheduler::registerBlock(CkIndex2D index) {
blockLimit--;
if (index.y != 0) {
localBlocks.push_back(BlockState(index));
if (index.x != 0) {
for (int i = 1; i < min(index.x, index.y) + 1; i++)
Upanels[i].updatesLeftToPlan++;
}
if (index.y > index.x)
panels[index.x].updatesLeftToPlan++;
}
if (blockLimit < 2)
CkAbort("Too little space to plan even one trailing update");
}
void printMemory(void *time, void *msg) {
int *s = (int*) ((CkReductionMsg *)msg)->getData();
int mem = s[0];
CkPrintf("%s memory usage: %zu KiB, additional stats: ", time, mem);
for (int i = 1; i < ((CkReductionMsg*)msg)->getLength() / sizeof(int); i++)
CkPrintf("%zd ", s[i]);
CkPrintf("\n");
delete (CkReductionMsg*)msg;
}
void BlockScheduler::outputStats() {
int stats[3];
stats[0] = maxMemory;
stats[1] = maxMemoryIncreases;
stats[2] = maxMemoryStep;
contribute(3 * sizeof(int), stats, CkReduction::max_int,
CkCallback(&printMemory, const_cast<char*>("Peak")));
}
void BlockScheduler::allRegistered(CkReductionMsg *m) {
delete m;
baseMemory = CmiMemoryUsage()/1024;
contribute(sizeof(int), &baseMemory, CkReduction::max_int,
CkCallback(&printMemory, const_cast<char*>("Base")));
// Add dependence for the first iteration if this is needed
for (StateList::iterator iter = localBlocks.begin();
iter != localBlocks.end(); ++iter)
if (iter->ix != 0)
addDependence(panels, 0, iter);
progress();
}
void BlockScheduler::startedActivePanel() {
numActive++;
}
template <typename K>
void BlockScheduler::updatePanel(map<K, Panel> &panels, K index) {
typename map<K, Panel>::iterator iter = panels.find(index);
CkAssert(iter != panels.end());
Panel &panel = iter->second;
panel.updatesLeftToPlan--;
if(panel.updatesLeftToPlan == 0) {
for (list<StateList::iterator>::iterator i = panel.dependents.begin();
i != panel.dependents.end(); ++i) {
(*i)->pendingDependencies.remove(&panel);
}
panels.erase(iter);
}
}
template <typename K>
void BlockScheduler::addDependence(map<K, Panel> &panels, K index,
StateList::iterator block) {
typename map<K, Panel>::iterator panel = panels.find(index);
if (panel != panels.end()) {
block->pendingDependencies.push_back(&panel->second);
panel->second.addDependent(block);
}
}
void BlockScheduler::planUpdate(StateList::iterator target) {
CkAssert(target->pendingDependencies.size() == 0);
int t = target->updatesPlanned++;
plannedUpdates.push_back(Update(&*target, t));
Update &update = plannedUpdates.back();
if (update.isComputeU()) {
CkAssert(t == min(target->ix, target->iy));
if (target->iy == target->ix + 1)
plannedUpdates.pop_back();
else
getBlock(target->ix, target->ix, update.L, &update);
updatePanel(panels, t);
doneBlocks.splice(doneBlocks.end(), localBlocks, target);
return;
}
getBlock(target->ix, t, update.L, &update);
getBlock(t, target->iy, update.U, &update);
if (target->updatesPlanned < min(target->ix, target->iy)) {
addDependence(panels, t+1, target);
addDependence(Upanels, t+1, target);
} else if (target->iy > target->ix) {
CkAssert(target->updatesPlanned == min(target->ix, target->iy));
addDependence(Upanels, t+1, target);
} else
doneBlocks.splice(doneBlocks.end(), localBlocks, target);
updatePanel(Upanels, t+1);
}
void BlockScheduler::getBlock(int srcx, int srcy, double *&data,
Update *update) {
LUBlk *local = luArr(srcx, srcy).ckLocal();
if (local) {
if (local->factored)
data = local->accessLocalBlock();
else
localWantedBlocks[make_pair(srcx, srcy)].push_back(update);
return;
}
pair<int, int> src = make_pair(srcx, srcy);
wantedBlock &block = wantedBlocks[src];
block.refs.push_back(update);
if (block.refs.size() == 1 && !block.isSending) {
// First reference to this block, so ask for it
CkEntryOptions opts;
luArr(src.first, src.second).requestBlock(CkMyPe(), update->target->ix, update->target->iy,
&(mgr->setPrio(GET_BLOCK, opts)));
}
if (block.m)
update->tryDeliver(srcx, srcy, block.data);
}
void BlockScheduler::deliverBlock(blkMsg *m) {
CkAssert(wantedBlocks.find(make_pair(m->src)) != wantedBlocks.end());
wantedBlock &block = wantedBlocks[make_pair(m->src)];
block.m = m;
block.data = m->data;
for (list<Update*>::iterator update = block.refs.begin();
update != block.refs.end(); ++update)
(*update)->tryDeliver(m->src.x, m->src.y, m->data);
CkAssert(m->npes_receiver >= 1);
CkAssert(CkMyPe() == m->pes[m->offset]);
// This processor is no longer part of the set
m->offset++;
m->npes_receiver--;
m->firstHalfSent = false;
if (m->npes_receiver >= 1) {
scheduleSend(m, false);
block.isSending = true;
}
progress();
}
void BlockScheduler::propagateBlkMsg(blkMsg *m) {
envelope *env = UsrToEnv(m);
_SET_USED(env, 0);
if (env->isPacked()) {
unsigned char msgidx = env->getMsgIdx();
if(_msgTable[msgidx]->unpack) {
m = (blkMsg*)_msgTable[msgidx]->unpack(m);
UsrToEnv(m)->setPacked(0);
}
}
if (!m->firstHalfSent) {
LUBlk *block = luArr[m->src].ckLocal();
if (block != NULL) {
block->resetMessage(reverseSends);
reverseSends = !reverseSends;
}
m->npes_sender = m->npes_receiver;
m->npes_receiver = (m->npes_receiver+1) / 2;
} else {
int secondHalf = m->npes_sender - m->npes_receiver;
CkAssert(secondHalf >= 0);
m->offset += m->npes_receiver;
m->npes_receiver = secondHalf;
}
if (m->npes_receiver >= 1) {
takeRef(m);
thisProxy[m->pes[m->offset]].deliverBlock(m);
}
}
void BlockScheduler::dropRef(int srcx, int srcy, Update *update) {
map<pair<int, int>, wantedBlock>::iterator input =
wantedBlocks.find(make_pair(srcx, srcy));
// Ignore local blocks used as inputs
if (input == wantedBlocks.end())
return;
wantedBlock &block = input->second;
block.refs.remove(update);
if (block.refs.size() == 0 && !block.isSending) {
delete block.m;
wantedBlocks.erase(input);
}
}
void BlockScheduler::runUpdate(list<Update>::iterator iter) {
Update &update = *iter;
CkAssert(update.ready());
int tx = update.target->ix, ty = update.target->iy;
update.triggered = true;
pendingTriggered++;
CkEntryOptions opts;
intptr_t update_ptr = (intptr_t)&update;
if (update.isComputeU()) {
luArr(tx, ty).processComputeU(update_ptr,
&(mgr->setPrio(PROCESS_COMPUTE_U, opts)));
} else {
int t = update.t;
luArr(tx, ty).processTrailingUpdate(t, update_ptr,
&(mgr->setPrio(PROCESS_TRAILING_UPDATE,
opts, ty, tx, t)));
}
}
void BlockScheduler::updateDone(intptr_t update_ptr) {
Update &update = *(Update *)update_ptr;
int tx = update.target->ix, ty = update.target->iy;
dropRef(tx, update.t, &update);
if (!update.isComputeU())
dropRef(update.t, ty, &update);
update.target->updatesCompleted++;
pendingTriggered--;
CkAssert(pendingTriggered >= 0);
plannedUpdates.erase(find(plannedUpdates.begin(), plannedUpdates.end(), update));
progress();
}
void BlockScheduler::updateUntriggered() {
pendingTriggered--;
if (pendingTriggered == 0) {
pumpMessages();
}
}
bool BlockScheduler::shouldExecute() {
return numActive == 0;
}
void BlockScheduler::factorizationDone(CkIndex2D index) {
if (index.x >= index.y)
numActive--;
map<pair<int, int>, list<Update*> >::iterator wanters =
localWantedBlocks.find(make_pair(index.x, index.y));
if (wanters != localWantedBlocks.end()) {
list<Update*> &wantList = wanters->second;
CkAssert(luArr(index).ckLocal());
for (list<Update*>::iterator wanter = wantList.begin();
wanter != wantList.end(); ++wanter)
(*wanter)->tryDeliver(index.x, index.y, luArr(index).ckLocal()->accessLocalBlock());
localWantedBlocks.erase(wanters);
}
progress();
}
bool eligibilityYOrder(const BlockState& block1, const BlockState& block2) {
if (block1.pendingDependencies.size() != block2.pendingDependencies.size())
return block1.pendingDependencies.size() < block2.pendingDependencies.size();
else
return block1.iy < block2.iy;
}
void BlockScheduler::progress() {
// Prevent re-entrance
if (inProgress) return;
inProgress = true;
bool stateModified;
do {
stateModified = false;
bool plannedAnything = true;
while (wantedBlocks.size() < blockLimit && plannedAnything) {
plannedAnything = false;
if (localBlocks.size() > 0) {
localBlocks.sort(eligibilityYOrder);
CkAssert(localBlocks.front().pendingDependencies.size() == 0);
planUpdate(localBlocks.begin());
plannedAnything = true;
}
}
if (numActive == 0) {
// Start triangular solves and trailing updates
for (list<Update>::iterator update = plannedUpdates.begin();
update != plannedUpdates.end(); ++update) {
if (update->ready()) {
runUpdate(update);
stateModified = true;
break;
}
}
}
} while (stateModified);
pumpMessages();
inProgress = false;
}