Belle II Software development
PathUtils Class Reference

Helper utils for path arithmetic needed in the pEventProcessor. More...

#include <PathUtils.h>

Static Public Member Functions

static bool isWorthParallelising (const Path &path)
 Is a parallel section made up of these modules worth creating processes for?
 
static std::tuple< PathPtr, PathPtr, PathPtr > splitPath (const PathPtr &path)
 Split the given part into the input, main and output path (in this order) by looking onto the parallel certificate of the modules.
 
static ModulePtrList preparePaths (PathPtr &inputPath, PathPtr &mainPath, PathPtr &outputPath)
 Adds internal zmq modules to the paths.
 
static ModulePtr getHistogramManager (PathPtr &inputPath)
 Find the histogram manager in the paths and return it.
 
static ModulePtrList getTerminateGloballyModules (const ModulePtrList &modules)
 Return only modules which have the TerminateGlobally Module flag set.
 
static void prependModulesIfNotPresent (ModulePtrList *modules, const ModulePtrList &prependModules)
 Prepend given 'prependModules' to 'modules', if they're not already present.
 
static void appendModule (PathPtr &path, const ModulePtr &module)
 Append the given module to the path.
 
static void prependModules (PathPtr &path, const ModulePtrList &modules)
 Prepend the given modules to the path, keeping their order.
 

Detailed Description

Helper utils for path arithmetic needed in the pEventProcessor.

Definition at line 19 of file PathUtils.h.

Member Function Documentation

◆ appendModule()

void appendModule ( PathPtr & path,
const ModulePtr & module )
static

Append the given module to the path.

Definition at line 211 of file PathUtils.cc.

212{
213 path->addModule(module);
214}

◆ getHistogramManager()

ModulePtr getHistogramManager ( PathPtr & inputPath)
static

Find the histogram manager in the paths and return it.

Definition at line 98 of file PathUtils.cc.

99{
100 ModulePtr histoManagerModule;
101 for (const ModulePtr& module : inputPath->getModules()) {
102 // Initialize histogram manager if found in the path
103 if (module->hasProperties(Module::c_HistogramManager)) histoManagerModule = module;
104 }
105 return histoManagerModule;
106}
@ c_HistogramManager
This module is used to manage histograms accumulated by other modules.
Definition Module.h:81
std::list< std::shared_ptr< Module > > getModules() const override
Returns a list of the modules in this path.
Definition Path.cc:50
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
Definition Module.h:43

◆ getTerminateGloballyModules()

ModulePtrList getTerminateGloballyModules ( const ModulePtrList & modules)
static

Return only modules which have the TerminateGlobally Module flag set.

Definition at line 192 of file PathUtils.cc.

193{
194 ModulePtrList tmpModuleList;
195 for (const ModulePtr& m : modules) {
196 if (m->hasProperties(Module::c_TerminateInAllProcesses))
197 tmpModuleList.push_back(m);
198 }
199 return tmpModuleList;
200}
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition Module.h:83
std::list< ModulePtr > ModulePtrList
Defines a std::list of shared module pointers.
Definition Module.h:584

◆ isWorthParallelising()

bool isWorthParallelising ( const Path & path)
static

Is a parallel section made up of these modules worth creating processes for?

False if every module in it is flagged Module::c_NoParallelWork, i.e. the section would fork three processes to run nothing but setup code. Such a section is merged back into the input path and the search for a parallel section continues after it.

That is what lets a module which is not parallel processing certified sit between the setup modules and the real work without forcing the whole job into a single process: otherwise the setup modules open a section that the uncertified module closes again on the very next module, and everything after it ends up in the output path.

Definition at line 16 of file PathUtils.cc.

17{
18 for (const ModulePtr& module : path.getModules()) {
19 if (not module->hasProperties(Module::c_NoParallelWork)) return true;
20 }
21 return false;
22}
@ c_NoParallelWork
This module does no per-event work that is worth running in parallel.
Definition Module.h:85

◆ preparePaths()

ModulePtrList preparePaths ( PathPtr & inputPath,
PathPtr & mainPath,
PathPtr & outputPath )
static

Adds internal zmq modules to the paths.

Definition at line 108 of file PathUtils.cc.

