[mathicgb] 89/393: Made the memory quamtum of the matrices a command line parameter.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:37 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 e1e1ae7ddcae29bcfb34134619e4afc60c321fe4
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Wed Oct 31 18:45:58 2012 +0100

    Made the memory quamtum of the matrices a command line parameter.
---
 src/cli/GBMain.cpp                 | 13 ++++++++++---
 src/mathicgb/BuchbergerAlg.hpp     |  4 ++++
 src/mathicgb/F4MatrixBuilder.cpp   |  8 ++++++--
 src/mathicgb/F4MatrixBuilder.hpp   |  6 +++++-
 src/mathicgb/F4MatrixReducer.cpp   | 17 ++++++++---------
 src/mathicgb/F4Reducer.cpp         | 11 ++++++++---
 src/mathicgb/F4Reducer.hpp         |  3 +++
 src/mathicgb/QuadMatrixBuilder.cpp | 12 ++++++++++--
 src/mathicgb/QuadMatrixBuilder.hpp |  2 +-
 src/mathicgb/Reducer.hpp           |  4 ++++
 src/mathicgb/SparseMatrix.cpp      | 25 +++++++++++++------------
 src/mathicgb/SparseMatrix.hpp      | 11 ++++++++---
 src/mathicgb/TypicalReducer.cpp    |  3 +++
 src/mathicgb/TypicalReducer.hpp    |  2 ++
 14 files changed, 85 insertions(+), 36 deletions(-)

diff --git a/src/cli/GBMain.cpp b/src/cli/GBMain.cpp
index 54c73d8..aa297fe 100755
--- a/src/cli/GBMain.cpp
+++ b/src/cli/GBMain.cpp
@@ -121,8 +121,12 @@ public:
 
     mThreadCount("threadCount",
       "Specifies how many threads to use to run the program in parallel.",
-     1)
-    
+     1),
+
+    mMemoryQuantum("memoryQuantumForReducer",
+      "Specifies how many items to allocate memory for at a time for the reducer.",
+      1024 * 1024)
+
   {
     {
       std::ostringstream orderOut;
@@ -196,6 +200,7 @@ public:
       alg.setPrintInterval(mPrintInterval.value());
       alg.setSPairGroupSize(mSPairGroupSize.value());
       alg.setThreadCount(mThreadCount.value());
+      alg.setReducerMemoryQuantum(mMemoryQuantum.value());
       alg.setUseAutoTopReduction(mAutoTopReduce.value());
       alg.setUseAutoTailReduction(mAutoTailReduce.value());
 
@@ -288,6 +293,7 @@ public:
     parameters.push_back(&mProjectName);
     parameters.push_back(&mSPairGroupSize);
     parameters.push_back(&mThreadCount);
+    parameters.push_back(&mMemoryQuantum);
 
     // we do not expose the strategy parameter - it is only here to
     // support the old format of using direct numeric parameters to
@@ -314,7 +320,8 @@ private:
   mic::StringParameter mProjectName;
   mic::IntegerParameter mStrategy;
   mic::IntegerParameter mSPairGroupSize;
-  mic::IntegerParameter mThreadCount;  
+  mic::IntegerParameter mThreadCount;
+  mic::IntegerParameter mMemoryQuantum;
 };
 
 int oldmain(int argc, char **argv);
diff --git a/src/mathicgb/BuchbergerAlg.hpp b/src/mathicgb/BuchbergerAlg.hpp
index 771969e..6500545 100755
--- a/src/mathicgb/BuchbergerAlg.hpp
+++ b/src/mathicgb/BuchbergerAlg.hpp
@@ -54,6 +54,10 @@ public:
     mThreadCount = threadCount;
   }
 
+  void setReducerMemoryQuantum(size_t memoryQuantum) {
+    mReducer->setMemoryQuantum(memoryQuantum);
+  }
+
   void setUseAutoTopReduction(bool value) {
     mUseAutoTopReduction = value;
   }
