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

"fftw_execute " case "Segmentation fault" #357

Open NevilleAdvan opened 1 month ago

NevilleAdvan commented 1 month ago

I wrote a test demo, and I noticed that many test demos online are tested in this way. But every time I run fftw_plan_dft-r2c_1d and then call fftw_execute, there will be a Segmentation fault. Is there an issue with incorrect compilation. In theory, there is no problem with the code. Here is my code:

#include "fftw3.h"
#include<stdio.h>
#include<iostream>
#include<vector>

using namespace std;
int main()
{
    //****************************ifft********************************
    double array[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    double* out;
    double* err;
    int i, size = 10;

    fftw_complex* out_cpx;

    fftw_plan fft;
    fftw_plan ifft;
    out_cpx = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * size);
    out = (double*)malloc(size * sizeof(double));
    err = (double*)malloc(size * sizeof(double));

    fft = fftw_plan_dft_r2c_1d(size, array, out_cpx, FFTW_ESTIMATE);  //Setup fftw plan for fft
    ifft = fftw_plan_dft_c2r_1d(size, out_cpx, out, FFTW_ESTIMATE);   //Setup fftw plan for ifft

    fftw_execute(fft);
    fftw_execute(ifft);

    for (i = 0; i < size; i++)
    {
        err[i] = (array[i] - out[i]);
        printf("%f\t%f\n", (array[i]), out[i] / size);
    }

    fftw_destroy_plan(fft);
    fftw_destroy_plan(ifft);
    fftw_free(out_cpx);
    free(err);
    free(out);
    //***************************************ifft*********************

    system("pause");
    return 0;
}
hominhquan commented 1 month ago

Hi, you may want to try fftw_malloc() (or attribute __aligned__ if static) for all input and output buffers (array, out_cpx and out). Internal FFTW kernels requires data to be aligned in a certain condition, specially for x86.

NevilleAdvan commented 3 weeks ago

Hi, you may want to try fftw_malloc() (or attribute __aligned__ if static) for all input and output buffers (array, out_cpx and out). Internal FFTW kernels requires data to be aligned in a certain condition, specially for x86.

Thanks for your answer. How can I fix this problem? Or provide some solutions? Because the parameters that the fftw_plan_dft_r2c_1d function needs to pass in require the data type of fftw_complex and the data type of double. However, according to this seemingly problem-free code, it will cause "Segmentation fault"

stevengj commented 3 weeks ago

Internal FFTW kernels requires data to be aligned in a certain condition, specially for x86.

FFTW does not require all data to be specially aligned. It is faster for specially aligned data (which is provided by fftw_malloc), but does not require it. (Unless you calll the new-array execute functions, which aren't being used here.)

(IIRC, x86 itself requires double arrays to be 8-byte aligned, but this should be enforced by the compiler.)

stevengj commented 3 weeks ago

I can't reproduce your problem, the program runs fine for me and outputs:

0.000000    0.000000
1.000000    1.000000
2.000000    2.000000
3.000000    3.000000
4.000000    4.000000
5.000000    5.000000
6.000000    6.000000
7.000000    7.000000
8.000000    8.000000
9.000000    9.000000

How are you compiling FFTW? Maybe there is some kind of architecture mismatch.