31 SmartIF<IAlgorithm> ialg;
32 Gaudi::Algorithm* alg;
33 std::set<AlgEntry const*> dependsOn;
34 std::vector<DataObjID const*> inputs;
35 std::vector<DataObjID const*> outputs;
37 friend bool operator<( AlgEntry
const& lhs, AlgEntry
const& rhs ) {
return lhs.index < rhs.index; }
39 friend bool operator==( AlgEntry
const& lhs, AlgEntry
const& rhs ) {
return lhs.index == rhs.index; }
41 AlgEntry(
size_t i, SmartIF<IAlgorithm>&& p )
42 : index{ i }, ialg{
std::
move( p ) }, alg{ dynamic_cast<
Gaudi::Algorithm*>( ialg.
get() ) } {
43 if ( !alg )
throw std::runtime_error(
"algorithm pointer == nullptr???" );
46 constexpr auto gather = [](
auto&
c,
auto const& in1,
auto const& in2 ) {
47 for (
const DataObjID&
id : in1 )
c.push_back( &
id );
48 for (
const DataObjID&
id : in2 )
c.push_back( &
id );
49 constexpr auto by_key = [](
const DataObjID*
id ) {
return id->fullKey(); };
50 std::ranges::sort( c, std::less{}, by_key );
51 auto od = std::ranges::unique( c, std::equal_to{}, by_key );
52 c.erase( od.begin(), od.end() );
54 gather( outputs, alg->outputDataObjs(), alg->extraOutputDeps() );
55 gather( inputs, alg->inputDataObjs(), alg->extraInputDeps() );
59 template <std::ranges::range R>
60 requires std::common_reference_with<std::ranges::range_reference_t<R>,
const AlgEntry&>
61 void dumpDataDepsGraphFile( std::string
const& fname, R
const& algorithms ) {
64 std::size_t algoIndex = 0ul;
74 for (
const AlgEntry& entry : algorithms ) {
75 std::string algIndex =
"Alg_" + std::to_string( algoIndex );
76 indexByName[entry.alg->name()] = algIndex;
81 for (
const AlgEntry& entry : algorithms ) {
83 g.addNode( indexByName[entry.alg->name()], entry.alg->name() );
86 for (
const auto*
id : entry.inputs ) {
87 const auto [itr, inserted] = definedObjects.
insert(
id->hash() );
88 std::string objIndex =
"obj_" + std::to_string(
id->hash() );
89 if ( inserted )
g.addNode( objIndex,
id->key() );
92 g.addEdge( objIndex, indexByName[entry.alg->name()] );
96 for (
const auto*
id : entry.outputs ) {
97 const auto [itr, inserted] = definedObjects.
insert(
id->hash() );
98 std::string objIndex =
"obj_" + std::to_string(
id->hash() );
99 if ( inserted )
g.addNode( objIndex,
id->key() );
102 g.addEdge( indexByName[entry.alg->name()], objIndex );
107 struct AlgorithmRepr {
108 const Gaudi::Algorithm& parent;
113 if ( a.parent.
name() != typ )
s <<
"/" << a.parent.
name();
119 template <
typename T>
122 std::sort( v.
begin(), v.
end(), [](
const auto* lhs,
const auto* rhs ) { return *lhs < *rhs; } );
136 using extends::extends;
150 "Attribute any unmet input dependencies to this Algorithm" };
152 this,
"DataProducers", {},
"List of algorithms to be used to resolve data dependencies" };
155 "Name of the output file (.dot, .md or .graphml extensions "
156 "allowed) containing the data dependency graph. "
157 "If empty, no graph is dumped" };
184 std::ranges::for_each(
m_algorithms | std::ranges::views::transform( [](
const auto& entry ) ->
decltype(
auto ) {
185 return entry.second.alg;
187 std::ranges::views::filter( [](
const auto* alg ) {
return alg->cardinality() > 0; } ),
189 this->
warning() <<
"non-reentrant algorithm: " << AlgorithmRepr{ *alg } <<
endmsg;
193 debug() <<
"Available DataProducers:\n";
194 std::ranges::for_each(
m_algorithms | std::ranges::views::transform( [](
const auto& entry ) ->
decltype(
auto ) {
195 return entry.second.alg;
198 this->
debug() <<
" " << AlgorithmRepr{ *alg } <<
" " << alg->outputDataObjs() <<
" "
199 << alg->extraOutputDeps() <<
endmsg;
215 ss = algEntry.alg->sysStart();
230 ss = algEntry.alg->sysStop();
241 algEntry.alg->sysFinalize().ignore( );
254 for (
const std::string& item : names ) {
260 myIAlg = createAlgorithm( *appMgr, tn.
type(), tn.
name() );
273 algorithms.emplace( tn.
name(), AlgEntry{ index++, std::move( myIAlg ) } );
282 debug() <<
"Data Dependencies for Algorithms:";
285 for (
const auto*
id : entry.inputs ) {
debug() <<
"\n o INPUT " <<
id->key(); }
286 for (
const auto*
id : entry.outputs ) {
debug() <<
"\n o OUTPUT " <<
id->key(); }
299 for (
auto& [
name, alg] : algorithms ) {
300 for (
auto id : alg.outputs ) {
301 auto r = producers.
emplace( *
id, &alg );
303 throw GaudiException(
"multiple algorithms declare " + id->key() +
" as output (" +
name +
" and " +
304 producers[*
id]->alg->name() +
" at least). This is not allowed",
311 for (
auto& [
name, algEntry] : algorithms ) {
312 for (
const DataObjID* idp : algEntry.inputs ) {
314 auto iproducer = producers.
find(
id );
315 if ( iproducer != producers.
end() ) {
316 algEntry.dependsOn.insert( iproducer->second );
318 std::ostringstream error_message;
319 error_message <<
"\nUnknown requested input by " << AlgorithmRepr{ *( algEntry.alg ) } <<
" : "
320 << std::quoted(
id.key(),
'\'' ) <<
".\n";
321 error_message <<
"You can set the OutputLevel of HiveDataBrokerSvc to DEBUG to get a list of inputs and "
322 "outputs of every registered algorithm.\n";
340 if ( visited[alg.index] ) {
return; }
344 [alg](
auto& stopper ) {
return alg.alg->name() == stopper; } ) ) {
345 visiting[alg.index] =
true;
346 for (
auto* dep : sorted_( alg.dependsOn ) ) {
visit( *dep, stoppers, sorted, visited, visiting ); }
347 visiting[alg.index] =
false;
350 visited[alg.index] =
true;
363 for (
const auto&
id : requested ) {
370 std::sort( deps.
begin(), deps.
end(), [](
auto const* lhs,
auto const* rhs ) { return *lhs < *rhs; } );
371 deps.
erase(
std::unique( deps.
begin(), deps.
end(), [](
auto const& lhs,
auto const& rhs ) { return *lhs == *rhs; } ),
376 for (
auto* alg : deps ) {
visit( *alg, stoppers, result, visited, visiting ); }
387 throw GaudiException{
"No algorithm with name " + requested.
name() +
" in DataProducers. Type is " +
388 ( requested.
haveType() ? requested.
type() :
"not specified" ),
391 auto const& alg = it->second;
392 if ( requested.
haveType() && alg.alg->type() != requested.
type() ) {
393 error() <<
"requested " << requested <<
" but have matching name with different type: " << alg.alg->type()
396 assert( alg.alg !=
nullptr );
400 visit( alg, stoppers, result, visited, visiting );
405 debug(), result,
",\n ",
406 [](
auto& os,
const Gaudi::Algorithm* a ) ->
decltype(
auto ) {
return os << AlgorithmRepr{ *a }; } );
std::unordered_set< DataObjID, DataObjID_Hasher > DataObjIDColl
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
#define DECLARE_COMPONENT(type)
strong_ordering operator(const error_code &__lhs, const error_code &__rhs) noexcept
basic_ostream< char > ostream
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
constexpr _ForwardIterator unique(_ForwardIterator __first, _ForwardIterator __last)
constexpr bool none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
constexpr void sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
basic_string< char > string
basic_ostream< _CharT, _Traits > & endl(basic_ostream< _CharT, _Traits > &__os)
constexpr auto begin(_Container &__cont) -> decltype(__cont.begin())
constexpr auto end(_Container &__cont) -> decltype(__cont.end())
constexpr bool operator==(const basic_string< _CharT, _Traits, _Alloc > &__lhs, const _CharT *__rhs)
__string_type str() const
const_iterator end() const noexcept
size_type size() const noexcept
iterator find(const key_type &__x)
std::pair< iterator, bool > emplace(_Args &&... __args)
void insert(_InputIterator __first, _InputIterator __last)
constexpr void push_back(const value_type &__x)
constexpr const_iterator begin() const noexcept
constexpr iterator erase(const_iterator __first, const_iterator __last)
constexpr void reserve(size_type __n)
constexpr const_iterator end() const noexcept
constexpr size_type size() const noexcept
size_type size() const noexcept
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Base class from which all concrete algorithm classes should be derived.
const std::string & name() const override
The identifying name of the algorithm object.
utilities to dump graphs in different formats
Implementation of property with value of concrete type.
const ValueType & value() const
Helper class to parse a string of format "type/name".
const std::string & type() const
const std::string & name() const
Define general base for Gaudi exception.
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1).
StatusCode initialize() override
std::map< std::string, AlgEntry > instantiateAndInitializeAlgorithms(const std::vector< std::string > &names) const
Gaudi::Property< std::vector< std::string > > m_producers
std::map< std::string, AlgEntry > m_algorithms
StatusCode stop() override
Gaudi::Property< std::string > m_dataDepsGraphFile
StatusCode finalize() override
Gaudi::Property< std::string > m_dataLoader
std::map< DataObjID, AlgEntry const * > mapProducers(std::map< std::string, AlgEntry > &algorithms) const
void visit(AlgEntry const &alg, std::vector< std::string > const &stoppers, std::vector< Gaudi::Algorithm * > &sorted, std::vector< bool > &visited, std::vector< bool > &visiting) const
Implements DFS topological sorting.
std::vector< Gaudi::Algorithm * > algorithmsRequiredFor(const DataObjIDColl &requested, const std::vector< std::string > &stoppers={}) const override
StatusCode start() override
std::map< DataObjID, AlgEntry const * > m_dependencies
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
virtual StatusCode createAlgorithm(const std::string &algtype, const 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.
The IAlgorithm is the interface implemented by the Algorithm base class.
StatusCode finalize() override
const std::string & name() const override
Retrieve name of the service.
StatusCode stop() override
SmartIF< IFace > service(const std::string &name, bool createIf=true) const
StatusCode start() override
StatusCode initialize() override
Small smart pointer class with automatic reference counting for IInterface.
This class is used for returning status codes from appropriate routines.
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
constexpr static const auto FAILURE
Base class used to extend a class implementing other interfaces.
get
decorate the vector of properties
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.