cannam@167: cannam@167: cannam@167: cannam@167: cannam@167:
cannam@167:cannam@167: Next: How Many Threads to Use?, Previous: Installation and Supported Hardware/Software, Up: Multi-threaded FFTW [Contents][Index]
cannam@167:Here, it is assumed that the reader is already familiar with the usage cannam@167: of the uniprocessor FFTW routines, described elsewhere in this manual. cannam@167: We only describe what one has to change in order to use the cannam@167: multi-threaded routines. cannam@167:
cannam@167: cannam@167:First, programs using the parallel complex transforms should be linked
cannam@167: with -lfftw3_threads -lfftw3 -lm
on Unix, or -lfftw3_omp
cannam@167: -lfftw3 -lm
if you compiled with OpenMP. You will also need to link
cannam@167: with whatever library is responsible for threads on your system
cannam@167: (e.g. -lpthread
on GNU/Linux) or include whatever compiler flag
cannam@167: enables OpenMP (e.g. -fopenmp
with gcc).
cannam@167:
cannam@167:
Second, before calling any FFTW routines, you should call the cannam@167: function: cannam@167:
cannam@167:int fftw_init_threads(void); cannam@167:
This function, which need only be called once, performs any one-time cannam@167: initialization required to use threads on your system. It returns zero cannam@167: if there was some error (which should not happen under normal cannam@167: circumstances) and a non-zero value otherwise. cannam@167:
cannam@167:Third, before creating a plan that you want to parallelize, you should cannam@167: call: cannam@167:
cannam@167:void fftw_plan_with_nthreads(int nthreads); cannam@167:
The nthreads
argument indicates the number of threads you want
cannam@167: FFTW to use (or actually, the maximum number). All plans subsequently
cannam@167: created with any planner routine will use that many threads. You can
cannam@167: call fftw_plan_with_nthreads
, create some plans, call
cannam@167: fftw_plan_with_nthreads
again with a different argument, and
cannam@167: create some more plans for a new number of threads. Plans already created
cannam@167: before a call to fftw_plan_with_nthreads
are unaffected. If you
cannam@167: pass an nthreads
argument of 1
(the default), threads are
cannam@167: disabled for subsequent plans.
cannam@167:
With OpenMP, to configure FFTW to use all of the currently running
cannam@167: OpenMP threads (set by omp_set_num_threads(nthreads)
or by the
cannam@167: OMP_NUM_THREADS
environment variable), you can do:
cannam@167: fftw_plan_with_nthreads(omp_get_max_threads())
. (The ‘omp_’
cannam@167: OpenMP functions are declared via #include <omp.h>
.)
cannam@167:
Given a plan, you then execute it as usual with
cannam@167: fftw_execute(plan)
, and the execution will use the number of
cannam@167: threads specified when the plan was created. When done, you destroy
cannam@167: it as usual with fftw_destroy_plan
. As described in
cannam@167: Thread safety, plan execution is thread-safe, but plan
cannam@167: creation and destruction are not: you should create/destroy
cannam@167: plans only from a single thread, but can safely execute multiple plans
cannam@167: in parallel.
cannam@167:
There is one additional routine: if you want to get rid of all memory cannam@167: and other resources allocated internally by FFTW, you can call: cannam@167:
cannam@167:void fftw_cleanup_threads(void); cannam@167:
which is much like the fftw_cleanup()
function except that it
cannam@167: also gets rid of threads-related data. You must not execute any
cannam@167: previously created plans after calling this function.
cannam@167:
We should also mention one other restriction: if you save wisdom from a
cannam@167: program using the multi-threaded FFTW, that wisdom cannot be used
cannam@167: by a program using only the single-threaded FFTW (i.e. not calling
cannam@167: fftw_init_threads
). See Words of Wisdom-Saving Plans.
cannam@167:
cannam@167: Next: How Many Threads to Use?, Previous: Installation and Supported Hardware/Software, Up: Multi-threaded FFTW [Contents][Index]
cannam@167: