vincefn / pyvkfft

Python interface to VkFFT
MIT License
51 stars 6 forks source link

Unintentional in-place irfftn transform #18

Closed mDiracYas closed 2 years ago

mDiracYas commented 2 years ago

Hi!

Thanks for pyvkfft and your effort on this awesome opensource project!

When applying vkfft.fft.irfftn to cupy array, the input array seems to be unintentionally changed despite of not using the in-place command.

Sample code

import pyvkfft
import pyvkfft.fft
import cupy as cp

x = cp.arange(4,dtype='float32').reshape(2,2)

x_fft = pyvkfft.fft.rfftn(x)
x_fft_copy = x_fft.copy()

x2 = pyvkfft.fft.irfftn(x_fft)

print("original array x:\n",x,"\n double-transformed array x2:\n",x2)

print("fft array x_fft:\n",x_fft, "\n fft array copied x_fft_copy:\n", x_fft_copy)
# these arrays are not identical in spite of not using inplace transform.

Output

original array x:
 [[0. 1.]
 [2. 3.]] 
 double-transformed array x2:
 [[0. 1.]
 [2. 3.]]
fft array x_fft:
 [[ 1.+0.j -1.+0.j]
 [ 5.+0.j -1.+0.j]] 
 fft array copied x_fft_copy:
 [[ 6.+0.j -2.+0.j]
 [-4.+0.j  0.+0.j]]

Environment OS:Windows 10 cuda: 11.3 pyvkfft version: 2022.1.1

vincefn commented 2 years ago

This is known and documented (see https://github.com/vincefn/pyvkfft/blob/master/pyvkfft/cuda.py#L101), it is due to the way intermediate calculations must be made. The same is true in cufft.

The same info should probably also be given in pyvkfft.fft.irfftn though

mDiracYas commented 2 years ago

I'm sorry for skipping over the details of the document. I understand that it is the present specification. Thank you for your quick response.

vincefn commented 2 years ago

No problem, it's counter-intuitive that an out-of-place transform destroys the original array. It took me a little while to understand that with cufft...