[mathicgb] 183/393: Added new log functionality to enable streaming output and the summary at the end independently.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:57 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 41df6c1c49fad6f76077e0d492b626fe5d293872
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Thu Mar 7 11:58:01 2013 +0100

    Added new log functionality to enable streaming output and the summary at the end independently.
---
 src/cli/CommonParams.cpp         | 20 +--------------
 src/mathicgb/F4MatrixReducer.cpp |  6 +++--
 src/mathicgb/LogDomain.cpp       | 14 +++++++----
 src/mathicgb/LogDomain.hpp       | 53 +++++++++++++++++++++++++++-------------
 src/mathicgb/LogDomainSet.cpp    | 40 ++++++++++++++++++++++++++++++
 src/mathicgb/LogDomainSet.hpp    | 31 +++++++++++++++++++++++
 src/mathicgb/SPairs.cpp          | 46 ++++++++++++++++++++++++----------
 src/mathicgb/SPairs.hpp          |  2 +-
 8 files changed, 155 insertions(+), 57 deletions(-)

diff --git a/src/cli/CommonParams.cpp b/src/cli/CommonParams.cpp
index f5a9044..2c24ca8 100644
--- a/src/cli/CommonParams.cpp
+++ b/src/cli/CommonParams.cpp
@@ -45,25 +45,7 @@ void CommonParams::pushBackParameters(
 }
 
 void CommonParams::perform() {
-  size_t offset = 0;
-  const auto& logStr = mLogs.value();
-  auto& logs = LogDomainSet::singleton();
-  while (offset < logStr.size()) {
-    size_t next = logStr.find(',', offset);
-    const auto name = logStr.substr(offset, next - offset);
-    auto log = logs.logDomain(name.c_str());
-    if (log == 0) {
-      std::ostringstream out;
-      out << "Unknown log domain \"" << name <<
-        "\" extracted from -log param \"" << logStr << "\".\n";
-      mathic::reportError(out.str());
-    }
-    log->setEnabled(true);
-    if (next < logStr.size())
-      ++next;
-    offset = next;
-  }
-
+  LogDomainSet::singleton().performLogCommands(mLogs.value());
   tracingLevel = mTracingLevel.value();
 
   // delete the old init object first to make the new one take control.
diff --git a/src/mathicgb/F4MatrixReducer.cpp b/src/mathicgb/F4MatrixReducer.cpp
index 545eede..6240fe4 100644
--- a/src/mathicgb/F4MatrixReducer.cpp
+++ b/src/mathicgb/F4MatrixReducer.cpp
@@ -678,7 +678,8 @@ SparseMatrix F4MatrixReducer::reduceToBottomRight(const QuadMatrix& matrix) {
   MATHICGB_ASSERT(matrix.debugAssertValid());
   MATHICGB_LOG_TIME(F4MatrixReduce) <<
     "\n***** Reducing QuadMatrix to bottom right matrix *****\n";
-  MATHICGB_IF_LOG(F4MatrixReduce) {matrix.printStatistics(log.stream());};
+  MATHICGB_IF_STREAM_LOG(F4MatrixReduce)
+    {matrix.printStatistics(log.stream());};
   return reduce(matrix, mModulus);
 }
 
@@ -687,7 +688,8 @@ SparseMatrix F4MatrixReducer::reducedRowEchelonForm(
 ) {
   MATHICGB_LOG_TIME(F4MatrixReduce) <<
     "\n***** Reducing SparseMatrix to reduced row echelon form *****\n";
-  MATHICGB_IF_LOG(F4MatrixReduce) {matrix.printStatistics(log.stream());};
+  MATHICGB_IF_STREAM_LOG(F4MatrixReduce)
+    {matrix.printStatistics(log.stream());};
 
   const bool useShrawan = false;
   const bool useDelayedModulus = false;
diff --git a/src/mathicgb/LogDomain.cpp b/src/mathicgb/LogDomain.cpp
index 7476d81..f75b87e 100755
--- a/src/mathicgb/LogDomain.cpp
+++ b/src/mathicgb/LogDomain.cpp
@@ -11,9 +11,11 @@ static const auto logDomainGlobalStartTime= tbb::tick_count::now();
 LogDomain<true>::LogDomain(
   const char* const name,
   const char* const description,
-  const bool enabled
+  const bool enabled,
+  const bool streamEnabled
 ):
   mEnabled(enabled),
+  mStreamEnabled(streamEnabled),
   mName(name),
   mDescription(description),
   mInterval()
@@ -50,10 +52,12 @@ void LogDomain<true>::recordTime(TimeInterval interval) {
   mInterval.realSeconds += interval.realSeconds;
   mHasTime = true;
 
-  MATHICGB_ASSERT(mName != 0);
-  stream() << mName << " time recorded:        ";
-  interval.print(stream());
-  stream() << std::endl;
+  if (streamEnabled()) {
+    MATHICGB_ASSERT(mName != 0);
+    stream() << mName << " time recorded:        ";
+    interval.print(stream());
+    stream() << std::endl;
+  }
 }
 
 LogDomain<true>::Timer::Timer(LogDomain<true>& logger):
diff --git a/src/mathicgb/LogDomain.hpp b/src/mathicgb/LogDomain.hpp
index 7b67b9d..57cd0bb 100755
--- a/src/mathicgb/LogDomain.hpp
+++ b/src/mathicgb/LogDomain.hpp
@@ -33,14 +33,17 @@ public:
   LogDomain(
     const char* const name,
     const char* const description,
-    const bool enabled
+    const bool enabled,
+    const bool streamEnabled
   );
 
   const char* name() const {return mName;}
   const char* description() const {return mDescription;}
   bool enabled() const {return mEnabled;}
+  bool streamEnabled() const {return enabled() && mStreamEnabled;}
 
   void setEnabled(const bool enabled) {mEnabled = enabled;}
+  void setStreamEnabled(const bool enabled) {mStreamEnabled = enabled;}
 
   std::ostream& stream();
 
@@ -72,7 +75,6 @@ public:
   /// 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
@@ -84,6 +86,7 @@ private:
   void recordTime(TimeInterval interval);
 
   bool mEnabled;
+  bool mStreamEnabled;
   const char* mName;
   const char* mDescription;
 
@@ -136,6 +139,7 @@ public:
   LogDomain(const char* const, const char* const, const bool) {}
 
   bool enabled() const {return false;}
+  bool streamEnabled() const {return false;}
 
   class Timer {
   public:
@@ -203,19 +207,34 @@ namespace LogDomainInternal {
 ///
 /// The logger is default compile-time enabled depending on MATHICGB_LOG_##NAME
 /// (see MATHICGB_CAPTURE_LOG_ENABLED) and it is initially runtime
-/// enabled depending on the value of DEFAULT_RUNTIME_ENABLED.
-#define MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(NAME, DESCRIPTION, DEFAULT_RUNTIME_ENABLED, DEFAULT_COMPILE_TIME_ENABLED) \
+/// enabled depending on the value of DEFAULT_RUNTIME_ENABLED. It is default
+/// runtime enabled for streaming (when also enabled in general) depending on
+/// DEFAULT_RUNTIME_STREAM_ENABLED.
+#define MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS( \
+  NAME, DESCRIPTION, \
+  DEFAULT_RUNTIME_ENABLED, \
+  DEFAULT_RUNTIME_STREAM_ENABLED, \
+  DEFAULT_COMPILE_TIME_ENABLED \
+) \
   MATHICGB_CAPTURE_LOG_ENABLED(NAME, DEFAULT_COMPILE_TIME_ENABLED); \
   namespace logs { \
     typedef LogDomain< ::LogDomainInternal::value_##NAME> Type##NAME; \
-    Type##NAME NAME(#NAME, DESCRIPTION, DEFAULT_RUNTIME_ENABLED); \
+    Type##NAME NAME( \
+      #NAME, \
+      DESCRIPTION, \
+      DEFAULT_RUNTIME_ENABLED, \
+      DEFAULT_RUNTIME_STREAM_ENABLED \
+    ); \
   }
 
 /// Defines a LogDomain with the given name and description.
 ///
-/// By default, the logger is compile-time enabled and runtime disabled.
+/// The defaults for the logger are as follows.
+///       compile-time: enabled,
+///            runtime: disabled,
+///  runtime streaming: enabled (only takes effect if also enabled)
 #define MATHICGB_DEFINE_LOG_DOMAIN(NAME, DESCRIPTION) \
-  MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(NAME, DESCRIPTION, 0, 1);
+  MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(NAME, DESCRIPTION, 0, 1, 1);
 
 /// This expression yields an l-value reference to the indicated logger.
 ///
@@ -230,30 +249,30 @@ namespace LogDomainInternal {
 ///     std::ostream << "MyDomain is compiled time enabled";
 #define MATHICGB_LOGGER_TYPE(DOMAIN) ::logs::Type##DOMAIN
 
-/// Runs the code in the following scope delimited by braces {} if the indicated
-/// logger is enabled - otherwise does nothing. Within the following scope
-/// there is a local reference variable log that refers to the indicated
-/// logger.
+/// Runs the code in the following scope delimited by braces {} if the
+/// indicated logger is enabled for streaming - otherwise does nothing.
+/// Within the following scope there is a local reference variable log
+/// that refers to the indicated logger.
 ///
 /// Example:
-///   MATHICGB_IF_LOG(MyDomain) {
+///   MATHICGB_IF_STREAM_LOG(MyDomain) {
 ///     std::string msg;
 ///     expensiveFunction(msg);
 ///     log << msg;
 ///   }
-#define MATHICGB_IF_LOG(DOMAIN) \
-  if (MATHICGB_LOGGER(DOMAIN).enabled()) \
+#define MATHICGB_IF_STREAM_LOG(DOMAIN) \
+  if (MATHICGB_LOGGER(DOMAIN).streamEnabled()) \
     LogDomainInternal::lambdaRunner(MATHICGB_LOGGER(DOMAIN)) + \
       [&](MATHICGB_LOGGER_TYPE(DOMAIN)& log)
 
 /// Display information to the log using <<.
-/// If domain is not enabled then the log message is not displayed and the
-/// code after << is not executed.
+/// If domain is not enabled and stream enabled then the log message is not
+/// displayed and the code after << is not executed.
 ///
 /// Example: (f() only called if logger is enabled)
 ///   MATHICGB_LOG(domain) << "f() = " << f();
 #define MATHICGB_LOG(DOMAIN) \
-  if (MATHICGB_LOGGER(DOMAIN).enabled()) MATHICGB_LOGGER(DOMAIN).stream()
+  if (MATHICGB_LOGGER(DOMAIN).streamEnabled()) MATHICGB_LOGGER(DOMAIN).stream()
 
 /// Will log the time to execute the remaining code in the current scope
 /// to the indicated domain. Also supports printing a message using <<.
diff --git a/src/mathicgb/LogDomainSet.cpp b/src/mathicgb/LogDomainSet.cpp
index ad0f8a5..e4c24e7 100644
--- a/src/mathicgb/LogDomainSet.cpp
+++ b/src/mathicgb/LogDomainSet.cpp
@@ -19,6 +19,46 @@ LogDomain<true>* LogDomainSet::logDomain(const char* const name) {
   return it == mLogDomains.end() ? static_cast<LogDomain<true>*>(0) : *it;
 }
 
+void LogDomainSet::performLogCommand(std::string cmd) {
+  if (cmd.empty())
+    return;
+
+  // This could be more efficient, but this is not supposed to be a
+  // method that is called very often.
+  const auto isSign = [](const char c) {return c == '+' || c == '-';};
+  char prefix = '+';
+  if (isSign(cmd.front())) {
+    prefix = cmd.front();
+    cmd.erase(cmd.begin());
+  }
+
+  char suffix = ' ';
+  if (!cmd.empty() && isSign(cmd.back())) {
+    suffix = cmd.back();
+    cmd.erase(cmd.end() - 1);
+  }
+
+  auto log = logDomain(cmd.c_str());
+  if (log == 0)
+    mathic::reportError("Unknown log domain \"" + cmd + "\".\n");
+  log->setEnabled(prefix == '+');
+  if (suffix != ' ')
+    log->setStreamEnabled(suffix == '+');
+}
+
+void LogDomainSet::performLogCommands(const std::string& cmds) {
+  size_t offset = 0;
+  while (offset < cmds.size()) {
+    const size_t next = cmds.find(',', offset);
+    performLogCommand(cmds.substr(offset, next - offset));
+    offset = next;
+    if (offset < cmds.size()) {
+      MATHICGB_ASSERT(cmds[offset] == ',');
+      ++offset;
+    }
+  }
+}
+
 void LogDomainSet::printReport(std::ostream& out) const {
   printCountReport(out);
   printTimeReport(out);
diff --git a/src/mathicgb/LogDomainSet.hpp b/src/mathicgb/LogDomainSet.hpp
index 1a0a324..46ee2c8 100755
--- a/src/mathicgb/LogDomainSet.hpp
+++ b/src/mathicgb/LogDomainSet.hpp
@@ -2,6 +2,7 @@
 #define MATHICGB_LOG_DOMAIN_SET_GUARD
 
 #include "LogDomain.hpp"
+#include <string>
 #include <vector>
 #include <algorithm>
 #include <cstring>
@@ -13,6 +14,36 @@ public:
   void registerLogDomain(LogDomain<true>& domain);
   void registerLogDomain(const LogDomain<false>& domain) {}
 
+  /// A log command has the format AXB, where
+  ///   X       the name of a compile-time enabled log domain
+  ///   A       a prefix
+  ///   B       a suffix
+  /// The possible values of A are
+  ///           enabled X (this is the empty string)
+  ///   +       enabled X
+  ///   -       disable X
+  /// The possible values of B are
+  ///           leaving sub-state as-is (this is the empty string)
+  ///   +       stream-enabled X
+  ///   -       stream-disable X
+  ///
+  /// No white-space is allowed.
+  /// If the command cannot be parsed then you will get an exception.
+  ///
+  /// *** Example ***
+  /// Consider this sequence of commands:
+  ///   "+MyLog-" will enabled MyLog, but silence any streaming from it.
+  ///   "+MyLog" will make no difference, as MyLog is already enabled.
+  ///   "-MyLog+" will disable MyLog, but set the streaming state to enabled.
+  ///     As MyLog is disabled there will still be no streaming output.
+  ///   "+MyLog" will enabled MyLog. Since the streaming state was enabled
+  ///     before, we now get streaming.
+  ///
+  void performLogCommand(std::string cmd);
+
+  /// Performs a comma-seperated list of commands. No white-space is allowed.
+  void performLogCommands(const std::string& cmds);
+
   LogDomain<true>* logDomain(const char* const name);
 
   const std::vector<LogDomain<true>*>& logDomains() const {return mLogDomains;}
diff --git a/src/mathicgb/SPairs.cpp b/src/mathicgb/SPairs.cpp
index d9736ac..63f0397 100755
--- a/src/mathicgb/SPairs.cpp
+++ b/src/mathicgb/SPairs.cpp
@@ -2,16 +2,29 @@
 #include "SPairs.hpp"
 
 #include "GroebnerBasis.hpp"
-
+#include "LogDomain.hpp"
 #include <iostream>
 
-// todo: queueType ignored?
+MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(
+  SPairEarly,
+  "Time early S-pair construction, elimination and ordering.",
+  0, 0, 1
+);
+
+MATHICGB_DEFINE_LOG_DOMAIN_WITH_DEFAULTS(
+  SPairLate,
+  "Time late S-pair elimination and ordering",
+  0, 0, 1
+);
+
 SPairs::SPairs(const PolyBasis& basis, bool preferSparseSPairs):
   mQueue(QueueConfiguration(basis, preferSparseSPairs)),
   mBasis(basis),
   mRing(basis.ring()) {}
 
 std::pair<size_t, size_t> SPairs::pop() {
+  MATHICGB_LOG_TIME(SPairLate);
+
   // Must call addPairs for new elements before popping.
   MATHICGB_ASSERT(mEliminated.columnCount() == mBasis.size());
 
@@ -38,6 +51,8 @@ std::pair<size_t, size_t> SPairs::pop() {
 }
 
 std::pair<size_t, size_t> SPairs::pop(exponent& w) {
+  MATHICGB_LOG_TIME(SPairLate);
+
   // Must call addPairs for new elements before popping.
   MATHICGB_ASSERT(mEliminated.columnCount() == mBasis.size());
 
@@ -108,21 +123,24 @@ void SPairs::addPairsAssumeAutoReduce(
   size_t newGen,
   std::vector<size_t>& toRetireAndReduce
 ) {
-  MATHICGB_ASSERT(mQueue.columnCount() == newGen);
+  {
+    MATHICGB_LOG_TIME(SPairEarly);
 
-  MATHICGB_ASSERT(newGen < mBasis.size());
-  MATHICGB_ASSERT(!mBasis.retired(newGen));
+    MATHICGB_ASSERT(mQueue.columnCount() == newGen);
+    MATHICGB_ASSERT(newGen < mBasis.size());
+    MATHICGB_ASSERT(!mBasis.retired(newGen));
 
-  while (mEliminated.columnCount() < mBasis.size()) {
-    if (mUseBuchbergerLcmHitCache) {
-      MATHICGB_ASSERT(mEliminated.columnCount() == mBuchbergerLcmHitCache.size());
-      mBuchbergerLcmHitCache.push_back(0);
+    while (mEliminated.columnCount() < mBasis.size()) {
+      if (mUseBuchbergerLcmHitCache) {
+        MATHICGB_ASSERT(mEliminated.columnCount() == mBuchbergerLcmHitCache.size());
+        mBuchbergerLcmHitCache.push_back(0);
+      }
+      mEliminated.addColumn();
     }
-    mEliminated.addColumn();
-  }
 
-  RecordIndexes indexes(newGen, mEliminated, toRetireAndReduce);
-  mBasis.divisorLookup().multiples(mBasis.leadMonomial(newGen), indexes);
+    RecordIndexes indexes(newGen, mEliminated, toRetireAndReduce);
+    mBasis.divisorLookup().multiples(mBasis.leadMonomial(newGen), indexes);
+  }
   addPairs(newGen);
 }
 
@@ -159,6 +177,8 @@ namespace {
 }
 
 void SPairs::addPairs(size_t newGen) {
+  MATHICGB_LOG_TIME(SPairEarly);
+
   // Must call addPairs with newGen parameter in the sequence 0, 1, ...
   // newGen could be implicitly picked up from mQueue.columnCount(), but
   // doing it this way ensures that what happens is what the client thinks
diff --git a/src/mathicgb/SPairs.hpp b/src/mathicgb/SPairs.hpp
index f6758f4..a81f3f4 100755
--- a/src/mathicgb/SPairs.hpp
+++ b/src/mathicgb/SPairs.hpp
@@ -20,7 +20,7 @@ public:
   // Returns true if no pending S-pairs remain.
   bool empty() const {return mQueue.empty();}
 
-  // Removes the minimal S-pair from the data structure and return it.
+  // Removes the minimal S-pair from the data structure and returns it.
   // The S-polynomial of that pair is assumed to reduce to zero, either
   // because it already does, or because it did not reduce to zero and then
   // that caused the addition of another basis element. If this assumption

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