[mathicgb] 215/393: PolyRing now forwards to PrimeField. Also got rid of PolyRing::getMonomialPool() in an effort to reduce the size of the interface of PolyRing.
Doug Torrance
dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:59:03 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 94230a4b783dd7ea49937488eda01f284dacab01
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date: Sun Mar 31 15:18:00 2013 +0200
PolyRing now forwards to PrimeField. Also got rid of PolyRing::getMonomialPool() in an effort to reduce the size of the interface of PolyRing.
---
src/mathicgb/BuchbergerAlg.cpp | 4 +--
src/mathicgb/GroebnerBasis.cpp | 4 +--
src/mathicgb/KoszulQueue.hpp | 34 +++++++++-------------
src/mathicgb/MonTableKDTree.hpp | 12 ++++----
src/mathicgb/PolyRing.hpp | 62 ++++++++++++++++++++++++++++++++++++++++-
src/mathicgb/PrimeField.hpp | 17 +++++++++++
src/mathicgb/SignatureGB.cpp | 6 ++--
7 files changed, 104 insertions(+), 35 deletions(-)
diff --git a/src/mathicgb/BuchbergerAlg.cpp b/src/mathicgb/BuchbergerAlg.cpp
index 711476d..af180fd 100755
--- a/src/mathicgb/BuchbergerAlg.cpp
+++ b/src/mathicgb/BuchbergerAlg.cpp
@@ -336,7 +336,7 @@ void BuchbergerAlg::autoTailReduce() {
size_t BuchbergerAlg::getMemoryUse() const {
return
mBasis.getMemoryUse() +
- mRing.getMonomialPool().getMemoryUse() +
+ mRing.getMemoryUse() +
mReducer.getMemoryUse() +
mSPairs.getMemoryUse();
}
@@ -553,7 +553,7 @@ void BuchbergerAlg::printMemoryUse(std::ostream& out) const
extra << mic::ColumnPrinter::percentInteger(reducerMem, total) << '\n';
}
{ // Signatures
- const size_t sigMem = mRing.getMonomialPool().getMemoryUse();
+ const size_t sigMem = mRing.getMemoryUse();
name << "Monomials:\n";
value << mic::ColumnPrinter::bytesInUnit(sigMem) << '\n';
extra << mic::ColumnPrinter::percentInteger(sigMem, total) << '\n';
diff --git a/src/mathicgb/GroebnerBasis.cpp b/src/mathicgb/GroebnerBasis.cpp
index 30a5aa3..9ccab34 100755
--- a/src/mathicgb/GroebnerBasis.cpp
+++ b/src/mathicgb/GroebnerBasis.cpp
@@ -53,7 +53,7 @@ void GroebnerBasis::insert(monomial sig, std::unique_ptr<Poly> f)
{
MATHICGB_ASSERT(f.get() != 0);
MATHICGB_ASSERT(f->getLeadCoefficient() != 0);
- MATHICGB_ASSERT(sig.isNull() || ring().getMonomialPool().fromPool(sig.unsafeGetRepresentation()));
+ MATHICGB_ASSERT(sig.isNull() || ring().fromPool(sig));
const size_t index = mSignatures.size();
mSignatures.push_back(sig);
@@ -64,7 +64,7 @@ void GroebnerBasis::insert(monomial sig, std::unique_ptr<Poly> f)
MATHICGB_ASSERT(component < mSignatureLookup.size());
mSignatureLookup[component]->insert(sig, index);
- ratio = ring().allocMonomial(ring().getMonomialPool());
+ ratio = ring().allocMonomial();
ring().monomialDivideToNegative(sig, f->getLeadMonomial(), ratio);
}
sigLeadRatio.push_back(ratio);
diff --git a/src/mathicgb/KoszulQueue.hpp b/src/mathicgb/KoszulQueue.hpp
old mode 100644
new mode 100755
index 784eba2..917ad5e
--- a/src/mathicgb/KoszulQueue.hpp
+++ b/src/mathicgb/KoszulQueue.hpp
@@ -10,11 +10,9 @@ class FreeModuleOrder;
class KoszulQueue
{
public:
- KoszulQueue(const FreeModuleOrder *order,
- memt::BufferPool &pool) :
- mQueue(Configuration(order, pool))
- {
- }
+ KoszulQueue(const FreeModuleOrder *order, const PolyRing& ring):
+ mQueue(Configuration(order, ring))
+ {}
const_monomial top() const {
MATHICGB_ASSERT(!empty());
@@ -28,17 +26,13 @@ public:
void pop() {
MATHICGB_ASSERT(!empty());
- mQueue.getConfiguration().getPool().free(popRelease().unsafeGetRepresentation());
+ auto toFree = popRelease().unsafeGetRepresentation();
+ mQueue.getConfiguration().ring().freeMonomial(toFree);
}
- void push(monomial sig) {
- //TODO: MATHICGB_ASSERT(mPool.member(sig));
- mQueue.push(sig);
- }
-
- bool empty() const { return mQueue.empty(); }
-
- size_t size() const { return mQueue.size(); }
+ void push(monomial sig) {mQueue.push(sig);}
+ bool empty() const {return mQueue.empty();}
+ size_t size() const {return mQueue.size();}
size_t getMemoryUse() const {return mQueue.getMemoryUse();}
@@ -48,15 +42,13 @@ private:
public:
typedef monomial Entry;
- Configuration(const FreeModuleOrder *order,
- memt::BufferPool &pool) :
- mOrder(order),
- mPool(pool)
+ Configuration(const FreeModuleOrder *order, const PolyRing& ring):
+ mOrder(order), mRing(ring)
{
MATHICGB_ASSERT(mOrder != 0);
}
- memt::BufferPool &getPool() { return mPool; }
+ const PolyRing& ring() const {return mRing;}
typedef int CompareResult; /* LT, EQ, GT */
@@ -70,12 +62,12 @@ private:
Entry deduplicate(Entry a, Entry b)
{
- mPool.free(b.unsafeGetRepresentation());
+ ring().freeMonomial(b.unsafeGetRepresentation());
return a;
}
private:
const FreeModuleOrder *mOrder;
- memt::BufferPool &mPool; // for signature monomials
+ const PolyRing& mRing;
};
mic::Heap<Configuration> mQueue;
diff --git a/src/mathicgb/MonTableKDTree.hpp b/src/mathicgb/MonTableKDTree.hpp
old mode 100644
new mode 100755
index 6b875c2..ddc8372
--- a/src/mathicgb/MonTableKDTree.hpp
+++ b/src/mathicgb/MonTableKDTree.hpp
@@ -184,15 +184,15 @@ public:
class MonomialDeleter {
public:
- MonomialDeleter(memt::BufferPool& pool): _pool(pool) {}
+ MonomialDeleter(const PolyRing& ring): mRing(ring) {}
void push_back(const void* p) {
- _pool.free(const_cast<void*>(p));
+ mRing.freeMonomial(static_cast<exponent*>(const_cast<void*>(p)));
}
void push_back(ConstMonomial p) {
- _pool.free(static_cast<void*>(const_cast<exponent *>(p.unsafeGetRepresentation())));
+ mRing.freeMonomial(const_cast<exponent *>(p.unsafeGetRepresentation()));
}
private:
- memt::BufferPool& _pool;
+ const PolyRing& mRing;
};
template<bool UDM, bool UTDM, size_t LS, bool AR>
@@ -204,11 +204,11 @@ inline bool MonTableKDTree<UDM, UTDM, LS, AR>::insert(const Entry& entry) {
if (C::AllowRemovals) {
if (findDivisor(entry) != 0)
return false;
- MonomialDeleter mondelete(getPolyRing()->getMonomialPool());
+ MonomialDeleter mondelete(*getPolyRing());
_finder.removeMultiples(entry, mondelete);
} else {
MATHICGB_ASSERT(findDivisor(entry) == 0);
- MonomialDeleter mondelete(getPolyRing()->getMonomialPool());
+ MonomialDeleter mondelete(*getPolyRing());
// todo: can't do the below ASSERT as KD tree won't allow a
// removal action when removals are not allowed. So there
// should be a getMultiples() method that could be
diff --git a/src/mathicgb/PolyRing.hpp b/src/mathicgb/PolyRing.hpp
index 6ff481c..956686e 100755
--- a/src/mathicgb/PolyRing.hpp
+++ b/src/mathicgb/PolyRing.hpp
@@ -5,6 +5,9 @@
#ifdef MATHICGB_USE_MONOID
#include "MonoMonoid.hpp"
+
+#define MATHICGB_USE_FIELD
+#include "PrimeField.hpp"
#endif
#include <assert.h>
@@ -20,9 +23,26 @@
#define EQ 0
#define GT 1
+#ifdef MATHICGB_USE_FIELD
+template<class T>
+PrimeField<
+ typename std::make_unsigned<
+ typename std::remove_reference<T>::type
+ >::type
+> makeField(T charac) {
+ return charac;
+}
+#endif
+
/** Returns a^-1 mod modulus. It is required that 0 < a < modulus. */
template<class T>
T modularInverse(T a, T modulus) {
+#ifdef MATHICGB_USE_FIELD
+ MATHICGB_ASSERT(0 < a);
+ MATHICGB_ASSERT(a < modulus);
+ auto f = makeField(modulus);
+ return f.inverse(f.toElementInRange(a)).value();
+#else
// we do two turns of the extended Euclidian algorithm per
// loop. Usually the sign of x changes each time through the loop,
// but we avoid that by representing every other x as its negative,
@@ -62,6 +82,7 @@ T modularInverse(T a, T modulus) {
MATHICGB_ASSERT(x < modulus);
MATHICGB_ASSERT_NO_ASSUME((static_cast<uint64>(origA) * x) % modulus == 1);
return x;
+#endif
}
template<class T>
@@ -76,6 +97,14 @@ template<> struct ModularProdType<int32> {typedef int64 type;};
/** Returns a*b mod modulus. It is required that 0 <= a, b < modulus. */
template<class T>
T modularProduct(T a, T b, T modulus) {
+#ifdef MATHICGB_USE_FIELD
+ MATHICGB_ASSERT(0 < a);
+ MATHICGB_ASSERT(a < modulus);
+ MATHICGB_ASSERT(0 <= b);
+ MATHICGB_ASSERT(b < modulus);
+ auto f = makeField(modulus);
+ return f.product(f.toElementInRange(a), f.toElementInRange(b)).value();
+#else
typedef typename ModularProdType<T>::type BigT;
MATHICGB_ASSERT(0 <= a);
MATHICGB_ASSERT(a < modulus);
@@ -84,11 +113,20 @@ T modularProduct(T a, T b, T modulus) {
BigT bigProd = static_cast<BigT>(a) * b;
MATHICGB_ASSERT(a == 0 || bigProd / a == b);
return static_cast<T>(bigProd % modulus);
+#endif
}
/** Returns a+b mod modulus. It is required that 0 <= a, b < modulus. */
template<class T>
T modularSum(T a, T b, T modulus) {
+#ifdef MATHICGB_USE_FIELD
+ MATHICGB_ASSERT(0 < a);
+ MATHICGB_ASSERT(a < modulus);
+ MATHICGB_ASSERT(0 <= b);
+ MATHICGB_ASSERT(b < modulus);
+ auto f = makeField(modulus);
+ return f.sum(f.toElementInRange(a), f.toElementInRange(b)).value();
+#else
typedef typename ModularProdType<T>::type BigT;
MATHICGB_ASSERT(0 <= a);
MATHICGB_ASSERT(a < modulus);
@@ -97,22 +135,37 @@ T modularSum(T a, T b, T modulus) {
BigT bigSum = static_cast<BigT>(a) + b;
MATHICGB_ASSERT(bigSum - a == b);
return static_cast<T>(bigSum % modulus);
+#endif
}
/** Returns -a mod modulus. It is required that 0 <= a < modulus. */
template<class T>
T modularNegative(T a, T modulus) {
+#ifdef MATHICGB_USE_FIELD
+ MATHICGB_ASSERT(0 < a);
+ MATHICGB_ASSERT(a < modulus);
+ auto f = makeField(modulus);
+ return f.negative(f.toElementInRange(a)).value();
+#else
MATHICGB_ASSERT(0 <= a);
MATHICGB_ASSERT(a < modulus);
return a == 0 ? 0 : modulus - a;
+#endif
}
/** Returns -a mod modulus. It is required that 0 < a < modulus. */
template<class T>
T modularNegativeNonZero(T a, T modulus) {
+#ifdef MATHICGB_USE_FIELD
+ MATHICGB_ASSERT(0 < a);
+ MATHICGB_ASSERT(a < modulus);
+ auto f = makeField(modulus);
+ return f.negativeNonZero(f.toElementInRange(a)).value();
+#else
MATHICGB_ASSERT(0 < a);
MATHICGB_ASSERT(a < modulus);
return modulus - a;
+#endif
}
typedef int32 exponent ;
@@ -234,7 +287,10 @@ public:
PolyRing(coefficient charac, int nvars, int nweights);
~PolyRing() {}
- memt::BufferPool &getMonomialPool() const { return mMonomialPool; }
+ size_t getMemoryUse() const {
+ // todo: Make this more accurate.
+ return mMonomialPool.getMemoryUse();
+ }
coefficient charac() const { return mCharac; }
size_t getNumVars() const { return mNumVars; }
@@ -270,6 +326,10 @@ public:
A.freeTop(m.unsafeGetRepresentation());
}
+ bool fromPool(ConstMonomial m) const {
+ return mMonomialPool.fromPool(m.unsafeGetRepresentation());
+ }
+
// Allocate a monomial from a pool that has had its size set to
// maxMonomialByteSize()
// Free monomials here using the SAME pool
diff --git a/src/mathicgb/PrimeField.hpp b/src/mathicgb/PrimeField.hpp
old mode 100644
new mode 100755
index b00c5e4..a1f8cd9
--- a/src/mathicgb/PrimeField.hpp
+++ b/src/mathicgb/PrimeField.hpp
@@ -45,6 +45,17 @@ public:
T charac() const {return mCharac;}
+ /// Assumes that i is in the range [0;charac()).
+ template<class Integer>
+ Element toElementInRange(Integer&& i) const {
+ typedef typename std::remove_reference<Integer>::type NoRefInteger;
+ static_assert(std::numeric_limits<NoRefInteger>::is_integer, "");
+
+ MATHICGB_ASSERT(0 <= i);
+ MATHICGB_ASSERT(i < charac());
+ return Element(i);
+ }
+
template<class Integer>
Element toElement(Integer&& i) const {
typedef typename std::remove_reference<Integer>::type NoRefInteger;
@@ -126,6 +137,12 @@ namespace PrimeFieldInternal {
template<> struct ModularProdType<uint8> {typedef uint16 type;};
template<> struct ModularProdType<uint16> {typedef uint32 type;};
template<> struct ModularProdType<uint32> {typedef uint64 type;};
+
+ // @todo: Remove this typedef when possible. 64 bits is not enough
+ // to store a 64 bit product. We need it right now because
+ // coefficients are handled as 64 bit in the legacy PolyRing.
+ template<> struct ModularProdType<uint64> {typedef uint64 type;};
+ template<> struct ModularProdType<long unsigned int> {typedef uint64 type;};
}
template<class T>
diff --git a/src/mathicgb/SignatureGB.cpp b/src/mathicgb/SignatureGB.cpp
index 0106659..15147b0 100755
--- a/src/mathicgb/SignatureGB.cpp
+++ b/src/mathicgb/SignatureGB.cpp
@@ -89,7 +89,7 @@ SignatureGB::SignatureGB(
stats_pairsReduced(0),
stats_nsecs(0.0),
GB(make_unique<GroebnerBasis>(R, F.get(), divlookup_type, montable_type, preferSparseReducers)),
- mKoszuls(make_unique<KoszulQueue>(F.get(), R->getMonomialPool())),
+ mKoszuls(make_unique<KoszulQueue>(F.get(), *R)),
Hsyz(MonomialTableArray::make(R, montable_type, ideal.size(), !mPostponeKoszul)),
reducer(Reducer::makeReducer(reductiontyp, *R)),
SP(make_unique<SigSPairs>(R, F.get(), GB.get(), Hsyz.get(), reducer.get(), mPostponeKoszul, mUseBaseDivisors, useSingularCriterionEarly, queueType))
@@ -275,7 +275,7 @@ size_t SignatureGB::getMemoryUse() const {
size_t sum =
GB->getMemoryUse() +
Hsyz->getMemoryUse() +
- R->getMonomialPool().getMemoryUse() +
+ R->getMemoryUse() +
reducer->getMemoryUse() +
mSpairTmp.capacity() * sizeof(mSpairTmp.front());
sum += SP->getMemoryUse();
@@ -702,7 +702,7 @@ void SignatureGB::displayMemoryUse(std::ostream& out) const
extra << mic::ColumnPrinter::percentInteger(reducerMem, total) << '\n';
}
{ // Signatures
- const size_t sigMem = R->getMonomialPool().getMemoryUse();
+ const size_t sigMem = R->getMemoryUse();
name << "Signatures:\n";
value << mic::ColumnPrinter::bytesInUnit(sigMem) << '\n';
extra << mic::ColumnPrinter::percentInteger(sigMem, total) << '\n';
--
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