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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
from time import time
import numpy as np
import pyfftw
import scipy.fftpack as sp
from mpi4py_fft import fftw
import pickle
try:
#fftw.import_wisdom('wisdom.dat')
pyfftw.import_wisdom(pickle.load(open('pyfftw.wisdom', 'rb')))
print('Wisdom imported')
except:
print('Wisdom not imported')
N = (64, 64, 64)
loops = 50
axis = 1
threads = 4
implicit = True
flags = (fftw.FFTW_PATIENT, fftw.FFTW_DESTROY_INPUT)
# Transform complex to complex
#A = pyfftw.byte_align(np.random.random(N).astype('D'))
#A = np.random.random(N).astype(np.dtype('D'))
A = fftw.aligned(N, n=8, dtype=np.dtype('D'))
A[:] = np.random.random(N).astype(np.dtype('D'))
#print(A.ctypes.data % 32)
input_array = fftw.aligned(A.shape, n=32, dtype=A.dtype)
output_array = fftw.aligned(A.shape, n=32, dtype=A.dtype)
AC = A.copy()
ptime = [[], []]
ftime = [[], []]
stime = [[], []]
for axis in ((1, 2), 0, 1, 2):
axes = axis if np.ndim(axis) else [axis]
# pyfftw
fft = pyfftw.builders.fftn(input_array, axes=axes, threads=threads,
overwrite_input=True)
t0 = time()
for i in range(loops):
C = fft(A)
ptime[0].append(time()-t0)
# us
fft = fftw.fftn(input_array, None, axes, threads, flags)
t0 = time()
for i in range(loops):
C2 = fft(A, implicit=implicit)
ftime[0].append(time()-t0)
assert np.allclose(C, C2)
# scipy
if not A.dtype.char.upper() == 'G':
C3 = sp.fftn(A, axes=axes) # scipy is caching, so call once before
t0 = time()
for i in range(loops):
C3 = sp.fftn(A, axes=axes)
stime[0].append(time()-t0)
else:
stime[0].append(0)
# pyfftw
ifft = pyfftw.builders.ifftn(output_array, axes=axes, threads=threads,
overwrite_input=True)
CC = C.copy()
t0 = time()
for i in range(loops):
B = ifft(C, normalise_idft=True)
ptime[1].append(time()-t0)
# us
ifft = fftw.ifftn(output_array, None, axes, threads, flags)
t0 = time()
for i in range(loops):
B2 = ifft(C, normalize=True, implicit=implicit)
ftime[1].append(time()-t0)
assert np.allclose(B, B2), np.linalg.norm(B-B2)
# scipy
if not C.dtype.char.upper() == 'G':
B3 = sp.ifftn(C, axes=axes) # scipy is caching, so call once before
t0 = time()
for i in range(loops):
B3 = sp.ifftn(C, axes=axes)
stime[1].append(time()-t0)
else:
stime[1].append(0)
print("Timing forward transform axes (1, 2), 0, 1, 2")
print("pyfftw {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ptime[0]))
print("mpi4py {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ftime[0]))
print("scipy {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*stime[0]))
print("Timing backward transform axes (1, 2), 0, 1, 2")
print("pyfftw {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ptime[1]))
print("mpi4py {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ftime[1]))
print("scipy {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*stime[1]))
# Transform real to complex
# Not scipy because they do not have rfftn
#A = pyfftw.byte_align(np.random.random(N).astype('d'))
A = np.random.random(N).astype(np.dtype('d', align=True))
input_array = np.zeros_like(A)
ptime = [[], []]
ftime = [[], []]
for axis in ((1, 2), 0, 1, 2):
axes = axis if np.ndim(axis) else [axis]
# pyfftw
rfft = pyfftw.builders.rfftn(input_array, axes=axes, threads=threads)
t0 = time()
for i in range(loops):
C = rfft(A)
ptime[0].append(time()-t0)
# us
rfft = fftw.rfftn(input_array, None, axes, threads, flags)
t0 = time()
for i in range(loops):
C2 = rfft(A, implicit=implicit)
ftime[0].append(time()-t0)
assert np.allclose(C, C2)
# pyfftw
irfft = pyfftw.builders.irfftn(C.copy(), s=np.take(input_array.shape, axes),
axes=axes, threads=threads)
t0 = time()
for i in range(loops):
C2[:] = C # Because irfft is overwriting input
D = irfft(C2, normalise_idft=True)
ptime[1].append(time()-t0)
# us
irfft = fftw.irfftn(C.copy(), np.take(input_array.shape, axes), axes, threads, flags)
t0 = time()
for i in range(loops):
C2[:] = C
D2 = irfft(C2, normalize=True, implicit=implicit)
ftime[1].append(time()-t0)
assert np.allclose(D, D2), np.linalg.norm(D-D2)
print("Timing real forward transform axes (1, 2), 0, 1, 2")
print("pyfftw {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ptime[0]))
print("mpi4py {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ftime[0]))
print("Timing real backward transform axes (1, 2), 0, 1, 2")
print("pyfftw {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ptime[1]))
print("mpi4py {0:2.4e} {1:2.4e} {2:2.4e} {3:2.4e}".format(*ftime[1]))
fftw.export_wisdom('wisdom.dat')
|