Thread safe FFTW


I am attempting to incorporate a layer of OpenMP code within an MPI program.
Unfortunately I seem to be falling foul of thread safety when attempting to perform FFTs within the OpenMP threads.

I am fairly convinced that thread safety is the issue as the code executes without issue if the call to dfftw_execute_dft is performed within an omp critical region but fails in parallel with a double free or corruption error.

As far as I can tell the code is structured in the manner advised by the fftw documentation: The plan is generated before the OpenMP region is created and dfftw_execute_dft is supposed to be a thread safe routine for executing the FFTs.

I have attempted to use the FFTW_UNALIGNED flag as specified in a post that describes a very similar problem on: with no joy.

Before I resort to changing the manner in which the workload is distributed I wondered of there may be anything I missed?

Thanks in advance,


Hi Karl,

I’m not sure. Using the sample code in the link you reference, if I move the create and destroy plan calls outside the OpenMP region, the code runs correctly. I did make the mistake of forgetting to move the “plan_r2c” out of the private clause which caused a segv, but was find once made shared. Could this be your issue as well?

  • Mat
% cat main.F 
program main
implicit none
#include ""
integer nx,ny
double precision val(nx,ny)
double precision tp(nx), tf(nx)
integer i,j,k
real*8 deltax,rlenx
real*8 pi,amp

integer*8 plan_r2c
integer tid,OMP_GET_THREAD_NUM

! write(*,*)'rlenx:',rlenx


do j=1,ny
do i=1,nx
call dfftw_plan_dft_r2c_1d(plan_r2c,nx,tp,tf,FFTW_MEASURE)


!omp parallel do private(tid,k,plan_r2c,tp,tf)
!$omp parallel do private(tid,k,tp,tf)
!omp parallel do default(private)
do k =1,ny
write(*,*) 'Thread ID = ', tid, ' Loop # = ', k

do i=1,nx

!call dfftw_plan_dft_r2c_1d(plan_r2c,nx,tp,tf,FFTW_MEASURE)
call dfftw_execute(plan_r2c)
!call dfftw_destroy_plan(plan_r2c)

write(*,*) 'plan_r2c = ', plan_r2c

call dfftw_destroy_plan(plan_r2c)

% pgf90 main.F -Mfree -L/local/home/colgrove/fftw/lib -lfftw3 -g -mp
% setenv OMP_NUM_THREADS 4
% a.out
 Thread ID =             0  Loop # =             1
 Thread ID =             2  Loop # =             5
 plan_r2c =                   8760496
 Thread ID =             0  Loop # =             2
 plan_r2c =                   8760496
 Thread ID =             2  Loop # =             6
 Thread ID =             3  Loop # =             7
 plan_r2c =                   8760496
 plan_r2c =                   8760496
 plan_r2c =                   8760496
 Thread ID =             3  Loop # =             8
 plan_r2c =                   8760496
 Thread ID =             1  Loop # =             3
 plan_r2c =                   8760496
 Thread ID =             1  Loop # =             4
 plan_r2c =                   8760496