109{
110 B2ASSERT("The main part is empty. This is a bug in the framework.",
111 mainPath and not mainPath->isEmpty());
112
113 ModuleManager& moduleManager = ModuleManager::Instance();
114
115 const auto& environment = Environment::Instance();
116
117 const auto& socketAddress = environment.getZMQSocketAddress();
118 const auto inputSocketAddress(ZMQAddressUtils::getSocketAddress(socketAddress, ZMQAddressType::c_input));
119 const auto outputSocketAddress(ZMQAddressUtils::getSocketAddress(socketAddress, ZMQAddressType::c_output));
120 const auto pubSocketAddress(ZMQAddressUtils::getSocketAddress(socketAddress, ZMQAddressType::c_pub));
121 const auto subSocketAddress(ZMQAddressUtils::getSocketAddress(socketAddress, ZMQAddressType::c_sub));
122
123 unsigned int maximalWaitingTime = environment.getZMQMaximalWaitingTime();
124 unsigned int eventBufferSize = environment.getZMQEventBufferSize();
125 unsigned int workerTimeout = environment.getZMQWorkerTimeout();
126 bool useEventBackup = environment.getZMQUseEventBackup();
127
128 if (inputPath) {
129 // Add TXInput after input path
130 ModulePtr zmqTxInputModule = moduleManager.registerModule("ZMQTxInput");
131 zmqTxInputModule->getParam<std::string>("socketName").setValue(inputSocketAddress);
132 zmqTxInputModule->getParam<std::string>("xpubProxySocketName").setValue(pubSocketAddress);
133 zmqTxInputModule->getParam<std::string>("xsubProxySocketName").setValue(subSocketAddress);
134 zmqTxInputModule->getParam<unsigned int>("workerProcessTimeout").setValue(workerTimeout);
135 zmqTxInputModule->getParam<bool>("useEventBackup").setValue(useEventBackup);
136 zmqTxInputModule->getParam<unsigned int>("maximalWaitingTime").setValue(maximalWaitingTime);
137 appendModule(inputPath, zmqTxInputModule);
138
139 // Add RXWorker before main path
140 ModulePtr zmqRxWorkerModule = moduleManager.registerModule("ZMQRxWorker");
141 zmqRxWorkerModule->getParam<std::string>("socketName").setValue(inputSocketAddress);
142 zmqRxWorkerModule->getParam<std::string>("xpubProxySocketName").setValue(pubSocketAddress);
143 zmqRxWorkerModule->getParam<std::string>("xsubProxySocketName").setValue(subSocketAddress);
144 zmqRxWorkerModule->getParam<unsigned int>("maximalWaitingTime").setValue(maximalWaitingTime);
145 zmqRxWorkerModule->getParam<unsigned int>("eventBufferSize").setValue(eventBufferSize);
146 prependModules(mainPath, {zmqRxWorkerModule});
147
148 }
149
150 if (outputPath) {
151 // Add TXWorker after main path
152 ModulePtr zmqTxWorkerModule = moduleManager.registerModule("ZMQTxWorker");
153 zmqTxWorkerModule->getParam<std::string>("socketName").setValue(outputSocketAddress);
154 zmqTxWorkerModule->getParam<std::string>("xpubProxySocketName").setValue(pubSocketAddress);
155 zmqTxWorkerModule->getParam<std::string>("xsubProxySocketName").setValue(pubSocketAddress);
156 appendModule(mainPath, zmqTxWorkerModule);
157
158 // Add RXOutput before output path
159 ModulePtr zmqRxOutputModule = moduleManager.registerModule("ZMQRxOutput");
160 zmqRxOutputModule->getParam<std::string>("socketName").setValue(outputSocketAddress);
161 zmqRxOutputModule->getParam<std::string>("xpubProxySocketName").setValue(pubSocketAddress);
162 zmqRxOutputModule->getParam<std::string>("xsubProxySocketName").setValue(subSocketAddress);
163 zmqRxOutputModule->getParam<unsigned int>("maximalWaitingTime").setValue(maximalWaitingTime);
164 prependModules(outputPath, {zmqRxOutputModule});
165
166 }
167
168 if (inputPath) {
169 B2INFO("Input Path " << inputPath->getPathString());
170 }
171 if (mainPath) {
172 B2INFO("Main Path " << mainPath->getPathString());
173 }
174 if (outputPath) {
175 B2INFO("Output Path " << outputPath->getPathString());
176 }
177
178 Path mergedPath;
179 if (inputPath) {
180 mergedPath.addPath(inputPath);
181 }
182 mergedPath.addPath(mainPath);
183 if (outputPath) {
184 mergedPath.addPath(outputPath);
185 }
186
187 B2INFO("ModuleList " << mergedPath.getPathString());
188
189 return mergedPath.buildModulePathList();
190}
static Environment & Instance()
Static method to get a reference to the Environment instance.
std::shared_ptr< Module > registerModule(const std::string &moduleName, std::string sharedLibPath="") noexcept(false)
Creates an instance of a module and registers it to the ModuleManager.
static ModuleManager & Instance()
Exception is thrown if the requested module could not be created by the ModuleManager.
static void prependModules(PathPtr &path, const ModulePtrList &modules)
Prepend the given modules to the path, keeping their order.
Definition PathUtils.cc:216
static void appendModule(PathPtr &path, const ModulePtr &module)
Append the given module to the path.
Definition PathUtils.cc:211
std::list< std::shared_ptr< Module > > buildModulePathList(bool unique=true) const
Builds a list of all modules which could be executed during the data processing.
Definition Path.cc:67
void addPath(const PathPtr &path)
See 'pydoc3 basf2.Path'.
Definition Path.cc:37
std::string getPathString() const override
return a string of the form [module a -> module b -> [another path]]
Definition Path.cc:206
static std::string getSocketAddress(const std::string &socketAddress, ZMQAddressType socketPart)
Create a full socket address for the given type from a random socket address, ba adding a suffix.
ModuleParam< T > & getParam(const std::string &name) const
Returns a reference to a parameter.
Definition Module.h:573
@ c_sub
Multicast publish socket.
@ c_pub
Output socket.
@ c_output
Input socket.

