Skip to content

Commit 360bc73

Browse files
committed
Add Init protection + stop() subgens + protection against nextEvent
1 parent 000230b commit 360bc73

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

‎Generators/include/Generators/GeneratorHybrid.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class GeneratorHybrid : public Generator
6464
Bool_t generateEvent() override;
6565
Bool_t importParticles() override;
6666
void updateHeader(o2::dataformats::MCEventHeader* eventHeader) override;
67-
void stop() override; // end-of-stream hook that closes the TBB task arena
67+
void stop() override; // end-of-stream hook that joins the TBB task pool
6868

6969
Bool_t parseJSON(const std::string& path);
7070
Bool_t confSetter(const auto& gen);

‎Generators/src/GeneratorHybrid.cxx‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,20 @@ void GeneratorHybrid::stop()
210210
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - t0).count();
211211
LOG(info) << "HybridGen: stop() joined TBB task pool after " << ms << " ms";
212212
}
213+
// Forward the hook to the sub-generators
214+
for (auto& gen : gens) {
215+
if (gen) {
216+
gen->stop();
217+
}
218+
}
213219
}
214220

215221
Bool_t GeneratorHybrid::Init()
216222
{
223+
// protection against additional Init() which would assign over a joinable mTBBTaskPoolRunner, which calls std::terminate
224+
if (mIsInitialized) {
225+
return true;
226+
}
217227
// init all sub-gens
218228
int count = 0;
219229
for (auto& gen : mGens) {
@@ -474,6 +484,9 @@ bool GeneratorHybrid::importParticles()
474484
// this is important when using event pools where the full header information is forwarded from the generator
475485
// otherwise some events might have mixed header information from different generators
476486
mMCEventHeader.Reset();
487+
// Once the event budget is exhausted there is no further event to prepare, and re-queueing a
488+
// sub-generator would start an event nobody consumes and that stop() then has to wait for
489+
const bool queueNextEvent = getTotalNEvents() == 0 || mEventCounter + 1 < getTotalNEvents();
477490
if (mCocktailMode) {
478491
// in cocktail mode we need to merge the particles from the different generators
479492
for (auto subIndex : subGenIndex) {
@@ -504,8 +517,10 @@ bool GeneratorHybrid::importParticles()
504517
gens[subIndex]->updateHeader(&mMCEventHeader);
505518
mHeaderGeneratorIndex = subIndex; // store index of generator updating the header
506519
}
507-
mInputTaskQueue.push(subIndex);
508-
mTasksStarted++;
520+
if (queueNextEvent) {
521+
mInputTaskQueue.push(subIndex);
522+
mTasksStarted++;
523+
}
509524
}
510525
} else {
511526
if (!mIsHyperloop) {
@@ -528,8 +543,10 @@ bool GeneratorHybrid::importParticles()
528543
// fetch the event Header information from the underlying generator
529544
gens[genIndex]->updateHeader(&mMCEventHeader);
530545
mHeaderGeneratorIndex = genIndex; // store index of generator updating the header
531-
mInputTaskQueue.push(genIndex);
532-
mTasksStarted++;
546+
if (queueNextEvent) {
547+
mInputTaskQueue.push(genIndex);
548+
mTasksStarted++;
549+
}
533550
}
534551

535552
mseqCounter++;

0 commit comments

Comments
 (0)