Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"fftw_execute " case "Segmentation fault" #357

Open
NevilleAdvan opened this issue Jun 6, 2024 · 4 comments
Open

"fftw_execute " case "Segmentation fault" #357

NevilleAdvan opened this issue Jun 6, 2024 · 4 comments

Comments

@NevilleAdvan
Copy link

NevilleAdvan commented Jun 6, 2024

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
Copy link

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
Copy link
Author

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
Copy link
Contributor

stevengj commented Jun 17, 2024

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants