[mathicgb] 50/393: No longer sort rows by increasing pivots. Also, decreased the load factor on the hash table.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:31 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 02cdb6af4bd7f71704f3c7c38a69be3883ce0dbd
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Fri Oct 5 19:20:45 2012 +0200

    No longer sort rows by increasing pivots. Also, decreased the load factor on the hash table.
---
 src/mathicgb/F4MatrixReducer.cpp   | 64 +++++++++++++-------------------------
 src/mathicgb/QuadMatrixBuilder.cpp |  9 ++++++
 src/mathicgb/QuadMatrixBuilder.hpp |  7 +----
 src/mathicgb/SparseMatrix.cpp      | 32 +++++++++++++++++++
 src/mathicgb/SparseMatrix.hpp      |  4 +++
 src/test/F4MatrixReducer.cpp       |  1 +
 6 files changed, 68 insertions(+), 49 deletions(-)

diff --git a/src/mathicgb/F4MatrixReducer.cpp b/src/mathicgb/F4MatrixReducer.cpp
index 1bdfe05..c6e4e49 100755
--- a/src/mathicgb/F4MatrixReducer.cpp
+++ b/src/mathicgb/F4MatrixReducer.cpp
@@ -261,35 +261,6 @@ void reformMatrix(const Matrix& matA, const Matrix& matB, SparseMatrix& matAB) {
   MATHICGB_ASSERT(matAB.colCount() == matA.coldim() + matB.coldim());
 }
 
