[llvm-commits] [llvm] r95633 - in /llvm/trunk/lib/CodeGen/PBQP: Graph.h HeuristicSolver.h

Lang Hames lhames at gmail.com
Mon Feb 8 16:45:48 PST 2010


Author: lhames
Date: Mon Feb  8 18:45:48 2010
New Revision: 95633

URL: http://llvm.org/viewvc/llvm-project?rev=95633&view=rev
Log:
Added copy sensible construction & assignment to PBQP graphs and fixed a memory access bug in the heuristic solver.

Modified:
    llvm/trunk/lib/CodeGen/PBQP/Graph.h
    llvm/trunk/lib/CodeGen/PBQP/HeuristicSolver.h

Modified: llvm/trunk/lib/CodeGen/PBQP/Graph.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/PBQP/Graph.h?rev=95633&r1=95632&r2=95633&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/PBQP/Graph.h (original)
+++ llvm/trunk/lib/CodeGen/PBQP/Graph.h Mon Feb  8 18:45:48 2010
@@ -19,6 +19,7 @@
 
 #include <list>
 #include <vector>
+#include <map>
 
 namespace PBQP {
 
@@ -37,7 +38,10 @@
   public:
 
     typedef NodeList::iterator NodeItr;
+    typedef NodeList::const_iterator ConstNodeItr;
+
     typedef EdgeList::iterator EdgeItr;
+    typedef EdgeList::const_iterator ConstEdgeItr;
 
   private:
 
@@ -58,6 +62,7 @@
     public:
       NodeEntry(const Vector &costs) : costs(costs), degree(0) {}
       Vector& getCosts() { return costs; }
+      const Vector& getCosts() const { return costs; }
       unsigned getDegree() const { return degree; }
       AdjEdgeItr edgesBegin() { return adjEdges.begin(); }
       AdjEdgeItr edgesEnd() { return adjEdges.end(); }
@@ -85,6 +90,7 @@
       NodeItr getNode1() const { return node1; }
       NodeItr getNode2() const { return node2; }
       Matrix& getCosts() { return costs; }
+      const Matrix& getCosts() const { return costs; }
       void setNode1AEItr(AdjEdgeItr ae) { node1AEItr = ae; }
       AdjEdgeItr getNode1AEItr() { return node1AEItr; }
       void setNode2AEItr(AdjEdgeItr ae) { node2AEItr = ae; }
@@ -104,9 +110,10 @@
     // ----- INTERNAL METHODS -----
 
     NodeEntry& getNode(NodeItr nItr) { return *nItr; }
-    const NodeEntry& getNode(NodeItr nItr) const { return *nItr; }
+    const NodeEntry& getNode(ConstNodeItr nItr) const { return *nItr; }
+
     EdgeEntry& getEdge(EdgeItr eItr) { return *eItr; }
-    const EdgeEntry& getEdge(EdgeItr eItr) const { return *eItr; }
+    const EdgeEntry& getEdge(ConstEdgeItr eItr) const { return *eItr; }
 
     NodeItr addConstructedNode(const NodeEntry &n) {
       ++numNodes;
@@ -130,10 +137,32 @@
       return edgeItr;
     }
 
+    inline void copyFrom(const Graph &other);
   public:
 
+    /// \brief Construct an empty PBQP graph.
     Graph() : numNodes(0), numEdges(0) {}
 
+    /// \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.
+    Graph(const Graph &other) : numNodes(0), numEdges(0) {
+      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;
+    }
+
     /// \brief Add a node with the given costs.
     /// @param costs Cost vector for the new node.
     /// @return Node iterator for the added node.
@@ -166,6 +195,13 @@
     /// @return Node cost vector.
     Vector& getNodeCosts(NodeItr nItr) { return getNode(nItr).getCosts(); }
 
+    /// \brief Get a node's cost vector (const version).
+    /// @param nItr Node iterator.
+    /// @return Node cost vector.
+    const Vector& getNodeCosts(ConstNodeItr nItr) const {
+      return getNode(nItr).getCosts();
+    }
+
     /// \brief Set a node's data pointer.
     /// @param nItr Node iterator.
     /// @param data Pointer to node data.
@@ -183,6 +219,13 @@
     /// @return Edge cost matrix.
     Matrix& getEdgeCosts(EdgeItr eItr) { return getEdge(eItr).getCosts(); }
 
+    /// \brief Get an edge's cost matrix (const version).
+    /// @param eItr Edge iterator.
+    /// @return Edge cost matrix.
+    const Matrix& getEdgeCosts(ConstEdgeItr eItr) const {
+      return getEdge(eItr).getCosts();
+    }
+
     /// \brief Set an edge's data pointer.
     /// @param eItr Edge iterator.
     /// @param data Pointer to edge data.
@@ -205,9 +248,15 @@
     /// \brief Begin iterator for node set.
     NodeItr nodesBegin() { return nodes.begin(); }
 
+    /// \brief Begin const iterator for node set.
+    ConstNodeItr nodesBegin() const { return nodes.begin(); }
+
     /// \brief End iterator for node set.
     NodeItr nodesEnd() { return nodes.end(); }
 
+    /// \brief End const iterator for node set.
+    ConstNodeItr nodesEnd() const { return nodes.end(); }
+
     /// \brief Begin iterator for edge set.
     EdgeItr edgesBegin() { return edges.begin(); }
 
@@ -342,6 +391,10 @@
     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 {
@@ -349,8 +402,23 @@
     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;
+
+     for (Graph::ConstNodeItr nItr = other.nodesBegin(),
+                             nEnd = other.nodesEnd();
+         nItr != nEnd; ++nItr) {
+      nodeMap[nItr] = addNode(other.getNodeCosts(nItr));
+    }
+      
+  }
 
 }
 