diff --git a/src/mathicgb/F4MatrixBuilder.cpp b/src/mathicgb/F4MatrixBuilder.cpp
index 4ae2668..9df7d74 100755
--- a/src/mathicgb/F4MatrixBuilder.cpp
+++ b/src/mathicgb/F4MatrixBuilder.cpp
@@ -20,10 +20,14 @@ MATHIC_INLINE QuadMatrixBuilder::LeftRightColIndex
   return createColumn(builder, monoA, monoB);
 }
 
-F4MatrixBuilder::F4MatrixBuilder(const PolyBasis& basis, const int threadCount):
+F4MatrixBuilder::F4MatrixBuilder(
+  const PolyBasis& basis,
+  const int threadCount,
+  const size_t memoryQuantum
+):
   mThreadCount(threadCount),
   mBasis(basis),
-  mBuilder(basis.ring()),
+  mBuilder(basis.ring(), memoryQuantum),
   mTmp(basis.ring().allocMonomial())
 {
   MATHICGB_ASSERT(threadCount >= 1);
diff --git a/src/mathicgb/F4MatrixBuilder.hpp b/src/mathicgb/F4MatrixBuilder.hpp
index 9732092..2d98368 100755
--- a/src/mathicgb/F4MatrixBuilder.hpp
+++ b/src/mathicgb/F4MatrixBuilder.hpp
@@ -22,7 +22,11 @@ private:
   typedef QuadMatrixBuilder::Scalar Scalar;
 
 public:
-  F4MatrixBuilder(const PolyBasis& basis, int threadCount);
+  /// memoryQuantum is how much to increase the memory size by each time the
+  /// current amount of memory is exhausted. A value of 0 indicates to start
+  /// small and double the quantum at each exhaustion.
+  F4MatrixBuilder
+    (const PolyBasis& basis, int threadCount, size_t memoryQuantum = 0);
 
   /** Schedules a row representing the S-polynomial between polyA and
     polyB to be added to the matrix. No ownership is taken, but polyA
diff --git a/src/mathicgb/F4MatrixReducer.cpp b/src/mathicgb/F4MatrixReducer.cpp
index e6bc3b1..1621b99 100755
--- a/src/mathicgb/F4MatrixReducer.cpp
+++ b/src/mathicgb/F4MatrixReducer.cpp
@@ -73,7 +73,7 @@ namespace {
       }
     }
 
-    void addRow(SparseMatrix const& matrix, SparseMatrix::RowIndex row) {
+    void addRow(const SparseMatrix& matrix, SparseMatrix::RowIndex row) {
       MATHICGB_ASSERT(row < matrix.rowCount());
       MATHICGB_ASSERT(matrix.colCount() == colCount());
       const auto end = matrix.rowEnd(row);
@@ -165,10 +165,10 @@ namespace {
     SparseMatrix::Scalar modulus,
     const int threadCount
   ) {
-    SparseMatrix const& toReduceLeft = qm.bottomLeft;
-    SparseMatrix const& toReduceRight = qm.bottomRight;
-    SparseMatrix const& reduceByLeft = qm.topLeft;
-    SparseMatrix const& reduceByRight = qm.topRight;
+    const SparseMatrix& toReduceLeft = qm.bottomLeft;
+    const SparseMatrix& toReduceRight = qm.bottomRight;
+    const SparseMatrix& reduceByLeft = qm.topLeft;
+    const SparseMatrix& reduceByRight = qm.topRight;
 
     MATHICGB_ASSERT(reduceByLeft.colCount() == reduceByLeft.rowCount());
     const auto pivotCount = reduceByLeft.colCount();
@@ -193,7 +193,7 @@ namespace {
       rowThatReducesCol[col] = pivot;
     }
 
-    SparseMatrix reduced(colCountRight);
+    SparseMatrix reduced(colCountRight, qm.topRight.memoryQuantum());
 
 #ifdef _OPENMP
     std::vector<DenseRow<uint64> > denseRowPerThread(threadCount);
@@ -201,7 +201,7 @@ namespace {
     DenseRow<uint64> denseRow;
 #endif
 
-    SparseMatrix tmp(pivotCount);
+    SparseMatrix tmp(pivotCount, qm.topRight.memoryQuantum());
 
     std::vector<SparseMatrix::RowIndex> rowOrder(rowCount);
 
@@ -304,8 +304,7 @@ namespace {
     std::vector<SparseMatrix::ColIndex> leadCols(rowCount);
 
     // pivot rows get copied here before being used to reduce the matrix.
-    SparseMatrix reduced;
-    reduced.clear(colCount);
+    SparseMatrix reduced(colCount, toReduce.memoryQuantum());
 
     // (col,row) in nextReducers, then use row as a pivot in column col
     // for the next iteration.
diff --git a/src/mathicgb/F4Reducer.cpp b/src/mathicgb/F4Reducer.cpp
index f2428ad..8b44c52 100755
--- a/src/mathicgb/F4Reducer.cpp
+++ b/src/mathicgb/F4Reducer.cpp
@@ -11,7 +11,8 @@ F4Reducer::F4Reducer(
 ):
   mFallback(std::move(fallback)),
   mRing(ring),
-  mThreadCount(1) {
+  mThreadCount(1),
+  mMemoryQuantum(0) {
 }
 
 std::unique_ptr<Poly> F4Reducer::classicReduce
@@ -102,7 +103,7 @@ void F4Reducer::classicReduceSPolySet
   {
     QuadMatrix qm;
     {
-      F4MatrixBuilder builder(basis, mThreadCount);
+      F4MatrixBuilder builder(basis, mThreadCount, mMemoryQuantum);
       for (auto it = spairs.begin(); it != spairs.end(); ++it) {
         builder.addSPolynomialToMatrix
           (basis.poly(it->first), basis.poly(it->second));
@@ -157,7 +158,7 @@ void F4Reducer::classicReducePolySet
   {
     QuadMatrix qm;
     {
-      F4MatrixBuilder builder(basis, mThreadCount);
+      F4MatrixBuilder builder(basis, mThreadCount, mMemoryQuantum);
       for (auto it = polys.begin(); it != polys.end(); ++it)
         builder.addPolynomialToMatrix(**it);
       builder.buildMatrixAndClear(qm);
@@ -200,6 +201,10 @@ void F4Reducer::setThreadCount(int threadCount) {
   mThreadCount = threadCount;
 }
 
+void F4Reducer::setMemoryQuantum(size_t quantum) {
+  mMemoryQuantum = quantum;
+}
+
 std::string F4Reducer::description() const {
   return "F4 reducer";
 }
diff --git a/src/mathicgb/F4Reducer.hpp b/src/mathicgb/F4Reducer.hpp
index 238f1d8..fe113d7 100755
--- a/src/mathicgb/F4Reducer.hpp
+++ b/src/mathicgb/F4Reducer.hpp
@@ -36,6 +36,8 @@ public:
 
   virtual void setThreadCount(int threadCount);
 
+  virtual void setMemoryQuantum(size_t quantum);
+
   virtual std::string description() const;
   virtual size_t getMemoryUse() const;
 
@@ -43,6 +45,7 @@ private:
   std::unique_ptr<Reducer> mFallback;
   const PolyRing& mRing;
   int mThreadCount;
+  size_t mMemoryQuantum;
 };
 
 #endif
diff --git a/src/mathicgb/QuadMatrixBuilder.cpp b/src/mathicgb/QuadMatrixBuilder.cpp
index 045f0f9..dc3d776 100755
--- a/src/mathicgb/QuadMatrixBuilder.cpp
+++ b/src/mathicgb/QuadMatrixBuilder.cpp
@@ -6,8 +6,16 @@
 #include <mathic.h>
 #include <sstream>
 
-QuadMatrixBuilder::QuadMatrixBuilder(const PolyRing& ring):
-  mMonomialToCol(ring) {}
+QuadMatrixBuilder::QuadMatrixBuilder(
+  const PolyRing& ring,
+  const size_t memoryQuantum
+):
+  mMonomialToCol(ring),
+  mTopLeft(0, memoryQuantum),
+  mTopRight(0, memoryQuantum),
+  mBottomLeft(0, memoryQuantum),
+  mBottomRight(0, memoryQuantum)
+{}
 
 void QuadMatrixBuilder::takeRowsFrom(QuadMatrix&& matrix) {
   MATHICGB_ASSERT(&ring() == matrix.ring);
diff --git a/src/mathicgb/QuadMatrixBuilder.hpp b/src/mathicgb/QuadMatrixBuilder.hpp
index 7db3091..cb0ca74 100755
--- a/src/mathicgb/QuadMatrixBuilder.hpp
+++ b/src/mathicgb/QuadMatrixBuilder.hpp
@@ -29,7 +29,7 @@ class QuadMatrixBuilder {
   typedef SparseMatrix::ColIndex ColIndex;
   typedef SparseMatrix::Scalar Scalar;
 
-  QuadMatrixBuilder(const PolyRing& ring);
+  QuadMatrixBuilder(const PolyRing& ring, size_t memoryQuantum = 0);
 
   /// Inserts the rows from builder. To avoid an assert either the matrix must
   /// have no column monomials specified or the monomials that are specified
diff --git a/src/mathicgb/Reducer.hpp b/src/mathicgb/Reducer.hpp
index 0e4aac4..4db9f10 100755
--- a/src/mathicgb/Reducer.hpp
+++ b/src/mathicgb/Reducer.hpp
@@ -66,6 +66,10 @@ public:
     reducer supports it. */
   virtual void setThreadCount(int threadCount) = 0;
 
+  /** Sets how many bytes of memory to increase the memory use by
+    at a time - if such a thing is appropriate for the reducer. */
+  virtual void setMemoryQuantum(size_t quantum) = 0;
+
   // ***** Kinds of reducers and creating a Reducer 
 
   enum ReducerType {
diff --git a/src/mathicgb/SparseMatrix.cpp b/src/mathicgb/SparseMatrix.cpp
index 5377ff6..b5b90fd 100755
--- a/src/mathicgb/SparseMatrix.cpp
+++ b/src/mathicgb/SparseMatrix.cpp
@@ -159,6 +159,7 @@ void SparseMatrix::swap(SparseMatrix& matrix) {
   using std::swap;
   swap(mRows, matrix.mRows);
   swap(mColCount, matrix.mColCount);
+  swap(mMemoryQuantum, matrix.mMemoryQuantum);
 }
 
 void SparseMatrix::clear(const ColIndex newColCount) {
@@ -310,20 +311,20 @@ void SparseMatrix::growEntryCapacity() {
   MATHICGB_ASSERT(mBlock.mColIndices.size() == mBlock.mScalars.size());
   MATHICGB_ASSERT(mBlock.mColIndices.capacity() == mBlock.mScalars.capacity());
 
-  // TODO: handle overflow of multiplication below
-  const size_t minBlockSize = 1 << 20;
-  const size_t minMultipleOfPending = 2;
-  const size_t pendingCount = mBlock.mHasNoRows ?
-    mBlock.mColIndices.size() :
-    std::distance(mRows.back().mIndicesEnd, mBlock.mColIndices.end());
-  const size_t blockSize =
-    std::max(minBlockSize, pendingCount * minMultipleOfPending);
-
-  reserveFreeEntries(blockSize);
+  // TODO: handle overflow of arithmetic here
+  if (mMemoryQuantum != 0 &&
+    (!mBlock.mHasNoRows || mBlock.mPreviousBlock == 0)
+  )
+    reserveFreeEntries(mMemoryQuantum);
+  else if (mBlock.mColIndices.capacity() == 0)
+    reserveFreeEntries(1 << 14); // minimum block size
+  else {
+    // do this if the quantum is not set or if the quantum is too small
+    // to store a single row being built.
+    reserveFreeEntries(mBlock.mColIndices.capacity() * 2);
+  }
 
   MATHICGB_ASSERT(mBlock.mColIndices.size() == mBlock.mScalars.size());
-  MATHICGB_ASSERT(mBlock.mColIndices.capacity() == blockSize + pendingCount);
-  MATHICGB_ASSERT(mBlock.mScalars.capacity() == blockSize + pendingCount);
 }
 
 std::ostream& operator<<(std::ostream& out, const SparseMatrix& matrix) {
diff --git a/src/mathicgb/SparseMatrix.hpp b/src/mathicgb/SparseMatrix.hpp
index d45b095..0bda96d 100755
--- a/src/mathicgb/SparseMatrix.hpp
+++ b/src/mathicgb/SparseMatrix.hpp
@@ -48,12 +48,16 @@ public:
   class ConstRowIterator;
 
   /// Construct a matrix with no rows.
-  SparseMatrix(ColIndex colCount = 0): mColCount(colCount) {}
+  SparseMatrix(const ColIndex colCount = 0, const size_t memoryQuantum = 0):
+    mColCount(colCount),
+    mMemoryQuantum(memoryQuantum)
+  {}
 
   SparseMatrix(SparseMatrix&& matrix):
     mRows(std::move(matrix.mRows)),
     mBlock(std::move(matrix.mBlock)),
-    mColCount(matrix.mColCount)
+    mColCount(matrix.mColCount),
+    mMemoryQuantum(matrix.mMemoryQuantum)
   {
   }
 
@@ -80,6 +84,7 @@ public:
 
   RowIndex rowCount() const {return mRows.size();}
   ColIndex colCount() const {return mColCount;}
+  size_t memoryQuantum() const {return mMemoryQuantum;}
 
   /// Returns the number of entries in the whole matrix. Is not constant time
   /// so avoid calling too many times.
@@ -338,6 +343,7 @@ private:
   Block mBlock;
 
   ColIndex mColCount;
+  size_t mMemoryQuantum;
 };
 
 inline void swap(SparseMatrix& a, SparseMatrix& b) {
@@ -346,5 +352,4 @@ inline void swap(SparseMatrix& a, SparseMatrix& b) {
 
 std::ostream& operator<<(std::ostream& out, const SparseMatrix& matrix);
 
-
 #endif
diff --git a/src/mathicgb/TypicalReducer.cpp b/src/mathicgb/TypicalReducer.cpp
index 37c97a1..4138218 100755
--- a/src/mathicgb/TypicalReducer.cpp
+++ b/src/mathicgb/TypicalReducer.cpp
@@ -159,6 +159,9 @@ void TypicalReducer::setThreadCount(int threadCount) {
   // multithreading not supported here (yet!)
 }
 
+void TypicalReducer::setMemoryQuantum(size_t quantum) {
+}
+
 std::unique_ptr<Poly> TypicalReducer::classicReduce
     (std::unique_ptr<Poly> result, const PolyBasis& basis) {
   const PolyRing& ring = basis.ring();
diff --git a/src/mathicgb/TypicalReducer.hpp b/src/mathicgb/TypicalReducer.hpp
index 65c27e8..de234b2 100755
--- a/src/mathicgb/TypicalReducer.hpp
+++ b/src/mathicgb/TypicalReducer.hpp
@@ -45,6 +45,8 @@ public:
 
   virtual void setThreadCount(int threadCount);
 
+  virtual void setMemoryQuantum(size_t quantum);
+
 protected:
   // These are the methods that sub-classes define in order to carry
   // out sub-steps in the reduction.

-- 
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