|
| 1 | +"""Benchmarks for 1-D FFT operations using the mkl_fft root API.""" |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import mkl_fft |
| 6 | + |
| 7 | +_RNG_SEED = 42 |
| 8 | + |
| 9 | + |
| 10 | +def _make_input(rng, n, dtype): |
| 11 | + """Return a 1-D array of length *n* with the given *dtype*. |
| 12 | +
|
| 13 | + Complex dtypes are populated with non-zero imaginary parts so the |
| 14 | + benchmark exercises a genuine complex transform path. |
| 15 | + """ |
| 16 | + dt = np.dtype(dtype) |
| 17 | + if dt.kind == "c": |
| 18 | + return (rng.standard_normal(n) + 1j * rng.standard_normal(n)).astype(dt) |
| 19 | + return rng.standard_normal(n).astype(dt) |
| 20 | + |
| 21 | + |
| 22 | +# --------------------------------------------------------------------------- |
| 23 | +# Complex-to-complex 1-D (power-of-two sizes) |
| 24 | +# --------------------------------------------------------------------------- |
| 25 | + |
| 26 | + |
| 27 | +class TimeFFT1D: |
| 28 | + """Forward and inverse complex FFT — power-of-two sizes.""" |
| 29 | + |
| 30 | + params = [ |
| 31 | + [64, 256, 1024, 4096, 16384, 65536], |
| 32 | + ["float32", "float64", "complex64", "complex128"], |
| 33 | + ] |
| 34 | + param_names = ["n", "dtype"] |
| 35 | + |
| 36 | + def setup(self, n, dtype): |
| 37 | + rng = np.random.default_rng(_RNG_SEED) |
| 38 | + self.x = _make_input(rng, n, dtype) |
| 39 | + |
| 40 | + def time_fft(self, n, dtype): |
| 41 | + mkl_fft.fft(self.x) |
| 42 | + |
| 43 | + def time_ifft(self, n, dtype): |
| 44 | + mkl_fft.ifft(self.x) |
| 45 | + |
| 46 | + |
| 47 | +# --------------------------------------------------------------------------- |
| 48 | +# Real-to-complex / complex-to-real 1-D (power-of-two sizes) |
| 49 | +# --------------------------------------------------------------------------- |
| 50 | + |
| 51 | + |
| 52 | +class TimeRFFT1D: |
| 53 | + """Forward rfft and inverse irfft — power-of-two sizes.""" |
| 54 | + |
| 55 | + params = [ |
| 56 | + [64, 256, 1024, 4096, 16384, 65536], |
| 57 | + ["float32", "float64"], |
| 58 | + ] |
| 59 | + param_names = ["n", "dtype"] |
| 60 | + |
| 61 | + def setup(self, n, dtype): |
| 62 | + rng = np.random.default_rng(_RNG_SEED) |
| 63 | + cdtype = "complex64" if dtype == "float32" else "complex128" |
| 64 | + self.x_real = rng.standard_normal(n).astype(dtype) |
| 65 | + # irfft input: complex half-spectrum of length n//2+1 |
| 66 | + self.x_complex = ( |
| 67 | + rng.standard_normal(n // 2 + 1) |
| 68 | + + 1j * rng.standard_normal(n // 2 + 1) |
| 69 | + ).astype(cdtype) |
| 70 | + |
| 71 | + def time_rfft(self, n, dtype): |
| 72 | + mkl_fft.rfft(self.x_real) |
| 73 | + |
| 74 | + def time_irfft(self, n, dtype): |
| 75 | + mkl_fft.irfft(self.x_complex, n=n) |
| 76 | + |
| 77 | + |
| 78 | +# --------------------------------------------------------------------------- |
| 79 | +# Complex-to-complex 1-D (non-power-of-two sizes) |
| 80 | +# --------------------------------------------------------------------------- |
| 81 | + |
| 82 | + |
| 83 | +class TimeFFT1DNonPow2: |
| 84 | + """Forward and inverse complex FFT — non-power-of-two sizes. |
| 85 | +
|
| 86 | + MKL uses a different code path for non-power-of-two transforms; |
| 87 | + this suite catches regressions in that path. |
| 88 | + """ |
| 89 | + |
| 90 | + params = [ |
| 91 | + [127, 509, 1000, 4001, 10007], |
| 92 | + ["float64", "complex128", "complex64"], |
| 93 | + ] |
| 94 | + param_names = ["n", "dtype"] |
| 95 | + |
| 96 | + def setup(self, n, dtype): |
| 97 | + rng = np.random.default_rng(_RNG_SEED) |
| 98 | + self.x = _make_input(rng, n, dtype) |
| 99 | + |
| 100 | + def time_fft(self, n, dtype): |
| 101 | + mkl_fft.fft(self.x) |
| 102 | + |
| 103 | + def time_ifft(self, n, dtype): |
| 104 | + mkl_fft.ifft(self.x) |
| 105 | + |
| 106 | + |
| 107 | +# --------------------------------------------------------------------------- |
| 108 | +# Real-to-complex / complex-to-real 1-D (non-power-of-two sizes) |
| 109 | +# --------------------------------------------------------------------------- |
| 110 | + |
| 111 | + |
| 112 | +class TimeRFFT1DNonPow2: |
| 113 | + """Forward rfft and inverse irfft — non-power-of-two sizes.""" |
| 114 | + |
| 115 | + params = [ |
| 116 | + [127, 509, 1000, 4001, 10007], |
| 117 | + ["float32", "float64"], |
| 118 | + ] |
| 119 | + param_names = ["n", "dtype"] |
| 120 | + |
| 121 | + def setup(self, n, dtype): |
| 122 | + rng = np.random.default_rng(_RNG_SEED) |
| 123 | + cdtype = "complex64" if dtype == "float32" else "complex128" |
| 124 | + self.x_real = rng.standard_normal(n).astype(dtype) |
| 125 | + self.x_complex = ( |
| 126 | + rng.standard_normal(n // 2 + 1) |
| 127 | + + 1j * rng.standard_normal(n // 2 + 1) |
| 128 | + ).astype(cdtype) |
| 129 | + |
| 130 | + def time_rfft(self, n, dtype): |
| 131 | + mkl_fft.rfft(self.x_real) |
| 132 | + |
| 133 | + def time_irfft(self, n, dtype): |
| 134 | + mkl_fft.irfft(self.x_complex, n=n) |
0 commit comments