Modified: llvm/trunk/lib/CodeGen/PBQP/HeuristicSolver.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/PBQP/HeuristicSolver.h?rev=95633&r1=95632&r2=95633&view=diff

==============================================================================
--- llvm/trunk/lib/CodeGen/PBQP/HeuristicSolver.h (original)
+++ llvm/trunk/lib/CodeGen/PBQP/HeuristicSolver.h Mon Feb  8 18:45:48 2010
@@ -107,8 +107,11 @@
     Solution s;
     std::vector<Graph::NodeItr> stack;
 
-    std::vector<NodeData> nodeData;
-    std::vector<EdgeData> edgeData;
+    typedef std::list<NodeData> NodeDataList;
+    NodeDataList nodeDataList;
+
+    typedef std::list<EdgeData> EdgeDataList;
+    EdgeDataList edgeDataList;
 
   public:
 
@@ -364,8 +367,8 @@
       } else if (addedEdge) {
         // If the edge was added, and non-null, finish setting it up, add it to
         // the solver & notify heuristic.
-        edgeData.push_back(EdgeData());
-        g.setEdgeData(yzeItr, &edgeData.back());
+        edgeDataList.push_back(EdgeData());
+        g.setEdgeData(yzeItr, &edgeDataList.back());
         addSolverEdge(yzeItr);
         h.handleAddEdge(yzeItr);
       }
@@ -402,22 +405,18 @@
         simplify();
       }
 
-      // Reserve space for the node and edge data.
-      nodeData.resize(g.getNumNodes());
-      edgeData.resize(g.getNumEdges());
-
       // Create node data objects.
-      unsigned ndIndex = 0;     
       for (Graph::NodeItr nItr = g.nodesBegin(), nEnd = g.nodesEnd();
-	       nItr != nEnd; ++nItr, ++ndIndex) {
-        g.setNodeData(nItr, &nodeData[ndIndex]);
+	       nItr != nEnd; ++nItr) {
+        nodeDataList.push_back(NodeData());
+        g.setNodeData(nItr, &nodeDataList.back());
       }
 
       // Create edge data objects.
-      unsigned edIndex = 0;
       for (Graph::EdgeItr eItr = g.edgesBegin(), eEnd = g.edgesEnd();
-           eItr != eEnd; ++eItr, ++edIndex) {
-        g.setEdgeData(eItr, &edgeData[edIndex]);
+           eItr != eEnd; ++eItr) {
+        edgeDataList.push_back(EdgeData());
+        g.setEdgeData(eItr, &edgeDataList.back());
         addSolverEdge(eItr);
       }
     }
@@ -563,8 +562,8 @@
 
     void cleanup() {
       h.cleanup();
-      nodeData.clear();
-      edgeData.clear();
+      nodeDataList.clear();
+      edgeDataList.clear();
     }
   };
 





More information about the llvm-commits mailing list