Users writing multi-threaded programs (including OpenMP) must concern themselves with the thread safety of the libraries they use—that is, whether it is safe to call routines in parallel from multiple threads. FFTW can be used in such an environment, but some care must be taken because the planner routines share data (e.g. wisdom and trigonometric tables) between calls and plans.
The upshot is that the only thread-safe (re-entrant) routine in FFTW is
fftw_execute (and the new-array variants thereof). All other routines
(e.g. the planner) should only be called from one thread at a time. So,
for example, you can wrap a semaphore lock around any calls to the
planner; even more simply, you can just create all of your plans from
one thread. We do not think this should be an important restriction
(FFTW is designed for the situation where the only performance-sensitive
code is the actual execution of the transform), and the benefits of
shared data between plans are great.
Note also that, since the plan is not modified by
it is safe to execute the same plan in parallel by multiple
threads. However, since a given plan operates by default on a fixed
array, you need to use one of the new-array execute functions (see New-array Execute Functions) so that different threads compute the transform of different data.
(Users should note that these comments only apply to programs using shared-memory threads or OpenMP. Parallelism using MPI or forked processes involves a separate address-space and global variables for each process, and is not susceptible to problems of this sort.)
If you are configured FFTW with the
--enable-debug-malloc flags (see Installation on Unix),
fftw_execute is not thread-safe. These flags are not
documented because they are intended only for developing
and debugging FFTW, but if you must use
--enable-debug then you
should also specifically pass
fftw_execute to be thread-safe.
Starting from FFTW-3.3.5, FFTW supports a new API to make the planner thread-safe:
This call installs a hook that wraps a lock around all planner calls.
This API is meant for “applications” that use “plugins” in multiple
threads, where each “plugin” calls single-threaded FFTW but is unaware
of the other “plugins” doing the same thing at the same time. In this
case, the “application” calls
at the beginning to protect “plugins” from each other.