Node:Usage of Multi-threaded FFTW, Next:How Many Threads to Use?, Previous:Installation and Supported Hardware/Software, Up:Multi-threaded FFTW

#### 5.1.2 Usage of Multi-threaded FFTW

Here, it is assumed that the reader is already familiar with the usage of the uniprocessor FFTW routines, described elsewhere in this manual. We only describe what one has to change in order to use the multi-threaded routines.

First, programs using the parallel complex transforms should be linked with `-lfftw3_threads -lfftw3 -lm` on Unix. You will also need to link with whatever library is responsible for threads on your system (e.g. `-lpthread` on GNU/Linux).

Second, before calling any FFTW routines, you should call the function:

    int fftw_init_threads(void);

This function, which need only be called once, performs any one-time initialization required to use threads on your system. It returns zero if there was some error (which should not happen under normal circumstances) and a non-zero value otherwise.

Third, before creating a plan that you want to parallelize, you should call:

    void fftw_plan_with_nthreads(int nthreads);

The `nthreads` argument indicates the number of threads you want FFTW to use (or actually, the maximum number). All plans subsequently created with any planner routine will use that many threads. You can call `fftw_plan_with_nthreads`, create some plans, call `fftw_plan_with_nthreads` again with a different argument, and create some more plans for a new number threads. Plans already created before a call to `fftw_plan_with_nthreads` are unaffected. If you pass an `nthreads` argument of `1` (the default), threads are disabled for subsequent plans.

Given a plan, you then execute it as usual with `fftw_execute(plan)`, and the execution will use the number of threads specified when the plan was created. When done, you destroy it as usual with `fftw_destroy_plan`.

There is one additional routine: if you want to get rid of all memory and other resources allocated internally by FFTW, you can call:

    void fftw_cleanup_threads(void);

which is much like the `fftw_cleanup()` function except that it also gets rid of threads-related data. You must not execute any previously created plans after calling this function.

We should also mention one other restriction: if you save wisdom from a program using the multi-threaded FFTW, that wisdom cannot be used by a program using only the single-threaded FFTW (i.e. not calling `fftw_init_threads`). See Words of Wisdom-Saving Plans.