使用gnu FFTW 3.3.8计算具有复杂输入的一维FFT

时间:2019-06-22 18:48:53

标签: fortran gfortran fftw

我使用gnu FFTW 3.3.8来计算具有复杂输入的一维FFT,由于某些原因,它无法按如下所述工作。

当使用N <= 16的FFTW(来自http://www.fftw.org/的FFTW 3.3.8)时,代码运行良好(N是数组长度,所有元素都等于1)。对于N> = 22,输入和输出均返回零,在我看来,这两个值之间例如16

program hello
    implicit none
    integer N
    parameter (N = 20)
    double complex, dimension (N) :: in
    double complex, dimension (N) :: out
    real pi, one
    integer i
    double precision xone

    xone=1.0000000000
     pi=4.0*atan(one)


    ! generate input
    do i=1,N
         in(i)=xone
    end do

    call calc_fft(N, in, out)

    ! print output
    do i=1,N
        write(*,*)real (in(i)), real (out(i))
    end do

     ! output data into a file 
   open(1, file = 'dataM.dat', status='new')  
   do i = 1,N  
      write(1,*) in(i), out(i)   
   end do  
   close(1) 



end program hello

subroutine calc_fft(N,in,out)
    integer N
    double complex, dimension (N) :: in
    double complex, dimension (N) :: out
    integer*8 plan
    integer i

    call dfftw_plan_dft_1d(plan,N,in,out,FFTW_FORWARD,FFTW_ESTIMATE)
    call dfftw_execute_dft(plan, in, out)
    call dfftw_destroy_plan(plan)

end subroutine

gfortran testfftw.f90 -L / usr / lib64 / -lfftw3

N = 16时的输入/输出

   1.0000000000000000        16.000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     
   1.0000000000000000        0.0000000000000000     

 gfortran testfftw.f90 -L/usr/lib64/ -lfftw3

Input / Output for N=20
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     
   0.0000000000000000        0.0000000000000000     

我正在使用Scientific Linux 7.6版(氮)

uname -a : 

Linux localhost.localdomain 3.10.0-957.el7.x86_64 #1 SMP Tue Oct 30 14:13:26 CDT 2018 x86_64 x86_64 x86_64 GNU/Linux

gcc-gfortran-4.8.5

Intel(R)CoreTM i3-3240 CPU @ 3.40GHz

具有4GB的内存

我也尝试了具有类似效果的另一个CPU

1 个答案:

答案 0 :(得分:1)

一个大问题似乎是calc_fft()没有implicit none,因此隐式键入适用...

subroutine calc_fft(N,in,out)
    integer N
    double complex, dimension (N) :: in
    double complex, dimension (N) :: out
    ...

如果我们将implicit none添加为

subroutine calc_fft(N,in,out)
    implicit none  !<--
    integer N
    double complex, dimension (N) :: in
    double complex, dimension (N) :: out
    ...

然后gfortran给出消息

testfftw.f90:37:67:

     call dfftw_plan_dft_1d(plan,N,in,out,FFTW_FORWARD,FFTW_ESTIMATE)
                                                                   1
Error: Symbol 'fftw_estimate' at (1) has no IMPLICIT type
testfftw.f90:37:53:

     call dfftw_plan_dft_1d(plan,N,in,out,FFTW_FORWARD,FFTW_ESTIMATE)
                                                     1
Error: Symbol 'fftw_forward' at (1) has no IMPLICIT type

此处,FFTW_FORWARDFFTW_ESTIMATE是一些需要通过FFTW头文件定义的参数(否则,这些参数被视为默认的real变量,而没有{{1} }!)。

implicit none

然后我们重新编译为

subroutine calc_fft(N, in, out)
    implicit none
    include 'fftw3.f'  !<--
    integer N

并获得预期的结果。 (包含文件的位置可能因计算机/操作系统而异,ScientificLinux7似乎在gfortran testfftw.f90 -L/usr/lib64 -I/usr/include -lfftw3 中包含了它们。请参阅this page,并在必要时将它们安装为/usr/include。此外,要获得最佳性能,使用不同的FFTW软件包可能要比从# yum install fftw-devel获得的软件包更好,但这是另一回事...)