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