FFT Speed on Noncubic meshes
<p>I need to repeatedly take the Fourier Transform/Inverse Fourier Transform of a 3d function in order to solve a differential equation. Something like:</p>

<pre><code>import pyfftw.interfaces.numpy_fft as fftw
for i in range(largeNumber):
    fFS = fftw.rfftn(f)
    # Do stuff
    f = fftw.irfftn(fFS)
</code></pre>

<p>The shape of f is highly noncubic. Is there any performance difference based on the order of dimensions, for example (512, 32, 128) vs (512, 128, 32), etc.? </p>

<p>I am looking for any speed ups available. I have already tried playing around with wisdom. I thought it might be fastest if the largest dimension went last (e.g. 32, 128, 512) so that fFS.shape = (32, 128, 257), but this doesn't appear to be the case.</p>

<p>If you really want to squeeze all the performance out you can, use the FFTW object directly (most easily accessed through <code>pyfftw.builders</code>). This way you get careful control over exactly what copies occur and whether the normalization is performed on inverse.</p>

<p>Your code as-is will likely benefit from using the cache (enabled by calling <code>pyfftw.interfaces.cache.enable()</code>), which minimises the set up time for the general and safe case, though doesn't eliminate it.</p>

<p>Regarding the best arrangement of dimensions, you'll have to suck it and see. Try all the various options and see what is fastest (with <code>timeit</code>). Make sure when you do the tests you're actually using the data arranged in memory as expected and not just taking a view of the same array in memory (which <code>pyfftw</code> may well handle fine without a copy - though there are tweak parameters for this sort of thing).</p>

<p><code>FFTW</code> tries lots of different options (different algorithms over different FFT representations) and picks the fastest, so you end up with non-obvious implementations that may well change for different datasets that are superficially very similar.</p>

<p>General tips:</p>

<ul>
<li>Turn on the multi-threading for maximum performance (set <code>threads=N</code> where appropriate).</li>
<li>Make sure your arrays are suitably byte aligned - this has less impact than it used to with modern hardware, but will probably make a difference (particularly if all your higher dimension sizes have the byte alignment as a factor).</li>
<li>Read the <a href="http://hgomersall.github.io/pyFFTW/sphinx/tutorial.html" rel="nofollow">tutorial</a> and the <a href="http://hgomersall.github.io/pyFFTW/sphinx/api.html" rel="nofollow">api docs</a>.</li>
</ul>

python performance fft fftw pyfftw