[mathicgb] 293/393: Implemented the functionality of the callback in the library interface. Also added tests for it which pass.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:22 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 6f39e3c175a0a183762412c480eb245c51070dff
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Sat Apr 20 17:54:07 2013 -0400

    Implemented the functionality of the callback in the library interface. Also added tests for it which pass.
---
 src/mathicgb.cpp               | 45 ++++++++++++++++++++++++++++++++++++++--
 src/mathicgb.h                 | 12 ++++++++---
 src/mathicgb/BuchbergerAlg.cpp |  6 +++++-
 src/mathicgb/BuchbergerAlg.hpp | 11 ++++++++++
 src/test/mathicgb.cpp          | 47 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 115 insertions(+), 6 deletions(-)

diff --git a/src/mathicgb.cpp b/src/mathicgb.cpp
index d8c780c..f96e815 100755
--- a/src/mathicgb.cpp
+++ b/src/mathicgb.cpp
@@ -708,9 +708,39 @@ namespace mgbi {
   }
 }
 
+namespace {
+  class CallbackAdapter : public BuchbergerAlg::Callback {
+  public:
+    typedef mgb::GroebnerConfiguration::Callback::Action Action;
+
+    CallbackAdapter(void* data, Action (*callback) (void*)):
+      mData(data),
+      mCallback(callback),
+      mLastAction(Action::ContinueAction)
+    {
+      MATHICGB_ASSERT(mCallback != 0 || mData == 0);
+    }
+
+    const bool isNull() const {return mCallback == 0;}
+    Action lastAction() const {return mLastAction;}
+
+    virtual bool call() {
+      if (isNull())
+        return true;
+      mLastAction = mCallback(mData);
+      return mLastAction == Action::ContinueAction;
+    }
+
+  private:
+    void* const mData;
+    Action (* const mCallback) (void*);
+    Action mLastAction;
+  };
+}
+
 // ** Implementation of function mgbi::internalComputeGroebnerBasis
 namespace mgbi {
-  void internalComputeGroebnerBasis(
+  bool internalComputeGroebnerBasis(
     GroebnerInputIdealStream& inputWhichWillBeCleared,
     IdealAdapter& output
   ) {
@@ -749,6 +779,10 @@ namespace mgbi {
       break;
     }
     const auto reducer = Reducer::makeReducer(reducerType, ring);
+    CallbackAdapter callback(
+      PimplOf()(conf).mCallbackData,
+      PimplOf()(conf).mCallback
+    );
 
     // Set up and configure algorithm
     BuchbergerAlg alg(basis, 4, *reducer, 2, true, 0);
@@ -756,9 +790,16 @@ namespace mgbi {
     alg.setUseAutoTopReduction(true);
     alg.setUseAutoTailReduction(false);
     alg.setSPairGroupSize(conf.maxSPairGroupSize());
+    if (!callback.isNull())
+      alg.setCallback(&callback);
 
     // Compute Groebner basis
     alg.computeGrobnerBasis();
-    PimplOf()(output).basis = alg.basis().toBasisAndRetireAll();
+    typedef mgb::GroebnerConfiguration::Callback::Action Action;
+    if (callback.lastAction() != Action::StopWithNoOutputAction) {
+      PimplOf()(output).basis = alg.basis().toBasisAndRetireAll();
+      return true;
+    } else
+      return false;
   }
 }
diff --git a/src/mathicgb.h b/src/mathicgb.h
index f9617b5..432ed9e 100755
--- a/src/mathicgb.h
+++ b/src/mathicgb.h
@@ -130,7 +130,7 @@ namespace mgb { // Part of the public interface of MathicGB
         StopWithNoOutputAction = 1,
         StopWithPartialOutputAction = 2
       };
-      virtual Action call() const = 0;
+      virtual Action call() = 0;
     };
 
     /// Set callback to be called at various unspecified times during
@@ -144,6 +144,9 @@ namespace mgb { // Part of the public interface of MathicGB
   private:
     friend class mgbi::PimplOf;
 
+    void operator=(const GroebnerConfiguration&); // not available
+    bool operator==(const GroebnerConfiguration&); // not available
+
     struct MonomialOrderData {
       BaseOrder baseOrder;
       const Exponent* gradings;
@@ -694,7 +697,7 @@ namespace mgbi {
     Pimpl* mPimpl;
   };
 
-  void internalComputeGroebnerBasis(
+  bool internalComputeGroebnerBasis(
     GroebnerInputIdealStream& inputWhichWillBeCleared,
     IdealAdapter& output
   );
@@ -707,7 +710,10 @@ namespace mgb {
     OutputStream& output
   ) {
     mgbi::IdealAdapter ideal;
-    mgbi::internalComputeGroebnerBasis(inputWhichWillBeCleared, ideal);
+    const bool doOutput =
+      mgbi::internalComputeGroebnerBasis(inputWhichWillBeCleared, ideal);
+    if (!doOutput)
+      return;
 
     const auto varCount = ideal.varCount();
     const auto polyCount = ideal.polyCount();
diff --git a/src/mathicgb/BuchbergerAlg.cpp b/src/mathicgb/BuchbergerAlg.cpp
index 5194241..1abdab3 100755
--- a/src/mathicgb/BuchbergerAlg.cpp
+++ b/src/mathicgb/BuchbergerAlg.cpp
@@ -18,6 +18,7 @@ BuchbergerAlg::BuchbergerAlg(
   bool preferSparseReducers,
   size_t queueType
 ):
+  mCallback(0),
   mBreakAfter(0),
   mPrintInterval(0),
   mSPairGroupSize(reducer.preferredSetSize()),
@@ -203,11 +204,14 @@ void BuchbergerAlg::insertReducedPoly(
 void BuchbergerAlg::computeGrobnerBasis() {
   size_t counter = 0;
   mTimer.reset();
-  
+
   if (mUseAutoTailReduction)
     autoTailReduce();
 
   while (!mSPairs.empty()) {
+    if (mCallback != 0 && !mCallback->call())
+      break;
+
     step();
     if (mBreakAfter != 0 && mBasis.size() > mBreakAfter) {
       std::cerr
diff --git a/src/mathicgb/BuchbergerAlg.hpp b/src/mathicgb/BuchbergerAlg.hpp
index 98c6b47..700bbe6 100755
--- a/src/mathicgb/BuchbergerAlg.hpp
+++ b/src/mathicgb/BuchbergerAlg.hpp
@@ -64,7 +64,18 @@ public:
     mUseAutoTailReduction = value;
   }
 
+  class Callback {
+  public:
+    /// Stop the computation if call return false.
+    virtual bool call() = 0;
+  };
+  /// callback is called every once in a while and then it has the
+  /// option of stopping the computation. callback can be null, in
+  /// which case no call is made and the computation continues.
+  void setCallback(Callback* callback) {mCallback = callback;}
+
 private:
+  Callback* mCallback;
   unsigned int mBreakAfter;
   unsigned int mPrintInterval;
   unsigned int mSPairGroupSize;
diff --git a/src/test/mathicgb.cpp b/src/test/mathicgb.cpp
index b223986..7f5ee90 100755
--- a/src/test/mathicgb.cpp
+++ b/src/test/mathicgb.cpp
@@ -470,6 +470,53 @@ TEST(MathicGBLib, Cyclic5) {
   }
 }
 
+namespace {
+  class TestCallback : public mgb::GroebnerConfiguration::Callback {
+  public:
+    TestCallback(int count, Action action): mCount(count), mAction(action) {}
+
+    virtual Action call() {
+      --mCount;
+      return mCount == 0 ? mAction : ContinueAction;
+    }
+
+  private:
+    int mCount;
+    const Action mAction;
+  };
+}
+
+TEST(MathicGBLib, EarlyExit) {
+  typedef mgb::GroebnerConfiguration::Callback::Action Action;
+  auto check = [](bool useClassic, int count, Action action) {
+    mgb::GroebnerConfiguration configuration(101, 5);
+    const auto reducer = useClassic ?
+      mgb::GroebnerConfiguration::ClassicReducer :
+      mgb::GroebnerConfiguration::MatrixReducer;
+    configuration.setReducer(reducer);
+    TestCallback callback(count, action);
+    configuration.setCallback(&callback);
+    mgb::GroebnerInputIdealStream input(configuration);
+    makeCyclic5Basis(input);
+
+    std::ostringstream strOut;
+    mgb::IdealStreamLog<> out(strOut, 101, 5);
+    mgb::computeGroebnerBasis(input, out);
+    return strOut.str().size();
+  };
+
+  for (int useClassic = 0; useClassic < 2; ++useClassic) {
+    size_t none = check(useClassic, 5, Action::StopWithNoOutputAction);
+    size_t minSize = check(useClassic, 1, Action::StopWithPartialOutputAction);
+    size_t midSize = check(useClassic, 4, Action::StopWithPartialOutputAction);
+    size_t maxSize = check(useClassic, 1, Action::ContinueAction);
+    ASSERT_LT(none, 35); // the stream writes a header even for no output
+    ASSERT_LT(none, minSize);
+    ASSERT_LT(minSize, midSize);
+    ASSERT_LT(midSize, maxSize);
+  }
+}
+
 TEST(MathicGBLib, SimpleEliminationGB) {
   std::vector<mgb::GroebnerConfiguration::Exponent> gradings = {1,0,0,0,  1,1,1,1};
   for (int i = 0; i < 2; ++i) {

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