Search code examples
c++fftw

Segmentation fault using FFTW


I have a pretty involving program that uses an in house FFT algorithm. I recently decided to try using FFTW for a performance increase. Just as a simple test to ensure that FFTW would link and run, I added the following code to the beginning of the application, however, when I run, I get a segmentation fault when I create the fftwf_plan:

const size_t size = 1024;
vector<complex<float> > data(size);
for(size_t i = 0; i < size; ++i) data[i] = complex<float>(i, -i);

fftwf_plan plan =
    fftwf_plan_dft_1d(size,
                      (fftwf_complex*)&data[0],
                      (fftwf_complex*)&data[0],
                      FFTW_FORWARD,
                      FFTW_ESTIMATE);
// ^ seg faults here ^

fftwf_execute(plan);
fftwf_destroy_plan(plan);

Any ideas what would be causing this?

Using FFTW 3.3. Tried 2 different compilers, g++ 4.1.1 and icc 11.1. Also, the core file file shows nothing of significance:

Thread 1.1: Error at 0x00000000
Stack Trace: PC: 000000, FP=Hex Address

EDIT

I reconfigured FFTW to add debug, using the following commands:

setenv CFLAGS "-fPIC -g -O0"
configure --enabled-shared --enable-float --enable-debug
make
make install

When the program has a segmentation fault, it is in a random location in the fftwf_plan_dft_1d() method, however, the stack trace allways shows that is in or below the function search which is called by mkplan.


Solution

  • Aparently the issue stems from multi-threading. Even though the main functions are thread safe in FFTW (e.g. fftwf_execute), the functions to create a plan are not. This doesn't fully explain why just running a test on startup failed, however, when I excapsulated the plan creation in mutex locks, the segmentation faults ceased.