Two-prime and cyclic fft_small-based mpn multiplication - #2804
Conversation
|
Two comments: The speedup actually extends to 100,000 limbs or so, maybe half that for high products. Not sure if I'll bump the cutoff here or in a followup PR with further tuning. This speeds up transform-based complex multiplication, but 2x2 matrix multiplication slows down at some sizes. The reason seems to be that complex multiplication uses the cached mpn_ctx buffer for its scratch space while matrix multiplication mallocs its operands and suffers extra page faults with the larger allocations. I think the solution is to redesign memory allocation for transformed rings to allow using the mpn_ctx buffer everywhere. |
6b3db38 to
102d795
Compare
|
Fixing some loose ends here forced refactoring the transformed mpn ring memory management for the better (simpler interface, faster). |
The
fft_smallbased integer multiplication previously only supported using 3 to 8 FFT primes. We add a specialized 2-prime version which has much less overhead for CRT and conversions.This speeds up
fft_small-basedflint_mpn_muland friends at every size up to about 12,000 limbs, usually by about 10-20%. The peak speedup I've measured is more than 50% occuring at 689 limbs (13,000 digits). The GMP Toom -> fft_small crossover for full multiplication drops from 400 to 240 limbs (7700 -> 4600 digits) on Zen 3 and to 300 limbs for mulhigh. Plot of the speedup forflint_mpn_mul:Speedup for
flint_mpn_mul,flint_mpn_sqr,flint_mpn_mulhigh_n,flint_mpn_sqrhigh, n in limbs:The PR also adds cyclic
mpnproducts (multiplying mod2^N-1) for future use and makes some tweaks to the negacyclic code.Developed using Claude Fable 5.
The updated tuning parameters for Zen 3 and Zen 4 are from my Zen 3 machine; the Skylake and later parameters are Claude's. Someone may want to double check these and do some tuning for arm64 as well.
Something to note is that there are three versions of the code for 2-prime input conversion. The one chosen for x86-64 uses byte-unaligned word loads, which in practice performed better than a SIMD version. The SIMD version is used on arm64 out of caution that unaligned loads may be much slower there, but neither version has been timed.