annotate fft/fftw/fftw-3.3.4/doc/modern-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 Modern Fortran, Calling FFTW from Legacy Fortran, Distributed-memory FFTW with MPI, Top
Chris@19 2 @chapter Calling FFTW from Modern Fortran
Chris@19 3 @cindex Fortran interface
Chris@19 4
Chris@19 5 Fortran 2003 standardized ways for Fortran code to call C libraries,
Chris@19 6 and this allows us to support a direct translation of the FFTW C API
Chris@19 7 into Fortran. Compared to the legacy Fortran 77 interface
Chris@19 8 (@pxref{Calling FFTW from Legacy Fortran}), this direct interface
Chris@19 9 offers many advantages, especially compile-time type-checking and
Chris@19 10 aligned memory allocation. As of this writing, support for these C
Chris@19 11 interoperability features seems widespread, having been implemented in
Chris@19 12 nearly all major Fortran compilers (e.g. GNU, Intel, IBM,
Chris@19 13 Oracle/Solaris, Portland Group, NAG).
Chris@19 14 @cindex portability
Chris@19 15
Chris@19 16 This chapter documents that interface. For the most part, since this
Chris@19 17 interface allows Fortran to call the C interface directly, the usage
Chris@19 18 is identical to C translated to Fortran syntax. However, there are a
Chris@19 19 few subtle points such as memory allocation, wisdom, and data types
Chris@19 20 that deserve closer attention.
Chris@19 21
Chris@19 22 @menu
Chris@19 23 * Overview of Fortran interface::
Chris@19 24 * Reversing array dimensions::
Chris@19 25 * FFTW Fortran type reference::
Chris@19 26 * Plan execution in Fortran::
Chris@19 27 * Allocating aligned memory in Fortran::
Chris@19 28 * Accessing the wisdom API from Fortran::
Chris@19 29 * Defining an FFTW module::
Chris@19 30 @end menu
Chris@19 31
Chris@19 32 @c -------------------------------------------------------
Chris@19 33 @node Overview of Fortran interface, Reversing array dimensions, Calling FFTW from Modern Fortran, Calling FFTW from Modern Fortran
Chris@19 34 @section Overview of Fortran interface
Chris@19 35
Chris@19 36 FFTW provides a file @code{fftw3.f03} that defines Fortran 2003
Chris@19 37 interfaces for all of its C routines, except for the MPI routines
Chris@19 38 described elsewhere, which can be found in the same directory as
Chris@19 39 @code{fftw3.h} (the C header file). In any Fortran subroutine where
Chris@19 40 you want to use FFTW functions, you should begin with:
Chris@19 41
Chris@19 42 @cindex iso_c_binding
Chris@19 43 @example
Chris@19 44 use, intrinsic :: iso_c_binding
Chris@19 45 include 'fftw3.f03'
Chris@19 46 @end example
Chris@19 47
Chris@19 48 This includes the interface definitions and the standard
Chris@19 49 @code{iso_c_binding} module (which defines the equivalents of C
Chris@19 50 types). You can also put the FFTW functions into a module if you
Chris@19 51 prefer (@pxref{Defining an FFTW module}).
Chris@19 52
Chris@19 53 At this point, you can now call anything in the FFTW C interface
Chris@19 54 directly, almost exactly as in C other than minor changes in syntax.
Chris@19 55 For example:
Chris@19 56
Chris@19 57 @findex fftw_plan_dft_2d
Chris@19 58 @findex fftw_execute_dft
Chris@19 59 @findex fftw_destroy_plan
Chris@19 60 @example
Chris@19 61 type(C_PTR) :: plan
Chris@19 62 complex(C_DOUBLE_COMPLEX), dimension(1024,1000) :: in, out
Chris@19 63 plan = fftw_plan_dft_2d(1000,1024, in,out, FFTW_FORWARD,FFTW_ESTIMATE)
Chris@19 64 ...
Chris@19 65 call fftw_execute_dft(plan, in, out)
Chris@19 66 ...
Chris@19 67 call fftw_destroy_plan(plan)
Chris@19 68 @end example
Chris@19 69
Chris@19 70 A few important things to keep in mind are:
Chris@19 71
Chris@19 72 @itemize @bullet
Chris@19 73
Chris@19 74 @item
Chris@19 75 @tindex fftw_complex
Chris@19 76 @ctindex C_PTR
Chris@19 77 @ctindex C_INT
Chris@19 78 @ctindex C_DOUBLE
Chris@19 79 @ctindex C_DOUBLE_COMPLEX
Chris@19 80 FFTW plans are @code{type(C_PTR)}. Other C types are mapped in the
Chris@19 81 obvious way via the @code{iso_c_binding} standard: @code{int} turns
Chris@19 82 into @code{integer(C_INT)}, @code{fftw_complex} turns into
Chris@19 83 @code{complex(C_DOUBLE_COMPLEX)}, @code{double} turns into
Chris@19 84 @code{real(C_DOUBLE)}, and so on. @xref{FFTW Fortran type reference}.
Chris@19 85
Chris@19 86 @item
Chris@19 87 Functions in C become functions in Fortran if they have a return value,
Chris@19 88 and subroutines in Fortran otherwise.
Chris@19 89
Chris@19 90 @item
Chris@19 91 The ordering of the Fortran array dimensions must be @emph{reversed}
Chris@19 92 when they are passed to the FFTW plan creation, thanks to differences
Chris@19 93 in array indexing conventions (@pxref{Multi-dimensional Array
Chris@19 94 Format}). This is @emph{unlike} the legacy Fortran interface
Chris@19 95 (@pxref{Fortran-interface routines}), which reversed the dimensions
Chris@19 96 for you. @xref{Reversing array dimensions}.
Chris@19 97
Chris@19 98 @item
Chris@19 99 @cindex alignment
Chris@19 100 @cindex SIMD
Chris@19 101 Using ordinary Fortran array declarations like this works, but may
Chris@19 102 yield suboptimal performance because the data may not be not aligned
Chris@19 103 to exploit SIMD instructions on modern proessors (@pxref{SIMD
Chris@19 104 alignment and fftw_malloc}). Better performance will often be obtained
Chris@19 105 by allocating with @samp{fftw_alloc}. @xref{Allocating aligned memory
Chris@19 106 in Fortran}.
Chris@19 107
Chris@19 108 @item
Chris@19 109 @findex fftw_execute
Chris@19 110 Similar to the legacy Fortran interface (@pxref{FFTW Execution in
Chris@19 111 Fortran}), we currently recommend @emph{not} using @code{fftw_execute}
Chris@19 112 but rather using the more specialized functions like
Chris@19 113 @code{fftw_execute_dft} (@pxref{New-array Execute Functions}).
Chris@19 114 However, you should execute the plan on the @code{same arrays} as the
Chris@19 115 ones for which you created the plan, unless you are especially
Chris@19 116 careful. @xref{Plan execution in Fortran}. To prevent
Chris@19 117 you from using @code{fftw_execute} by mistake, the @code{fftw3.f03}
Chris@19 118 file does not provide an @code{fftw_execute} interface declaration.
Chris@19 119
Chris@19 120 @item
Chris@19 121 @cindex flags
Chris@19 122 Multiple planner flags are combined with @code{ior} (equivalent to @samp{|} in C). e.g. @code{FFTW_MEASURE | FFTW_DESTROY_INPUT} becomes @code{ior(FFTW_MEASURE, FFTW_DESTROY_INPUT)}. (You can also use @samp{+} as long as you don't try to include a given flag more than once.)
Chris@19 123
Chris@19 124 @end itemize
Chris@19 125
Chris@19 126 @menu
Chris@19 127 * Extended and quadruple precision in Fortran::
Chris@19 128 @end menu
Chris@19 129
Chris@19 130 @node Extended and quadruple precision in Fortran, , Overview of Fortran interface, Overview of Fortran interface
Chris@19 131 @subsection Extended and quadruple precision in Fortran
Chris@19 132 @cindex precision
Chris@19 133
Chris@19 134 If FFTW is compiled in @code{long double} (extended) precision
Chris@19 135 (@pxref{Installation and Customization}), you may be able to call the
Chris@19 136 resulting @code{fftwl_} routines (@pxref{Precision}) from Fortran if
Chris@19 137 your compiler supports the @code{C_LONG_DOUBLE_COMPLEX} type code.
Chris@19 138
Chris@19 139 Because some Fortran compilers do not support
Chris@19 140 @code{C_LONG_DOUBLE_COMPLEX}, the @code{fftwl_} declarations are
Chris@19 141 segregated into a separate interface file @code{fftw3l.f03}, which you
Chris@19 142 should include @emph{in addition} to @code{fftw3.f03} (which declares
Chris@19 143 precision-independent @samp{FFTW_} constants):
Chris@19 144
Chris@19 145 @cindex iso_c_binding
Chris@19 146 @example
Chris@19 147 use, intrinsic :: iso_c_binding
Chris@19 148 include 'fftw3.f03'
Chris@19 149 include 'fftw3l.f03'
Chris@19 150 @end example
Chris@19 151
Chris@19 152 We also support using the nonstandard @code{__float128}
Chris@19 153 quadruple-precision type provided by recent versions of @code{gcc} on
Chris@19 154 32- and 64-bit x86 hardware (@pxref{Installation and Customization}),
Chris@19 155 using the corresponding @code{real(16)} and @code{complex(16)} types
Chris@19 156 supported by @code{gfortran}. The quadruple-precision @samp{fftwq_}
Chris@19 157 functions (@pxref{Precision}) are declared in a @code{fftw3q.f03}
Chris@19 158 interface file, which should be included in addition to
Chris@19 159 @code{fftw3l.f03}, as above. You should also link with
Chris@19 160 @code{-lfftw3q -lquadmath -lm} as in C.
Chris@19 161
Chris@19 162 @c -------------------------------------------------------
Chris@19 163 @node Reversing array dimensions, FFTW Fortran type reference, Overview of Fortran interface, Calling FFTW from Modern Fortran
Chris@19 164 @section Reversing array dimensions
Chris@19 165
Chris@19 166 @cindex row-major
Chris@19 167 @cindex column-major
Chris@19 168 A minor annoyance in calling FFTW from Fortran is that FFTW's array
Chris@19 169 dimensions are defined in the C convention (row-major order), while
Chris@19 170 Fortran's array dimensions are the opposite convention (column-major
Chris@19 171 order). @xref{Multi-dimensional Array Format}. This is just a
Chris@19 172 bookkeeping difference, with no effect on performance. The only
Chris@19 173 consequence of this is that, whenever you create an FFTW plan for a
Chris@19 174 multi-dimensional transform, you must always @emph{reverse the
Chris@19 175 ordering of the dimensions}.
Chris@19 176
Chris@19 177 For example, consider the three-dimensional (@threedims{L,M,N}) arrays:
Chris@19 178
Chris@19 179 @example
Chris@19 180 complex(C_DOUBLE_COMPLEX), dimension(L,M,N) :: in, out
Chris@19 181 @end example
Chris@19 182
Chris@19 183 To plan a DFT for these arrays using @code{fftw_plan_dft_3d}, you could do:
Chris@19 184
Chris@19 185 @findex fftw_plan_dft_3d
Chris@19 186 @example
Chris@19 187 plan = fftw_plan_dft_3d(N,M,L, in,out, FFTW_FORWARD,FFTW_ESTIMATE)
Chris@19 188 @end example
Chris@19 189
Chris@19 190 That is, from FFTW's perspective this is a @threedims{N,M,L} array.
Chris@19 191 @emph{No data transposition need occur}, as this is @emph{only
Chris@19 192 notation}. Similarly, to use the more generic routine
Chris@19 193 @code{fftw_plan_dft} with the same arrays, you could do:
Chris@19 194
Chris@19 195 @example
Chris@19 196 integer(C_INT), dimension(3) :: n = [N,M,L]
Chris@19 197 plan = fftw_plan_dft_3d(3, n, in,out, FFTW_FORWARD,FFTW_ESTIMATE)
Chris@19 198 @end example
Chris@19 199
Chris@19 200 Note, by the way, that this is different from the legacy Fortran
Chris@19 201 interface (@pxref{Fortran-interface routines}), which automatically
Chris@19 202 reverses the order of the array dimension for you. Here, you are
Chris@19 203 calling the C interface directly, so there is no ``translation'' layer.
Chris@19 204
Chris@19 205 @cindex r2c/c2r multi-dimensional array format
Chris@19 206 An important thing to keep in mind is the implication of this for
Chris@19 207 multidimensional real-to-complex transforms (@pxref{Multi-Dimensional
Chris@19 208 DFTs of Real Data}). In C, a multidimensional real-to-complex DFT
Chris@19 209 chops the last dimension roughly in half (@threedims{N,M,L} real input
Chris@19 210 goes to @threedims{N,M,L/2+1} complex output). In Fortran, because
Chris@19 211 the array dimension notation is reversed, the @emph{first} dimension of
Chris@19 212 the complex data is chopped roughly in half. For example consider the
Chris@19 213 @samp{r2c} transform of @threedims{L,M,N} real input in Fortran:
Chris@19 214
Chris@19 215 @findex fftw_plan_dft_r2c_3d
Chris@19 216 @findex fftw_execute_dft_r2c
Chris@19 217 @example
Chris@19 218 type(C_PTR) :: plan
Chris@19 219 real(C_DOUBLE), dimension(L,M,N) :: in
Chris@19 220 complex(C_DOUBLE_COMPLEX), dimension(L/2+1,M,N) :: out
Chris@19 221 plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE)
Chris@19 222 ...
Chris@19 223 call fftw_execute_dft_r2c(plan, in, out)
Chris@19 224 @end example
Chris@19 225
Chris@19 226 @cindex in-place
Chris@19 227 @cindex padding
Chris@19 228 Alternatively, for an in-place r2c transform, as described in the C
Chris@19 229 documentation we must @emph{pad} the @emph{first} dimension of the
Chris@19 230 real input with an extra two entries (which are ignored by FFTW) so as
Chris@19 231 to leave enough space for the complex output. The input is
Chris@19 232 @emph{allocated} as a @threedims{2[L/2+1],M,N} array, even though only
Chris@19 233 @threedims{L,M,N} of it is actually used. In this example, we will
Chris@19 234 allocate the array as a pointer type, using @samp{fftw_alloc} to
Chris@19 235 ensure aligned memory for maximum performance (@pxref{Allocating
Chris@19 236 aligned memory in Fortran}); this also makes it easy to reference the
Chris@19 237 same memory as both a real array and a complex array.
Chris@19 238
Chris@19 239 @findex fftw_alloc_complex
Chris@19 240 @findex c_f_pointer
Chris@19 241 @example
Chris@19 242 real(C_DOUBLE), pointer :: in(:,:,:)
Chris@19 243 complex(C_DOUBLE_COMPLEX), pointer :: out(:,:,:)
Chris@19 244 type(C_PTR) :: plan, data
Chris@19 245 data = fftw_alloc_complex(int((L/2+1) * M * N, C_SIZE_T))
Chris@19 246 call c_f_pointer(data, in, [2*(L/2+1),M,N])
Chris@19 247 call c_f_pointer(data, out, [L/2+1,M,N])
Chris@19 248 plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE)
Chris@19 249 ...
Chris@19 250 call fftw_execute_dft_r2c(plan, in, out)
Chris@19 251 ...
Chris@19 252 call fftw_destroy_plan(plan)
Chris@19 253 call fftw_free(data)
Chris@19 254 @end example
Chris@19 255
Chris@19 256 @c -------------------------------------------------------
Chris@19 257 @node FFTW Fortran type reference, Plan execution in Fortran, Reversing array dimensions, Calling FFTW from Modern Fortran
Chris@19 258 @section FFTW Fortran type reference
Chris@19 259
Chris@19 260 The following are the most important type correspondences between the
Chris@19 261 C interface and Fortran:
Chris@19 262
Chris@19 263 @itemize @bullet
Chris@19 264
Chris@19 265 @item
Chris@19 266 @tindex fftw_plan
Chris@19 267 Plans (@code{fftw_plan} and variants) are @code{type(C_PTR)} (i.e. an
Chris@19 268 opaque pointer).
Chris@19 269
Chris@19 270 @item
Chris@19 271 @tindex fftw_complex
Chris@19 272 @cindex precision
Chris@19 273 @ctindex C_DOUBLE
Chris@19 274 @ctindex C_FLOAT
Chris@19 275 @ctindex C_LONG_DOUBLE
Chris@19 276 @ctindex C_DOUBLE_COMPLEX
Chris@19 277 @ctindex C_FLOAT_COMPLEX
Chris@19 278 @ctindex C_LONG_DOUBLE_COMPLEX
Chris@19 279 The C floating-point types @code{double}, @code{float}, and @code{long
Chris@19 280 double} correspond to @code{real(C_DOUBLE)}, @code{real(C_FLOAT)}, and
Chris@19 281 @code{real(C_LONG_DOUBLE)}, respectively. The C complex types
Chris@19 282 @code{fftw_complex}, @code{fftwf_complex}, and @code{fftwl_complex}
Chris@19 283 correspond in Fortran to @code{complex(C_DOUBLE_COMPLEX)},
Chris@19 284 @code{complex(C_FLOAT_COMPLEX)}, and
Chris@19 285 @code{complex(C_LONG_DOUBLE_COMPLEX)}, respectively.
Chris@19 286 Just as in C
Chris@19 287 (@pxref{Precision}), the FFTW subroutines and types are prefixed with
Chris@19 288 @samp{fftw_}, @code{fftwf_}, and @code{fftwl_} for the different precisions, and link to different libraries (@code{-lfftw3}, @code{-lfftw3f}, and @code{-lfftw3l} on Unix), but use the @emph{same} include file @code{fftw3.f03} and the @emph{same} constants (all of which begin with @samp{FFTW_}). The exception is @code{long double} precision, for which you should @emph{also} include @code{fftw3l.f03} (@pxref{Extended and quadruple precision in Fortran}).
Chris@19 289
Chris@19 290 @item
Chris@19 291 @tindex ptrdiff_t
Chris@19 292 @ctindex C_INT
Chris@19 293 @ctindex C_INTPTR_T
Chris@19 294 @ctindex C_SIZE_T
Chris@19 295 @findex fftw_malloc
Chris@19 296 The C integer types @code{int} and @code{unsigned} (used for planner
Chris@19 297 flags) become @code{integer(C_INT)}. The C integer type @code{ptrdiff_t} (e.g. in the @ref{64-bit Guru Interface}) becomes @code{integer(C_INTPTR_T)}, and @code{size_t} (in @code{fftw_malloc} etc.) becomes @code{integer(C_SIZE_T)}.
Chris@19 298
Chris@19 299 @item
Chris@19 300 @tindex fftw_r2r_kind
Chris@19 301 @ctindex C_FFTW_R2R_KIND
Chris@19 302 The @code{fftw_r2r_kind} type (@pxref{Real-to-Real Transform Kinds})
Chris@19 303 becomes @code{integer(C_FFTW_R2R_KIND)}. The various constant values
Chris@19 304 of the C enumerated type (@code{FFTW_R2HC} etc.) become simply integer
Chris@19 305 constants of the same names in Fortran.
Chris@19 306
Chris@19 307 @item
Chris@19 308 @ctindex FFTW_DESTROY_INPUT
Chris@19 309 @cindex in-place
Chris@19 310 @findex fftw_flops
Chris@19 311 Numeric array pointer arguments (e.g. @code{double *})
Chris@19 312 become @code{dimension(*), intent(out)} arrays of the same type, or
Chris@19 313 @code{dimension(*), intent(in)} if they are pointers to constant data
Chris@19 314 (e.g. @code{const int *}). There are a few exceptions where numeric
Chris@19 315 pointers refer to scalar outputs (e.g. for @code{fftw_flops}), in which
Chris@19 316 case they are @code{intent(out)} scalar arguments in Fortran too.
Chris@19 317 For the new-array execute functions (@pxref{New-array Execute Functions}),
Chris@19 318 the input arrays are declared @code{dimension(*), intent(inout)}, since
Chris@19 319 they can be modified in the case of in-place or @code{FFTW_DESTROY_INPUT}
Chris@19 320 transforms.
Chris@19 321
Chris@19 322 @item
Chris@19 323 @findex fftw_alloc_real
Chris@19 324 @findex c_f_pointer
Chris@19 325 Pointer @emph{return} values (e.g @code{double *}) become
Chris@19 326 @code{type(C_PTR)}. (If they are pointers to arrays, as for
Chris@19 327 @code{fftw_alloc_real}, you can convert them back to Fortran array
Chris@19 328 pointers with the standard intrinsic function @code{c_f_pointer}.)
Chris@19 329
Chris@19 330 @item
Chris@19 331 @cindex guru interface
Chris@19 332 @tindex fftw_iodim
Chris@19 333 @tindex fftw_iodim64
Chris@19 334 @cindex 64-bit architecture
Chris@19 335 The @code{fftw_iodim} type in the guru interface (@pxref{Guru vector
Chris@19 336 and transform sizes}) becomes @code{type(fftw_iodim)} in Fortran, a
Chris@19 337 derived data type (the Fortran analogue of C's @code{struct}) with
Chris@19 338 three @code{integer(C_INT)} components: @code{n}, @code{is}, and
Chris@19 339 @code{os}, with the same meanings as in C. The @code{fftw_iodim64} type in the 64-bit guru interface (@pxref{64-bit Guru Interface}) is the same, except that its components are of type @code{integer(C_INTPTR_T)}.
Chris@19 340
Chris@19 341 @item
Chris@19 342 @ctindex C_FUNPTR
Chris@19 343 Using the wisdom import/export functions from Fortran is a bit tricky,
Chris@19 344 and is discussed in @ref{Accessing the wisdom API from Fortran}. In
Chris@19 345 brief, the @code{FILE *} arguments map to @code{type(C_PTR)}, @code{const char *} to @code{character(C_CHAR), dimension(*), intent(in)} (null-terminated!), and the generic read-char/write-char functions map to @code{type(C_FUNPTR)}.
Chris@19 346
Chris@19 347 @end itemize
Chris@19 348
Chris@19 349 @cindex portability
Chris@19 350 You may be wondering if you need to search-and-replace
Chris@19 351 @code{real(kind(0.0d0))} (or whatever your favorite Fortran spelling
Chris@19 352 of ``double precision'' is) with @code{real(C_DOUBLE)} everywhere in
Chris@19 353 your program, and similarly for @code{complex} and @code{integer}
Chris@19 354 types. The answer is no; you can still use your existing types. As
Chris@19 355 long as these types match their C counterparts, things should work
Chris@19 356 without a hitch. The worst that can happen, e.g. in the (unlikely)
Chris@19 357 event of a system where @code{real(kind(0.0d0))} is different from
Chris@19 358 @code{real(C_DOUBLE)}, is that the compiler will give you a
Chris@19 359 type-mismatch error. That is, if you don't use the
Chris@19 360 @code{iso_c_binding} kinds you need to accept at least the theoretical
Chris@19 361 possibility of having to change your code in response to compiler
Chris@19 362 errors on some future machine, but you don't need to worry about
Chris@19 363 silently compiling incorrect code that yields runtime errors.
Chris@19 364
Chris@19 365 @c -------------------------------------------------------
Chris@19 366 @node Plan execution in Fortran, Allocating aligned memory in Fortran, FFTW Fortran type reference, Calling FFTW from Modern Fortran
Chris@19 367 @section Plan execution in Fortran
Chris@19 368
Chris@19 369 In C, in order to use a plan, one normally calls @code{fftw_execute},
Chris@19 370 which executes the plan to perform the transform on the input/output
Chris@19 371 arrays passed when the plan was created (@pxref{Using Plans}). The
Chris@19 372 corresponding subroutine call in modern Fortran is:
Chris@19 373 @example
Chris@19 374 call fftw_execute(plan)
Chris@19 375 @end example
Chris@19 376 @findex fftw_execute
Chris@19 377
Chris@19 378 However, we have had reports that this causes problems with some
Chris@19 379 recent optimizing Fortran compilers. The problem is, because the
Chris@19 380 input/output arrays are not passed as explicit arguments to
Chris@19 381 @code{fftw_execute}, the semantics of Fortran (unlike C) allow the
Chris@19 382 compiler to assume that the input/output arrays are not changed by
Chris@19 383 @code{fftw_execute}. As a consequence, certain compilers end up
Chris@19 384 repositioning the call to @code{fftw_execute}, assuming incorrectly
Chris@19 385 that it does nothing to the arrays.
Chris@19 386
Chris@19 387 There are various workarounds to this, but the safest and simplest
Chris@19 388 thing is to not use @code{fftw_execute} in Fortran. Instead, use the
Chris@19 389 functions described in @ref{New-array Execute Functions}, which take
Chris@19 390 the input/output arrays as explicit arguments. For example, if the
Chris@19 391 plan is for a complex-data DFT and was created for the arrays
Chris@19 392 @code{in} and @code{out}, you would do:
Chris@19 393 @example
Chris@19 394 call fftw_execute_dft(plan, in, out)
Chris@19 395 @end example
Chris@19 396 @findex fftw_execute_dft
Chris@19 397
Chris@19 398 There are a few things to be careful of, however:
Chris@19 399
Chris@19 400 @itemize @bullet
Chris@19 401
Chris@19 402 @item
Chris@19 403 @findex fftw_execute_dft_r2c
Chris@19 404 @findex fftw_execute_dft_c2r
Chris@19 405 @findex fftw_execute_r2r
Chris@19 406 You must use the correct type of execute function, matching the way
Chris@19 407 the plan was created. Complex DFT plans should use
Chris@19 408 @code{fftw_execute_dft}, Real-input (r2c) DFT plans should use use
Chris@19 409 @code{fftw_execute_dft_r2c}, and real-output (c2r) DFT plans should
Chris@19 410 use @code{fftw_execute_dft_c2r}. The various r2r plans should use
Chris@19 411 @code{fftw_execute_r2r}. Fortunately, if you use the wrong one you
Chris@19 412 will get a compile-time type-mismatch error (unlike legacy Fortran).
Chris@19 413
Chris@19 414 @item
Chris@19 415 You should normally pass the same input/output arrays that were used when
Chris@19 416 creating the plan. This is always safe.
Chris@19 417
Chris@19 418 @item
Chris@19 419 @emph{If} you pass @emph{different} input/output arrays compared to
Chris@19 420 those used when creating the plan, you must abide by all the
Chris@19 421 restrictions of the new-array execute functions (@pxref{New-array
Chris@19 422 Execute Functions}). The most tricky of these is the
Chris@19 423 requirement that the new arrays have the same alignment as the
Chris@19 424 original arrays; the best (and possibly only) way to guarantee this
Chris@19 425 is to use the @samp{fftw_alloc} functions to allocate your arrays (@pxref{Allocating aligned memory in Fortran}). Alternatively, you can
Chris@19 426 use the @code{FFTW_UNALIGNED} flag when creating the
Chris@19 427 plan, in which case the plan does not depend on the alignment, but
Chris@19 428 this may sacrifice substantial performance on architectures (like x86)
Chris@19 429 with SIMD instructions (@pxref{SIMD alignment and fftw_malloc}).
Chris@19 430 @ctindex FFTW_UNALIGNED
Chris@19 431
Chris@19 432 @end itemize
Chris@19 433
Chris@19 434 @c -------------------------------------------------------
Chris@19 435 @node Allocating aligned memory in Fortran, Accessing the wisdom API from Fortran, Plan execution in Fortran, Calling FFTW from Modern Fortran
Chris@19 436 @section Allocating aligned memory in Fortran
Chris@19 437
Chris@19 438 @cindex alignment
Chris@19 439 @findex fftw_alloc_real
Chris@19 440 @findex fftw_alloc_complex
Chris@19 441 In order to obtain maximum performance in FFTW, you should store your
Chris@19 442 data in arrays that have been specially aligned in memory (@pxref{SIMD
Chris@19 443 alignment and fftw_malloc}). Enforcing alignment also permits you to
Chris@19 444 safely use the new-array execute functions (@pxref{New-array Execute
Chris@19 445 Functions}) to apply a given plan to more than one pair of in/out
Chris@19 446 arrays. Unfortunately, standard Fortran arrays do @emph{not} provide
Chris@19 447 any alignment guarantees. The @emph{only} way to allocate aligned
Chris@19 448 memory in standard Fortran is to allocate it with an external C
Chris@19 449 function, like the @code{fftw_alloc_real} and
Chris@19 450 @code{fftw_alloc_complex} functions. Fortunately, Fortran 2003 provides
Chris@19 451 a simple way to associate such allocated memory with a standard Fortran
Chris@19 452 array pointer that you can then use normally.
Chris@19 453
Chris@19 454 We therefore recommend allocating all your input/output arrays using
Chris@19 455 the following technique:
Chris@19 456
Chris@19 457 @enumerate
Chris@19 458
Chris@19 459 @item
Chris@19 460 Declare a @code{pointer}, @code{arr}, to your array of the desired type
Chris@19 461 and dimensions. For example, @code{real(C_DOUBLE), pointer :: a(:,:)}
Chris@19 462 for a 2d real array, or @code{complex(C_DOUBLE_COMPLEX), pointer ::
Chris@19 463 a(:,:,:)} for a 3d complex array.
Chris@19 464
Chris@19 465 @item
Chris@19 466 The number of elements to allocate must be an
Chris@19 467 @code{integer(C_SIZE_T)}. You can either declare a variable of this
Chris@19 468 type, e.g. @code{integer(C_SIZE_T) :: sz}, to store the number of
Chris@19 469 elements to allocate, or you can use the @code{int(..., C_SIZE_T)}
Chris@19 470 intrinsic function. e.g. set @code{sz = L * M * N} or use
Chris@19 471 @code{int(L * M * N, C_SIZE_T)} for an @threedims{L,M,N} array.
Chris@19 472
Chris@19 473 @item
Chris@19 474 Declare a @code{type(C_PTR) :: p} to hold the return value from
Chris@19 475 FFTW's allocation routine. Set @code{p = fftw_alloc_real(sz)} for a real array, or @code{p = fftw_alloc_complex(sz)} for a complex array.
Chris@19 476
Chris@19 477 @item
Chris@19 478 @findex c_f_pointer
Chris@19 479 Associate your pointer @code{arr} with the allocated memory @code{p}
Chris@19 480 using the standard @code{c_f_pointer} subroutine: @code{call
Chris@19 481 c_f_pointer(p, arr, [...dimensions...])}, where
Chris@19 482 @code{[...dimensions...])} are an array of the dimensions of the array
Chris@19 483 (in the usual Fortran order). e.g. @code{call c_f_pointer(p, arr,
Chris@19 484 [L,M,N])} for an @threedims{L,M,N} array. (Alternatively, you can
Chris@19 485 omit the dimensions argument if you specified the shape explicitly
Chris@19 486 when declaring @code{arr}.) You can now use @code{arr} as a usual
Chris@19 487 multidimensional array.
Chris@19 488
Chris@19 489 @item
Chris@19 490 When you are done using the array, deallocate the memory by @code{call
Chris@19 491 fftw_free(p)} on @code{p}.
Chris@19 492
Chris@19 493 @end enumerate
Chris@19 494
Chris@19 495 For example, here is how we would allocate an @twodims{L,M} 2d real array:
Chris@19 496
Chris@19 497 @example
Chris@19 498 real(C_DOUBLE), pointer :: arr(:,:)
Chris@19 499 type(C_PTR) :: p
Chris@19 500 p = fftw_alloc_real(int(L * M, C_SIZE_T))
Chris@19 501 call c_f_pointer(p, arr, [L,M])
Chris@19 502 @emph{...use arr and arr(i,j) as usual...}
Chris@19 503 call fftw_free(p)
Chris@19 504 @end example
Chris@19 505
Chris@19 506 and here is an @threedims{L,M,N} 3d complex array:
Chris@19 507
Chris@19 508 @example
Chris@19 509 complex(C_DOUBLE_COMPLEX), pointer :: arr(:,:,:)
Chris@19 510 type(C_PTR) :: p
Chris@19 511 p = fftw_alloc_complex(int(L * M * N, C_SIZE_T))
Chris@19 512 call c_f_pointer(p, arr, [L,M,N])
Chris@19 513 @emph{...use arr and arr(i,j,k) as usual...}
Chris@19 514 call fftw_free(p)
Chris@19 515 @end example
Chris@19 516
Chris@19 517 See @ref{Reversing array dimensions} for an example allocating a
Chris@19 518 single array and associating both real and complex array pointers with
Chris@19 519 it, for in-place real-to-complex transforms.
Chris@19 520
Chris@19 521 @c -------------------------------------------------------
Chris@19 522 @node Accessing the wisdom API from Fortran, Defining an FFTW module, Allocating aligned memory in Fortran, Calling FFTW from Modern Fortran
Chris@19 523 @section Accessing the wisdom API from Fortran
Chris@19 524 @cindex wisdom
Chris@19 525 @cindex saving plans to disk
Chris@19 526
Chris@19 527 As explained in @ref{Words of Wisdom-Saving Plans}, FFTW provides a
Chris@19 528 ``wisdom'' API for saving plans to disk so that they can be recreated
Chris@19 529 quickly. The C API for exporting (@pxref{Wisdom Export}) and
Chris@19 530 importing (@pxref{Wisdom Import}) wisdom is somewhat tricky to use
Chris@19 531 from Fortran, however, because of differences in file I/O and string
Chris@19 532 types between C and Fortran.
Chris@19 533
Chris@19 534 @menu
Chris@19 535 * Wisdom File Export/Import from Fortran::
Chris@19 536 * Wisdom String Export/Import from Fortran::
Chris@19 537 * Wisdom Generic Export/Import from Fortran::
Chris@19 538 @end menu
Chris@19 539
Chris@19 540 @c =========>
Chris@19 541 @node Wisdom File Export/Import from Fortran, Wisdom String Export/Import from Fortran, Accessing the wisdom API from Fortran, Accessing the wisdom API from Fortran
Chris@19 542 @subsection Wisdom File Export/Import from Fortran
Chris@19 543
Chris@19 544 @findex fftw_import wisdom_from_filename
Chris@19 545 @findex fftw_export_wisdom_to_filename
Chris@19 546 The easiest way to export and import wisdom is to do so using
Chris@19 547 @code{fftw_export_wisdom_to_filename} and
Chris@19 548 @code{fftw_wisdom_from_filename}. The only trick is that these
Chris@19 549 require you to pass a C string, which is an array of type
Chris@19 550 @code{CHARACTER(C_CHAR)} that is terminated by @code{C_NULL_CHAR}.
Chris@19 551 You can call them like this:
Chris@19 552
Chris@19 553 @example
Chris@19 554 integer(C_INT) :: ret
Chris@19 555 ret = fftw_export_wisdom_to_filename(C_CHAR_'my_wisdom.dat' // C_NULL_CHAR)
Chris@19 556 if (ret .eq. 0) stop 'error exporting wisdom to file'
Chris@19 557 ret = fftw_import_wisdom_from_filename(C_CHAR_'my_wisdom.dat' // C_NULL_CHAR)
Chris@19 558 if (ret .eq. 0) stop 'error importing wisdom from file'
Chris@19 559 @end example
Chris@19 560
Chris@19 561 Note that prepending @samp{C_CHAR_} is needed to specify that the
Chris@19 562 literal string is of kind @code{C_CHAR}, and we null-terminate the
Chris@19 563 string by appending @samp{// C_NULL_CHAR}. These functions return an
Chris@19 564 @code{integer(C_INT)} (@code{ret}) which is @code{0} if an error
Chris@19 565 occurred during export/import and nonzero otherwise.
Chris@19 566
Chris@19 567 It is also possible to use the lower-level routines
Chris@19 568 @code{fftw_export_wisdom_to_file} and
Chris@19 569 @code{fftw_import_wisdom_from_file}, which accept parameters of the C
Chris@19 570 type @code{FILE*}, expressed in Fortran as @code{type(C_PTR)}.
Chris@19 571 However, you are then responsible for creating the @code{FILE*}
Chris@19 572 yourself. You can do this by using @code{iso_c_binding} to define
Chris@19 573 Fortran intefaces for the C library functions @code{fopen} and
Chris@19 574 @code{fclose}, which is a bit strange in Fortran but workable.
Chris@19 575
Chris@19 576 @c =========>
Chris@19 577 @node Wisdom String Export/Import from Fortran, Wisdom Generic Export/Import from Fortran, Wisdom File Export/Import from Fortran, Accessing the wisdom API from Fortran
Chris@19 578 @subsection Wisdom String Export/Import from Fortran
Chris@19 579
Chris@19 580 @findex fftw_export_wisdom_to_string
Chris@19 581 Dealing with FFTW's C string export/import is a bit more painful. In
Chris@19 582 particular, the @code{fftw_export_wisdom_to_string} function requires
Chris@19 583 you to deal with a dynamically allocated C string. To get its length,
Chris@19 584 you must define an interface to the C @code{strlen} function, and to
Chris@19 585 deallocate it you must define an interface to C @code{free}:
Chris@19 586
Chris@19 587 @example
Chris@19 588 use, intrinsic :: iso_c_binding
Chris@19 589 interface
Chris@19 590 integer(C_INT) function strlen(s) bind(C, name='strlen')
Chris@19 591 import
Chris@19 592 type(C_PTR), value :: s
Chris@19 593 end function strlen
Chris@19 594 subroutine free(p) bind(C, name='free')
Chris@19 595 import
Chris@19 596 type(C_PTR), value :: p
Chris@19 597 end subroutine free
Chris@19 598 end interface
Chris@19 599 @end example
Chris@19 600
Chris@19 601 Given these definitions, you can then export wisdom to a Fortran
Chris@19 602 character array:
Chris@19 603
Chris@19 604 @example
Chris@19 605 character(C_CHAR), pointer :: s(:)
Chris@19 606 integer(C_SIZE_T) :: slen
Chris@19 607 type(C_PTR) :: p
Chris@19 608 p = fftw_export_wisdom_to_string()
Chris@19 609 if (.not. c_associated(p)) stop 'error exporting wisdom'
Chris@19 610 slen = strlen(p)
Chris@19 611 call c_f_pointer(p, s, [slen+1])
Chris@19 612 ...
Chris@19 613 call free(p)
Chris@19 614 @end example
Chris@19 615 @findex c_associated
Chris@19 616 @findex c_f_pointer
Chris@19 617
Chris@19 618 Note that @code{slen} is the length of the C string, but the length of
Chris@19 619 the array is @code{slen+1} because it includes the terminating null
Chris@19 620 character. (You can omit the @samp{+1} if you don't want Fortran to
Chris@19 621 know about the null character.) The standard @code{c_associated} function
Chris@19 622 checks whether @code{p} is a null pointer, which is returned by
Chris@19 623 @code{fftw_export_wisdom_to_string} if there was an error.
Chris@19 624
Chris@19 625 @findex fftw_import_wisdom_from_string
Chris@19 626 To import wisdom from a string, use
Chris@19 627 @code{fftw_import_wisdom_from_string} as usual; note that the argument
Chris@19 628 of this function must be a @code{character(C_CHAR)} that is terminated
Chris@19 629 by the @code{C_NULL_CHAR} character, like the @code{s} array above.
Chris@19 630
Chris@19 631 @c =========>
Chris@19 632 @node Wisdom Generic Export/Import from Fortran, , Wisdom String Export/Import from Fortran, Accessing the wisdom API from Fortran
Chris@19 633 @subsection Wisdom Generic Export/Import from Fortran
Chris@19 634
Chris@19 635 The most generic wisdom export/import functions allow you to provide
Chris@19 636 an arbitrary callback function to read/write one character at a time
Chris@19 637 in any way you want. However, your callback function must be written
Chris@19 638 in a special way, using the @code{bind(C)} attribute to be passed to a
Chris@19 639 C interface.
Chris@19 640
Chris@19 641 @findex fftw_export_wisdom
Chris@19 642 In particular, to call the generic wisdom export function
Chris@19 643 @code{fftw_export_wisdom}, you would write a callback subroutine of the form:
Chris@19 644
Chris@19 645 @example
Chris@19 646 subroutine my_write_char(c, p) bind(C)
Chris@19 647 use, intrinsic :: iso_c_binding
Chris@19 648 character(C_CHAR), value :: c
Chris@19 649 type(C_PTR), value :: p
Chris@19 650 @emph{...write c...}
Chris@19 651 end subroutine my_write_char
Chris@19 652 @end example
Chris@19 653
Chris@19 654 Given such a subroutine (along with the corresponding interface definition), you could then export wisdom using:
Chris@19 655
Chris@19 656 @findex c_funloc
Chris@19 657 @example
Chris@19 658 call fftw_export_wisdom(c_funloc(my_write_char), p)
Chris@19 659 @end example
Chris@19 660
Chris@19 661 @findex c_loc
Chris@19 662 @findex c_f_pointer
Chris@19 663 The standard @code{c_funloc} intrinsic converts a Fortran
Chris@19 664 @code{bind(C)} subroutine into a C function pointer. The parameter
Chris@19 665 @code{p} is a @code{type(C_PTR)} to any arbitrary data that you want
Chris@19 666 to pass to @code{my_write_char} (or @code{C_NULL_PTR} if none). (Note
Chris@19 667 that you can get a C pointer to Fortran data using the intrinsic
Chris@19 668 @code{c_loc}, and convert it back to a Fortran pointer in
Chris@19 669 @code{my_write_char} using @code{c_f_pointer}.)
Chris@19 670
Chris@19 671 Similarly, to use the generic @code{fftw_import_wisdom}, you would
Chris@19 672 define a callback function of the form:
Chris@19 673
Chris@19 674 @findex fftw_import_wisdom
Chris@19 675 @example
Chris@19 676 integer(C_INT) function my_read_char(p) bind(C)
Chris@19 677 use, intrinsic :: iso_c_binding
Chris@19 678 type(C_PTR), value :: p
Chris@19 679 character :: c
Chris@19 680 @emph{...read a character c...}
Chris@19 681 my_read_char = ichar(c, C_INT)
Chris@19 682 end function my_read_char
Chris@19 683
Chris@19 684 ....
Chris@19 685
Chris@19 686 integer(C_INT) :: ret
Chris@19 687 ret = fftw_import_wisdom(c_funloc(my_read_char), p)
Chris@19 688 if (ret .eq. 0) stop 'error importing wisdom'
Chris@19 689 @end example
Chris@19 690
Chris@19 691 Your function can return @code{-1} if the end of the input is reached.
Chris@19 692 Again, @code{p} is an arbitrary @code{type(C_PTR} that is passed
Chris@19 693 through to your function. @code{fftw_import_wisdom} returns @code{0}
Chris@19 694 if an error occurred and nonzero otherwise.
Chris@19 695
Chris@19 696 @c -------------------------------------------------------
Chris@19 697 @node Defining an FFTW module, , Accessing the wisdom API from Fortran, Calling FFTW from Modern Fortran
Chris@19 698 @section Defining an FFTW module
Chris@19 699
Chris@19 700 Rather than using the @code{include} statement to include the
Chris@19 701 @code{fftw3.f03} interface file in any subroutine where you want to
Chris@19 702 use FFTW, you might prefer to define an FFTW Fortran module. FFTW
Chris@19 703 does not install itself as a module, primarily because
Chris@19 704 @code{fftw3.f03} can be shared between different Fortran compilers while
Chris@19 705 modules (in general) cannot. However, it is trivial to define your
Chris@19 706 own FFTW module if you want. Just create a file containing:
Chris@19 707
Chris@19 708 @example
Chris@19 709 module FFTW3
Chris@19 710 use, intrinsic :: iso_c_binding
Chris@19 711 include 'fftw3.f03'
Chris@19 712 end module
Chris@19 713 @end example
Chris@19 714
Chris@19 715 Compile this file into a module as usual for your compiler (e.g. with
Chris@19 716 @code{gfortran -c} you will get a file @code{fftw3.mod}). Now,
Chris@19 717 instead of @code{include 'fftw3.f03'}, whenever you want to use FFTW
Chris@19 718 routines you can just do:
Chris@19 719
Chris@19 720 @example
Chris@19 721 use FFTW3
Chris@19 722 @end example
Chris@19 723
Chris@19 724 as usual for Fortran modules. (You still need to link to the FFTW
Chris@19 725 library, of course.)