PYTHIA  8.316
PythiaParallel.h
1 // PythiaParallel.h is a part of the PYTHIA event generator.
2 // Copyright (C) 2025 Marius Utheim, Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL v2 or later, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 #ifndef PYTHIA_PARALLEL_H
7 #define PYTHIA_PARALLEL_H
8 
9 #include "Pythia8/Pythia.h"
10 #include "Pythia8/PythiaStdlib.h"
11 
12 namespace Pythia8 {
13 
14 //==========================================================================
15 
16 // Class for doing Pythia runs in parallel.
17 
19 
20 public:
21 
22  // Constructor.
23  PythiaParallel(string xmlDir = "../share/Pythia8/xmldoc",
24  bool printBanner = true);
25 
26  // Read in one update for a setting or particle data from a single line.
27  bool readString(string setting, bool warn = true) {
28  return pythiaHelper.readString(setting, warn); }
29 
30  // Read in updates for settings or particle data from user-defined file.
31  bool readFile(string fileName, bool warn = true,
32  int subrun = SUBRUNDEFAULT);
33  bool readFile(string fileName, int subrun) {
34  return readFile(fileName, true, subrun); }
35  bool readFile(istream& is = cin, bool warn = true,
36  int subrun = SUBRUNDEFAULT);
37  bool readFile(istream& is, int subrun) {
38  return readFile(is, true, subrun);}
39 
40  // Initialize all Pythia objects.
41  bool init();
42  bool init(function<bool(Pythia*)> additionalSetup);
43 
44  // Perform the specified action for each Pythia instance.
45  void foreach(function<void(Pythia*)> action);
46 
47  // Perform the specified action for each instance in parallel.
48  void foreachAsync(function<void(Pythia*)> action);
49 
50  // Write final statistics, combining errors from each Pythia instance.
51  // For all PhysicsBase objects, combine that PhysicsBase object
52  // across all threads, if onStat is defined for that specific
53  // PhysicsBase type.
54  void stat(bool combine = true);
55 
56  // Generate events in parallel.
57  vector<long> run(long nEvents, function<void(Pythia*)> callback);
58  vector<long> run(function<void(Pythia*)> callback) {
59  return run(settings.mode("Main:numberOfEvents"), callback); }
60 
61  // Pythia object used for loading data.
63 
64  // Weighted average of the generated cross section for each Pythia instance.
65  double sigmaGen() const { return sigmaGenSave; }
66 
67  // Sum of weights from all Pythia instances.
68  double weightSum() const { return weightSumSave; }
69 
70  // The settings that will be used to initialize Pythia instances.
72 
73  // The particle database that will be used to initialize Pythia instances.
75 
76 private:
77 
78  // Object used for logging.
79  Logger& logger;
80 
81  // Flag if initialized.
82  bool isInit = false;
83 
84  // Sum of weights and weighted cross section.
85  double weightSumSave, sigmaGenSave;
86 
87  // Configuration flags.
88  int numThreads;
89  bool processAsync;
90  bool balanceLoad;
91  bool doNext;
92 
93  // Internal Pythia objects.
94  vector<unique_ptr<Pythia> > pythiaObjects;
95 
96  // Mutex that can be used by each internal Pythia object.
97  mutex mainMutex;
98 
99 };
100 
101 //==========================================================================
102 
103 } // end namespace Pythia8
104 
105 #endif // Pythia8_PythiaParallel_H
bool readString(string line, bool warn=true, int subrun=SUBRUNDEFAULT)
Read in one update for a setting or particle data from a single line.
Definition: Pythia.h:99
Pythia pythiaHelper
Pythia object used for loading data.
Definition: PythiaParallel.h:62
Class for doing Pythia runs in parallel.
Definition: PythiaParallel.h:18
PythiaParallel(string xmlDir="../share/Pythia8/xmldoc", bool printBanner=true)
Constructor.
Definition: PythiaParallel.cc:21
Definition: Logger.h:23
double weightSum() const
Sum of weights from all Pythia instances.
Definition: PythiaParallel.h:68
Settings & settings
The settings that will be used to initialize Pythia instances.
Definition: PythiaParallel.h:71
vector< long > run(long nEvents, function< void(Pythia *)> callback)
Generate events in parallel.
bool init()
Initialize all Pythia objects.
Definition: PythiaParallel.cc:52
void stat(bool combine=true)
Definition: PythiaParallel.cc:278
void foreachAsync(function< void(Pythia *)> action)
Perform the specified action for each instance in parallel.
Definition: PythiaParallel.cc:255
double sigmaGen() const
Weighted average of the generated cross section for each Pythia instance.
Definition: PythiaParallel.h:65
bool readString(string setting, bool warn=true)
Read in one update for a setting or particle data from a single line.
Definition: PythiaParallel.h:27
Header for classes to set beam momentum and interaction vertex spread.
Definition: Analysis.h:20
The Pythia class contains the top-level routines to generate an event.
Definition: Pythia.h:72
bool readFile(string fileName, bool warn=true, int subrun=SUBRUNDEFAULT)
Read in updates for settings or particle data from user-defined file.
Definition: PythiaParallel.cc:30
This class holds a map of all ParticleDataEntries.
Definition: ParticleData.h:423
ParticleData & particleData
The particle database that will be used to initialize Pythia instances.
Definition: PythiaParallel.h:74
Definition: Settings.h:196