cannam@167: cannam@167: cannam@167: cannam@167: cannam@167:
cannam@167:cannam@167: Previous: FFTW MPI Reference, Up: Distributed-memory FFTW with MPI [Contents][Index]
cannam@167:The FFTW MPI interface is callable from modern Fortran compilers
cannam@167: supporting the Fortran 2003 iso_c_binding
standard for calling
cannam@167: C functions. As described in Calling FFTW from Modern Fortran,
cannam@167: this means that you can directly call FFTW’s C interface from Fortran
cannam@167: with only minor changes in syntax. There are, however, a few things
cannam@167: specific to the MPI interface to keep in mind:
cannam@167:
fftw3.f03
as in Overview of Fortran interface, you should include 'fftw3-mpi.f03'
(after
cannam@167: use, intrinsic :: iso_c_binding
as before). The
cannam@167: fftw3-mpi.f03
file includes fftw3.f03
, so you should
cannam@167: not include
them both yourself. (You will also want to
cannam@167: include the MPI header file, usually via include 'mpif.h'
or
cannam@167: similar, although though this is not needed by fftw3-mpi.f03
cannam@167: per se.) (To use the ‘fftwl_’ long double
extended-precision routines in supporting compilers, you should include fftw3f-mpi.f03
in addition to fftw3-mpi.f03
. See Extended and quadruple precision in Fortran.)
cannam@167:
cannam@167: integer
types; there is
cannam@167: no MPI_Comm
type, nor is there any way to access a C
cannam@167: MPI_Comm
. Fortunately, this is taken care of for you by the
cannam@167: FFTW Fortran interface: whenever the C interface expects an
cannam@167: MPI_Comm
type, you should pass the Fortran communicator as an
cannam@167: integer
.8
cannam@167:
cannam@167: ptrdiff_t
in C, you should use integer(C_INTPTR_T)
in
cannam@167: Fortran (see FFTW Fortran type reference).
cannam@167:
cannam@167: fftw_execute_dft
becomes fftw_mpi_execute_dft
,
cannam@167: etcetera. See Using MPI Plans.
cannam@167:
cannam@167: For example, here is a Fortran code snippet to perform a distributed
cannam@167: L × M
cannam@167: complex DFT in-place. (This assumes you have already
cannam@167: initialized MPI with MPI_init
and have also performed
cannam@167: call fftw_mpi_init
.)
cannam@167:
use, intrinsic :: iso_c_binding cannam@167: include 'fftw3-mpi.f03' cannam@167: integer(C_INTPTR_T), parameter :: L = ... cannam@167: integer(C_INTPTR_T), parameter :: M = ... cannam@167: type(C_PTR) :: plan, cdata cannam@167: complex(C_DOUBLE_COMPLEX), pointer :: data(:,:) cannam@167: integer(C_INTPTR_T) :: i, j, alloc_local, local_M, local_j_offset cannam@167: cannam@167: ! get local data size and allocate (note dimension reversal) cannam@167: alloc_local = fftw_mpi_local_size_2d(M, L, MPI_COMM_WORLD, & cannam@167: local_M, local_j_offset) cannam@167: cdata = fftw_alloc_complex(alloc_local) cannam@167: call c_f_pointer(cdata, data, [L,local_M]) cannam@167: cannam@167: ! create MPI plan for in-place forward DFT (note dimension reversal) cannam@167: plan = fftw_mpi_plan_dft_2d(M, L, data, data, MPI_COMM_WORLD, & cannam@167: FFTW_FORWARD, FFTW_MEASURE) cannam@167: cannam@167: ! initialize data to some function my_function(i,j) cannam@167: do j = 1, local_M cannam@167: do i = 1, L cannam@167: data(i, j) = my_function(i, j + local_j_offset) cannam@167: end do cannam@167: end do cannam@167: cannam@167: ! compute transform (as many times as desired) cannam@167: call fftw_mpi_execute_dft(plan, data, data) cannam@167: cannam@167: call fftw_destroy_plan(plan) cannam@167: call fftw_free(cdata) cannam@167:
Note that when we called fftw_mpi_local_size_2d
and
cannam@167: fftw_mpi_plan_dft_2d
with the dimensions in reversed order,
cannam@167: since a L × M
cannam@167: Fortran array is viewed by FFTW in C as a
cannam@167: M × L
cannam@167: array. This means that the array was distributed over
cannam@167: the M
dimension, the local portion of which is a
cannam@167: L × local_M
cannam@167: array in Fortran. (You must not use an
cannam@167: allocate
statement to allocate an L × local_M
cannam@167: array,
cannam@167: however; you must allocate alloc_local
complex numbers, which
cannam@167: may be greater than L * local_M
, in order to reserve space for
cannam@167: intermediate steps of the transform.) Finally, we mention that
cannam@167: because C’s array indices are zero-based, the local_j_offset
cannam@167: argument can conveniently be interpreted as an offset in the 1-based
cannam@167: j
index (rather than as a starting index as in C).
cannam@167:
If instead you had used the ior(FFTW_MEASURE,
cannam@167: FFTW_MPI_TRANSPOSED_OUT)
flag, the output of the transform would be a
cannam@167: transposed M × local_L
cannam@167: array, associated with the same
cannam@167: cdata
allocation (since the transform is in-place), and which
cannam@167: you could declare with:
cannam@167:
complex(C_DOUBLE_COMPLEX), pointer :: tdata(:,:) cannam@167: ... cannam@167: call c_f_pointer(cdata, tdata, [M,local_L]) cannam@167:
where local_L
would have been obtained by changing the
cannam@167: fftw_mpi_local_size_2d
call to:
cannam@167:
alloc_local = fftw_mpi_local_size_2d_transposed(M, L, MPI_COMM_WORLD, & cannam@167: local_M, local_j_offset, local_L, local_i_offset) cannam@167:
Technically, this is because you aren’t
cannam@167: actually calling the C functions directly. You are calling wrapper
cannam@167: functions that translate the communicator with MPI_Comm_f2c
cannam@167: before calling the ordinary C interface. This is all done
cannam@167: transparently, however, since the fftw3-mpi.f03
interface file
cannam@167: renames the wrappers so that they are called in Fortran with the same
cannam@167: names as the C interface functions.
cannam@167: Previous: FFTW MPI Reference, Up: Distributed-memory FFTW with MPI [Contents][Index]
cannam@167: