Debian Codegraph @

src/fft/README

raw
1 FFT Integer Multiplication code
2 ===============================
3
4 License: BSD
5
6 NOTE: The following note is no longer applicable.
7
8 (Note the FLINT library of which this is a part is overall LGPL v2.1+ and the
9 latest version of GMP on which this currently depends is LGPL v3+. But
10 the files in this implementation of the FFT are individually licensed BSD.)
11
12 Introduction
13 ------------
14
15 Many bignum libraries and programming languages do not contain fast code for
16 multiplication of huge integers. It is important to have this when computing
17 millions of digits of Pi, multiplying polynomials using Kronecker segmentation
18 (or the Schoenhage-Strassen technique using an FFT directly) and a variety of
19 other problems.
20
21 Here we introduce fast FFT code for multiplication of huge integers.
22
23 Currently the code depends on GMP, however, any sufficiently well
24 developed bignum library should have equivalent primitives for bignums. (There
25 is also a dependence on the flint function n_revbin, which is found in the
26 ulong_extras directory of flint -- I hereby license it under the BSD
27 license as per the remainder of the FFT implementation.)
28
29 To use the FFT for multiplying large integers, one needs to use the function
30 flint_mpn_mul_fft_main as documented in the doc directory. This relies on tuning
31 values supplied in fft_tuning.h in the top level source directory.
32
33 Features:
34 --------
35
36 * Cache friendly up to huge transforms (integers of billions of bits)
37 * Truncated -- no uglytwit performance jumps at power of 2 lengths and no problem
38 with unbalanced multiplications
39 * Extremely fast
40 * Easy to tune
41 * Truncated FFT/IFFT functions can be used for polynomial multiplication
42
43 Performance Data
44 ----------------
45
46 Here are timings for multiplication of two integers of the given number of
47 bits, comparing MPIR 2.4.0, this code and GMP 5.0.2 respectively. The timings
48 are for varying numbers of iterations as specified. The timings were done on
49 a 2.2GHz AMD K10-2 Mangy Cours machine.
50
51 The tuning values used are specified in the final two columns.
52
53 The first part of the table uses mul_truncate_sqrt2, the second half uses
54 mul_mfa_truncate_sqrt2.
55
56 bits iters mpir this gmp n w
57
58 195840 1000 1.149s 1.105s 0.997s 7 16
59 261120 1000 1.483s 1.415s 1.396s 7 16
60 391296 100 0.261s 0.248s 0.282s 8 8
61 521728 100 0.344s 0.315s 0.411s 8 8
62 782592 100 0.577s 0.539s 0.628s 9 4
63 1043456 100 0.706s 0.688s 0.848s 9 4
64 1569024 100 1.229s 1.153s 1.317s 9 8
65
66 2092032 100 1.543s 1.440s 2.765s 9 8
67 3127296 10 0.283s 0.266s 0.408s 11 1
68 4169728 10 0.357s 0.335s 0.543s 11 1
69 6273024 10 0.621s 0.597s 0.843s 11 2
70 8364032 10 0.831s 0.742s 1.156s 11 2
71 12539904 10 1.441s 1.394s 1.798s 12 1
72 16719872 1 0.230s 0.205s 0.288s 12 1
73 25122816 1 0.379s 0.336s 0.434s 12 2
74 33497088 1 0.524s 0.428s 0.646s 12 2
75 50245632 1 0.833s 0.693s 1.035s 13 1
76 66994176 1 1.596s 0.896s 1.358s 13 1
77 100577280 1 1.906s 1.552s 2.177s 13 2
78 134103040 1 2.784s 2.076s 2.984s 13 2
79 201129984 1 3.971s 3.158s 4.536s 14 1
80 268173312 1 5.146s 4.137s 5.781s 14 1
81 402456576 1 7.548s 6.443s 9.867s 14 2
82 536608768 1 9.841s 8.365s 12.71s 14 2
83 804913152 1 15.48s 13.29s 20.06s 15 1
84 1073217536 1 21.17s 17.16s 27.19s 15 1
85 1610219520 1 31.64s 28.60s 43.37s 15 2
86 2146959360 1 43.25s 37.02s 57.66s 15 2
87 3220340736 1 70.14s 58.09s 92.94s 16 1
88 4293787648 1 96.00s 74.26s 146.1s 16 1
89 6441566208 1 150.2s 131.1s 217.5s 16 2
90 8588754944 1 208.4s 175.0s 312.8s 16 2
91 12883132416 1 327.4s 278.6s 447.7s 17 1
92 17177509888 1 485.0s 360.ss 614.2s 17 1
93
94
95 Additional tuning
96 -----------------
97
98 Technically one should tune the values that appear in fft_tuning.h. The
99 mulmod_2expp1 tuning array indices correspond to (n, w) pairs starting
100 at n = 12, w = 1. The values in the array should be nonnegative and less
101 than 6. The length of the array is given by FFT_N_NUM. The cutoff
102 FFT_MULMOD_2EXPP1_CUTOFF should also be tuned. It must be bigger than
103 128. The function that these values tunes is in the file mulmod_2expp1.c.
104 See the corresponding test function for an example of how to call it.
105
106 The fft tuning array indices correspond to (n, w) pairs starting at
107 n = 6, w = 1. The values in the array should be nonnegative and less
108 than 6. The function that is tuned is in the file mpn_mul_fft_main.c.
109 See the corresponding test function for an example of how to call it.
110 The function implementation itself is the best reference for which
111 inputs will use which table entries.
112
113 Strategy
114 --------
115
116 Let's suppose we wish to compute a convolution of length 2n where n is a power of 2. We do this with a standard Fermat transform with coefficients mod p = 2^wn + 1. Note 2^w is a 2n-th root of unity.
117
118 We assume wn is divisible by GMP_LIMB_BITS (either 32 or 64). In practice n is always divisible by this constant.
119
120 Write l = wn/GMP_LIMB_BITS. Each coeff is stored in a block of l+1 limbs in twos complement form. We accumulate carries in the top limb meaning reduction mod p does not need to be done after an addition or subtraction.
121
122 Coefficients are also accessed via one level of indirection so that coefficients can be swapped by swapping pointers.
123
124 A couple of extra temporary coefficients are allocated for operations which cannot be done entirely in-place.
125
126 1. Efficient butterflies
127
128 The FFT butterfly step is:
129
130 [a{i}, b{i}] => [a{i}+b{i}, z^i*(a{i}-b{i})]
131
132 We use sumdiff to simultaneously perform the addition and subtraction. The multiplication by z^i is a shift by iw bits which we decompose into a shift by b bits and x limbs. The output is written in a location with an offset of x limbs. To handle the wraparound we split the operation into two parts. Finally we shift by the remaining b bits. An additional negation needs to occur when i >= n as nw = -1 mod p.
133
134 The IFFT butterfly is:
135
136 [a{i}, b{i}] => [a{i}+z^-i*b{i}, a{i}-z^-i*b{i}]
137
138 We first decompose iw into b bits and x limbs. We perform the bit shift first, in-place. Then we use sumdiff, this time reading at an offset of x limbs, splitting the operation into two parts as before.
139
140 2. Cache locality
141
142 We use the Matrix Fourier Algorithm. To perform an FFT of length m = RC we:
143
144 * Split the coefficients into R rows of C columns
145 * Perform a length R FFT on each column, i.e. with an input stride of C
146 * Multiply each coefficient by z^{r*c} where z = exp(2*Pi*I/m),
147 note z corresponds to a shift of w bits
148 * Perform a length C FFT on each row, i.e. with an input stride of 1
149 * Transpose the matrix of coefficients
150
151 To perform an IFFT we complete the steps in reverse, using IFFT's instead of FFT's.
152
153 We set R, C to be both around sqrt(m) to minimise the maximum size of FFT which is in cache at any one time. When the FFT is followed by the IFFT as in the convolution we do not perform the transposes of the matrix coefficients as they cancel each other out.
154
155 We do not perform the twiddles by z^{rc} in a separate pass over the data. We combine them with the length R FFT's and IFFT's. They are combined with the butterflies at the very bottom level of the FFT's and IFFT's. They essentially cost nothing as they just increase the bit shifts already being performed.
156
157 The algorithm expects the FFT's to output their coefficients in reverse binary order, thus we have to revbin the coefficient order after the column FFTs and before the column IFFTs.
158
159 3. Truncation
160
161 When performing a convolution where we know that many of the output coefficients will be zero (this happens when multiplying integers that are not of an optimal split-into-a-nice-power-of-two length) we can use Van der Hoeven's truncated FFT.
162
163 There are two cases: a) less than or equal to half of the FFT output coeffs
164 are non-zero and b) more than half the coefficients are non-zero:
165
166 a) A 0 0 0
167
168 b) A A A 0
169
170 In the first case, the first layer of the FFT would do nothing. As we
171 only care about the left half, we recurse on only the left half A 0,
172 ignoring the remaining zeros.
173
174 In the second case we compute the first layer of the FFT. We then do
175 an ordinary FFT on the left half and recurse with a truncated FFT on
176 the right half.
177
178 Of course we need to be careful in that the first layer of the FFT
179 will have replaced our zeroes with non-zero coefficients, so we don't
180 recurse to the above two cases.
181
182 We start instead with an FFT with non-zero coefficients (labelled B).
183
184 A B B B
185
186 or
187
188 A A A B
189
190 But the cases can be dealt with in a similar way to the cases where
191 there are zeros. The saving comes in that we repeatedly ignore
192 coefficients on the right hand side when they are all past the
193 truncation point.
194
195 The IFFT is slightly more involved. We know that we are going to
196 *end up with* zeroes on the right hand side. We start with the results
197 of the pointwise mults, though we do not perform all the pointwise
198 mults. If we are going to end up with c zeroes, we do not perform the
199 last c pointwise mults.
200
201 So we want our IFFT to get to
202
203 A A A 0
204
205 starting from
206
207 P P P ?
208
209 Again there are two cases, depending on how many zeros we will end up with:
210
211 a) A 0 0 0
212
213 b) A A A 0
214
215 In case (a) , by working backwards from what we know we will get, the
216 next to last level must be
217
218 A/2 0 (A/2)~ 0 where ~ is the opposite of the twiddle that will be
219 applied by the IFFT butterfly.
220
221 But I can compute the LHS, A/2 0, simply by recursing on the truncated
222 IFFT. Then it is just a matter of multiplying by 2 to get A 0 which is
223 what I was after.
224
225 In case (b) an ordinary IFFT can compute the left hand of the
226 penultimate layer, as we have all the necessary pointwise mults for
227 that.
228
229 A A A 0
230 B B ? ?
231
232 The right hand side we compute by recursing on the truncated IFFT. But
233 we don't know what the final question mark is. To get it we have to
234 reverse the steps of the IFFT to find it. As we have the second B we
235 can compute the second A simply by performing some IFFT butterflies.
236 Now we can compute the second ? by reversing the IFFT butterflies. So
237 we are left with:
238
239 A A' A 0'
240 B' B' ? C'
241
242 where I wrote a dash on the coefficients we actually now know.
243
244 Now we can recurse using the truncated IFFT on the right hand side.
245
246 Although the coefficients C' are not zero, the principles are the same
247 and we split into two cases as above.
248
249 This allows us to get the question mark, yielding:
250
251 A A' A 0'
252 B' B' C' C'
253
254 and clearly now we can compute the A's we don't know from the known
255 coefficients.
256
257 To combine the MFA with truncation we simply truncate at one level of the MFA, i.e. set the truncation length to be a multiple of the length of the inner FFT's. When we are at the lower levels computing row FFT's we don't compute those which lie past the truncation point.
258
259 We need to take care to perform the right pointwise mults because we do not transpose the matrix or output coefficients in revbin order.
260
261 4. Negacyclic convolution
262
263 The pointwise multiplications mod p are sometimes large enough to make use of an FFT. For this purpose we use a negacyclic convolution which naturally performs integer multiplication mod p.
264
265 If we do this naively we break up into coefficients whose sizes are multiples of half the negacyclic FFT lengths. This becomes inefficient.
266
267 In order to get around this we must perform two multiplications, one via a negacyclic FFT with big coefficients and one naively with very small coefficients and CRT them together. This gives more flexibility in the size of coefficients we use in the negacyclic FFT allowing the large pointwise multication mod p to be done efficiently (not implemented yet).
268
269 5. Sqrt 2 trick
270
271 In the ring Z/pZ where p = 2^S + 1 the value 2^(2S/4)-2^(S/4) is a
272 square root of 2. This allows us to perform a convolution of twice
273 the length without twice the cost. To perform the operations we need
274 to be able to perform twiddles by powers of sqrt2. These are decomposed
275 and the operations are combined as much as possible with the
276 multiplications by powers of 2.
277
278 Acknowledgements
279 ----------------
280
281 "Matters Computational: ideas, algorithms and source code", by Jorg
282 Arndt, see https://www.jjj.de/fxt/fxtbook.pdf
283
284 "Primes numbers: a computational perspective", by Richard Crandall and
285 Carl Pomerance, 2nd ed., 2005, Springer.
286
287 "A GMP-based implementation of Schonhage-Strassen's Large Integer
288 Multiplication Algorithm" by Pierrick Gaudry, Alexander Kruppa and
289 Paul Zimmermann, ISAAC 2007 proceedings, pp 167-174. See
290 http://www.loria.fr/~gaudry/publis/issac07.pdf
291
292 "Multidigit multiplication for mathematicians" by Dan Bernstein (to
293 appear). see https://cr.yp.to/papers/m3.pdf
294
295 "A cache-friendly truncated FFT" by David Harvey, Theor. Comput. Sci. 410 (2009), 2649.2658. See http://web.maths.unsw.edu.au/~davidharvey/papers/cache-trunc-fft/
296
297 "The truncated Fourier transform and applications" by Joris van der Hoeven, J. Gutierrez, editor, Proc. ISSAC 2004, pages 290.296, Univ. of Cantabria, Santander, Spain, July 4.7 2004.
298