I need to perform lots of 2D FFTs on square, power of 2, complex64 arrays. I'd like to use the pyfftw wrappers to make this as quick as possible.
I'm running Anaconda through Ubuntu 18.04 and downloaded the pyfftw package using "conda install -c conda-forge pyfftw"
I've managed to get 2D FFTs running on my system but my first-order-approximation code didn't work and my "workaround" seems like it's adding redundant code.
I'd like to know if I'm setting up the arrays, the fft_objects and performing the FFTs correctly.
pix = 256
mid = np.int(pix/2)
arr1 = np.zeros((pix,pix)).astype('complex64')
# make a rectangular "aperture"
arr1[ mid-8:mid+8 , mid-16:mid+16 ] = 1.0+0.0j
pyfftw_threads = 4
# "declare" input and output buffers to be used for the forward transform
forward_input_buffer = pyfftw.empty_aligned((pix,pix), dtype='complex64')
forward_output_buffer = pyfftw.empty_aligned((pix,pix), dtype='complex64')
# create the forward transform object using these buffers
forward_fft_object = pyfftw.FFTW(forward_input_buffer, forward_output_buffer, axes=(0,1), direction='FFTW_FORWARD', flags=('FFTW_MEASURE', ), threads=pyfftw_threads)
ok now at this point, the documentation (under Arguments in the pyfftw.FFTW class) says,
"The contents of these arrays will be destroyed by the planning process during initialisation."
so that says to me that I need to copy the data from "arr1" into the forward_input_buffer array (at this point we don't care about the output buffer because we're about to calculate that data)
so,
forward_input_buffer = np.copy( arr1 )
forward_fft_object.execute()
should calculate the FFT of the rectangular array (which we copied into the input buffer) and put the output into forward_output_buffer. But, this DOESN'T work. The forward_output_buffer data is unchanged. And this is where I get confused. Instead, to make it work you have to do,
forward_input_buffer = np.copy( arr1 )
forward_fft_object.update_arrays(forward_input_buffer, forward_output_buffer)
forward_fft_object.execute()
this DOES work. But now I have a question. Surely the np.copy command is redundant right? Instead I could just do,
forward_fft_object.update_arrays(arr1, forward_output_buffer)
forward_fft_object.execute()
but THEN my question is; does the update_arrays command copy the contents of argument1 (arr1) into the original instantiation buffer (forward_input_buffer)? OR does the command tell the forward_fft_object to now look at arr1 for its input data?
what if the other arrays I make which are the same size, dtype etc (my code might use arr1, arr2, arr3... i.e. lots of different arrays for the input) aren't n-byte aligned or simd-aligned?
edit (I realise that last bit is unclear):
if I have to perform lots of 2D FFTs on lots of different data (but always the same size, dtype). In that case I would have to do,
forward_input_buffer = np.copy( current_data_array )
forward_fft_object.update_arrays(forward_input_buffer, forward_output_buffer)
forward_fft_object.execute()
But this seems verbose so I'm wondering if I'm using the pyfftw functions correctly.