Chris@10: @node Distributed-memory FFTW with MPI, Calling FFTW from Modern Fortran, Multi-threaded FFTW, Top Chris@10: @chapter Distributed-memory FFTW with MPI Chris@10: @cindex MPI Chris@10: Chris@10: @cindex parallel transform Chris@10: In this chapter we document the parallel FFTW routines for parallel Chris@10: systems supporting the MPI message-passing interface. Unlike the Chris@10: shared-memory threads described in the previous chapter, MPI allows Chris@10: you to use @emph{distributed-memory} parallelism, where each CPU has Chris@10: its own separate memory, and which can scale up to clusters of many Chris@10: thousands of processors. This capability comes at a price, however: Chris@10: each process only stores a @emph{portion} of the data to be Chris@10: transformed, which means that the data structures and Chris@10: programming-interface are quite different from the serial or threads Chris@10: versions of FFTW. Chris@10: @cindex data distribution Chris@10: Chris@10: Chris@10: Distributed-memory parallelism is especially useful when you are Chris@10: transforming arrays so large that they do not fit into the memory of a Chris@10: single processor. The storage per-process required by FFTW's MPI Chris@10: routines is proportional to the total array size divided by the number Chris@10: of processes. Conversely, distributed-memory parallelism can easily Chris@10: pose an unacceptably high communications overhead for small problems; Chris@10: the threshold problem size for which parallelism becomes advantageous Chris@10: will depend on the precise problem you are interested in, your Chris@10: hardware, and your MPI implementation. Chris@10: Chris@10: A note on terminology: in MPI, you divide the data among a set of Chris@10: ``processes'' which each run in their own memory address space. Chris@10: Generally, each process runs on a different physical processor, but Chris@10: this is not required. A set of processes in MPI is described by an Chris@10: opaque data structure called a ``communicator,'' the most common of Chris@10: which is the predefined communicator @code{MPI_COMM_WORLD} which Chris@10: refers to @emph{all} processes. For more information on these and Chris@10: other concepts common to all MPI programs, we refer the reader to the Chris@10: documentation at @uref{http://www.mcs.anl.gov/research/projects/mpi/, the MPI home Chris@10: page}. Chris@10: @cindex MPI communicator Chris@10: @ctindex MPI_COMM_WORLD Chris@10: Chris@10: Chris@10: We assume in this chapter that the reader is familiar with the usage Chris@10: of the serial (uniprocessor) FFTW, and focus only on the concepts new Chris@10: to the MPI interface. Chris@10: Chris@10: @menu Chris@10: * FFTW MPI Installation:: Chris@10: * Linking and Initializing MPI FFTW:: Chris@10: * 2d MPI example:: Chris@10: * MPI Data Distribution:: Chris@10: * Multi-dimensional MPI DFTs of Real Data:: Chris@10: * Other Multi-dimensional Real-data MPI Transforms:: Chris@10: * FFTW MPI Transposes:: Chris@10: * FFTW MPI Wisdom:: Chris@10: * Avoiding MPI Deadlocks:: Chris@10: * FFTW MPI Performance Tips:: Chris@10: * Combining MPI and Threads:: Chris@10: * FFTW MPI Reference:: Chris@10: * FFTW MPI Fortran Interface:: Chris@10: @end menu Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node FFTW MPI Installation, Linking and Initializing MPI FFTW, Distributed-memory FFTW with MPI, Distributed-memory FFTW with MPI Chris@10: @section FFTW MPI Installation Chris@10: Chris@10: All of the FFTW MPI code is located in the @code{mpi} subdirectory of Chris@10: the FFTW package. On Unix systems, the FFTW MPI libraries and header Chris@10: files are automatically configured, compiled, and installed along with Chris@10: the uniprocessor FFTW libraries simply by including Chris@10: @code{--enable-mpi} in the flags to the @code{configure} script Chris@10: (@pxref{Installation on Unix}). Chris@10: @fpindex configure Chris@10: Chris@10: Chris@10: Any implementation of the MPI standard, version 1 or later, should Chris@10: work with FFTW. The @code{configure} script will attempt to Chris@10: automatically detect how to compile and link code using your MPI Chris@10: implementation. In some cases, especially if you have multiple Chris@10: different MPI implementations installed or have an unusual MPI Chris@10: software package, you may need to provide this information explicitly. Chris@10: Chris@10: Most commonly, one compiles MPI code by invoking a special compiler Chris@10: command, typically @code{mpicc} for C code. The @code{configure} Chris@10: script knows the most common names for this command, but you can Chris@10: specify the MPI compilation command explicitly by setting the Chris@10: @code{MPICC} variable, as in @samp{./configure MPICC=mpicc ...}. Chris@10: @fpindex mpicc Chris@10: Chris@10: Chris@10: If, instead of a special compiler command, you need to link a certain Chris@10: library, you can specify the link command via the @code{MPILIBS} Chris@10: variable, as in @samp{./configure MPILIBS=-lmpi ...}. Note that if Chris@10: your MPI library is installed in a non-standard location (one the Chris@10: compiler does not know about by default), you may also have to specify Chris@10: the location of the library and header files via @code{LDFLAGS} and Chris@10: @code{CPPFLAGS} variables, respectively, as in @samp{./configure Chris@10: LDFLAGS=-L/path/to/mpi/libs CPPFLAGS=-I/path/to/mpi/include ...}. Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node Linking and Initializing MPI FFTW, 2d MPI example, FFTW MPI Installation, Distributed-memory FFTW with MPI Chris@10: @section Linking and Initializing MPI FFTW Chris@10: Chris@10: Programs using the MPI FFTW routines should be linked with Chris@10: @code{-lfftw3_mpi -lfftw3 -lm} on Unix in double precision, Chris@10: @code{-lfftw3f_mpi -lfftw3f -lm} in single precision, and so on Chris@10: (@pxref{Precision}). You will also need to link with whatever library Chris@10: is responsible for MPI on your system; in most MPI implementations, Chris@10: there is a special compiler alias named @code{mpicc} to compile and Chris@10: link MPI code. Chris@10: @fpindex mpicc Chris@10: @cindex linking on Unix Chris@10: @cindex precision Chris@10: Chris@10: Chris@10: @findex fftw_init_threads Chris@10: Before calling any FFTW routines except possibly Chris@10: @code{fftw_init_threads} (@pxref{Combining MPI and Threads}), but after calling Chris@10: @code{MPI_Init}, you should call the function: Chris@10: Chris@10: @example Chris@10: void fftw_mpi_init(void); Chris@10: @end example Chris@10: @findex fftw_mpi_init Chris@10: Chris@10: If, at the end of your program, you want to get rid of all memory and Chris@10: other resources allocated internally by FFTW, for both the serial and Chris@10: MPI routines, you can call: Chris@10: Chris@10: @example Chris@10: void fftw_mpi_cleanup(void); Chris@10: @end example Chris@10: @findex fftw_mpi_cleanup Chris@10: Chris@10: which is much like the @code{fftw_cleanup()} function except that it Chris@10: also gets rid of FFTW's MPI-related data. You must @emph{not} execute Chris@10: any previously created plans after calling this function. Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node 2d MPI example, MPI Data Distribution, Linking and Initializing MPI FFTW, Distributed-memory FFTW with MPI Chris@10: @section 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: @code{N0} by @code{N1} complex DFT. Chris@10: Chris@10: @example Chris@10: #include 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: /* @r{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: /* @r{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: /* @r{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: /* @r{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: @end example 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 @samp{fftw_mpi_} instead Chris@10: of @samp{fftw_}. There are a few important differences, however: Chris@10: Chris@10: First, we must call @code{fftw_mpi_init()} after calling Chris@10: @code{MPI_Init} (required in all MPI programs) and before calling any Chris@10: other @samp{fftw_mpi_} routine. Chris@10: @findex MPI_Init Chris@10: @findex fftw_mpi_init Chris@10: Chris@10: Chris@10: Second, when we create the plan with @code{fftw_mpi_plan_dft_2d}, Chris@10: analogous to @code{fftw_plan_dft_2d}, we pass an additional argument: Chris@10: the communicator, indicating which processes will participate in the Chris@10: transform (here @code{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 @emph{all} processes Chris@10: in the communicator for that transform. (That is, these are Chris@10: @emph{collective} calls.) Note that the plan for the MPI transform Chris@10: uses the standard @code{fftw_execute} and @code{fftw_destroy} routines Chris@10: (on the other hand, there are MPI-specific new-array execute functions Chris@10: documented below). Chris@10: @cindex collective function Chris@10: @findex fftw_mpi_plan_dft_2d Chris@10: @ctindex MPI_COMM_WORLD Chris@10: Chris@10: Chris@10: Third, all of the FFTW MPI routines take @code{ptrdiff_t} arguments Chris@10: instead of @code{int} as for the serial FFTW. @code{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 @samp{guru64} planner interface. @xref{64-bit Guru Chris@10: Interface}.) Chris@10: @tindex ptrdiff_t Chris@10: @cindex 64-bit architecture 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: @code{fftw_mpi_local_size_2d} to find out what @emph{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 @code{local_n0} by Chris@10: @code{N1} slice of the total array, starting at index Chris@10: @code{local_0_start}. The total number of @code{fftw_complex} numbers Chris@10: to allocate is given by the @code{alloc_local} return value, which Chris@10: @emph{may} be greater than @code{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: @findex fftw_mpi_local_size_2d Chris@10: @cindex data distribution 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: @code{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 @uref{http://www.hdfgroup.org/, HDF home page}.) Chris@10: @cindex HDF5 Chris@10: @cindex MPI I/O Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node MPI Data Distribution, Multi-dimensional MPI DFTs of Real Data, 2d MPI example, Distributed-memory FFTW with MPI Chris@10: @section MPI Data Distribution Chris@10: @cindex data distribution Chris@10: Chris@10: The most important concept to understand in using FFTW's MPI interface Chris@10: is the data distribution. With a serial or multithreaded FFT, all of Chris@10: the inputs and outputs are stored as a single contiguous chunk of Chris@10: memory. With a distributed-memory FFT, the inputs and outputs are Chris@10: broken into disjoint blocks, one per process. Chris@10: Chris@10: In particular, FFTW uses a @emph{1d block distribution} of the data, Chris@10: distributed along the @emph{first dimension}. For example, if you Chris@10: want to perform a @twodims{100,200} complex DFT, distributed over 4 Chris@10: processes, each process will get a @twodims{25,200} slice of the data. Chris@10: That is, process 0 will get rows 0 through 24, process 1 will get rows Chris@10: 25 through 49, process 2 will get rows 50 through 74, and process 3 Chris@10: will get rows 75 through 99. If you take the same array but Chris@10: distribute it over 3 processes, then it is not evenly divisible so the Chris@10: different processes will have unequal chunks. FFTW's default choice Chris@10: in this case is to assign 34 rows to processes 0 and 1, and 32 rows to Chris@10: process 2. Chris@10: @cindex block distribution Chris@10: Chris@10: Chris@10: FFTW provides several @samp{fftw_mpi_local_size} routines that you can Chris@10: call to find out what portion of an array is stored on the current Chris@10: process. In most cases, you should use the default block sizes picked Chris@10: by FFTW, but it is also possible to specify your own block size. For Chris@10: example, with a @twodims{100,200} array on three processes, you can Chris@10: tell FFTW to use a block size of 40, which would assign 40 rows to Chris@10: processes 0 and 1, and 20 rows to process 2. FFTW's default is to Chris@10: divide the data equally among the processes if possible, and as best Chris@10: it can otherwise. The rows are always assigned in ``rank order,'' Chris@10: i.e. process 0 gets the first block of rows, then process 1, and so Chris@10: on. (You can change this by using @code{MPI_Comm_split} to create a Chris@10: new communicator with re-ordered processes.) However, you should Chris@10: always call the @samp{fftw_mpi_local_size} routines, if possible, Chris@10: rather than trying to predict FFTW's distribution choices. Chris@10: Chris@10: In particular, it is critical that you allocate the storage size that Chris@10: is returned by @samp{fftw_mpi_local_size}, which is @emph{not} Chris@10: necessarily the size of the local slice of the array. The reason is Chris@10: that intermediate steps of FFTW's algorithms involve transposing the Chris@10: array and redistributing the data, so at these intermediate steps FFTW Chris@10: may require more local storage space (albeit always proportional to Chris@10: the total size divided by the number of processes). The Chris@10: @samp{fftw_mpi_local_size} functions know how much storage is required Chris@10: for these intermediate steps and tell you the correct amount to Chris@10: allocate. Chris@10: Chris@10: @menu Chris@10: * Basic and advanced distribution interfaces:: Chris@10: * Load balancing:: Chris@10: * Transposed distributions:: Chris@10: * One-dimensional distributions:: Chris@10: @end menu Chris@10: Chris@10: @node Basic and advanced distribution interfaces, Load balancing, MPI Data Distribution, MPI Data Distribution Chris@10: @subsection Basic and advanced distribution interfaces Chris@10: Chris@10: As with the planner interface, the @samp{fftw_mpi_local_size} Chris@10: distribution interface is broken into basic and advanced Chris@10: (@samp{_many}) interfaces, where the latter allows you to specify the Chris@10: block size manually and also to request block sizes when computing Chris@10: multiple transforms simultaneously. These functions are documented Chris@10: more exhaustively by the FFTW MPI Reference, but we summarize the Chris@10: basic ideas here using a couple of two-dimensional examples. Chris@10: Chris@10: For the @twodims{100,200} complex-DFT example, above, we would find Chris@10: the distribution by calling the following function in the basic Chris@10: interface: Chris@10: Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_2d(ptrdiff_t n0, ptrdiff_t n1, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start); Chris@10: @end example Chris@10: @findex fftw_mpi_local_size_2d Chris@10: Chris@10: Given the total size of the data to be transformed (here, @code{n0 = Chris@10: 100} and @code{n1 = 200}) and an MPI communicator (@code{comm}), this Chris@10: function provides three numbers. Chris@10: Chris@10: First, it describes the shape of the local data: the current process Chris@10: should store a @code{local_n0} by @code{n1} slice of the overall Chris@10: dataset, in row-major order (@code{n1} dimension contiguous), starting Chris@10: at index @code{local_0_start}. That is, if the total dataset is Chris@10: viewed as a @code{n0} by @code{n1} matrix, the current process should Chris@10: store the rows @code{local_0_start} to Chris@10: @code{local_0_start+local_n0-1}. Obviously, if you are running with Chris@10: only a single MPI process, that process will store the entire array: Chris@10: @code{local_0_start} will be zero and @code{local_n0} will be Chris@10: @code{n0}. @xref{Row-major Format}. Chris@10: @cindex row-major Chris@10: Chris@10: Chris@10: Second, the return value is the total number of data elements (e.g., Chris@10: complex numbers for a complex DFT) that should be allocated for the Chris@10: input and output arrays on the current process (ideally with Chris@10: @code{fftw_malloc} or an @samp{fftw_alloc} function, to ensure optimal Chris@10: alignment). It might seem that this should always be equal to Chris@10: @code{local_n0 * n1}, but this is @emph{not} the case. FFTW's Chris@10: distributed FFT algorithms require data redistributions at Chris@10: intermediate stages of the transform, and in some circumstances this Chris@10: may require slightly larger local storage. This is discussed in more Chris@10: detail below, under @ref{Load balancing}. Chris@10: @findex fftw_malloc Chris@10: @findex fftw_alloc_complex Chris@10: Chris@10: Chris@10: @cindex advanced interface Chris@10: The advanced-interface @samp{local_size} function for multidimensional Chris@10: transforms returns the same three things (@code{local_n0}, Chris@10: @code{local_0_start}, and the total number of elements to allocate), Chris@10: but takes more inputs: Chris@10: Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_many(int rnk, const ptrdiff_t *n, Chris@10: ptrdiff_t howmany, Chris@10: ptrdiff_t block0, Chris@10: MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, Chris@10: ptrdiff_t *local_0_start); Chris@10: @end example Chris@10: @findex fftw_mpi_local_size_many Chris@10: Chris@10: The two-dimensional case above corresponds to @code{rnk = 2} and an Chris@10: array @code{n} of length 2 with @code{n[0] = n0} and @code{n[1] = n1}. Chris@10: This routine is for any @code{rnk > 1}; one-dimensional transforms Chris@10: have their own interface because they work slightly differently, as Chris@10: discussed below. Chris@10: Chris@10: First, the advanced interface allows you to perform multiple Chris@10: transforms at once, of interleaved data, as specified by the Chris@10: @code{howmany} parameter. (@code{hoamany} is 1 for a single Chris@10: transform.) Chris@10: Chris@10: Second, here you can specify your desired block size in the @code{n0} Chris@10: dimension, @code{block0}. To use FFTW's default block size, pass Chris@10: @code{FFTW_MPI_DEFAULT_BLOCK} (0) for @code{block0}. Otherwise, on Chris@10: @code{P} processes, FFTW will return @code{local_n0} equal to Chris@10: @code{block0} on the first @code{P / block0} processes (rounded down), Chris@10: return @code{local_n0} equal to @code{n0 - block0 * (P / block0)} on Chris@10: the next process, and @code{local_n0} equal to zero on any remaining Chris@10: processes. In general, we recommend using the default block size Chris@10: (which corresponds to @code{n0 / P}, rounded up). Chris@10: @ctindex FFTW_MPI_DEFAULT_BLOCK Chris@10: @cindex block distribution Chris@10: Chris@10: Chris@10: For example, suppose you have @code{P = 4} processes and @code{n0 = Chris@10: 21}. The default will be a block size of @code{6}, which will give Chris@10: @code{local_n0 = 6} on the first three processes and @code{local_n0 = Chris@10: 3} on the last process. Instead, however, you could specify Chris@10: @code{block0 = 5} if you wanted, which would give @code{local_n0 = 5} Chris@10: on processes 0 to 2, @code{local_n0 = 6} on process 3. (This choice, Chris@10: while it may look superficially more ``balanced,'' has the same Chris@10: critical path as FFTW's default but requires more communications.) Chris@10: Chris@10: @node Load balancing, Transposed distributions, Basic and advanced distribution interfaces, MPI Data Distribution Chris@10: @subsection Load balancing Chris@10: @cindex load balancing Chris@10: Chris@10: Ideally, when you parallelize a transform over some @math{P} Chris@10: processes, each process should end up with work that takes equal time. Chris@10: Otherwise, all of the processes end up waiting on whichever process is Chris@10: slowest. This goal is known as ``load balancing.'' In this section, Chris@10: we describe the circumstances under which FFTW is able to load-balance Chris@10: well, and in particular how you should choose your transform size in Chris@10: order to load balance. Chris@10: Chris@10: Load balancing is especially difficult when you are parallelizing over Chris@10: heterogeneous machines; for example, if one of your processors is a Chris@10: old 486 and another is a Pentium IV, obviously you should give the Chris@10: Pentium more work to do than the 486 since the latter is much slower. Chris@10: FFTW does not deal with this problem, however---it assumes that your Chris@10: processes run on hardware of comparable speed, and that the goal is Chris@10: therefore to divide the problem as equally as possible. Chris@10: Chris@10: For a multi-dimensional complex DFT, FFTW can divide the problem Chris@10: equally among the processes if: (i) the @emph{first} dimension Chris@10: @code{n0} is divisible by @math{P}; and (ii), the @emph{product} of Chris@10: the subsequent dimensions is divisible by @math{P}. (For the advanced Chris@10: interface, where you can specify multiple simultaneous transforms via Chris@10: some ``vector'' length @code{howmany}, a factor of @code{howmany} is Chris@10: included in the product of the subsequent dimensions.) Chris@10: Chris@10: For a one-dimensional complex DFT, the length @code{N} of the data Chris@10: should be divisible by @math{P} @emph{squared} to be able to divide Chris@10: the problem equally among the processes. Chris@10: Chris@10: @node Transposed distributions, One-dimensional distributions, Load balancing, MPI Data Distribution Chris@10: @subsection Transposed distributions Chris@10: Chris@10: Internally, FFTW's MPI transform algorithms work by first computing Chris@10: transforms of the data local to each process, then by globally Chris@10: @emph{transposing} the data in some fashion to redistribute the data Chris@10: among the processes, transforming the new data local to each process, Chris@10: and transposing back. For example, a two-dimensional @code{n0} by Chris@10: @code{n1} array, distributed across the @code{n0} dimension, is Chris@10: transformd by: (i) transforming the @code{n1} dimension, which are Chris@10: local to each process; (ii) transposing to an @code{n1} by @code{n0} Chris@10: array, distributed across the @code{n1} dimension; (iii) transforming Chris@10: the @code{n0} dimension, which is now local to each process; (iv) Chris@10: transposing back. Chris@10: @cindex transpose Chris@10: Chris@10: Chris@10: However, in many applications it is acceptable to compute a Chris@10: multidimensional DFT whose results are produced in transposed order Chris@10: (e.g., @code{n1} by @code{n0} in two dimensions). This provides a Chris@10: significant performance advantage, because it means that the final Chris@10: transposition step can be omitted. FFTW supports this optimization, Chris@10: which you specify by passing the flag @code{FFTW_MPI_TRANSPOSED_OUT} Chris@10: to the planner routines. To compute the inverse transform of Chris@10: transposed output, you specify @code{FFTW_MPI_TRANSPOSED_IN} to tell Chris@10: it that the input is transposed. In this section, we explain how to Chris@10: interpret the output format of such a transform. Chris@10: @ctindex FFTW_MPI_TRANSPOSED_OUT Chris@10: @ctindex FFTW_MPI_TRANSPOSED_IN Chris@10: Chris@10: Chris@10: Suppose you have are transforming multi-dimensional data with (at Chris@10: least two) dimensions @ndims{}. As always, it is distributed along Chris@10: the first dimension @dimk{0}. Now, if we compute its DFT with the Chris@10: @code{FFTW_MPI_TRANSPOSED_OUT} flag, the resulting output data are stored Chris@10: with the first @emph{two} dimensions transposed: @ndimstrans{}, Chris@10: distributed along the @dimk{1} dimension. Conversely, if we take the Chris@10: @ndimstrans{} data and transform it with the Chris@10: @code{FFTW_MPI_TRANSPOSED_IN} flag, then the format goes back to the Chris@10: original @ndims{} array. Chris@10: Chris@10: There are two ways to find the portion of the transposed array that Chris@10: resides on the current process. First, you can simply call the Chris@10: appropriate @samp{local_size} function, passing @ndimstrans{} (the Chris@10: transposed dimensions). This would mean calling the @samp{local_size} Chris@10: function twice, once for the transposed and once for the Chris@10: non-transposed dimensions. Alternatively, you can call one of the Chris@10: @samp{local_size_transposed} functions, which returns both the Chris@10: non-transposed and transposed data distribution from a single call. Chris@10: For example, for a 3d transform with transposed output (or input), you Chris@10: might call: Chris@10: Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_3d_transposed( Chris@10: ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t n2, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start, Chris@10: ptrdiff_t *local_n1, ptrdiff_t *local_1_start); Chris@10: @end example Chris@10: @findex fftw_mpi_local_size_3d_transposed Chris@10: Chris@10: Here, @code{local_n0} and @code{local_0_start} give the size and Chris@10: starting index of the @code{n0} dimension for the Chris@10: @emph{non}-transposed data, as in the previous sections. For Chris@10: @emph{transposed} data (e.g. the output for Chris@10: @code{FFTW_MPI_TRANSPOSED_OUT}), @code{local_n1} and Chris@10: @code{local_1_start} give the size and starting index of the @code{n1} Chris@10: dimension, which is the first dimension of the transposed data Chris@10: (@code{n1} by @code{n0} by @code{n2}). Chris@10: Chris@10: (Note that @code{FFTW_MPI_TRANSPOSED_IN} is completely equivalent to Chris@10: performing @code{FFTW_MPI_TRANSPOSED_OUT} and passing the first two Chris@10: dimensions to the planner in reverse order, or vice versa. If you Chris@10: pass @emph{both} the @code{FFTW_MPI_TRANSPOSED_IN} and Chris@10: @code{FFTW_MPI_TRANSPOSED_OUT} flags, it is equivalent to swapping the Chris@10: first two dimensions passed to the planner and passing @emph{neither} Chris@10: flag.) Chris@10: Chris@10: @node One-dimensional distributions, , Transposed distributions, MPI Data Distribution Chris@10: @subsection One-dimensional distributions Chris@10: Chris@10: For one-dimensional distributed DFTs using FFTW, matters are slightly Chris@10: more complicated because the data distribution is more closely tied to Chris@10: how the algorithm works. In particular, you can no longer pass an Chris@10: arbitrary block size and must accept FFTW's default; also, the block Chris@10: sizes may be different for input and output. Also, the data Chris@10: distribution depends on the flags and transform direction, in order Chris@10: for forward and backward transforms to work correctly. Chris@10: Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_1d(ptrdiff_t n0, MPI_Comm comm, Chris@10: int sign, unsigned flags, Chris@10: ptrdiff_t *local_ni, ptrdiff_t *local_i_start, Chris@10: ptrdiff_t *local_no, ptrdiff_t *local_o_start); Chris@10: @end example Chris@10: @findex fftw_mpi_local_size_1d Chris@10: Chris@10: This function computes the data distribution for a 1d transform of Chris@10: size @code{n0} with the given transform @code{sign} and @code{flags}. Chris@10: Both input and output data use block distributions. The input on the Chris@10: current process will consist of @code{local_ni} numbers starting at Chris@10: index @code{local_i_start}; e.g. if only a single process is used, Chris@10: then @code{local_ni} will be @code{n0} and @code{local_i_start} will Chris@10: be @code{0}. Similarly for the output, with @code{local_no} numbers Chris@10: starting at index @code{local_o_start}. The return value of Chris@10: @code{fftw_mpi_local_size_1d} will be the total number of elements to Chris@10: allocate on the current process (which might be slightly larger than Chris@10: the local size due to intermediate steps in the algorithm). Chris@10: Chris@10: As mentioned above (@pxref{Load balancing}), the data will be divided Chris@10: equally among the processes if @code{n0} is divisible by the Chris@10: @emph{square} of the number of processes. In this case, Chris@10: @code{local_ni} will equal @code{local_no}. Otherwise, they may be Chris@10: different. Chris@10: Chris@10: For some applications, such as convolutions, the order of the output Chris@10: data is irrelevant. In this case, performance can be improved by Chris@10: specifying that the output data be stored in an FFTW-defined Chris@10: ``scrambled'' format. (In particular, this is the analogue of Chris@10: transposed output in the multidimensional case: scrambled output saves Chris@10: a communications step.) If you pass @code{FFTW_MPI_SCRAMBLED_OUT} in Chris@10: the flags, then the output is stored in this (undocumented) scrambled Chris@10: order. Conversely, to perform the inverse transform of data in Chris@10: scrambled order, pass the @code{FFTW_MPI_SCRAMBLED_IN} flag. Chris@10: @ctindex FFTW_MPI_SCRAMBLED_OUT Chris@10: @ctindex FFTW_MPI_SCRAMBLED_IN Chris@10: Chris@10: Chris@10: In MPI FFTW, only composite sizes @code{n0} can be parallelized; we Chris@10: have not yet implemented a parallel algorithm for large prime sizes. Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node Multi-dimensional MPI DFTs of Real Data, Other Multi-dimensional Real-data MPI Transforms, MPI Data Distribution, Distributed-memory FFTW with MPI Chris@10: @section Multi-dimensional MPI DFTs of Real Data Chris@10: Chris@10: FFTW's MPI interface also supports multi-dimensional DFTs of real Chris@10: data, similar to the serial r2c and c2r interfaces. (Parallel Chris@10: one-dimensional real-data DFTs are not currently supported; you must Chris@10: use a complex transform and set the imaginary parts of the inputs to Chris@10: zero.) Chris@10: Chris@10: The key points to understand for r2c and c2r MPI transforms (compared Chris@10: to the MPI complex DFTs or the serial r2c/c2r transforms), are: Chris@10: Chris@10: @itemize @bullet Chris@10: Chris@10: @item Chris@10: Just as for serial transforms, r2c/c2r DFTs transform @ndims{} real Chris@10: data to/from @ndimshalf{} complex data: the last dimension of the Chris@10: complex data is cut in half (rounded down), plus one. As for the Chris@10: serial transforms, the sizes you pass to the @samp{plan_dft_r2c} and Chris@10: @samp{plan_dft_c2r} are the @ndims{} dimensions of the real data. Chris@10: Chris@10: @item Chris@10: @cindex padding Chris@10: Although the real data is @emph{conceptually} @ndims{}, it is Chris@10: @emph{physically} stored as an @ndimspad{} array, where the last Chris@10: dimension has been @emph{padded} to make it the same size as the Chris@10: complex output. This is much like the in-place serial r2c/c2r Chris@10: interface (@pxref{Multi-Dimensional DFTs of Real Data}), except that Chris@10: in MPI the padding is required even for out-of-place data. The extra Chris@10: padding numbers are ignored by FFTW (they are @emph{not} like Chris@10: zero-padding the transform to a larger size); they are only used to Chris@10: determine the data layout. Chris@10: Chris@10: @item Chris@10: @cindex data distribution Chris@10: The data distribution in MPI for @emph{both} the real and complex data Chris@10: is determined by the shape of the @emph{complex} data. That is, you Chris@10: call the appropriate @samp{local size} function for the @ndimshalf{} Chris@10: complex data, and then use the @emph{same} distribution for the real Chris@10: data except that the last complex dimension is replaced by a (padded) Chris@10: real dimension of twice the length. Chris@10: Chris@10: @end itemize Chris@10: Chris@10: For example suppose we are performing an out-of-place r2c transform of Chris@10: @threedims{L,M,N} real data [padded to @threedims{L,M,2(N/2+1)}], Chris@10: resulting in @threedims{L,M,N/2+1} complex data. Similar to the Chris@10: example in @ref{2d MPI example}, we might do something like: Chris@10: Chris@10: @example Chris@10: #include Chris@10: Chris@10: int main(int argc, char **argv) Chris@10: @{ Chris@10: const ptrdiff_t L = ..., M = ..., N = ...; Chris@10: fftw_plan plan; Chris@10: double *rin; Chris@10: fftw_complex *cout; Chris@10: ptrdiff_t alloc_local, local_n0, local_0_start, i, j, k; Chris@10: Chris@10: MPI_Init(&argc, &argv); Chris@10: fftw_mpi_init(); Chris@10: Chris@10: /* @r{get local data size and allocate} */ Chris@10: alloc_local = fftw_mpi_local_size_3d(L, M, N/2+1, MPI_COMM_WORLD, Chris@10: &local_n0, &local_0_start); Chris@10: rin = fftw_alloc_real(2 * alloc_local); Chris@10: cout = fftw_alloc_complex(alloc_local); Chris@10: Chris@10: /* @r{create plan for out-of-place r2c DFT} */ Chris@10: plan = fftw_mpi_plan_dft_r2c_3d(L, M, N, rin, cout, MPI_COMM_WORLD, Chris@10: FFTW_MEASURE); Chris@10: Chris@10: /* @r{initialize rin to some function} my_func(x,y,z) */ Chris@10: for (i = 0; i < local_n0; ++i) Chris@10: for (j = 0; j < M; ++j) Chris@10: for (k = 0; k < N; ++k) Chris@10: rin[(i*M + j) * (2*(N/2+1)) + k] = my_func(local_0_start+i, j, k); Chris@10: Chris@10: /* @r{compute transforms 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: @end example Chris@10: Chris@10: @findex fftw_alloc_real Chris@10: @cindex row-major Chris@10: Note that we allocated @code{rin} using @code{fftw_alloc_real} with an Chris@10: argument of @code{2 * alloc_local}: since @code{alloc_local} is the Chris@10: number of @emph{complex} values to allocate, the number of @emph{real} Chris@10: values is twice as many. The @code{rin} array is then Chris@10: @threedims{local_n0,M,2(N/2+1)} in row-major order, so its Chris@10: @code{(i,j,k)} element is at the index @code{(i*M + j) * (2*(N/2+1)) + Chris@10: k} (@pxref{Multi-dimensional Array Format }). Chris@10: Chris@10: @cindex transpose Chris@10: @ctindex FFTW_TRANSPOSED_OUT Chris@10: @ctindex FFTW_TRANSPOSED_IN Chris@10: As for the complex transforms, improved performance can be obtained by Chris@10: specifying that the output is the transpose of the input or vice versa Chris@10: (@pxref{Transposed distributions}). In our @threedims{L,M,N} r2c Chris@10: example, including @code{FFTW_TRANSPOSED_OUT} in the flags means that Chris@10: the input would be a padded @threedims{L,M,2(N/2+1)} real array Chris@10: distributed over the @code{L} dimension, while the output would be a Chris@10: @threedims{M,L,N/2+1} complex array distributed over the @code{M} Chris@10: dimension. To perform the inverse c2r transform with the same data Chris@10: distributions, you would use the @code{FFTW_TRANSPOSED_IN} flag. Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node Other Multi-dimensional Real-data MPI Transforms, FFTW MPI Transposes, Multi-dimensional MPI DFTs of Real Data, Distributed-memory FFTW with MPI Chris@10: @section Other multi-dimensional Real-Data MPI Transforms Chris@10: Chris@10: @cindex r2r Chris@10: FFTW's MPI interface also supports multi-dimensional @samp{r2r} Chris@10: transforms of all kinds supported by the serial interface Chris@10: (e.g. discrete cosine and sine transforms, discrete Hartley Chris@10: transforms, etc.). Only multi-dimensional @samp{r2r} transforms, not Chris@10: one-dimensional transforms, are currently parallelized. Chris@10: Chris@10: @tindex fftw_r2r_kind Chris@10: These are used much like the multidimensional complex DFTs discussed Chris@10: above, except that the data is real rather than complex, and one needs Chris@10: to pass an r2r transform kind (@code{fftw_r2r_kind}) for each Chris@10: dimension as in the serial FFTW (@pxref{More DFTs of Real Data}). Chris@10: Chris@10: For example, one might perform a two-dimensional @twodims{L,M} that is Chris@10: an REDFT10 (DCT-II) in the first dimension and an RODFT10 (DST-II) in Chris@10: the second dimension with code like: Chris@10: Chris@10: @example Chris@10: const ptrdiff_t L = ..., M = ...; Chris@10: fftw_plan plan; Chris@10: double *data; Chris@10: ptrdiff_t alloc_local, local_n0, local_0_start, i, j; Chris@10: Chris@10: /* @r{get local data size and allocate} */ Chris@10: alloc_local = fftw_mpi_local_size_2d(L, M, MPI_COMM_WORLD, Chris@10: &local_n0, &local_0_start); Chris@10: data = fftw_alloc_real(alloc_local); Chris@10: Chris@10: /* @r{create plan for in-place REDFT10 x RODFT10} */ Chris@10: plan = fftw_mpi_plan_r2r_2d(L, M, data, data, MPI_COMM_WORLD, Chris@10: FFTW_REDFT10, FFTW_RODFT10, FFTW_MEASURE); Chris@10: Chris@10: /* @r{initialize data to some function} my_function(x,y) */ Chris@10: for (i = 0; i < local_n0; ++i) for (j = 0; j < M; ++j) Chris@10: data[i*M + j] = my_function(local_0_start + i, j); Chris@10: Chris@10: /* @r{compute transforms, in-place, as many times as desired} */ Chris@10: fftw_execute(plan); Chris@10: Chris@10: fftw_destroy_plan(plan); Chris@10: @end example Chris@10: Chris@10: @findex fftw_alloc_real Chris@10: Notice that we use the same @samp{local_size} functions as we did for Chris@10: complex data, only now we interpret the sizes in terms of real rather Chris@10: than complex values, and correspondingly use @code{fftw_alloc_real}. Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node FFTW MPI Transposes, FFTW MPI Wisdom, Other Multi-dimensional Real-data MPI Transforms, Distributed-memory FFTW with MPI Chris@10: @section FFTW MPI Transposes Chris@10: @cindex transpose Chris@10: Chris@10: The FFTW's MPI Fourier transforms rely on one or more @emph{global Chris@10: transposition} step for their communications. For example, the Chris@10: multidimensional transforms work by transforming along some Chris@10: dimensions, then transposing to make the first dimension local and Chris@10: transforming that, then transposing back. Because global Chris@10: transposition of a block-distributed matrix has many other potential Chris@10: uses besides FFTs, FFTW's transpose routines can be called directly, Chris@10: as documented in this section. Chris@10: Chris@10: @menu Chris@10: * Basic distributed-transpose interface:: Chris@10: * Advanced distributed-transpose interface:: Chris@10: * An improved replacement for MPI_Alltoall:: Chris@10: @end menu Chris@10: Chris@10: @node Basic distributed-transpose interface, Advanced distributed-transpose interface, FFTW MPI Transposes, FFTW MPI Transposes Chris@10: @subsection Basic distributed-transpose interface Chris@10: Chris@10: In particular, suppose that we have an @code{n0} by @code{n1} array in Chris@10: row-major order, block-distributed across the @code{n0} dimension. To Chris@10: transpose this into an @code{n1} by @code{n0} array block-distributed Chris@10: across the @code{n1} dimension, we would create a plan by calling the Chris@10: following function: Chris@10: Chris@10: @example Chris@10: fftw_plan fftw_mpi_plan_transpose(ptrdiff_t n0, ptrdiff_t n1, Chris@10: double *in, double *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: @end example Chris@10: @findex fftw_mpi_plan_transpose Chris@10: Chris@10: The input and output arrays (@code{in} and @code{out}) can be the Chris@10: same. The transpose is actually executed by calling Chris@10: @code{fftw_execute} on the plan, as usual. Chris@10: @findex fftw_execute Chris@10: Chris@10: Chris@10: The @code{flags} are the usual FFTW planner flags, but support Chris@10: two additional flags: @code{FFTW_MPI_TRANSPOSED_OUT} and/or Chris@10: @code{FFTW_MPI_TRANSPOSED_IN}. What these flags indicate, for Chris@10: transpose plans, is that the output and/or input, respectively, are Chris@10: @emph{locally} transposed. That is, on each process input data is Chris@10: normally stored as a @code{local_n0} by @code{n1} array in row-major Chris@10: order, but for an @code{FFTW_MPI_TRANSPOSED_IN} plan the input data is Chris@10: stored as @code{n1} by @code{local_n0} in row-major order. Similarly, Chris@10: @code{FFTW_MPI_TRANSPOSED_OUT} means that the output is @code{n0} by Chris@10: @code{local_n1} instead of @code{local_n1} by @code{n0}. Chris@10: @ctindex FFTW_MPI_TRANSPOSED_OUT Chris@10: @ctindex FFTW_MPI_TRANSPOSED_IN Chris@10: Chris@10: Chris@10: To determine the local size of the array on each process before and Chris@10: after the transpose, as well as the amount of storage that must be Chris@10: allocated, one should call @code{fftw_mpi_local_size_2d_transposed}, Chris@10: just as for a 2d DFT as described in the previous section: Chris@10: @cindex data distribution Chris@10: Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_2d_transposed Chris@10: (ptrdiff_t n0, ptrdiff_t n1, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start, Chris@10: ptrdiff_t *local_n1, ptrdiff_t *local_1_start); Chris@10: @end example Chris@10: @findex fftw_mpi_local_size_2d_transposed Chris@10: Chris@10: Again, the return value is the local storage to allocate, which in Chris@10: this case is the number of @emph{real} (@code{double}) values rather Chris@10: than complex numbers as in the previous examples. Chris@10: Chris@10: @node Advanced distributed-transpose interface, An improved replacement for MPI_Alltoall, Basic distributed-transpose interface, FFTW MPI Transposes Chris@10: @subsection Advanced distributed-transpose interface Chris@10: Chris@10: The above routines are for a transpose of a matrix of numbers (of type Chris@10: @code{double}), using FFTW's default block sizes. More generally, one Chris@10: can perform transposes of @emph{tuples} of numbers, with Chris@10: user-specified block sizes for the input and output: Chris@10: Chris@10: @example Chris@10: fftw_plan fftw_mpi_plan_many_transpose Chris@10: (ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t howmany, Chris@10: ptrdiff_t block0, ptrdiff_t block1, Chris@10: double *in, double *out, MPI_Comm comm, unsigned flags); Chris@10: @end example Chris@10: @findex fftw_mpi_plan_many_transpose Chris@10: Chris@10: In this case, one is transposing an @code{n0} by @code{n1} matrix of Chris@10: @code{howmany}-tuples (e.g. @code{howmany = 2} for complex numbers). Chris@10: The input is distributed along the @code{n0} dimension with block size Chris@10: @code{block0}, and the @code{n1} by @code{n0} output is distributed Chris@10: along the @code{n1} dimension with block size @code{block1}. If Chris@10: @code{FFTW_MPI_DEFAULT_BLOCK} (0) is passed for a block size then FFTW Chris@10: uses its default block size. To get the local size of the data on Chris@10: each process, you should then call @code{fftw_mpi_local_size_many_transposed}. Chris@10: @ctindex FFTW_MPI_DEFAULT_BLOCK Chris@10: @findex fftw_mpi_local_size_many_transposed Chris@10: Chris@10: @node An improved replacement for MPI_Alltoall, , Advanced distributed-transpose interface, FFTW MPI Transposes Chris@10: @subsection An improved replacement for MPI_Alltoall Chris@10: Chris@10: We close this section by noting that FFTW's MPI transpose routines can Chris@10: be thought of as a generalization for the @code{MPI_Alltoall} function Chris@10: (albeit only for floating-point types), and in some circumstances can Chris@10: function as an improved replacement. Chris@10: @findex MPI_Alltoall Chris@10: Chris@10: Chris@10: @code{MPI_Alltoall} is defined by the MPI standard as: Chris@10: Chris@10: @example Chris@10: int MPI_Alltoall(void *sendbuf, int sendcount, MPI_Datatype sendtype, Chris@10: void *recvbuf, int recvcnt, MPI_Datatype recvtype, Chris@10: MPI_Comm comm); Chris@10: @end example Chris@10: Chris@10: In particular, for @code{double*} arrays @code{in} and @code{out}, Chris@10: consider the call: Chris@10: Chris@10: @example Chris@10: MPI_Alltoall(in, howmany, MPI_DOUBLE, out, howmany MPI_DOUBLE, comm); Chris@10: @end example Chris@10: Chris@10: This is completely equivalent to: Chris@10: Chris@10: @example Chris@10: MPI_Comm_size(comm, &P); Chris@10: plan = fftw_mpi_plan_many_transpose(P, P, howmany, 1, 1, in, out, comm, FFTW_ESTIMATE); Chris@10: fftw_execute(plan); Chris@10: fftw_destroy_plan(plan); Chris@10: @end example Chris@10: Chris@10: That is, computing a @twodims{P,P} transpose on @code{P} processes, Chris@10: with a block size of 1, is just a standard all-to-all communication. Chris@10: Chris@10: However, using the FFTW routine instead of @code{MPI_Alltoall} may Chris@10: have certain advantages. First of all, FFTW's routine can operate Chris@10: in-place (@code{in == out}) whereas @code{MPI_Alltoall} can only Chris@10: operate out-of-place. Chris@10: @cindex in-place Chris@10: Chris@10: Chris@10: Second, even for out-of-place plans, FFTW's routine may be faster, Chris@10: especially if you need to perform the all-to-all communication many Chris@10: times and can afford to use @code{FFTW_MEASURE} or Chris@10: @code{FFTW_PATIENT}. It should certainly be no slower, not including Chris@10: the time to create the plan, since one of the possible algorithms that Chris@10: FFTW uses for an out-of-place transpose @emph{is} simply to call Chris@10: @code{MPI_Alltoall}. However, FFTW also considers several other Chris@10: possible algorithms that, depending on your MPI implementation and Chris@10: your hardware, may be faster. Chris@10: @ctindex FFTW_MEASURE Chris@10: @ctindex FFTW_PATIENT Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node FFTW MPI Wisdom, Avoiding MPI Deadlocks, FFTW MPI Transposes, Distributed-memory FFTW with MPI Chris@10: @section FFTW MPI Wisdom Chris@10: @cindex wisdom Chris@10: @cindex saving plans to disk Chris@10: Chris@10: FFTW's ``wisdom'' facility (@pxref{Words of Wisdom-Saving Plans}) can Chris@10: be used to save MPI plans as well as to save uniprocessor plans. Chris@10: However, for MPI there are several unavoidable complications. Chris@10: Chris@10: @cindex MPI I/O Chris@10: First, the MPI standard does not guarantee that every process can Chris@10: perform file I/O (at least, not using C stdio routines)---in general, Chris@10: we may only assume that process 0 is capable of I/O.@footnote{In fact, Chris@10: even this assumption is not technically guaranteed by the standard, Chris@10: although it seems to be universal in actual MPI implementations and is Chris@10: widely assumed by MPI-using software. Technically, you need to query Chris@10: the @code{MPI_IO} attribute of @code{MPI_COMM_WORLD} with Chris@10: @code{MPI_Attr_get}. If this attribute is @code{MPI_PROC_NULL}, no Chris@10: I/O is possible. If it is @code{MPI_ANY_SOURCE}, any process can Chris@10: perform I/O. Otherwise, it is the rank of a process that can perform Chris@10: I/O ... but since it is not guaranteed to yield the @emph{same} rank Chris@10: on all processes, you have to do an @code{MPI_Allreduce} of some kind Chris@10: if you want all processes to agree about which is going to do I/O. Chris@10: And even then, the standard only guarantees that this process can Chris@10: perform output, but not input. See e.g. @cite{Parallel Programming Chris@10: with MPI} by P. S. Pacheco, section 8.1.3. Needless to say, in our Chris@10: experience virtually no MPI programmers worry about this.} So, if we Chris@10: want to export the wisdom from a single process to a file, we must Chris@10: first export the wisdom to a string, then send it to process 0, then Chris@10: write it to a file. Chris@10: Chris@10: Second, in principle we may want to have separate wisdom for every Chris@10: process, since in general the processes may run on different hardware Chris@10: even for a single MPI program. However, in practice FFTW's MPI code Chris@10: is designed for the case of homogeneous hardware (@pxref{Load Chris@10: balancing}), and in this case it is convenient to use the same wisdom Chris@10: for every process. Thus, we need a mechanism to synchronize the wisdom. Chris@10: Chris@10: To address both of these problems, FFTW provides the following two Chris@10: functions: Chris@10: Chris@10: @example Chris@10: void fftw_mpi_broadcast_wisdom(MPI_Comm comm); Chris@10: void fftw_mpi_gather_wisdom(MPI_Comm comm); Chris@10: @end example Chris@10: @findex fftw_mpi_gather_wisdom Chris@10: @findex fftw_mpi_broadcast_wisdom Chris@10: Chris@10: Given a communicator @code{comm}, @code{fftw_mpi_broadcast_wisdom} Chris@10: will broadcast the wisdom from process 0 to all other processes. Chris@10: Conversely, @code{fftw_mpi_gather_wisdom} will collect wisdom from all Chris@10: processes onto process 0. (If the plans created for the same problem Chris@10: by different processes are not the same, @code{fftw_mpi_gather_wisdom} Chris@10: will arbitrarily choose one of the plans.) Both of these functions Chris@10: may result in suboptimal plans for different processes if the Chris@10: processes are running on non-identical hardware. Both of these Chris@10: functions are @emph{collective} calls, which means that they must be Chris@10: executed by all processes in the communicator. Chris@10: @cindex collective function Chris@10: Chris@10: Chris@10: So, for example, a typical code snippet to import wisdom from a file Chris@10: and use it on all processes would be: Chris@10: Chris@10: @example Chris@10: @{ Chris@10: int rank; Chris@10: Chris@10: fftw_mpi_init(); Chris@10: MPI_Comm_rank(MPI_COMM_WORLD, &rank); Chris@10: if (rank == 0) fftw_import_wisdom_from_filename("mywisdom"); Chris@10: fftw_mpi_broadcast_wisdom(MPI_COMM_WORLD); Chris@10: @} Chris@10: @end example Chris@10: Chris@10: (Note that we must call @code{fftw_mpi_init} before importing any Chris@10: wisdom that might contain MPI plans.) Similarly, a typical code Chris@10: snippet to export wisdom from all processes to a file is: Chris@10: @findex fftw_mpi_init Chris@10: Chris@10: @example Chris@10: @{ Chris@10: int rank; Chris@10: Chris@10: fftw_mpi_gather_wisdom(MPI_COMM_WORLD); Chris@10: MPI_Comm_rank(MPI_COMM_WORLD, &rank); Chris@10: if (rank == 0) fftw_export_wisdom_to_filename("mywisdom"); Chris@10: @} Chris@10: @end example Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node Avoiding MPI Deadlocks, FFTW MPI Performance Tips, FFTW MPI Wisdom, Distributed-memory FFTW with MPI Chris@10: @section Avoiding MPI Deadlocks Chris@10: @cindex deadlock Chris@10: Chris@10: An MPI program can @emph{deadlock} if one process is waiting for a Chris@10: message from another process that never gets sent. To avoid deadlocks Chris@10: when using FFTW's MPI routines, it is important to know which Chris@10: functions are @emph{collective}: that is, which functions must Chris@10: @emph{always} be called in the @emph{same order} from @emph{every} Chris@10: process in a given communicator. (For example, @code{MPI_Barrier} is Chris@10: the canonical example of a collective function in the MPI standard.) Chris@10: @cindex collective function Chris@10: @findex MPI_Barrier Chris@10: Chris@10: Chris@10: The functions in FFTW that are @emph{always} collective are: every Chris@10: function beginning with @samp{fftw_mpi_plan}, as well as Chris@10: @code{fftw_mpi_broadcast_wisdom} and @code{fftw_mpi_gather_wisdom}. Chris@10: Also, the following functions from the ordinary FFTW interface are Chris@10: collective when they are applied to a plan created by an Chris@10: @samp{fftw_mpi_plan} function: @code{fftw_execute}, Chris@10: @code{fftw_destroy_plan}, and @code{fftw_flops}. Chris@10: @findex fftw_execute Chris@10: @findex fftw_destroy_plan Chris@10: @findex fftw_flops Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node FFTW MPI Performance Tips, Combining MPI and Threads, Avoiding MPI Deadlocks, Distributed-memory FFTW with MPI Chris@10: @section FFTW MPI Performance Tips Chris@10: Chris@10: In this section, we collect a few tips on getting the best performance Chris@10: out of FFTW's MPI transforms. Chris@10: Chris@10: First, because of the 1d block distribution, FFTW's parallelization is Chris@10: currently limited by the size of the first dimension. Chris@10: (Multidimensional block distributions may be supported by a future Chris@10: version.) More generally, you should ideally arrange the dimensions so Chris@10: that FFTW can divide them equally among the processes. @xref{Load Chris@10: balancing}. Chris@10: @cindex block distribution Chris@10: @cindex load balancing Chris@10: Chris@10: Chris@10: Second, if it is not too inconvenient, you should consider working Chris@10: with transposed output for multidimensional plans, as this saves a Chris@10: considerable amount of communications. @xref{Transposed distributions}. Chris@10: @cindex transpose Chris@10: Chris@10: Chris@10: Third, the fastest choices are generally either an in-place transform Chris@10: or an out-of-place transform with the @code{FFTW_DESTROY_INPUT} flag Chris@10: (which allows the input array to be used as scratch space). In-place Chris@10: is especially beneficial if the amount of data per process is large. Chris@10: @ctindex FFTW_DESTROY_INPUT Chris@10: Chris@10: Chris@10: Fourth, if you have multiple arrays to transform at once, rather than Chris@10: calling FFTW's MPI transforms several times it usually seems to be Chris@10: faster to interleave the data and use the advanced interface. (This Chris@10: groups the communications together instead of requiring separate Chris@10: messages for each transform.) Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node Combining MPI and Threads, FFTW MPI Reference, FFTW MPI Performance Tips, Distributed-memory FFTW with MPI Chris@10: @section Combining MPI and Threads Chris@10: @cindex threads Chris@10: Chris@10: In certain cases, it may be advantageous to combine MPI Chris@10: (distributed-memory) and threads (shared-memory) parallelization. Chris@10: FFTW supports this, with certain caveats. For example, if you have a Chris@10: cluster of 4-processor shared-memory nodes, you may want to use Chris@10: threads within the nodes and MPI between the nodes, instead of MPI for Chris@10: all parallelization. Chris@10: Chris@10: In particular, it is possible to seamlessly combine the MPI FFTW Chris@10: routines with the multi-threaded FFTW routines (@pxref{Multi-threaded Chris@10: FFTW}). However, some care must be taken in the initialization code, Chris@10: which should look something like this: Chris@10: Chris@10: @example Chris@10: int threads_ok; Chris@10: Chris@10: int main(int argc, char **argv) Chris@10: @{ Chris@10: int provided; Chris@10: MPI_Init_thread(&argc, &argv, MPI_THREAD_FUNNELED, &provided); Chris@10: threads_ok = provided >= MPI_THREAD_FUNNELED; Chris@10: Chris@10: if (threads_ok) threads_ok = fftw_init_threads(); Chris@10: fftw_mpi_init(); Chris@10: Chris@10: ... Chris@10: if (threads_ok) fftw_plan_with_nthreads(...); Chris@10: ... Chris@10: Chris@10: MPI_Finalize(); Chris@10: @} Chris@10: @end example Chris@10: @findex fftw_mpi_init Chris@10: @findex fftw_init_threads Chris@10: @findex fftw_plan_with_nthreads Chris@10: Chris@10: First, note that instead of calling @code{MPI_Init}, you should call Chris@10: @code{MPI_Init_threads}, which is the initialization routine defined Chris@10: by the MPI-2 standard to indicate to MPI that your program will be Chris@10: multithreaded. We pass @code{MPI_THREAD_FUNNELED}, which indicates Chris@10: that we will only call MPI routines from the main thread. (FFTW will Chris@10: launch additional threads internally, but the extra threads will not Chris@10: call MPI code.) (You may also pass @code{MPI_THREAD_SERIALIZED} or Chris@10: @code{MPI_THREAD_MULTIPLE}, which requests additional multithreading Chris@10: support from the MPI implementation, but this is not required by Chris@10: FFTW.) The @code{provided} parameter returns what level of threads Chris@10: support is actually supported by your MPI implementation; this Chris@10: @emph{must} be at least @code{MPI_THREAD_FUNNELED} if you want to call Chris@10: the FFTW threads routines, so we define a global variable Chris@10: @code{threads_ok} to record this. You should only call Chris@10: @code{fftw_init_threads} or @code{fftw_plan_with_nthreads} if Chris@10: @code{threads_ok} is true. For more information on thread safety in Chris@10: MPI, see the Chris@10: @uref{http://www.mpi-forum.org/docs/mpi-20-html/node162.htm, MPI and Chris@10: Threads} section of the MPI-2 standard. Chris@10: @cindex thread safety Chris@10: Chris@10: Chris@10: Second, we must call @code{fftw_init_threads} @emph{before} Chris@10: @code{fftw_mpi_init}. This is critical for technical reasons having Chris@10: to do with how FFTW initializes its list of algorithms. Chris@10: Chris@10: Then, if you call @code{fftw_plan_with_nthreads(N)}, @emph{every} MPI Chris@10: process will launch (up to) @code{N} threads to parallelize its transforms. Chris@10: Chris@10: For example, in the hypothetical cluster of 4-processor nodes, you Chris@10: might wish to launch only a single MPI process per node, and then call Chris@10: @code{fftw_plan_with_nthreads(4)} on each process to use all Chris@10: processors in the nodes. Chris@10: Chris@10: This may or may not be faster than simply using as many MPI processes Chris@10: as you have processors, however. On the one hand, using threads Chris@10: within a node eliminates the need for explicit message passing within Chris@10: the node. On the other hand, FFTW's transpose routines are not Chris@10: multi-threaded, and this means that the communications that do take Chris@10: place will not benefit from parallelization within the node. Chris@10: Moreover, many MPI implementations already have optimizations to Chris@10: exploit shared memory when it is available, so adding the Chris@10: multithreaded FFTW on top of this may be superfluous. Chris@10: @cindex transpose Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node FFTW MPI Reference, FFTW MPI Fortran Interface, Combining MPI and Threads, Distributed-memory FFTW with MPI Chris@10: @section FFTW MPI Reference Chris@10: Chris@10: This chapter provides a complete reference to all FFTW MPI functions, Chris@10: datatypes, and constants. See also @ref{FFTW Reference} for information Chris@10: on functions and types in common with the serial interface. Chris@10: Chris@10: @menu Chris@10: * MPI Files and Data Types:: Chris@10: * MPI Initialization:: Chris@10: * Using MPI Plans:: Chris@10: * MPI Data Distribution Functions:: Chris@10: * MPI Plan Creation:: Chris@10: * MPI Wisdom Communication:: Chris@10: @end menu Chris@10: Chris@10: @node MPI Files and Data Types, MPI Initialization, FFTW MPI Reference, FFTW MPI Reference Chris@10: @subsection MPI Files and Data Types Chris@10: Chris@10: All programs using FFTW's MPI support should include its header file: Chris@10: Chris@10: @example Chris@10: #include Chris@10: @end example Chris@10: Chris@10: Note that this header file includes the serial-FFTW @code{fftw3.h} Chris@10: header file, and also the @code{mpi.h} header file for MPI, so you Chris@10: need not include those files separately. Chris@10: Chris@10: You must also link to @emph{both} the FFTW MPI library and to the Chris@10: serial FFTW library. On Unix, this means adding @code{-lfftw3_mpi Chris@10: -lfftw3 -lm} at the end of the link command. Chris@10: Chris@10: @cindex precision Chris@10: Different precisions are handled as in the serial interface: Chris@10: @xref{Precision}. That is, @samp{fftw_} functions become Chris@10: @code{fftwf_} (in single precision) etcetera, and the libraries become Chris@10: @code{-lfftw3f_mpi -lfftw3f -lm} etcetera on Unix. Long-double Chris@10: precision is supported in MPI, but quad precision (@samp{fftwq_}) is Chris@10: not due to the lack of MPI support for this type. Chris@10: Chris@10: @node MPI Initialization, Using MPI Plans, MPI Files and Data Types, FFTW MPI Reference Chris@10: @subsection MPI Initialization Chris@10: Chris@10: Before calling any other FFTW MPI (@samp{fftw_mpi_}) function, and Chris@10: before importing any wisdom for MPI problems, you must call: Chris@10: Chris@10: @findex fftw_mpi_init Chris@10: @example Chris@10: void fftw_mpi_init(void); Chris@10: @end example Chris@10: Chris@10: @findex fftw_init_threads Chris@10: If FFTW threads support is used, however, @code{fftw_mpi_init} should Chris@10: be called @emph{after} @code{fftw_init_threads} (@pxref{Combining MPI Chris@10: and Threads}). Calling @code{fftw_mpi_init} additional times (before Chris@10: @code{fftw_mpi_cleanup}) has no effect. Chris@10: Chris@10: Chris@10: If you want to deallocate all persistent data and reset FFTW to the Chris@10: pristine state it was in when you started your program, you can call: Chris@10: Chris@10: @findex fftw_mpi_cleanup Chris@10: @example Chris@10: void fftw_mpi_cleanup(void); Chris@10: @end example Chris@10: Chris@10: @findex fftw_cleanup Chris@10: (This calls @code{fftw_cleanup}, so you need not call the serial Chris@10: cleanup routine too, although it is safe to do so.) After calling Chris@10: @code{fftw_mpi_cleanup}, all existing plans become undefined, and you Chris@10: should not attempt to execute or destroy them. You must call Chris@10: @code{fftw_mpi_init} again after @code{fftw_mpi_cleanup} if you want Chris@10: to resume using the MPI FFTW routines. Chris@10: Chris@10: @node Using MPI Plans, MPI Data Distribution Functions, MPI Initialization, FFTW MPI Reference Chris@10: @subsection Using MPI Plans Chris@10: Chris@10: Once an MPI plan is created, you can execute and destroy it using Chris@10: @code{fftw_execute}, @code{fftw_destroy_plan}, and the other functions Chris@10: in the serial interface that operate on generic plans (@pxref{Using Chris@10: Plans}). Chris@10: Chris@10: @cindex collective function Chris@10: @cindex MPI communicator Chris@10: The @code{fftw_execute} and @code{fftw_destroy_plan} functions, applied to Chris@10: MPI plans, are @emph{collective} calls: they must be called for all processes Chris@10: in the communicator that was used to create the plan. Chris@10: Chris@10: @cindex new-array execution Chris@10: You must @emph{not} use the serial new-array plan-execution functions Chris@10: @code{fftw_execute_dft} and so on (@pxref{New-array Execute Chris@10: Functions}) with MPI plans. Such functions are specialized to the Chris@10: problem type, and there are specific new-array execute functions for MPI plans: Chris@10: Chris@10: @findex fftw_mpi_execute_dft Chris@10: @findex fftw_mpi_execute_dft_r2c Chris@10: @findex fftw_mpi_execute_dft_c2r Chris@10: @findex fftw_mpi_execute_r2r Chris@10: @example Chris@10: void fftw_mpi_execute_dft(fftw_plan p, fftw_complex *in, fftw_complex *out); Chris@10: void fftw_mpi_execute_dft_r2c(fftw_plan p, double *in, fftw_complex *out); Chris@10: void fftw_mpi_execute_dft_c2r(fftw_plan p, fftw_complex *in, double *out); Chris@10: void fftw_mpi_execute_r2r(fftw_plan p, double *in, double *out); Chris@10: @end example Chris@10: Chris@10: @cindex alignment Chris@10: @findex fftw_malloc Chris@10: These functions have the same restrictions as those of the serial Chris@10: new-array execute functions. They are @emph{always} safe to apply to Chris@10: the @emph{same} @code{in} and @code{out} arrays that were used to Chris@10: create the plan. They can only be applied to new arrarys if those Chris@10: arrays have the same types, dimensions, in-placeness, and alignment as Chris@10: the original arrays, where the best way to ensure the same alignment Chris@10: is to use FFTW's @code{fftw_malloc} and related allocation functions Chris@10: for all arrays (@pxref{Memory Allocation}). Note that distributed Chris@10: transposes (@pxref{FFTW MPI Transposes}) use Chris@10: @code{fftw_mpi_execute_r2r}, since they count as rank-zero r2r plans Chris@10: from FFTW's perspective. Chris@10: Chris@10: @node MPI Data Distribution Functions, MPI Plan Creation, Using MPI Plans, FFTW MPI Reference Chris@10: @subsection MPI Data Distribution Functions Chris@10: Chris@10: @cindex data distribution Chris@10: As described above (@pxref{MPI Data Distribution}), in order to Chris@10: allocate your arrays, @emph{before} creating a plan, you must first Chris@10: call one of the following routines to determine the required Chris@10: allocation size and the portion of the array locally stored on a given Chris@10: process. The @code{MPI_Comm} communicator passed here must be Chris@10: equivalent to the communicator used below for plan creation. Chris@10: Chris@10: The basic interface for multidimensional transforms consists of the Chris@10: functions: Chris@10: Chris@10: @findex fftw_mpi_local_size_2d Chris@10: @findex fftw_mpi_local_size_3d Chris@10: @findex fftw_mpi_local_size Chris@10: @findex fftw_mpi_local_size_2d_transposed Chris@10: @findex fftw_mpi_local_size_3d_transposed Chris@10: @findex fftw_mpi_local_size_transposed Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_2d(ptrdiff_t n0, ptrdiff_t n1, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start); Chris@10: ptrdiff_t fftw_mpi_local_size_3d(ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t n2, Chris@10: MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start); Chris@10: ptrdiff_t fftw_mpi_local_size(int rnk, const ptrdiff_t *n, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start); Chris@10: Chris@10: ptrdiff_t fftw_mpi_local_size_2d_transposed(ptrdiff_t n0, ptrdiff_t n1, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start, Chris@10: ptrdiff_t *local_n1, ptrdiff_t *local_1_start); Chris@10: ptrdiff_t fftw_mpi_local_size_3d_transposed(ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t n2, Chris@10: MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start, Chris@10: ptrdiff_t *local_n1, ptrdiff_t *local_1_start); Chris@10: ptrdiff_t fftw_mpi_local_size_transposed(int rnk, const ptrdiff_t *n, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start, Chris@10: ptrdiff_t *local_n1, ptrdiff_t *local_1_start); Chris@10: @end example Chris@10: Chris@10: These functions return the number of elements to allocate (complex Chris@10: numbers for DFT/r2c/c2r plans, real numbers for r2r plans), whereas Chris@10: the @code{local_n0} and @code{local_0_start} return the portion Chris@10: (@code{local_0_start} to @code{local_0_start + local_n0 - 1}) of the Chris@10: first dimension of an @ndims{} array that is stored on the local Chris@10: process. @xref{Basic and advanced distribution interfaces}. For Chris@10: @code{FFTW_MPI_TRANSPOSED_OUT} plans, the @samp{_transposed} variants Chris@10: are useful in order to also return the local portion of the first Chris@10: dimension in the @ndimstrans{} transposed output. @xref{Transposed Chris@10: distributions}. The advanced interface for multidimensional Chris@10: transforms is: Chris@10: Chris@10: @cindex advanced interface Chris@10: @findex fftw_mpi_local_size_many Chris@10: @findex fftw_mpi_local_size_many_transposed Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_many(int rnk, const ptrdiff_t *n, ptrdiff_t howmany, Chris@10: ptrdiff_t block0, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start); Chris@10: ptrdiff_t fftw_mpi_local_size_many_transposed(int rnk, const ptrdiff_t *n, ptrdiff_t howmany, Chris@10: ptrdiff_t block0, ptrdiff_t block1, MPI_Comm comm, Chris@10: ptrdiff_t *local_n0, ptrdiff_t *local_0_start, Chris@10: ptrdiff_t *local_n1, ptrdiff_t *local_1_start); Chris@10: @end example Chris@10: Chris@10: These differ from the basic interface in only two ways. First, they Chris@10: allow you to specify block sizes @code{block0} and @code{block1} (the Chris@10: latter for the transposed output); you can pass Chris@10: @code{FFTW_MPI_DEFAULT_BLOCK} to use FFTW's default block size as in Chris@10: the basic interface. Second, you can pass a @code{howmany} parameter, Chris@10: corresponding to the advanced planning interface below: this is for Chris@10: transforms of contiguous @code{howmany}-tuples of numbers Chris@10: (@code{howmany = 1} in the basic interface). Chris@10: Chris@10: The corresponding basic and advanced routines for one-dimensional Chris@10: transforms (currently only complex DFTs) are: Chris@10: Chris@10: @findex fftw_mpi_local_size_1d Chris@10: @findex fftw_mpi_local_size_many_1d Chris@10: @example Chris@10: ptrdiff_t fftw_mpi_local_size_1d( Chris@10: ptrdiff_t n0, MPI_Comm comm, int sign, unsigned flags, Chris@10: ptrdiff_t *local_ni, ptrdiff_t *local_i_start, Chris@10: ptrdiff_t *local_no, ptrdiff_t *local_o_start); Chris@10: ptrdiff_t fftw_mpi_local_size_many_1d( Chris@10: ptrdiff_t n0, ptrdiff_t howmany, Chris@10: MPI_Comm comm, int sign, unsigned flags, Chris@10: ptrdiff_t *local_ni, ptrdiff_t *local_i_start, Chris@10: ptrdiff_t *local_no, ptrdiff_t *local_o_start); Chris@10: @end example Chris@10: Chris@10: @ctindex FFTW_MPI_SCRAMBLED_OUT Chris@10: @ctindex FFTW_MPI_SCRAMBLED_IN Chris@10: As above, the return value is the number of elements to allocate Chris@10: (complex numbers, for complex DFTs). The @code{local_ni} and Chris@10: @code{local_i_start} arguments return the portion Chris@10: (@code{local_i_start} to @code{local_i_start + local_ni - 1}) of the Chris@10: 1d array that is stored on this process for the transform Chris@10: @emph{input}, and @code{local_no} and @code{local_o_start} are the Chris@10: corresponding quantities for the input. The @code{sign} Chris@10: (@code{FFTW_FORWARD} or @code{FFTW_BACKWARD}) and @code{flags} must Chris@10: match the arguments passed when creating a plan. Although the inputs Chris@10: and outputs have different data distributions in general, it is Chris@10: guaranteed that the @emph{output} data distribution of an Chris@10: @code{FFTW_FORWARD} plan will match the @emph{input} data distribution Chris@10: of an @code{FFTW_BACKWARD} plan and vice versa; similarly for the Chris@10: @code{FFTW_MPI_SCRAMBLED_OUT} and @code{FFTW_MPI_SCRAMBLED_IN} flags. Chris@10: @xref{One-dimensional distributions}. Chris@10: Chris@10: @node MPI Plan Creation, MPI Wisdom Communication, MPI Data Distribution Functions, FFTW MPI Reference Chris@10: @subsection MPI Plan Creation Chris@10: Chris@10: @subsubheading Complex-data MPI DFTs Chris@10: Chris@10: Plans for complex-data DFTs (@pxref{2d MPI example}) are created by: Chris@10: Chris@10: @findex fftw_mpi_plan_dft_1d Chris@10: @findex fftw_mpi_plan_dft_2d Chris@10: @findex fftw_mpi_plan_dft_3d Chris@10: @findex fftw_mpi_plan_dft Chris@10: @findex fftw_mpi_plan_many_dft Chris@10: @example Chris@10: fftw_plan fftw_mpi_plan_dft_1d(ptrdiff_t n0, fftw_complex *in, fftw_complex *out, Chris@10: MPI_Comm comm, int sign, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_2d(ptrdiff_t n0, ptrdiff_t n1, Chris@10: fftw_complex *in, fftw_complex *out, Chris@10: MPI_Comm comm, int sign, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_3d(ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t n2, Chris@10: fftw_complex *in, fftw_complex *out, Chris@10: MPI_Comm comm, int sign, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft(int rnk, const ptrdiff_t *n, Chris@10: fftw_complex *in, fftw_complex *out, Chris@10: MPI_Comm comm, int sign, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_many_dft(int rnk, const ptrdiff_t *n, Chris@10: ptrdiff_t howmany, ptrdiff_t block, ptrdiff_t tblock, Chris@10: fftw_complex *in, fftw_complex *out, Chris@10: MPI_Comm comm, int sign, unsigned flags); Chris@10: @end example Chris@10: Chris@10: @cindex MPI communicator Chris@10: @cindex collective function Chris@10: These are similar to their serial counterparts (@pxref{Complex DFTs}) Chris@10: in specifying the dimensions, sign, and flags of the transform. The Chris@10: @code{comm} argument gives an MPI communicator that specifies the set Chris@10: of processes to participate in the transform; plan creation is a Chris@10: collective function that must be called for all processes in the Chris@10: communicator. The @code{in} and @code{out} pointers refer only to a Chris@10: portion of the overall transform data (@pxref{MPI Data Distribution}) Chris@10: as specified by the @samp{local_size} functions in the previous Chris@10: section. Unless @code{flags} contains @code{FFTW_ESTIMATE}, these Chris@10: arrays are overwritten during plan creation as for the serial Chris@10: interface. For multi-dimensional transforms, any dimensions @code{> Chris@10: 1} are supported; for one-dimensional transforms, only composite Chris@10: (non-prime) @code{n0} are currently supported (unlike the serial Chris@10: FFTW). Requesting an unsupported transform size will yield a Chris@10: @code{NULL} plan. (As in the serial interface, highly composite sizes Chris@10: generally yield the best performance.) Chris@10: Chris@10: @cindex advanced interface Chris@10: @ctindex FFTW_MPI_DEFAULT_BLOCK Chris@10: @cindex stride Chris@10: The advanced-interface @code{fftw_mpi_plan_many_dft} additionally Chris@10: allows you to specify the block sizes for the first dimension Chris@10: (@code{block}) of the @ndims{} input data and the first dimension Chris@10: (@code{tblock}) of the @ndimstrans{} transposed data (at intermediate Chris@10: steps of the transform, and for the output if Chris@10: @code{FFTW_TRANSPOSED_OUT} is specified in @code{flags}). These must Chris@10: be the same block sizes as were passed to the corresponding Chris@10: @samp{local_size} function; you can pass @code{FFTW_MPI_DEFAULT_BLOCK} Chris@10: to use FFTW's default block size as in the basic interface. Also, the Chris@10: @code{howmany} parameter specifies that the transform is of contiguous Chris@10: @code{howmany}-tuples rather than individual complex numbers; this Chris@10: corresponds to the same parameter in the serial advanced interface Chris@10: (@pxref{Advanced Complex DFTs}) with @code{stride = howmany} and Chris@10: @code{dist = 1}. Chris@10: Chris@10: @subsubheading MPI flags Chris@10: Chris@10: The @code{flags} can be any of those for the serial FFTW Chris@10: (@pxref{Planner Flags}), and in addition may include one or more of Chris@10: the following MPI-specific flags, which improve performance at the Chris@10: cost of changing the output or input data formats. Chris@10: Chris@10: @itemize @bullet Chris@10: Chris@10: @item Chris@10: @ctindex FFTW_MPI_SCRAMBLED_OUT Chris@10: @ctindex FFTW_MPI_SCRAMBLED_IN Chris@10: @code{FFTW_MPI_SCRAMBLED_OUT}, @code{FFTW_MPI_SCRAMBLED_IN}: valid for Chris@10: 1d transforms only, these flags indicate that the output/input of the Chris@10: transform are in an undocumented ``scrambled'' order. A forward Chris@10: @code{FFTW_MPI_SCRAMBLED_OUT} transform can be inverted by a backward Chris@10: @code{FFTW_MPI_SCRAMBLED_IN} (times the usual 1/@i{N} normalization). Chris@10: @xref{One-dimensional distributions}. Chris@10: Chris@10: @item Chris@10: @ctindex FFTW_MPI_TRANSPOSED_OUT Chris@10: @ctindex FFTW_MPI_TRANSPOSED_IN Chris@10: @code{FFTW_MPI_TRANSPOSED_OUT}, @code{FFTW_MPI_TRANSPOSED_IN}: valid Chris@10: for multidimensional (@code{rnk > 1}) transforms only, these flags Chris@10: specify that the output or input of an @ndims{} transform is Chris@10: transposed to @ndimstrans{}. @xref{Transposed distributions}. Chris@10: Chris@10: @end itemize Chris@10: Chris@10: @subsubheading Real-data MPI DFTs Chris@10: Chris@10: @cindex r2c Chris@10: Plans for real-input/output (r2c/c2r) DFTs (@pxref{Multi-dimensional Chris@10: MPI DFTs of Real Data}) are created by: Chris@10: Chris@10: @findex fftw_mpi_plan_dft_r2c_2d Chris@10: @findex fftw_mpi_plan_dft_r2c_2d Chris@10: @findex fftw_mpi_plan_dft_r2c_3d Chris@10: @findex fftw_mpi_plan_dft_r2c Chris@10: @findex fftw_mpi_plan_dft_c2r_2d Chris@10: @findex fftw_mpi_plan_dft_c2r_2d Chris@10: @findex fftw_mpi_plan_dft_c2r_3d Chris@10: @findex fftw_mpi_plan_dft_c2r Chris@10: @example Chris@10: fftw_plan fftw_mpi_plan_dft_r2c_2d(ptrdiff_t n0, ptrdiff_t n1, Chris@10: double *in, fftw_complex *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_r2c_2d(ptrdiff_t n0, ptrdiff_t n1, Chris@10: double *in, fftw_complex *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_r2c_3d(ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t n2, Chris@10: double *in, fftw_complex *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_r2c(int rnk, const ptrdiff_t *n, Chris@10: double *in, fftw_complex *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_c2r_2d(ptrdiff_t n0, ptrdiff_t n1, Chris@10: fftw_complex *in, double *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_c2r_2d(ptrdiff_t n0, ptrdiff_t n1, Chris@10: fftw_complex *in, double *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_c2r_3d(ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t n2, Chris@10: fftw_complex *in, double *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_dft_c2r(int rnk, const ptrdiff_t *n, Chris@10: fftw_complex *in, double *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: @end example Chris@10: Chris@10: Similar to the serial interface (@pxref{Real-data DFTs}), these Chris@10: transform logically @ndims{} real data to/from @ndimshalf{} complex Chris@10: data, representing the non-redundant half of the conjugate-symmetry Chris@10: output of a real-input DFT (@pxref{Multi-dimensional Transforms}). Chris@10: However, the real array must be stored within a padded @ndimspad{} Chris@10: array (much like the in-place serial r2c transforms, but here for Chris@10: out-of-place transforms as well). Currently, only multi-dimensional Chris@10: (@code{rnk > 1}) r2c/c2r transforms are supported (requesting a plan Chris@10: for @code{rnk = 1} will yield @code{NULL}). As explained above Chris@10: (@pxref{Multi-dimensional MPI DFTs of Real Data}), the data Chris@10: distribution of both the real and complex arrays is given by the Chris@10: @samp{local_size} function called for the dimensions of the Chris@10: @emph{complex} array. Similar to the other planning functions, the Chris@10: input and output arrays are overwritten when the plan is created Chris@10: except in @code{FFTW_ESTIMATE} mode. Chris@10: Chris@10: As for the complex DFTs above, there is an advance interface that Chris@10: allows you to manually specify block sizes and to transform contiguous Chris@10: @code{howmany}-tuples of real/complex numbers: Chris@10: Chris@10: @findex fftw_mpi_plan_many_dft_r2c Chris@10: @findex fftw_mpi_plan_many_dft_c2r Chris@10: @example Chris@10: fftw_plan fftw_mpi_plan_many_dft_r2c Chris@10: (int rnk, const ptrdiff_t *n, ptrdiff_t howmany, Chris@10: ptrdiff_t iblock, ptrdiff_t oblock, Chris@10: double *in, fftw_complex *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_many_dft_c2r Chris@10: (int rnk, const ptrdiff_t *n, ptrdiff_t howmany, Chris@10: ptrdiff_t iblock, ptrdiff_t oblock, Chris@10: fftw_complex *in, double *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: @end example Chris@10: Chris@10: @subsubheading MPI r2r transforms Chris@10: Chris@10: @cindex r2r Chris@10: There are corresponding plan-creation routines for r2r Chris@10: transforms (@pxref{More DFTs of Real Data}), currently supporting Chris@10: multidimensional (@code{rnk > 1}) transforms only (@code{rnk = 1} will Chris@10: yield a @code{NULL} plan): Chris@10: Chris@10: @example Chris@10: fftw_plan fftw_mpi_plan_r2r_2d(ptrdiff_t n0, ptrdiff_t n1, Chris@10: double *in, double *out, Chris@10: MPI_Comm comm, Chris@10: fftw_r2r_kind kind0, fftw_r2r_kind kind1, Chris@10: unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_r2r_3d(ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t n2, Chris@10: double *in, double *out, Chris@10: MPI_Comm comm, Chris@10: fftw_r2r_kind kind0, fftw_r2r_kind kind1, fftw_r2r_kind kind2, Chris@10: unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_r2r(int rnk, const ptrdiff_t *n, Chris@10: double *in, double *out, Chris@10: MPI_Comm comm, const fftw_r2r_kind *kind, Chris@10: unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_many_r2r(int rnk, const ptrdiff_t *n, Chris@10: ptrdiff_t iblock, ptrdiff_t oblock, Chris@10: double *in, double *out, Chris@10: MPI_Comm comm, const fftw_r2r_kind *kind, Chris@10: unsigned flags); Chris@10: @end example Chris@10: Chris@10: The parameters are much the same as for the complex DFTs above, except Chris@10: that the arrays are of real numbers (and hence the outputs of the Chris@10: @samp{local_size} data-distribution functions should be interpreted as Chris@10: counts of real rather than complex numbers). Also, the @code{kind} Chris@10: parameters specify the r2r kinds along each dimension as for the Chris@10: serial interface (@pxref{Real-to-Real Transform Kinds}). @xref{Other Chris@10: Multi-dimensional Real-data MPI Transforms}. Chris@10: Chris@10: @subsubheading MPI transposition Chris@10: @cindex transpose Chris@10: Chris@10: FFTW also provides routines to plan a transpose of a distributed Chris@10: @code{n0} by @code{n1} array of real numbers, or an array of Chris@10: @code{howmany}-tuples of real numbers with specified block sizes Chris@10: (@pxref{FFTW MPI Transposes}): Chris@10: Chris@10: @findex fftw_mpi_plan_transpose Chris@10: @findex fftw_mpi_plan_many_transpose Chris@10: @example Chris@10: fftw_plan fftw_mpi_plan_transpose(ptrdiff_t n0, ptrdiff_t n1, Chris@10: double *in, double *out, Chris@10: MPI_Comm comm, unsigned flags); Chris@10: fftw_plan fftw_mpi_plan_many_transpose Chris@10: (ptrdiff_t n0, ptrdiff_t n1, ptrdiff_t howmany, Chris@10: ptrdiff_t block0, ptrdiff_t block1, Chris@10: double *in, double *out, MPI_Comm comm, unsigned flags); Chris@10: @end example Chris@10: Chris@10: @cindex new-array execution Chris@10: @findex fftw_mpi_execute_r2r Chris@10: These plans are used with the @code{fftw_mpi_execute_r2r} new-array Chris@10: execute function (@pxref{Using MPI Plans }), since they count as (rank Chris@10: zero) r2r plans from FFTW's perspective. Chris@10: Chris@10: @node MPI Wisdom Communication, , MPI Plan Creation, FFTW MPI Reference Chris@10: @subsection MPI Wisdom Communication Chris@10: Chris@10: To facilitate synchronizing wisdom among the different MPI processes, Chris@10: we provide two functions: Chris@10: Chris@10: @findex fftw_mpi_gather_wisdom Chris@10: @findex fftw_mpi_broadcast_wisdom Chris@10: @example Chris@10: void fftw_mpi_gather_wisdom(MPI_Comm comm); Chris@10: void fftw_mpi_broadcast_wisdom(MPI_Comm comm); Chris@10: @end example Chris@10: Chris@10: The @code{fftw_mpi_gather_wisdom} function gathers all wisdom in the Chris@10: given communicator @code{comm} to the process of rank 0 in the Chris@10: communicator: that process obtains the union of all wisdom on all the Chris@10: processes. As a side effect, some other processes will gain Chris@10: additional wisdom from other processes, but only process 0 will gain Chris@10: the complete union. Chris@10: Chris@10: The @code{fftw_mpi_broadcast_wisdom} does the reverse: it exports Chris@10: wisdom from process 0 in @code{comm} to all other processes in the Chris@10: communicator, replacing any wisdom they currently have. Chris@10: Chris@10: @xref{FFTW MPI Wisdom}. Chris@10: Chris@10: @c ------------------------------------------------------------ Chris@10: @node FFTW MPI Fortran Interface, , FFTW MPI Reference, Distributed-memory FFTW with MPI Chris@10: @section FFTW MPI Fortran Interface Chris@10: @cindex Fortran interface Chris@10: Chris@10: @cindex iso_c_binding Chris@10: The FFTW MPI interface is callable from modern Fortran compilers Chris@10: supporting the Fortran 2003 @code{iso_c_binding} standard for calling Chris@10: C functions. As described in @ref{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: @itemize @bullet Chris@10: Chris@10: @item Chris@10: Instead of including @code{fftw3.f03} as in @ref{Overview of Fortran Chris@10: interface }, you should @code{include 'fftw3-mpi.f03'} (after Chris@10: @code{use, intrinsic :: iso_c_binding} as before). The Chris@10: @code{fftw3-mpi.f03} file includes @code{fftw3.f03}, so you should Chris@10: @emph{not} @code{include} them both yourself. (You will also want to Chris@10: include the MPI header file, usually via @code{include 'mpif.h'} or Chris@10: similar, although though this is not needed by @code{fftw3-mpi.f03} Chris@10: @i{per se}.) (To use the @samp{fftwl_} @code{long double} extended-precision routines in supporting compilers, you should include @code{fftw3f-mpi.f03} in @emph{addition} to @code{fftw3-mpi.f03}. @xref{Extended and quadruple precision in Fortran}.) Chris@10: Chris@10: @item Chris@10: Because of the different storage conventions between C and Fortran, Chris@10: you reverse the order of your array dimensions when passing them to Chris@10: FFTW (@pxref{Reversing array dimensions}). This is merely a Chris@10: difference in notation and incurs no performance overhead. However, Chris@10: it means that, whereas in C the @emph{first} dimension is distributed, Chris@10: in Fortran the @emph{last} dimension of your array is distributed. Chris@10: Chris@10: @item Chris@10: @cindex MPI communicator Chris@10: In Fortran, communicators are stored as @code{integer} types; there is Chris@10: no @code{MPI_Comm} type, nor is there any way to access a C Chris@10: @code{MPI_Comm}. Fortunately, this is taken care of for you by the Chris@10: FFTW Fortran interface: whenever the C interface expects an Chris@10: @code{MPI_Comm} type, you should pass the Fortran communicator as an Chris@10: @code{integer}.@footnote{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 @code{MPI_Comm_f2c} Chris@10: before calling the ordinary C interface. This is all done Chris@10: transparently, however, since the @code{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: @item Chris@10: Because you need to call the @samp{local_size} function to find out Chris@10: how much space to allocate, and this may be @emph{larger} than the Chris@10: local portion of the array (@pxref{MPI Data Distribution}), you should Chris@10: @emph{always} allocate your arrays dynamically using FFTW's allocation Chris@10: routines as described in @ref{Allocating aligned memory in Fortran}. Chris@10: (Coincidentally, this also provides the best performance by Chris@10: guaranteeding proper data alignment.) Chris@10: Chris@10: @item Chris@10: Because all sizes in the MPI FFTW interface are declared as Chris@10: @code{ptrdiff_t} in C, you should use @code{integer(C_INTPTR_T)} in Chris@10: Fortran (@pxref{FFTW Fortran type reference}). Chris@10: Chris@10: @item Chris@10: @findex fftw_execute_dft Chris@10: @findex fftw_mpi_execute_dft Chris@10: @cindex new-array execution Chris@10: In Fortran, because of the language semantics, we generally recommend Chris@10: using the new-array execute functions for all plans, even in the Chris@10: common case where you are executing the plan on the same arrays for Chris@10: which the plan was created (@pxref{Plan execution in Fortran}). Chris@10: However, note that in the MPI interface these functions are changed: Chris@10: @code{fftw_execute_dft} becomes @code{fftw_mpi_execute_dft}, Chris@10: etcetera. @xref{Using MPI Plans}. Chris@10: Chris@10: @end itemize Chris@10: Chris@10: For example, here is a Fortran code snippet to perform a distributed Chris@10: @twodims{L,M} complex DFT in-place. (This assumes you have already Chris@10: initialized MPI with @code{MPI_init} and have also performed Chris@10: @code{call fftw_mpi_init}.) Chris@10: Chris@10: @example 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: ! @r{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: ! @r{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: ! @r{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: ! @r{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: @end example Chris@10: Chris@10: Note that when we called @code{fftw_mpi_local_size_2d} and Chris@10: @code{fftw_mpi_plan_dft_2d} with the dimensions in reversed order, Chris@10: since a @twodims{L,M} Fortran array is viewed by FFTW in C as a Chris@10: @twodims{M, L} array. This means that the array was distributed over Chris@10: the @code{M} dimension, the local portion of which is a Chris@10: @twodims{L,local_M} array in Fortran. (You must @emph{not} use an Chris@10: @code{allocate} statement to allocate an @twodims{L,local_M} array, Chris@10: however; you must allocate @code{alloc_local} complex numbers, which Chris@10: may be greater than @code{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 @code{local_j_offset} Chris@10: argument can conveniently be interpreted as an offset in the 1-based Chris@10: @code{j} index (rather than as a starting index as in C). Chris@10: Chris@10: If instead you had used the @code{ior(FFTW_MEASURE, Chris@10: FFTW_MPI_TRANSPOSED_OUT)} flag, the output of the transform would be a Chris@10: transposed @twodims{M,local_L} array, associated with the @emph{same} Chris@10: @code{cdata} allocation (since the transform is in-place), and which Chris@10: you could declare with: Chris@10: Chris@10: @example Chris@10: complex(C_DOUBLE_COMPLEX), pointer :: tdata(:,:) Chris@10: ... Chris@10: call c_f_pointer(cdata, tdata, [M,local_L]) Chris@10: @end example Chris@10: Chris@10: where @code{local_L} would have been obtained by changing the Chris@10: @code{fftw_mpi_local_size_2d} call to: Chris@10: Chris@10: @example 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: @end example