Skip to content

Commit

Permalink
format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
lambda7xx committed Jul 15, 2023
1 parent c850cdd commit a85093d
Show file tree
Hide file tree
Showing 21 changed files with 271 additions and 241 deletions.
2 changes: 1 addition & 1 deletion lib/utils/include/utils/graph/adjacency_digraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class AdjacencyDiGraph : public IDiGraph {

private:
using ContentsType = std::unordered_map<Node, std::unordered_set<Node>>;

AdjacencyDiGraph(std::size_t next_node_idx, ContentsType adjacency)
: next_node_idx(next_node_idx), adjacency(adjacency) {}
std::size_t next_node_idx = 0;
Expand Down
12 changes: 8 additions & 4 deletions lib/utils/include/utils/graph/adjacency_multidigraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class AdjacencyMultiDiGraph : public IMultiDiGraph {
std::unordered_set<Node> query_nodes(NodeQuery const &) const override;

AdjacencyMultiDiGraph *clone() const override {
return new AdjacencyMultiDiGraph(this->next_node_idx, this->next_node_port, this->adjacency);
return new AdjacencyMultiDiGraph(
this->next_node_idx, this->next_node_port, this->adjacency);
}

private:
Expand All @@ -31,9 +32,12 @@ class AdjacencyMultiDiGraph : public IMultiDiGraph {
Node,
std::unordered_map<NodePort, std::unordered_set<NodePort>>>>;

AdjacencyMultiDiGraph(std::size_t next_node_idx, std::size_t next_node_port, ContentsType const & adjacency)
: next_node_idx(next_node_idx), next_node_port(next_node_port), adjacency(adjacency) {}

AdjacencyMultiDiGraph(std::size_t next_node_idx,
std::size_t next_node_port,
ContentsType const &adjacency)
: next_node_idx(next_node_idx), next_node_port(next_node_port),
adjacency(adjacency) {}

std::size_t next_node_idx = 0;
std::size_t next_node_port = 0;
ContentsType adjacency;
Expand Down
5 changes: 3 additions & 2 deletions lib/utils/include/utils/graph/digraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct IDiGraphView : public IGraphView {
IDiGraphView &operator=(IDiGraphView const &) = delete;

virtual std::unordered_set<Edge> query_edges(EdgeQuery const &) const = 0;
virtual ~IDiGraphView()=default;
virtual ~IDiGraphView() = default;

protected:
IDiGraphView() = default;
Expand Down Expand Up @@ -76,8 +76,9 @@ struct DiGraphView {
return DiGraphView(std::make_shared<T>(std::forward<Args>(args)...));
}

DiGraphView(std::shared_ptr<IDiGraphView const> ptr): ptr(ptr) {}
DiGraphView(std::shared_ptr<IDiGraphView const> ptr) : ptr(ptr) {}
static DiGraphView unsafe_create(IDiGraphView const &graphView);

private:
friend DiGraphView unsafe_create(IDiGraphView const &);

Expand Down
4 changes: 2 additions & 2 deletions lib/utils/include/utils/graph/multidigraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct MultiDiGraphView {
return MultiDiGraphView(
std::make_shared<T const>(std::forward<Args>(args)...));
}
MultiDiGraphView(std::shared_ptr<IMultiDiGraphView const> ptr):ptr(ptr){}
MultiDiGraphView(std::shared_ptr<IMultiDiGraphView const> ptr) : ptr(ptr) {}
static MultiDiGraphView unsafe_create(IMultiDiGraphView const &);

private:
Expand Down Expand Up @@ -74,7 +74,7 @@ struct MultiDiGraph {
}

private:
MultiDiGraph(std::unique_ptr<IMultiDiGraph> ptr):ptr(std::move(ptr)){}
MultiDiGraph(std::unique_ptr<IMultiDiGraph> ptr) : ptr(std::move(ptr)) {}

private:
cow_ptr_t<IMultiDiGraph> ptr;
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/include/utils/graph/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct GraphView {
return GraphView(std::make_shared<T>(std::forward<Args>(args)...));
}

GraphView(std::shared_ptr<IGraphView const> ptr):ptr(ptr){}
GraphView(std::shared_ptr<IGraphView const> ptr) : ptr(ptr) {}

private:
std::shared_ptr<IGraphView const> ptr;
Expand All @@ -75,7 +75,7 @@ CHECK_RC_COPY_VIRTUAL_COMPLIANT(IGraphView);

struct IGraph : IGraphView {
IGraph(IGraph const &) = delete;
IGraph()=default;
IGraph() = default;
IGraph &operator=(IGraph const &) = delete;

virtual Node add_node() = 0;
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/include/utils/graph/open_graph_interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ struct OutputMultiDiEdge {
};
FF_VISITABLE_STRUCT(OutputMultiDiEdge, uid, src, srcIdx);

using OpenMultiDiEdge =variant<InputMultiDiEdge, OutputMultiDiEdge, MultiDiEdge>;
using OpenMultiDiEdge =
variant<InputMultiDiEdge, OutputMultiDiEdge, MultiDiEdge>;

using DownwardOpenMultiDiEdge = variant<OutputMultiDiEdge, MultiDiEdge>;

Expand Down
3 changes: 2 additions & 1 deletion lib/utils/include/utils/graph/open_graphs.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct OpenMultiDiGraphView {
}

private:
OpenMultiDiGraphView(std::shared_ptr<IOpenMultiDiGraphView const> ptr):ptr(ptr){}
OpenMultiDiGraphView(std::shared_ptr<IOpenMultiDiGraphView const> ptr)
: ptr(ptr) {}
std::shared_ptr<IOpenMultiDiGraphView const> ptr;
};

Expand Down
6 changes: 3 additions & 3 deletions lib/utils/include/utils/graph/undirected.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct IUndirectedGraphView : public IGraphView {

virtual std::unordered_set<Edge>
query_edges(UndirectedEdgeQuery const &) const = 0;
virtual ~IUndirectedGraphView()=default;
virtual ~IUndirectedGraphView() = default;

protected:
IUndirectedGraphView() = default;
Expand Down Expand Up @@ -81,8 +81,8 @@ struct UndirectedGraphView {
std::make_shared<T>(std::forward<Args>(args)...));
}

UndirectedGraphView(std::shared_ptr<IUndirectedGraphView const> ptr): ptr(ptr) {
}
UndirectedGraphView(std::shared_ptr<IUndirectedGraphView const> ptr)
: ptr(ptr) {}

private:
friend UndirectedGraphView unsafe(IUndirectedGraphView const &);
Expand Down
15 changes: 8 additions & 7 deletions lib/utils/include/utils/graph/views.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ struct DiSubgraphView : public IDiGraphView {
struct MultiDiSubgraphView : public IMultiDiGraphView {
public:
MultiDiSubgraphView() = delete;
explicit MultiDiSubgraphView(MultiDiGraphView const & g,
std::unordered_set<Node> const & subgraph_nodes);
explicit MultiDiSubgraphView(MultiDiGraphView const &g,
std::unordered_set<Node> const &subgraph_nodes);

std::unordered_set<MultiDiEdge>
query_edges(MultiDiEdgeQuery const &) const override;
Expand All @@ -87,8 +87,8 @@ enum class LRDirection { LEFT, RIGHT };

struct JoinNodeKey {
JoinNodeKey() = delete;
JoinNodeKey(Node const & node, LRDirection direction):
node(node), direction(direction) {}
JoinNodeKey(Node const &node, LRDirection direction)
: node(node), direction(direction) {}

bool operator==(JoinNodeKey const &) const;
bool operator<(JoinNodeKey const &) const;
Expand Down Expand Up @@ -218,9 +218,10 @@ struct SingleSourceNodeView : public IDiGraphView {

struct ContractNodeView : public IDiGraphView {
ContractNodeView() = delete;
explicit ContractNodeView(DiGraphView const & g,
explicit ContractNodeView(DiGraphView const &g,
Node const &removed,
Node const &into): g(g), from(removed), to(into) {}
Node const &into)
: g(g), from(removed), to(into) {}

std::unordered_set<DirectedEdge>
query_edges(DirectedEdgeQuery const &) const override;
Expand Down Expand Up @@ -302,7 +303,7 @@ struct ViewMultiDiGraphAsDiGraph : public IDiGraphView {
struct ViewOpenMultiDiGraphAsMultiDiGraph : public IMultiDiGraphView {
public:
ViewOpenMultiDiGraphAsMultiDiGraph() = delete;
explicit ViewOpenMultiDiGraphAsMultiDiGraph(OpenMultiDiGraphView const & g)
explicit ViewOpenMultiDiGraphAsMultiDiGraph(OpenMultiDiGraphView const &g)
: g(g) {}

std::unordered_set<MultiDiEdge>
Expand Down
1 change: 0 additions & 1 deletion lib/utils/src/graph/adjacency_digraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Node AdjacencyDiGraph::add_node() {
return node;
}


void AdjacencyDiGraph::add_node_unsafe(Node const &node) {
adjacency[node];
this->next_node_idx = std::max(this->next_node_idx, node.value() + 1);
Expand Down
127 changes: 66 additions & 61 deletions lib/utils/src/graph/algorithms.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,15 @@ std::size_t num_nodes(GraphView const &g) {
return get_nodes(g).size();
}

DiGraphView contract_node(DiGraphView const &g , Node const &from, Node const &into) {
DiGraphView
contract_node(DiGraphView const &g, Node const &from, Node const &into) {
return DiGraphView::create<ContractNodeView>(g, from, into);
}

DiGraphView apply_contraction(DiGraphView const & g, std::unordered_map<Node, Node> const & nodes){
DiGraphView contractedView = g;
for(auto const & kv : nodes){
DiGraphView apply_contraction(DiGraphView const &g,
std::unordered_map<Node, Node> const &nodes) {
DiGraphView contractedView = g;
for (auto const &kv : nodes) {
Node from = kv.first;
Node into = kv.second;
contractedView = contract_node(contractedView, from, into);
Expand Down Expand Up @@ -216,9 +218,9 @@ std::unordered_set<DirectedEdge>
return to_directed_edges(get_outgoing_edges(multidigraph_view, dsts));
}

std::unordered_set<DirectedEdge> get_outgoing_edges(DiGraphView const & g,
Node const & n){
return get_outgoing_edges(g, std::unordered_set<Node>{n});
std::unordered_set<DirectedEdge> get_outgoing_edges(DiGraphView const &g,
Node const &n) {
return get_outgoing_edges(g, std::unordered_set<Node>{n});
}

std::unordered_map<Node, std::unordered_set<Node>>
Expand Down Expand Up @@ -269,26 +271,24 @@ std::vector<Node>
return {bfs_view.begin(), bfs_view.end()};
}


std::unordered_set<Node> get_sinks(DiGraphView const & g){
std::unordered_set<Node> dsts ;
for(Node const &n : get_nodes(g)) {
std::unordered_set<Node> get_sinks(DiGraphView const &g) {
std::unordered_set<Node> dsts;
for (Node const &n : get_nodes(g)) {
auto outgoing = get_outgoing_edges(g, n);
if(outgoing.size() == 0){
if (outgoing.size() == 0) {
dsts.insert(n);
}
}
return dsts;
}

std::unordered_set<Node> get_sinks(MultiDiGraphView const & g){
std::unordered_set<Node> get_sinks(MultiDiGraphView const &g) {
DiGraphView digraph_view = as_digraph(g);
return get_sinks(digraph_view);
}

DiGraphView flipped(DiGraphView const & g) {
DiGraphView flipped(DiGraphView const &g) {
return DiGraphView::create<FlippedView>(g);

}

std::unordered_set<Node> get_sources(DiGraphView const &g) {
Expand Down Expand Up @@ -381,13 +381,16 @@ std::vector<DirectedEdge> get_edge_topological_ordering(DiGraphView const &g) {
}

/*
transform(get_outgoing_edges(g, n), [](DirectedEdge const &n) { return n.dst; }) return std::unorder_set<Node>
set_union return st::unorder_set<Node>
as_vector convert the std::unorder_set<Node> to std::vector<Node>
transform(get_outgoing_edges(g, n), [](DirectedEdge const &n) { return n.dst; })
return std::unorder_set<Node> set_union return st::unorder_set<Node> as_vector
convert the std::unorder_set<Node> to std::vector<Node>
*/
std::vector<Node> get_neighbors(DiGraphView const & g, Node const & n) {
return as_vector(set_union( transform(get_outgoing_edges(g, n), [](DirectedEdge const &n) { return n.dst; }), transform(get_incoming_edges(g, n), [](DirectedEdge const &n) { return n.src; }) ));

std::vector<Node> get_neighbors(DiGraphView const &g, Node const &n) {
return as_vector(
set_union(transform(get_outgoing_edges(g, n),
[](DirectedEdge const &n) { return n.dst; }),
transform(get_incoming_edges(g, n),
[](DirectedEdge const &n) { return n.src; })));
}

std::vector<MultiDiEdge>
Expand Down Expand Up @@ -500,23 +503,24 @@ optional<Node> imm_post_dominator(MultiDiGraphView const &g, Node const &n) {
return get_imm_post_dominators(g).at(n);
}

optional<Node> get_imm_post_dominator(DiGraphView const & g, Node const & n) {
optional<Node> get_imm_post_dominator(DiGraphView const &g, Node const &n) {
return get_imm_post_dominators(g).at(n);
}

optional<Node> get_imm_post_dominator(DiGraphView const &g,
std::unordered_set<Node> const &nodes) {
std::unordered_set<Node> commonDoms =
get_post_dominators(g).at(get_first(nodes));

optional<Node> get_imm_post_dominator(DiGraphView const & g, std::unordered_set<Node> const & nodes ){
std::unordered_set<Node> commonDoms = get_post_dominators(g).at(get_first(nodes));

for(Node const & node : nodes){
commonDoms = intersection(get_post_dominators(g).at(node), commonDoms);
}
for (Node const &node : nodes) {
commonDoms = intersection(get_post_dominators(g).at(node), commonDoms);
}

if (!commonDoms.empty()) {
return get_first(commonDoms);
} else {
return tl::nullopt;
}
if (!commonDoms.empty()) {
return get_first(commonDoms);
} else {
return tl::nullopt;
}
}

std::pair<OutputMultiDiEdge, InputMultiDiEdge>
Expand Down Expand Up @@ -581,44 +585,45 @@ MultiDiGraphView as_multidigraph(OpenMultiDiGraphView const &g) {
}

std::vector<std::unordered_set<Node>>
get_weakly_connected_components(DiGraphView const & g) {
std::unordered_set<Node> start_pointes = get_sources(g);
std::vector<Node> dfs_order = get_dfs_ordering(g, start_pointes);
get_weakly_connected_components(DiGraphView const &g) {
std::unordered_set<Node> start_pointes = get_sources(g);
std::vector<Node> dfs_order = get_dfs_ordering(g, start_pointes);

std::vector<std::unordered_set<Node>> components;
std::unordered_set<Node> visited;
std::vector<std::unordered_set<Node>> components;
std::unordered_set<Node> visited;

for (Node const & node : dfs_order) {
if (contains(visited, node)) {
continue; // Skip nodes already in a component
}

std::unordered_set<Node> component;
std::stack<Node> stack;
stack.push(node);
for (Node const &node : dfs_order) {
if (contains(visited, node)) {
continue; // Skip nodes already in a component
}

while (!stack.empty()) {
Node current = stack.top();
stack.pop();
std::unordered_set<Node> component;
std::stack<Node> stack;
stack.push(node);

if (contains(visited, current)) {
continue;
}
while (!stack.empty()) {
Node current = stack.top();
stack.pop();

component.insert(current);
visited.insert(current);
if (contains(visited, current)) {
continue;
}

std::vector<Node> neighbors = get_neighbors(g, current); // Replace with your own function to get neighbors
component.insert(current);
visited.insert(current);

for (Node const & neighbor : neighbors) {
stack.push(neighbor);
}
}
std::vector<Node> neighbors = get_neighbors(
g, current); // Replace with your own function to get neighbors

components.push_back(std::move(component));
for (Node const &neighbor : neighbors) {
stack.push(neighbor);
}
}

return components;
components.push_back(std::move(component));
}

return components;
}

} // namespace FlexFlow
Loading

0 comments on commit a85093d

Please sign in to comment.