Chris@82: @node Calling FFTW from Legacy Fortran, Upgrading from FFTW version 2, Calling FFTW from Modern Fortran, Top Chris@82: @chapter Calling FFTW from Legacy Fortran Chris@82: @cindex Fortran interface Chris@82: Chris@82: This chapter describes the interface to FFTW callable by Fortran code Chris@82: in older compilers not supporting the Fortran 2003 C interoperability Chris@82: features (@pxref{Calling FFTW from Modern Fortran}). This interface Chris@82: has the major disadvantage that it is not type-checked, so if you Chris@82: mistake the argument types or ordering then your program will not have Chris@82: any compiler errors, and will likely crash at runtime. So, greater Chris@82: care is needed. Also, technically interfacing older Fortran versions Chris@82: to C is nonstandard, but in practice we have found that the techniques Chris@82: used in this chapter have worked with all known Fortran compilers for Chris@82: many years. Chris@82: Chris@82: The legacy Fortran interface differs from the C interface only in the Chris@82: prefix (@samp{dfftw_} instead of @samp{fftw_} in double precision) and Chris@82: a few other minor details. This Fortran interface is included in the Chris@82: FFTW libraries by default, unless a Fortran compiler isn't found on Chris@82: your system or @code{--disable-fortran} is included in the Chris@82: @code{configure} flags. We assume here that the reader is already Chris@82: familiar with the usage of FFTW in C, as described elsewhere in this Chris@82: manual. Chris@82: Chris@82: The MPI parallel interface to FFTW is @emph{not} currently available Chris@82: to legacy Fortran. Chris@82: Chris@82: @menu Chris@82: * Fortran-interface routines:: Chris@82: * FFTW Constants in Fortran:: Chris@82: * FFTW Execution in Fortran:: Chris@82: * Fortran Examples:: Chris@82: * Wisdom of Fortran?:: Chris@82: @end menu Chris@82: Chris@82: @c ------------------------------------------------------- Chris@82: @node Fortran-interface routines, FFTW Constants in Fortran, Calling FFTW from Legacy Fortran, Calling FFTW from Legacy Fortran Chris@82: @section Fortran-interface routines Chris@82: Chris@82: Nearly all of the FFTW functions have Fortran-callable equivalents. Chris@82: The name of the legacy Fortran routine is the same as that of the Chris@82: corresponding C routine, but with the @samp{fftw_} prefix replaced by Chris@82: @samp{dfftw_}.@footnote{Technically, Fortran 77 identifiers are not Chris@82: allowed to have more than 6 characters, nor may they contain Chris@82: underscores. Any compiler that enforces this limitation doesn't Chris@82: deserve to link to FFTW.} The single and long-double precision Chris@82: versions use @samp{sfftw_} and @samp{lfftw_}, respectively, instead of Chris@82: @samp{fftwf_} and @samp{fftwl_}; quadruple precision (@code{real*16}) Chris@82: is available on some systems as @samp{fftwq_} (@pxref{Precision}). Chris@82: (Note that @code{long double} on x86 hardware is usually at most Chris@82: 80-bit extended precision, @emph{not} quadruple precision.) Chris@82: Chris@82: For the most part, all of the arguments to the functions are the same, Chris@82: with the following exceptions: Chris@82: Chris@82: @itemize @bullet Chris@82: Chris@82: @item Chris@82: @code{plan} variables (what would be of type @code{fftw_plan} in C), Chris@82: must be declared as a type that is at least as big as a pointer Chris@82: (address) on your machine. We recommend using @code{integer*8} everywhere, Chris@82: since this should always be big enough. Chris@82: @cindex portability Chris@82: Chris@82: @item Chris@82: Any function that returns a value (e.g. @code{fftw_plan_dft}) is Chris@82: converted into a @emph{subroutine}. The return value is converted into Chris@82: an additional @emph{first} parameter of this subroutine.@footnote{The Chris@82: reason for this is that some Fortran implementations seem to have Chris@82: trouble with C function return values, and vice versa.} Chris@82: Chris@82: @item Chris@82: @cindex column-major Chris@82: The Fortran routines expect multi-dimensional arrays to be in Chris@82: @emph{column-major} order, which is the ordinary format of Fortran Chris@82: arrays (@pxref{Multi-dimensional Array Format}). They do this Chris@82: transparently and costlessly simply by reversing the order of the Chris@82: dimensions passed to FFTW, but this has one important consequence for Chris@82: multi-dimensional real-complex transforms, discussed below. Chris@82: Chris@82: @item Chris@82: Wisdom import and export is somewhat more tricky because one cannot Chris@82: easily pass files or strings between C and Fortran; see @ref{Wisdom of Chris@82: Fortran?}. Chris@82: Chris@82: @item Chris@82: Legacy Fortran cannot use the @code{fftw_malloc} dynamic-allocation routine. Chris@82: If you want to exploit the SIMD FFTW (@pxref{SIMD alignment and fftw_malloc}), you'll Chris@82: need to figure out some other way to ensure that your arrays are at Chris@82: least 16-byte aligned. Chris@82: Chris@82: @item Chris@82: @tindex fftw_iodim Chris@82: @cindex guru interface Chris@82: Since Fortran 77 does not have data structures, the @code{fftw_iodim} Chris@82: structure from the guru interface (@pxref{Guru vector and transform Chris@82: sizes}) must be split into separate arguments. In particular, any Chris@82: @code{fftw_iodim} array arguments in the C guru interface become three Chris@82: integer array arguments (@code{n}, @code{is}, and @code{os}) in the Chris@82: Fortran guru interface, all of whose lengths should be equal to the Chris@82: corresponding @code{rank} argument. Chris@82: Chris@82: @item Chris@82: The guru planner interface in Fortran does @emph{not} do any automatic Chris@82: translation between column-major and row-major; you are responsible Chris@82: for setting the strides etcetera to correspond to your Fortran arrays. Chris@82: However, as a slight bug that we are preserving for backwards Chris@82: compatibility, the @samp{plan_guru_r2r} in Fortran @emph{does} reverse the Chris@82: order of its @code{kind} array parameter, so the @code{kind} array Chris@82: of that routine should be in the reverse of the order of the iodim Chris@82: arrays (see above). Chris@82: Chris@82: @end itemize Chris@82: Chris@82: In general, you should take care to use Fortran data types that Chris@82: correspond to (i.e. are the same size as) the C types used by FFTW. Chris@82: In practice, this correspondence is usually straightforward Chris@82: (i.e. @code{integer} corresponds to @code{int}, @code{real} Chris@82: corresponds to @code{float}, etcetera). The native Fortran Chris@82: double/single-precision complex type should be compatible with Chris@82: @code{fftw_complex}/@code{fftwf_complex}. Such simple correspondences Chris@82: are assumed in the examples below. Chris@82: @cindex portability Chris@82: Chris@82: @c ------------------------------------------------------- Chris@82: @node FFTW Constants in Fortran, FFTW Execution in Fortran, Fortran-interface routines, Calling FFTW from Legacy Fortran Chris@82: @section FFTW Constants in Fortran Chris@82: Chris@82: When creating plans in FFTW, a number of constants are used to specify Chris@82: options, such as @code{FFTW_MEASURE} or @code{FFTW_ESTIMATE}. The Chris@82: same constants must be used with the wrapper routines, but of course the Chris@82: C header files where the constants are defined can't be incorporated Chris@82: directly into Fortran code. Chris@82: Chris@82: Instead, we have placed Fortran equivalents of the FFTW constant Chris@82: definitions in the file @code{fftw3.f}, which can be found in the same Chris@82: directory as @code{fftw3.h}. If your Fortran compiler supports a Chris@82: preprocessor of some sort, you should be able to @code{include} or Chris@82: @code{#include} this file; otherwise, you can paste it directly into Chris@82: your code. Chris@82: Chris@82: @cindex flags Chris@82: In C, you combine different flags (like @code{FFTW_PRESERVE_INPUT} and Chris@82: @code{FFTW_MEASURE}) using the @samp{@code{|}} operator; in Fortran Chris@82: you should just use @samp{@code{+}}. (Take care not to add in the Chris@82: same flag more than once, though. Alternatively, you can use the Chris@82: @code{ior} intrinsic function standardized in Fortran 95.) Chris@82: Chris@82: @c ------------------------------------------------------- Chris@82: @node FFTW Execution in Fortran, Fortran Examples, FFTW Constants in Fortran, Calling FFTW from Legacy Fortran Chris@82: @section FFTW Execution in Fortran Chris@82: Chris@82: In C, in order to use a plan, one normally calls @code{fftw_execute}, Chris@82: which executes the plan to perform the transform on the input/output Chris@82: arrays passed when the plan was created (@pxref{Using Plans}). The Chris@82: corresponding subroutine call in legacy Fortran is: Chris@82: @example Chris@82: call dfftw_execute(plan) Chris@82: @end example Chris@82: @findex dfftw_execute Chris@82: Chris@82: However, we have had reports that this causes problems with some Chris@82: recent optimizing Fortran compilers. The problem is, because the Chris@82: input/output arrays are not passed as explicit arguments to Chris@82: @code{dfftw_execute}, the semantics of Fortran (unlike C) allow the Chris@82: compiler to assume that the input/output arrays are not changed by Chris@82: @code{dfftw_execute}. As a consequence, certain compilers end up Chris@82: optimizing out or repositioning the call to @code{dfftw_execute}, Chris@82: assuming incorrectly that it does nothing. Chris@82: Chris@82: There are various workarounds to this, but the safest and simplest Chris@82: thing is to not use @code{dfftw_execute} in Fortran. Instead, use the Chris@82: functions described in @ref{New-array Execute Functions}, which take Chris@82: the input/output arrays as explicit arguments. For example, if the Chris@82: plan is for a complex-data DFT and was created for the arrays Chris@82: @code{in} and @code{out}, you would do: Chris@82: @example Chris@82: call dfftw_execute_dft(plan, in, out) Chris@82: @end example Chris@82: @findex dfftw_execute_dft Chris@82: Chris@82: There are a few things to be careful of, however: Chris@82: Chris@82: @itemize @bullet Chris@82: Chris@82: @item Chris@82: You must use the correct type of execute function, matching the way Chris@82: the plan was created. Complex DFT plans should use Chris@82: @code{dfftw_execute_dft}, Real-input (r2c) DFT plans should use use Chris@82: @code{dfftw_execute_dft_r2c}, and real-output (c2r) DFT plans should Chris@82: use @code{dfftw_execute_dft_c2r}. The various r2r plans should use Chris@82: @code{dfftw_execute_r2r}. Chris@82: Chris@82: @item Chris@82: You should normally pass the same input/output arrays that were used when Chris@82: creating the plan. This is always safe. Chris@82: Chris@82: @item Chris@82: @emph{If} you pass @emph{different} input/output arrays compared to Chris@82: those used when creating the plan, you must abide by all the Chris@82: restrictions of the new-array execute functions (@pxref{New-array Chris@82: Execute Functions}). The most difficult of these, in Fortran, is the Chris@82: requirement that the new arrays have the same alignment as the Chris@82: original arrays, because there seems to be no way in legacy Fortran to obtain Chris@82: guaranteed-aligned arrays (analogous to @code{fftw_malloc} in C). You Chris@82: can, of course, use the @code{FFTW_UNALIGNED} flag when creating the Chris@82: plan, in which case the plan does not depend on the alignment, but Chris@82: this may sacrifice substantial performance on architectures (like x86) Chris@82: with SIMD instructions (@pxref{SIMD alignment and fftw_malloc}). Chris@82: @ctindex FFTW_UNALIGNED Chris@82: Chris@82: @end itemize Chris@82: Chris@82: @c ------------------------------------------------------- Chris@82: @node Fortran Examples, Wisdom of Fortran?, FFTW Execution in Fortran, Calling FFTW from Legacy Fortran Chris@82: @section Fortran Examples Chris@82: Chris@82: In C, you might have something like the following to transform a Chris@82: one-dimensional complex array: Chris@82: Chris@82: @example Chris@82: fftw_complex in[N], out[N]; Chris@82: fftw_plan plan; Chris@82: Chris@82: plan = fftw_plan_dft_1d(N,in,out,FFTW_FORWARD,FFTW_ESTIMATE); Chris@82: fftw_execute(plan); Chris@82: fftw_destroy_plan(plan); Chris@82: @end example Chris@82: Chris@82: In Fortran, you would use the following to accomplish the same thing: Chris@82: Chris@82: @example Chris@82: double complex in, out Chris@82: dimension in(N), out(N) Chris@82: integer*8 plan Chris@82: Chris@82: call dfftw_plan_dft_1d(plan,N,in,out,FFTW_FORWARD,FFTW_ESTIMATE) Chris@82: call dfftw_execute_dft(plan, in, out) Chris@82: call dfftw_destroy_plan(plan) Chris@82: @end example Chris@82: @findex dfftw_plan_dft_1d Chris@82: @findex dfftw_execute_dft Chris@82: @findex dfftw_destroy_plan Chris@82: Chris@82: Notice how all routines are called as Fortran subroutines, and the Chris@82: plan is returned via the first argument to @code{dfftw_plan_dft_1d}. Chris@82: Notice also that we changed @code{fftw_execute} to Chris@82: @code{dfftw_execute_dft} (@pxref{FFTW Execution in Fortran}). To do Chris@82: the same thing, but using 8 threads in parallel (@pxref{Multi-threaded Chris@82: FFTW}), you would simply prefix these calls with: Chris@82: Chris@82: @example Chris@82: integer iret Chris@82: call dfftw_init_threads(iret) Chris@82: call dfftw_plan_with_nthreads(8) Chris@82: @end example Chris@82: @findex dfftw_init_threads Chris@82: @findex dfftw_plan_with_nthreads Chris@82: Chris@82: (You might want to check the value of @code{iret}: if it is zero, it Chris@82: indicates an unlikely error during thread initialization.) Chris@82: Chris@82: To transform a three-dimensional array in-place with C, you might do: Chris@82: Chris@82: @example Chris@82: fftw_complex arr[L][M][N]; Chris@82: fftw_plan plan; Chris@82: Chris@82: plan = fftw_plan_dft_3d(L,M,N, arr,arr, Chris@82: FFTW_FORWARD, FFTW_ESTIMATE); Chris@82: fftw_execute(plan); Chris@82: fftw_destroy_plan(plan); Chris@82: @end example Chris@82: Chris@82: In Fortran, you would use this instead: Chris@82: Chris@82: @example Chris@82: double complex arr Chris@82: dimension arr(L,M,N) Chris@82: integer*8 plan Chris@82: Chris@82: call dfftw_plan_dft_3d(plan, L,M,N, arr,arr, Chris@82: & FFTW_FORWARD, FFTW_ESTIMATE) Chris@82: call dfftw_execute_dft(plan, arr, arr) Chris@82: call dfftw_destroy_plan(plan) Chris@82: @end example Chris@82: @findex dfftw_plan_dft_3d Chris@82: Chris@82: Note that we pass the array dimensions in the ``natural'' order in both C Chris@82: and Fortran. Chris@82: Chris@82: To transform a one-dimensional real array in Fortran, you might do: Chris@82: Chris@82: @example Chris@82: double precision in Chris@82: dimension in(N) Chris@82: double complex out Chris@82: dimension out(N/2 + 1) Chris@82: integer*8 plan Chris@82: Chris@82: call dfftw_plan_dft_r2c_1d(plan,N,in,out,FFTW_ESTIMATE) Chris@82: call dfftw_execute_dft_r2c(plan, in, out) Chris@82: call dfftw_destroy_plan(plan) Chris@82: @end example Chris@82: @findex dfftw_plan_dft_r2c_1d Chris@82: @findex dfftw_execute_dft_r2c Chris@82: Chris@82: To transform a two-dimensional real array, out of place, you might use Chris@82: the following: Chris@82: Chris@82: @example Chris@82: double precision in Chris@82: dimension in(M,N) Chris@82: double complex out Chris@82: dimension out(M/2 + 1, N) Chris@82: integer*8 plan Chris@82: Chris@82: call dfftw_plan_dft_r2c_2d(plan,M,N,in,out,FFTW_ESTIMATE) Chris@82: call dfftw_execute_dft_r2c(plan, in, out) Chris@82: call dfftw_destroy_plan(plan) Chris@82: @end example Chris@82: @findex dfftw_plan_dft_r2c_2d Chris@82: Chris@82: @strong{Important:} Notice that it is the @emph{first} dimension of the Chris@82: complex output array that is cut in half in Fortran, rather than the Chris@82: last dimension as in C. This is a consequence of the interface routines Chris@82: reversing the order of the array dimensions passed to FFTW so that the Chris@82: Fortran program can use its ordinary column-major order. Chris@82: @cindex column-major Chris@82: @cindex r2c/c2r multi-dimensional array format Chris@82: Chris@82: @c ------------------------------------------------------- Chris@82: @node Wisdom of Fortran?, , Fortran Examples, Calling FFTW from Legacy Fortran Chris@82: @section Wisdom of Fortran? Chris@82: Chris@82: In this section, we discuss how one can import/export FFTW wisdom Chris@82: (saved plans) to/from a Fortran program; we assume that the reader is Chris@82: already familiar with wisdom, as described in @ref{Words of Chris@82: Wisdom-Saving Plans}. Chris@82: Chris@82: @cindex portability Chris@82: The basic problem is that is difficult to (portably) pass files and Chris@82: strings between Fortran and C, so we cannot provide a direct Fortran Chris@82: equivalent to the @code{fftw_export_wisdom_to_file}, etcetera, Chris@82: functions. Fortran interfaces @emph{are} provided for the functions Chris@82: that do not take file/string arguments, however: Chris@82: @code{dfftw_import_system_wisdom}, @code{dfftw_import_wisdom}, Chris@82: @code{dfftw_export_wisdom}, and @code{dfftw_forget_wisdom}. Chris@82: @findex dfftw_import_system_wisdom Chris@82: @findex dfftw_import_wisdom Chris@82: @findex dfftw_export_wisdom Chris@82: @findex dfftw_forget_wisdom Chris@82: Chris@82: Chris@82: So, for example, to import the system-wide wisdom, you would do: Chris@82: Chris@82: @example Chris@82: integer isuccess Chris@82: call dfftw_import_system_wisdom(isuccess) Chris@82: @end example Chris@82: Chris@82: As usual, the C return value is turned into a first parameter; Chris@82: @code{isuccess} is non-zero on success and zero on failure (e.g. if Chris@82: there is no system wisdom installed). Chris@82: Chris@82: If you want to import/export wisdom from/to an arbitrary file or Chris@82: elsewhere, you can employ the generic @code{dfftw_import_wisdom} and Chris@82: @code{dfftw_export_wisdom} functions, for which you must supply a Chris@82: subroutine to read/write one character at a time. The FFTW package Chris@82: contains an example file @code{doc/f77_wisdom.f} demonstrating how to Chris@82: implement @code{import_wisdom_from_file} and Chris@82: @code{export_wisdom_to_file} subroutines in this way. (These routines Chris@82: cannot be compiled into the FFTW library itself, lest all FFTW-using Chris@82: programs be required to link with the Fortran I/O library.)