File: test_libfft.py

package info (click to toggle)
mpi4py-fft 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 720 kB
  • sloc: python: 3,053; ansic: 87; makefile: 42; sh: 33
file content (138 lines) | stat: -rw-r--r-- 5,386 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from __future__ import print_function
from time import time
import importlib
import functools
import numpy as np
from mpi4py_fft import fftw
from mpi4py_fft.libfft import FFT

has_backend = {'fftw': True}
for backend in ('pyfftw', 'mkl_fft', 'scipy', 'numpy'):
    has_backend[backend] = True
    try:
        importlib.import_module(backend)
    except ImportError:
        has_backend[backend] = False

abstol = dict(f=5e-5, d=1e-14, g=1e-14)

def allclose(a, b):
    atol = abstol[a.dtype.char.lower()]
    return np.allclose(a, b, rtol=0, atol=atol)

def test_libfft():
    from itertools import product

    dims = (1, 2, 3)
    sizes = (7, 8, 9)
    types = ''
    for t in 'fd':
        if fftw.get_fftw_lib(t):
            types += t+t.upper()

    for backend in ('pyfftw', 'mkl_fft', 'scipy', 'numpy', 'fftw'):
        if has_backend[backend] is False:
            continue
        t0 = 0
        for typecode in types:
            for dim in dims:
                for shape in product(*([sizes]*dim)):
                    allaxes = tuple(reversed(range(dim)))
                    for i in range(dim):
                        for j in range(i+1, dim):
                            for axes in (None, allaxes[i:j]):
                                #print(shape, axes, typecode)
                                fft = FFT(shape, axes, dtype=typecode, backend=backend,
                                          planner_effort='FFTW_ESTIMATE')
                                A = fft.forward.input_array
                                B = fft.forward.output_array

                                A[...] = np.random.random(A.shape).astype(typecode)
                                X = A.copy()

                                B.fill(0)
                                t0 -= time()
                                B = fft.forward(A, B)
                                t0 += time()

                                A.fill(0)
                                t0 -= time()
                                A = fft.backward(B, A)
                                t0 += time()
                                assert allclose(A, X)
        print('backend: ', backend, t0)
    # Padding is different because the physical space is padded and as such
    # difficult to initialize. We solve this problem by making one extra
    # transform
    for backend in ('pyfftw', 'mkl_fft', 'scipy', 'numpy', 'fftw'):
        if has_backend[backend] is False:
            continue
        for padding in (1.5, 2.0):
            for typecode in types:
                for dim in dims:
                    for shape in product(*([sizes]*dim)):
                        allaxes = tuple(reversed(range(dim)))
                        for i in range(dim):
                            axis = allaxes[i]
                            axis -= len(shape)
                            shape = list(shape)
                            shape[axis] = int(shape[axis]*padding)

                            #print(shape, axis, typecode, backend)
                            fft = FFT(shape, axis, dtype=typecode, backend=backend,
                                      padding=padding, planner_effort='FFTW_ESTIMATE')
                            A = fft.forward.input_array
                            B = fft.forward.output_array

                            A[...] = np.random.random(A.shape).astype(typecode)

                            B.fill(0)
                            B = fft.forward(A, B)
                            X = B.copy()

                            A.fill(0)
                            A = fft.backward(B, A)

                            B.fill(0)
                            B = fft.forward(A, B)
                            assert allclose(B, X), np.linalg.norm(B-X)

    for backend in ('pyfftw', 'mkl_fft', 'scipy', 'numpy', 'fftw'):
        if has_backend[backend] is False:
            continue

        if backend == 'fftw':
            dctn = functools.partial(fftw.dctn, type=3)
            idctn = functools.partial(fftw.idctn, type=3)
            transforms = {(1,): (dctn, idctn),
                          (0, 1): (dctn, idctn)}
        elif backend == 'pyfftw':
            import pyfftw
            transforms = {(1,): (pyfftw.builders.rfftn, pyfftw.builders.irfftn),
                          (0, 1): (pyfftw.builders.rfftn, pyfftw.builders.irfftn)}
        elif backend == 'numpy':
            transforms = {(1,): (np.fft.rfftn, np.fft.irfftn),
                          (0, 1): (np.fft.rfftn, np.fft.irfftn)}
        elif backend == 'mkl_fft':
            import mkl_fft
            transforms = {(1,): (mkl_fft._numpy_fft.rfftn, mkl_fft._numpy_fft.irfftn),
                          (0, 1): (mkl_fft._numpy_fft.rfftn, mkl_fft._numpy_fft.irfftn)}
        elif backend == 'scipy':
            from scipy.fftpack import fftn, ifftn
            transforms = {(1,): (fftn, ifftn),
                          (0, 1): (fftn, ifftn)}

        for axis in ((1,), (0, 1)):
            fft = FFT(shape, axis, backend=backend, transforms=transforms)
            A = fft.forward.input_array
            B = fft.forward.output_array
            A[...] = np.random.random(A.shape)
            X = A.copy()
            B.fill(0)
            B = fft.forward(A, B)
            A.fill(0)
            A = fft.backward(B, A)
            assert allclose(A, X)

if __name__ == '__main__':
    test_libfft()