Belle II Software development
PathUtils.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include <framework/pcore/PathUtils.h>
9#include <framework/pcore/zmq/utils/ZMQAddressUtils.h>
10#include <framework/core/ModuleManager.h>
11#include <framework/core/Environment.h>
12#include <set>
13
14using namespace Belle2;
15
17{
18 for (const ModulePtr& module : path.getModules()) {
19 if (not module->hasProperties(Module::c_NoParallelWork)) return true;
20 }
21 return false;
22}
23
24std::tuple<PathPtr, PathPtr, PathPtr> PathUtils::splitPath(const PathPtr& path)
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}
97
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}
107
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}
191
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}
201
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}
210
211void PathUtils::appendModule(PathPtr& path, const ModulePtr& module)
212{
213 path->addModule(module);
214}
215
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}
static Environment & Instance()
Static method to get a reference to the Environment instance.
The ModuleManager Class.
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 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_HistogramManager
This module is used to manage histograms accumulated by other modules.
Definition Module.h:81
@ c_NoParallelWork
This module does no per-event work that is worth running in parallel.
Definition Module.h:85
@ 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
@ c_TerminateInAllProcesses
When using parallel processing, call this module's terminate() function in all processes().
Definition Module.h:83
static ModulePtrList getTerminateGloballyModules(const ModulePtrList &modules)
Return only modules which have the TerminateGlobally Module flag set.
Definition PathUtils.cc:192
static ModulePtrList preparePaths(PathPtr &inputPath, PathPtr &mainPath, PathPtr &outputPath)
Adds internal zmq modules to the paths.
Definition PathUtils.cc:108
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 paralle...
Definition PathUtils.cc:24
static void prependModules(PathPtr &path, const ModulePtrList &modules)
Prepend the given modules to the path, keeping their order.
Definition PathUtils.cc:216
static ModulePtr getHistogramManager(PathPtr &inputPath)
Find the histogram manager in the paths and return it.
Definition PathUtils.cc:98
static void prependModulesIfNotPresent(ModulePtrList *modules, const ModulePtrList &prependModules)
Prepend given 'prependModules' to 'modules', if they're not already present.
Definition PathUtils.cc:202
static bool isWorthParallelising(const Path &path)
Is a parallel section made up of these modules worth creating processes for?
Definition PathUtils.cc:16
static void appendModule(PathPtr &path, const ModulePtr &module)
Append the given module to the path.
Definition PathUtils.cc:211
Implements a path consisting of Module and/or Path objects.
Definition Path.h:38
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
void addModule(const std::shared_ptr< Module > &module)
Adds a module to the path.
Definition Path.cc:32
bool isEmpty() const
Returns true if this Path doesn't contain any elements.
Definition Path.cc:45
std::list< std::shared_ptr< Module > > getModules() const override
Returns a list of the modules in this path.
Definition Path.cc:50
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
std::shared_ptr< Path > PathPtr
Defines a pointer to a path object as a boost shared pointer.
Definition Path.h:35
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
Definition Module.h:43
std::list< ModulePtr > ModulePtrList
Defines a std::list of shared module pointers.
Definition Module.h:584
@ c_sub
Multicast publish socket.
@ c_pub
Output socket.
@ c_output
Input socket.
Abstract base class for different kinds of events.