-void sortRowsByIncreasingPivots(SparseMatrix& in, SparseMatrix& out) {
-  MATHICGB_ASSERT(&in != &out);
-
-  // compute pairs (pivot column index, row)
-  std::vector<std::pair<SparseMatrix::ColIndex, SparseMatrix::RowIndex> > order;
-  const SparseMatrix::RowIndex rowCount = in.rowCount();
-  const SparseMatrix::ColIndex colCount = in.colCount();
-  for (SparseMatrix::RowIndex row = 0; row < rowCount; ++row) {
-    if (in.entryCountInRow(row) == 0)
-      order.push_back(std::make_pair(colCount, row));
-    else
-      order.push_back(std::make_pair(in.rowBegin(row).index(), row));
-  }
-
-  // sort pairs by pivot column index
-  std::sort(order.begin(), order.end());
-
-  // construct out with pivot columns in increaing order
-  out.clear(colCount);
-  for (size_t i = 0; i < rowCount; ++i) {
-    const SparseMatrix::RowIndex row = order[i].second;
-    SparseMatrix::RowIterator it = in.rowBegin(row);
-    SparseMatrix::RowIterator end = in.rowEnd(row);
-    for (; it != end; ++it)
-      out.appendEntry(it.index(), it.scalar());
-    out.rowDone();
-  }
-}
-
 void myReduce
 (SparseMatrix const& toReduce,
  SparseMatrix const& reduceBy,
@@ -299,11 +270,24 @@ void myReduce
   // assuming that the left part of reduceBy is upper triangular.
   MATHICGB_ASSERT(reduceBy.colCount() >= reduceBy.rowCount());
   MATHICGB_ASSERT(reduceBy.colCount() == toReduce.colCount());
-  size_t const pivotCount = reduceBy.rowCount();
-  size_t const colCount = toReduce.colCount();
+  const auto pivotCount = reduceBy.rowCount();
+  const auto colCount = toReduce.colCount();
+  const auto rowCount = toReduce.rowCount();
 
   reduced.clear(toReduce.colCount());
-  size_t rowCount = toReduce.rowCount();
+
+  // pre-calculate what rows are pivots for what columns
+  std::vector<SparseMatrix::RowIndex> rowThatReducesCol(pivotCount);
+#ifdef MATHICGB_DEBUG
+  // fill in an invalid value that can be recognized by asserts to be invalid.
+  std::fill(rowThatReducesCol.begin(), rowThatReducesCol.end(), pivotCount);
+#endif
+  for (SparseMatrix::RowIndex pivot = 0; pivot < pivotCount; ++pivot) {
+    MATHICGB_ASSERT(!reduceBy.emptyRow(pivot));
+    SparseMatrix::ColIndex col = reduceBy.leadCol(pivot);
+    MATHICGB_ASSERT(rowThatReducesCol[col] == pivotCount);
+    rowThatReducesCol[col] = pivot;
+  }
 
 #ifdef _OPENMP
   std::vector<DenseRow<uint64> > denseRowPerThread(threadCount);
@@ -323,7 +307,7 @@ void myReduce
     for (size_t pivot = 0; pivot < pivotCount; ++pivot) {
       if (denseRow[pivot] == 0)
         continue;
-      denseRow.rowReduceByUnitary(pivot, reduceBy, modulus);
+      denseRow.rowReduceByUnitary(rowThatReducesCol[pivot], reduceBy, modulus);
     }
     if (denseRow.takeModulus(modulus, pivotCount)) {
 #pragma omp critical
@@ -782,25 +766,19 @@ void F4MatrixReducer::reduce
 
   SparseMatrix::Scalar modulus = ring.charac();
 
-  SparseMatrix reducedD;
   {
     const SparseMatrix::ColIndex pivotColCount = matrix.topLeft.colCount();
 
     SparseMatrix matrixAB;
-    {
-      SparseMatrix tmp;
-      concatenateMatricesHorizontal(matrix.topLeft, matrix.topRight, tmp);
-      sortRowsByIncreasingPivots(tmp, matrixAB);
-    }
+    concatenateMatricesHorizontal(matrix.topLeft, matrix.topRight, matrixAB);
 
     SparseMatrix matrixCD;
     concatenateMatricesHorizontal
       (matrix.bottomLeft, matrix.bottomRight, matrixCD);
 
-    myReduce(matrixCD, matrixAB, modulus, reducedD, mThreadCount);
-    reducedD.trimLeadingZeroColumns(pivotColCount);
+    myReduce(matrixCD, matrixAB, modulus, newPivots, mThreadCount);
+    newPivots.trimLeadingZeroColumns(pivotColCount);
   }
 
-  myReduceToEchelonForm5(reducedD, modulus, mThreadCount);
-  sortRowsByIncreasingPivots(reducedD, newPivots);
+  myReduceToEchelonForm5(newPivots, modulus, mThreadCount);
 }
diff --git a/src/mathicgb/QuadMatrixBuilder.cpp b/src/mathicgb/QuadMatrixBuilder.cpp
index 209cea7..3c4e57d 100755
--- a/src/mathicgb/QuadMatrixBuilder.cpp
+++ b/src/mathicgb/QuadMatrixBuilder.cpp
@@ -6,6 +6,15 @@
 #include <mathic.h>
 #include <sstream>
 
+QuadMatrixBuilder::QuadMatrixBuilder(const PolyRing& ring):
+#ifndef MATHICGB_USE_QUADMATRIX_STD_HASH
+  mMonomialToCol(ArbitraryOrdering(ring)) {}
+#else
+mMonomialToCol(100, Hash(ring), Equal(ring)) {
+  mMonomialToCol.max_load_factor(0.3);
+}
+#endif
+
 namespace {
   /// Creates a column and updates the associated data structures that
   /// are passed in. Copies mono - ownership is not taken over. The
diff --git a/src/mathicgb/QuadMatrixBuilder.hpp b/src/mathicgb/QuadMatrixBuilder.hpp
index 48c8c69..c7021e9 100755
--- a/src/mathicgb/QuadMatrixBuilder.hpp
+++ b/src/mathicgb/QuadMatrixBuilder.hpp
@@ -25,12 +25,7 @@ class QuadMatrixBuilder {
   typedef SparseMatrix::ColIndex ColIndex;
   typedef SparseMatrix::Scalar Scalar;
 
-  QuadMatrixBuilder(const PolyRing& ring):
-#ifndef MATHICGB_USE_QUADMATRIX_STD_HASH
-    mMonomialToCol(ArbitraryOrdering(ring)) {}
-#else
-  mMonomialToCol(100, Hash(ring), Equal(ring)) {}
-#endif
+  QuadMatrixBuilder(const PolyRing& ring);
 
   /// The index of a column that can be either on the left or the
   /// right side. The largest representable ColIndex is an invalid
diff --git a/src/mathicgb/SparseMatrix.cpp b/src/mathicgb/SparseMatrix.cpp
index e08c54f..6697291 100755
--- a/src/mathicgb/SparseMatrix.cpp
+++ b/src/mathicgb/SparseMatrix.cpp
@@ -2,6 +2,7 @@
 #include "SparseMatrix.hpp"
 
 #include "Poly.hpp"
+#include <algorithm>
 
 std::ostream& operator<<(std::ostream& out, const SparseMatrix& matrix) {
   matrix.print(out);
@@ -19,3 +20,34 @@ void SparseMatrix::rowToPolynomial
       poly.appendTerm(it.scalar(), colMonomials[it.index()]);
   }
 }
+
+void SparseMatrix::sortRowsByIncreasingPivots() {
+  SparseMatrix ordered;
+
+  // compute pairs (pivot column index, row)
+  std::vector<std::pair<SparseMatrix::ColIndex, SparseMatrix::RowIndex> > order;
+  const SparseMatrix::RowIndex lRowCount = rowCount();
+  const SparseMatrix::ColIndex lColCount = colCount();
+  for (SparseMatrix::RowIndex row = 0; row < lRowCount; ++row) {
+    if (entryCountInRow(row) == 0)
+      order.push_back(std::make_pair(lColCount, row));
+    else
+      order.push_back(std::make_pair(rowBegin(row).index(), row));
+  }
+
+  // sort pairs by pivot column index
+  std::sort(order.begin(), order.end());
+
+  // construct ordered with pivot columns in increaing order
+  ordered.clear(lColCount);
+  for (size_t i = 0; i < lRowCount; ++i) {
+    const SparseMatrix::RowIndex row = order[i].second;
+    SparseMatrix::RowIterator it = rowBegin(row);
+    SparseMatrix::RowIterator end = rowEnd(row);
+    for (; it != end; ++it)
+      ordered.appendEntry(it.index(), it.scalar());
+    ordered.rowDone();
+  }
+
+  *this = std::move(ordered);
+}
diff --git a/src/mathicgb/SparseMatrix.hpp b/src/mathicgb/SparseMatrix.hpp
index b3712f4..77feb41 100755
--- a/src/mathicgb/SparseMatrix.hpp
+++ b/src/mathicgb/SparseMatrix.hpp
@@ -354,6 +354,10 @@ class SparseMatrix {
   void rowToPolynomial
   (RowIndex row, std::vector<monomial> colMonomials, Poly& poly);
 
+  /// Reorders the rows so that the index of the leading column in
+  /// each row is weakly increasing going from top to bottom. Quite
+  /// slow and it makes a copy internally.
+  void sortRowsByIncreasingPivots();
 
 private:
   friend class RowIterator;
diff --git a/src/test/F4MatrixReducer.cpp b/src/test/F4MatrixReducer.cpp
index d86e36d..c2839a3 100755
--- a/src/test/F4MatrixReducer.cpp
+++ b/src/test/F4MatrixReducer.cpp
@@ -123,5 +123,6 @@ TEST(F4MatrixReducer, Reduce) {
   const char* redStr =
     "0: 0#1 2#4 3#22 4#11\n"
     "1: 1#1 3#66 4#34\n";
+  reduced.sortRowsByIncreasingPivots();
   ASSERT_EQ(redStr, reduced.toString()) << "Printed reduced:\n" << reduced;
 }

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