Macaulay2 Engine
Loading...
Searching...
No Matches
NCF4.hpp
Go to the documentation of this file.
1#ifndef __nc_f4_hpp__
2#define __nc_f4_hpp__
3
50
51#include "m2tbb.hpp" // for tbb interface
52
53#include "NCAlgebras/FreeMonoid.hpp" // for MonomEq
54#include "MemoryBlock.hpp" // for MemoryBlock
55#include "NCAlgebras/Range.hpp" // for Range
56#include "NCAlgebras/Word.hpp" // for Word
57#include "NCAlgebras/OverlapTable.hpp" // for OverlapTable
58#include "NCAlgebras/WordTable.hpp" // for Overlap, WordTable
59#include "NCAlgebras/SuffixTree.hpp" // for experimental suffix tree code
60#include "NCAlgebras/FreeAlgebra.hpp" // for FreeAlgebra
61#include "VectorArithmetic.hpp" // for VectorArithmetic, ElementArray, etc
62#include "Polynomial.hpp" // for Monom, ConstPolyList, Poly
63#include "newdelete.hpp" // for VECTOR, our_new_delete
64
65#include <deque> // for deque
66#include <iosfwd> // for ostream
67#include <map> // for map
68#include <unordered_map> // for unordered_map
69#include <tuple> // for tuple
70#include <utility> // for pair
71#include <vector> // for vector
72
73union ring_elem;
74
75// this class contains an NCGB calculation using the F4 algorithm.
76// subclasses needed:
77// WordTable/SuffixTree (used for this class and Naive algorithm)
78// OverlapTable (used for this class and Naive algorithm)
79// F4MatrixBuilder
80// F4Matrix
81
103class NCF4 : public our_new_delete
104{
105private:
106 // Data structures for construction of each spair matrix
107 // and data for the matrix itself.
108
109 // memory space for monomials and words for F4 matrix.
110 // PreRow is left, index of poly, right, prevReducer
111 // where the entries depend on the value of prevReducer.
112 // prevReducer = false: if index \geq 0, then left*mGroebner[index]*right
113 // is the lead term of a reducer.
114 // if index < 0, then left*mInput[-index-1]*right
115 // is the lead term of a reducer.
116 // prevReducer = true : left*mRows[i]*right is the lead term of a reducer
117 // the int at the end is the index of the PreRow in the
118 // corresponding vector it belongs to, which will eventually
119 // be the corresponding row.
120
122
144
157 struct Row
158 {
159 ElementArray coeffVector; // vector of coefficients
160 Range<int> columnIndices; // column indices used in the row. Valid *only* after labelAndSortF4Matrix,
161 // as the indices are not known during creation.
162 Range<Word> columnWords; // monoms used in the row. Valid only *before* reduction begins, as reduction
163 // does not update this field
164 };
165
176 struct Column
177 {
178 Word word; // Monom corresponding to the column
179 int pivotRow; // pivot row corresponding to this monomial
180 };
181 // the index of a Column in a ColumnsVector is the column index and is used in Row.columnIndices.
182
183 //using ColumnsVector = tbb::concurrent_vector<Column>;
184 using ColumnsVector = std::vector<Column>;
185
186 // unfortunately we must use the GC allocator here for now
187 using RowsVector = std::vector<Row,gc_allocator<Row>>;
188 //using RowsVector = tbb::concurrent_vector<Row>;
189
190 using PreRowFeeder = mtbb::feeder<PreRow>;
191
192 // The pair in this unordered_map is (i,j) where:
193 // i is the column number
194 // j is the row that reduces it
195 // (and -1 if there is no such row).
196 using MonomialHash = mtbb::unordered_map<Word,std::pair<int,int>,MonomHash,MonomHashEqual>;
197
208 // thread local information
214
215 // data
218
220 //SuffixTree mWordTable;
223
225 //int mTopComputedDegree; // not used yet
226 //int mHardDegreeLimit; // not used yet
227
230
234
237
238 std::vector<PreRow> mReducersTodo;
239 std::vector<PreRow> mOverlapsTodo;
242
246
247 int mFirstOverlap; // First non pivot row (and all later ones are also non-pivot rows).
248
249 // vector arithmetic class for reduction
251
254
255 mtbb::task_arena mScheduler;
256
257
258 // these are pointers to the MemoryBlocks used in creating the various structures.
259 // only used in parallelBuildF4Matrix, which is currently not used.
260 std::vector<MemoryBlock*> mMemoryBlocks;
261 std::vector<MemoryBlock*> mPreviousMemoryBlocks;
262 mtbb::queuing_mutex mColumnMutex;
263
264public:
265 NCF4(const FreeAlgebra& A,
266 const ConstPolyList& input,
267 int hardDegreeLimit,
268 int strategy,
269 int numThreads // 0 for tbb::info::default_concurrency(), for now
270 );
271
273 for (auto f : mGroebner) {
274 mFreeAlgebra.clear(*f);
275 delete f;
276 }
279 delete mVectorArithmetic;
280 };
281
282 [[nodiscard]] const FreeAlgebra& freeAlgebra() const { return mFreeAlgebra; }
283
284 const PolyList& currentValue() const
285 {
286 //return reinterpret_cast<const ConstPolyList&>(mGroebner);
287 return mGroebner;
288 }
289
290 void compute(int softDegreeLimit);
291
292 void displayF4Matrix(std::ostream& o) const;
293
294 void displayFullF4Matrix(std::ostream& o) const;
295
296 void displayF4MatrixSize(std::ostream & o) const;
297
298private:
299 void process(const std::deque<Overlap>& overlapsToProcess);
300
301 void buildF4Matrix(const std::deque<Overlap>& overlapsToProcess);
302 void parallelBuildF4Matrix(const std::deque<Overlap>& overlapsToProcess);
303
305
306 void reduceF4Matrix();
308
309 // FM : I had to discard const qualifiers here because I used mMonomialSpace.
310 // Should I be doing this? Two questions: Should we make mMonomialSpace
311 // mutable so changes don't trigger const errors, and should I really be
312 // using mMonomialSpace anyway?
314 auto isOverlapNecessary(const Overlap& o) -> bool;
315 auto checkOldOverlaps(Word& newLeadWord) -> void;
316
317 void matrixReset();
318
319 // These functions are essentially from NCGroebner
320 void addToGroebnerBasis(Poly * toAdd);
321 void updateOverlaps(const Poly * toAdd);
322 auto overlapHeft(Overlap o) const -> int;
323 auto insertNewOverlaps(std::vector<Overlap>& newOverlaps) -> void;
324
326 const RowsVector& rows,
327 const ColumnsVector& cols,
328 int i) const;
329 PolyList newGBelements() const; // From current F4 matrix.
330
331 void processPreRow(PreRow r,
332 RowsVector& rowsVector,
333 MemoryBlock& memoryBlock,
334 PreRowFeeder* feeder);
335 void processPreRow(PreRow r,
336 RowsVector& rowsVector);
337
339 PreRowFeeder* feeder);
340
341 void preRowsFromOverlap(const Overlap& o);
342
343 std::pair<bool, PreRow> findDivisor(Word w);
344
346 ring_elem getCoeffOfMonom(const Poly& f, const Monom& m) const;
347
348 template<typename LockType>
349 void generalReduceF4Row(int index,
350 int first,
351 int firstcol,
352 NCF4Stats &ncF4Stats,
353 ElementArray& dense,
354 bool updateColumnIndex,
355 LockType& lock);
356
357 void reduceF4Row(int index,
358 int first,
359 int firstcol,
360 NCF4Stats &ncF4Stats,
361 ElementArray& dense)
362 {
363 mtbb::null_mutex noLock;
365 first,
366 firstcol,
367 ncF4Stats,
368 dense,
369 true,
370 noLock);
371 }
372
373 void parallelReduceF4Row(int index,
374 int first,
375 int firstcol,
376 NCF4Stats &ncF4Stats,
377 ElementArray& dense,
378 mtbb::queuing_mutex& lock)
379 {
381 first,
382 firstcol,
383 ncF4Stats,
384 dense,
385 false,
386 lock);
387 }
388
389
390 // return value is isFound, columnIndexOfFound
391 // discard const qualifier here again because this creates a monom in mMonomialSpace
392 std::pair<bool,int> findPreviousReducerPrefix(const Word& w);
393 std::pair<bool,int> findPreviousReducerSuffix(const Word& w);
394
395 void clearRows(RowsVector& rowsVector);
396
398};
399
400#endif
401
402// Local Variables:
403// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
404// indent-tabs-mode: nil
405// End:
Free associative algebra k<x_1,...,x_n> over an arbitrary coefficient ring.
FreeMonoid — monoid of length-prefixed non-commutative words with weight-vector prefix.
Bump-pointer arena allocator for transient inner-loop allocations.
std::tuple< int, int, int, bool > Overlap
OverlapTable — degree-sorted queue of pending word overlaps for non-commutative GB drivers.
gc_vector< const Poly * > ConstPolyList
Polynomial< CoefficientRingType > Poly
gc_vector< Poly * > PolyList
Modern Monom / Polynomial value types shared by NC algebras and the refactored F4.
Home-rolled std::span substitute and zipped-range view for the NC engines.
SuffixTree / SuffixTreeNode — experimental generalised suffix tree for non-commutative leading-word l...
Coefficient-ring-erased arithmetic dispatcher used by F4, GB, and resolution code.
Word and WordWithData — non-owning views over the flat-int encoding of a non-commutative word.
WordTable / WordWithDataTable — leading-word indices for non-commutative Gröbner basis lookup.
Type-erased owning handle to a dense coefficient vector held by a ConcreteVectorArithmetic<Ring>.
Free associative algebra over a coefficient ring: the non-commutative analogue of PolynomialRing.
Thin RAII wrapper around memtailor::Arena providing bump-pointer array allocation with optional mutex...
Strict comparator on Monoms under a FreeMonoid order: returns true exactly when the first monomial is...
Equality functor on Monom (or Word), the KeyEqual companion of MonomHash for std::unordered_map<Monom...
Hash functor on Monom (or Word) suitable for std::unordered_map / std::unordered_set.
void process(const std::deque< Overlap > &overlapsToProcess)
Definition NCF4.cpp:89
MonomEq mMonomEq
Definition NCF4.hpp:231
PolyList mGroebner
Definition NCF4.hpp:222
NCF4(const FreeAlgebra &A, const ConstPolyList &input, int hardDegreeLimit, int strategy, int numThreads)
Definition NCF4.cpp:19
auto overlapHeft(Overlap o) const -> int
Definition NCF4.cpp:222
void reduceF4Row(int index, int first, int firstcol, NCF4Stats &ncF4Stats, ElementArray &dense)
Definition NCF4.hpp:357
void displayF4Matrix(std::ostream &o) const
Definition NCF4.cpp:1098
MemoryBlock mPreviousMonomialSpace
Definition NCF4.hpp:229
auto isOverlapNecessary(const Overlap &o) -> bool
Definition NCF4.cpp:251
ColumnsVector mPreviousColumns
Definition NCF4.hpp:241
bool mIsGraded
Definition NCF4.hpp:224
void parallelReduceF4Matrix()
Definition NCF4.cpp:961
MonomHash mMonomHash
Definition NCF4.hpp:233
void parallelReduceF4Row(int index, int first, int firstcol, NCF4Stats &ncF4Stats, ElementArray &dense, mtbb::queuing_mutex &lock)
Definition NCF4.hpp:373
const ConstPolyList mInput
Definition NCF4.hpp:217
mtbb::feeder< PreRow > PreRowFeeder
Definition NCF4.hpp:190
void parallelBuildF4Matrix(const std::deque< Overlap > &overlapsToProcess)
Definition NCF4.cpp:510
void generalReduceF4Row(int index, int first, int firstcol, NCF4Stats &ncF4Stats, ElementArray &dense, bool updateColumnIndex, LockType &lock)
Definition NCF4.cpp:906
RowsVector mPreviousRows
Definition NCF4.hpp:244
void addToGroebnerBasis(Poly *toAdd)
Definition NCF4.cpp:193
ring_elem getCoeffOfMonom(const Poly &f, const Monom &m) const
Definition NCF4.cpp:347
void clearRows(RowsVector &rowsVector)
Definition NCF4.cpp:1210
mtbb::queuing_mutex mColumnMutex
Definition NCF4.hpp:262
std::vector< MemoryBlock * > mMemoryBlocks
Definition NCF4.hpp:260
void reduceF4Matrix()
Definition NCF4.cpp:1039
void updateOverlaps(const Poly *toAdd)
Definition NCF4.cpp:198
void matrixReset()
Definition NCF4.cpp:375
PreRowType
Definition NCF4.hpp:121
@ OverlapPreRow
Definition NCF4.hpp:121
@ ReducerPreRow
Definition NCF4.hpp:121
@ PreviousReducerPreRow
Definition NCF4.hpp:121
void processPreviousF4Matrix()
Definition NCF4.cpp:159
std::vector< PreRow > mOverlapsTodo
Definition NCF4.hpp:239
MonomHashEqual mMonomHashEqual
Definition NCF4.hpp:232
PolyList newGBelements() const
Definition NCF4.cpp:315
void reducedRowToPoly(Poly *result, const RowsVector &rows, const ColumnsVector &cols, int i) const
Definition NCF4.cpp:328
mtbb::unordered_map< Word, std::pair< int, int >, MonomHash, MonomHashEqual > MonomialHash
Definition NCF4.hpp:196
void processPreRow(PreRow r, RowsVector &rowsVector, MemoryBlock &memoryBlock, PreRowFeeder *feeder)
Definition NCF4.cpp:583
std::vector< Row, gc_allocator< Row > > RowsVector
Definition NCF4.hpp:187
MonomialHash mColumnMonomials
Definition NCF4.hpp:235
void preRowsFromOverlap(const Overlap &o)
Definition NCF4.cpp:393
const FreeAlgebra & mFreeAlgebra
Definition NCF4.hpp:216
std::pair< bool, int > findPreviousReducerSuffix(const Word &w)
Definition NCF4.cpp:808
int mFirstOverlap
Definition NCF4.hpp:247
std::vector< MemoryBlock * > mPreviousMemoryBlocks
Definition NCF4.hpp:261
bool mIsParallel
Definition NCF4.hpp:253
void processWordInPreRow(Word &w, PreRowFeeder *feeder)
Definition NCF4.cpp:678
void displayFullF4Matrix(std::ostream &o) const
Definition NCF4.cpp:1162
RowsVector mRows
Definition NCF4.hpp:243
std::pair< bool, PreRow > findDivisor(Word w)
Definition NCF4.cpp:710
Word createOverlapLeadWord(const Overlap &o)
Definition NCF4.cpp:301
void displayF4MatrixSize(std::ostream &o) const
Definition NCF4.cpp:1066
auto checkOldOverlaps(Word &newLeadWord) -> void
Definition NCF4.cpp:267
WordTable mWordTable
Definition NCF4.hpp:219
MemoryBlock mMonomialSpace
Definition NCF4.hpp:228
std::vector< PreRow > mReducersTodo
Definition NCF4.hpp:238
void labelAndSortF4Matrix()
Definition NCF4.cpp:837
int mNumThreads
Definition NCF4.hpp:252
const VectorArithmetic * mVectorArithmetic
Definition NCF4.hpp:250
std::vector< Column > ColumnsVector
Definition NCF4.hpp:184
void compute(int softDegreeLimit)
Definition NCF4.cpp:67
const PolyList & currentValue() const
Definition NCF4.hpp:284
~NCF4()
Definition NCF4.hpp:272
std::pair< bool, int > findPreviousReducerPrefix(const Word &w)
Definition NCF4.cpp:769
auto insertNewOverlaps(std::vector< Overlap > &newOverlaps) -> void
Definition NCF4.cpp:235
RowsVector mOverlaps
Definition NCF4.hpp:245
mtbb::task_arena mScheduler
Definition NCF4.hpp:255
ColumnsVector mColumns
Definition NCF4.hpp:240
void buildF4Matrix(const std::deque< Overlap > &overlapsToProcess)
Definition NCF4.cpp:468
OverlapTable mOverlapTable
Definition NCF4.hpp:221
const FreeAlgebra & freeAlgebra() const
Definition NCF4.hpp:282
void autoreduceByLastElement()
Definition NCF4.cpp:358
MonomialHash mPreviousColumnMonomials
Definition NCF4.hpp:236
Per-degree FIFO queue of pending overlaps for the NC Groebner basis driver to process.
Runtime dispatcher that hides the concrete coefficient ring behind a std::variant of ConcreteVectorAr...
Non-owning view of a non-commutative word: [begin, end) of int variable indices.
Definition Word.hpp:56
Index of Words (non-commutative monomials) with subword, prefix/suffix, and overlap lookup used by th...
Definition WordTable.hpp:89
VALGRIND_MAKE_MEM_DEFINED & result(result)
Engine TBB shim — single point of inclusion for every parallel primitive.
our_new_delete — per-class opt-in routing of new / delete through bdwgc.
Non-owning view onto a [length, degree, v1, v2, ..., vn] packed monomial in some externally managed b...
Word word
Definition NCF4.hpp:178
int pivotRow
Definition NCF4.hpp:179
A column of the Macaulay matrix: the monomial that names it plus the row currently acting as its pivo...
Definition NCF4.hpp:177
long numCancellations
Definition NCF4.hpp:211
Per-thread counters tracking how much work the F4 reduction did.
Definition NCF4.hpp:209
Word left
Definition NCF4.hpp:139
int preRowIndex
Definition NCF4.hpp:140
Word right
Definition NCF4.hpp:141
PreRowType preRowType
Definition NCF4.hpp:142
Symbolic description of one row before it is materialised in the matrix: a left * (something) * right...
Definition NCF4.hpp:138
ElementArray coeffVector
Definition NCF4.hpp:159
Range< Word > columnWords
Definition NCF4.hpp:162
Range< int > columnIndices
Definition NCF4.hpp:160
A materialised row of the Macaulay matrix: parallel coefficient and monomial arrays.
Definition NCF4.hpp:158