Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: FFTW 3.3.8: Thread safety Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: Chris@82:
Chris@82:

Chris@82: Previous: , Up: Multi-threaded FFTW   [Contents][Index]

Chris@82:
Chris@82:
Chris@82: Chris@82:

5.4 Thread safety

Chris@82: Chris@82: Chris@82: Chris@82: Chris@82:

Users writing multi-threaded programs (including OpenMP) must concern Chris@82: themselves with the thread safety of the libraries they Chris@82: use—that is, whether it is safe to call routines in parallel from Chris@82: multiple threads. FFTW can be used in such an environment, but some Chris@82: care must be taken because the planner routines share data Chris@82: (e.g. wisdom and trigonometric tables) between calls and plans. Chris@82:

Chris@82:

The upshot is that the only thread-safe routine in FFTW is Chris@82: fftw_execute (and the new-array variants thereof). All other routines Chris@82: (e.g. the planner) should only be called from one thread at a time. So, Chris@82: for example, you can wrap a semaphore lock around any calls to the Chris@82: planner; even more simply, you can just create all of your plans from Chris@82: one thread. We do not think this should be an important restriction Chris@82: (FFTW is designed for the situation where the only performance-sensitive Chris@82: code is the actual execution of the transform), and the benefits of Chris@82: shared data between plans are great. Chris@82:

Chris@82:

Note also that, since the plan is not modified by fftw_execute, Chris@82: it is safe to execute the same plan in parallel by multiple Chris@82: threads. However, since a given plan operates by default on a fixed Chris@82: 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. Chris@82:

Chris@82:

(Users should note that these comments only apply to programs using Chris@82: shared-memory threads or OpenMP. Parallelism using MPI or forked processes Chris@82: involves a separate address-space and global variables for each process, Chris@82: and is not susceptible to problems of this sort.) Chris@82:

Chris@82:

The FFTW planner is intended to be called from a single thread. If you Chris@82: really must call it from multiple threads, you are expected to grab Chris@82: whatever lock makes sense for your application, with the understanding Chris@82: that you may be holding that lock for a long time, which is undesirable. Chris@82:

Chris@82:

Neither strategy works, however, in the following situation. The Chris@82: “application” is structured as a set of “plugins” which are unaware Chris@82: of each other, and for whatever reason the “plugins” cannot coordinate Chris@82: on grabbing the lock. (This is not a technical problem, but an Chris@82: organizational one. The “plugins” are written by independent agents, Chris@82: and from the perspective of each plugin’s author, each plugin is using Chris@82: FFTW correctly from a single thread.) To cope with this situation, Chris@82: starting from FFTW-3.3.5, FFTW supports an API to make the planner Chris@82: thread-safe: Chris@82:

Chris@82:
Chris@82:
void fftw_make_planner_thread_safe(void);
Chris@82: 
Chris@82: Chris@82: Chris@82:

This call operates by brute force: It just installs a hook that wraps a Chris@82: lock (chosen by us) around all planner calls. So there is no magic and Chris@82: you get the worst of all worlds. The planner is still single-threaded, Chris@82: but you cannot choose which lock to use. The planner still holds the Chris@82: lock for a long time, but you cannot impose a timeout on lock Chris@82: acquisition. As of FFTW-3.3.5 and FFTW-3.3.6, this call does not work Chris@82: when using OpenMP as threading substrate. (Suggestions on what to do Chris@82: about this bug are welcome.) Do not use Chris@82: fftw_make_planner_thread_safe unless there is no other choice, Chris@82: such as in the application/plugin situation. Chris@82:


Chris@82:
Chris@82:

Chris@82: Previous: , Up: Multi-threaded FFTW   [Contents][Index]

Chris@82:
Chris@82: Chris@82: Chris@82: Chris@82: Chris@82: