[mathicgb] 363/393: Documentation improvements and slight clean-up on F4 code.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:35 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 99891532374318c264f72a8cfeea4d210eebc81c
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Wed Sep 11 15:02:01 2013 +0200

    Documentation improvements and slight clean-up on F4 code.
---
 src/mathicgb/F4MatrixBuilder.cpp   |   2 -
 src/mathicgb/F4MatrixBuilder2.cpp  |  33 ++--------
 src/mathicgb/F4MatrixBuilder2.hpp  | 120 +++++++++++++++++++++++--------------
 src/mathicgb/F4Reducer.cpp         |   8 +--
 src/mathicgb/QuadMatrix.hpp        |  16 +++--
 src/mathicgb/QuadMatrixBuilder.hpp |  14 ++---
 6 files changed, 96 insertions(+), 97 deletions(-)

diff --git a/src/mathicgb/F4MatrixBuilder.cpp b/src/mathicgb/F4MatrixBuilder.cpp
index 988aa25..46ebfe0 100755
--- a/src/mathicgb/F4MatrixBuilder.cpp
+++ b/src/mathicgb/F4MatrixBuilder.cpp
@@ -411,8 +411,6 @@ void F4MatrixBuilder::appendRowBottom(
     const auto colB = findOrCreateColumn
       (itB.getMonomial(), mulB, colMap, feeder);
     const auto cmp = ring().monomialCompare(colA.second, colB.second);
-    //const auto cmp = ring().monomialCompare
-    //  (builder.monomialOfCol(colA), builder.monomialOfCol(colB));
     if (cmp != LT) {
       coeff = itA.getCoefficient();
       col = colA.first;
diff --git a/src/mathicgb/F4MatrixBuilder2.cpp b/src/mathicgb/F4MatrixBuilder2.cpp
index dcdf43a..bbee57c 100755
--- a/src/mathicgb/F4MatrixBuilder2.cpp
+++ b/src/mathicgb/F4MatrixBuilder2.cpp
@@ -44,22 +44,13 @@ F4MatrixBuilder2::findOrCreateColumn(
   MATHICGB_ASSERT(!monoA.isNull());
   MATHICGB_ASSERT(!monoB.isNull());
   const auto col = colMap.findProduct(monoA, monoB);
-  if (col.first == 0)
+  if (col.first == 0) {
+    // The reader may be out of date, so try again with a fresh reader.
     return findOrCreateColumn(monoA, monoB, feeder);
+  }
   return std::make_pair(*col.first, col.second);
 }
 
-MATHICGB_NO_INLINE
-void F4MatrixBuilder2::createTwoColumns(
-  const const_monomial monoA1,
-  const const_monomial monoA2,
-  const const_monomial monoB,
-  TaskFeeder& feeder
-) {
-  createColumn(monoA1, monoB, feeder);
-  createColumn(monoA2, monoB, feeder);
-}
-
 F4MatrixBuilder2::F4MatrixBuilder2(
   const PolyBasis& basis,
   const size_t memoryQuantum
@@ -372,11 +363,8 @@ void F4MatrixBuilder2::appendRow(
       (it.getMonomial(), multiple, reader, feeder);
 	MATHICGB_ASSERT(it.getCoefficient() < std::numeric_limits<Scalar>::max());
     MATHICGB_ASSERT(it.getCoefficient());
-    //matrix.appendEntry(col.first, static_cast<Scalar>(it.getCoefficient()));
     *indices = col.first;
     ++indices;
-    //*row.first++ = col.first;
-    //*row.second++ = static_cast<Scalar>(it.getCoefficient());
     ++it;
   }
 updateReader:
@@ -397,26 +385,18 @@ updateReader:
 
     const auto colPair = colMap.findTwoProducts(mono1, mono2, multiple);
     if (colPair.first == 0 || colPair.second == 0) {
-      createTwoColumns(mono1, mono2, multiple, feeder);
+      createColumn(mono1, multiple, feeder);
+      createColumn(mono2, multiple, feeder);
       goto updateReader;
     }
 
-    //matrix.appendEntry(*colPair.first, scalar1);
-    //matrix.appendEntry(*colPair.second, scalar2);
-
     *indices = *colPair.first;
     ++indices;
     *indices = *colPair.second;
     ++indices;
 
-    //*row.first++ = *colPair.first;
-    //*row.second++ = scalar1;
-    //*row.first++ = *colPair.second;
-    //*row.second++ = scalar2;
-
     it = ++it2;
   }
-  //matrix.rowDone();
 }
 
 void F4MatrixBuilder2::appendRowSPair(
@@ -476,7 +456,6 @@ void F4MatrixBuilder2::appendRowSPair(
     }
     MATHICGB_ASSERT(coeff < std::numeric_limits<Scalar>::max());
     if (coeff != 0) {
-      //matrix.appendEntry(col, static_cast<Scalar>(coeff));
       *row.first++ = col;
       *row.second++ = static_cast<Scalar>(coeff);
     }
@@ -485,7 +464,6 @@ void F4MatrixBuilder2::appendRowSPair(
   for (; itA != endA; ++itA) {
     const auto colA = findOrCreateColumn
       (itA.getMonomial(), mulA, colMap, feeder);
-    //matrix.appendEntry(colA.first, static_cast<Scalar>(itA.getCoefficient()));
     *row.first++ = colA.first;
     *row.second++ = static_cast<Scalar>(itA.getCoefficient());
   }
@@ -494,7 +472,6 @@ void F4MatrixBuilder2::appendRowSPair(
     const auto colB = findOrCreateColumn
       (itB.getMonomial(), mulB, colMap, feeder);
     const auto negative = ring().coefficientNegate(itB.getCoefficient());
-    //matrix.appendEntry(colB.first, static_cast<Scalar>(negative));
     *row.first++ = colB.first;
     *row.second++ = static_cast<Scalar>(negative);
   }
diff --git a/src/mathicgb/F4MatrixBuilder2.hpp b/src/mathicgb/F4MatrixBuilder2.hpp
index dde14f0..4088bd4 100755
--- a/src/mathicgb/F4MatrixBuilder2.hpp
+++ b/src/mathicgb/F4MatrixBuilder2.hpp
@@ -15,11 +15,11 @@
 
 MATHICGB_NAMESPACE_BEGIN
 
-/** Class for constructing an F4 matrix.
-
-  @todo: this class does not offer exception guarantees. It's just not
-  very workable without an RAII monomial handle or a scope exit functionality,
-  so add one of those before fixing this. */
+/// Class for constructing an F4 matrix.
+///
+/// @todo: this class does not offer exception guarantees. It's just not
+/// very workable without an RAII monomial handle or a scope exit
+/// functionality, so add one of those before fixing this.
 class F4MatrixBuilder2 {
 private:
   typedef SparseMatrix::ColIndex ColIndex;
@@ -33,39 +33,41 @@ public:
   /// small and double the quantum at each exhaustion.
   F4MatrixBuilder2(const PolyBasis& basis, 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
-    and polyB must remain valid until the matrix is constructed.
-
-    Currently, the two monomials must be monic, though this is just
-    because they always happen to be monic so there was no reason to
-    support the non-monic case. */
+  /// Schedules a row representing the S-polynomial between polyA and
+  /// polyB to be added to the matrix. No ownership is taken, but polyA
+  /// and polyB must remain valid until the matrix is constructed.
+  ///
+  /// Currently, the two monomials must be monic, though this is just
+  /// because they happen always to be monic so there was no reason to
+  /// support the non-monic case.
   void addSPolynomialToMatrix(const Poly& polyA, const Poly& polyB);
 
-  /** Schedules a row representing multiple*poly to be added to the
-    matrix. No ownership is taken, but poly must remain valid until
-    the matrix is constructed. multiple is copied so it need not
-    remain valid. */
+  /// Schedules a row representing multiple*poly to be added to the
+  /// matrix. No ownership is taken, but poly must remain valid until
+  /// the matrix is constructed. multiple is copied, so it need not
+  /// remain valid.
   void addPolynomialToMatrix(const_monomial multiple, const Poly& poly);
 
-  /// as the overload with a multiple, just letting multiple be the
-  /// identity.
+  /// As the overload with a multiple, where the multiple is 1.
   void addPolynomialToMatrix(const Poly& poly);
 
-  /** Builds an F4 matrix to the specifications given. Also clears the
-    information in this object.
-
-    The right columns are ordered by decreasing monomial of each
-    column according to the order from the basis. The left columns are
-    ordered in some way so that the first entry in each top row (the
-    pivot) has a lower index than any other entries in that row.
-
-    The matrix contains a reducer/pivot for every monomial that can be
-    reduced by the basis and that is present in the matrix. There is
-    no guarantee that the bottom part of the matrix contains rows that
-    exactly correspond to the polynomials that have been scheduled to
-    be added to the matrix. It is only guaranteed that the whole matrix has
-    the same row-space as though that had been the case. */
+  /// Builds an F4 matrix to the specifications given. Also clears the
+  /// information in this object.
+  ///
+  /// The right columns are in order of strictly decreasing monomial.
+  /// The left columns are ordered in some way so that the leading non-zero
+  /// entry in each top row has the maximal column monomial out of all
+  /// non-zero entries in that row.
+  ///
+  /// The monomials that can be reduced by some element of the basis go on
+  /// the left while the remaining monomials go on the right. The upper left
+  /// matrix is upper triangular, thus having a reducer/pivot row for every
+  /// column.
+  ///
+  /// There is no guarantee that the bottom part of the matrix contains rows
+  /// that exactly correspond to the polynomials that have been scheduled to
+  /// be added to the matrix. It is only guaranteed that the whole matrix has
+  /// the same row-space as though that had been the case.
   void buildMatrixAndClear(QuadMatrix& matrix);
 
   const PolyRing& ring() const {return mBasis.ring();}
@@ -80,15 +82,23 @@ private:
   /// where multiply and sPairMultiply are such that the leading terms become
   /// desiredLead.
   struct RowTask {
-    monomial desiredLead; // multiply monomial onto poly to get this lead
+    monomial desiredLead; // multiply a monomial onto poly to get this lead
     const Poly* poly;
     const Poly* sPairPoly;
   };
   typedef mgb::mtbb::parallel_do_feeder<RowTask> TaskFeeder;
 
-  /// Creates a column with monomial label x and schedules a new row to
-  /// reduce that column if possible. Here x is monoA if monoB is
-  /// null and otherwise x is the product of monoA and monoB.
+  /// Creates a column with monomial label monoA * monoB and schedules a new
+  /// row to reduce that column if possible. If such a column already
+  /// exists, then a new column is not inserted. In either case, returns
+  /// the column index and column monomial corresponding to monoA * monoB.
+  ///
+  /// createColumn can be used simply to search for an existing column, but
+  /// since createColumn incurs locking overhead, this is not a good idea.
+  /// Note that createColumn has to work correctly for pre-existing columns
+  /// because the only way to be *certain* that no other thread has inserted
+  /// the column of interest is to grab a lock, and the lock being grabbed
+  /// is being grabbed inside createColumn.
   MATHICGB_NO_INLINE
   std::pair<ColIndex, ConstMonomial> createColumn(
     const_monomial monoA,
@@ -96,12 +106,16 @@ private:
     TaskFeeder& feeder
   );
 
+  /// Append multiple * poly to block, creating new columns as necessary.
   void appendRow(
     const_monomial multiple,
     const Poly& poly,
     F4ProtoMatrix& block,
     TaskFeeder& feeder
   );
+
+  /// Append poly*multiply - sPairPoly*sPairMultiply to block, creating new
+  /// columns as necessary.
   void appendRowSPair(
     const Poly* poly,
     monomial multiply,
@@ -111,13 +125,19 @@ private:
     TaskFeeder& feeder
   );
 
+  /// As createColumn, except with much better performance in the common
+  /// case that the column for monoA * monoB already exists. In particular,
+  /// no lock is grabbed in that case.
   MATHICGB_NO_INLINE
   std::pair<ColIndex, ConstMonomial> findOrCreateColumn(
     const_monomial monoA,
     const_monomial monoB,
     TaskFeeder& feeder
   );
-  
+
+  /// As the overload that does not take a ColReader parameter, except with
+  /// better performance in the common case that the column already exists
+  /// and colMap is up-to-date.
   MATHICGB_INLINE
   std::pair<ColIndex, ConstMonomial> findOrCreateColumn(
     const_monomial monoA,
@@ -126,20 +146,30 @@ private:
     TaskFeeder& feeder
   );
 
-  MATHICGB_NO_INLINE
-  void createTwoColumns(
-    const const_monomial monoA1,
-    const const_monomial monoA2,
-    const const_monomial monoB,
-    TaskFeeder& feeder
-  );
-
+  /// The split into left and right columns is not done until the whole matrix
+  /// has been constructed. This vector keeps track of which side each column
+  /// should go to once we do the split. char is used in place of bool because
+  /// the specialized bool would just be slower for this use case. See
+  /// http://isocpp.org/blog/2012/11/on-vectorbool .
   std::vector<char> mIsColumnToLeft;
+
+  /// How much memory to allocate every time more memory is needed.
   const size_t mMemoryQuantum;
+
+  /// If you want to modify the columns, you need to grab this lock first.
   mgb::mtbb::mutex mCreateColumnLock;
+
+  /// A monomial for temporary scratch calculations. Protected by
+  /// mCreateColumnLock.
   monomial mTmp;
+
+  /// The basis that supplies reducers.
   const PolyBasis& mBasis;
+
+  /// Mapping from monomials to column indices.
   Map mMap;
+
+  /// Stores the rows that have been scheduled to be added.
   std::vector<RowTask> mTodo;
 };
 
diff --git a/src/mathicgb/F4Reducer.cpp b/src/mathicgb/F4Reducer.cpp
index ae219f6..491c662 100755
--- a/src/mathicgb/F4Reducer.cpp
+++ b/src/mathicgb/F4Reducer.cpp
@@ -168,9 +168,9 @@ std::unique_ptr<Poly> F4Reducer::classicReduceSPoly(
 }
 
 void F4Reducer::classicReduceSPolySet(
-  std::vector< std::pair<size_t, size_t> >& spairs,
+  std::vector<std::pair<size_t, size_t>>& spairs,
   const PolyBasis& basis,
-  std::vector< std::unique_ptr<Poly> >& reducedOut
+  std::vector<std::unique_ptr<Poly>>& reducedOut
 ) {
   if (spairs.size() <= 1 && false) {
     if (tracingLevel >= 2)
@@ -219,10 +219,6 @@ void F4Reducer::classicReduceSPolySet(
       mRing.freeMonomial(*it);
   }
 
-  if (tracingLevel >= 2 && false)
-    std::cerr << "F4Reducer: Extracted " << reduced.rowCount()
-              << " non-zero rows\n";
-
   for (SparseMatrix::RowIndex row = 0; row < reduced.rowCount(); ++row) {
     auto p = make_unique<Poly>(basis.ring());
     reduced.rowToPolynomial(row, monomials, *p);
diff --git a/src/mathicgb/QuadMatrix.hpp b/src/mathicgb/QuadMatrix.hpp
index 37127a5..91485e2 100755
--- a/src/mathicgb/QuadMatrix.hpp
+++ b/src/mathicgb/QuadMatrix.hpp
@@ -11,15 +11,13 @@
 
 MATHICGB_NAMESPACE_BEGIN
 
-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. */
+/// 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() {}
diff --git a/src/mathicgb/QuadMatrixBuilder.hpp b/src/mathicgb/QuadMatrixBuilder.hpp
index ee6e3d7..2d44d9d 100755
--- a/src/mathicgb/QuadMatrixBuilder.hpp
+++ b/src/mathicgb/QuadMatrixBuilder.hpp
@@ -17,10 +17,10 @@ MATHICGB_NAMESPACE_BEGIN
 
 class QuadMatrix;
 
-/** Builder for QuadMatrix. This is not quite the builder pattern in
-  that the interface is not virtual and the implementation cannot be
-  swapped out - it only follows the builder pattern in that it is a
-  class that allows step-wise construction of a final product. */
+/// Builder for QuadMatrix. This is not quite the builder pattern in
+/// that the interface is not virtual and the implementation cannot be
+/// swapped out - it only follows the builder pattern in that it is a
+/// class that allows step-wise construction of a final product.
 class QuadMatrixBuilder {
  public:
   typedef SparseMatrix::RowIndex RowIndex;
@@ -36,8 +36,8 @@ class QuadMatrixBuilder {
     LeftRightColIndex():
       mRawIndex(std::numeric_limits<ColIndex>::max()), mLeft(false) {}
     LeftRightColIndex(ColIndex index, bool left):
-      mRawIndex(index), mLeft(left) {
-    }
+      mRawIndex(index), mLeft(left)
+    {}
 
     ColIndex leftIndex() const {
       MATHICGB_ASSERT(left());
@@ -50,7 +50,7 @@ class QuadMatrixBuilder {
     }
 
     /// Use leftIndex() or rightIndex() instead if you know what side
-    /// you are expecting, as this does an assert on your expectation.
+    /// you are expecting, as those do an assert on your expectation.
     ColIndex index() const {
       MATHICGB_ASSERT(valid());
       return mRawIndex;

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