| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

GaudiParallelizer.cpp
Go to the documentation of this file.
1 // Include files
2 // From Gaudi
3 #include "GaudiKernel/AlgFactory.h"
4 #include "GaudiKernel/IAlgManager.h"
5 #include "GaudiKernel/IJobOptionsSvc.h"
6 #include "GaudiKernel/SerializeSTL.h"
7 #include "GaudiAlg/ISequencerTimerTool.h"
8
9 #include "GaudiParallelizer.h"
10
11 // ----------------------------------------------------------------------------
12 // Implementation file for class: GaudiParallelizer
13 //
14 // 09/12/2011: Illya Shapoval
15 // ----------------------------------------------------------------------------
16 DECLARE_ALGORITHM_FACTORY(GaudiParallelizer)
17
18 // ============================================================================
19 // Standard constructor, initializes variables
20 // ============================================================================
21 GaudiParallelizer::GaudiParallelizer(const std::string& name, ISvcLocator* pSvcLocator)
22 : GaudiAlgorithm(name, pSvcLocator)
23 , m_timerTool( 0 )
24 {
25 declareProperty( "Members" , m_names );
26 declareProperty( "ModeOR" , m_modeOR = false );
27 declareProperty( "MeasureTime" , m_measureTime = false );
28 declareProperty( "ReturnOK" , m_returnOK = false );
29 declareProperty( "NumberOfThreads", m_nthreads = 0 );
30
31 m_names.declareUpdateHandler (&GaudiParallelizer::membershipHandler, this );
32 }
33
34 // ============================================================================
35 // Destructor
36 // ============================================================================
37 GaudiParallelizer::~GaudiParallelizer() {}
38
39 // ============================================================================
40 // Initialization
41 // ============================================================================
42 StatusCode GaudiParallelizer::initialize() {
43 if ( msgLevel(MSG::DEBUG) ) debug() << "==> Initialize" << endmsg;
44 StatusCode sc = GaudiAlgorithm::initialize(); // must be executed first
45 if ( sc.isFailure() ) return sc; // error printed already by GaudiAlgorithm
46
47 StatusCode status = decodeNames();
48 if ( !status.isSuccess() ) return status;
49
50 m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" );
51 if ( m_timerTool->globalTiming() ) m_measureTime = true;
52
53 if ( m_measureTime ) {
54 m_timer = m_timerTool->addTimer( name() );
55 m_timerTool->increaseIndent();
56 } else {
57 release( m_timerTool );
58 m_timerTool = 0;
59 }
60
61 //== Initialize the algorithms
62 std::vector<AlgorithmEntry>::iterator itE;
63 for ( itE = m_entries.begin(); m_entries.end() != itE; itE++ ) {
64 Algorithm* myAlg = itE->algorithm();
65 if ( m_measureTime ) {
66 itE->setTimer( m_timerTool->addTimer( myAlg->name() ) );
67 }
68
69 status = myAlg->sysInitialize();
70 if ( !status.isSuccess() ) {
71 return Error( "Can not initialize " + myAlg->name(),
72 status );
73 }
74 }
75
76 if ( m_measureTime ) m_timerTool->decreaseIndent();
77
78 if ( m_nthreads != 0 ) {
79 // Construct the TBB task scheduler with m_nthreads threads
80 tbb::task_scheduler_init init( m_nthreads );
81 }
82 else {
83 m_nthreads = tbb::task_scheduler_init::default_num_threads();
84 }
85
86 if ( msgLevel(MSG::DEBUG) ) debug() << "Number of threads set to be used in the TBB thread pool is "
87 << m_nthreads << endmsg;
88 return StatusCode::SUCCESS;
89 }
90
91 // ============================================================================
92 // Main execution
93 // ============================================================================
94 StatusCode GaudiParallelizer::execute() {
95 if ( m_measureTime ) m_timerTool->start( m_timer );
96
97 if ( msgLevel(MSG::DEBUG) ) debug() << "==> Execute algorithms in parallel" << endmsg;
98
99 for (std::vector<AlgorithmEntry>::iterator itE = m_entries.begin(); m_entries.end() != itE; ++itE ) {
100 Algorithm* myAlg = itE->algorithm();
101 if ( ! myAlg->isEnabled() ) continue;
102 if ( ! myAlg->isExecuted() ) {
103
104 m_task_group.run(boost::bind(&AlgorithmEntry::run,
105 boost::ref(*itE),
106 boost::ref(*this)));
107 }
108 }
109
110 m_task_group.wait();
111 if ( msgLevel(MSG::DEBUG) ) debug() << "==> Joining parallel algorithm tasks" << endmsg;
112
113 for( std::vector<AlgorithmEntry>::const_iterator it = m_entries.begin(); it != m_entries.end(); ++it ){
114 if ( msgLevel(MSG::DEBUG) ) debug() << "Algorithm wrapper " << &*it
115 << " around the algorithm " << it->algorithm()->name()
116 << " received return status code " << it->m_returncode
117 << endmsg;
118 }
119
120 for( std::vector<AlgorithmEntry>::const_iterator it = m_entries.begin(); it != m_entries.end(); ++it )
121 if ( !(it->m_returncode.isSuccess()) ) return it->m_returncode;
122
123 setExecuted( true );
124 if ( m_measureTime ) m_timerTool->stop( m_timer );
125 return StatusCode::SUCCESS;
126 }
127
128 // ============================================================================
129 // Finalize
130 // ============================================================================
131 StatusCode GaudiParallelizer::finalize() {
132 if ( msgLevel(MSG::DEBUG) ) debug() << "==> Finalize" << endmsg;
133
134 return GaudiAlgorithm::finalize(); // must be called after all other actions
135 }
136
137 // ============================================================================
138
139 StatusCode GaudiParallelizer::decodeNames(){
140
141 StatusCode final = StatusCode::SUCCESS;
142 m_entries.clear();
143
144 //== Get the "Context" option if in the file...
145 auto jos = service<IJobOptionsSvc>( "JobOptionsSvc" );
146 bool addedContext = false; //= Have we added the context ?
147 bool addedRootInTES = false; //= Have we added the rootInTES ?
148
149
150 //= Get the Application manager, to see if algorithm exist
151 auto appMgr = service<IAlgManager>("ApplicationMgr");
152 const std::vector<std::string>& nameVector = m_names.value();
153 std::vector<std::string>::const_iterator it;
154 for ( it = nameVector.begin(); nameVector.end() != it; it++ ) {
155 const Gaudi::Utils::TypeNameString typeName(*it);
156 const std::string &theName = typeName.name();
157 const std::string &theType = typeName.type();
158
159 //== Check whether the specified algorithm already exists. If not, create it
160 StatusCode result = StatusCode::SUCCESS;
161 SmartIF<IAlgorithm> myIAlg = appMgr->algorithm(typeName, false); // do not create it now
162 if ( !myIAlg.isValid() ) {
163 //== Set the Context if not in the jobOptions list
164 if ( !context().empty() || !rootInTES().empty() ) {
165 bool foundContext = false;
166 bool foundRootInTES = false;
167 const auto properties = jos->getProperties( theName );
168 if ( properties ) {
169 // Iterate over the list to set the options
170 for ( const auto& p : *properties ) {
171 if ( "Context" == p->name() ) {
172 foundContext = true;
173 }
174 if ( "RootInTES" == p->name() ) {
175 foundRootInTES = true;
176 }
177 }
178 }
179 if ( !foundContext && !context().empty() ) {
180 StringProperty contextProperty( "Context", context() );
181 jos->addPropertyToCatalogue( theName, contextProperty ).ignore();
182 addedContext = true;
183 }
184 if ( !foundRootInTES && !rootInTES().empty() ) {
185 StringProperty rootInTESProperty( "RootInTES", rootInTES() );
186 jos->addPropertyToCatalogue( theName, rootInTESProperty ).ignore();
187 addedRootInTES = true;
188 }
189 }
190
191 Algorithm *myAlg = nullptr;
192 result = createSubAlgorithm( theType, theName, myAlg );
193 // (MCl) this should prevent bug #35199... even if I didn't manage to
194 // reproduce it with a simple test.
195 if (result.isSuccess()) myIAlg = myAlg;
196 } else {
197 Algorithm *myAlg = dynamic_cast<Algorithm*>(myIAlg.get());
198 if (myAlg) {
199 subAlgorithms()->push_back(myAlg);
200 // when the algorithm is not created, the ref count is short by one, so we have to fix it.
201 myAlg->addRef();
202 }
203 }
204
205 //== Remove the property, in case this is not a GaudiAlgorithm...
206 if ( addedContext ) {
207 jos->removePropertyFromCatalogue( theName, "Context" ).ignore();
208 addedContext = false;
209 }
210 if ( addedRootInTES ) {
211 jos->removePropertyFromCatalogue( theName, "RootInTES" ).ignore();
212 addedRootInTES = false;
213 }
214
215 // propagate the sub-algorithm into own state.
216 if ( result.isSuccess () &&
217 Gaudi::StateMachine::INITIALIZED <= FSMState() &&
218 myIAlg.isValid () &&
219 Gaudi::StateMachine::INITIALIZED > myIAlg->FSMState() )
220 {
221 StatusCode sc = myIAlg->sysInitialize() ;
222 if ( sc.isFailure() ) { result = sc ; }
223 }
224
225 // propagate the sub-algorithm into own state.
226 if ( result.isSuccess () &&
227 Gaudi::StateMachine::RUNNING <= FSMState() &&
228 myIAlg.isValid () &&
229 Gaudi::StateMachine::RUNNING > myIAlg->FSMState() )
230 {
231 StatusCode sc = myIAlg->sysStart () ;
232 if ( sc.isFailure() ) { result = sc ; }
233 }
234
235 //== Is it an Algorithm ? Strange test...
236 if ( result.isSuccess() ) {
237 // TODO: (MCl) it is possible to avoid the dynamic_cast in most of the
238 // cases by keeping the result of createSubAlgorithm.
239 Algorithm* myAlg = dynamic_cast<Algorithm*>(myIAlg.get());
240 if (myAlg!=0) {
241 // Note: The reference counting is kept by the system of sub-algorithms
242 m_entries.push_back( AlgorithmEntry( myAlg ) );
243 if (msgLevel(MSG::DEBUG)) debug () << "Added algorithm " << theName << endmsg;
244 } else {
245 warning() << theName << " is not an Algorithm - failed dynamic_cast"
246 << endmsg;
247 final = StatusCode::FAILURE;
248 }
249 } else {
250 warning() << "Unable to find or create " << theName << endmsg;
251 final = result;
252 }
253
254 }
255 //== Print the list of algorithms
256 MsgStream& msg = info();
257 if ( m_modeOR ) msg << "OR ";
258 msg << "Member list: ";
259 using GaudiUtils::details::ostream_joiner;
260 ostream_joiner( msg, m_entries, ", ",
261 [](MsgStream& msg,const AlgorithmEntry& entry) -> MsgStream& {
262 Algorithm* myAlg = entry.algorithm();
263 auto myAlgType = System::typeinfoName( typeid(*myAlg) ) ;
264 if ( myAlg->name() != myAlgType ) {
265 msg << myAlgType << "/" ;
266 }
267 return msg << myAlg->name();
268 } );
269 if ( !context().empty() ) msg << ", with context '" << context() << "'";
270 if ( !rootInTES().empty() ) msg << ", with rootInTES '" << rootInTES() << "'";
271 msg << endmsg;
272 return final;
273
274 }
275
276 //=========================================================================
277 // Interface for the Property manager
278 //=========================================================================
279 void GaudiParallelizer::membershipHandler ( Property& /* p */ )
280 {
281 // no action for not-yet initialized sequencer
282 if ( Gaudi::StateMachine::INITIALIZED > FSMState() ) { return ; } // RETURN
283
284 decodeNames().ignore();
285
286 if ( !m_measureTime ) { return ; } // RETURN
287
288 // add the entries into timer table:
289
290 if ( 0 == m_timerTool )
291 { m_timerTool = tool<ISequencerTimerTool>( "SequencerTimerTool" ) ; }
292
293 if ( m_timerTool->globalTiming() ) m_measureTime = true;
294
295 m_timer = m_timerTool->addTimer( name() );
296 m_timerTool->increaseIndent();
297
298 for ( std::vector<AlgorithmEntry>::iterator itE = m_entries.begin() ;
299 m_entries.end() != itE; ++itE )
300 {
301 itE->setTimer( m_timerTool->addTimer( itE->algorithm()->name() ) );
302 }
303
304 m_timerTool->decreaseIndent();
305
306 }
307 //=============================================================================
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Gaudi::StateMachine::INITIALIZED
Definition: StateMachine.h:15
SmartIF< IAlgorithm >
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:133
std::string::empty
T empty(T...args)
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
ISequencerTimerTool::decreaseIndent
virtual void decreaseIndent()=0
Decrease the indentation of the name.
Algorithm::createSubAlgorithm
StatusCode createSubAlgorithm(const std::string &type, const std::string &name, Algorithm *&pSubAlg)
Create a sub algorithm.
Definition: Algorithm.cpp:955
GaudiParallelizer::AlgorithmEntry::algorithm
Algorithm * algorithm() const
Definition: GaudiParallelizer.h:46
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
IAlgorithm::sysStart
virtual StatusCode sysStart()=0
Startup method invoked by the framework.
GaudiCommon< Algorithm >::rootInTES
const std::string & rootInTES() const
Returns the "rootInTES" string.
Definition: GaudiCommon.h:716
GaudiParallelizer::~GaudiParallelizer
virtual ~GaudiParallelizer()
Destructor.
Definition: GaudiParallelizer.cpp:37
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:124
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
ISequencerTimerTool::addTimer
virtual int addTimer(const std::string &name)=0
add a timer entry with the specified name
GaudiAlgorithm::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiAlgorithm.cpp:48
StatusCode::isSuccess
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
GaudiCommon< Algorithm >::Error
StatusCode Error(const std::string &msg, const StatusCode st=StatusCode::FAILURE, const size_t mx=10) const
Print the error message and return with the given StatusCode.
MSG::DEBUG
Definition: IMessageSvc.h:18
GaudiParallelizer
Definition: GaudiParallelizer.h:20
std
STL namespace.
GaudiParallelizer::decodeNames
StatusCode decodeNames()
Decode a vector of string.
Definition: GaudiParallelizer.cpp:139
GaudiCommon< Algorithm >::context
const std::string & context() const
Returns the "context" string. Used to identify different processing states.
Definition: GaudiCommon.h:712
ISequencerTimerTool::increaseIndent
virtual void increaseIndent()=0
Increase the indentation of the name.
std::string::end
T end(T...args)
GaudiParallelizer::m_modeOR
bool m_modeOR
Indicates that the OR is wanted instead of AND.
Definition: GaudiParallelizer.h:73
merge_files.sc
int sc
Definition: merge_files.py:167
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
ISequencerTimerTool::globalTiming
virtual bool globalTiming()=0
returns the flag telling that global timing is wanted
IAlgorithm::sysInitialize
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
SerializeSTL.h
Provide serialization function (output only) for some common STL classes (vectors, lists, pairs, maps) plus GaudiUtils::Map and GaudiUtils::HashMap.
GaudiParallelizer::finalize
virtual StatusCode finalize()
Algorithm finalization.
Definition: GaudiParallelizer.cpp:131
std::string
STL class.
compareRootHistos.ref
string ref
Definition: compareRootHistos.py:14
Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:820
SimpleProperty< std::string >
GaudiParallelizer::m_names
StringArrayProperty m_names
Input string, list of algorithms.
Definition: GaudiParallelizer.h:71
SmartIF::get
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:76
std::vector::push_back
T push_back(T...args)
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:9
Algorithm::setExecuted
void setExecuted(bool state) override
Set the executed flag to the specified state.
Definition: Algorithm.cpp:844
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:121
StatusCode::FAILURE
Definition: StatusCode.h:29
StatusCode::SUCCESS
Definition: StatusCode.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
GaudiParallelizer.h
Algorithm::isExecuted
bool isExecuted() const override
Has this algorithm been executed since the last reset?
Definition: Algorithm.cpp:840
GaudiAlgorithm::finalize
StatusCode finalize() override
standard finalization method
Definition: GaudiAlgorithm.cpp:62
DECLARE_ALGORITHM_FACTORY
#define DECLARE_ALGORITHM_FACTORY(x)
Definition: Algorithm.h:777
GaudiAlgorithm
The useful base class for data processing algorithms.
Definition: GaudiAlgorithm.h:93
PropertyWithValue::value
const TYPE & value() const
explicit conversion
Definition: Property.h:341
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:35
GaudiParallelizer::m_task_group
tbb::task_group m_task_group
TBB task group.
Definition: GaudiParallelizer.h:80
ISequencerTimerTool::start
virtual void start(int index)=0
start the counter, i.e.
Algorithm::subAlgorithms
const std::vector< Algorithm * > * subAlgorithms() const
List of sub-algorithms. Returns a pointer to a vector of (sub) Algorithms.
Definition: Algorithm.cpp:865
Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:74
Gaudi::StateMachine::RUNNING
Definition: StateMachine.h:16
Property
Property base class allowing Property* collections to be "homogeneous".
Definition: Property.h:38
std::vector
STL class.
Algorithm::FSMState
Gaudi::StateMachine::State FSMState() const override
returns the current state of the algorithm
Definition: Algorithm.h:193
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:127
GaudiParallelizer::execute
virtual StatusCode execute()
Algorithm execution.
Definition: GaudiParallelizer.cpp:94
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
Algorithm::isEnabled
bool isEnabled() const override
Is this algorithm enabled or disabled?
Definition: Algorithm.cpp:853
std::vector::begin
T begin(T...args)
Gaudi::Utils::TypeNameString::type
const std::string & type() const
Definition: TypeNameString.h:22
GaudiParallelizer::m_entries
std::vector< AlgorithmEntry > m_entries
List of algorithms to process.
Definition: GaudiParallelizer.h:72
GaudiParallelizer::membershipHandler
void membershipHandler(Property &theProp)
for asynchronous changes in the list of algorithms
Definition: GaudiParallelizer.cpp:279
ISequencerTimerTool.h
IOTest.appMgr
tuple appMgr
Definition: IOTest.py:83
IAlgManager.h
gaudiComponentHelp.properties
tuple properties
Definition: gaudiComponentHelp.py:37
GaudiParallelizer::m_timerTool
ISequencerTimerTool * m_timerTool
Pointer to the timer tool.
Definition: GaudiParallelizer.h:77
GaudiParallelizer::m_measureTime
bool m_measureTime
Flag to measure time.
Definition: GaudiParallelizer.h:75
GaudiParallelizer::m_timer
int m_timer
Timer number for the sequencer.
Definition: GaudiParallelizer.h:78
AlgFactory.h
Gaudi::Utils::TypeNameString::name
const std::string & name() const
Definition: TypeNameString.h:23
StatusCode::ignore
void ignore() const
Definition: StatusCode.h:108
GaudiDict::typeName
std::string typeName(const std::type_info &typ)
Definition: Dictionary.cpp:21
GaudiParallelizer::m_nthreads
unsigned short m_nthreads
Number of threads in the thread pool.
Definition: GaudiParallelizer.h:81
CommonMessagingBase::msgLevel
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:136
GaudiParallelizer::AlgorithmEntry
Definition: GaudiParallelizer.h:34
Algorithm::sysInitialize
StatusCode sysInitialize() override
Initialization method invoked by the framework.
Definition: Algorithm.cpp:105
ISequencerTimerTool::stop
virtual double stop(int index)=0
stop the counter, return the elapsed time
GaudiCommon< Algorithm >::release
StatusCode release(const IInterface *interface) const
Manual forced (and 'safe') release of the active tool or service.
GaudiParallelizer::AlgorithmEntry::run
void run(GaudiParallelizer &prlzr)
Thread task executor method to wrap an algorithm execution in.
Definition: GaudiParallelizer.h:52
GaudiParallelizer::initialize
virtual StatusCode initialize()
Algorithm initialization.
Definition: GaudiParallelizer.cpp:42
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
IJobOptionsSvc.h