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