Chris@10: Chris@10: Chris@10: 2d MPI example - 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:

Chris@10: Next: , Chris@10: Previous: Linking and Initializing MPI FFTW, Chris@10: Up: Distributed-memory FFTW with MPI Chris@10:


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

6.3 2d MPI example

Chris@10: Chris@10:

Before we document the FFTW MPI interface in detail, we begin with a Chris@10: simple example outlining how one would perform a two-dimensional Chris@10: N0 by N1 complex DFT. Chris@10: Chris@10:

     #include <fftw3-mpi.h>
Chris@10:      
Chris@10:      int main(int argc, char **argv)
Chris@10:      {
Chris@10:          const ptrdiff_t N0 = ..., N1 = ...;
Chris@10:          fftw_plan plan;
Chris@10:          fftw_complex *data;
Chris@10:          ptrdiff_t alloc_local, local_n0, local_0_start, i, j;
Chris@10:      
Chris@10:          MPI_Init(&argc, &argv);
Chris@10:          fftw_mpi_init();
Chris@10:      
Chris@10:          /* get local data size and allocate */
Chris@10:          alloc_local = fftw_mpi_local_size_2d(N0, N1, MPI_COMM_WORLD,
Chris@10:                                               &local_n0, &local_0_start);
Chris@10:          data = fftw_alloc_complex(alloc_local);
Chris@10:      
Chris@10:          /* create plan for in-place forward DFT */
Chris@10:          plan = fftw_mpi_plan_dft_2d(N0, N1, data, data, MPI_COMM_WORLD,
Chris@10:                                      FFTW_FORWARD, FFTW_ESTIMATE);
Chris@10:      
Chris@10:          /* initialize data to some function my_function(x,y) */
Chris@10:          for (i = 0; i < local_n0; ++i) for (j = 0; j < N1; ++j)
Chris@10:             data[i*N1 + j] = my_function(local_0_start + i, j);
Chris@10:      
Chris@10:          /* compute transforms, in-place, as many times as desired */
Chris@10:          fftw_execute(plan);
Chris@10:      
Chris@10:          fftw_destroy_plan(plan);
Chris@10:      
Chris@10:          MPI_Finalize();
Chris@10:      }
Chris@10: 
Chris@10:

As can be seen above, the MPI interface follows the same basic style Chris@10: of allocate/plan/execute/destroy as the serial FFTW routines. All of Chris@10: the MPI-specific routines are prefixed with ‘fftw_mpi_’ instead Chris@10: of ‘fftw_’. There are a few important differences, however: Chris@10: Chris@10:

First, we must call fftw_mpi_init() after calling Chris@10: MPI_Init (required in all MPI programs) and before calling any Chris@10: other ‘fftw_mpi_’ routine. Chris@10: Chris@10: Chris@10:

Second, when we create the plan with fftw_mpi_plan_dft_2d, Chris@10: analogous to fftw_plan_dft_2d, we pass an additional argument: Chris@10: the communicator, indicating which processes will participate in the Chris@10: transform (here MPI_COMM_WORLD, indicating all processes). Chris@10: Whenever you create, execute, or destroy a plan for an MPI transform, Chris@10: you must call the corresponding FFTW routine on all processes Chris@10: in the communicator for that transform. (That is, these are Chris@10: collective calls.) Note that the plan for the MPI transform Chris@10: uses the standard fftw_execute and fftw_destroy routines Chris@10: (on the other hand, there are MPI-specific new-array execute functions Chris@10: documented below). Chris@10: Chris@10: Chris@10:

Third, all of the FFTW MPI routines take ptrdiff_t arguments Chris@10: instead of int as for the serial FFTW. ptrdiff_t is a Chris@10: standard C integer type which is (at least) 32 bits wide on a 32-bit Chris@10: machine and 64 bits wide on a 64-bit machine. This is to make it easy Chris@10: to specify very large parallel transforms on a 64-bit machine. (You Chris@10: can specify 64-bit transform sizes in the serial FFTW, too, but only Chris@10: by using the ‘guru64’ planner interface. See 64-bit Guru Interface.) Chris@10: Chris@10: Chris@10:

Fourth, and most importantly, you don't allocate the entire Chris@10: two-dimensional array on each process. Instead, you call Chris@10: fftw_mpi_local_size_2d to find out what portion of the Chris@10: array resides on each processor, and how much space to allocate. Chris@10: Here, the portion of the array on each process is a local_n0 by Chris@10: N1 slice of the total array, starting at index Chris@10: local_0_start. The total number of fftw_complex numbers Chris@10: to allocate is given by the alloc_local return value, which Chris@10: may be greater than local_n0 * N1 (in case some Chris@10: intermediate calculations require additional storage). The data Chris@10: distribution in FFTW's MPI interface is described in more detail by Chris@10: the next section. Chris@10: Chris@10: Chris@10:

Given the portion of the array that resides on the local process, it Chris@10: is straightforward to initialize the data (here to a function Chris@10: myfunction) and otherwise manipulate it. Of course, at the end Chris@10: of the program you may want to output the data somehow, but Chris@10: synchronizing this output is up to you and is beyond the scope of this Chris@10: manual. (One good way to output a large multi-dimensional distributed Chris@10: array in MPI to a portable binary file is to use the free HDF5 Chris@10: library; see the HDF home page.) Chris@10: Chris@10: Chris@10: Chris@10: Chris@10: