annotate src/fftw-3.3.5/doc/legacy-fortran.texi @ 169:223a55898ab9 tip default

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