From 952782c8f13b43eb861cd792c089b013c714c639 Mon Sep 17 00:00:00 2001 From: Marco Giacalone Date: Fri, 20 Nov 2026 22:12:02 +0100 Subject: [PATCH] Set hybrid expansion to num_workers in HY The expansion to the hybrid configuration when running external generators on HY is updated so that the cloning happens only up to the set number of workers. The previous version proved to be successful, however it caused an increased configuration time per MCGen job, which is fixed here (it will be expanded by a multi-threaded initialisation of the generators in hybrid mode in the near future). --- Generators/include/Generators/Generator.h | 5 --- Generators/src/GeneratorFactory.cxx | 55 ++++++++++++++++------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Generators/include/Generators/Generator.h b/Generators/include/Generators/Generator.h index 500a40d3579f7..ac98b6f7b33a5 100644 --- a/Generators/include/Generators/Generator.h +++ b/Generators/include/Generators/Generator.h @@ -104,11 +104,6 @@ class Generator : public FairGenerator // Check if simulation is running in Hyperloop mode static bool isHyperloop(); - // Number of parallel sub-generator clones a Hyperloop-aware generator expands into - // (currently used by the external-generator-to-hybrid expansion in GeneratorFactory). - // 8 matches the standard core counts of Grid/Hyperloop nodes - static constexpr int NHyperloopParallelGenerators = 8; - /** other **/ void clearParticles() { mParticles.clear(); }; diff --git a/Generators/src/GeneratorFactory.cxx b/Generators/src/GeneratorFactory.cxx index a8a10e96494eb..4e6789726cc78 100644 --- a/Generators/src/GeneratorFactory.cxx +++ b/Generators/src/GeneratorFactory.cxx @@ -40,6 +40,7 @@ #include #include #include +#include #endif #include #include @@ -56,17 +57,33 @@ namespace eventgen { #if defined(GENERATORS_WITH_PYTHIA8) && defined(GENERATORS_WITH_HEPMC3) -// Builds a parallel GeneratorHybrid JSON configuration out of -// 8 (Generator::NHyperloopParallelGenerators) identical "external" sub-generator entries, -// using the configured GeneratorExternalParam. This makes the simulation parallelisation automatic -// on Hyperloop. +// Maximum number of parallel sub-generators on Hyperloop. Fixed rather than taken from the host +// core count, which does not reflect the CPU slot assigned to the job: 8 is the largest slot +// available on GRID/Hyperloop nodes. +constexpr int MaxHyperloopParallelGenerators = 8; + +// Number of identical sub-generators the external generator is expanded into on Hyperloop: +// one sub-generator per GeneratorHybrid.num_workers (1 by default), capped to +// MaxHyperloopParallelGenerators. +int getHyperloopNParallelGenerators() +{ + const int nWorkers = GeneratorHybridParam::Instance().num_workers; + if (nWorkers > MaxHyperloopParallelGenerators) { + LOG(warning) << "GeneratorHybrid.num_workers = " << nWorkers << " exceeds the maximum of " + << MaxHyperloopParallelGenerators << " parallel sub-generators: capping to " << MaxHyperloopParallelGenerators; + } + return std::clamp(nWorkers, 1, MaxHyperloopParallelGenerators); +} + +// Builds a parallel GeneratorHybrid JSON configuration out of nGenerators +// identical "external" sub-generator entries, using the configured GeneratorExternalParam. +// This makes the simulation parallelisation automatic on Hyperloop. // To-do: Define the behaviour with ini configuration which are already definining a hybrid gen // -// Note on GeneratorHybrid.num_workers: expanding to 8 sub-generators does not by itself raise -// the number of TBB workers that actually run them in parallel. This is controlled by -// the parameter GeneratorHybrid.num_workers. On Hyperloop this will be part -// of the standard train configuration -std::string buildHyperloopExternalHybridConfig(GeneratorExternalParam const& extparams) +// Note on GeneratorHybrid.num_workers: it controls both the number of TBB workers running the +// sub-generators in parallel and nGenerators (see getHyperloopNParallelGenerators). +// On Hyperloop it is part of the standard train configuration for MCGen datasets +std::string buildHyperloopExternalHybridConfig(GeneratorExternalParam const& extparams, int nGenerators) { rapidjson::Document doc; doc.SetObject(); @@ -75,7 +92,7 @@ std::string buildHyperloopExternalHybridConfig(GeneratorExternalParam const& ext rapidjson::Value generators(rapidjson::kArrayType); rapidjson::Value fractions(rapidjson::kArrayType); - for (int i = 0; i < Generator::NHyperloopParallelGenerators; ++i) { + for (int i = 0; i < nGenerators; ++i) { rapidjson::Value config(rapidjson::kObjectType); config.AddMember("fileName", rapidjson::Value(extparams.fileName.c_str(), alloc), alloc); config.AddMember("funcName", rapidjson::Value(extparams.funcName.c_str(), alloc), alloc); @@ -157,12 +174,18 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair genconfig = "hybrid"; } else if (Generator::isHyperloop()) { // Running under Hyperloop: transparently expand the single external generator - // configuration into a parallel hybrid of Generator::NHyperloopParallelGenerators - // clones, to increase on-the-fly MC-generation throughput. - LOG(info) << "IS_HYPERLOOP detected: expanding external generator into " - << Generator::NHyperloopParallelGenerators << " parallel hybrid sub-generators"; - hyperloopExtHybridConfigFile = buildHyperloopExternalHybridConfig(GeneratorExternalParam::Instance()); - genconfig = "hybrid"; + // configuration into a parallel hybrid of clones (one per worker, capped to + // MaxHyperloopParallelGenerators), to increase on-the-fly MC-generation throughput. + // A single clone brings no parallelism, so the plain external generator is kept. + const int nGenerators = getHyperloopNParallelGenerators(); + if (nGenerators > 1) { + LOG(info) << "IS_HYPERLOOP detected: expanding external generator into " + << nGenerators << " parallel hybrid sub-generators"; + hyperloopExtHybridConfigFile = buildHyperloopExternalHybridConfig(GeneratorExternalParam::Instance(), nGenerators); + genconfig = "hybrid"; + } else { + LOG(info) << "IS_HYPERLOOP detected: single worker requested, keeping the external generator as is"; + } } } #endif