cannam@95: cannam@95: cannam@95: FFTW MPI Fortran Interface - FFTW 3.3.3 cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95: cannam@95:
cannam@95: cannam@95:

cannam@95: Previous: FFTW MPI Reference, cannam@95: Up: Distributed-memory FFTW with MPI cannam@95:


cannam@95:
cannam@95: cannam@95:

6.13 FFTW MPI Fortran Interface

cannam@95: cannam@95:

cannam@95: The FFTW MPI interface is callable from modern Fortran compilers cannam@95: supporting the Fortran 2003 iso_c_binding standard for calling cannam@95: C functions. As described in Calling FFTW from Modern Fortran, cannam@95: this means that you can directly call FFTW's C interface from Fortran cannam@95: with only minor changes in syntax. There are, however, a few things cannam@95: specific to the MPI interface to keep in mind: cannam@95: cannam@95:

cannam@95: cannam@95:

For example, here is a Fortran code snippet to perform a distributed cannam@95: L × M complex DFT in-place. (This assumes you have already cannam@95: initialized MPI with MPI_init and have also performed cannam@95: call fftw_mpi_init.) cannam@95: cannam@95:

       use, intrinsic :: iso_c_binding
cannam@95:        include 'fftw3-mpi.f03'
cannam@95:        integer(C_INTPTR_T), parameter :: L = ...
cannam@95:        integer(C_INTPTR_T), parameter :: M = ...
cannam@95:        type(C_PTR) :: plan, cdata
cannam@95:        complex(C_DOUBLE_COMPLEX), pointer :: data(:,:)
cannam@95:        integer(C_INTPTR_T) :: i, j, alloc_local, local_M, local_j_offset
cannam@95:      
cannam@95:      !   get local data size and allocate (note dimension reversal)
cannam@95:        alloc_local = fftw_mpi_local_size_2d(M, L, MPI_COMM_WORLD, &
cannam@95:                                             local_M, local_j_offset)
cannam@95:        cdata = fftw_alloc_complex(alloc_local)
cannam@95:        call c_f_pointer(cdata, data, [L,local_M])
cannam@95:      
cannam@95:      !   create MPI plan for in-place forward DFT (note dimension reversal)
cannam@95:        plan = fftw_mpi_plan_dft_2d(M, L, data, data, MPI_COMM_WORLD, &
cannam@95:                                    FFTW_FORWARD, FFTW_MEASURE)
cannam@95:      
cannam@95:      ! initialize data to some function my_function(i,j)
cannam@95:        do j = 1, local_M
cannam@95:          do i = 1, L
cannam@95:            data(i, j) = my_function(i, j + local_j_offset)
cannam@95:          end do
cannam@95:        end do
cannam@95:      
cannam@95:      ! compute transform (as many times as desired)
cannam@95:        call fftw_mpi_execute_dft(plan, data, data)
cannam@95:      
cannam@95:        call fftw_destroy_plan(plan)
cannam@95:        call fftw_free(cdata)
cannam@95: 
cannam@95:

Note that when we called fftw_mpi_local_size_2d and cannam@95: fftw_mpi_plan_dft_2d with the dimensions in reversed order, cannam@95: since a L × M Fortran array is viewed by FFTW in C as a cannam@95: M × L array. This means that the array was distributed over cannam@95: the M dimension, the local portion of which is a cannam@95: L × local_M array in Fortran. (You must not use an cannam@95: allocate statement to allocate an L × local_M array, cannam@95: however; you must allocate alloc_local complex numbers, which cannam@95: may be greater than L * local_M, in order to reserve space for cannam@95: intermediate steps of the transform.) Finally, we mention that cannam@95: because C's array indices are zero-based, the local_j_offset cannam@95: argument can conveniently be interpreted as an offset in the 1-based cannam@95: j index (rather than as a starting index as in C). cannam@95: cannam@95:

If instead you had used the ior(FFTW_MEASURE, cannam@95: FFTW_MPI_TRANSPOSED_OUT) flag, the output of the transform would be a cannam@95: transposed M × local_L array, associated with the same cannam@95: cdata allocation (since the transform is in-place), and which cannam@95: you could declare with: cannam@95: cannam@95:

       complex(C_DOUBLE_COMPLEX), pointer :: tdata(:,:)
cannam@95:        ...
cannam@95:        call c_f_pointer(cdata, tdata, [M,local_L])
cannam@95: 
cannam@95:

where local_L would have been obtained by changing the cannam@95: fftw_mpi_local_size_2d call to: cannam@95: cannam@95:

       alloc_local = fftw_mpi_local_size_2d_transposed(M, L, MPI_COMM_WORLD, &
cannam@95:                                 local_M, local_j_offset, local_L, local_i_offset)
cannam@95: 
cannam@95:
cannam@95:
cannam@95:

Footnotes

[1] Technically, this is because you aren't cannam@95: actually calling the C functions directly. You are calling wrapper cannam@95: functions that translate the communicator with MPI_Comm_f2c cannam@95: before calling the ordinary C interface. This is all done cannam@95: transparently, however, since the fftw3-mpi.f03 interface file cannam@95: renames the wrappers so that they are called in Fortran with the same cannam@95: names as the C interface functions.

cannam@95: cannam@95:
cannam@95: cannam@95: cannam@95: