FFTW / fftw3

DO NOT CHECK OUT THESE FILES FROM GITHUB UNLESS YOU KNOW WHAT YOU ARE DOING. (See below.)
GNU General Public License v2.0
2.66k stars 651 forks source link

Bug of fftw_plan_many_dft #327

Open Qianruipku opened 1 year ago

Qianruipku commented 1 year ago
#include "fftw3.h"
#include <stdlib.h>
#include <cmath>

int main()
{
int nrank = 24;
int nfft = 9;
int ngrid = nrank * nfft;

fftw_complex* aux1  = (fftw_complex *) fftw_malloc(sizeof(fftw_complex) * ngrid);
int *embed = nullptr;
fftw_plan planyfor  = fftw_plan_many_dft(1, &nrank, nfft,   
                                         aux1,  embed, nfft, 1,
                                         aux1,  embed, nfft, 1,
                                         FFTW_FORWARD, FFTW_MEASURE);
for(int i = 0 ; i < ngrid; ++i)
{
    double r = std::rand() / double(RAND_MAX);
    double arg = std::rand() / double(RAND_MAX);
    aux1[i][0] = r * cos(arg);
    aux1[i][1] = r * sin(arg);
}
fftw_execute(planyfor);
double sum = 0;
for(int i = 0 ; i < ngrid; ++i)
{
    sum += aux1[i][0] + aux1[i][0];
}
printf("%.20f\n", sum);

fftw_cleanup();
fftw_destroy_plan(planyfor);
fftw_free(aux1);
}

The code produces different results every time it is executed. When I use valgrind to test it, errors will be found:

==3216633== Invalid read of size 8 ==3216633== at 0x5AD9FF: awake (dftw-direct.c:129) ==3216633== by 0x4024B5: fftw_plan_awake (plan.c:66) ==3216633== by 0x5A9DC0: awake (ct.c:64) ==3216633== by 0x4024B5: fftw_plan_awake (plan.c:66) ==3216633== by 0x40C3E4: awake (vrank-geq1.c:70) ==3216633== by 0x4024B5: fftw_plan_awake (plan.c:66) ==3216633== by 0x5A91BA: awake (buffered.c:76) ==3216633== by 0x4024B5: fftw_plan_awake (plan.c:66) ==3216633== by 0x401979: fftw_destroy_plan (apiplan.c:185) ==3216633== by 0x401511: main (test.cpp:35) ==3216633== Address 0x4dcd730 is 48 bytes inside a block of size 72 free'd ==3216633== at 0x483F0C3: free (vg_replace_malloc.c:872) ==3216633== by 0x4023CE: fftw_kernel_free (kalloc.c:143) ==3216633== by 0x4022B9: fftw_ifree (alloc.c:40) ==3216633== by 0x405391: fftw_solver_destroy (solver.c:43) ==3216633== by 0x404954: fftw_planner_destroy (planner.c:959) ==3216633== by 0x402202: fftw_cleanup (the-planner.c:39) ==3216633== by 0x401505: main (test.cpp:34) ==3216633== Block was alloc'd at ==3216633== at 0x483C855: malloc (vg_replace_malloc.c:381) ==3216633== by 0x4023A8: fftw_kernel_malloc (kalloc.c:135) ==3216633== by 0x402274: fftw_malloc_plain (alloc.c:28) ==3216633== by 0x4052F5: fftw_mksolver (solver.c:26) ==3216633== by 0x5AA5F2: fftw_mksolver_ct (ct.c:239) ==3216633== by 0x5AE317: regone (dftw-direct.c:312) ==3216633== by 0x5AE3F9: fftw_regsolver_ct_directw (dftw-direct.c:330) ==3216633== by 0x5AA6D0: fftw_kdft_dit_register (kdft-dit.c:26) ==3216633== by 0x450963: fftw_codelet_t1_6 (t1_6.c:293) ==3216633== by 0x5A5C26: fftw_solvtab_exec (solvtab.c:29) ==3216633== by 0x4077B7: fftw_dft_conf_standard (conf.c:43) ==3216633== by 0x405DBE: fftw_configure_planner (configure.c:28)

When int nrank = 24 is changed to int nrank = 20, results won't change and no errors will occur.

Enviroment: g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0 Copyright (C) 2019 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.