I’m encountering a problem, which I am not being able to solve despite trying several options. The problem goes like this.
I have to carry a FFT forward transformation for a Poisson solver, which I’m doing with the fftw3
routines. I’m using FORTRAN90
in a Mac with gfortran
. The fft
routine goes as follows:
subroutine fft_forward(f,fout)
use,intrinsic :: iso_c_binding
implicit none
include 'fftw3.f03'
double precision,dimension(:) :: f,fout
integer :: J,N,k,nn
double complex,allocatable :: ffout(:)
double precision,allocatable :: ff(:)
integer(8) :: p
! Find J. Declare local arrays.
J = size(f)-1
N = 2*J
allocate(ff(N),ffout(N))
! Load and extend data
ff(1) = 0.0
ff(J+1) = 0.0
ff(2:J) = f(2:J)
do k = 2,J
ff(2*(J+1)-k) = -f(k)
enddo
call dfftw_plan_dft_r2c_1d(p,N,ff,ffout,FFTW_ESTIMATE)
call dfftw_execute_dft_r2c(p,ff,ffout)
call dfftw_destroy_plan (p)
! Unload data
fout(1) = 0.0; fout(J+1) = 0.0
fout(2:J) = - 2.0*ffout(2:J)%im
! Normalize data
fout = fout/(2.0*J)
end subroutine fft_forward
I also have a sample driver program, which goes like this:
program driver
implicit none
double precision,dimension(:),allocatable :: inarray,outarray
integer :: J,N
interface
subroutine fft_forward(f,fout)
use,intrinsic :: iso_c_binding
implicit none
include 'fftw3.f03'
double precision :: f(:),fout(:)
end subroutine fft_forward
end interface
! Declare local arrays.
J = 100
allocate(inarray(J),outarray(J))
! Here goes the assignment of inarray
call random_number(inarray)
call fft_forward(inarray,outarray)
print*,outarray
end program driver
As one can check, the driver
compiled standalone with the fft
program produces the right results. However, when I incoporate the same fft
routine with my larger program, it indicates a
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
I have not beein able to detect the error at all! All arrays are declared in a compatibel way. The compilation command line is
gfortran -I/opt/local/include -L/opt/local/lib -lfftw3 -lm
The fftw3
package is instlled through configure-make
system and installed in the dir /opt/local
. My question is whether it is speciifc to a Mac machine or something drastically wrong with my code.
Madhurjya