2010-01-26 04:49:58 +00:00
|
|
|
//===-------------------- Graph.h - PBQP Graph ------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// PBQP Graph class.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_PBQP_GRAPH_H
|
|
|
|
#define LLVM_CODEGEN_PBQP_GRAPH_H
|
|
|
|
|
|
|
|
#include "Math.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/ilist.h"
|
|
|
|
#include "llvm/ADT/ilist_node.h"
|
2010-01-26 04:49:58 +00:00
|
|
|
#include <list>
|
2010-02-09 00:45:48 +00:00
|
|
|
#include <map>
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
namespace PBQP {
|
|
|
|
|
|
|
|
/// PBQP Graph class.
|
|
|
|
/// Instances of this class describe PBQP problems.
|
|
|
|
class Graph {
|
2013-11-09 02:01:25 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
// ----- TYPEDEFS -----
|
|
|
|
class NodeEntry;
|
|
|
|
class EdgeEntry;
|
|
|
|
|
|
|
|
typedef llvm::ilist<NodeEntry> NodeList;
|
|
|
|
typedef llvm::ilist<EdgeEntry> EdgeList;
|
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
public:
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
typedef NodeList::iterator NodeItr;
|
|
|
|
typedef NodeList::const_iterator ConstNodeItr;
|
2010-01-26 04:49:58 +00:00
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
typedef EdgeList::iterator EdgeItr;
|
|
|
|
typedef EdgeList::const_iterator ConstEdgeItr;
|
2010-01-26 04:49:58 +00:00
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
private:
|
Rewrite the PBQP graph data structure.
The new graph structure replaces the node and edge linked lists with vectors.
Free lists (well, free vectors) are used for fast insertion/deletion.
The ultimate aim is to make PBQP graphs cheap to clone. The motivation is that
the PBQP solver destructively consumes input graphs while computing a solution,
forcing the graph to be fully reconstructed for each round of PBQP. This
imposes a high cost on large functions, which often require several rounds of
solving/spilling to find a final register allocation. If we can cheaply clone
the PBQP graph and incrementally update it between rounds then hopefully we can
reduce this cost. Further, once we begin pooling matrix/vector values (future
work), we can cache some PBQP solver metadata and share it between cloned
graphs, allowing the PBQP solver to re-use some of the computation done in
earlier rounds.
For now this is just a data structure update. The allocator and solver still
use the graph the same way as before, fully reconstructing it between each
round. I expect no material change from this update, although it may change
the iteration order of the nodes, causing ties in the solver to break in
different directions, and this could perturb the generated allocations
(hopefully in a completely benign way).
Thanks very much to Arnaud Allard de Grandmaison for encouraging me to get back
to work on this, and for a lot of discussion and many useful PBQP test cases.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194300 91177308-0d34-0410-b5e6-96231b3b80d8
2013-11-09 00:14:07 +00:00
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
typedef std::list<EdgeItr> AdjEdgeList;
|
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
typedef AdjEdgeList::iterator AdjEdgeItr;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
class NodeEntry : public llvm::ilist_node<NodeEntry> {
|
|
|
|
friend struct llvm::ilist_sentinel_traits<NodeEntry>;
|
2010-01-26 04:49:58 +00:00
|
|
|
private:
|
|
|
|
Vector costs;
|
|
|
|
AdjEdgeList adjEdges;
|
2013-11-09 02:01:25 +00:00
|
|
|
unsigned degree;
|
2010-01-26 04:49:58 +00:00
|
|
|
void *data;
|
2012-10-23 17:10:51 +00:00
|
|
|
NodeEntry() : costs(0, 0) {}
|
2010-01-26 04:49:58 +00:00
|
|
|
public:
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeEntry(const Vector &costs) : costs(costs), degree(0) {}
|
2010-01-26 04:49:58 +00:00
|
|
|
Vector& getCosts() { return costs; }
|
2010-02-09 00:45:48 +00:00
|
|
|
const Vector& getCosts() const { return costs; }
|
2013-11-09 02:01:25 +00:00
|
|
|
unsigned getDegree() const { return degree; }
|
2010-01-26 04:49:58 +00:00
|
|
|
AdjEdgeItr edgesBegin() { return adjEdges.begin(); }
|
|
|
|
AdjEdgeItr edgesEnd() { return adjEdges.end(); }
|
2013-11-09 02:01:25 +00:00
|
|
|
AdjEdgeItr addEdge(EdgeItr e) {
|
|
|
|
++degree;
|
2010-01-26 04:49:58 +00:00
|
|
|
return adjEdges.insert(adjEdges.end(), e);
|
|
|
|
}
|
|
|
|
void removeEdge(AdjEdgeItr ae) {
|
2013-11-09 02:01:25 +00:00
|
|
|
--degree;
|
2010-01-26 04:49:58 +00:00
|
|
|
adjEdges.erase(ae);
|
|
|
|
}
|
|
|
|
void setData(void *data) { this->data = data; }
|
|
|
|
void* getData() { return data; }
|
|
|
|
};
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
class EdgeEntry : public llvm::ilist_node<EdgeEntry> {
|
|
|
|
friend struct llvm::ilist_sentinel_traits<EdgeEntry>;
|
2010-01-26 04:49:58 +00:00
|
|
|
private:
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr node1, node2;
|
2010-01-26 04:49:58 +00:00
|
|
|
Matrix costs;
|
|
|
|
AdjEdgeItr node1AEItr, node2AEItr;
|
|
|
|
void *data;
|
2013-11-09 02:01:25 +00:00
|
|
|
EdgeEntry() : costs(0, 0, 0) {}
|
2010-01-26 04:49:58 +00:00
|
|
|
public:
|
2013-11-09 02:01:25 +00:00
|
|
|
EdgeEntry(NodeItr node1, NodeItr node2, const Matrix &costs)
|
2010-01-26 04:49:58 +00:00
|
|
|
: node1(node1), node2(node2), costs(costs) {}
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr getNode1() const { return node1; }
|
|
|
|
NodeItr getNode2() const { return node2; }
|
2010-01-26 04:49:58 +00:00
|
|
|
Matrix& getCosts() { return costs; }
|
2010-02-09 00:45:48 +00:00
|
|
|
const Matrix& getCosts() const { return costs; }
|
2010-01-26 04:49:58 +00:00
|
|
|
void setNode1AEItr(AdjEdgeItr ae) { node1AEItr = ae; }
|
|
|
|
AdjEdgeItr getNode1AEItr() { return node1AEItr; }
|
|
|
|
void setNode2AEItr(AdjEdgeItr ae) { node2AEItr = ae; }
|
|
|
|
AdjEdgeItr getNode2AEItr() { return node2AEItr; }
|
|
|
|
void setData(void *data) { this->data = data; }
|
|
|
|
void *getData() { return data; }
|
|
|
|
};
|
|
|
|
|
|
|
|
// ----- MEMBERS -----
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeList nodes;
|
|
|
|
unsigned numNodes;
|
2010-01-26 04:49:58 +00:00
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
EdgeList edges;
|
|
|
|
unsigned numEdges;
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
// ----- INTERNAL METHODS -----
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeEntry& getNode(NodeItr nItr) { return *nItr; }
|
|
|
|
const NodeEntry& getNode(ConstNodeItr nItr) const { return *nItr; }
|
|
|
|
|
|
|
|
EdgeEntry& getEdge(EdgeItr eItr) { return *eItr; }
|
|
|
|
const EdgeEntry& getEdge(ConstEdgeItr eItr) const { return *eItr; }
|
|
|
|
|
|
|
|
NodeItr addConstructedNode(const NodeEntry &n) {
|
|
|
|
++numNodes;
|
|
|
|
return nodes.insert(nodes.end(), n);
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
EdgeItr addConstructedEdge(const EdgeEntry &e) {
|
|
|
|
assert(findEdge(e.getNode1(), e.getNode2()) == edges.end() &&
|
2010-01-26 04:49:58 +00:00
|
|
|
"Attempt to add duplicate edge.");
|
2013-11-09 02:01:25 +00:00
|
|
|
++numEdges;
|
|
|
|
EdgeItr edgeItr = edges.insert(edges.end(), e);
|
|
|
|
EdgeEntry &ne = getEdge(edgeItr);
|
2010-01-26 04:49:58 +00:00
|
|
|
NodeEntry &n1 = getNode(ne.getNode1());
|
|
|
|
NodeEntry &n2 = getNode(ne.getNode2());
|
|
|
|
// Sanity check on matrix dimensions:
|
|
|
|
assert((n1.getCosts().getLength() == ne.getCosts().getRows()) &&
|
|
|
|
(n2.getCosts().getLength() == ne.getCosts().getCols()) &&
|
|
|
|
"Edge cost dimensions do not match node costs dimensions.");
|
2013-11-09 02:01:25 +00:00
|
|
|
ne.setNode1AEItr(n1.addEdge(edgeItr));
|
|
|
|
ne.setNode2AEItr(n2.addEdge(edgeItr));
|
|
|
|
return edgeItr;
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
2010-02-09 00:45:48 +00:00
|
|
|
inline void copyFrom(const Graph &other);
|
2010-01-26 04:49:58 +00:00
|
|
|
public:
|
|
|
|
|
2010-02-09 00:45:48 +00:00
|
|
|
/// \brief Construct an empty PBQP graph.
|
2013-11-09 02:01:25 +00:00
|
|
|
Graph() : numNodes(0), numEdges(0) {}
|
2010-01-26 04:49:58 +00:00
|
|
|
|
2010-02-09 00:45:48 +00:00
|
|
|
/// \brief Copy construct this graph from "other". Note: Does not copy node
|
|
|
|
/// and edge data, only graph structure and costs.
|
|
|
|
/// @param other Source graph to copy from.
|
2013-11-09 02:01:25 +00:00
|
|
|
Graph(const Graph &other) : numNodes(0), numEdges(0) {
|
2010-02-09 00:45:48 +00:00
|
|
|
copyFrom(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Make this graph a copy of "other". Note: Does not copy node and
|
|
|
|
/// edge data, only graph structure and costs.
|
|
|
|
/// @param other The graph to copy from.
|
|
|
|
/// @return A reference to this graph.
|
|
|
|
///
|
|
|
|
/// This will clear the current graph, erasing any nodes and edges added,
|
|
|
|
/// before copying from other.
|
|
|
|
Graph& operator=(const Graph &other) {
|
|
|
|
clear();
|
|
|
|
copyFrom(other);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
/// \brief Add a node with the given costs.
|
|
|
|
/// @param costs Cost vector for the new node.
|
|
|
|
/// @return Node iterator for the added node.
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr addNode(const Vector &costs) {
|
2010-01-26 04:49:58 +00:00
|
|
|
return addConstructedNode(NodeEntry(costs));
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Add an edge between the given nodes with the given costs.
|
|
|
|
/// @param n1Itr First node.
|
|
|
|
/// @param n2Itr Second node.
|
|
|
|
/// @return Edge iterator for the added edge.
|
2013-11-09 02:01:25 +00:00
|
|
|
EdgeItr addEdge(Graph::NodeItr n1Itr, Graph::NodeItr n2Itr,
|
|
|
|
const Matrix &costs) {
|
|
|
|
assert(getNodeCosts(n1Itr).getLength() == costs.getRows() &&
|
|
|
|
getNodeCosts(n2Itr).getLength() == costs.getCols() &&
|
2010-01-26 04:49:58 +00:00
|
|
|
"Matrix dimensions mismatch.");
|
2013-11-09 02:01:25 +00:00
|
|
|
return addConstructedEdge(EdgeEntry(n1Itr, n2Itr, costs));
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the number of nodes in the graph.
|
|
|
|
/// @return Number of nodes in the graph.
|
2013-11-09 02:01:25 +00:00
|
|
|
unsigned getNumNodes() const { return numNodes; }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief Get the number of edges in the graph.
|
|
|
|
/// @return Number of edges in the graph.
|
2013-11-09 02:01:25 +00:00
|
|
|
unsigned getNumEdges() const { return numEdges; }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief Get a node's cost vector.
|
|
|
|
/// @param nItr Node iterator.
|
|
|
|
/// @return Node cost vector.
|
2013-11-09 02:01:25 +00:00
|
|
|
Vector& getNodeCosts(NodeItr nItr) { return getNode(nItr).getCosts(); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
2010-02-09 00:45:48 +00:00
|
|
|
/// \brief Get a node's cost vector (const version).
|
|
|
|
/// @param nItr Node iterator.
|
|
|
|
/// @return Node cost vector.
|
2013-11-09 02:01:25 +00:00
|
|
|
const Vector& getNodeCosts(ConstNodeItr nItr) const {
|
|
|
|
return getNode(nItr).getCosts();
|
2010-02-09 00:45:48 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
/// \brief Set a node's data pointer.
|
|
|
|
/// @param nItr Node iterator.
|
|
|
|
/// @param data Pointer to node data.
|
|
|
|
///
|
|
|
|
/// Typically used by a PBQP solver to attach data to aid in solution.
|
2013-11-09 02:01:25 +00:00
|
|
|
void setNodeData(NodeItr nItr, void *data) { getNode(nItr).setData(data); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief Get the node's data pointer.
|
|
|
|
/// @param nItr Node iterator.
|
|
|
|
/// @return Pointer to node data.
|
2013-11-09 02:01:25 +00:00
|
|
|
void* getNodeData(NodeItr nItr) { return getNode(nItr).getData(); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief Get an edge's cost matrix.
|
|
|
|
/// @param eItr Edge iterator.
|
|
|
|
/// @return Edge cost matrix.
|
2013-11-09 02:01:25 +00:00
|
|
|
Matrix& getEdgeCosts(EdgeItr eItr) { return getEdge(eItr).getCosts(); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
2010-02-09 00:45:48 +00:00
|
|
|
/// \brief Get an edge's cost matrix (const version).
|
|
|
|
/// @param eItr Edge iterator.
|
|
|
|
/// @return Edge cost matrix.
|
2013-11-09 02:01:25 +00:00
|
|
|
const Matrix& getEdgeCosts(ConstEdgeItr eItr) const {
|
|
|
|
return getEdge(eItr).getCosts();
|
2010-02-09 00:45:48 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
/// \brief Set an edge's data pointer.
|
|
|
|
/// @param eItr Edge iterator.
|
|
|
|
/// @param data Pointer to edge data.
|
|
|
|
///
|
|
|
|
/// Typically used by a PBQP solver to attach data to aid in solution.
|
2013-11-09 02:01:25 +00:00
|
|
|
void setEdgeData(EdgeItr eItr, void *data) { getEdge(eItr).setData(data); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief Get an edge's data pointer.
|
|
|
|
/// @param eItr Edge iterator.
|
|
|
|
/// @return Pointer to edge data.
|
2013-11-09 02:01:25 +00:00
|
|
|
void* getEdgeData(EdgeItr eItr) { return getEdge(eItr).getData(); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief Get a node's degree.
|
|
|
|
/// @param nItr Node iterator.
|
|
|
|
/// @return The degree of the node.
|
2013-11-09 02:01:25 +00:00
|
|
|
unsigned getNodeDegree(NodeItr nItr) const {
|
|
|
|
return getNode(nItr).getDegree();
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Begin iterator for node set.
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr nodesBegin() { return nodes.begin(); }
|
|
|
|
|
|
|
|
/// \brief Begin const iterator for node set.
|
|
|
|
ConstNodeItr nodesBegin() const { return nodes.begin(); }
|
2010-02-09 00:45:48 +00:00
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
/// \brief End iterator for node set.
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr nodesEnd() { return nodes.end(); }
|
|
|
|
|
|
|
|
/// \brief End const iterator for node set.
|
|
|
|
ConstNodeItr nodesEnd() const { return nodes.end(); }
|
2010-02-09 00:45:48 +00:00
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
/// \brief Begin iterator for edge set.
|
2013-11-09 02:01:25 +00:00
|
|
|
EdgeItr edgesBegin() { return edges.begin(); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief End iterator for edge set.
|
2013-11-09 02:01:25 +00:00
|
|
|
EdgeItr edgesEnd() { return edges.end(); }
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
/// \brief Get begin iterator for adjacent edge set.
|
|
|
|
/// @param nItr Node iterator.
|
|
|
|
/// @return Begin iterator for the set of edges connected to the given node.
|
2013-11-09 02:01:25 +00:00
|
|
|
AdjEdgeItr adjEdgesBegin(NodeItr nItr) {
|
|
|
|
return getNode(nItr).edgesBegin();
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get end iterator for adjacent edge set.
|
|
|
|
/// @param nItr Node iterator.
|
|
|
|
/// @return End iterator for the set of edges connected to the given node.
|
2013-11-09 02:01:25 +00:00
|
|
|
AdjEdgeItr adjEdgesEnd(NodeItr nItr) {
|
|
|
|
return getNode(nItr).edgesEnd();
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the first node connected to this edge.
|
|
|
|
/// @param eItr Edge iterator.
|
|
|
|
/// @return The first node connected to the given edge.
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr getEdgeNode1(EdgeItr eItr) {
|
|
|
|
return getEdge(eItr).getNode1();
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the second node connected to this edge.
|
|
|
|
/// @param eItr Edge iterator.
|
|
|
|
/// @return The second node connected to the given edge.
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr getEdgeNode2(EdgeItr eItr) {
|
|
|
|
return getEdge(eItr).getNode2();
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the "other" node connected to this edge.
|
|
|
|
/// @param eItr Edge iterator.
|
|
|
|
/// @param nItr Node iterator for the "given" node.
|
|
|
|
/// @return The iterator for the "other" node connected to this edge.
|
2013-11-09 02:01:25 +00:00
|
|
|
NodeItr getEdgeOtherNode(EdgeItr eItr, NodeItr nItr) {
|
|
|
|
EdgeEntry &e = getEdge(eItr);
|
|
|
|
if (e.getNode1() == nItr) {
|
2010-01-26 04:49:58 +00:00
|
|
|
return e.getNode2();
|
|
|
|
} // else
|
|
|
|
return e.getNode1();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Get the edge connecting two nodes.
|
2013-11-09 02:01:25 +00:00
|
|
|
/// @param n1Itr First node iterator.
|
|
|
|
/// @param n2Itr Second node iterator.
|
|
|
|
/// @return An iterator for edge (n1Itr, n2Itr) if such an edge exists,
|
|
|
|
/// otherwise returns edgesEnd().
|
|
|
|
EdgeItr findEdge(NodeItr n1Itr, NodeItr n2Itr) {
|
|
|
|
for (AdjEdgeItr aeItr = adjEdgesBegin(n1Itr), aeEnd = adjEdgesEnd(n1Itr);
|
2010-01-26 04:49:58 +00:00
|
|
|
aeItr != aeEnd; ++aeItr) {
|
2013-11-09 02:01:25 +00:00
|
|
|
if ((getEdgeNode1(*aeItr) == n2Itr) ||
|
|
|
|
(getEdgeNode2(*aeItr) == n2Itr)) {
|
2010-01-26 04:49:58 +00:00
|
|
|
return *aeItr;
|
|
|
|
}
|
|
|
|
}
|
2013-11-09 02:01:25 +00:00
|
|
|
return edges.end();
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Remove a node from the graph.
|
2013-11-09 02:01:25 +00:00
|
|
|
/// @param nItr Node iterator.
|
|
|
|
void removeNode(NodeItr nItr) {
|
|
|
|
NodeEntry &n = getNode(nItr);
|
|
|
|
for (AdjEdgeItr itr = n.edgesBegin(), end = n.edgesEnd(); itr != end;) {
|
|
|
|
EdgeItr eItr = *itr;
|
|
|
|
++itr;
|
|
|
|
removeEdge(eItr);
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
2013-11-09 02:01:25 +00:00
|
|
|
nodes.erase(nItr);
|
|
|
|
--numNodes;
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Remove an edge from the graph.
|
|
|
|
/// @param eItr Edge iterator.
|
2013-11-09 02:01:25 +00:00
|
|
|
void removeEdge(EdgeItr eItr) {
|
|
|
|
EdgeEntry &e = getEdge(eItr);
|
2010-01-26 04:49:58 +00:00
|
|
|
NodeEntry &n1 = getNode(e.getNode1());
|
|
|
|
NodeEntry &n2 = getNode(e.getNode2());
|
|
|
|
n1.removeEdge(e.getNode1AEItr());
|
|
|
|
n2.removeEdge(e.getNode2AEItr());
|
2013-11-09 02:01:25 +00:00
|
|
|
edges.erase(eItr);
|
|
|
|
--numEdges;
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Remove all nodes and edges from the graph.
|
|
|
|
void clear() {
|
|
|
|
nodes.clear();
|
|
|
|
edges.clear();
|
2013-11-09 02:01:25 +00:00
|
|
|
numNodes = numEdges = 0;
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
2012-03-26 23:07:23 +00:00
|
|
|
/// \brief Dump a graph to an output stream.
|
|
|
|
template <typename OStream>
|
|
|
|
void dump(OStream &os) {
|
|
|
|
os << getNumNodes() << " " << getNumEdges() << "\n";
|
|
|
|
|
|
|
|
for (NodeItr nodeItr = nodesBegin(), nodeEnd = nodesEnd();
|
|
|
|
nodeItr != nodeEnd; ++nodeItr) {
|
2013-11-09 02:01:25 +00:00
|
|
|
const Vector& v = getNodeCosts(nodeItr);
|
2012-03-26 23:07:23 +00:00
|
|
|
os << "\n" << v.getLength() << "\n";
|
|
|
|
assert(v.getLength() != 0 && "Empty vector in graph.");
|
|
|
|
os << v[0];
|
|
|
|
for (unsigned i = 1; i < v.getLength(); ++i) {
|
|
|
|
os << " " << v[i];
|
|
|
|
}
|
|
|
|
os << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (EdgeItr edgeItr = edgesBegin(), edgeEnd = edgesEnd();
|
|
|
|
edgeItr != edgeEnd; ++edgeItr) {
|
2013-11-09 02:01:25 +00:00
|
|
|
unsigned n1 = std::distance(nodesBegin(), getEdgeNode1(edgeItr));
|
|
|
|
unsigned n2 = std::distance(nodesBegin(), getEdgeNode2(edgeItr));
|
2012-03-26 23:07:23 +00:00
|
|
|
assert(n1 != n2 && "PBQP graphs shound not have self-edges.");
|
2013-11-09 02:01:25 +00:00
|
|
|
const Matrix& m = getEdgeCosts(edgeItr);
|
2012-03-26 23:07:23 +00:00
|
|
|
os << "\n" << n1 << " " << n2 << "\n"
|
|
|
|
<< m.getRows() << " " << m.getCols() << "\n";
|
|
|
|
assert(m.getRows() != 0 && "No rows in matrix.");
|
|
|
|
assert(m.getCols() != 0 && "No cols in matrix.");
|
|
|
|
for (unsigned i = 0; i < m.getRows(); ++i) {
|
|
|
|
os << m[i][0];
|
|
|
|
for (unsigned j = 1; j < m.getCols(); ++j) {
|
|
|
|
os << " " << m[i][j];
|
|
|
|
}
|
|
|
|
os << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-26 04:49:58 +00:00
|
|
|
/// \brief Print a representation of this graph in DOT format.
|
|
|
|
/// @param os Output stream to print on.
|
|
|
|
template <typename OStream>
|
|
|
|
void printDot(OStream &os) {
|
|
|
|
|
|
|
|
os << "graph {\n";
|
|
|
|
|
|
|
|
for (NodeItr nodeItr = nodesBegin(), nodeEnd = nodesEnd();
|
|
|
|
nodeItr != nodeEnd; ++nodeItr) {
|
|
|
|
|
|
|
|
os << " node" << nodeItr << " [ label=\""
|
2013-11-09 02:01:25 +00:00
|
|
|
<< nodeItr << ": " << getNodeCosts(nodeItr) << "\" ]\n";
|
2010-01-26 04:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os << " edge [ len=" << getNumNodes() << " ]\n";
|
|
|
|
|
|
|
|
for (EdgeItr edgeItr = edgesBegin(), edgeEnd = edgesEnd();
|
|
|
|
edgeItr != edgeEnd; ++edgeItr) {
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
os << " node" << getEdgeNode1(edgeItr)
|
|
|
|
<< " -- node" << getEdgeNode2(edgeItr)
|
2010-01-26 04:49:58 +00:00
|
|
|
<< " [ label=\"";
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
const Matrix &edgeCosts = getEdgeCosts(edgeItr);
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < edgeCosts.getRows(); ++i) {
|
|
|
|
os << edgeCosts.getRowAsVector(i) << "\\n";
|
|
|
|
}
|
|
|
|
os << "\" ]\n";
|
|
|
|
}
|
|
|
|
os << "}\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
class NodeItrComparator {
|
|
|
|
public:
|
|
|
|
bool operator()(Graph::NodeItr n1, Graph::NodeItr n2) const {
|
|
|
|
return &*n1 < &*n2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(Graph::ConstNodeItr n1, Graph::ConstNodeItr n2) const {
|
|
|
|
return &*n1 < &*n2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class EdgeItrCompartor {
|
|
|
|
public:
|
|
|
|
bool operator()(Graph::EdgeItr e1, Graph::EdgeItr e2) const {
|
|
|
|
return &*e1 < &*e2;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator()(Graph::ConstEdgeItr e1, Graph::ConstEdgeItr e2) const {
|
|
|
|
return &*e1 < &*e2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void Graph::copyFrom(const Graph &other) {
|
|
|
|
std::map<Graph::ConstNodeItr, Graph::NodeItr,
|
|
|
|
NodeItrComparator> nodeMap;
|
2010-02-09 00:45:48 +00:00
|
|
|
|
2013-11-09 02:01:25 +00:00
|
|
|
for (Graph::ConstNodeItr nItr = other.nodesBegin(),
|
|
|
|
nEnd = other.nodesEnd();
|
|
|
|
nItr != nEnd; ++nItr) {
|
|
|
|
nodeMap[nItr] = addNode(other.getNodeCosts(nItr));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2010-01-26 04:49:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // LLVM_CODEGEN_PBQP_GRAPH_HPP
|