Multi-threaded FFTW + Cython + MinGW

449 views
Skip to first unread message

sebastie...@gmail.com

unread,
Sep 19, 2013, 10:17:04 AM9/19/13
to cython...@googlegroups.com
Hello,
I would like to report about a crash that occurs under Windows 7 (Python 3.3 + MinGW), when trying to call the multi-threaded version of FFTW from within Cython. I should mention that FFTW was compiled with the --enable-threads --with-combined-threads options. The example below works smoothly in my linux virtual machine. Under windows, it compiles without any problem, but crashes at runtime. I've narrowed the crash down to the call to fftw_execute. I've temporarily switched to a FFTW library which was compiled without the --enable-threads, and the example works under windows. Finally, I wrote the same example in pure C, and it works under Windows.

To sum up, it seems like there is a problem with the threaded version of FFTW when called from Cython under Windows 7. I suspect it has something to do with Python not liking the threads (GIL?). I'm not an expert on that topic, but what is strange is the fact that my Cython code works like a charm under linux. Also, I do not think there is a problem with my MinGW compiler, as the pure C (multi-threaded) example also works under linux.

Any ideas ?

Thanks a lot !

Sébastien

Here is the demo_bug_fftw_cython.pyx file

__________________________________________________________________
cdef extern from "fftw3.h":
    int fftw_init_threads()
    void fftw_plan_with_nthreads(int)
   
    cdef int FFTW_FORWARD
    cdef unsigned FFTW_ESTIMATE     

    ctypedef double fftw_complex[2]

    void *fftw_malloc(size_t)
    void fftw_free(void *)

    ctypedef struct _fftw_plan:
       pass

    ctypedef _fftw_plan *fftw_plan

    void fftw_execute(fftw_plan)
    void fftw_destroy_plan(fftw_plan)
    fftw_plan fftw_plan_dft_1d(int, fftw_complex*, fftw_complex*, int, unsigned)
    void fftw_print_plan(fftw_plan)

def fft(double[:] input, double[:] output):
    fftw_init_threads()
    fftw_plan_with_nthreads(4)
    cdef int n = input.shape[0]
    cdef double *tmp = <double *> fftw_malloc(n * sizeof(double))
    cdef int i
    for i in range(n):
        tmp[i] = input[i]
    cdef fftw_plan plan =  fftw_plan_dft_1d(n / 2,
                                           <fftw_complex *>tmp,
                                           <fftw_complex *>tmp,
                                           FFTW_FORWARD,
                                           FFTW_ESTIMATE)
    fftw_print_plan(plan)
    fftw_execute(plan)
    for i in range(n):
        output[i] = tmp[i]
    fftw_destroy_plan(plan)
    fftw_free(tmp)
__________________________________________________________________

Compilation into a module is done through the following setup.py file

____________________________________________________
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("demo_bug_fftw_cython",
                         ["demo_bug_fftw_cython.pyx"],
                         libraries=["fftw3"],
                         extra_compile_args=['-march=native'])]

setup(
  name = 'FFTW + Cython: example which fails',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)
__________________________________________________

This file is called from the following python code

__________________________________
import numpy as np
from demo_bug_fftw_cython import *

if __name__ == '__main__':
    n = 65536
    a = np.zeros((n,), dtype = np.float64)
    a[0] = 1.
    b = np.zeros_like(a)
    fft(a, b)
    print(a)
    print(b)
__________________________________

Henry Gomersall

