4040#include < rapidjson/ostreamwrapper.h>
4141#include < fstream>
4242#include < unistd.h>
43+ #include < algorithm>
4344#endif
4445#include < Generators/PrimaryGenerator.h>
4546#include < Generators/BoxGunParam.h>
@@ -56,17 +57,33 @@ namespace eventgen
5657{
5758
5859#if defined(GENERATORS_WITH_PYTHIA8) && defined(GENERATORS_WITH_HEPMC3)
59- // Builds a parallel GeneratorHybrid JSON configuration out of
60- // 8 (Generator::NHyperloopParallelGenerators) identical "external" sub-generator entries,
61- // using the configured GeneratorExternalParam. This makes the simulation parallelisation automatic
62- // on Hyperloop.
60+ // Maximum number of parallel sub-generators on Hyperloop. Fixed rather than taken from the host
61+ // core count, which does not reflect the CPU slot assigned to the job: 8 is the largest slot
62+ // available on GRID/Hyperloop nodes.
63+ constexpr int MaxHyperloopParallelGenerators = 8 ;
64+
65+ // Number of identical sub-generators the external generator is expanded into on Hyperloop:
66+ // one sub-generator per GeneratorHybrid.num_workers (1 by default), capped to
67+ // MaxHyperloopParallelGenerators.
68+ int getHyperloopNParallelGenerators ()
69+ {
70+ const int nWorkers = GeneratorHybridParam::Instance ().num_workers ;
71+ if (nWorkers > MaxHyperloopParallelGenerators) {
72+ LOG (warning) << " GeneratorHybrid.num_workers = " << nWorkers << " exceeds the maximum of "
73+ << MaxHyperloopParallelGenerators << " parallel sub-generators: capping to " << MaxHyperloopParallelGenerators;
74+ }
75+ return std::clamp (nWorkers, 1 , MaxHyperloopParallelGenerators);
76+ }
77+
78+ // Builds a parallel GeneratorHybrid JSON configuration out of nGenerators
79+ // identical "external" sub-generator entries, using the configured GeneratorExternalParam.
80+ // This makes the simulation parallelisation automatic on Hyperloop.
6381// To-do: Define the behaviour with ini configuration which are already definining a hybrid gen
6482//
65- // Note on GeneratorHybrid.num_workers: expanding to 8 sub-generators does not by itself raise
66- // the number of TBB workers that actually run them in parallel. This is controlled by
67- // the parameter GeneratorHybrid.num_workers. On Hyperloop this will be part
68- // of the standard train configuration
69- std::string buildHyperloopExternalHybridConfig (GeneratorExternalParam const & extparams)
83+ // Note on GeneratorHybrid.num_workers: it controls both the number of TBB workers running the
84+ // sub-generators in parallel and nGenerators (see getHyperloopNParallelGenerators).
85+ // On Hyperloop it is part of the standard train configuration for MCGen datasets
86+ std::string buildHyperloopExternalHybridConfig (GeneratorExternalParam const & extparams, int nGenerators)
7087{
7188 rapidjson::Document doc;
7289 doc.SetObject ();
@@ -75,7 +92,7 @@ std::string buildHyperloopExternalHybridConfig(GeneratorExternalParam const& ext
7592
7693 rapidjson::Value generators (rapidjson::kArrayType );
7794 rapidjson::Value fractions (rapidjson::kArrayType );
78- for (int i = 0 ; i < Generator::NHyperloopParallelGenerators ; ++i) {
95+ for (int i = 0 ; i < nGenerators ; ++i) {
7996 rapidjson::Value config (rapidjson::kObjectType );
8097 config.AddMember (" fileName" , rapidjson::Value (extparams.fileName .c_str (), alloc), alloc);
8198 config.AddMember (" funcName" , rapidjson::Value (extparams.funcName .c_str (), alloc), alloc);
@@ -157,12 +174,18 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
157174 genconfig = " hybrid" ;
158175 } else if (Generator::isHyperloop ()) {
159176 // Running under Hyperloop: transparently expand the single external generator
160- // configuration into a parallel hybrid of Generator::NHyperloopParallelGenerators
161- // clones, to increase on-the-fly MC-generation throughput.
162- LOG (info) << " IS_HYPERLOOP detected: expanding external generator into "
163- << Generator::NHyperloopParallelGenerators << " parallel hybrid sub-generators" ;
164- hyperloopExtHybridConfigFile = buildHyperloopExternalHybridConfig (GeneratorExternalParam::Instance ());
165- genconfig = " hybrid" ;
177+ // configuration into a parallel hybrid of clones (one per worker, capped to
178+ // MaxHyperloopParallelGenerators), to increase on-the-fly MC-generation throughput.
179+ // A single clone brings no parallelism, so the plain external generator is kept.
180+ const int nGenerators = getHyperloopNParallelGenerators ();
181+ if (nGenerators > 1 ) {
182+ LOG (info) << " IS_HYPERLOOP detected: expanding external generator into "
183+ << nGenerators << " parallel hybrid sub-generators" ;
184+ hyperloopExtHybridConfigFile = buildHyperloopExternalHybridConfig (GeneratorExternalParam::Instance (), nGenerators);
185+ genconfig = " hybrid" ;
186+ } else {
187+ LOG (info) << " IS_HYPERLOOP detected: single worker requested, keeping the external generator as is" ;
188+ }
166189 }
167190 }
168191#endif
0 commit comments