The Gaudi Framework  v36r1 (3e2fb5a8)
HiveDataBroker.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "HiveDataBroker.h"
14 #include "GaudiKernel/System.h"
15 #include "boost/lexical_cast.hpp"
16 #include "boost/tokenizer.hpp"
17 #include <Gaudi/Algorithm.h>
18 #include <algorithm>
19 #ifdef __cpp_lib_ranges
20 # include <ranges>
21 namespace ranges = std::ranges;
22 #else
23 # include "range/v3/algorithm/for_each.hpp"
24 # include "range/v3/view/filter.hpp"
25 # include "range/v3/view/reverse.hpp"
26 # include "range/v3/view/transform.hpp"
27 // upstream has renamed namespace ranges::view ranges::views
28 # if RANGE_V3_VERSION < 900
29 namespace ranges::views {
30  using namespace ranges::view;
31 }
32 # endif
33 #endif
34 
36 
37 namespace {
38  struct AlgorithmRepr {
39  const Gaudi::Algorithm& parent;
40 
41  friend std::ostream& operator<<( std::ostream& s, const AlgorithmRepr& a ) {
42  std::string typ = System::typeinfoName( typeid( a.parent ) );
43  s << typ;
44  if ( a.parent.name() != typ ) s << "/" << a.parent.name();
45  return s;
46  }
47  };
48 
49  struct DataObjIDSorter {
50  bool operator()( const DataObjID* a, const DataObjID* b ) { return a->fullKey() < b->fullKey(); }
51  };
52 
53  // Sort a DataObjIDColl in a well-defined, reproducible manner.
54  // Used for making debugging dumps.
55  std::vector<const DataObjID*> sortedDataObjIDColl( const DataObjIDColl& coll ) {
57  v.reserve( coll.size() );
58  for ( const DataObjID& id : coll ) v.push_back( &id );
59  std::sort( v.begin(), v.end(), DataObjIDSorter() );
60  return v;
61  }
62 
63  SmartIF<IAlgorithm> createAlgorithm( IAlgManager& am, const std::string& type, const std::string& name ) {
64  // Maybe modify the AppMgr interface to return Algorithm* ??
65  IAlgorithm* tmp;
66  StatusCode sc = am.createAlgorithm( type, name, tmp );
67  return {sc.isSuccess() ? dynamic_cast<Gaudi::Algorithm*>( tmp ) : nullptr};
68  }
69 } // namespace
70 
72  auto sc = Service::initialize();
73  if ( sc.isFailure() ) return sc;
74  // populate m_algorithms
75  m_algorithms = instantiateAndInitializeAlgorithms( m_producers );
76  if ( sc.isFailure() ) return sc;
77 
78  // warn about non-reentrant algorithms
79  ranges::for_each( m_algorithms | ranges::views::transform( []( const auto& entry ) { return entry.alg; } ) |
80  ranges::views::filter( []( const auto* alg ) { return alg->cardinality() > 0; } ),
81  [&]( const Gaudi::Algorithm* alg ) {
82  this->warning() << "non-reentrant algorithm: " << AlgorithmRepr{*alg} << endmsg;
83  } );
84  //== Print the list of the created algorithms
85  if ( msgLevel( MSG::DEBUG ) ) {
86  MsgStream& msg = debug();
87  msg << "Available DataProducers: ";
89  msg, m_algorithms, ", ",
90  []( auto& os, const AlgEntry& e ) -> decltype( auto ) { return os << AlgorithmRepr{*e.alg}; } );
91  msg << endmsg;
92  }
93 
94  // populate m_dependencies
95  m_dependencies = mapProducers( m_algorithms );
96  return sc;
97 }
98 
100 
101  StatusCode ss = Service::start();
102  if ( !ss.isSuccess() ) return ss;
103 
104  // sysStart for m_algorithms
105  for ( AlgEntry& algEntry : m_algorithms ) {
106  ss = algEntry.alg->sysStart();
107  if ( ss.isFailure() ) {
108  error() << "Unable to start Algorithm: " << algEntry.alg->name() << endmsg;
109  return ss;
110  }
111  }
112  // sysStart for m_cfnodes
113  for ( AlgEntry& algEntry : m_cfnodes ) {
114  ss = algEntry.alg->sysStart();
115  if ( ss.isFailure() ) {
116  error() << "Unable to start Algorithm: " << algEntry.alg->name() << endmsg;
117  return ss;
118  }
119  }
120  return ss;
121 }
122 
124  StatusCode ss = Service::stop();
125  if ( !ss.isSuccess() ) return ss;
126 
127  // sysStart for m_algorithms
128  for ( AlgEntry& algEntry : m_algorithms ) {
129  ss = algEntry.alg->sysStop();
130  if ( ss.isFailure() ) {
131  error() << "Unable to stop Algorithm: " << algEntry.alg->name() << endmsg;
132  return ss;
133  }
134  }
135  // sysStart for m_cfnodes
136  for ( AlgEntry& algEntry : m_cfnodes ) {
137  ss = algEntry.alg->sysStop();
138  if ( ss.isFailure() ) {
139  error() << "Unable to stop Algorithm: " << algEntry.alg->name() << endmsg;
140  return ss;
141  }
142  }
143  return ss;
144 }
145 
147  ranges::for_each( m_algorithms | ranges::views::transform( &AlgEntry::alg ), []( Gaudi::Algorithm* alg ) {
148  alg->sysFinalize().ignore( /* AUTOMATICALLY ADDED FOR gaudi/Gaudi!763 */ );
149  } );
150  m_algorithms.clear();
151  return Service::finalize();
152 }
153 
154 // populate m_algorithms
158 
159  //= Get the Application manager, to see if algorithm exist
160  auto appMgr = service<IAlgManager>( "ApplicationMgr" );
161  for ( const Gaudi::Utils::TypeNameString item : names ) {
162  const std::string& theName = item.name();
163  const std::string& theType = item.type();
164 
165  //== Check wether the specified algorithm already exists. If not, create it
166  SmartIF<IAlgorithm> myIAlg = appMgr->algorithm( item, false ); // do not create it now
167  if ( !myIAlg ) {
168  myIAlg = createAlgorithm( *appMgr, theType, theName );
169  } else {
170  // when the algorithm is not created, the ref count is short by one, so we
171  // have to fix it.
172  myIAlg->addRef();
173  }
174 
175  if ( !myIAlg ) {
176  throw GaudiException{"Failed to create " + boost::lexical_cast<std::string>( item ), __func__,
178  }
179 
180  // propagate the sub-algorithm into own state.
181  StatusCode sc = myIAlg->sysInitialize();
182  if ( sc.isFailure() ) {
183  throw GaudiException{"Failed to initialize " + boost::lexical_cast<std::string>( item ), __func__,
185  }
186 
187  algorithms.emplace_back( std::move( myIAlg ) );
188  }
189 
190  return algorithms;
191 }
192 
195  if ( msgLevel( MSG::DEBUG ) ) {
196  debug() << "Data Dependencies for Algorithms:";
197  for ( const auto& entry : m_algorithms ) {
198  debug() << "\n " << entry.alg->name() << " :";
199  for ( const auto* id : sortedDataObjIDColl( entry.alg->inputDataObjs() ) ) {
200  debug() << "\n o INPUT " << id->key();
201  }
202  for ( const auto* id : sortedDataObjIDColl( entry.alg->outputDataObjs() ) ) {
203  debug() << "\n o OUTPUT " << id->key();
204  }
205  }
206  debug() << endmsg;
207  }
208 
209  // figure out all outputs
211  for ( AlgEntry& alg : algorithms ) {
212  const auto& output = alg.alg->outputDataObjs();
213  if ( output.empty() ) { continue; }
214  for ( auto id : output ) {
215  if ( id.key().find( ":" ) != std::string::npos ) {
216  error() << " in Alg " << AlgorithmRepr{*alg.alg} << " alternatives are NOT allowed for outputs! id: " << id
217  << endmsg;
218  }
219 
220  auto r = producers.emplace( id, &alg );
221  if ( !r.second ) {
222  throw GaudiException( "multiple algorithms declare " + id.key() + " as output (" + alg.alg->name() + " and " +
223  producers[id]->alg->name() + " at least). This is not allowed",
224  __func__, StatusCode::FAILURE );
225  }
226  }
227  }
228 
229  // resolve dependencies
230  for ( auto& algEntry : algorithms ) {
231  auto input = sortedDataObjIDColl( algEntry.alg->inputDataObjs() );
232  for ( const DataObjID* idp : input ) {
233  DataObjID id = *idp;
234  if ( id.key().find( ":" ) != std::string::npos ) {
235  warning() << AlgorithmRepr{*( algEntry.alg )} << " contains alternatives which require resolution...\n";
236  auto tokens = boost::tokenizer<boost::char_separator<char>>{id.key(), boost::char_separator<char>{":"}};
237  auto itok = std::find_if( tokens.begin(), tokens.end(),
238  [&]( DataObjID t ) { return producers.find( t ) != producers.end(); } );
239  if ( itok != tokens.end() ) {
240  warning() << "found matching output for " << *itok << " -- updating info\n";
241  id.updateKey( *itok );
242  warning() << "Please update input to not require alternatives, and "
243  "instead properly configure the dataloader"
244  << endmsg;
245  } else {
246  error() << "failed to find alternate in global output list"
247  << " for id: " << id << " in Alg " << algEntry.alg << endmsg;
248  }
249  }
250  auto iproducer = producers.find( id );
251  if ( iproducer != producers.end() ) {
252  algEntry.dependsOn.insert( iproducer->second );
253  } else {
254  std::ostringstream error_message;
255  error_message << "\nUnknown requested input by " << AlgorithmRepr{*( algEntry.alg )} << " : " << id.key()
256  << " .\n";
257  error_message << "You can set the OutputLevel of HiveDataBrokerSvc to DEBUG to get a list of inputs and "
258  "outputs of every algorithm.\n";
259  throw GaudiException( error_message.str(), __func__, StatusCode::FAILURE );
260  // TODO: assign to dataloader!
261  // algEntry.dependsOn.insert(dataloader.alg);
262  // dataloader.data.emplace( id ); // TODO: we may ask to much of the
263  // dataloader this way...
264  }
265  }
266  }
267  return producers;
268 }
269 
272  const std::vector<std::string>& stoppers ) const {
274 
276  deps.reserve( requested.size() );
277 
278  // start with seeding from the initial request
279  for ( const auto& req : requested ) {
280  DataObjID id = req;
281  if ( id.key().find( ":" ) != std::string::npos ) {
282  warning() << req.key() << " contains alternatives which require resolution...\n";
283  auto tokens = boost::tokenizer<boost::char_separator<char>>{id.key(), boost::char_separator<char>{":"}};
284  auto itok = std::find_if( tokens.begin(), tokens.end(),
285  [&]( DataObjID t ) { return m_dependencies.find( t ) != m_dependencies.end(); } );
286  if ( itok != tokens.end() ) {
287  warning() << "found matching output for " << *itok << " -- updating info\n";
288  id.updateKey( *itok );
289  warning() << "Please update input to not require alternatives, and "
290  "instead properly configure the dataloader"
291  << endmsg;
292  } else {
293  error() << "failed to find alternate in global output list"
294  << " for id: " << id << endmsg;
295  }
296  }
297  auto i = m_dependencies.find( id );
298  if ( i == m_dependencies.end() )
299  throw GaudiException( "unknown requested input: " + id.key(), __func__, StatusCode::FAILURE );
300  deps.push_back( i->second );
301  }
302  // insert the (direct) dependencies of 'current' right after 'current', and
303  // interate until done...
304  for ( auto current = deps.begin(); current != deps.end(); ++current ) {
305  if ( std::any_of( std::begin( stoppers ), std::end( stoppers ),
306  [current]( auto& stopper ) { return ( *current )->alg->name() == stopper; } ) ) {
307  continue;
308  }
309  for ( auto* entry : ( *current )->dependsOn ) {
310  if ( std::find( std::next( current ), deps.end(), entry ) != deps.end() ) continue; // already there downstream...
311 
312  auto dup = std::find( deps.begin(), current, entry );
313  // if present upstream, move it downstream. Otherwise, insert
314  // downstream...
315  current = std::prev( dup != current ? std::rotate( dup, std::next( dup ), std::next( current ) )
316  : deps.insert( std::next( current ), entry ) );
317  }
318  }
319  auto range = ( deps | ranges::views::transform( []( auto& i ) { return i->alg; } ) | ranges::views::reverse );
320  return {begin( range ), end( range )};
321 }
322 
325  const std::vector<std::string>& stoppers ) const {
327 
328  auto alg = std::find_if( begin( m_cfnodes ), end( m_cfnodes ),
329  [&]( const AlgEntry& ae ) { return ae.alg->name() == requested.name(); } );
330 
331  if ( alg != end( m_cfnodes ) && alg->alg->type() != requested.type() ) {
332  error() << "requested " << requested << " but have matching name with different type: " << alg->alg->type()
333  << endmsg;
334  }
335  if ( alg == end( m_cfnodes ) ) {
336  auto av = instantiateAndInitializeAlgorithms( {requested.type() + '/' + requested.name()} );
337  assert( av.size() == 1 );
338  m_cfnodes.push_back( std::move( av.front() ) );
339  alg = std::next( m_cfnodes.rbegin() ).base();
340  }
341  assert( alg != end( m_cfnodes ) );
342  assert( alg->alg != nullptr );
343  if ( std::find_if( std::begin( stoppers ), std::end( stoppers ),
344  [&requested]( auto& stopper ) { return requested.name() == stopper; } ) == std::end( stoppers ) ) {
345  result = algorithmsRequiredFor( alg->alg->inputDataObjs(), stoppers );
346  }
347  result.push_back( alg->alg );
348  if ( msgLevel( MSG::DEBUG ) ) {
349  debug() << std::endl << "requested " << requested << " returning " << std::endl << " ";
351  debug(), result, ",\n ",
352  []( auto& os, const Gaudi::Algorithm* a ) -> decltype( auto ) { return os << AlgorithmRepr{*a}; } );
353  debug() << std::endl << endmsg;
354  }
355  return result;
356 }
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
HiveDataBrokerSvc::initialize
StatusCode initialize() override
Definition: HiveDataBroker.cpp:71
IAlgManager.h
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
Gaudi::Utils::TypeNameString::name
const std::string & name() const
Definition: TypeNameString.h:49
std::move
T move(T... args)
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:542
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:355
Service::start
StatusCode start() override
Definition: Service.cpp:187
std::unordered_set< DataObjID, DataObjID_Hasher >
System.h
std::vector::reserve
T reserve(T... args)
GaudiException.h
reverse
::details::reverse_wrapper< T > reverse(T &&iterable)
Definition: reverse.h:59
gaudirun.s
string s
Definition: gaudirun.py:328
std::vector
STL class.
std::find_if
T find_if(T... args)
std::unordered_set::size
T size(T... args)
GaudiException
Definition: GaudiException.h:31
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:18
std::map::emplace
T emplace(T... args)
ranges
Definition: FunctionalDetails.h:33
std::rotate
T rotate(T... args)
std::any_of
T any_of(T... args)
ranges::views
Definition: FunctionalDetails.h:33
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
gaudirun.output
output
Definition: gaudirun.py:506
HiveDataBrokerSvc::stop
StatusCode stop() override
Definition: HiveDataBroker.cpp:123
std::sort
T sort(T... args)
Service::finalize
StatusCode finalize() override
Definition: Service.cpp:222
basic.alg
alg
Definition: basic.py:15
DataObjID::fullKey
std::string fullKey() const
combination of the key and the ClassName, mostly for debugging
Definition: DataObjID.cpp:99
std::vector::push_back
T push_back(T... args)
bug_34121.t
t
Definition: bug_34121.py:30
IAlgManager
Definition: IAlgManager.h:37
GaudiAlg::operator<<
std::ostream & operator<<(std::ostream &str, const GaudiAlg::ID &id)
Operator overloading for ostream.
Definition: GaudiHistoID.h:142
Gaudi::Utils::TypeNameString
Helper class to parse a string of format "type/name".
Definition: TypeNameString.h:20
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
HistoDumpEx.r
r
Definition: HistoDumpEx.py:20
IAlgorithm
Definition: IAlgorithm.h:38
HiveDataBrokerSvc::instantiateAndInitializeAlgorithms
std::vector< AlgEntry > instantiateAndInitializeAlgorithms(const std::vector< std::string > &names) const
Definition: HiveDataBroker.cpp:156
Histograms_with_global.algorithms
list algorithms
Definition: Histograms_with_global.py:17
std::ostream
STL class.
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
HiveDataBrokerSvc::start
StatusCode start() override
Definition: HiveDataBroker.cpp:99
HiveDataBrokerSvc::algorithmsRequiredFor
std::vector< Gaudi::Algorithm * > algorithmsRequiredFor(const DataObjIDColl &requested, const std::vector< std::string > &stoppers={}) const override
Definition: HiveDataBroker.cpp:271
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:90
Algorithm.h
SmartIF< IAlgorithm >
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
std::map< DataObjID, HiveDataBrokerSvc::AlgEntry * >
MsgStream
Definition: MsgStream.h:34
IAlgManager::createAlgorithm
virtual StatusCode createAlgorithm(std::string algtype, std::string algname, IAlgorithm *&alg, bool managed=false, bool checkIfExists=true)=0
Create an instance of a algorithm type that has been declared beforehand and assigns to it a name.
Gaudi::Utils::TypeNameString::type
const std::string & type() const
Definition: TypeNameString.h:48
DataObjID
Definition: DataObjID.h:47
HiveDataBrokerSvc::mapProducers
std::map< DataObjID, AlgEntry * > mapProducers(std::vector< AlgEntry > &algorithms) const
Definition: HiveDataBroker.cpp:194
HistoDumpEx.v
v
Definition: HistoDumpEx.py:27
std::ostringstream
STL class.
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:142
gaudirun.type
type
Definition: gaudirun.py:154
std::endl
T endl(T... args)
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
HiveDataBrokerSvc
Definition: HiveDataBroker.h:17
std::vector::begin
T begin(T... args)
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
std::vector::insert
T insert(T... args)
DataObjID::key
const std::string & key() const
only return the last part of the key
Definition: DataObjID.h:58
std::ostringstream::str
T str(T... args)
std::map::end
T end(T... args)
IOTest.end
end
Definition: IOTest.py:123
HiveDataBrokerSvc::AlgEntry
Definition: HiveDataBroker.h:37
HiveDataBrokerSvc::AlgEntry::alg
Gaudi::Algorithm * alg
Definition: HiveDataBroker.h:39
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
std::prev
T prev(T... args)
HiveDataBrokerSvc::finalize
StatusCode finalize() override
Definition: HiveDataBroker.cpp:146
HiveDataBroker.h
ProduceConsume.key
key
Definition: ProduceConsume.py:52
IOTest.appMgr
appMgr
Definition: IOTest.py:103
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:97
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:73
std::next
T next(T... args)