cannam@167: @node Upgrading from FFTW version 2, Installation and Customization, Calling FFTW from Legacy Fortran, Top cannam@167: @chapter Upgrading from FFTW version 2 cannam@167: cannam@167: In this chapter, we outline the process for updating codes designed for cannam@167: the older FFTW 2 interface to work with FFTW 3. The interface for FFTW cannam@167: 3 is not backwards-compatible with the interface for FFTW 2 and earlier cannam@167: versions; codes written to use those versions will fail to link with cannam@167: FFTW 3. Nor is it possible to write ``compatibility wrappers'' to cannam@167: bridge the gap (at least not efficiently), because FFTW 3 has different cannam@167: semantics from previous versions. However, upgrading should be a cannam@167: straightforward process because the data formats are identical and the cannam@167: overall style of planning/execution is essentially the same. cannam@167: cannam@167: Unlike FFTW 2, there are no separate header files for real and complex cannam@167: transforms (or even for different precisions) in FFTW 3; all interfaces cannam@167: are defined in the @code{} header file. cannam@167: cannam@167: @heading Numeric Types cannam@167: cannam@167: The main difference in data types is that @code{fftw_complex} in FFTW 2 cannam@167: was defined as a @code{struct} with macros @code{c_re} and @code{c_im} cannam@167: for accessing the real/imaginary parts. (This is binary-compatible with cannam@167: FFTW 3 on any machine except perhaps for some older Crays in single cannam@167: precision.) The equivalent macros for FFTW 3 are: cannam@167: cannam@167: @example cannam@167: #define c_re(c) ((c)[0]) cannam@167: #define c_im(c) ((c)[1]) cannam@167: @end example cannam@167: cannam@167: This does not work if you are using the C99 complex type, however, cannam@167: unless you insert a @code{double*} typecast into the above macros cannam@167: (@pxref{Complex numbers}). cannam@167: cannam@167: Also, FFTW 2 had an @code{fftw_real} typedef that was an alias for cannam@167: @code{double} (in double precision). In FFTW 3 you should just use cannam@167: @code{double} (or whatever precision you are employing). cannam@167: cannam@167: @heading Plans cannam@167: cannam@167: The major difference between FFTW 2 and FFTW 3 is in the cannam@167: planning/execution division of labor. In FFTW 2, plans were found for a cannam@167: given transform size and type, and then could be applied to @emph{any} cannam@167: arrays and for @emph{any} multiplicity/stride parameters. In FFTW 3, cannam@167: you specify the particular arrays, stride parameters, etcetera when cannam@167: creating the plan, and the plan is then executed for @emph{those} arrays cannam@167: (unless the guru interface is used) and @emph{those} parameters cannam@167: @emph{only}. (FFTW 2 had ``specific planner'' routines that planned for cannam@167: a particular array and stride, but the plan could still be used for cannam@167: other arrays and strides.) That is, much of the information that was cannam@167: formerly specified at execution time is now specified at planning time. cannam@167: cannam@167: Like FFTW 2's specific planner routines, the FFTW 3 planner overwrites cannam@167: the input/output arrays unless you use @code{FFTW_ESTIMATE}. cannam@167: cannam@167: FFTW 2 had separate data types @code{fftw_plan}, @code{fftwnd_plan}, cannam@167: @code{rfftw_plan}, and @code{rfftwnd_plan} for complex and real one- and cannam@167: multi-dimensional transforms, and each type had its own @samp{destroy} cannam@167: function. In FFTW 3, all plans are of type @code{fftw_plan} and all are cannam@167: destroyed by @code{fftw_destroy_plan(plan)}. cannam@167: cannam@167: Where you formerly used @code{fftw_create_plan} and @code{fftw_one} to cannam@167: plan and compute a single 1d transform, you would now use cannam@167: @code{fftw_plan_dft_1d} to plan the transform. If you used the generic cannam@167: @code{fftw} function to execute the transform with multiplicity cannam@167: (@code{howmany}) and stride parameters, you would now use the advanced cannam@167: interface @code{fftw_plan_many_dft} to specify those parameters. The cannam@167: plans are now executed with @code{fftw_execute(plan)}, which takes all cannam@167: of its parameters (including the input/output arrays) from the plan. cannam@167: cannam@167: In-place transforms no longer interpret their output argument as scratch cannam@167: space, nor is there an @code{FFTW_IN_PLACE} flag. You simply pass the cannam@167: same pointer for both the input and output arguments. (Previously, the cannam@167: output @code{ostride} and @code{odist} parameters were ignored for cannam@167: in-place transforms; now, if they are specified via the advanced cannam@167: interface, they are significant even in the in-place case, although they cannam@167: should normally equal the corresponding input parameters.) cannam@167: cannam@167: The @code{FFTW_ESTIMATE} and @code{FFTW_MEASURE} flags have the same cannam@167: meaning as before, although the planning time will differ. You may also cannam@167: consider using @code{FFTW_PATIENT}, which is like @code{FFTW_MEASURE} cannam@167: except that it takes more time in order to consider a wider variety of cannam@167: algorithms. cannam@167: cannam@167: For multi-dimensional complex DFTs, instead of @code{fftwnd_create_plan} cannam@167: (or @code{fftw2d_create_plan} or @code{fftw3d_create_plan}), followed by cannam@167: @code{fftwnd_one}, you would use @code{fftw_plan_dft} (or cannam@167: @code{fftw_plan_dft_2d} or @code{fftw_plan_dft_3d}). followed by cannam@167: @code{fftw_execute}. If you used @code{fftwnd} to to specify strides cannam@167: etcetera, you would instead specify these via @code{fftw_plan_many_dft}. cannam@167: cannam@167: The analogues to @code{rfftw_create_plan} and @code{rfftw_one} with cannam@167: @code{FFTW_REAL_TO_COMPLEX} or @code{FFTW_COMPLEX_TO_REAL} directions cannam@167: are @code{fftw_plan_r2r_1d} with kind @code{FFTW_R2HC} or cannam@167: @code{FFTW_HC2R}, followed by @code{fftw_execute}. The stride etcetera cannam@167: arguments of @code{rfftw} are now in @code{fftw_plan_many_r2r}. cannam@167: cannam@167: Instead of @code{rfftwnd_create_plan} (or @code{rfftw2d_create_plan} or cannam@167: @code{rfftw3d_create_plan}) followed by cannam@167: @code{rfftwnd_one_real_to_complex} or cannam@167: @code{rfftwnd_one_complex_to_real}, you now use @code{fftw_plan_dft_r2c} cannam@167: (or @code{fftw_plan_dft_r2c_2d} or @code{fftw_plan_dft_r2c_3d}) or cannam@167: @code{fftw_plan_dft_c2r} (or @code{fftw_plan_dft_c2r_2d} or cannam@167: @code{fftw_plan_dft_c2r_3d}), respectively, followed by cannam@167: @code{fftw_execute}. As usual, the strides etcetera of cannam@167: @code{rfftwnd_real_to_complex} or @code{rfftwnd_complex_to_real} are no cannam@167: specified in the advanced planner routines, cannam@167: @code{fftw_plan_many_dft_r2c} or @code{fftw_plan_many_dft_c2r}. cannam@167: cannam@167: @heading Wisdom cannam@167: cannam@167: In FFTW 2, you had to supply the @code{FFTW_USE_WISDOM} flag in order to cannam@167: use wisdom; in FFTW 3, wisdom is always used. (You could simulate the cannam@167: FFTW 2 wisdom-less behavior by calling @code{fftw_forget_wisdom} after cannam@167: every planner call.) cannam@167: cannam@167: The FFTW 3 wisdom import/export routines are almost the same as before cannam@167: (although the storage format is entirely different). There is one cannam@167: significant difference, however. In FFTW 2, the import routines would cannam@167: never read past the end of the wisdom, so you could store extra data cannam@167: beyond the wisdom in the same file, for example. In FFTW 3, the cannam@167: file-import routine may read up to a few hundred bytes past the end of cannam@167: the wisdom, so you cannot store other data just beyond it.@footnote{We cannam@167: do our own buffering because GNU libc I/O routines are horribly slow for cannam@167: single-character I/O, apparently for thread-safety reasons (whether you cannam@167: are using threads or not).} cannam@167: cannam@167: Wisdom has been enhanced by additional humility in FFTW 3: whereas FFTW cannam@167: 2 would re-use wisdom for a given transform size regardless of the cannam@167: stride etc., in FFTW 3 wisdom is only used with the strides etc. for cannam@167: which it was created. Unfortunately, this means FFTW 3 has to create cannam@167: new plans from scratch more often than FFTW 2 (in FFTW 2, planning cannam@167: e.g. one transform of size 1024 also created wisdom for all smaller cannam@167: powers of 2, but this no longer occurs). cannam@167: cannam@167: FFTW 3 also has the new routine @code{fftw_import_system_wisdom} to cannam@167: import wisdom from a standard system-wide location. cannam@167: cannam@167: @heading Memory allocation cannam@167: cannam@167: In FFTW 3, we recommend allocating your arrays with @code{fftw_malloc} cannam@167: and deallocating them with @code{fftw_free}; this is not required, but cannam@167: allows optimal performance when SIMD acceleration is used. (Those two cannam@167: functions actually existed in FFTW 2, and worked the same way, but were cannam@167: not documented.) cannam@167: cannam@167: In FFTW 2, there were @code{fftw_malloc_hook} and @code{fftw_free_hook} cannam@167: functions that allowed the user to replace FFTW's memory-allocation cannam@167: routines (e.g. to implement different error-handling, since by default cannam@167: FFTW prints an error message and calls @code{exit} to abort the program cannam@167: if @code{malloc} returns @code{NULL}). These hooks are not supported in cannam@167: FFTW 3; those few users who require this functionality can just cannam@167: directly modify the memory-allocation routines in FFTW (they are defined cannam@167: in @code{kernel/alloc.c}). cannam@167: cannam@167: @heading Fortran interface cannam@167: cannam@167: In FFTW 2, the subroutine names were obtained by replacing @samp{fftw_} cannam@167: with @samp{fftw_f77}; in FFTW 3, you replace @samp{fftw_} with cannam@167: @samp{dfftw_} (or @samp{sfftw_} or @samp{lfftw_}, depending upon the cannam@167: precision). cannam@167: cannam@167: In FFTW 3, we have begun recommending that you always declare the type cannam@167: used to store plans as @code{integer*8}. (Too many people didn't notice cannam@167: our instruction to switch from @code{integer} to @code{integer*8} for cannam@167: 64-bit machines.) cannam@167: cannam@167: In FFTW 3, we provide a @code{fftw3.f} ``header file'' to include in cannam@167: your code (and which is officially installed on Unix systems). (In FFTW cannam@167: 2, we supplied a @code{fftw_f77.i} file, but it was not installed.) cannam@167: cannam@167: Otherwise, the C-Fortran interface relationship is much the same as it cannam@167: was before (e.g. return values become initial parameters, and cannam@167: multi-dimensional arrays are in column-major order). Unlike FFTW 2, we cannam@167: do provide some support for wisdom import/export in Fortran cannam@167: (@pxref{Wisdom of Fortran?}). cannam@167: cannam@167: @heading Threads cannam@167: cannam@167: Like FFTW 2, only the execution routines are thread-safe. All planner cannam@167: routines, etcetera, should be called by only a single thread at a time cannam@167: (@pxref{Thread safety}). @emph{Unlike} FFTW 2, there is no special cannam@167: @code{FFTW_THREADSAFE} flag for the planner to allow a given plan to be cannam@167: usable by multiple threads in parallel; this is now the case by default. cannam@167: cannam@167: The multi-threaded version of FFTW 2 required you to pass the number of cannam@167: threads each time you execute the transform. The number of threads is cannam@167: now stored in the plan, and is specified before the planner is called by cannam@167: @code{fftw_plan_with_nthreads}. The threads initialization routine used cannam@167: to be called @code{fftw_threads_init} and would return zero on success; cannam@167: the new routine is called @code{fftw_init_threads} and returns zero on cannam@167: failure. @xref{Multi-threaded FFTW}. cannam@167: cannam@167: There is no separate threads header file in FFTW 3; all the function cannam@167: prototypes are in @code{}. However, you still have to link to cannam@167: a separate library (@code{-lfftw3_threads -lfftw3 -lm} on Unix), as well as cannam@167: to the threading library (e.g. POSIX threads on Unix). cannam@167: