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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_PBQP_GRAPH_H
16 #define LLVM_CODEGEN_PBQP_GRAPH_H
18 #include "llvm/ADT/ilist.h"
19 #include "llvm/ADT/ilist_node.h"
20 #include "llvm/Support/Compiler.h"
29 typedef unsigned NodeId;
30 typedef unsigned EdgeId;
34 /// Instances of this class describe PBQP problems.
36 template <typename SolverT>
37 class Graph : public GraphBase {
39 typedef typename SolverT::CostAllocator CostAllocator;
41 typedef typename SolverT::RawVector RawVector;
42 typedef typename SolverT::RawMatrix RawMatrix;
43 typedef typename SolverT::Vector Vector;
44 typedef typename SolverT::Matrix Matrix;
45 typedef typename CostAllocator::VectorPtr VectorPtr;
46 typedef typename CostAllocator::MatrixPtr MatrixPtr;
47 typedef typename SolverT::NodeMetadata NodeMetadata;
48 typedef typename SolverT::EdgeMetadata EdgeMetadata;
54 typedef std::vector<EdgeId> AdjEdgeList;
55 typedef AdjEdgeList::size_type AdjEdgeIdx;
56 typedef AdjEdgeList::const_iterator AdjEdgeItr;
58 static AdjEdgeIdx getInvalidAdjEdgeIdx() {
59 return std::numeric_limits<AdjEdgeIdx>::max();
62 NodeEntry(VectorPtr Costs) : Costs(Costs) {}
64 AdjEdgeIdx addAdjEdgeId(EdgeId EId) {
65 AdjEdgeIdx Idx = AdjEdgeIds.size();
66 AdjEdgeIds.push_back(EId);
70 void removeAdjEdgeId(Graph &G, NodeId ThisNId, AdjEdgeIdx Idx) {
71 // Swap-and-pop for fast removal.
72 // 1) Update the adj index of the edge currently at back().
73 // 2) Swap Edge at Idx with back().
75 // If Idx == size() - 1 then the updateAdjEdgeIdx and swap are
76 // redundant, but both operations are cheap.
77 G.getEdge(AdjEdgeIds.back()).updateAdjEdgeIdx(ThisNId, Idx);
78 std::swap(AdjEdgeIds[Idx], AdjEdgeIds.back());
79 AdjEdgeIds.pop_back();
82 const AdjEdgeList& getAdjEdgeIds() const { return AdjEdgeIds; }
85 NodeMetadata Metadata;
87 AdjEdgeList AdjEdgeIds;
92 EdgeEntry(NodeId N1Id, NodeId N2Id, MatrixPtr Costs)
96 ThisEdgeAdjIdxs[0] = NodeEntry::getInvalidAdjEdgeIdx();
97 ThisEdgeAdjIdxs[1] = NodeEntry::getInvalidAdjEdgeIdx();
101 NIds[0] = NIds[1] = Graph::invalidNodeId();
102 ThisEdgeAdjIdxs[0] = ThisEdgeAdjIdxs[1] =
103 NodeEntry::getInvalidAdjEdgeIdx();
107 void connectToN(Graph &G, EdgeId ThisEdgeId, unsigned NIdx) {
108 assert(ThisEdgeAdjIdxs[NIdx] == NodeEntry::getInvalidAdjEdgeIdx() &&
109 "Edge already connected to NIds[NIdx].");
110 NodeEntry &N = G.getNode(NIds[NIdx]);
111 ThisEdgeAdjIdxs[NIdx] = N.addAdjEdgeId(ThisEdgeId);
114 void connectTo(Graph &G, EdgeId ThisEdgeId, NodeId NId) {
116 connectToN(G, ThisEdgeId, 0);
118 assert(NId == NIds[1] && "Edge does not connect NId.");
119 connectToN(G, ThisEdgeId, 1);
123 void connect(Graph &G, EdgeId ThisEdgeId) {
124 connectToN(G, ThisEdgeId, 0);
125 connectToN(G, ThisEdgeId, 1);
128 void updateAdjEdgeIdx(NodeId NId, typename NodeEntry::AdjEdgeIdx NewIdx) {
130 ThisEdgeAdjIdxs[0] = NewIdx;
132 assert(NId == NIds[1] && "Edge not connected to NId");
133 ThisEdgeAdjIdxs[1] = NewIdx;
137 void disconnectFromN(Graph &G, unsigned NIdx) {
138 assert(ThisEdgeAdjIdxs[NIdx] != NodeEntry::getInvalidAdjEdgeIdx() &&
139 "Edge not connected to NIds[NIdx].");
140 NodeEntry &N = G.getNode(NIds[NIdx]);
141 N.removeAdjEdgeId(G, NIds[NIdx], ThisEdgeAdjIdxs[NIdx]);
142 ThisEdgeAdjIdxs[NIdx] = NodeEntry::getInvalidAdjEdgeIdx();
145 void disconnectFrom(Graph &G, NodeId NId) {
147 disconnectFromN(G, 0);
149 assert(NId == NIds[1] && "Edge does not connect NId");
150 disconnectFromN(G, 1);
154 NodeId getN1Id() const { return NIds[0]; }
155 NodeId getN2Id() const { return NIds[1]; }
157 EdgeMetadata Metadata;
160 typename NodeEntry::AdjEdgeIdx ThisEdgeAdjIdxs[2];
163 // ----- MEMBERS -----
165 CostAllocator CostAlloc;
168 typedef std::vector<NodeEntry> NodeVector;
169 typedef std::vector<NodeId> FreeNodeVector;
171 FreeNodeVector FreeNodeIds;
173 typedef std::vector<EdgeEntry> EdgeVector;
174 typedef std::vector<EdgeId> FreeEdgeVector;
176 FreeEdgeVector FreeEdgeIds;
178 // ----- INTERNAL METHODS -----
180 NodeEntry& getNode(NodeId NId) { return Nodes[NId]; }
181 const NodeEntry& getNode(NodeId NId) const { return Nodes[NId]; }
183 EdgeEntry& getEdge(EdgeId EId) { return Edges[EId]; }
184 const EdgeEntry& getEdge(EdgeId EId) const { return Edges[EId]; }
186 NodeId addConstructedNode(const NodeEntry &N) {
188 if (!FreeNodeIds.empty()) {
189 NId = FreeNodeIds.back();
190 FreeNodeIds.pop_back();
191 Nodes[NId] = std::move(N);
194 Nodes.push_back(std::move(N));
199 EdgeId addConstructedEdge(const EdgeEntry &E) {
200 assert(findEdge(E.getN1Id(), E.getN2Id()) == invalidEdgeId() &&
201 "Attempt to add duplicate edge.");
203 if (!FreeEdgeIds.empty()) {
204 EId = FreeEdgeIds.back();
205 FreeEdgeIds.pop_back();
206 Edges[EId] = std::move(E);
209 Edges.push_back(std::move(E));
212 EdgeEntry &NE = getEdge(EId);
214 // Add the edge to the adjacency sets of its nodes.
215 NE.connect(*this, EId);
219 Graph(const Graph &Other) {}
220 void operator=(const Graph &Other) {}
224 typedef typename NodeEntry::AdjEdgeItr AdjEdgeItr;
228 NodeItr(NodeId CurNId, const Graph &G)
229 : CurNId(CurNId), EndNId(G.Nodes.size()), FreeNodeIds(G.FreeNodeIds) {
230 this->CurNId = findNextInUse(CurNId); // Move to first in-use node id
233 bool operator==(const NodeItr &O) const { return CurNId == O.CurNId; }
234 bool operator!=(const NodeItr &O) const { return !(*this == O); }
235 NodeItr& operator++() { CurNId = findNextInUse(++CurNId); return *this; }
236 NodeId operator*() const { return CurNId; }
239 NodeId findNextInUse(NodeId NId) const {
240 while (NId < EndNId &&
241 std::find(FreeNodeIds.begin(), FreeNodeIds.end(), NId) !=
248 NodeId CurNId, EndNId;
249 const FreeNodeVector &FreeNodeIds;
254 EdgeItr(EdgeId CurEId, const Graph &G)
255 : CurEId(CurEId), EndEId(G.Edges.size()), FreeEdgeIds(G.FreeEdgeIds) {
256 this->CurEId = findNextInUse(CurEId); // Move to first in-use edge id
259 bool operator==(const EdgeItr &O) const { return CurEId == O.CurEId; }
260 bool operator!=(const EdgeItr &O) const { return !(*this == O); }
261 EdgeItr& operator++() { CurEId = findNextInUse(++CurEId); return *this; }
262 EdgeId operator*() const { return CurEId; }
265 EdgeId findNextInUse(EdgeId EId) const {
266 while (EId < EndEId &&
267 std::find(FreeEdgeIds.begin(), FreeEdgeIds.end(), EId) !=
274 EdgeId CurEId, EndEId;
275 const FreeEdgeVector &FreeEdgeIds;
280 NodeIdSet(const Graph &G) : G(G) { }
281 NodeItr begin() const { return NodeItr(0, G); }
282 NodeItr end() const { return NodeItr(G.Nodes.size(), G); }
283 bool empty() const { return G.Nodes.empty(); }
284 typename NodeVector::size_type size() const {
285 return G.Nodes.size() - G.FreeNodeIds.size();
293 EdgeIdSet(const Graph &G) : G(G) { }
294 EdgeItr begin() const { return EdgeItr(0, G); }
295 EdgeItr end() const { return EdgeItr(G.Edges.size(), G); }
296 bool empty() const { return G.Edges.empty(); }
297 typename NodeVector::size_type size() const {
298 return G.Edges.size() - G.FreeEdgeIds.size();
306 AdjEdgeIdSet(const NodeEntry &NE) : NE(NE) { }
307 typename NodeEntry::AdjEdgeItr begin() const {
308 return NE.getAdjEdgeIds().begin();
310 typename NodeEntry::AdjEdgeItr end() const {
311 return NE.getAdjEdgeIds().end();
313 bool empty() const { return NE.getAdjEdgeIds().empty(); }
314 typename NodeEntry::AdjEdgeList::size_type size() const {
315 return NE.getAdjEdgeIds().size();
321 /// \brief Construct an empty PBQP graph.
322 Graph() : Solver(nullptr) { }
324 /// \brief Lock this graph to the given solver instance in preparation
325 /// for running the solver. This method will call solver.handleAddNode for
326 /// each node in the graph, and handleAddEdge for each edge, to give the
327 /// solver an opportunity to set up any requried metadata.
328 void setSolver(SolverT &S) {
329 assert(Solver == nullptr && "Solver already set. Call unsetSolver().");
331 for (auto NId : nodeIds())
332 Solver->handleAddNode(NId);
333 for (auto EId : edgeIds())
334 Solver->handleAddEdge(EId);
337 /// \brief Release from solver instance.
339 assert(Solver != nullptr && "Solver not set.");
343 /// \brief Add a node with the given costs.
344 /// @param Costs Cost vector for the new node.
345 /// @return Node iterator for the added node.
346 template <typename OtherVectorT>
347 NodeId addNode(OtherVectorT Costs) {
348 // Get cost vector from the problem domain
349 VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
350 NodeId NId = addConstructedNode(NodeEntry(AllocatedCosts));
352 Solver->handleAddNode(NId);
356 /// \brief Add an edge between the given nodes with the given costs.
357 /// @param N1Id First node.
358 /// @param N2Id Second node.
359 /// @return Edge iterator for the added edge.
360 template <typename OtherVectorT>
361 EdgeId addEdge(NodeId N1Id, NodeId N2Id, OtherVectorT Costs) {
362 assert(getNodeCosts(N1Id).getLength() == Costs.getRows() &&
363 getNodeCosts(N2Id).getLength() == Costs.getCols() &&
364 "Matrix dimensions mismatch.");
365 // Get cost matrix from the problem domain.
366 MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
367 EdgeId EId = addConstructedEdge(EdgeEntry(N1Id, N2Id, AllocatedCosts));
369 Solver->handleAddEdge(EId);
373 /// \brief Returns true if the graph is empty.
374 bool empty() const { return NodeIdSet(*this).empty(); }
376 NodeIdSet nodeIds() const { return NodeIdSet(*this); }
377 EdgeIdSet edgeIds() const { return EdgeIdSet(*this); }
379 AdjEdgeIdSet adjEdgeIds(NodeId NId) { return AdjEdgeIdSet(getNode(NId)); }
381 /// \brief Get the number of nodes in the graph.
382 /// @return Number of nodes in the graph.
383 unsigned getNumNodes() const { return NodeIdSet(*this).size(); }
385 /// \brief Get the number of edges in the graph.
386 /// @return Number of edges in the graph.
387 unsigned getNumEdges() const { return EdgeIdSet(*this).size(); }
389 /// \brief Set a node's cost vector.
390 /// @param NId Node to update.
391 /// @param Costs New costs to set.
392 template <typename OtherVectorT>
393 void setNodeCosts(NodeId NId, OtherVectorT Costs) {
394 VectorPtr AllocatedCosts = CostAlloc.getVector(std::move(Costs));
396 Solver->handleSetNodeCosts(NId, *AllocatedCosts);
397 getNode(NId).Costs = AllocatedCosts;
400 /// \brief Get a node's cost vector (const version).
401 /// @param NId Node id.
402 /// @return Node cost vector.
403 const Vector& getNodeCosts(NodeId NId) const {
404 return *getNode(NId).Costs;
407 NodeMetadata& getNodeMetadata(NodeId NId) {
408 return getNode(NId).Metadata;
411 const NodeMetadata& getNodeMetadata(NodeId NId) const {
412 return getNode(NId).Metadata;
415 typename NodeEntry::AdjEdgeList::size_type getNodeDegree(NodeId NId) const {
416 return getNode(NId).getAdjEdgeIds().size();
419 /// \brief Set an edge's cost matrix.
420 /// @param EId Edge id.
421 /// @param Costs New cost matrix.
422 template <typename OtherMatrixT>
423 void setEdgeCosts(EdgeId EId, OtherMatrixT Costs) {
424 MatrixPtr AllocatedCosts = CostAlloc.getMatrix(std::move(Costs));
426 Solver->handleSetEdgeCosts(EId, *AllocatedCosts);
427 getEdge(EId).Costs = AllocatedCosts;
430 /// \brief Get an edge's cost matrix (const version).
431 /// @param EId Edge id.
432 /// @return Edge cost matrix.
433 const Matrix& getEdgeCosts(EdgeId EId) const { return *getEdge(EId).Costs; }
435 EdgeMetadata& getEdgeMetadata(EdgeId NId) {
436 return getEdge(NId).Metadata;
439 const EdgeMetadata& getEdgeMetadata(EdgeId NId) const {
440 return getEdge(NId).Metadata;
443 /// \brief Get the first node connected to this edge.
444 /// @param EId Edge id.
445 /// @return The first node connected to the given edge.
446 NodeId getEdgeNode1Id(EdgeId EId) {
447 return getEdge(EId).getN1Id();
450 /// \brief Get the second node connected to this edge.
451 /// @param EId Edge id.
452 /// @return The second node connected to the given edge.
453 NodeId getEdgeNode2Id(EdgeId EId) {
454 return getEdge(EId).getN2Id();
457 /// \brief Get the "other" node connected to this edge.
458 /// @param EId Edge id.
459 /// @param NId Node id for the "given" node.
460 /// @return The iterator for the "other" node connected to this edge.
461 NodeId getEdgeOtherNodeId(EdgeId EId, NodeId NId) {
462 EdgeEntry &E = getEdge(EId);
463 if (E.getN1Id() == NId) {
469 /// \brief Returns a value representing an invalid (non-existant) node.
470 static NodeId invalidNodeId() {
471 return std::numeric_limits<NodeId>::max();
474 /// \brief Returns a value representing an invalid (non-existant) edge.
475 static EdgeId invalidEdgeId() {
476 return std::numeric_limits<EdgeId>::max();
479 /// \brief Get the edge connecting two nodes.
480 /// @param N1Id First node id.
481 /// @param N2Id Second node id.
482 /// @return An id for edge (N1Id, N2Id) if such an edge exists,
483 /// otherwise returns an invalid edge id.
484 EdgeId findEdge(NodeId N1Id, NodeId N2Id) {
485 for (auto AEId : adjEdgeIds(N1Id)) {
486 if ((getEdgeNode1Id(AEId) == N2Id) ||
487 (getEdgeNode2Id(AEId) == N2Id)) {
491 return invalidEdgeId();
494 /// \brief Remove a node from the graph.
495 /// @param NId Node id.
496 void removeNode(NodeId NId) {
498 Solver->handleRemoveNode(NId);
499 NodeEntry &N = getNode(NId);
500 // TODO: Can this be for-each'd?
501 for (AdjEdgeItr AEItr = N.adjEdgesBegin(),
502 AEEnd = N.adjEdgesEnd();
508 FreeNodeIds.push_back(NId);
511 /// \brief Disconnect an edge from the given node.
513 /// Removes the given edge from the adjacency list of the given node.
514 /// This operation leaves the edge in an 'asymmetric' state: It will no
515 /// longer appear in an iteration over the given node's (NId's) edges, but
516 /// will appear in an iteration over the 'other', unnamed node's edges.
518 /// This does not correspond to any normal graph operation, but exists to
519 /// support efficient PBQP graph-reduction based solvers. It is used to
520 /// 'effectively' remove the unnamed node from the graph while the solver
521 /// is performing the reduction. The solver will later call reconnectNode
522 /// to restore the edge in the named node's adjacency list.
524 /// Since the degree of a node is the number of connected edges,
525 /// disconnecting an edge from a node 'u' will cause the degree of 'u' to
528 /// A disconnected edge WILL still appear in an iteration over the graph
531 /// A disconnected edge should not be removed from the graph, it should be
532 /// reconnected first.
534 /// A disconnected edge can be reconnected by calling the reconnectEdge
536 void disconnectEdge(EdgeId EId, NodeId NId) {
538 Solver->handleDisconnectEdge(EId, NId);
540 EdgeEntry &E = getEdge(EId);
541 E.disconnectFrom(*this, NId);
544 /// \brief Convenience method to disconnect all neighbours from the given
546 void disconnectAllNeighborsFromNode(NodeId NId) {
547 for (auto AEId : adjEdgeIds(NId))
548 disconnectEdge(AEId, getEdgeOtherNodeId(AEId, NId));
551 /// \brief Re-attach an edge to its nodes.
553 /// Adds an edge that had been previously disconnected back into the
554 /// adjacency set of the nodes that the edge connects.
555 void reconnectEdge(EdgeId EId, NodeId NId) {
556 EdgeEntry &E = getEdge(EId);
557 E.connectTo(*this, EId, NId);
559 Solver->handleReconnectEdge(EId, NId);
562 /// \brief Remove an edge from the graph.
563 /// @param EId Edge id.
564 void removeEdge(EdgeId EId) {
566 Solver->handleRemoveEdge(EId);
567 EdgeEntry &E = getEdge(EId);
569 FreeEdgeIds.push_back(EId);
570 Edges[EId].invalidate();
573 /// \brief Remove all nodes and edges from the graph.
581 /// \brief Dump a graph to an output stream.
582 template <typename OStream>
583 void dump(OStream &OS) {
584 OS << nodeIds().size() << " " << edgeIds().size() << "\n";
586 for (auto NId : nodeIds()) {
587 const Vector& V = getNodeCosts(NId);
588 OS << "\n" << V.getLength() << "\n";
589 assert(V.getLength() != 0 && "Empty vector in graph.");
591 for (unsigned i = 1; i < V.getLength(); ++i) {
597 for (auto EId : edgeIds()) {
598 NodeId N1Id = getEdgeNode1Id(EId);
599 NodeId N2Id = getEdgeNode2Id(EId);
600 assert(N1Id != N2Id && "PBQP graphs shound not have self-edges.");
601 const Matrix& M = getEdgeCosts(EId);
602 OS << "\n" << N1Id << " " << N2Id << "\n"
603 << M.getRows() << " " << M.getCols() << "\n";
604 assert(M.getRows() != 0 && "No rows in matrix.");
605 assert(M.getCols() != 0 && "No cols in matrix.");
606 for (unsigned i = 0; i < M.getRows(); ++i) {
608 for (unsigned j = 1; j < M.getCols(); ++j) {
609 OS << " " << M[i][j];
616 /// \brief Print a representation of this graph in DOT format.
617 /// @param OS Output stream to print on.
618 template <typename OStream>
619 void printDot(OStream &OS) {
621 for (auto NId : nodeIds()) {
622 OS << " node" << NId << " [ label=\""
623 << NId << ": " << getNodeCosts(NId) << "\" ]\n";
625 OS << " edge [ len=" << nodeIds().size() << " ]\n";
626 for (auto EId : edgeIds()) {
627 OS << " node" << getEdgeNode1Id(EId)
628 << " -- node" << getEdgeNode2Id(EId)
630 const Matrix &EdgeCosts = getEdgeCosts(EId);
631 for (unsigned i = 0; i < EdgeCosts.getRows(); ++i) {
632 OS << EdgeCosts.getRowAsVector(i) << "\\n";
642 #endif // LLVM_CODEGEN_PBQP_GRAPH_HPP