unread,
Sep 19, 2013, 1:03:50 PM9/19/13
to cython...@googlegroups.com, sebastie...@gmail.com
On 2013-09-19 15:17, sebastie...@gmail.com wrote:
> Hello,
> I would like to report about a crash that occurs under Windows 7
> (Python 3.3 + MinGW), when trying to call the multi-threaded version
> of FFTW from within Cython. I should mention that FFTW was compiled
> with the --enable-threads --with-combined-threads options. The example
> below works smoothly in my linux virtual machine. Under windows, it
> compiles without any problem, but crashes at runtime. I've narrowed
> the crash down to the call to fftw_execute. I've temporarily switched
> to a FFTW library which was compiled without the --enable-threads, and
> the example works under windows. Finally, I wrote the same example in
> pure C, and it works under Windows.
>
> To sum up, it seems like there is a problem with the threaded version
> of FFTW when called from Cython under Windows 7. I suspect it has
> something to do with Python not liking the threads (GIL?). I'm not an
> expert on that topic, but what is strange is the fact that my Cython
> code works like a charm under linux. Also, I do not think there is a
> problem with my MinGW compiler, as the pure C (multi-threaded) example
> also works under linux.
>
> Any ideas ?
>

How old is the version of FFTW you are using?

There was a release a 18 months ago (some version of 3.3.1) that had a
misaligned stack pointer (or something - forget exactly what) that
manifested itself on the multi-threaded library. The effect was to
segfault pretty reliably :) (I think it was a misaligned SSE load)

Since then, I've been using FFTW multi-threaded with no notable
problems.

Cheers,

Henry

sebastie...@gmail.com

unread,
Sep 19, 2013, 1:46:32 PM9/19/13
to cython...@googlegroups.com, sebastie...@gmail.com
Hi,

thanks for your answer.



How old is the version of FFTW you are using?

There was a release a 18 months ago (some version of 3.3.1) that had a
misaligned stack pointer (or something - forget exactly what) that
manifested itself on the multi-threaded library. The effect was to
segfault pretty reliably :) (I think it was a misaligned SSE load)

I have installed FFTW v 3.3.3 (the latest). I don't think the problem is with FFTW, since when I call the threaded version of FFTW from pure C code, I don't get this nasty crash. Furthermore, under linux, everything (Cython or pure C) works fine. My guess would be that it has something to do with Python + threads, I do not know.

Since then, I've been using FFTW multi-threaded with no notable
problems.

Are you using FFTW under Windows 7 + MinGW? Are you calling FFTW (multi-threaded) from within Python, through Cython?

Thanks again,
Sébastien
Cheers,

Henry

Henry Gomersall

unread,
Sep 20, 2013, 3:10:38 AM9/20/13
to cython...@googlegroups.com, sebastie...@gmail.com
On 2013-09-19 18:46, sebastie...@gmail.com wrote:
> Hi,
>
> thanks for your answer.
>
>> How old is the version of FFTW you are using?
>>
>> There was a release a 18 months ago (some version of 3.3.1) that had
>> a
>> misaligned stack pointer (or something - forget exactly what) that
>> manifested itself on the multi-threaded library. The effect was to
>> segfault pretty reliably :) (I think it was a misaligned SSE load)
>
> I have installed FFTW v 3.3.3 (the latest). I don't think the problem
> is with FFTW, since when I call the threaded version of FFTW from pure
> C code, I don't get this nasty crash. Furthermore, under linux,
> everything (Cython or pure C) works fine. My guess would be that it
> has something to do with Python + threads, I do not know.
>

Interesting. It was a very specific version that had this bug, I was
just checking it wasn't that rearing it's head.

>> Since then, I've been using FFTW multi-threaded with no notable
>> problems.
>
> Are you using FFTW under Windows 7 + MinGW? Are you calling FFTW
> (multi-threaded) from within Python, through Cython?
>

Yes, that is exactly what I am doing. See
https://github.com/hgomersall/pyFFTW and the builds on PyPI.

Cheers,

Henry

Sébastien Brisard

unread,
Sep 20, 2013, 3:14:31 AM9/20/13
to Henry Gomersall, cython...@googlegroups.com
Thanks Henry,
I'll check out your project and see if I get the same kind of crashes (in which case it might have something to do with my build of FFTW).

NOTE : I built FFTW myself.

S


2013/9/20 Henry Gomersall <he...@cantab.net>
Reply all
Reply to author
Forward
0 new messages