Chris@10: @node Calling FFTW from Modern Fortran, Calling FFTW from Legacy Fortran, Distributed-memory FFTW with MPI, Top Chris@10: @chapter Calling FFTW from Modern Fortran Chris@10: @cindex Fortran interface Chris@10: Chris@10: Fortran 2003 standardized ways for Fortran code to call C libraries, Chris@10: and this allows us to support a direct translation of the FFTW C API Chris@10: into Fortran. Compared to the legacy Fortran 77 interface Chris@10: (@pxref{Calling FFTW from Legacy Fortran}), this direct interface Chris@10: offers many advantages, especially compile-time type-checking and Chris@10: aligned memory allocation. As of this writing, support for these C Chris@10: interoperability features seems widespread, having been implemented in Chris@10: nearly all major Fortran compilers (e.g. GNU, Intel, IBM, Chris@10: Oracle/Solaris, Portland Group, NAG). Chris@10: @cindex portability Chris@10: Chris@10: This chapter documents that interface. For the most part, since this Chris@10: interface allows Fortran to call the C interface directly, the usage Chris@10: is identical to C translated to Fortran syntax. However, there are a Chris@10: few subtle points such as memory allocation, wisdom, and data types Chris@10: that deserve closer attention. Chris@10: Chris@10: @menu Chris@10: * Overview of Fortran interface:: Chris@10: * Reversing array dimensions:: Chris@10: * FFTW Fortran type reference:: Chris@10: * Plan execution in Fortran:: Chris@10: * Allocating aligned memory in Fortran:: Chris@10: * Accessing the wisdom API from Fortran:: Chris@10: * Defining an FFTW module:: Chris@10: @end menu Chris@10: Chris@10: @c ------------------------------------------------------- Chris@10: @node Overview of Fortran interface, Reversing array dimensions, Calling FFTW from Modern Fortran, Calling FFTW from Modern Fortran Chris@10: @section Overview of Fortran interface Chris@10: Chris@10: FFTW provides a file @code{fftw3.f03} that defines Fortran 2003 Chris@10: interfaces for all of its C routines, except for the MPI routines Chris@10: described elsewhere, which can be found in the same directory as Chris@10: @code{fftw3.h} (the C header file). In any Fortran subroutine where Chris@10: you want to use FFTW functions, you should begin with: Chris@10: Chris@10: @cindex iso_c_binding Chris@10: @example Chris@10: use, intrinsic :: iso_c_binding Chris@10: include 'fftw3.f03' Chris@10: @end example Chris@10: Chris@10: This includes the interface definitions and the standard Chris@10: @code{iso_c_binding} module (which defines the equivalents of C Chris@10: types). You can also put the FFTW functions into a module if you Chris@10: prefer (@pxref{Defining an FFTW module}). Chris@10: Chris@10: At this point, you can now call anything in the FFTW C interface Chris@10: directly, almost exactly as in C other than minor changes in syntax. Chris@10: For example: Chris@10: Chris@10: @findex fftw_plan_dft_2d Chris@10: @findex fftw_execute_dft Chris@10: @findex fftw_destroy_plan Chris@10: @example Chris@10: type(C_PTR) :: plan Chris@10: complex(C_DOUBLE_COMPLEX), dimension(1024,1000) :: in, out Chris@10: plan = fftw_plan_dft_2d(1000,1024, in,out, FFTW_FORWARD,FFTW_ESTIMATE) Chris@10: ... Chris@10: call fftw_execute_dft(plan, in, out) Chris@10: ... Chris@10: call fftw_destroy_plan(plan) Chris@10: @end example Chris@10: Chris@10: A few important things to keep in mind are: Chris@10: Chris@10: @itemize @bullet Chris@10: Chris@10: @item Chris@10: @tindex fftw_complex Chris@10: @ctindex C_PTR Chris@10: @ctindex C_INT Chris@10: @ctindex C_DOUBLE Chris@10: @ctindex C_DOUBLE_COMPLEX Chris@10: FFTW plans are @code{type(C_PTR)}. Other C types are mapped in the Chris@10: obvious way via the @code{iso_c_binding} standard: @code{int} turns Chris@10: into @code{integer(C_INT)}, @code{fftw_complex} turns into Chris@10: @code{complex(C_DOUBLE_COMPLEX)}, @code{double} turns into Chris@10: @code{real(C_DOUBLE)}, and so on. @xref{FFTW Fortran type reference}. Chris@10: Chris@10: @item Chris@10: Functions in C become functions in Fortran if they have a return value, Chris@10: and subroutines in Fortran otherwise. Chris@10: Chris@10: @item Chris@10: The ordering of the Fortran array dimensions must be @emph{reversed} Chris@10: when they are passed to the FFTW plan creation, thanks to differences Chris@10: in array indexing conventions (@pxref{Multi-dimensional Array Chris@10: Format}). This is @emph{unlike} the legacy Fortran interface Chris@10: (@pxref{Fortran-interface routines}), which reversed the dimensions Chris@10: for you. @xref{Reversing array dimensions}. Chris@10: Chris@10: @item Chris@10: @cindex alignment Chris@10: @cindex SIMD Chris@10: Using ordinary Fortran array declarations like this works, but may Chris@10: yield suboptimal performance because the data may not be not aligned Chris@10: to exploit SIMD instructions on modern proessors (@pxref{SIMD Chris@10: alignment and fftw_malloc}). Better performance will often be obtained Chris@10: by allocating with @samp{fftw_alloc}. @xref{Allocating aligned memory Chris@10: in Fortran}. Chris@10: Chris@10: @item Chris@10: @findex fftw_execute Chris@10: Similar to the legacy Fortran interface (@pxref{FFTW Execution in Chris@10: Fortran}), we currently recommend @emph{not} using @code{fftw_execute} Chris@10: but rather using the more specialized functions like Chris@10: @code{fftw_execute_dft} (@pxref{New-array Execute Functions}). Chris@10: However, you should execute the plan on the @code{same arrays} as the Chris@10: ones for which you created the plan, unless you are especially Chris@10: careful. @xref{Plan execution in Fortran}. To prevent Chris@10: you from using @code{fftw_execute} by mistake, the @code{fftw3.f03} Chris@10: file does not provide an @code{fftw_execute} interface declaration. Chris@10: Chris@10: @item Chris@10: @cindex flags Chris@10: 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@10: Chris@10: @end itemize Chris@10: Chris@10: @menu Chris@10: * Extended and quadruple precision in Fortran:: Chris@10: @end menu Chris@10: Chris@10: @node Extended and quadruple precision in Fortran, , Overview of Fortran interface, Overview of Fortran interface Chris@10: @subsection Extended and quadruple precision in Fortran Chris@10: @cindex precision Chris@10: Chris@10: If FFTW is compiled in @code{long double} (extended) precision Chris@10: (@pxref{Installation and Customization}), you may be able to call the Chris@10: resulting @code{fftwl_} routines (@pxref{Precision}) from Fortran if Chris@10: your compiler supports the @code{C_LONG_DOUBLE_COMPLEX} type code. Chris@10: Chris@10: Because some Fortran compilers do not support Chris@10: @code{C_LONG_DOUBLE_COMPLEX}, the @code{fftwl_} declarations are Chris@10: segregated into a separate interface file @code{fftw3l.f03}, which you Chris@10: should include @emph{in addition} to @code{fftw3.f03} (which declares Chris@10: precision-independent @samp{FFTW_} constants): Chris@10: Chris@10: @cindex iso_c_binding Chris@10: @example Chris@10: use, intrinsic :: iso_c_binding Chris@10: include 'fftw3.f03' Chris@10: include 'fftw3l.f03' Chris@10: @end example Chris@10: Chris@10: We also support using the nonstandard @code{__float128} Chris@10: quadruple-precision type provided by recent versions of @code{gcc} on Chris@10: 32- and 64-bit x86 hardware (@pxref{Installation and Customization}), Chris@10: using the corresponding @code{real(16)} and @code{complex(16)} types Chris@10: supported by @code{gfortran}. The quadruple-precision @samp{fftwq_} Chris@10: functions (@pxref{Precision}) are declared in a @code{fftw3q.f03} Chris@10: interface file, which should be included in addition to Chris@10: @code{fftw3l.f03}, as above. You should also link with Chris@10: @code{-lfftw3q -lquadmath -lm} as in C. Chris@10: Chris@10: @c ------------------------------------------------------- Chris@10: @node Reversing array dimensions, FFTW Fortran type reference, Overview of Fortran interface, Calling FFTW from Modern Fortran Chris@10: @section Reversing array dimensions Chris@10: Chris@10: @cindex row-major Chris@10: @cindex column-major Chris@10: A minor annoyance in calling FFTW from Fortran is that FFTW's array Chris@10: dimensions are defined in the C convention (row-major order), while Chris@10: Fortran's array dimensions are the opposite convention (column-major Chris@10: order). @xref{Multi-dimensional Array Format}. This is just a Chris@10: bookkeeping difference, with no effect on performance. The only Chris@10: consequence of this is that, whenever you create an FFTW plan for a Chris@10: multi-dimensional transform, you must always @emph{reverse the Chris@10: ordering of the dimensions}. Chris@10: Chris@10: For example, consider the three-dimensional (@threedims{L,M,N}) arrays: Chris@10: Chris@10: @example Chris@10: complex(C_DOUBLE_COMPLEX), dimension(L,M,N) :: in, out Chris@10: @end example Chris@10: Chris@10: To plan a DFT for these arrays using @code{fftw_plan_dft_3d}, you could do: Chris@10: Chris@10: @findex fftw_plan_dft_3d Chris@10: @example Chris@10: plan = fftw_plan_dft_3d(N,M,L, in,out, FFTW_FORWARD,FFTW_ESTIMATE) Chris@10: @end example Chris@10: Chris@10: That is, from FFTW's perspective this is a @threedims{N,M,L} array. Chris@10: @emph{No data transposition need occur}, as this is @emph{only Chris@10: notation}. Similarly, to use the more generic routine Chris@10: @code{fftw_plan_dft} with the same arrays, you could do: Chris@10: Chris@10: @example Chris@10: integer(C_INT), dimension(3) :: n = [N,M,L] Chris@10: plan = fftw_plan_dft_3d(3, n, in,out, FFTW_FORWARD,FFTW_ESTIMATE) Chris@10: @end example Chris@10: Chris@10: Note, by the way, that this is different from the legacy Fortran Chris@10: interface (@pxref{Fortran-interface routines}), which automatically Chris@10: reverses the order of the array dimension for you. Here, you are Chris@10: calling the C interface directly, so there is no ``translation'' layer. Chris@10: Chris@10: @cindex r2c/c2r multi-dimensional array format Chris@10: An important thing to keep in mind is the implication of this for Chris@10: multidimensional real-to-complex transforms (@pxref{Multi-Dimensional Chris@10: DFTs of Real Data}). In C, a multidimensional real-to-complex DFT Chris@10: chops the last dimension roughly in half (@threedims{N,M,L} real input Chris@10: goes to @threedims{N,M,L/2+1} complex output). In Fortran, because Chris@10: the array dimension notation is reversed, the @emph{first} dimension of Chris@10: the complex data is chopped roughly in half. For example consider the Chris@10: @samp{r2c} transform of @threedims{L,M,N} real input in Fortran: Chris@10: Chris@10: @findex fftw_plan_dft_r2c_3d Chris@10: @findex fftw_execute_dft_r2c Chris@10: @example Chris@10: type(C_PTR) :: plan Chris@10: real(C_DOUBLE), dimension(L,M,N) :: in Chris@10: complex(C_DOUBLE_COMPLEX), dimension(L/2+1,M,N) :: out Chris@10: plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE) Chris@10: ... Chris@10: call fftw_execute_dft_r2c(plan, in, out) Chris@10: @end example Chris@10: Chris@10: @cindex in-place Chris@10: @cindex padding Chris@10: Alternatively, for an in-place r2c transform, as described in the C Chris@10: documentation we must @emph{pad} the @emph{first} dimension of the Chris@10: real input with an extra two entries (which are ignored by FFTW) so as Chris@10: to leave enough space for the complex output. The input is Chris@10: @emph{allocated} as a @threedims{2[L/2+1],M,N} array, even though only Chris@10: @threedims{L,M,N} of it is actually used. In this example, we will Chris@10: allocate the array as a pointer type, using @samp{fftw_alloc} to Chris@10: ensure aligned memory for maximum performance (@pxref{Allocating Chris@10: aligned memory in Fortran}); this also makes it easy to reference the Chris@10: same memory as both a real array and a complex array. Chris@10: Chris@10: @findex fftw_alloc_complex Chris@10: @findex c_f_pointer Chris@10: @example Chris@10: real(C_DOUBLE), pointer :: in(:,:,:) Chris@10: complex(C_DOUBLE_COMPLEX), pointer :: out(:,:,:) Chris@10: type(C_PTR) :: plan, data Chris@10: data = fftw_alloc_complex(int((L/2+1) * M * N, C_SIZE_T)) Chris@10: call c_f_pointer(data, in, [2*(L/2+1),M,N]) Chris@10: call c_f_pointer(data, out, [L/2+1,M,N]) Chris@10: plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE) Chris@10: ... Chris@10: call fftw_execute_dft_r2c(plan, in, out) Chris@10: ... Chris@10: call fftw_destroy_plan(plan) Chris@10: call fftw_free(data) Chris@10: @end example Chris@10: Chris@10: @c ------------------------------------------------------- Chris@10: @node FFTW Fortran type reference, Plan execution in Fortran, Reversing array dimensions, Calling FFTW from Modern Fortran Chris@10: @section FFTW Fortran type reference Chris@10: Chris@10: The following are the most important type correspondences between the Chris@10: C interface and Fortran: Chris@10: Chris@10: @itemize @bullet Chris@10: Chris@10: @item Chris@10: @tindex fftw_plan Chris@10: Plans (@code{fftw_plan} and variants) are @code{type(C_PTR)} (i.e. an Chris@10: opaque pointer). Chris@10: Chris@10: @item Chris@10: @tindex fftw_complex Chris@10: @cindex precision Chris@10: @ctindex C_DOUBLE Chris@10: @ctindex C_FLOAT Chris@10: @ctindex C_LONG_DOUBLE Chris@10: @ctindex C_DOUBLE_COMPLEX Chris@10: @ctindex C_FLOAT_COMPLEX Chris@10: @ctindex C_LONG_DOUBLE_COMPLEX Chris@10: The C floating-point types @code{double}, @code{float}, and @code{long Chris@10: double} correspond to @code{real(C_DOUBLE)}, @code{real(C_FLOAT)}, and Chris@10: @code{real(C_LONG_DOUBLE)}, respectively. The C complex types Chris@10: @code{fftw_complex}, @code{fftwf_complex}, and @code{fftwl_complex} Chris@10: correspond in Fortran to @code{complex(C_DOUBLE_COMPLEX)}, Chris@10: @code{complex(C_FLOAT_COMPLEX)}, and Chris@10: @code{complex(C_LONG_DOUBLE_COMPLEX)}, respectively. Chris@10: Just as in C Chris@10: (@pxref{Precision}), the FFTW subroutines and types are prefixed with Chris@10: @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@10: Chris@10: @item Chris@10: @tindex ptrdiff_t Chris@10: @ctindex C_INT Chris@10: @ctindex C_INTPTR_T Chris@10: @ctindex C_SIZE_T Chris@10: @findex fftw_malloc Chris@10: The C integer types @code{int} and @code{unsigned} (used for planner Chris@10: 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@10: Chris@10: @item Chris@10: @tindex fftw_r2r_kind Chris@10: @ctindex C_FFTW_R2R_KIND Chris@10: The @code{fftw_r2r_kind} type (@pxref{Real-to-Real Transform Kinds}) Chris@10: becomes @code{integer(C_FFTW_R2R_KIND)}. The various constant values Chris@10: of the C enumerated type (@code{FFTW_R2HC} etc.) become simply integer Chris@10: constants of the same names in Fortran. Chris@10: Chris@10: @item Chris@10: @ctindex FFTW_DESTROY_INPUT Chris@10: @cindex in-place Chris@10: @findex fftw_flops Chris@10: Numeric array pointer arguments (e.g. @code{double *}) Chris@10: become @code{dimension(*), intent(out)} arrays of the same type, or Chris@10: @code{dimension(*), intent(in)} if they are pointers to constant data Chris@10: (e.g. @code{const int *}). There are a few exceptions where numeric Chris@10: pointers refer to scalar outputs (e.g. for @code{fftw_flops}), in which Chris@10: case they are @code{intent(out)} scalar arguments in Fortran too. Chris@10: For the new-array execute functions (@pxref{New-array Execute Functions}), Chris@10: the input arrays are declared @code{dimension(*), intent(inout)}, since Chris@10: they can be modified in the case of in-place or @code{FFTW_DESTROY_INPUT} Chris@10: transforms. Chris@10: Chris@10: @item Chris@10: @findex fftw_alloc_real Chris@10: @findex c_f_pointer Chris@10: Pointer @emph{return} values (e.g @code{double *}) become Chris@10: @code{type(C_PTR)}. (If they are pointers to arrays, as for Chris@10: @code{fftw_alloc_real}, you can convert them back to Fortran array Chris@10: pointers with the standard intrinsic function @code{c_f_pointer}.) Chris@10: Chris@10: @item Chris@10: @cindex guru interface Chris@10: @tindex fftw_iodim Chris@10: @tindex fftw_iodim64 Chris@10: @cindex 64-bit architecture Chris@10: The @code{fftw_iodim} type in the guru interface (@pxref{Guru vector Chris@10: and transform sizes}) becomes @code{type(fftw_iodim)} in Fortran, a Chris@10: derived data type (the Fortran analogue of C's @code{struct}) with Chris@10: three @code{integer(C_INT)} components: @code{n}, @code{is}, and Chris@10: @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@10: Chris@10: @item Chris@10: @ctindex C_FUNPTR Chris@10: Using the wisdom import/export functions from Fortran is a bit tricky, Chris@10: and is discussed in @ref{Accessing the wisdom API from Fortran}. In Chris@10: 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@10: Chris@10: @end itemize Chris@10: Chris@10: @cindex portability Chris@10: You may be wondering if you need to search-and-replace Chris@10: @code{real(kind(0.0d0))} (or whatever your favorite Fortran spelling Chris@10: of ``double precision'' is) with @code{real(C_DOUBLE)} everywhere in Chris@10: your program, and similarly for @code{complex} and @code{integer} Chris@10: types. The answer is no; you can still use your existing types. As Chris@10: long as these types match their C counterparts, things should work Chris@10: without a hitch. The worst that can happen, e.g. in the (unlikely) Chris@10: event of a system where @code{real(kind(0.0d0))} is different from Chris@10: @code{real(C_DOUBLE)}, is that the compiler will give you a Chris@10: type-mismatch error. That is, if you don't use the Chris@10: @code{iso_c_binding} kinds you need to accept at least the theoretical Chris@10: possibility of having to change your code in response to compiler Chris@10: errors on some future machine, but you don't need to worry about Chris@10: silently compiling incorrect code that yields runtime errors. Chris@10: Chris@10: @c ------------------------------------------------------- Chris@10: @node Plan execution in Fortran, Allocating aligned memory in Fortran, FFTW Fortran type reference, Calling FFTW from Modern Fortran Chris@10: @section Plan execution in Fortran Chris@10: Chris@10: In C, in order to use a plan, one normally calls @code{fftw_execute}, Chris@10: which executes the plan to perform the transform on the input/output Chris@10: arrays passed when the plan was created (@pxref{Using Plans}). The Chris@10: corresponding subroutine call in modern Fortran is: Chris@10: @example Chris@10: call fftw_execute(plan) Chris@10: @end example Chris@10: @findex fftw_execute Chris@10: Chris@10: However, we have had reports that this causes problems with some Chris@10: recent optimizing Fortran compilers. The problem is, because the Chris@10: input/output arrays are not passed as explicit arguments to Chris@10: @code{fftw_execute}, the semantics of Fortran (unlike C) allow the Chris@10: compiler to assume that the input/output arrays are not changed by Chris@10: @code{fftw_execute}. As a consequence, certain compilers end up Chris@10: repositioning the call to @code{fftw_execute}, assuming incorrectly Chris@10: that it does nothing to the arrays. Chris@10: Chris@10: There are various workarounds to this, but the safest and simplest Chris@10: thing is to not use @code{fftw_execute} in Fortran. Instead, use the Chris@10: functions described in @ref{New-array Execute Functions}, which take Chris@10: the input/output arrays as explicit arguments. For example, if the Chris@10: plan is for a complex-data DFT and was created for the arrays Chris@10: @code{in} and @code{out}, you would do: Chris@10: @example Chris@10: call fftw_execute_dft(plan, in, out) Chris@10: @end example Chris@10: @findex fftw_execute_dft Chris@10: Chris@10: There are a few things to be careful of, however: Chris@10: Chris@10: @itemize @bullet Chris@10: Chris@10: @item Chris@10: @findex fftw_execute_dft_r2c Chris@10: @findex fftw_execute_dft_c2r Chris@10: @findex fftw_execute_r2r Chris@10: You must use the correct type of execute function, matching the way Chris@10: the plan was created. Complex DFT plans should use Chris@10: @code{fftw_execute_dft}, Real-input (r2c) DFT plans should use use Chris@10: @code{fftw_execute_dft_r2c}, and real-output (c2r) DFT plans should Chris@10: use @code{fftw_execute_dft_c2r}. The various r2r plans should use Chris@10: @code{fftw_execute_r2r}. Fortunately, if you use the wrong one you Chris@10: will get a compile-time type-mismatch error (unlike legacy Fortran). Chris@10: Chris@10: @item Chris@10: You should normally pass the same input/output arrays that were used when Chris@10: creating the plan. This is always safe. Chris@10: Chris@10: @item Chris@10: @emph{If} you pass @emph{different} input/output arrays compared to Chris@10: those used when creating the plan, you must abide by all the Chris@10: restrictions of the new-array execute functions (@pxref{New-array Chris@10: Execute Functions}). The most tricky of these is the Chris@10: requirement that the new arrays have the same alignment as the Chris@10: original arrays; the best (and possibly only) way to guarantee this Chris@10: is to use the @samp{fftw_alloc} functions to allocate your arrays (@pxref{Allocating aligned memory in Fortran}). Alternatively, you can Chris@10: use the @code{FFTW_UNALIGNED} flag when creating the Chris@10: plan, in which case the plan does not depend on the alignment, but Chris@10: this may sacrifice substantial performance on architectures (like x86) Chris@10: with SIMD instructions (@pxref{SIMD alignment and fftw_malloc}). Chris@10: @ctindex FFTW_UNALIGNED Chris@10: Chris@10: @end itemize Chris@10: Chris@10: @c ------------------------------------------------------- Chris@10: @node Allocating aligned memory in Fortran, Accessing the wisdom API from Fortran, Plan execution in Fortran, Calling FFTW from Modern Fortran Chris@10: @section Allocating aligned memory in Fortran Chris@10: Chris@10: @cindex alignment Chris@10: @findex fftw_alloc_real Chris@10: @findex fftw_alloc_complex Chris@10: In order to obtain maximum performance in FFTW, you should store your Chris@10: data in arrays that have been specially aligned in memory (@pxref{SIMD Chris@10: alignment and fftw_malloc}). Enforcing alignment also permits you to Chris@10: safely use the new-array execute functions (@pxref{New-array Execute Chris@10: Functions}) to apply a given plan to more than one pair of in/out Chris@10: arrays. Unfortunately, standard Fortran arrays do @emph{not} provide Chris@10: any alignment guarantees. The @emph{only} way to allocate aligned Chris@10: memory in standard Fortran is to allocate it with an external C Chris@10: function, like the @code{fftw_alloc_real} and Chris@10: @code{fftw_alloc_complex} functions. Fortunately, Fortran 2003 provides Chris@10: a simple way to associate such allocated memory with a standard Fortran Chris@10: array pointer that you can then use normally. Chris@10: Chris@10: We therefore recommend allocating all your input/output arrays using Chris@10: the following technique: Chris@10: Chris@10: @enumerate Chris@10: Chris@10: @item Chris@10: Declare a @code{pointer}, @code{arr}, to your array of the desired type Chris@10: and dimensions. For example, @code{real(C_DOUBLE), pointer :: a(:,:)} Chris@10: for a 2d real array, or @code{complex(C_DOUBLE_COMPLEX), pointer :: Chris@10: a(:,:,:)} for a 3d complex array. Chris@10: Chris@10: @item Chris@10: The number of elements to allocate must be an Chris@10: @code{integer(C_SIZE_T)}. You can either declare a variable of this Chris@10: type, e.g. @code{integer(C_SIZE_T) :: sz}, to store the number of Chris@10: elements to allocate, or you can use the @code{int(..., C_SIZE_T)} Chris@10: intrinsic function. e.g. set @code{sz = L * M * N} or use Chris@10: @code{int(L * M * N, C_SIZE_T)} for an @threedims{L,M,N} array. Chris@10: Chris@10: @item Chris@10: Declare a @code{type(C_PTR) :: p} to hold the return value from Chris@10: 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@10: Chris@10: @item Chris@10: @findex c_f_pointer Chris@10: Associate your pointer @code{arr} with the allocated memory @code{p} Chris@10: using the standard @code{c_f_pointer} subroutine: @code{call Chris@10: c_f_pointer(p, arr, [...dimensions...])}, where Chris@10: @code{[...dimensions...])} are an array of the dimensions of the array Chris@10: (in the usual Fortran order). e.g. @code{call c_f_pointer(p, arr, Chris@10: [L,M,N])} for an @threedims{L,M,N} array. (Alternatively, you can Chris@10: omit the dimensions argument if you specified the shape explicitly Chris@10: when declaring @code{arr}.) You can now use @code{arr} as a usual Chris@10: multidimensional array. Chris@10: Chris@10: @item Chris@10: When you are done using the array, deallocate the memory by @code{call Chris@10: fftw_free(p)} on @code{p}. Chris@10: Chris@10: @end enumerate Chris@10: Chris@10: For example, here is how we would allocate an @twodims{L,M} 2d real array: Chris@10: Chris@10: @example Chris@10: real(C_DOUBLE), pointer :: arr(:,:) Chris@10: type(C_PTR) :: p Chris@10: p = fftw_alloc_real(int(L * M, C_SIZE_T)) Chris@10: call c_f_pointer(p, arr, [L,M]) Chris@10: @emph{...use arr and arr(i,j) as usual...} Chris@10: call fftw_free(p) Chris@10: @end example Chris@10: Chris@10: and here is an @threedims{L,M,N} 3d complex array: Chris@10: Chris@10: @example Chris@10: complex(C_DOUBLE_COMPLEX), pointer :: arr(:,:,:) Chris@10: type(C_PTR) :: p Chris@10: p = fftw_alloc_complex(int(L * M * N, C_SIZE_T)) Chris@10: call c_f_pointer(p, arr, [L,M,N]) Chris@10: @emph{...use arr and arr(i,j,k) as usual...} Chris@10: call fftw_free(p) Chris@10: @end example Chris@10: Chris@10: See @ref{Reversing array dimensions} for an example allocating a Chris@10: single array and associating both real and complex array pointers with Chris@10: it, for in-place real-to-complex transforms. Chris@10: Chris@10: @c ------------------------------------------------------- Chris@10: @node Accessing the wisdom API from Fortran, Defining an FFTW module, Allocating aligned memory in Fortran, Calling FFTW from Modern Fortran Chris@10: @section Accessing the wisdom API from Fortran Chris@10: @cindex wisdom Chris@10: @cindex saving plans to disk Chris@10: Chris@10: As explained in @ref{Words of Wisdom-Saving Plans}, FFTW provides a Chris@10: ``wisdom'' API for saving plans to disk so that they can be recreated Chris@10: quickly. The C API for exporting (@pxref{Wisdom Export}) and Chris@10: importing (@pxref{Wisdom Import}) wisdom is somewhat tricky to use Chris@10: from Fortran, however, because of differences in file I/O and string Chris@10: types between C and Fortran. Chris@10: Chris@10: @menu Chris@10: * Wisdom File Export/Import from Fortran:: Chris@10: * Wisdom String Export/Import from Fortran:: Chris@10: * Wisdom Generic Export/Import from Fortran:: Chris@10: @end menu Chris@10: Chris@10: @c =========> Chris@10: @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@10: @subsection Wisdom File Export/Import from Fortran Chris@10: Chris@10: @findex fftw_import wisdom_from_filename Chris@10: @findex fftw_export_wisdom_to_filename Chris@10: The easiest way to export and import wisdom is to do so using Chris@10: @code{fftw_export_wisdom_to_filename} and Chris@10: @code{fftw_wisdom_from_filename}. The only trick is that these Chris@10: require you to pass a C string, which is an array of type Chris@10: @code{CHARACTER(C_CHAR)} that is terminated by @code{C_NULL_CHAR}. Chris@10: You can call them like this: Chris@10: Chris@10: @example Chris@10: integer(C_INT) :: ret Chris@10: ret = fftw_export_wisdom_to_filename(C_CHAR_'my_wisdom.dat' // C_NULL_CHAR) Chris@10: if (ret .eq. 0) stop 'error exporting wisdom to file' Chris@10: ret = fftw_import_wisdom_from_filename(C_CHAR_'my_wisdom.dat' // C_NULL_CHAR) Chris@10: if (ret .eq. 0) stop 'error importing wisdom from file' Chris@10: @end example Chris@10: Chris@10: Note that prepending @samp{C_CHAR_} is needed to specify that the Chris@10: literal string is of kind @code{C_CHAR}, and we null-terminate the Chris@10: string by appending @samp{// C_NULL_CHAR}. These functions return an Chris@10: @code{integer(C_INT)} (@code{ret}) which is @code{0} if an error Chris@10: occurred during export/import and nonzero otherwise. Chris@10: Chris@10: It is also possible to use the lower-level routines Chris@10: @code{fftw_export_wisdom_to_file} and Chris@10: @code{fftw_import_wisdom_from_file}, which accept parameters of the C Chris@10: type @code{FILE*}, expressed in Fortran as @code{type(C_PTR)}. Chris@10: However, you are then responsible for creating the @code{FILE*} Chris@10: yourself. You can do this by using @code{iso_c_binding} to define Chris@10: Fortran intefaces for the C library functions @code{fopen} and Chris@10: @code{fclose}, which is a bit strange in Fortran but workable. Chris@10: Chris@10: @c =========> Chris@10: @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@10: @subsection Wisdom String Export/Import from Fortran Chris@10: Chris@10: @findex fftw_export_wisdom_to_string Chris@10: Dealing with FFTW's C string export/import is a bit more painful. In Chris@10: particular, the @code{fftw_export_wisdom_to_string} function requires Chris@10: you to deal with a dynamically allocated C string. To get its length, Chris@10: you must define an interface to the C @code{strlen} function, and to Chris@10: deallocate it you must define an interface to C @code{free}: Chris@10: Chris@10: @example Chris@10: use, intrinsic :: iso_c_binding Chris@10: interface Chris@10: integer(C_INT) function strlen(s) bind(C, name='strlen') Chris@10: import Chris@10: type(C_PTR), value :: s Chris@10: end function strlen Chris@10: subroutine free(p) bind(C, name='free') Chris@10: import Chris@10: type(C_PTR), value :: p Chris@10: end subroutine free Chris@10: end interface Chris@10: @end example Chris@10: Chris@10: Given these definitions, you can then export wisdom to a Fortran Chris@10: character array: Chris@10: Chris@10: @example Chris@10: character(C_CHAR), pointer :: s(:) Chris@10: integer(C_SIZE_T) :: slen Chris@10: type(C_PTR) :: p Chris@10: p = fftw_export_wisdom_to_string() Chris@10: if (.not. c_associated(p)) stop 'error exporting wisdom' Chris@10: slen = strlen(p) Chris@10: call c_f_pointer(p, s, [slen+1]) Chris@10: ... Chris@10: call free(p) Chris@10: @end example Chris@10: @findex c_associated Chris@10: @findex c_f_pointer Chris@10: Chris@10: Note that @code{slen} is the length of the C string, but the length of Chris@10: the array is @code{slen+1} because it includes the terminating null Chris@10: character. (You can omit the @samp{+1} if you don't want Fortran to Chris@10: know about the null character.) The standard @code{c_associated} function Chris@10: checks whether @code{p} is a null pointer, which is returned by Chris@10: @code{fftw_export_wisdom_to_string} if there was an error. Chris@10: Chris@10: @findex fftw_import_wisdom_from_string Chris@10: To import wisdom from a string, use Chris@10: @code{fftw_import_wisdom_from_string} as usual; note that the argument Chris@10: of this function must be a @code{character(C_CHAR)} that is terminated Chris@10: by the @code{C_NULL_CHAR} character, like the @code{s} array above. Chris@10: Chris@10: @c =========> Chris@10: @node Wisdom Generic Export/Import from Fortran, , Wisdom String Export/Import from Fortran, Accessing the wisdom API from Fortran Chris@10: @subsection Wisdom Generic Export/Import from Fortran Chris@10: Chris@10: The most generic wisdom export/import functions allow you to provide Chris@10: an arbitrary callback function to read/write one character at a time Chris@10: in any way you want. However, your callback function must be written Chris@10: in a special way, using the @code{bind(C)} attribute to be passed to a Chris@10: C interface. Chris@10: Chris@10: @findex fftw_export_wisdom Chris@10: In particular, to call the generic wisdom export function Chris@10: @code{fftw_export_wisdom}, you would write a callback subroutine of the form: Chris@10: Chris@10: @example Chris@10: subroutine my_write_char(c, p) bind(C) Chris@10: use, intrinsic :: iso_c_binding Chris@10: character(C_CHAR), value :: c Chris@10: type(C_PTR), value :: p Chris@10: @emph{...write c...} Chris@10: end subroutine my_write_char Chris@10: @end example Chris@10: Chris@10: Given such a subroutine (along with the corresponding interface definition), you could then export wisdom using: Chris@10: Chris@10: @findex c_funloc Chris@10: @example Chris@10: call fftw_export_wisdom(c_funloc(my_write_char), p) Chris@10: @end example Chris@10: Chris@10: @findex c_loc Chris@10: @findex c_f_pointer Chris@10: The standard @code{c_funloc} intrinsic converts a Fortran Chris@10: @code{bind(C)} subroutine into a C function pointer. The parameter Chris@10: @code{p} is a @code{type(C_PTR)} to any arbitrary data that you want Chris@10: to pass to @code{my_write_char} (or @code{C_NULL_PTR} if none). (Note Chris@10: that you can get a C pointer to Fortran data using the intrinsic Chris@10: @code{c_loc}, and convert it back to a Fortran pointer in Chris@10: @code{my_write_char} using @code{c_f_pointer}.) Chris@10: Chris@10: Similarly, to use the generic @code{fftw_import_wisdom}, you would Chris@10: define a callback function of the form: Chris@10: Chris@10: @findex fftw_import_wisdom Chris@10: @example Chris@10: integer(C_INT) function my_read_char(p) bind(C) Chris@10: use, intrinsic :: iso_c_binding Chris@10: type(C_PTR), value :: p Chris@10: character :: c Chris@10: @emph{...read a character c...} Chris@10: my_read_char = ichar(c, C_INT) Chris@10: end function my_read_char Chris@10: Chris@10: .... Chris@10: Chris@10: integer(C_INT) :: ret Chris@10: ret = fftw_import_wisdom(c_funloc(my_read_char), p) Chris@10: if (ret .eq. 0) stop 'error importing wisdom' Chris@10: @end example Chris@10: Chris@10: Your function can return @code{-1} if the end of the input is reached. Chris@10: Again, @code{p} is an arbitrary @code{type(C_PTR} that is passed Chris@10: through to your function. @code{fftw_import_wisdom} returns @code{0} Chris@10: if an error occurred and nonzero otherwise. Chris@10: Chris@10: @c ------------------------------------------------------- Chris@10: @node Defining an FFTW module, , Accessing the wisdom API from Fortran, Calling FFTW from Modern Fortran Chris@10: @section Defining an FFTW module Chris@10: Chris@10: Rather than using the @code{include} statement to include the Chris@10: @code{fftw3.f03} interface file in any subroutine where you want to Chris@10: use FFTW, you might prefer to define an FFTW Fortran module. FFTW Chris@10: does not install itself as a module, primarily because Chris@10: @code{fftw3.f03} can be shared between different Fortran compilers while Chris@10: modules (in general) cannot. However, it is trivial to define your Chris@10: own FFTW module if you want. Just create a file containing: Chris@10: Chris@10: @example Chris@10: module FFTW3 Chris@10: use, intrinsic :: iso_c_binding Chris@10: include 'fftw3.f03' Chris@10: end module Chris@10: @end example Chris@10: Chris@10: Compile this file into a module as usual for your compiler (e.g. with Chris@10: @code{gfortran -c} you will get a file @code{fftw3.mod}). Now, Chris@10: instead of @code{include 'fftw3.f03'}, whenever you want to use FFTW Chris@10: routines you can just do: Chris@10: Chris@10: @example Chris@10: use FFTW3 Chris@10: @end example Chris@10: Chris@10: as usual for Fortran modules. (You still need to link to the FFTW Chris@10: library, of course.)