[mathicgb] 160/393: Added support for logs that count and used it to add a log for counting the total number of rows added in all matrices in F4.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:53 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 c3952d40fab0e7647ed53806e07e434aa36a5cd6
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Thu Feb 7 17:47:16 2013 +0100

    Added support for logs that count and used it to add a log for counting the total number of rows added in all matrices in F4.
---
 src/mathicgb/F4MatrixReducer.cpp | 74 +++++++++++++++++++-------------------
 src/mathicgb/F4Reducer.cpp       | 43 +++++++++++++++--------
 src/mathicgb/LogDomain.hpp       | 76 ++++++++++++++++++++++++++++++++--------
 src/mathicgb/LogDomainSet.cpp    | 35 ++++++++++++++++--
 src/mathicgb/LogDomainSet.hpp    |  2 ++
 src/mathicgb/TypicalReducer.cpp  |  8 +++--
 6 files changed, 166 insertions(+), 72 deletions(-)

diff --git a/src/mathicgb/F4MatrixReducer.cpp b/src/mathicgb/F4MatrixReducer.cpp
index bab9c49..545eede 100644
--- a/src/mathicgb/F4MatrixReducer.cpp
+++ b/src/mathicgb/F4MatrixReducer.cpp
@@ -229,48 +229,48 @@ namespace {
     std::vector<SparseMatrix::RowIndex> rowOrder(rowCount);
 
     tbb::mutex lock;
-    tbb::parallel_for(tbb::blocked_range<SparseMatrix::RowIndex>(0, rowCount),
+    tbb::parallel_for(tbb::blocked_range<SparseMatrix::RowIndex>(0, rowCount, 2),
       [&](const tbb::blocked_range<SparseMatrix::RowIndex>& range)
-      {for (auto it = range.begin(); it != range.end(); ++it)
     {
-      const auto row = it;
       auto& denseRow = denseRowPerThread.local();
-
-      denseRow.clear(leftColCount);
-      denseRow.addRow(toReduceLeft, row);
-      MATHICGB_ASSERT(leftColCount == pivotCount);
-
-      for (size_t pivot = 0; pivot < pivotCount; ++pivot) {
-        if (denseRow[pivot] == 0)
-          continue;
-        auto entry = denseRow[pivot];
-        entry %= modulus;
-        if (entry == 0) {
-          denseRow[pivot] = 0;
-          continue;
+      for (auto it = range.begin(); it != range.end(); ++it) {
+        const auto row = it;
+        denseRow.clear(leftColCount);
+        denseRow.addRow(toReduceLeft, row);
+
+        MATHICGB_ASSERT(leftColCount == pivotCount);
+        for  (size_t pivot = 0; pivot < pivotCount; ++pivot) {
+          if (denseRow[pivot] != 0) {
+            auto entry = denseRow[pivot];
+            entry %= modulus;
+            if (entry == 0) {
+              denseRow[pivot] = 0;
+            } else {
+              entry = modulus - entry;
+              const auto row = rowThatReducesCol[pivot];
+              MATHICGB_ASSERT(row < pivotCount);
+              MATHICGB_ASSERT(!reduceByLeft.emptyRow(row));
+              MATHICGB_ASSERT(reduceByLeft.leadCol(row) == pivot);
+              MATHICGB_ASSERT(entry < std::numeric_limits<SparseMatrix::Scalar>::max());
+              denseRow.addRowMultiple(
+                static_cast<SparseMatrix::Scalar>(entry),
+                ++reduceByLeft.rowBegin(row),
+                reduceByLeft.rowEnd(row)
+              );
+              denseRow[pivot] = entry;
+            }
+          }
         }
-        entry = modulus - entry;
-        const auto row = rowThatReducesCol[pivot];
-        MATHICGB_ASSERT(row < pivotCount);
-        MATHICGB_ASSERT(!reduceByLeft.emptyRow(row));
-        MATHICGB_ASSERT(reduceByLeft.leadCol(row) == pivot);
-        MATHICGB_ASSERT(entry < std::numeric_limits<SparseMatrix::Scalar>::max());
-        denseRow.addRowMultiple(
-          static_cast<SparseMatrix::Scalar>(entry),
-          ++reduceByLeft.rowBegin(row),
-          reduceByLeft.rowEnd(row)
-        );
-        denseRow[pivot] = entry;
+        tbb::mutex::scoped_lock lockGuard(lock);
+        for (size_t pivot = 0; pivot < pivotCount; ++pivot) {
+		  MATHICGB_ASSERT(denseRow[pivot] < std::numeric_limits<SparseMatrix::Scalar>::max());
+          if (denseRow[pivot] != 0)
+            tmp.appendEntry(rowThatReducesCol[pivot], static_cast<SparseMatrix::Scalar>(denseRow[pivot]));
+	    }
+        tmp.rowDone();
+        rowOrder[tmp.rowCount() - 1] = row;
       }
-      tbb::mutex::scoped_lock lockGuard(lock);
-      for (size_t pivot = 0; pivot < pivotCount; ++pivot) {
-		MATHICGB_ASSERT(denseRow[pivot] < std::numeric_limits<SparseMatrix::Scalar>::max());
-        if (denseRow[pivot] != 0)
-          tmp.appendEntry(rowThatReducesCol[pivot], static_cast<SparseMatrix::Scalar>(denseRow[pivot]));
-	  }
-      tmp.rowDone();
-      rowOrder[tmp.rowCount() - 1] = row;
-    }});
+    });
 
     tbb::parallel_for(tbb::blocked_range<SparseMatrix::RowIndex>(0, rowCount),
       [&](const tbb::blocked_range<SparseMatrix::RowIndex>& range)
diff --git a/src/mathicgb/F4Reducer.cpp b/src/mathicgb/F4Reducer.cpp
index 20f3ccd..d729134 100755
--- a/src/mathicgb/F4Reducer.cpp
+++ b/src/mathicgb/F4Reducer.cpp
@@ -5,9 +5,15 @@
 #include "F4MatrixBuilder2.hpp"
 #include "F4MatrixReducer.hpp"
 #include "QuadMatrix.hpp"
+#include "LogDomain.hpp"
 #include <iostream>
 #include <limits>
 
+MATHICGB_DEFINE_LOG_DOMAIN(
+  F4MatrixRows,
+  "Count number of rows in F4 matrices."
+);
+
 F4Reducer::F4Reducer(const PolyRing& ring, Type type):
   mType(type),
   mFallback(Reducer::makeReducer(Reducer::Reducer_BjarkeGeo, ring)),
@@ -30,24 +36,22 @@ std::unique_ptr<Poly> F4Reducer::classicReduce
     std::cerr <<
       "F4Reducer: Using fall-back reducer for single classic reduction\n";
 
-  std::unique_ptr<Poly> p;
-  p = mFallback->classicReduce(poly, basis);
+  auto p = mFallback->classicReduce(poly, basis);
   mSigStats = mFallback->sigStats();
   mClassicStats = mFallback->classicStats();
-  return p;
+  return std::move(p);
 }
 
 std::unique_ptr<Poly> F4Reducer::classicTailReduce
 (const Poly& poly, const PolyBasis& basis) {
-  std::unique_ptr<Poly> p;
   if (tracingLevel >= 2)
     std::cerr <<
       "F4Reducer: Using fall-back reducer for single classic tail reduction\n";
 
-  p = mFallback->classicTailReduce(poly, basis);
+  auto p = mFallback->classicTailReduce(poly, basis);
   mSigStats = mFallback->sigStats();
   mClassicStats = mFallback->classicStats();
-  return p;
+  return std::move(p);
 }
 
 std::unique_ptr<Poly> F4Reducer::classicReduceSPoly(
@@ -58,7 +62,10 @@ std::unique_ptr<Poly> F4Reducer::classicReduceSPoly(
   if (tracingLevel >= 2)
     std::cerr << "F4Reducer: "
       "Using fall-back reducer for single classic S-pair reduction\n";
-  return mFallback->classicReduceSPoly(a, b, basis);
+  auto p = mFallback->classicReduceSPoly(a, b, basis);
+  mSigStats = mFallback->sigStats();
+  mClassicStats = mFallback->classicStats();
+  return std::move(p);
 }
 
 void F4Reducer::classicReduceSPolySet(
@@ -66,17 +73,19 @@ void F4Reducer::classicReduceSPolySet(
   const PolyBasis& basis,
   std::vector<std::unique_ptr<Poly> >& reducedOut
 ) {
-  if (spairs.size() <= 1) {
+  if (spairs.size() <= 1 && false) {
     if (tracingLevel >= 2)
       std::cerr << "F4Reducer: Using fall-back reducer for "
         << spairs.size() << " S-pairs.\n";
     mFallback->classicReduceSPolySet(spairs, basis, reducedOut);
+    mSigStats = mFallback->sigStats();
+    mClassicStats = mFallback->classicStats();
     return;
   }
   reducedOut.clear();
 
   MATHICGB_ASSERT(!spairs.empty());
-  if (tracingLevel >= 2)
+  if (tracingLevel >= 2 && false)
     std::cerr << "F4Reducer: Reducing " << spairs.size() << " S-polynomials.\n";
 
   SparseMatrix reduced;
@@ -100,6 +109,7 @@ void F4Reducer::classicReduceSPolySet(
         builder.buildMatrixAndClear(qm);
       }
     }
+    MATHICGB_LOG_INCREMENT_BY(F4MatrixRows, qm.rowCount());
     saveMatrix(qm);
     reduced = F4MatrixReducer(basis.ring().charac()).
       reducedRowEchelonFormBottomRight(qm);
@@ -109,7 +119,7 @@ void F4Reducer::classicReduceSPolySet(
       mRing.freeMonomial(*it);
   }
 
-  if (tracingLevel >= 2)
+  if (tracingLevel >= 2 && false)
     std::cerr << "F4Reducer: Extracted " << reduced.rowCount()
               << " non-zero rows\n";
 
@@ -128,18 +138,20 @@ void F4Reducer::classicReducePolySet
  const PolyBasis& basis,
  std::vector<std::unique_ptr<Poly> >& reducedOut)
 {
-  if (polys.size() <= 1) {
+  if (polys.size() <= 1 && false) {
     if (tracingLevel >= 2)
       std::cerr << "F4Reducer: Using fall-back reducer for "
                 << polys.size() << " polynomials.\n";
     mFallback->classicReducePolySet(polys, basis, reducedOut);
+    mSigStats = mFallback->sigStats();
+    mClassicStats = mFallback->classicStats();
     return;
   }
 
   reducedOut.clear();
 
   MATHICGB_ASSERT(!polys.empty());
-  if (tracingLevel >= 2)
+  if (tracingLevel >= 2 && false)
     std::cerr << "F4Reducer: Reducing " << polys.size() << " polynomials.\n";
 
   SparseMatrix reduced;
@@ -159,6 +171,7 @@ void F4Reducer::classicReducePolySet
         builder.buildMatrixAndClear(qm);
       }
     }
+    MATHICGB_LOG_INCREMENT_BY(F4MatrixRows, qm.rowCount());
     saveMatrix(qm);
     reduced = F4MatrixReducer(basis.ring().charac()).
       reducedRowEchelonFormBottomRight(qm);
@@ -168,7 +181,7 @@ void F4Reducer::classicReducePolySet
       mRing.freeMonomial(*it);
   }
 
-  if (tracingLevel >= 2)
+  if (tracingLevel >= 2 && false)
     std::cerr << "F4Reducer: Extracted " << reduced.rowCount()
               << " non-zero rows\n";
 
@@ -191,10 +204,10 @@ Poly* F4Reducer::regularReduce(
   if (tracingLevel >= 2)
     std::cerr <<
       "F4Reducer: Using fall-back reducer for single regular reduction\n";
-  Poly* p = mFallback->regularReduce(sig, multiple, basisElement, basis);
+  auto p = mFallback->regularReduce(sig, multiple, basisElement, basis);
   mSigStats = mFallback->sigStats();
   mClassicStats = mFallback->classicStats();
-  return p;
+  return std::move(p);
 }
 
 void F4Reducer::setMemoryQuantum(size_t quantum) {
diff --git a/src/mathicgb/LogDomain.hpp b/src/mathicgb/LogDomain.hpp
index 37da596..7b67b9d 100755
--- a/src/mathicgb/LogDomain.hpp
+++ b/src/mathicgb/LogDomain.hpp
@@ -16,8 +16,8 @@
 /// LogDomains properly have zero overhead. LogDomains can be turned on
 /// and off at compile time and at runtime individually.
 ///
-/// Compile-time enabled loggers automatically register themselves with
-/// LogDomainSet::singleton().
+/// Compile-time enabled loggers automatically register themselves at start-up
+/// with LogDomainSet::singleton().
 ///
 /// @todo: support turning all loggers off globally with a macro, regardless
 /// of their individual compile-time on/off setting.
@@ -44,16 +44,35 @@ public:
 
   std::ostream& stream();
 
-  bool hasTime() const {return mHasTime;}
-
   /// Class for recording time that is logged. Movable.
   class Timer;
 
   /// Returns a started timer.
   Timer timer();
 
+  /// Returns true if any time has been logged on this logger, even if the
+  /// duration of that time was zero (that is., less than the resolution
+  /// of the timer).
+  bool hasTime() const {return mHasTime;}
+
   double loggedSecondsReal() const;
 
+
+  typedef unsigned long long Counter;
+
+  Counter count() const {return mCount;}
+
+  void setCount(const Counter counter) {
+    if (enabled()) {
+      mCount = counter;
+      mHasCount = true;
+    }
+  }
+
+  /// Returns true if setCount has been called.
+  bool hasCount() const {return mHasCount;}
+
+
 private:
   struct TimeInterval {
     // todo: support user time too. clock() doesn't seem to sum the time
@@ -69,10 +88,10 @@ private:
   const char* mDescription;
 
   TimeInterval mInterval; /// Total amount of time recorded on this log.
+  bool mHasTime; /// Whether any time has been registered (even if 0s).
 
-  /// Indicates if any period of time has been recorded, even if that period
-  /// of time was recorded as 0 seconds.
-  bool mHasTime;
+  Counter mCount;
+  bool mHasCount; /// Whether the count has been set
 };
 
 class LogDomain<true>::Timer {
@@ -95,7 +114,7 @@ public:
 
   /// Start recording time on a stopped timer.
   ///
-  /// This is a no-op is the timer is already running or if the logger is
+  /// This is a no-op if the timer is already running or if the logger is
   /// disabled.
   void start();
 
@@ -105,7 +124,10 @@ private:
   tbb::tick_count mRealTicks; // high precision
 };
 
-/// This is a compile-time disabled logger.
+/// This is a compile-time disabled logger. You are not supposed to dynamically
+/// call any non-const methods on it other than the constructor. Code that
+/// calls other code will compile but it is an error if any of those
+/// methods get called at runtime.
 template<>
 class LogDomain<false> {
 public:
@@ -119,20 +141,27 @@ public:
   public:
     Timer(LogDomain<false>&) {}
     bool running() const {return false;}
-    void stop() {}
-    void start() {}
+    void stop() {MATHICGB_ASSERT(false);}
+    void start() {MATHICGB_ASSERT(false);}
   };
-  Timer timer() {return Timer(*this);}
+  Timer timer() {
+    MATHICGB_ASSERT(false);
+    return Timer(*this);
+  }
 
   std::ostream& stream() {
     MATHICGB_ASSERT(false);
     return *static_cast<std::ostream*>(0);
   }
+
+  typedef unsigned long long Counter;
+  Counter count() const {return 0;}
+  void setCount(const Counter counter) {MATHICGB_ASSERT(false);}
+  bool hasCount() const {return false;}
 };
 
 namespace LogDomainInternal {
-  // Support code for the logging macroes
-
+  // Helpers for the logging macroes
 
   template<class Tag, bool Default>
   struct SelectValue {static const bool value = Default;};
@@ -239,5 +268,22 @@ namespace LogDomainInternal {
   (MATHICGB_LOGGER(DOMAIN).timer()); \
   MATHICGB_LOG(DOMAIN)
 
-#endif
+/// Increments the count of DOMAIN by the value of the expression BY. The
+/// expression BY is evaluated at most once and it is not evaluated if
+/// DOMAIN is disabled.
+///
+/// Example:
+///   MATHICGB_LOG_INCREMENT_BY(MyDomain, 3);
+#define MATHICGB_LOG_INCREMENT_BY(DOMAIN, BY) \
+  do { \
+    auto& MGBLOG_log = MATHICGB_LOGGER(DOMAIN); \
+    if (MGBLOG_log.enabled()) { \
+      MGBLOG_log.setCount(MGBLOG_log.count() + BY); \
+    } \
+  } while (false)
+
+/// Increments the count of DOMAIN by 1.
+#define MATHICGB_LOG_INCREMENT(DOMAIN) \
+  MATHICGB_LOG_INCREMENT_BY(DOMAIN, 1)
 
+#endif
diff --git a/src/mathicgb/LogDomainSet.cpp b/src/mathicgb/LogDomainSet.cpp
index 01712c1..ad0f8a5 100644
--- a/src/mathicgb/LogDomainSet.cpp
+++ b/src/mathicgb/LogDomainSet.cpp
@@ -20,6 +20,37 @@ LogDomain<true>* LogDomainSet::logDomain(const char* const name) {
 }
 
 void LogDomainSet::printReport(std::ostream& out) const {
+  printCountReport(out);
+  printTimeReport(out);
+}
+
+void LogDomainSet::printCountReport(std::ostream& out) const {
+  mathic::ColumnPrinter pr;
+  auto& names = pr.addColumn(true);
+  auto& counts = pr.addColumn(false);
+
+  names << "Log name  \n";
+  counts << "  Count\n";
+  pr.repeatToEndOfLine('-');
+
+  bool somethingToReport = false;
+  const auto end = logDomains().cend();
+  for (auto it = logDomains().cbegin(); it != end; ++it) {
+    const auto& log = **it;
+    if (!log.enabled() || !log.hasCount())
+      continue;
+    somethingToReport = true;
+
+    names << log.name() << "  \n";
+    counts << "  " << mathic::ColumnPrinter::commafy(log.count()) << '\n';
+  }
+  if (!somethingToReport)
+    return;
+
+  out << "***** Logging count report *****\n\n" << pr << '\n';
+}
+
+void LogDomainSet::printTimeReport(std::ostream& out) const {
   const auto allTime = (tbb::tick_count::now() - mStartTime).seconds();
 
   mathic::ColumnPrinter pr;
@@ -47,7 +78,7 @@ void LogDomainSet::printReport(std::ostream& out) const {
 
     const auto logTime = log.loggedSecondsReal();
     timeSum += logTime;
-    names << log.name() << '\n';
+    names << log.name() << "  \n";
     times << logTime << '\n';
     ratios << mathic::ColumnPrinter::percentDouble(logTime, allTime) << '\n';
   }
@@ -62,7 +93,7 @@ void LogDomainSet::printReport(std::ostream& out) const {
   const auto oldPrecision = out.precision();
   out << std::fixed;
   out.precision(3);
-  out << "***** Logging report *****\nTime elapsed: "
+  out << "***** Logging time report *****\nTime elapsed: "
     << allTime << "s\n\n" << pr << '\n';
 
   // todo: restore the stream state using RAII, since the above code might
diff --git a/src/mathicgb/LogDomainSet.hpp b/src/mathicgb/LogDomainSet.hpp
index 9385f59..1a0a324 100755
--- a/src/mathicgb/LogDomainSet.hpp
+++ b/src/mathicgb/LogDomainSet.hpp
@@ -18,6 +18,8 @@ public:
   const std::vector<LogDomain<true>*>& logDomains() const {return mLogDomains;}
 
   void printReport(std::ostream& out) const;
+  void printTimeReport(std::ostream& out) const;
+  void printCountReport(std::ostream& out) const;
 
   static LogDomainSet& singleton();
 
diff --git a/src/mathicgb/TypicalReducer.cpp b/src/mathicgb/TypicalReducer.cpp
index 7b55e74..d8ee1bf 100755
--- a/src/mathicgb/TypicalReducer.cpp
+++ b/src/mathicgb/TypicalReducer.cpp
@@ -49,13 +49,14 @@ Poly* TypicalReducer::regularReduce(
   Poly* result = new Poly(ring);
 
   unsigned long long steps = 2; // number of steps in this reduction
-  for (const_term v; leadTerm(v); ++steps) {
+  for (const_term v; leadTerm(v);) {
     MATHICGB_ASSERT(v.coeff != 0);
     reducer = basis.regularReducer(sig, v.monom);
     if (reducer == static_cast<size_t>(-1)) { // no reducer found
       result->appendTerm(v.coeff, v.monom);
       removeLeadTerm();
     } else { // reduce by reducer
+      ++steps;
       basis.basis().usedAsReducer(reducer);
       monomial mon = ring.allocMonomial(mArena);
       ring.monomialDivide(v.monom, basis.getLeadMonomial(reducer), mon);
@@ -168,8 +169,8 @@ std::unique_ptr<Poly> TypicalReducer::classicReduce
     std::cerr << "Classic reduction begun." << std::endl;
 
   coefficient coef;
-  unsigned long long steps = 1; // number of steps in this reduction
-  for (const_term v; leadTerm(v); ++steps) {
+  unsigned long long steps = 0; // number of steps in this reduction
+  for (const_term v; leadTerm(v);) {
     if (tracingLevel > 100) {
       std::cerr << "from reducer queue: ";
       basis.ring().monomialDisplay(std::cerr, v.monom);
@@ -183,6 +184,7 @@ std::unique_ptr<Poly> TypicalReducer::classicReduce
       result->appendTerm(v.coeff, v.monom);
       removeLeadTerm();
     } else { // reduce by reducer
+      ++steps;
       basis.usedAsReducer(reducer);
       monomial mon = ring.allocMonomial(mArena);
       ring.monomialDivide(v.monom, basis.leadMonomial(reducer), mon);

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