annotate fft/fftw/fftw-3.3.4/doc/legacy-fortran.texi @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 @node Calling FFTW from Legacy Fortran, Upgrading from FFTW version 2, Calling FFTW from Modern Fortran, Top
Chris@19 2 @chapter Calling FFTW from Legacy Fortran
Chris@19 3 @cindex Fortran interface
Chris@19 4
Chris@19 5 This chapter describes the interface to FFTW callable by Fortran code
Chris@19 6 in older compilers not supporting the Fortran 2003 C interoperability
Chris@19 7 features (@pxref{Calling FFTW from Modern Fortran}). This interface
Chris@19 8 has the major disadvantage that it is not type-checked, so if you
Chris@19 9 mistake the argument types or ordering then your program will not have
Chris@19 10 any compiler errors, and will likely crash at runtime. So, greater
Chris@19 11 care is needed. Also, technically interfacing older Fortran versions
Chris@19 12 to C is nonstandard, but in practice we have found that the techniques
Chris@19 13 used in this chapter have worked with all known Fortran compilers for
Chris@19 14 many years.
Chris@19 15
Chris@19 16 The legacy Fortran interface differs from the C interface only in the
Chris@19 17 prefix (@samp{dfftw_} instead of @samp{fftw_} in double precision) and
Chris@19 18 a few other minor details. This Fortran interface is included in the
Chris@19 19 FFTW libraries by default, unless a Fortran compiler isn't found on
Chris@19 20 your system or @code{--disable-fortran} is included in the
Chris@19 21 @code{configure} flags. We assume here that the reader is already
Chris@19 22 familiar with the usage of FFTW in C, as described elsewhere in this
Chris@19 23 manual.
Chris@19 24
Chris@19 25 The MPI parallel interface to FFTW is @emph{not} currently available
Chris@19 26 to legacy Fortran.
Chris@19 27
Chris@19 28 @menu
Chris@19 29 * Fortran-interface routines::
Chris@19 30 * FFTW Constants in Fortran::
Chris@19 31 * FFTW Execution in Fortran::
Chris@19 32 * Fortran Examples::
Chris@19 33 * Wisdom of Fortran?::
Chris@19 34 @end menu
Chris@19 35
Chris@19 36 @c -------------------------------------------------------
Chris@19 37 @node Fortran-interface routines, FFTW Constants in Fortran, Calling FFTW from Legacy Fortran, Calling FFTW from Legacy Fortran
Chris@19 38 @section Fortran-interface routines
Chris@19 39
Chris@19 40 Nearly all of the FFTW functions have Fortran-callable equivalents.
Chris@19 41 The name of the legacy Fortran routine is the same as that of the
Chris@19 42 corresponding C routine, but with the @samp{fftw_} prefix replaced by
Chris@19 43 @samp{dfftw_}.@footnote{Technically, Fortran 77 identifiers are not
Chris@19 44 allowed to have more than 6 characters, nor may they contain
Chris@19 45 underscores. Any compiler that enforces this limitation doesn't
Chris@19 46 deserve to link to FFTW.} The single and long-double precision
Chris@19 47 versions use @samp{sfftw_} and @samp{lfftw_}, respectively, instead of
Chris@19 48 @samp{fftwf_} and @samp{fftwl_}; quadruple precision (@code{real*16})
Chris@19 49 is available on some systems as @samp{fftwq_} (@pxref{Precision}).
Chris@19 50 (Note that @code{long double} on x86 hardware is usually at most
Chris@19 51 80-bit extended precision, @emph{not} quadruple precision.)
Chris@19 52
Chris@19 53 For the most part, all of the arguments to the functions are the same,
Chris@19 54 with the following exceptions:
Chris@19 55
Chris@19 56 @itemize @bullet
Chris@19 57
Chris@19 58 @item
Chris@19 59 @code{plan} variables (what would be of type @code{fftw_plan} in C),
Chris@19 60 must be declared as a type that is at least as big as a pointer
Chris@19 61 (address) on your machine. We recommend using @code{integer*8} everywhere,
Chris@19 62 since this should always be big enough.
Chris@19 63 @cindex portability
Chris@19 64
Chris@19 65 @item
Chris@19 66 Any function that returns a value (e.g. @code{fftw_plan_dft}) is
Chris@19 67 converted into a @emph{subroutine}. The return value is converted into
Chris@19 68 an additional @emph{first} parameter of this subroutine.@footnote{The
Chris@19 69 reason for this is that some Fortran implementations seem to have
Chris@19 70 trouble with C function return values, and vice versa.}
Chris@19 71
Chris@19 72 @item
Chris@19 73 @cindex column-major
Chris@19 74 The Fortran routines expect multi-dimensional arrays to be in
Chris@19 75 @emph{column-major} order, which is the ordinary format of Fortran
Chris@19 76 arrays (@pxref{Multi-dimensional Array Format}). They do this
Chris@19 77 transparently and costlessly simply by reversing the order of the
Chris@19 78 dimensions passed to FFTW, but this has one important consequence for
Chris@19 79 multi-dimensional real-complex transforms, discussed below.
Chris@19 80
Chris@19 81 @item
Chris@19 82 Wisdom import and export is somewhat more tricky because one cannot
Chris@19 83 easily pass files or strings between C and Fortran; see @ref{Wisdom of
Chris@19 84 Fortran?}.
Chris@19 85
Chris@19 86 @item
Chris@19 87 Legacy Fortran cannot use the @code{fftw_malloc} dynamic-allocation routine.
Chris@19 88 If you want to exploit the SIMD FFTW (@pxref{SIMD alignment and fftw_malloc}), you'll
Chris@19 89 need to figure out some other way to ensure that your arrays are at
Chris@19 90 least 16-byte aligned.
Chris@19 91
Chris@19 92 @item
Chris@19 93 @tindex fftw_iodim
Chris@19 94 @cindex guru interface
Chris@19 95 Since Fortran 77 does not have data structures, the @code{fftw_iodim}
Chris@19 96 structure from the guru interface (@pxref{Guru vector and transform
Chris@19 97 sizes}) must be split into separate arguments. In particular, any
Chris@19 98 @code{fftw_iodim} array arguments in the C guru interface become three
Chris@19 99 integer array arguments (@code{n}, @code{is}, and @code{os}) in the
Chris@19 100 Fortran guru interface, all of whose lengths should be equal to the
Chris@19 101 corresponding @code{rank} argument.
Chris@19 102
Chris@19 103 @item
Chris@19 104 The guru planner interface in Fortran does @emph{not} do any automatic
Chris@19 105 translation between column-major and row-major; you are responsible
Chris@19 106 for setting the strides etcetera to correspond to your Fortran arrays.
Chris@19 107 However, as a slight bug that we are preserving for backwards
Chris@19 108 compatibility, the @samp{plan_guru_r2r} in Fortran @emph{does} reverse the
Chris@19 109 order of its @code{kind} array parameter, so the @code{kind} array
Chris@19 110 of that routine should be in the reverse of the order of the iodim
Chris@19 111 arrays (see above).
Chris@19 112
Chris@19 113 @end itemize
Chris@19 114
Chris@19 115 In general, you should take care to use Fortran data types that
Chris@19 116 correspond to (i.e. are the same size as) the C types used by FFTW.
Chris@19 117 In practice, this correspondence is usually straightforward
Chris@19 118 (i.e. @code{integer} corresponds to @code{int}, @code{real}
Chris@19 119 corresponds to @code{float}, etcetera). The native Fortran
Chris@19 120 double/single-precision complex type should be compatible with
Chris@19 121 @code{fftw_complex}/@code{fftwf_complex}. Such simple correspondences
Chris@19 122 are assumed in the examples below.
Chris@19 123 @cindex portability
Chris@19 124
Chris@19 125 @c -------------------------------------------------------
Chris@19 126 @node FFTW Constants in Fortran, FFTW Execution in Fortran, Fortran-interface routines, Calling FFTW from Legacy Fortran
Chris@19 127 @section FFTW Constants in Fortran
Chris@19 128
Chris@19 129 When creating plans in FFTW, a number of constants are used to specify
Chris@19 130 options, such as @code{FFTW_MEASURE} or @code{FFTW_ESTIMATE}. The
Chris@19 131 same constants must be used with the wrapper routines, but of course the
Chris@19 132 C header files where the constants are defined can't be incorporated
Chris@19 133 directly into Fortran code.
Chris@19 134
Chris@19 135 Instead, we have placed Fortran equivalents of the FFTW constant
Chris@19 136 definitions in the file @code{fftw3.f}, which can be found in the same
Chris@19 137 directory as @code{fftw3.h}. If your Fortran compiler supports a
Chris@19 138 preprocessor of some sort, you should be able to @code{include} or
Chris@19 139 @code{#include} this file; otherwise, you can paste it directly into
Chris@19 140 your code.
Chris@19 141
Chris@19 142 @cindex flags
Chris@19 143 In C, you combine different flags (like @code{FFTW_PRESERVE_INPUT} and
Chris@19 144 @code{FFTW_MEASURE}) using the @samp{@code{|}} operator; in Fortran
Chris@19 145 you should just use @samp{@code{+}}. (Take care not to add in the
Chris@19 146 same flag more than once, though. Alternatively, you can use the
Chris@19 147 @code{ior} intrinsic function standardized in Fortran 95.)
Chris@19 148
Chris@19 149 @c -------------------------------------------------------
Chris@19 150 @node FFTW Execution in Fortran, Fortran Examples, FFTW Constants in Fortran, Calling FFTW from Legacy Fortran
Chris@19 151 @section FFTW Execution in Fortran
Chris@19 152
Chris@19 153 In C, in order to use a plan, one normally calls @code{fftw_execute},
Chris@19 154 which executes the plan to perform the transform on the input/output
Chris@19 155 arrays passed when the plan was created (@pxref{Using Plans}). The
Chris@19 156 corresponding subroutine call in legacy Fortran is:
Chris@19 157 @example
Chris@19 158 call dfftw_execute(plan)
Chris@19 159 @end example
Chris@19 160 @findex dfftw_execute
Chris@19 161
Chris@19 162 However, we have had reports that this causes problems with some
Chris@19 163 recent optimizing Fortran compilers. The problem is, because the
Chris@19 164 input/output arrays are not passed as explicit arguments to
Chris@19 165 @code{dfftw_execute}, the semantics of Fortran (unlike C) allow the
Chris@19 166 compiler to assume that the input/output arrays are not changed by
Chris@19 167 @code{dfftw_execute}. As a consequence, certain compilers end up
Chris@19 168 optimizing out or repositioning the call to @code{dfftw_execute},
Chris@19 169 assuming incorrectly that it does nothing.
Chris@19 170
Chris@19 171 There are various workarounds to this, but the safest and simplest
Chris@19 172 thing is to not use @code{dfftw_execute} in Fortran. Instead, use the
Chris@19 173 functions described in @ref{New-array Execute Functions}, which take
Chris@19 174 the input/output arrays as explicit arguments. For example, if the
Chris@19 175 plan is for a complex-data DFT and was created for the arrays
Chris@19 176 @code{in} and @code{out}, you would do:
Chris@19 177 @example
Chris@19 178 call dfftw_execute_dft(plan, in, out)
Chris@19 179 @end example
Chris@19 180 @findex dfftw_execute_dft
Chris@19 181
Chris@19 182 There are a few things to be careful of, however:
Chris@19 183
Chris@19 184 @itemize @bullet
Chris@19 185
Chris@19 186 @item
Chris@19 187 You must use the correct type of execute function, matching the way
Chris@19 188 the plan was created. Complex DFT plans should use
Chris@19 189 @code{dfftw_execute_dft}, Real-input (r2c) DFT plans should use use
Chris@19 190 @code{dfftw_execute_dft_r2c}, and real-output (c2r) DFT plans should
Chris@19 191 use @code{dfftw_execute_dft_c2r}. The various r2r plans should use
Chris@19 192 @code{dfftw_execute_r2r}.
Chris@19 193
Chris@19 194 @item
Chris@19 195 You should normally pass the same input/output arrays that were used when
Chris@19 196 creating the plan. This is always safe.
Chris@19 197
Chris@19 198 @item
Chris@19 199 @emph{If} you pass @emph{different} input/output arrays compared to
Chris@19 200 those used when creating the plan, you must abide by all the
Chris@19 201 restrictions of the new-array execute functions (@pxref{New-array
Chris@19 202 Execute Functions}). The most difficult of these, in Fortran, is the
Chris@19 203 requirement that the new arrays have the same alignment as the
Chris@19 204 original arrays, because there seems to be no way in legacy Fortran to obtain
Chris@19 205 guaranteed-aligned arrays (analogous to @code{fftw_malloc} in C). You
Chris@19 206 can, of course, use the @code{FFTW_UNALIGNED} flag when creating the
Chris@19 207 plan, in which case the plan does not depend on the alignment, but
Chris@19 208 this may sacrifice substantial performance on architectures (like x86)
Chris@19 209 with SIMD instructions (@pxref{SIMD alignment and fftw_malloc}).
Chris@19 210 @ctindex FFTW_UNALIGNED
Chris@19 211
Chris@19 212 @end itemize
Chris@19 213
Chris@19 214 @c -------------------------------------------------------
Chris@19 215 @node Fortran Examples, Wisdom of Fortran?, FFTW Execution in Fortran, Calling FFTW from Legacy Fortran
Chris@19 216 @section Fortran Examples
Chris@19 217
Chris@19 218 In C, you might have something like the following to transform a
Chris@19 219 one-dimensional complex array:
Chris@19 220
Chris@19 221 @example
Chris@19 222 fftw_complex in[N], out[N];
Chris@19 223 fftw_plan plan;
Chris@19 224
Chris@19 225 plan = fftw_plan_dft_1d(N,in,out,FFTW_FORWARD,FFTW_ESTIMATE);
Chris@19 226 fftw_execute(plan);
Chris@19 227 fftw_destroy_plan(plan);
Chris@19 228 @end example
Chris@19 229
Chris@19 230 In Fortran, you would use the following to accomplish the same thing:
Chris@19 231
Chris@19 232 @example
Chris@19 233 double complex in, out
Chris@19 234 dimension in(N), out(N)
Chris@19 235 integer*8 plan
Chris@19 236
Chris@19 237 call dfftw_plan_dft_1d(plan,N,in,out,FFTW_FORWARD,FFTW_ESTIMATE)
Chris@19 238 call dfftw_execute_dft(plan, in, out)
Chris@19 239 call dfftw_destroy_plan(plan)
Chris@19 240 @end example
Chris@19 241 @findex dfftw_plan_dft_1d
Chris@19 242 @findex dfftw_execute_dft
Chris@19 243 @findex dfftw_destroy_plan
Chris@19 244
Chris@19 245 Notice how all routines are called as Fortran subroutines, and the
Chris@19 246 plan is returned via the first argument to @code{dfftw_plan_dft_1d}.
Chris@19 247 Notice also that we changed @code{fftw_execute} to
Chris@19 248 @code{dfftw_execute_dft} (@pxref{FFTW Execution in Fortran}). To do
Chris@19 249 the same thing, but using 8 threads in parallel (@pxref{Multi-threaded
Chris@19 250 FFTW}), you would simply prefix these calls with:
Chris@19 251
Chris@19 252 @example
Chris@19 253 integer iret
Chris@19 254 call dfftw_init_threads(iret)
Chris@19 255 call dfftw_plan_with_nthreads(8)
Chris@19 256 @end example
Chris@19 257 @findex dfftw_init_threads
Chris@19 258 @findex dfftw_plan_with_nthreads
Chris@19 259
Chris@19 260 (You might want to check the value of @code{iret}: if it is zero, it
Chris@19 261 indicates an unlikely error during thread initialization.)
Chris@19 262
Chris@19 263 To transform a three-dimensional array in-place with C, you might do:
Chris@19 264
Chris@19 265 @example
Chris@19 266 fftw_complex arr[L][M][N];
Chris@19 267 fftw_plan plan;
Chris@19 268
Chris@19 269 plan = fftw_plan_dft_3d(L,M,N, arr,arr,
Chris@19 270 FFTW_FORWARD, FFTW_ESTIMATE);
Chris@19 271 fftw_execute(plan);
Chris@19 272 fftw_destroy_plan(plan);
Chris@19 273 @end example
Chris@19 274
Chris@19 275 In Fortran, you would use this instead:
Chris@19 276
Chris@19 277 @example
Chris@19 278 double complex arr
Chris@19 279 dimension arr(L,M,N)
Chris@19 280 integer*8 plan
Chris@19 281
Chris@19 282 call dfftw_plan_dft_3d(plan, L,M,N, arr,arr,
Chris@19 283 & FFTW_FORWARD, FFTW_ESTIMATE)
Chris@19 284 call dfftw_execute_dft(plan, arr, arr)
Chris@19 285 call dfftw_destroy_plan(plan)
Chris@19 286 @end example
Chris@19 287 @findex dfftw_plan_dft_3d
Chris@19 288
Chris@19 289 Note that we pass the array dimensions in the ``natural'' order in both C
Chris@19 290 and Fortran.
Chris@19 291
Chris@19 292 To transform a one-dimensional real array in Fortran, you might do:
Chris@19 293
Chris@19 294 @example
Chris@19 295 double precision in
Chris@19 296 dimension in(N)
Chris@19 297 double complex out
Chris@19 298 dimension out(N/2 + 1)
Chris@19 299 integer*8 plan
Chris@19 300
Chris@19 301 call dfftw_plan_dft_r2c_1d(plan,N,in,out,FFTW_ESTIMATE)
Chris@19 302 call dfftw_execute_dft_r2c(plan, in, out)
Chris@19 303 call dfftw_destroy_plan(plan)
Chris@19 304 @end example
Chris@19 305 @findex dfftw_plan_dft_r2c_1d
Chris@19 306 @findex dfftw_execute_dft_r2c
Chris@19 307
Chris@19 308 To transform a two-dimensional real array, out of place, you might use
Chris@19 309 the following:
Chris@19 310
Chris@19 311 @example
Chris@19 312 double precision in
Chris@19 313 dimension in(M,N)
Chris@19 314 double complex out
Chris@19 315 dimension out(M/2 + 1, N)
Chris@19 316 integer*8 plan
Chris@19 317
Chris@19 318 call dfftw_plan_dft_r2c_2d(plan,M,N,in,out,FFTW_ESTIMATE)
Chris@19 319 call dfftw_execute_dft_r2c(plan, in, out)
Chris@19 320 call dfftw_destroy_plan(plan)
Chris@19 321 @end example
Chris@19 322 @findex dfftw_plan_dft_r2c_2d
Chris@19 323
Chris@19 324 @strong{Important:} Notice that it is the @emph{first} dimension of the
Chris@19 325 complex output array that is cut in half in Fortran, rather than the
Chris@19 326 last dimension as in C. This is a consequence of the interface routines
Chris@19 327 reversing the order of the array dimensions passed to FFTW so that the
Chris@19 328 Fortran program can use its ordinary column-major order.
Chris@19 329 @cindex column-major
Chris@19 330 @cindex r2c/c2r multi-dimensional array format
Chris@19 331
Chris@19 332 @c -------------------------------------------------------
Chris@19 333 @node Wisdom of Fortran?, , Fortran Examples, Calling FFTW from Legacy Fortran
Chris@19 334 @section Wisdom of Fortran?
Chris@19 335
Chris@19 336 In this section, we discuss how one can import/export FFTW wisdom
Chris@19 337 (saved plans) to/from a Fortran program; we assume that the reader is
Chris@19 338 already familiar with wisdom, as described in @ref{Words of
Chris@19 339 Wisdom-Saving Plans}.
Chris@19 340
Chris@19 341 @cindex portability
Chris@19 342 The basic problem is that is difficult to (portably) pass files and
Chris@19 343 strings between Fortran and C, so we cannot provide a direct Fortran
Chris@19 344 equivalent to the @code{fftw_export_wisdom_to_file}, etcetera,
Chris@19 345 functions. Fortran interfaces @emph{are} provided for the functions
Chris@19 346 that do not take file/string arguments, however:
Chris@19 347 @code{dfftw_import_system_wisdom}, @code{dfftw_import_wisdom},
Chris@19 348 @code{dfftw_export_wisdom}, and @code{dfftw_forget_wisdom}.
Chris@19 349 @findex dfftw_import_system_wisdom
Chris@19 350 @findex dfftw_import_wisdom
Chris@19 351 @findex dfftw_export_wisdom
Chris@19 352 @findex dfftw_forget_wisdom
Chris@19 353
Chris@19 354
Chris@19 355 So, for example, to import the system-wide wisdom, you would do:
Chris@19 356
Chris@19 357 @example
Chris@19 358 integer isuccess
Chris@19 359 call dfftw_import_system_wisdom(isuccess)
Chris@19 360 @end example
Chris@19 361
Chris@19 362 As usual, the C return value is turned into a first parameter;
Chris@19 363 @code{isuccess} is non-zero on success and zero on failure (e.g. if
Chris@19 364 there is no system wisdom installed).
Chris@19 365
Chris@19 366 If you want to import/export wisdom from/to an arbitrary file or
Chris@19 367 elsewhere, you can employ the generic @code{dfftw_import_wisdom} and
Chris@19 368 @code{dfftw_export_wisdom} functions, for which you must supply a
Chris@19 369 subroutine to read/write one character at a time. The FFTW package
Chris@19 370 contains an example file @code{doc/f77_wisdom.f} demonstrating how to
Chris@19 371 implement @code{import_wisdom_from_file} and
Chris@19 372 @code{export_wisdom_to_file} subroutines in this way. (These routines
Chris@19 373 cannot be compiled into the FFTW library itself, lest all FFTW-using
Chris@19 374 programs be required to link with the Fortran I/O library.)