Commit 18291d91 authored by Matthieu Boileau's avatar Matthieu Boileau

Add FFTW_ESTIMATE

parent 3dc279c3
......@@ -3,60 +3,93 @@ Uncomment below to use 32 bit floats,
increasing the speed by a factor of 4
and remove the difference between the "builders" and "FFTW" methods
"""
import time
from __future__ import print_function
import functools
from timeit import default_timer
import numpy
import pyfftw
import scipy.fftpack
nthread = 10
nthread = 4
#a = numpy.random.rand(2364,2756).astype('complex128')
#a = numpy.random.rand(2364,2756).astype('complex64')
a = numpy.random.rand(2048, 2048).astype('complex64')
start = time.time()
b1 = numpy.fft.fft2(a)
end1 = time.time() - start
start = time.time()
b11 = scipy.fftpack.fft2(a)
end11 = time.time() - start
start = time.time()
#b2=scipy.fftpack.fft2(a)
b2 = pyfftw.interfaces.scipy_fftpack.fft2(a, threads=nthread)
end2 = time.time() - start
pyfftw.forget_wisdom()
start = time.time()
#b3=scipy.fftpack.fft2(a,overwrite_x=True)
b3 = pyfftw.interfaces.numpy_fft.fft2(a, threads=nthread)
end3 = time.time() - start
""" By far the most efficient method """
pyfftw.forget_wisdom()
start = time.time()
b4 = numpy.zeros_like(a)
fft = pyfftw.FFTW( a, b4, axes=(0,1), direction='FFTW_FORWARD', flags=('FFTW_MEASURE', ), threads=nthread, planning_timelimit=None )
fft()
end4 = time.time() - start
"""
For large arrays avoiding the copy is very important,
doing this I get a speedup of 2x compared to not using it
"""
pyfftw.forget_wisdom()
start = time.time()
b5 = numpy.zeros_like(a)
fft = pyfftw.builders.fft2(a, s=None, axes=(-2, -1), overwrite_input=False, planner_effort='FFTW_MEASURE', threads=nthread, auto_align_input=False, auto_contiguous=False, avoid_copy=True)
b5 = fft()
end5 = time.time() - start
def full_name(o):
try:
return "{}.{}".format(o.__module__, o.__name__)
except AttributeError:
return "Unknown"
def timer(func, comment=None):
"""Decorator that display the function execution time"""
@functools.wraps(func)
def func_call(*args, **kwargs):
if comment:
print("Running {} with {}...".format(full_name(func), comment))
else:
print("Running {}...".format(full_name(func)))
start_time = default_timer() # Start timer
result = func(*args, **kwargs)
elapsed_time = default_timer() - start_time
print("Total time [s]: {:f}".format(elapsed_time))
return result
return func_call
if __name__ == '__main__':
# Numpy FFT
numpy.fft.fft2 = timer(numpy.fft.fft2)
b1 = numpy.fft.fft2(a)
# Scipy FFT
scipy.fftpack.fft2 = timer(scipy.fftpack.fft2)
b2 = scipy.fftpack.fft2(a)
# PyFFTW
pyfftw.interfaces.scipy_fftpack.fft2 = timer(
pyfftw.interfaces.scipy_fftpack.fft2)
# PyFFTW 2
pyfftw.forget_wisdom()
pyfftw.interfaces.numpy_fft.fft2 = timer(pyfftw.interfaces.numpy_fft.fft2)
b4 = pyfftw.interfaces.numpy_fft.fft2(a, threads=nthread)
# PyFFTW 3
pyfftw.forget_wisdom()
b4 = numpy.zeros_like(a)
fftw_measure = timer(pyfftw.FFTW, comment="FFTW_MEASURE")
b5 = fftw_measure(a, b4, axes=(0, 1), direction='FFTW_FORWARD',
flags=('FFTW_MEASURE', ), threads=nthread,
planning_timelimit=None)
# PyFFTW 3
pyfftw.forget_wisdom()
fftw_estimate = timer(pyfftw.FFTW, comment="FFTW_ESTIMATE")
b6 = fftw_estimate(a, b4, axes=(0, 1), direction='FFTW_FORWARD',
flags=('FFTW_ESTIMATE', ), threads=nthread,
planning_timelimit=None)
# PyFFTW 4
pyfftw.forget_wisdom()
pyfftw_builders_measure = timer(pyfftw.builders.fft2,
comment="FFTW_MEASURE")
b7 = pyfftw_builders_measure(a, s=None, axes=(-2, -1),
overwrite_input=False,
planner_effort='FFTW_MEASURE',
threads=nthread, auto_align_input=False,
auto_contiguous=False, avoid_copy=True)
# PyFFTW 5
pyfftw.forget_wisdom()
pyfftw_builders_estimate = timer(pyfftw.builders.fft2,
comment="FFTW_ESTIMATE")
b8 = pyfftw_builders_estimate(a, s=None, axes=(-2, -1),
overwrite_input=False,
planner_effort='FFTW_ESTIMATE',
threads=nthread, auto_align_input=False,
auto_contiguous=False, avoid_copy=True)
print('numpy.fft.fft2: %.3f secs.' % end1)
print('scipy.fftpack.fft2: %.3f secs.' % end11)
print('pyfftw.interfaces.scipy_fftpack.fft2: %.3f secs.' % end2)
print('pyfftw.interfaces.numpy_fft.fft2: %.3f secs.' % end3)
print('pyfftw.FFTW: %.3f secs.' % end4)
print('pyfftw.builders: %.3f secs.' % end5)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment