1 //===- Graph.h - PBQP Graph -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_PBQP_GRAPH_H
15 #define LLVM_CODEGEN_PBQP_GRAPH_H
17 #include "llvm/ADT/STLExtras.h"
29 using NodeId = unsigned;
30 using EdgeId = unsigned;
32 /// @brief Returns a value representing an invalid (non-existent) node.
33 static NodeId invalidNodeId() {
34 return std::numeric_limits<NodeId>::max();
37 /// @brief Returns a value representing an invalid (non-existent) edge.
38 static EdgeId invalidEdgeId() {
39 return std::numeric_limits<EdgeId>::max();
44 /// Instances of this class describe PBQP problems.
46 template <typename SolverT>
47 class Graph : public GraphBase {
49 using CostAllocator = typename SolverT::CostAllocator;
52 using RawVector = typename SolverT::RawVector;
53 using RawMatrix = typename SolverT::RawMatrix;
54 using Vector = typename SolverT::Vector;
55 using Matrix = typename SolverT::Matrix;
56 using VectorPtr = typename CostAllocator::VectorPtr;
57 using MatrixPtr = typename CostAllocator::MatrixPtr;
58 using NodeMetadata = typename SolverT::NodeMetadata;
59 using EdgeMetadata = typename SolverT::EdgeMetadata;
60 using GraphMetadata = typename SolverT::GraphMetadata;
65 using AdjEdgeList = std::vector<EdgeId>;
66 using AdjEdgeIdx = AdjEdgeList::size_type;
67 using AdjEdgeItr = AdjEdgeList::const_iterator;
69 NodeEntry(VectorPtr Costs) : Costs(std::move(Costs)) {}
71 static AdjEdgeIdx getInvalidAdjEdgeIdx() {
72 return std::numeric_limits<AdjEdgeIdx>::max();
75 AdjEdgeIdx addAdjEdgeId(EdgeId EId) {
76 AdjEdgeIdx Idx = AdjEdgeIds.size();
77 AdjEdgeIds.push_back(EId);
81 void removeAdjEdgeId(Graph &G, NodeId ThisNId, AdjEdgeIdx Idx) {
82 // Swap-and-pop for fast removal.
83 // 1) Update the adj index of the edge currently at back().
84 // 2) Move last Edge down to Idx.
86 // If Idx == size() - 1 then the setAdjEdgeIdx and swap are
87 // redundant, but both operations are cheap.
88 G.getEdge(AdjEdgeIds.back()).setAdjEdgeIdx(ThisNId, Idx);
89 AdjEdgeIds[Idx] = AdjEdgeIds.back();
90 AdjEdgeIds.pop_back();
93 const AdjEdgeList& getAdjEdgeIds() const { return AdjEdgeIds; }
96 NodeMetadata Metadata;
99 AdjEdgeList AdjEdgeIds;
104 EdgeEntry(NodeId N1Id, NodeId N2Id, MatrixPtr Costs)
105 : Costs(std::move(Costs)) {
108 ThisEdgeAdjIdxs[0] = NodeEntry::getInvalidAdjEdgeIdx();
109 ThisEdgeAdjIdxs[1] = NodeEntry::getInvalidAdjEdgeIdx();
112 void connectToN(Graph &G, EdgeId ThisEdgeId, unsigned NIdx) {
113 assert(ThisEdgeAdjIdxs[NIdx] == NodeEntry::getInvalidAdjEdgeIdx() &&
114 "Edge already connected to NIds[NIdx].");
115 NodeEntry &N = G.getNode(NIds[NIdx]);
116 ThisEdgeAdjIdxs[NIdx] = N.addAdjEdgeId(ThisEdgeId);
119 void connect(Graph &G, EdgeId ThisEdgeId) {
120 connectToN(G, ThisEdgeId, 0);
121 connectToN(G, ThisEdgeId, 1);
124 void setAdjEdgeIdx(NodeId NId, typename NodeEntry::AdjEdgeIdx NewIdx) {
126 ThisEdgeAdjIdxs[0] = NewIdx;
128 assert(NId == NIds[1] && "Edge not connected to NId");
129 ThisEdgeAdjIdxs[1] = NewIdx;
133 void disconnectFromN(Graph &G, unsigned NIdx) {
134 assert(ThisEdgeAdjIdxs[NIdx] != NodeEntry::getInvalidAdjEdgeIdx() &&
135 "Edge not connected to NIds[NIdx].");
136 NodeEntry &N = G.getNode(NIds[NIdx]);
137 N.removeAdjEdgeId(G, NIds[NIdx], ThisEdgeAdjIdxs[NIdx]);
138 ThisEdgeAdjIdxs[NIdx] = NodeEntry::getInvalidAdjEdgeIdx();
141 void disconnectFrom(Graph &G, NodeId NId) {
143 disconnectFromN(G, 0);
145 assert(NId == NIds[1] && "Edge does not connect NId");
146 disconnectFromN(G, 1);
150 NodeId getN1Id() const { return NIds[0]; }
151 NodeId getN2Id() const { return NIds[1]; }
154 EdgeMetadata Metadata;
158 typename NodeEntry::AdjEdgeIdx ThisEdgeAdjIdxs[2];
161 // ----- MEMBERS -----
163 GraphMetadata Metadata;
164 CostAllocator CostAlloc;
165 SolverT *Solver = nullptr;
167 using NodeVector = std::vector<NodeEntry>;
168 using FreeNodeVector = std::vector<NodeId>;
170 FreeNodeVector FreeNodeIds;
172 using EdgeVector = std::vector<EdgeEntry>;
173 using FreeEdgeVector = std::vector<EdgeId>;
175 FreeEdgeVector FreeEdgeIds;
177 Graph(const Graph &Other) {}
179 // ----- INTERNAL METHODS -----
181 NodeEntry &getNode(NodeId NId) {
182 assert(NId < Nodes.size() && "Out of bound NodeId");
185 const NodeEntry &getNode(NodeId NId) const {
186 assert(NId < Nodes.size() && "Out of bound NodeId");
190 EdgeEntry& getEdge(EdgeId EId) { return Edges[EId]; }
191 const EdgeEntry& getEdge(EdgeId EId) const { return Edges[EId]; }
193 NodeId addConstructedNode(NodeEntry N) {
195 if (!FreeNodeIds.empty()) {
196 NId = FreeNodeIds.back();
197 FreeNodeIds.pop_back();
198 Nodes[NId] = std::move(N);
201 Nodes.push_back(std::move(N));
206 EdgeId addConstructedEdge(EdgeEntry E) {
207 assert(findEdge(E.getN1Id(), E.getN2Id()) == invalidEdgeId() &&
208 "Attempt to add duplicate edge.");
210 if (!FreeEdgeIds.empty()) {
211 EId = FreeEdgeIds.back();
212 FreeEdgeIds.pop_back();
213 Edges[EId] = std::move(E);
216 Edges.push_back(std::move(E));
219 EdgeEntry &NE = getEdge(EId);
221 // Add the edge to the adjacency sets of its nodes.
222 NE.connect(*this, EId);
226 void operator=(const Graph &Other) {}
229 using AdjEdgeItr = typename NodeEntry::AdjEdgeItr;
233 using iterator_category = std::forward_iterator_tag;
234 using value_type = NodeId;
235 using difference_type = int;
236 using pointer = NodeId *;
237 using reference = NodeId &;
239 NodeItr(NodeId CurNId, const Graph &G)
240 : CurNId(CurNId), EndNId(G.Nodes.size()), FreeNodeIds(G.FreeNodeIds) {
241 this->CurNId = findNextInUse(CurNId); // Move to first in-use node id
244 bool operator==(const NodeItr &O) const { return CurNId == O.CurNId; }
245 bool operator!=(const NodeItr &O) const { return !(*this == O); }
246 NodeItr& operator++() { CurNId = findNextInUse(++CurNId); return *this; }
247 NodeId operator*() const { return CurNId; }
250 NodeId findNextInUse(NodeId NId) const {
251 while (NId < EndNId && is_contained(FreeNodeIds, NId)) {
257 NodeId CurNId, EndNId;
258 const FreeNodeVector &FreeNodeIds;
263 EdgeItr(EdgeId CurEId, const Graph &G)
264 : CurEId(CurEId), EndEId(G.Edges.size()), FreeEdgeIds(G.FreeEdgeIds) {
265 this->CurEId = findNextInUse(CurEId); // Move to first in-use edge id
268 bool operator==(const EdgeItr &O) const { return CurEId == O.CurEId; }
269 bool operator!=(const EdgeItr &O) const { return !(*this == O); }
270 EdgeItr& operator++() { CurEId = findNextInUse(++CurEId); return *this; }
271 EdgeId operator*() const { return CurEId; }
274 EdgeId findNextInUse(EdgeId EId) const {
275 while (EId < EndEId && is_contained(FreeEdgeIds, EId)) {
281 EdgeId CurEId, EndEId;
282 const FreeEdgeVector &FreeEdgeIds;
287 NodeIdSet(const Graph &G) : G(G) {}
289 NodeItr begin() const { return NodeItr(0, G); }
290 NodeItr end() const { return NodeItr(G.Nodes.size(), G); }
292 bool empty() const { return G.Nodes.empty(); }
294 typename NodeVector::size_type size() const {
295 return G.Nodes.size() - G.FreeNodeIds.size();
304 EdgeIdSet(const Graph &G) : G(G) {}
306 EdgeItr begin() const { return EdgeItr(0, G); }
307 EdgeItr end() const { return EdgeItr(G.Edges.size(), G); }
309 bool empty() const { return G.Edges.empty(); }
311 typename NodeVector::size_type size() const {
312 return G.Edges.size() - G.FreeEdgeIds.size();
321 AdjEdgeIdSet(const NodeEntry &NE) : NE(NE) {}
323 typename NodeEntry::AdjEdgeItr begin() const {
324 return NE.getAdjEdgeIds().begin();
327 typename NodeEntry::AdjEdgeItr end() const {
328 return NE.getAdjEdgeIds().end();
331 bool empty() const { return NE.getAdjEdgeIds().empty(); }
333 typename NodeEntry::AdjEdgeList::size_type size() const {
334 return NE.getAdjEdgeIds().size();
341 /// @brief Construct an empty PBQP graph.
344 /// @brief Construct an empty PBQP graph with the given graph metadata.
345 Graph(GraphMetadata Metadata) : Metadata(std::move(Metadata)) {}
347 /// @brief Get a reference to the graph metadata.
348 GraphMetadata& getMetadata() { return Metadata; }
350 /// @brief Get a const-reference to the graph metadata.
351 const GraphMetadata& getMetadata() const { return Metadata; }
353 /// @brief Lock this graph to the given solver instance in preparation
354 /// for running the solver. This method will call solver.handleAddNode for
355 /// each node in the graph, and handleAddEdge for each edge, to give the
356 /// solver an opportunity to set up any requried metadata.
357 void setSolver(SolverT &S) {
358 assert(!Solver && "Solver already set. Call unsetSolver().");
360 for (auto NId : nodeIds())
361 Solver->handleAddNode(NId);
362 for (auto EId : edgeIds())
363 Solver->handleAddEdge(EId);
366 /// @brief Release from solver instance.
368 assert(Solver && "Solver not set.");
372 /// @brief Add a node with the given costs.
373 /// @param Costs Cost vector for the new node.
374 /// @return Node iterator for the added node.
375 template <typename OtherVectorT>
376 NodeId addNode(OtherVectorT Costs) {
377 // Get cost vector from the problem domain
378 VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
379 NodeId NId = addConstructedNode(NodeEntry(AllocatedCosts));
381 Solver->handleAddNode(NId);
385 /// @brief Add a node bypassing the cost allocator.
386 /// @param Costs Cost vector ptr for the new node (must be convertible to
388 /// @return Node iterator for the added node.
390 /// This method allows for fast addition of a node whose costs don't need
391 /// to be passed through the cost allocator. The most common use case for
392 /// this is when duplicating costs from an existing node (when using a
393 /// pooling allocator). These have already been uniqued, so we can avoid
394 /// re-constructing and re-uniquing them by attaching them directly to the
396 template <typename OtherVectorPtrT>
397 NodeId addNodeBypassingCostAllocator(OtherVectorPtrT Costs) {
398 NodeId NId = addConstructedNode(NodeEntry(Costs));
400 Solver->handleAddNode(NId);
404 /// @brief Add an edge between the given nodes with the given costs.
405 /// @param N1Id First node.
406 /// @param N2Id Second node.
407 /// @param Costs Cost matrix for new edge.
408 /// @return Edge iterator for the added edge.
409 template <typename OtherVectorT>
410 EdgeId addEdge(NodeId N1Id, NodeId N2Id, OtherVectorT Costs) {
411 assert(getNodeCosts(N1Id).getLength() == Costs.getRows() &&
412 getNodeCosts(N2Id).getLength() == Costs.getCols() &&
413 "Matrix dimensions mismatch.");
414 // Get cost matrix from the problem domain.
415 MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
416 EdgeId EId = addConstructedEdge(EdgeEntry(N1Id, N2Id, AllocatedCosts));
418 Solver->handleAddEdge(EId);
422 /// @brief Add an edge bypassing the cost allocator.
423 /// @param N1Id First node.
424 /// @param N2Id Second node.
425 /// @param Costs Cost matrix for new edge.
426 /// @return Edge iterator for the added edge.
428 /// This method allows for fast addition of an edge whose costs don't need
429 /// to be passed through the cost allocator. The most common use case for
430 /// this is when duplicating costs from an existing edge (when using a
431 /// pooling allocator). These have already been uniqued, so we can avoid
432 /// re-constructing and re-uniquing them by attaching them directly to the
434 template <typename OtherMatrixPtrT>
435 NodeId addEdgeBypassingCostAllocator(NodeId N1Id, NodeId N2Id,
436 OtherMatrixPtrT Costs) {
437 assert(getNodeCosts(N1Id).getLength() == Costs->getRows() &&
438 getNodeCosts(N2Id).getLength() == Costs->getCols() &&
439 "Matrix dimensions mismatch.");
440 // Get cost matrix from the problem domain.
441 EdgeId EId = addConstructedEdge(EdgeEntry(N1Id, N2Id, Costs));
443 Solver->handleAddEdge(EId);
447 /// @brief Returns true if the graph is empty.
448 bool empty() const { return NodeIdSet(*this).empty(); }
450 NodeIdSet nodeIds() const { return NodeIdSet(*this); }
451 EdgeIdSet edgeIds() const { return EdgeIdSet(*this); }
453 AdjEdgeIdSet adjEdgeIds(NodeId NId) { return AdjEdgeIdSet(getNode(NId)); }
455 /// @brief Get the number of nodes in the graph.
456 /// @return Number of nodes in the graph.
457 unsigned getNumNodes() const { return NodeIdSet(*this).size(); }
459 /// @brief Get the number of edges in the graph.
460 /// @return Number of edges in the graph.
461 unsigned getNumEdges() const { return EdgeIdSet(*this).size(); }
463 /// @brief Set a node's cost vector.
464 /// @param NId Node to update.
465 /// @param Costs New costs to set.
466 template <typename OtherVectorT>
467 void setNodeCosts(NodeId NId, OtherVectorT Costs) {
468 VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
470 Solver->handleSetNodeCosts(NId, *AllocatedCosts);
471 getNode(NId).Costs = AllocatedCosts;
474 /// @brief Get a VectorPtr to a node's cost vector. Rarely useful - use
475 /// getNodeCosts where possible.
476 /// @param NId Node id.
477 /// @return VectorPtr to node cost vector.
479 /// This method is primarily useful for duplicating costs quickly by
480 /// bypassing the cost allocator. See addNodeBypassingCostAllocator. Prefer
481 /// getNodeCosts when dealing with node cost values.
482 const VectorPtr& getNodeCostsPtr(NodeId NId) const {
483 return getNode(NId).Costs;
486 /// @brief Get a node's cost vector.
487 /// @param NId Node id.
488 /// @return Node cost vector.
489 const Vector& getNodeCosts(NodeId NId) const {
490 return *getNodeCostsPtr(NId);
493 NodeMetadata& getNodeMetadata(NodeId NId) {
494 return getNode(NId).Metadata;
497 const NodeMetadata& getNodeMetadata(NodeId NId) const {
498 return getNode(NId).Metadata;
501 typename NodeEntry::AdjEdgeList::size_type getNodeDegree(NodeId NId) const {
502 return getNode(NId).getAdjEdgeIds().size();
505 /// @brief Update an edge's cost matrix.
506 /// @param EId Edge id.
507 /// @param Costs New cost matrix.
508 template <typename OtherMatrixT>
509 void updateEdgeCosts(EdgeId EId, OtherMatrixT Costs) {
510 MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
512 Solver->handleUpdateCosts(EId, *AllocatedCosts);
513 getEdge(EId).Costs = AllocatedCosts;
516 /// @brief Get a MatrixPtr to a node's cost matrix. Rarely useful - use
517 /// getEdgeCosts where possible.
518 /// @param EId Edge id.
519 /// @return MatrixPtr to edge cost matrix.
521 /// This method is primarily useful for duplicating costs quickly by
522 /// bypassing the cost allocator. See addNodeBypassingCostAllocator. Prefer
523 /// getEdgeCosts when dealing with edge cost values.
524 const MatrixPtr& getEdgeCostsPtr(EdgeId EId) const {
525 return getEdge(EId).Costs;
528 /// @brief Get an edge's cost matrix.
529 /// @param EId Edge id.
530 /// @return Edge cost matrix.
531 const Matrix& getEdgeCosts(EdgeId EId) const {
532 return *getEdge(EId).Costs;
535 EdgeMetadata& getEdgeMetadata(EdgeId EId) {
536 return getEdge(EId).Metadata;
539 const EdgeMetadata& getEdgeMetadata(EdgeId EId) const {
540 return getEdge(EId).Metadata;
543 /// @brief Get the first node connected to this edge.
544 /// @param EId Edge id.
545 /// @return The first node connected to the given edge.
546 NodeId getEdgeNode1Id(EdgeId EId) const {
547 return getEdge(EId).getN1Id();
550 /// @brief Get the second node connected to this edge.
551 /// @param EId Edge id.
552 /// @return The second node connected to the given edge.
553 NodeId getEdgeNode2Id(EdgeId EId) const {
554 return getEdge(EId).getN2Id();
557 /// @brief Get the "other" node connected to this edge.
558 /// @param EId Edge id.
559 /// @param NId Node id for the "given" node.
560 /// @return The iterator for the "other" node connected to this edge.
561 NodeId getEdgeOtherNodeId(EdgeId EId, NodeId NId) {
562 EdgeEntry &E = getEdge(EId);
563 if (E.getN1Id() == NId) {
569 /// @brief Get the edge connecting two nodes.
570 /// @param N1Id First node id.
571 /// @param N2Id Second node id.
572 /// @return An id for edge (N1Id, N2Id) if such an edge exists,
573 /// otherwise returns an invalid edge id.
574 EdgeId findEdge(NodeId N1Id, NodeId N2Id) {
575 for (auto AEId : adjEdgeIds(N1Id)) {
576 if ((getEdgeNode1Id(AEId) == N2Id) ||
577 (getEdgeNode2Id(AEId) == N2Id)) {
581 return invalidEdgeId();
584 /// @brief Remove a node from the graph.
585 /// @param NId Node id.
586 void removeNode(NodeId NId) {
588 Solver->handleRemoveNode(NId);
589 NodeEntry &N = getNode(NId);
590 // TODO: Can this be for-each'd?
591 for (AdjEdgeItr AEItr = N.adjEdgesBegin(),
592 AEEnd = N.adjEdgesEnd();
598 FreeNodeIds.push_back(NId);
601 /// @brief Disconnect an edge from the given node.
603 /// Removes the given edge from the adjacency list of the given node.
604 /// This operation leaves the edge in an 'asymmetric' state: It will no
605 /// longer appear in an iteration over the given node's (NId's) edges, but
606 /// will appear in an iteration over the 'other', unnamed node's edges.
608 /// This does not correspond to any normal graph operation, but exists to
609 /// support efficient PBQP graph-reduction based solvers. It is used to
610 /// 'effectively' remove the unnamed node from the graph while the solver
611 /// is performing the reduction. The solver will later call reconnectNode
612 /// to restore the edge in the named node's adjacency list.
614 /// Since the degree of a node is the number of connected edges,
615 /// disconnecting an edge from a node 'u' will cause the degree of 'u' to
618 /// A disconnected edge WILL still appear in an iteration over the graph
621 /// A disconnected edge should not be removed from the graph, it should be
622 /// reconnected first.
624 /// A disconnected edge can be reconnected by calling the reconnectEdge
626 void disconnectEdge(EdgeId EId, NodeId NId) {
628 Solver->handleDisconnectEdge(EId, NId);
630 EdgeEntry &E = getEdge(EId);
631 E.disconnectFrom(*this, NId);
634 /// @brief Convenience method to disconnect all neighbours from the given
636 void disconnectAllNeighborsFromNode(NodeId NId) {
637 for (auto AEId : adjEdgeIds(NId))
638 disconnectEdge(AEId, getEdgeOtherNodeId(AEId, NId));
641 /// @brief Re-attach an edge to its nodes.
643 /// Adds an edge that had been previously disconnected back into the
644 /// adjacency set of the nodes that the edge connects.
645 void reconnectEdge(EdgeId EId, NodeId NId) {
646 EdgeEntry &E = getEdge(EId);
647 E.connectTo(*this, EId, NId);
649 Solver->handleReconnectEdge(EId, NId);
652 /// @brief Remove an edge from the graph.
653 /// @param EId Edge id.
654 void removeEdge(EdgeId EId) {
656 Solver->handleRemoveEdge(EId);
657 EdgeEntry &E = getEdge(EId);
659 FreeEdgeIds.push_back(EId);
660 Edges[EId].invalidate();
663 /// @brief Remove all nodes and edges from the graph.
672 } // end namespace PBQP
673 } // end namespace llvm
675 #endif // LLVM_CODEGEN_PBQP_GRAPH_HPP