◆ prependModules()

void prependModules ( PathPtr & path,
const ModulePtrList & modules )
static

Prepend the given modules to the path, keeping their order.

Definition at line 216 of file PathUtils.cc.

217{
218 // nothing to do, and wrapping an empty path would stop it from counting as empty
219 if (modules.empty()) return;
220
221 PathPtr newPath(new Path());
222 for (const ModulePtr& module : modules) newPath->addModule(module);
223 newPath->addPath(path);
224 path.swap(newPath);
225}
std::shared_ptr< Path > PathPtr
Defines a pointer to a path object as a boost shared pointer.
Definition Path.h:35

◆ prependModulesIfNotPresent()

void prependModulesIfNotPresent ( ModulePtrList * modules,
const ModulePtrList & prependModules )
static

Prepend given 'prependModules' to 'modules', if they're not already present.

Definition at line 202 of file PathUtils.cc.

203{
204 for (const ModulePtr& m : prependModules) {
205 if (std::find(modules->begin(), modules->end(), m) == modules->end()) { //not present
206 modules->push_front(m);
207 }
208 }
209}

◆ splitPath()

std::tuple< PathPtr, PathPtr, PathPtr > splitPath ( const PathPtr & path)
static

Split the given part into the input, main and output path (in this order) by looking onto the parallel certificate of the modules.

Definition at line 24 of file PathUtils.cc.

25{
26 PathPtr inputPath(new Path);
27 PathPtr mainPath(new Path);
28 PathPtr outputPath(new Path);
29
30 // only collected here, and added below once the splitting is done: the merge-back
31 // discards and restarts the main path, taking anything put on it along
32 ModulePtrList histogramManagers;
33
34 int stage = 0; //0: in, 1: event/main, 2: out
35 for (const ModulePtr& module : path->getModules()) {
36 bool hasParallelFlag = module->hasProperties(Module::c_ParallelProcessingCertified) and
37 !module->hasProperties(Module::c_Input) and
38 !module->hasProperties(Module::c_Output) and
39 !module->hasProperties(Module::c_HistogramManager) ;
40 // entire conditional path must also be compatible
41 if (hasParallelFlag and module->hasCondition()) {
42 for (const auto& conditionPath : module->getAllConditionPaths()) {
44 hasParallelFlag = false;
45 }
46 }
47 }
48 // if modules have parallal flag -> stage = 1 , event/main
49 if ((stage == 0 and hasParallelFlag) or (stage == 1 and !hasParallelFlag)) {
50 stage++;
51
52 if (stage == 2) {
53 if (not isWorthParallelising(*mainPath)) {
54 // merge mainPath back into input path
55 inputPath->addPath(mainPath);
56 mainPath.reset(new Path);
57 // and search for further parallel sections
58 stage = 0;
59 }
60 }
61 }
62
63 if (stage == 0) {
64 inputPath->addModule(module);
65 if (module->hasProperties(Module::c_HistogramManager)) histogramManagers.push_back(module);
66 } else if (stage == 1) {
67 mainPath->addModule(module);
68 } else if (stage == 2) {
69 outputPath->addModule(module);
70 }
71 }
72
73 // the histogram manager has to run in every process
74 prependModules(mainPath, histogramManagers);
75 prependModules(outputPath, histogramManagers);
76
77 bool createAllPaths = false; // usually we might not need e.g. an output path
78 for (const ModulePtr& module : path->getModules()) {
79 if (module->hasProperties(Module::c_TerminateInAllProcesses)) {
80 createAllPaths = true; // ensure there are all kinds of processes
81 }
82 }
83
84 // if main path is empty, createAllPaths doesn't really matter, since we'll fall back to single-core processing
85 if (mainPath->isEmpty() and not createAllPaths) {
86 mainPath.reset();
87 }
88 if (inputPath->isEmpty() and not createAllPaths) {
89 inputPath.reset();
90 }
91 if (outputPath->isEmpty() and not createAllPaths) {
92 outputPath.reset();
93 }
94
95 return {inputPath, mainPath, outputPath};
96}
static bool allModulesHaveFlag(const std::list< std::shared_ptr< Module > > &list, unsigned int flag)
Returns true if and only if all modules in list have the given flag (or list is empty).
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
static bool isWorthParallelising(const Path &path)
Is a parallel section made up of these modules worth creating processes for?
Definition PathUtils.cc:16

The documentation for this class was generated from the following files: