-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coordinator.cc
486 lines (427 loc) · 15.3 KB
/
Coordinator.cc
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
// -*- C++ -*-
//
// Package: Package
// Class : Coordinator
//
// Implementation:
// [Notes on implementation]
//
// Original Author: Chris Jones
// Created: Thu, 14 Mar 2013 19:15:21 GMT
// $Id$
//
// system include files
#include <cassert>
#include <iostream>
#include "tbb/task.h"
// user include files
#include "Coordinator.h"
#include "RunCache.h"
#include "GlobalWatcher.h"
#include "Stream.h"
#include "Source.h"
#include "writeLock.h"
//
// constants, enums and typedefs
//
//
// static data member definitions
//
namespace {
class StreamBeginRunTask : public tbb::task {
public:
StreamBeginRunTask(Stream* iStream, Coordinator* iCoord, const Run* iRun):
m_stream(iStream), m_coord(iCoord), m_run(iRun) {}
tbb::task* execute() {
m_stream->processBeginRun(m_run);
return m_coord->assignWorkTo(m_stream);
}
private:
Stream* m_stream;
Coordinator* m_coord;
const Run* m_run;
};
class StreamBeginRunThenEventTask : public tbb::task {
public:
StreamBeginRunThenEventTask(Stream* iStream, Coordinator* iCoord, const Run* iRun):
m_stream(iStream), m_coord(iCoord), m_run(iRun) {}
tbb::task* execute() {
m_stream->processBeginRun(m_run);
m_stream->processEvent();
return m_coord->assignWorkTo(m_stream);
}
private:
Stream* m_stream;
Coordinator* m_coord;
const Run* m_run;
};
class StreamEndRunTask : public tbb::task {
public:
StreamEndRunTask(Stream* iStream, Coordinator* iCoord, tbb::task* iDoneTask):
m_stream(iStream), m_coord(iCoord), m_doneTask(iDoneTask) {}
tbb::task* execute() {
m_stream->processEndRun();
auto newStreamTask = m_coord->assignWorkTo(m_stream);
auto v = m_doneTask->decrement_ref_count() ;
/*writeLock([&]() {
std::cout <<" decrementing "<<m_doneTask<<" "<<v<<std::endl;
});*/
if( 0 == v ) {
//we want the end of run task to happen before the stream
// task starts
if(nullptr!=newStreamTask) {
tbb::task::spawn(*newStreamTask);
}
return m_doneTask;
}
return newStreamTask;
}
private:
Stream* m_stream;
Coordinator* m_coord;
tbb::task* m_doneTask;
};
class GlobalBeginRunTask : public tbb::task {
public:
GlobalBeginRunTask(GlobalWatcher* iWatcher, Coordinator* iCoordinator, Run* iRun) :
m_watcher(iWatcher),
m_coordinator(iCoordinator),
m_run(iRun) {}
tbb::task* execute() {
//Call to globalBeginRun would be done here
m_watcher->globalBeginRun(*m_run);
m_coordinator->beginRunHasFinished(m_run);
}
private:
GlobalWatcher* m_watcher;
Coordinator* m_coordinator;
Run* m_run;
};
class GlobalEndRunTask : public tbb::task {
public:
//NOTE: in reality, this would need access to the list of modules that want global transitions
GlobalEndRunTask(GlobalWatcher* iWatcher, Coordinator* iCoordinator, Run* iRun,tbb::task* iDoneWithRunTask):
m_watcher(iWatcher),
m_coordinator(iCoordinator),
m_doneTask(iDoneWithRunTask),
m_run(iRun) {
assert(0!=iWatcher);
assert(0!=iCoordinator);
assert(0!=m_doneTask);
}
tbb::task* execute() {
//Call go globalEndRun would be done here
m_watcher->globalEndRun(*m_run);
m_coordinator->doneWithRun(m_run);
tbb::task* returnValue = nullptr;
if( 0 == m_doneTask->decrement_ref_count()) {
returnValue = m_doneTask;
}
return returnValue;
}
private:
GlobalWatcher* m_watcher;
Coordinator* m_coordinator;
tbb::task* m_doneTask;
Run* m_run;
};
class TryLaterTask : public tbb::task {
public:
TryLaterTask(Stream* iStream, Coordinator* iCoordinator):
m_stream(iStream),
m_coordinator(iCoordinator) {}
tbb::task* execute() {
return m_coordinator->assignWorkTo(m_stream);
}
private:
Stream* m_stream;
Coordinator* m_coordinator;
};
}
//
// constructors and destructor
//
Coordinator::Coordinator(tbb::task* iEndTask, Source* iSource, GlobalWatcher* iWatcher, RunCache& iRunCache):
m_waitingTask(iEndTask),
m_source(iSource),
m_watcher(iWatcher),
m_runHandler(iRunCache),
m_activeRun(nullptr),
m_waitingForBeginRunToFinish(),
m_endRunTasks(iRunCache.maxNumberOfConcurrentRuns()),
m_runSumQueues(iRunCache.maxNumberOfConcurrentRuns()),
m_presentRunTransitionID(0),
m_presentRunNumber(0)
{
//presize everything
unsigned int maxRuns = iRunCache.maxNumberOfConcurrentRuns();
m_waitingForBeginRunToFinish.reserve(maxRuns);
for(unsigned int i = 0; i<maxRuns; ++i) {
m_endRunTasks[i].store(nullptr);
m_waitingForBeginRunToFinish.push_back(std::shared_ptr<edm::WaitingTaskList>(new edm::WaitingTaskList));
}
}
// Coordinator::Coordinator(const Coordinator& rhs)
// {
// // do actual copying here;
// }
//Coordinator::~Coordinator()
//{
//}
tbb::task*
Coordinator::assignWorkTo(Stream* iStream) {
return m_queue.pushAndGetNextTaskToRun([iStream,this](){
auto p = this->doAssignWorkTo_(iStream);
if(p) {
tbb::task::spawn(*p);
}
}
);
}
class StreamEndStreamTask : public tbb::task {
public:
StreamEndStreamTask(Stream* iStream, tbb::task* iDoneTask):
m_stream(iStream), m_doneTask(iDoneTask) {}
tbb::task* execute() {
m_stream->processEndStream(m_doneTask);
return 0;
}
private:
Stream* m_stream;
tbb::task* m_doneTask;
};
class StreamEventTask : public tbb::task {
public:
StreamEventTask(Stream* iStream, Coordinator* iCoord):
m_stream(iStream), m_coord(iCoord) {}
tbb::task* execute() {
m_stream->processEvent();
return m_coord->assignWorkTo(m_stream);
}
private:
Stream* m_stream;
Coordinator* m_coord;
unsigned int m_number;
};
static
tbb::task* doRunTaskFirstIfNotNull(tbb::task* iOne, tbb::task* iTwo) {
if (nullptr == iOne) {
return iTwo;
}
if(nullptr!=iTwo) {
tbb::task::spawn(*iTwo);
}
return iOne;
}
tbb::task*
Coordinator::doAssignWorkTo_(Stream* iStream) {
Source::Transitions nextTran = m_source->nextTransition();
if(nextTran == Source::kStop) {
doneProcessing();
//tell Stream to do end processing
if(Stream::kEvent==iStream->state() or Stream::kBeginRun == iStream->state()) {
//need to do endRun
return prepareToRemoveFromRun(iStream);
}
if(Stream::kEndRun == iStream->state() or Stream::kInitialized == iStream->state()) {
//Launch end stream task
tbb::task* task{new (tbb::task::allocate_root()) StreamEndStreamTask(iStream,m_waitingTask)};
return task;
}
}
//If we have a task to finish up for the run (e.g. summing run products), we want that to run before starting
// a task for the Stream. This can be done by pushing the Stream task onto the 'spawn' queue first
tbb::task* runTask = nullptr;
if(nextTran == Source::kRun) {
//RunCache knows if this run has already been 'pulled' or if we are waiting
// for a resource to become available
// When a resource becomes available RunCache puts a 'pull' task onto the Coordinator's queue
//If we get the same Run we just 'pulled' then we pull this
runTask = newRun(m_source->nextRunsNumber());
if(nullptr == runTask) {
if(Stream::kEndRun==iStream->state() or Stream::kInitialized ==iStream->state()) {
writeLock([&]() {
std::cout <<"no available run for stream "<<iStream->id()<<std::endl;
});
m_runHandler.waitForAnAvailableRun(new (tbb::task::allocate_root()) TryLaterTask(iStream,this));
return nullptr;
}
if( m_presentRunTransitionID != iStream->runTransitionID()) {
//need to do endRun
return prepareToRemoveFromRun(iStream);
}
}
//newRun is allowed to 'pull' the transition from the source
nextTran = m_source->nextTransition();
}
if(Stream::kEndRun==iStream->state() or Stream::kInitialized ==iStream->state()) {
if(nextTran == Source::kRun or nextTran == Source::kStop) {
return doRunTaskFirstIfNotNull(runTask,assignToARun(iStream));
}
if(nextTran == Source::kEvent) {
//pull the event
m_source->gotoNextEvent(iStream->event());
return doRunTaskFirstIfNotNull(runTask,assignToRunThenDoEvent(iStream));
}
}
if( m_presentRunTransitionID != iStream->runTransitionID()) {
//need to do endRun
return doRunTaskFirstIfNotNull(runTask,prepareToRemoveFromRun(iStream));
}
if(nextTran == Source::kEvent) {
//process the event
m_source->gotoNextEvent(iStream->event());
tbb::task* task{new (tbb::task::allocate_root()) StreamEventTask(iStream,this)};
return doRunTaskFirstIfNotNull(runTask,task);
}
assert(false);
return runTask;
}
void
Coordinator::beginRunHasFinished(Run* iRun)
{
auto cacheID = iRun->cacheID();
assert(cacheID < m_waitingForBeginRunToFinish.size());
assert(nullptr != m_waitingForBeginRunToFinish[cacheID].get());
m_waitingForBeginRunToFinish[cacheID]->doneWaiting();
}
tbb::task*
Coordinator::assignToARun(Stream* iStream) {
writeLock([&](){
std::cout<<"assignToARun "<<iStream->id()<<std::endl;
});
unsigned int cacheID = m_activeRun->cacheID();
assert(cacheID < m_endRunTasks.size());
/*writeLock([&]() {
std::cout <<" incrementing "<<m_endRunTasks[m_presentCacheID].load()<<" "<<m_presentCacheID<<std::endl;
});*/
m_endRunTasks[cacheID].load()->increment_ref_count();
tbb::task* task{new (tbb::task::allocate_root()) StreamBeginRunTask{iStream,this,m_activeRun}};
//NOTE: would be nice to know if global beginrun was already called so we could just return the new task
assert(cacheID < m_waitingForBeginRunToFinish.size());
m_waitingForBeginRunToFinish[cacheID]->add(task);
return nullptr;
}
tbb::task*
Coordinator::assignToRunThenDoEvent(Stream* iStream) {
unsigned int cacheID = m_activeRun->cacheID();
assert(cacheID < m_endRunTasks.size());
/*writeLock([&]() {
std::cout <<" incrementing "<<m_endRunTasks[m_presentCacheID].load()<<" "<<m_presentCacheID<<std::endl;
});*/
m_endRunTasks[cacheID].load()->increment_ref_count();
tbb::task* task{new (tbb::task::allocate_root()) StreamBeginRunThenEventTask(iStream,this,m_activeRun)};
//NOTE: would be nice to know if global beginrun was already called so we could just return the new task
assert(cacheID < m_waitingForBeginRunToFinish.size());
m_waitingForBeginRunToFinish[cacheID]->add(task);
return nullptr;
}
tbb::task*
Coordinator::prepareToRemoveFromRun(Stream* iStream) {
auto cacheID = iStream->run()->cacheID();
assert(cacheID < m_endRunTasks.size());
tbb::task* endTask = m_endRunTasks[cacheID];
/*writeLock([&]() {
std::cout <<"prepareToRemoveFromRun "<<cacheID<<" "<<iStream->id()<<std::endl;
});*/
assert(nullptr != endTask);
return (new (tbb::task::allocate_root()) StreamEndRunTask(iStream,this,endTask));
}
void
Coordinator::doneWithRun(Run* iRun) {
unsigned int cacheID = iRun->cacheID();
assert(cacheID < m_endRunTasks.size());
m_endRunTasks[cacheID].store(nullptr);
m_runHandler.doneWithRun(cacheID);
}
void
Coordinator::doneProcessing() {
if(m_activeRun) {
unsigned int cacheID = m_activeRun->cacheID();
assert(cacheID < m_endRunTasks.size());
auto v = m_endRunTasks[cacheID].load()->decrement_ref_count();
/*writeLock([&]() {
std::cout <<"decrementing "<<m_presentCacheID<<" "<<m_endRunTasks[m_presentCacheID].load()<<" count "<<v<<std::endl;
});*/
m_activeRun = nullptr;
}
}
tbb::task*
Coordinator::newRun(unsigned int iNewRunsNumber) {
if(iNewRunsNumber == m_presentRunNumber) {
//NOTE: should check to see if this is the same run and if we've already pulled this run
// then this indicates a 'reduction' step should be done
bool keepSumming=true;
tbb::task* returnValue = nullptr;
do {
writeLock([&]() {
std::cout <<"doing summing"<<std::endl;
});
keepSumming=false;
//the following should be put into a task and run asynchronously
// We should have a 'summing' queue for each Run whis is where the
// summing tasks are put.
// It should also be 'dependent' on the Run so we will not run the
// global end run until all summing has finished.
assert(m_activeRun != nullptr);
auto id = m_source->nextRunFragmentIdentifier();
m_source->gotoNextRun(*m_activeRun);
unsigned int cacheID = m_activeRun->cacheID();
tbb::task* doneTask = m_endRunTasks[cacheID];
doneTask->increment_ref_count();
auto activeRun = m_activeRun;
auto source = m_source;
auto t = [activeRun,source,id,doneTask] () {
source->sumRunInfo(*activeRun,id);
doneTask->decrement_ref_count();
};
if( nullptr == returnValue ) {
returnValue = m_runSumQueues[cacheID].pushAndGetNextTaskToRun(t);
} else {
m_runSumQueues[cacheID].push(t);
}
auto trans = m_source->nextTransition();
keepSumming = ((trans == Source::kRun) and (iNewRunsNumber==m_source->nextRunsNumber()));
}while(keepSumming);
return returnValue;
}
//previous run is now allowed to handle endRun
if(m_activeRun) {
unsigned int cacheID = m_activeRun->cacheID();
assert(cacheID < m_endRunTasks.size());
auto v = m_endRunTasks[cacheID].load()->decrement_ref_count();
writeLock([&]() {
std::cout <<"decrementing "<<cacheID<<" "<<m_endRunTasks[cacheID].load()<<" count "<<v<<std::endl;
});
m_activeRun = nullptr;
}
//Id moves here because after this call we might check to see if a waiting Stream uses this ID
++m_presentRunTransitionID;
Run* newRun = m_runHandler.getARun();
//do we have an available run slot?
if(0!=newRun) {
return startNewRun(iNewRunsNumber,newRun);
}
return nullptr;
}
//NOTE: startNewRun method must only be called from Coordinator's queue
tbb::task*
Coordinator::startNewRun(unsigned int iRunNumber, Run* iRun) {
unsigned int cacheID = iRun->cacheID();
assert(cacheID < m_endRunTasks.size());
assert(m_endRunTasks[cacheID].load()==nullptr);
m_waitingTask->increment_ref_count();
tbb::task* task{new (tbb::task::allocate_root()) GlobalEndRunTask(m_watcher,this,iRun,m_waitingTask)};
/*writeLock([&]() {
std::cout <<" made and incremented "<<task<<" id "<<iCacheID<<std::endl;
});*/
task->increment_ref_count();
m_endRunTasks[cacheID].store(task);
m_activeRun = iRun;
m_presentRunNumber=iRunNumber;
iRun->set(iRunNumber,m_presentRunTransitionID);
m_source->gotoNextRun(*iRun);
m_waitingForBeginRunToFinish[cacheID]->reset();
tbb::task* t{new (tbb::task::allocate_root()) GlobalBeginRunTask(m_watcher,this,iRun)};
return t;
}