Chris@10: Chris@10: Chris@10: FFTW MPI Fortran Interface - FFTW 3.3.3 Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: Chris@10:
Chris@10: Chris@10:

Chris@10: Previous: FFTW MPI Reference, Chris@10: Up: Distributed-memory FFTW with MPI Chris@10:


Chris@10:
Chris@10: Chris@10:

6.13 FFTW MPI Fortran Interface

Chris@10: Chris@10:

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

Chris@10: Chris@10:

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

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

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

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

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

where local_L would have been obtained by changing the Chris@10: fftw_mpi_local_size_2d call to: Chris@10: Chris@10:

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

Footnotes

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

Chris@10: Chris@10:
Chris@10: Chris@10: Chris@10: