https://mooseframework.inl.gov
Public Member Functions | Static Public Member Functions | List of all members
DependencyResolverInterface Class Referenceabstract

Interface for sorting dependent vectors of objects. More...

#include <DependencyResolverInterface.h>

Inheritance diagram for DependencyResolverInterface:
[legend]

Public Member Functions

 DependencyResolverInterface ()
 Constructor. More...
 
 DependencyResolverInterface (const DependencyResolverInterface &, const Moose::Kokkos::FunctorCopy &)
 Special constructor used for Kokkos functor copy during parallel dispatch. More...
 
virtual const std::set< std::string > & getRequestedItems ()=0
 Return a set containing the names of items requested by the object. More...
 
virtual const std::set< std::string > & getSuppliedItems ()=0
 Return a set containing the names of items owned by the object. More...
 

Static Public Member Functions

template<typename T >
static void sort (typename std::vector< T > &vector)
 Given a vector, sort using the getRequested/SuppliedItems sets. More...
 
template<typename T >
static void sortDFS (typename std::vector< T > &vector)
 Given a vector, sort using the depth-first search. More...
 
template<typename T , typename T2 >
static void cyclicDependencyError (CyclicDependencyException< T2 > &e, const std::string &header)
 A helper method for cyclic errors. More...
 

Detailed Description

Interface for sorting dependent vectors of objects.

Definition at line 25 of file DependencyResolverInterface.h.

Constructor & Destructor Documentation

◆ DependencyResolverInterface() [1/2]

DependencyResolverInterface::DependencyResolverInterface ( )
inline

Constructor.

Definition at line 31 of file DependencyResolverInterface.h.

31 {}

◆ DependencyResolverInterface() [2/2]

DependencyResolverInterface::DependencyResolverInterface ( const DependencyResolverInterface ,
const Moose::Kokkos::FunctorCopy  
)
inline

Special constructor used for Kokkos functor copy during parallel dispatch.

Definition at line 37 of file DependencyResolverInterface.h.

39  {
40  }

Member Function Documentation

◆ cyclicDependencyError()

template<typename T , typename T2 >
void DependencyResolverInterface::cyclicDependencyError ( CyclicDependencyException< T2 > &  e,
const std::string &  header 
)
static

A helper method for cyclic errors.

Definition at line 129 of file DependencyResolverInterface.h.

131 {
132  std::ostringstream oss;
133 
134  oss << header << ":\n";
135  const auto cycle = e.getCyclicDependencies();
136  std::vector<std::string> names(cycle.size());
137  for (const auto i : index_range(cycle))
138  names[i] = static_cast<T>(cycle[i])->name();
139  oss << MooseUtils::join(names, " <- ");
140  mooseError(oss.str());
141 }
std::string join(Iterator begin, Iterator end, const std::string &delimiter)
Python-like join function for strings over an iterator range.
Definition: MooseUtils.h:142
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application...
Definition: MooseError.h:323
const std::vector< T > & getCyclicDependencies() const
auto index_range(const T &sizable)

◆ getRequestedItems()

virtual const std::set<std::string>& DependencyResolverInterface::getRequestedItems ( )
pure virtual

◆ getSuppliedItems()

virtual const std::set<std::string>& DependencyResolverInterface::getSuppliedItems ( )
pure virtual

◆ sort()

template<typename T >
void DependencyResolverInterface::sort ( typename std::vector< T > &  vector)
static

Given a vector, sort using the getRequested/SuppliedItems sets.

Definition at line 74 of file DependencyResolverInterface.h.

Referenced by TheWarehouse::prepare().

75 {
76  sortDFS(vector);
77 }
static void sortDFS(typename std::vector< T > &vector)
Given a vector, sort using the depth-first search.

◆ sortDFS()

template<typename T >
void DependencyResolverInterface::sortDFS ( typename std::vector< T > &  vector)
static

Given a vector, sort using the depth-first search.

Class that represents the dependency as a graph

Definition at line 81 of file DependencyResolverInterface.h.

Referenced by sort().

82 {
83  if (vector.size() <= 1)
84  return;
85 
90 
91  // Map of suppliers: what is supplied -> by what object
92  std::multimap<std::string, T> suppliers_map;
93  for (auto & v : vector)
94  {
95  // Whether or not this object supplies something, we will always
96  // add it as a node because we want to make sure that it gets returned
97  graph.addNode(v);
98 
99  for (const auto & supplied_item : v->getSuppliedItems())
100  suppliers_map.emplace(supplied_item, v);
101  }
102 
103  // build the dependency graph
104  for (auto & v : vector)
105  for (const auto & requested_item : v->getRequestedItems())
106  {
107  const auto & [begin_it, end_it] = suppliers_map.equal_range(requested_item);
108  for (const auto & [supplier_name, supplier_object] : as_range(begin_it, end_it))
109  {
110  libmesh_ignore(supplier_name);
111 
112  // We allow an object to have a circular dependency within itself; e.g. we choose to
113  // trust a developer knows what they are doing within a single object
114  if (supplier_object != v)
115  graph.addEdge(supplier_object, v);
116  }
117  }
118 
119  const auto & sorted = graph.dfs();
120 
121  // The set here gets unique objects, as it's valid to pass in duplicates
122  mooseAssert(sorted.size() == std::set<T>(vector.begin(), vector.end()).size(), "Size mismatch");
123 
124  vector = sorted;
125 }
void addEdge(const T &a, const T &b)
Add an edge between nodes &#39;a&#39; and &#39;b&#39;.
void libmesh_ignore(const Args &...)
SimpleRange< IndexType > as_range(const std::pair< IndexType, IndexType > &p)
void addNode(const T &a)
Add a node &#39;a&#39; to the graph.
const std::vector< T > & dfs()
Do depth-first search from root nodes to obtain order in which graph nodes should be "executed"...
Class that represents the dependecy as a graph.

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