Repository navigation
Use several threads to lift Groebner bases over QQ - #418
Open
mohabsafey wants to merge 19 commits into
Open
mohabsafey wants to merge 19 commits into
mohabsafey wants to merge 19 commits into
Conversation
When lifting a Groebner basis over QQ (-g 2), the denominator of each polynomial is guessed from the previous one, and three witness coefficients are rationally reconstructed before the whole polynomial is lifted. When the denominator of a polynomial has a large factor which does not divide the previous ones (e.g. the first polynomial, or one where the denominators jump), this reconstruction needs about bits(n) + 2 bits(d) bits of modulus, d being the new factor, while the numerators n only need bits(n). The witnesses of a polynomial share their denominator. Up to 8 of them (the three witnesses and nonzero coefficients spread over the polynomial) are now reconstructed simultaneously as a short vector of a lattice found by LLL, which needs about bits(n) + bits(d) / k bits. LLL is tried when the rational reconstruction of the witnesses fails, first with the guessed denominator (the first column of the lattice is scaled by its bitsize), then without it with 4 witnesses, since the guess inflates the numerators when it shares little with the denominator. The candidate is stored as the reconstructed witnesses, hence it is checked with the next prime and used as the denominator of the full lifting, as before. LLL is not tried before the modulus has the bitsize of the guessed denominator, then again each time the number of primes grows by max(5%, sqrt(2A/n)), A being the cost of an attempt in units of the cost of a prime, and its estimated cost is kept below 10% of the estimated cost of the primes. These estimates use the operations recorded in the trace with the weights of the cost model of hensel.c, and not timings, so that the number of primes used stays reproducible. Without this bound, LLL made random2_8_8 three times slower. Fewer primes on bruno (1089 -> 629), KLYZ22 (725 -> 481), cyclic8 (46 -> 30), vor1 (124 -> 95) and minrank_d_2_p_3_q_7_r_3 (1633 -> 1417), for speedups of 2.04, 1.67, 1.39, 1.25 and 1.23 (-g 2 -t 1); no change on random2, kat and eco. The output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Once the witness coefficients of a polynomial are checked, all its coefficients are reconstructed with the lcm of the denominators found so far as a guess. A coefficient whose denominator has a factor which divides none of these needs about bits(n) + 2 bits(d) bits of modulus, d being this factor, and the whole polynomial waits for it. This is what bounds the number of primes on katsura and eco. A coefficient whose reconstruction fails is now kept with its CRT image, and up to 8 of them are reconstructed simultaneously by LLL with the guessed denominator, as the witnesses are, together with already reconstructed coefficients when less than 8 fail. On success, the guess is multiplied by the factor found and they are reconstructed again with it. The cost of these calls is bounded as for the witnesses, with a schedule of their own. A wrong factor makes the lifted polynomial fail the check with the next primes, as a wrong reconstruction does, and the content of the lifted polynomial is removed in case LLL found a multiple of the missing factor. The lattice reduction is shared with the reconstruction of the witnesses (lll_common_den). The reconstruction of the witnesses without guessed denominator, on a lattice where LLL is much slower, only pays when the guess carries factors foreign to the denominator. It is now tried only once, for some lifted polynomial, at least 1/8 of the bits of the denominator of the previous polynomial do not divide its own. On many systems (random, minrank, cp_d) all denominators divide about the same integer and LLL then only cost time: minrank_d_3_p_3_q_6_r_3 and random2_9_9 are no longer slower than before the reconstruction by LLL. henrion7 needs 161 primes instead of 202 (1.33 times faster with -g 2 -t 1), kat12 35 instead of 43, kat11 27 instead of 28, kat9 12 instead of 13 and chandra-11 67 instead of 69. The output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Once the witnesses of a polynomial are checked, each coefficient u was lifted by CRT, then rationally reconstructed with the guessed denominator g, the lcm of the denominators found so far. For most coefficients the denominator divides g, so that u g is an integer and the rational reconstruction only finds it again. On systems needing thousands of primes (random, minrank, cp_d), this took about as much time as the CRT, 15 to 25% of the total. The CRT is now applied to the residues of u g modulo the primes, which gives u g as the symmetric remainder modulo their product M. It is accepted as an integer when it has RR_MARGIN bits less than M, else u is lifted by CRT and rationally reconstructed as before. Most coefficients thus cost one CRT, and neither the product u g of large integers nor a rational reconstruction. As with a rational reconstruction, a wrong integer (probability about 2^(-RR_MARGIN)) is rejected when checking the lifted polynomial with the next primes. The CRT is now taken over the primes whose product is M only: it was also taken over the prime used for checking, with an old residue, which was harmless since the result was then reduced modulo M. The time of the rational reconstructions is divided by 3 to 9 on minrank, random, cp_d and henrion7 (minrank_d_3_p_3_q_6_r_3: 14.4 to 1.5 s, 1.39 times faster in total, cp_d_3_n_6_p_2 1.25, random2_9_9 1.13 with -g 2 -t 1). Integers are also accepted with fewer bits than the bounds of the rational reconstruction required: kat12 needs 34 primes instead of 35, eco12 12 instead of 13. The output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the coefficients of a Groebner basis polynomial are reconstructed by LLL with a guessed denominator g of s bits, the numerators of the short vector have about s plus the bitsize of the missing factor d of the denominator. The s - 64 lowest bits of the lattice entries therefore do not change the short vector: LLL now runs on the lattice whose entries are shifted right by these bits, d is read from the reduced basis, and the numerators are recomputed exactly from d before the unchanged acceptance test. This makes each call 2 to 4 times faster for large guesses, which is where LLL is wasted on random systems: their last stalled polynomials are reconstructed by rational reconstruction at about the bitsize of their integers, so that LLL cannot save primes there. The cost model deciding the attempts is kept as an upper bound, so that the number of primes is unchanged. On random2_10_10, LLL time drops from 74 to 21 s for the same 15749 primes; outputs are identical on all systems tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The multi-modular computation of Groebner bases over the rationals (-g 2) used primes of 31 bits only. F4 computes modulo primes of up to 52 bits with 64 bit integers, and with AVX-512 IFMA its cost per bit of the modulus is then lower than with primes of 31 bits. The trace is still learned modulo a prime of 31 bits, since learning modulo a larger prime is slower; it is then applied modulo primes of 52 bits, whose images are stored on 64 bits (the images modulo 31 bit primes keep their 32 bit storage). The modular arithmetic of the lifting, the CRT updates and the checks handle primes of 64 bits, and copy_basis_mod_p supports the 64 bit coefficients. Primes of 52 bits are used by default when msolve is built with AVX-512 IFMA, else primes of 31 bits; the new option --gb-prime-bits (31 or 52) selects them explicitly. With 31 bit primes, the primes used and the outputs are unchanged. On a Xeon Gold 6354 (Ice Lake, AVX-512 IFMA), 26 systems, 5 seeds (2 for the largest ones), identical outputs: about 40% fewer primes, 1.16 times faster on geometric mean (from 0.96 to 1.33, 1.17 on random2_10_10). Without IFMA, on an AVX2 machine, the gain is only 1.02 with losses up to 0.84, hence the default. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The lattices used to reconstruct coefficients by LLL are small, and fmpz_lll spends most of its time on them beyond the floating-point reduction: on lattices of dimension 9 with a modulus of 360 to 900 bits, it takes 1.2 to 1.6 ms with FLINT 3.6 and 10 to 13 ms with FLINT 3.1, against 0.1 to 0.4 ms for fmpz_lll_d_heuristic alone, which finds the same short vectors. A certified reduction is not needed, since the candidates are checked by their sizes and then with new primes, so fmpz_lll is now only called when the heuristic reduction fails. With FLINT 3.1, LLL took about 1 s out of 4.5 s on kat11-qq. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When the check of a lifted polynomial fails, the lifting restarts from this polynomial, and the following ones, which were lifted but not checked yet, are lifted again. Their counter of checks was incremented instead of being reset, so that their new lift was taken as checked without being compared to the images modulo a prime not used to lift it. Since the coefficients are accepted as integers when they are small enough, such a lift may be wrong: this happened on chandra-15 when lifting with batches of 32 primes. A lifted polynomial is now marked as unchecked, whether it was lifted before or not. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On Windows, unsigned long has 32 bits, so that the GMP functions mpz_*_ui truncate the primes of 52 bits used for the Groebner bases over the rationals when AVX-512 IFMA is available: the reductions of the input coefficients modulo the primes, the products of the CRT modulus by the primes, the CRT itself and the checks of the lifted coefficients were wrong, so that the lifted polynomials never passed their checks. On the Windows runners of the CI with AMD Zen 4 and Zen 5 processors, four tests failed and diff_elim-drl-qq did not end. The primes and the coefficients modulo the primes are now passed to GMP as limbs, which have 64 bits on all 64 bit platforms (new header gmp_u64.h), and they are printed with PRIu64. The failures are reproduced on Linux by truncating to 32 bits the arguments of the GMP functions taking an unsigned long: with this change, make check passes in that setting, with a random seed and with the seed of the CI. Otherwise, the outputs and the timings are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a trace is applied, the matrices and the supports of the basis elements are cached in the trace and published with compare-and-swap, so that several primes can share them. Three places read these pointers with plain loads, which is a data race when the trace is applied concurrently for several primes. Read them with acquire loads instead, so that the data written before publication is visible to the readers. ThreadSanitizer reported no more races in the trace application after this change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…acers Over the rationals, the elimination via the DRL Groebner basis set the round of the multi-modular computation, the information level and the number of threads in the meta data of the DRL and elimination steps, which are kept from one prime to the next. Once their tracers are learnt, these fields are now set on copies of these meta data, which are then only read, as in run_f4, so that the tracers can be applied for several primes at the same time. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -t > 1, the trace is applied in batches of primes, one prime per thread, and the images are then consumed prime by prime in the same order as with a single thread, so the lifting decisions do not change. The CRT of the coefficients runs in parallel chunks with one workspace per thread. Chunks start at one coefficient per thread at each polynomial and double, since the guessed denominator mostly changes at the first coefficients. After such a change, the results already computed are updated from the previous guess instead of being recomputed. The witness CRTs, the residues of the guessed denominators and the decimal conversion of the output also run in parallel, and the inverse used by the witness CRTs is computed once per prime. On 6 cores, random2_9_9 goes from 77.5 s to 23.0 s and minrank_d_3_p_3_q_6_r_3 from 47.9 s to 12.9 s, with identical outputs. The number of primes may still vary slightly between runs with several threads, because the multi-threaded learning of the trace is not deterministic. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Splitting these CRTs among the threads made them slower: each one is cheap, but their results then had to move from the caches of the other threads to the one that reconstructs the witnesses. On random2_9_9 with 6 threads, they took 3.2 s in parallel and 1.3 s on one thread. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When lifting a Groebner basis over QQ, the CRTs of the three witness coefficients of every polynomial not lifted yet were updated with each new prime, while they are only used by the rational reconstruction of the witnesses, which stops at the first polynomial for which it fails. Only the polynomials already reached are now kept up to date prime by prime; the CRTs of the next ones are computed from their images modulo all the primes when the reconstruction reaches them, for as many polynomials as threads at once, in parallel. The results are the same symmetric remainders, so the primes used and the output are unchanged. On an Intel Xeon Gold 6354 with 32 threads and FLINT 3.6, the time of the CRTs goes from 33.5 to 23.0 s on random2_10_10 (total 137.8 to 132.4 s) and from 5.4 to 4.4 s on minrank_d_2_p_2_q_8_r_2 (total 28.9 to 27.4 s). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With several threads, the trace is applied to batches of as many primes as threads, and then the main thread lifts them one by one: the reconstructions of the witnesses and the calls to LLL run on a single thread while the other threads wait. Most of them fail, and an attempt in the middle of a batch saves no time since the whole batch is computed anyway. Above 4 threads, the witnesses are now reconstructed at most once per batch, two primes before its end so that a reconstruction found is checked within the batch. In the spacing of the attempts of LLL, an attempt now costs as many primes as threads, since the primes are computed that many at once while LLL runs on one thread. The output is unchanged, and so are the primes used with at most 4 threads. On an Intel Xeon Gold 6354 with 32 threads and FLINT 3.6, bruno goes from 3.78 to 2.24 s, KLYZ22 from 5.43 to 3.86 s, zimmermann from 5.38 to 3.83 s, random2_9_9 from 16.0 to 12.7 s and random2_10_10 from 134.8 to 113.7 s (12 systems, geometric mean 1.18x); eco12 and minrank_d_2_p_3_q_7_r_3, which need few primes or lose an LLL success, are 6 to 10% slower. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Writing a lifted basis converted the coefficients in parallel but formatted the terms on one thread, flushing the file after each term, so that writing took up to a quarter of the running time with many threads (11 s out of 47 s for chandra-14 with 32 threads). The terms are now formatted in memory, without flushing, by all threads, by chunks of at most 256 terms of a polynomial and about 64 KB of text, which are written in order: this suits bases with many small polynomials as well as bases with few polynomials with huge coefficients, and bounds the memory used. The output is unchanged. On a 2 x 18-core Xeon Gold 6354 with 32 threads, writing the basis takes 1.8 s instead of 11.1 s for chandra-14, 0.8 s instead of 4.6 s for kat13, and 14.0 s instead of 15.1 s for random2_10_10. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With many threads, the lifting steps run between two batches of primes kept the other threads idle for up to half of the computation. The modular images of each polynomial were stored in one array, which was enlarged (and copied) every few primes and zeroed, and the coefficients of each lifted polynomial were reduced to their common denominator and checked modulo the new primes by one thread. The images are now stored by blocks of rows, one row per prime, allocated together for the primes added at once, so that adding primes never copies the rows already stored; the images of a new prime are copied by all threads, and so are the reduction to the common denominator and the checks of the polynomials of at least 512 terms. Since the blocks have various sizes, the memory they occupied once free-ed is returned to the system once per batch (with glibc). The output and the primes used are unchanged. On a 2 x 18-core Xeon Gold 6354 with 32 threads, chandra-14 takes 31.1 s instead of 40.5 s, minrank_d_2_p_2_q_8_r_2 18.2 s instead of 23.1 s and random2_10_10 99.3 s instead of 108.1 s, with less memory (12.7 GB instead of 15.9 GB for chandra-14). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The coefficients of a polynomial are reconstructed by chunks, which start with one coefficient per thread and double, the coefficients of a chunk being split among the threads by blocks of four. The first chunks of each polynomial thus kept only a quarter or a half of the threads busy, and on bases with few, large polynomials most chunks are small. The blocks are now made smaller for small chunks, so that each thread gets about four of them; the output is unchanged. On a 2 x 18-core Xeon Gold 6354 with 32 threads, the threads are busy 91 to 93% of the time of this CRT instead of 55 to 71% on random2_9_9, random2_10_10 and minrank_d_2_p_2_q_8_r_2, and random2_9_9 takes 9.9 s instead of 11.2 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With batches of primes, the witnesses are reconstructed once per batch, but the polynomials are reconstructed in a few attempts only, by groups of polynomials with coefficients of about the same size: in between, every attempt fails on the same polynomial, and these failures cost up to a tenth of the computation on random systems. After an attempt which reconstructs no polynomial, the next one is now made after a growth of the number of primes given, as for LLL, by the estimated cost of an attempt (fitted on the size of the modulus) relative to the cost of a prime, but limited to 3 percent, which bounds the primes added when the last polynomials are reconstructed later. It depends only on the trace and on the number of primes and of threads, and nothing changes with 4 threads or fewer. On a 2 x 18-core Xeon Gold 6354, random2_10_10 takes 89.1 s instead of 93.8 s with 32 threads and 216.0 s instead of 226.6 s with 8 threads, with up to 3 percent more primes; random2_9_9 takes 19.1 s instead of 21.8 s with 8 threads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With -g 2 and t threads, the trace is applied to batches of t primes, one thread per prime, so that the memory grows with t: each thread runs its own F4 application, the bases computed are held until they are lifted, and the images of the primes wait for the next reconstruction attempt, made once per batch. With the new option --local-threads p, where p divides t, the batches have t/p primes, each computed by p threads, and the reconstruction attempts are spaced by these batches; the CRT still uses the t threads. The primes used then depend on p, but they are reproducible for given t and p. If p does not divide t, a warning is printed and one thread per prime is used; without the option, nothing changes. A test checks both cases on cyclic5. The primes of a batch are then applied by threads started with pthread_create rather than in a parallel region: the parallel regions of F4 would be nested in it, and libgomp starts new threads for each nested region, which doubles the time of an application when a batch has a single prime. On an Intel Xeon Gold 6354 with 32 threads and FLINT 3.6, with p = 4, eco14 goes from 27.8 s and 9.7 GB to 24.4 s and 3.3 GB, cyclic9 from 160 s and 24.3 GB to 159 s and 6.9 GB, and chandra-15 from 148 s and 58.5 GB to 150 s and 22.2 GB. With p = 32, eco14 takes 35.8 s and 1.5 GB and chandra-15 331 s and 15.4 GB, against 41.2 s and 429 s when the primes are computed and lifted one at a time with 32 threads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR uses the threads given by
-tto lift Groebner bases over the rationals (-g 2). The output does not depend on the number of threads.It builds on #411: until #411 is merged, the diff also shows its commits. The 11 commits of this PR start with "Read the trace cache with atomic loads".
Changes
-t t, the trace is applied to batches of t primes, one prime per thread. The images are then lifted prime by prime, in the same order as with one thread, so that the lifting decisions do not change. Two data races made this unsafe and are fixed first: the matrices cached in the trace are now read with acquire loads, and the elimination via DRL no longer writes into meta data shared by the primes.--local-threads p. The memory grows with t (see below). With--local-threads p, where p divides t, the batches have t/p primes, each computed by p threads; the CRT still uses the t threads. Without the option, nothing changes.Number of primes
With one thread, the primes are those of #411 (identical on the 39 systems below). With t threads, the number of primes may grow:
--local-threads p, the batches have t/p primes, so that the primes used depend on p.The primes depend only on the trace and on t (and p), not on timings. The trace learned with several threads is not deterministic, so that the number of primes may vary between two runs with the same t; in two runs of the 39 systems at 8 and 32 threads, it did not.
Benchmarks
On a 2 x 18-core Intel Xeon Gold 6354 (AVX-512 IFMA, primes of 52 bits), FLINT 3.6,
-g 2 -t t --random-seed 1, one run per system and thread count. The times are elapsed times of the whole run, including writing the output. Memory is the peak resident size. "#411" is the branch of #411 before its last two commits (fix of the checks and of the 52-bit primes on Windows), which do not change these times. All outputs are identical to the output with one thread.On these 39 systems, with 1 thread, the PR takes the time of #411 (from 0.98 to 1.08 times faster, 1.01 as a geometric mean). It is 2.61 times faster than #411 with 8 threads (from 1.22 on eco15 to 5.50 on minrank_d_2_p_2_q_8_r_2) and 3.73 times faster with 32 threads (from 1.22 to 10.96), as geometric means. With 32 threads, it is 7.0 times faster than with one thread, and 11.1 times on the 19 systems taking at least 30 s with one thread (from 5.7 on chandra-14 to 18.7 on KLYZ22). The total time goes from 9102 s with one thread to 1575 s with 8 threads and 798 s with 32 threads (#411: 2885 s and 2041 s).
The scaling is limited on chandra (5 to 6 times with 32 threads): the trace learned with several threads makes about 1.5 times more operations per prime than the trace learned with one thread. It is also limited on the systems which need few primes (eco, noon, kat11, kat12), where the last batch is mostly wasted.
Larger systems, gb-lifting-mt only (before the fix of the checks in #411), with a time limit of one hour, at 8 and 32 threads; with one thread, most of them exceed the limit. Both outputs are identical where both runs end. KLYZ23 and 3 other sing-visual-servoing systems exceed the limit with 32 threads too.
Memory
The memory grows with the number of threads: each thread runs its own F4 application, the bases computed are held until they are lifted, and the images wait for the next reconstruction attempt, made once per batch. With 32 threads, the peak is 3.9 times that with one thread as a geometric mean on the 39 systems, and 4.4 to 14 times on the systems with large F4 applications (chandra-15: 12.8 to 55.7 GB, kat14: 5.7 to 31.5 GB, eco15: 5.2 to 39.2 GB, cyclic9: 1.7 to 23.2 GB).
--local-threads ptrades time for memory. With 32 threads:A bug found on the way
The first runs of this branch gave a wrong basis for chandra-15 with 32 threads: one coefficient of 3369 bits instead of 1028 bits, correct modulo the first 64 primes only. The polynomials lifted again after a failed check were counted as checked by a new prime without being checked. This is fixed in #411 ("Check the polynomials lifted again after a failed check"); with the fix, chandra-15 gives the right basis with 32 threads (149.7 s, 96 primes). The same counting is still in master, where a wrong lift is less likely since the coefficients are not accepted as integers.
Tests
make checkpasses (98 tests, including a new test of--local-threadson cyclic5), on Linux with and without AVX-512 IFMA, and with the arguments of the GMP functions taking anunsigned longtruncated to 32 bits, as on Windows.Some of the changes in this PR were made with the assistance of generative AI (Claude Code).
🤖 Generated with Claude Code