[mathicgb] 92/393: Improved tracking output for size and memory used by matrices. Use -trac 5 -cla -reducer 25 to see.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:38 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.

commit 5d333dbd43673bf9175ef40cc5c63cf4e99c988e
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Fri Nov 2 10:57:35 2012 +0100

    Improved tracking output for size and memory used by matrices. Use -trac 5 -cla -reducer 25 to see.
---
 src/mathicgb/QuadMatrix.cpp   |  58 ++++++++++++------
 src/mathicgb/QuadMatrix.hpp   | 139 +++++++++++++++++++++---------------------
 src/mathicgb/RawVector.hpp    |   8 +++
 src/mathicgb/SparseMatrix.cpp |  22 +++++++
 src/mathicgb/SparseMatrix.hpp |   9 ++-
 5 files changed, 149 insertions(+), 87 deletions(-)

diff --git a/src/mathicgb/QuadMatrix.cpp b/src/mathicgb/QuadMatrix.cpp
index 6d0d3ec..68031d0 100755
--- a/src/mathicgb/QuadMatrix.cpp
+++ b/src/mathicgb/QuadMatrix.cpp
@@ -64,6 +64,16 @@ std::string QuadMatrix::toString() const {
   return out.str();
 }
 
+size_t QuadMatrix::memoryUse() const {
+  return topLeft.memoryUse() + topRight.memoryUse() +
+	bottomLeft.memoryUse() + bottomRight.memoryUse();
+}
+
+size_t QuadMatrix::memoryUseTrimmed() const {
+  return topLeft.memoryUseTrimmed() + topRight.memoryUseTrimmed() +
+	bottomLeft.memoryUseTrimmed() + bottomRight.memoryUseTrimmed();
+}
+
 void QuadMatrix::printSizes(std::ostream& out) const {
   typedef mathic::ColumnPrinter ColPr;
 
@@ -71,40 +81,52 @@ void QuadMatrix::printSizes(std::ostream& out) const {
   pr.addColumn(false, " ", "");
   pr.addColumn(false, "", "");
   pr.addColumn(false, "", "");
-  //pr.addColumn(false, " ", "");
-  const char* const line = "-------------------";
+  const char* const line = "----------";
 
   pr[0] << '\n';
   pr[1] << ColPr::commafy(topLeft.colCount()) << "  \n";
-  pr[2] << ColPr::commafy(topRight.colCount()) << "   \n";
-  //pr[3] << '\n';
+  pr[2] << ColPr::commafy(topRight.colCount()) << "  \n";
 
-  pr[0] << '\n';
-  pr[1] << "/"  << line << "|\n";
-  pr[2] << line << "\\ \n";
-  //pr[3] << '\n';
+  pr[0] << "/\n";
+  pr[1] << line << "|\n";
+  pr[2] << line << "\\\n";
 
   pr[0] << ColPr::commafy(topLeft.rowCount()) << " |\n";
   pr[1] << ColPr::commafy(topLeft.entryCount()) << " |\n";
   pr[2] << ColPr::commafy(topRight.entryCount()) << " |\n";
-  //pr[3] << "| " << ColPr::commafy(topRight.rowCount()) << '\n';
 
   pr[0] << "|\n";
-  pr[1] << '-' << line << "|\n";
-  pr[2] << line << "-|\n";
-  //pr[3] << "|\n";
+  pr[1] << ColPr::bytesInUnit(topLeft.memoryUse()) << " |\n";
+  pr[2] << ColPr::bytesInUnit(topRight.memoryUse()) << " |\n";
+
+  pr[0] << "|\n";
+  pr[1] << ColPr::percent(topLeft.memoryUse(), memoryUse()) << " |\n";
+  pr[2] << ColPr::percent(topRight.memoryUse(), memoryUse()) << " |\n";
+
+  pr[0] << "|\n";
+  pr[1] << line << "|\n";
+  pr[2] << line << "|\n";
 
   pr[0] << ColPr::commafy(bottomLeft.rowCount()) << " |\n";
   pr[1] << ColPr::commafy(bottomLeft.entryCount()) << " |\n";
   pr[2] << ColPr::commafy(bottomRight.entryCount()) << " |\n";
-  //pr[3] << "| " << ColPr::commafy(bottomRight.rowCount());
 
-  pr[0] << '\n';
-  pr[1] << "\\"  << line << "|\n";
-  pr[2] << line << "/ \n";
-  //pr[3] << '\n';
+  pr[0] << "|\n";
+  pr[1] << ColPr::bytesInUnit(bottomLeft.memoryUse()) << " |\n";
+  pr[2] << ColPr::bytesInUnit(bottomRight.memoryUse()) << " |\n";
+
+  pr[0] << "|\n";
+  pr[1] << ColPr::percent(bottomLeft.memoryUse(), memoryUse()) << " |\n";
+  pr[2] << ColPr::percent(bottomRight.memoryUse(), memoryUse()) << " |\n";
+
+  pr[0] << "\\\n";
+  pr[1] << line << "|\n";
+  pr[2] << line << "/\n";
 
-  out << '\n' << pr;
+  out << '\n' << pr
+	  << "Total memory: " << memoryUse() << "  ("
+	  << ColPr::percent(memoryUseTrimmed(), memoryUse())
+	  << " written to)\n";
 }
 
 QuadMatrix QuadMatrix::toCanonical() const {
diff --git a/src/mathicgb/QuadMatrix.hpp b/src/mathicgb/QuadMatrix.hpp
index 42918b9..a508114 100755
--- a/src/mathicgb/QuadMatrix.hpp
+++ b/src/mathicgb/QuadMatrix.hpp
@@ -1,69 +1,72 @@
-#ifndef MATHICGB_QUAD_MATRIX_GUARD
-#define MATHICGB_QUAD_MATRIX_GUARD
-
-#include "PolyRing.hpp"
-#include "SparseMatrix.hpp"
-#include <vector>
-#include <string>
-#include <ostream>
-class ostream;
-
-/** Represents a matrix composed of 4 sub-matrices that fit together
-  into one matrix divided into top left, top right, bottom left and
-  bottom right. This is a convenient representation of the matrices
-  encountered in the F4 polynomial reduction algorithm.
-
-  So far this is just a data class used as the output of a
-  QuadMatrixBuilder or F4MatrixBuilder. */
-class QuadMatrix {
-public:
-  QuadMatrix() {}
-  QuadMatrix(QuadMatrix&& matrix):
-    topLeft(std::move(matrix.topLeft)),
-    topRight(std::move(matrix.topRight)),
-    bottomLeft(std::move(matrix.bottomLeft)),
-    bottomRight(std::move(matrix.bottomRight)),
-    leftColumnMonomials(std::move(matrix.leftColumnMonomials)),
-    rightColumnMonomials(std::move(matrix.rightColumnMonomials)),
-    ring(matrix.ring)
-  {}
-  QuadMatrix& operator=(QuadMatrix&& matrix) {
-    this->~QuadMatrix();
-    new (this) QuadMatrix(std::move(matrix));
-    return *this;
-  }
-
-  SparseMatrix topLeft; 
-  SparseMatrix topRight;
-  SparseMatrix bottomLeft;
-  SparseMatrix bottomRight;
-  std::vector<monomial> leftColumnMonomials;
-  std::vector<monomial> rightColumnMonomials;
-  const PolyRing* ring;
-
-  /// Prints whole matrix to out in human-readable format. Useful for
-  /// debugging.
-  void print(std::ostream& out) const;
-
-  /// Prints the sizes of the matrix out, in terms of number of rows and
-  /// columns and how many non-zero entries in each submatrix.
-  void printSizes(std::ostream& out) const;
-
-  /// Shows whole matrix in a string. Useful for debugging.
-  std::string toString() const;
-
-  /// Makes a copy of this matrix whose rows are sorted in some canonical way.
-  /// TODO: Actually only coarsely sorts the top rows right now.
-  QuadMatrix toCanonical() const;
-
+#ifndef MATHICGB_QUAD_MATRIX_GUARD
+#define MATHICGB_QUAD_MATRIX_GUARD
+
+#include "PolyRing.hpp"
+#include "SparseMatrix.hpp"
+#include <vector>
+#include <string>
+#include <ostream>
+class ostream;
+
+/** Represents a matrix composed of 4 sub-matrices that fit together
+  into one matrix divided into top left, top right, bottom left and
+  bottom right. This is a convenient representation of the matrices
+  encountered in the F4 polynomial reduction algorithm.
+
+  So far this is just a data class used as the output of a
+  QuadMatrixBuilder or F4MatrixBuilder. */
+class QuadMatrix {
+public:
+  QuadMatrix() {}
+  QuadMatrix(QuadMatrix&& matrix):
+    topLeft(std::move(matrix.topLeft)),
+    topRight(std::move(matrix.topRight)),
+    bottomLeft(std::move(matrix.bottomLeft)),
+    bottomRight(std::move(matrix.bottomRight)),
+    leftColumnMonomials(std::move(matrix.leftColumnMonomials)),
+    rightColumnMonomials(std::move(matrix.rightColumnMonomials)),
+    ring(matrix.ring)
+  {}
+  QuadMatrix& operator=(QuadMatrix&& matrix) {
+    this->~QuadMatrix();
+    new (this) QuadMatrix(std::move(matrix));
+    return *this;
+  }
+
+  SparseMatrix topLeft; 
+  SparseMatrix topRight;
+  SparseMatrix bottomLeft;
+  SparseMatrix bottomRight;
+  std::vector<monomial> leftColumnMonomials;
+  std::vector<monomial> rightColumnMonomials;
+  const PolyRing* ring;
+
+  /// Prints whole matrix to out in human-readable format. Useful for
+  /// debugging.
+  void print(std::ostream& out) const;
+
+  /// Prints the sizes of the matrix out, in terms of number of rows and
+  /// columns and how many non-zero entries in each submatrix.
+  void printSizes(std::ostream& out) const;
+
+  size_t memoryUse() const;
+  size_t memoryUseTrimmed() const;
+
+  /// Shows whole matrix in a string. Useful for debugging.
+  std::string toString() const;
+
+  /// Makes a copy of this matrix whose rows are sorted in some canonical way.
+  /// TODO: Actually only coarsely sorts the top rows right now.
+  QuadMatrix toCanonical() const;
+
   /// Asserts internal invariants if asserts are turned on.
-  bool debugAssertValid() const;
-
-private:
-  QuadMatrix(const QuadMatrix&); // not available
-  void operator=(const QuadMatrix&); // not available
-};
-
-std::ostream& operator<<(std::ostream& out, const QuadMatrix& qm);
-
-#endif
+  bool debugAssertValid() const;
+
+private:
+  QuadMatrix(const QuadMatrix&); // not available
+  void operator=(const QuadMatrix&); // not available
+};
+
+std::ostream& operator<<(std::ostream& out, const QuadMatrix& qm);
+
+#endif
diff --git a/src/mathicgb/RawVector.hpp b/src/mathicgb/RawVector.hpp
index 95981a3..b7b253a 100755
--- a/src/mathicgb/RawVector.hpp
+++ b/src/mathicgb/RawVector.hpp
@@ -163,6 +163,14 @@ public:
     return *(mEnd - 1);
   }
 
+  size_t memoryUse() const {
+    return capacity() * sizeof(value_type);
+  }
+
+  size_t memoryUseTrimmed() const {
+    return size() * sizeof(value_type);
+  }
+
   /// There must be enough capacity for the new size.
   template<class Iter>
   void rawAssign(Iter begin, Iter end) {
diff --git a/src/mathicgb/SparseMatrix.cpp b/src/mathicgb/SparseMatrix.cpp
index 5377ff6..ad22515 100755
--- a/src/mathicgb/SparseMatrix.cpp
+++ b/src/mathicgb/SparseMatrix.cpp
@@ -326,6 +326,28 @@ void SparseMatrix::growEntryCapacity() {
   MATHICGB_ASSERT(mBlock.mScalars.capacity() == blockSize + pendingCount);
 }
 
+size_t SparseMatrix::memoryUse() const {
+  size_t count = 0;
+  for (auto block = &mBlock; block != 0; block = block->mPreviousBlock)
+    count += block->memoryUse() + sizeof(Block);
+  return count;
+}
+
+size_t SparseMatrix::memoryUseTrimmed() const {
+  size_t count = 0;
+  for (auto block = &mBlock; block != 0; block = block->mPreviousBlock)
+    count += block->memoryUseTrimmed() + sizeof(Block);
+  return count;
+}
+
+size_t SparseMatrix::Block::memoryUse() const {
+  return mColIndices.memoryUse() + mScalars.memoryUse();
+}
+
+size_t SparseMatrix::Block::memoryUseTrimmed() const {
+  return mColIndices.memoryUseTrimmed() + mScalars.memoryUseTrimmed();
+}
+
 std::ostream& operator<<(std::ostream& out, const SparseMatrix& matrix) {
   matrix.print(out);
   return out;
diff --git a/src/mathicgb/SparseMatrix.hpp b/src/mathicgb/SparseMatrix.hpp
index d45b095..40b3099 100755
--- a/src/mathicgb/SparseMatrix.hpp
+++ b/src/mathicgb/SparseMatrix.hpp
@@ -91,6 +91,11 @@ public:
     return count;
   }
 
+  /// Returns the number of bytes of memory allocated by this object. Is not
+  /// constant time so avoid calling too many times.
+  size_t memoryUse() const;
+  size_t memoryUseTrimmed() const;
+
    /// Returns the number of entries in the given row.
   ColIndex entryCountInRow(RowIndex row) const {
     MATHICGB_ASSERT(row < rowCount());
@@ -325,6 +330,9 @@ private:
       return *this;
     }
 
+    size_t memoryUse() const;
+    size_t memoryUseTrimmed() const;
+
     /// We need a RawVector here to tie the checks for the need to reallocate
     /// together between mColIndices and mEntries. We only need to check
     /// the capacity once, which, believe it or not, is a significant performance
@@ -346,5 +354,4 @@ inline void swap(SparseMatrix& a, SparseMatrix& b) {
 
 std::ostream& operator<<(std::ostream& out, const SparseMatrix& matrix);
 
-
 #endif

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git



More information about the debian-science-commits mailing list