annotate src/fftw-3.3.3/doc/html/FFTW-MPI-Fortran-Interface.html @ 95:89f5e221ed7b

Add FFTW3
author Chris Cannam <cannam@all-day-breakfast.com>
date Wed, 20 Mar 2013 15:35:50 +0000
parents
children
rev   line source
cannam@95 1 <html lang="en">
cannam@95 2 <head>
cannam@95 3 <title>FFTW MPI Fortran Interface - FFTW 3.3.3</title>
cannam@95 4 <meta http-equiv="Content-Type" content="text/html">
cannam@95 5 <meta name="description" content="FFTW 3.3.3">
cannam@95 6 <meta name="generator" content="makeinfo 4.13">
cannam@95 7 <link title="Top" rel="start" href="index.html#Top">
cannam@95 8 <link rel="up" href="Distributed_002dmemory-FFTW-with-MPI.html#Distributed_002dmemory-FFTW-with-MPI" title="Distributed-memory FFTW with MPI">
cannam@95 9 <link rel="prev" href="FFTW-MPI-Reference.html#FFTW-MPI-Reference" title="FFTW MPI Reference">
cannam@95 10 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
cannam@95 11 <!--
cannam@95 12 This manual is for FFTW
cannam@95 13 (version 3.3.3, 25 November 2012).
cannam@95 14
cannam@95 15 Copyright (C) 2003 Matteo Frigo.
cannam@95 16
cannam@95 17 Copyright (C) 2003 Massachusetts Institute of Technology.
cannam@95 18
cannam@95 19 Permission is granted to make and distribute verbatim copies of
cannam@95 20 this manual provided the copyright notice and this permission
cannam@95 21 notice are preserved on all copies.
cannam@95 22
cannam@95 23 Permission is granted to copy and distribute modified versions of
cannam@95 24 this manual under the conditions for verbatim copying, provided
cannam@95 25 that the entire resulting derived work is distributed under the
cannam@95 26 terms of a permission notice identical to this one.
cannam@95 27
cannam@95 28 Permission is granted to copy and distribute translations of this
cannam@95 29 manual into another language, under the above conditions for
cannam@95 30 modified versions, except that this permission notice may be
cannam@95 31 stated in a translation approved by the Free Software Foundation.
cannam@95 32 -->
cannam@95 33 <meta http-equiv="Content-Style-Type" content="text/css">
cannam@95 34 <style type="text/css"><!--
cannam@95 35 pre.display { font-family:inherit }
cannam@95 36 pre.format { font-family:inherit }
cannam@95 37 pre.smalldisplay { font-family:inherit; font-size:smaller }
cannam@95 38 pre.smallformat { font-family:inherit; font-size:smaller }
cannam@95 39 pre.smallexample { font-size:smaller }
cannam@95 40 pre.smalllisp { font-size:smaller }
cannam@95 41 span.sc { font-variant:small-caps }
cannam@95 42 span.roman { font-family:serif; font-weight:normal; }
cannam@95 43 span.sansserif { font-family:sans-serif; font-weight:normal; }
cannam@95 44 --></style>
cannam@95 45 </head>
cannam@95 46 <body>
cannam@95 47 <div class="node">
cannam@95 48 <a name="FFTW-MPI-Fortran-Interface"></a>
cannam@95 49 <p>
cannam@95 50 Previous:&nbsp;<a rel="previous" accesskey="p" href="FFTW-MPI-Reference.html#FFTW-MPI-Reference">FFTW MPI Reference</a>,
cannam@95 51 Up:&nbsp;<a rel="up" accesskey="u" href="Distributed_002dmemory-FFTW-with-MPI.html#Distributed_002dmemory-FFTW-with-MPI">Distributed-memory FFTW with MPI</a>
cannam@95 52 <hr>
cannam@95 53 </div>
cannam@95 54
cannam@95 55 <h3 class="section">6.13 FFTW MPI Fortran Interface</h3>
cannam@95 56
cannam@95 57 <p><a name="index-Fortran-interface-494"></a>
cannam@95 58 <a name="index-iso_005fc_005fbinding-495"></a>The FFTW MPI interface is callable from modern Fortran compilers
cannam@95 59 supporting the Fortran 2003 <code>iso_c_binding</code> standard for calling
cannam@95 60 C functions. As described in <a href="Calling-FFTW-from-Modern-Fortran.html#Calling-FFTW-from-Modern-Fortran">Calling FFTW from Modern Fortran</a>,
cannam@95 61 this means that you can directly call FFTW's C interface from Fortran
cannam@95 62 with only minor changes in syntax. There are, however, a few things
cannam@95 63 specific to the MPI interface to keep in mind:
cannam@95 64
cannam@95 65 <ul>
cannam@95 66 <li>Instead of including <code>fftw3.f03</code> as in <a href="Overview-of-Fortran-interface.html#Overview-of-Fortran-interface">Overview of Fortran interface</a>, you should <code>include 'fftw3-mpi.f03'</code> (after
cannam@95 67 <code>use, intrinsic :: iso_c_binding</code> as before). The
cannam@95 68 <code>fftw3-mpi.f03</code> file includes <code>fftw3.f03</code>, so you should
cannam@95 69 <em>not</em> <code>include</code> them both yourself. (You will also want to
cannam@95 70 include the MPI header file, usually via <code>include 'mpif.h'</code> or
cannam@95 71 similar, although though this is not needed by <code>fftw3-mpi.f03</code>
cannam@95 72 <i>per se</i>.) (To use the &lsquo;<samp><span class="samp">fftwl_</span></samp>&rsquo; <code>long double</code> extended-precision routines in supporting compilers, you should include <code>fftw3f-mpi.f03</code> in <em>addition</em> to <code>fftw3-mpi.f03</code>. See <a href="Extended-and-quadruple-precision-in-Fortran.html#Extended-and-quadruple-precision-in-Fortran">Extended and quadruple precision in Fortran</a>.)
cannam@95 73
cannam@95 74 <li>Because of the different storage conventions between C and Fortran,
cannam@95 75 you reverse the order of your array dimensions when passing them to
cannam@95 76 FFTW (see <a href="Reversing-array-dimensions.html#Reversing-array-dimensions">Reversing array dimensions</a>). This is merely a
cannam@95 77 difference in notation and incurs no performance overhead. However,
cannam@95 78 it means that, whereas in C the <em>first</em> dimension is distributed,
cannam@95 79 in Fortran the <em>last</em> dimension of your array is distributed.
cannam@95 80
cannam@95 81 <li><a name="index-MPI-communicator-496"></a>In Fortran, communicators are stored as <code>integer</code> types; there is
cannam@95 82 no <code>MPI_Comm</code> type, nor is there any way to access a C
cannam@95 83 <code>MPI_Comm</code>. Fortunately, this is taken care of for you by the
cannam@95 84 FFTW Fortran interface: whenever the C interface expects an
cannam@95 85 <code>MPI_Comm</code> type, you should pass the Fortran communicator as an
cannam@95 86 <code>integer</code>.<a rel="footnote" href="#fn-1" name="fnd-1"><sup>1</sup></a>
cannam@95 87
cannam@95 88 <li>Because you need to call the &lsquo;<samp><span class="samp">local_size</span></samp>&rsquo; function to find out
cannam@95 89 how much space to allocate, and this may be <em>larger</em> than the
cannam@95 90 local portion of the array (see <a href="MPI-Data-Distribution.html#MPI-Data-Distribution">MPI Data Distribution</a>), you should
cannam@95 91 <em>always</em> allocate your arrays dynamically using FFTW's allocation
cannam@95 92 routines as described in <a href="Allocating-aligned-memory-in-Fortran.html#Allocating-aligned-memory-in-Fortran">Allocating aligned memory in Fortran</a>.
cannam@95 93 (Coincidentally, this also provides the best performance by
cannam@95 94 guaranteeding proper data alignment.)
cannam@95 95
cannam@95 96 <li>Because all sizes in the MPI FFTW interface are declared as
cannam@95 97 <code>ptrdiff_t</code> in C, you should use <code>integer(C_INTPTR_T)</code> in
cannam@95 98 Fortran (see <a href="FFTW-Fortran-type-reference.html#FFTW-Fortran-type-reference">FFTW Fortran type reference</a>).
cannam@95 99
cannam@95 100 <li><a name="index-fftw_005fexecute_005fdft-497"></a><a name="index-fftw_005fmpi_005fexecute_005fdft-498"></a><a name="index-new_002darray-execution-499"></a>In Fortran, because of the language semantics, we generally recommend
cannam@95 101 using the new-array execute functions for all plans, even in the
cannam@95 102 common case where you are executing the plan on the same arrays for
cannam@95 103 which the plan was created (see <a href="Plan-execution-in-Fortran.html#Plan-execution-in-Fortran">Plan execution in Fortran</a>).
cannam@95 104 However, note that in the MPI interface these functions are changed:
cannam@95 105 <code>fftw_execute_dft</code> becomes <code>fftw_mpi_execute_dft</code>,
cannam@95 106 etcetera. See <a href="Using-MPI-Plans.html#Using-MPI-Plans">Using MPI Plans</a>.
cannam@95 107
cannam@95 108 </ul>
cannam@95 109
cannam@95 110 <p>For example, here is a Fortran code snippet to perform a distributed
cannam@95 111 L&nbsp;&times;&nbsp;M complex DFT in-place. (This assumes you have already
cannam@95 112 initialized MPI with <code>MPI_init</code> and have also performed
cannam@95 113 <code>call fftw_mpi_init</code>.)
cannam@95 114
cannam@95 115 <pre class="example"> use, intrinsic :: iso_c_binding
cannam@95 116 include 'fftw3-mpi.f03'
cannam@95 117 integer(C_INTPTR_T), parameter :: L = ...
cannam@95 118 integer(C_INTPTR_T), parameter :: M = ...
cannam@95 119 type(C_PTR) :: plan, cdata
cannam@95 120 complex(C_DOUBLE_COMPLEX), pointer :: data(:,:)
cannam@95 121 integer(C_INTPTR_T) :: i, j, alloc_local, local_M, local_j_offset
cannam@95 122
cannam@95 123 ! <span class="roman">get local data size and allocate (note dimension reversal)</span>
cannam@95 124 alloc_local = fftw_mpi_local_size_2d(M, L, MPI_COMM_WORLD, &amp;
cannam@95 125 local_M, local_j_offset)
cannam@95 126 cdata = fftw_alloc_complex(alloc_local)
cannam@95 127 call c_f_pointer(cdata, data, [L,local_M])
cannam@95 128
cannam@95 129 ! <span class="roman">create MPI plan for in-place forward DFT (note dimension reversal)</span>
cannam@95 130 plan = fftw_mpi_plan_dft_2d(M, L, data, data, MPI_COMM_WORLD, &amp;
cannam@95 131 FFTW_FORWARD, FFTW_MEASURE)
cannam@95 132
cannam@95 133 ! <span class="roman">initialize data to some function</span> my_function(i,j)
cannam@95 134 do j = 1, local_M
cannam@95 135 do i = 1, L
cannam@95 136 data(i, j) = my_function(i, j + local_j_offset)
cannam@95 137 end do
cannam@95 138 end do
cannam@95 139
cannam@95 140 ! <span class="roman">compute transform (as many times as desired)</span>
cannam@95 141 call fftw_mpi_execute_dft(plan, data, data)
cannam@95 142
cannam@95 143 call fftw_destroy_plan(plan)
cannam@95 144 call fftw_free(cdata)
cannam@95 145 </pre>
cannam@95 146 <p>Note that when we called <code>fftw_mpi_local_size_2d</code> and
cannam@95 147 <code>fftw_mpi_plan_dft_2d</code> with the dimensions in reversed order,
cannam@95 148 since a L&nbsp;&times;&nbsp;M Fortran array is viewed by FFTW in C as a
cannam@95 149 M&nbsp;&times;&nbsp;L array. This means that the array was distributed over
cannam@95 150 the <code>M</code> dimension, the local portion of which is a
cannam@95 151 L&nbsp;&times;&nbsp;local_M array in Fortran. (You must <em>not</em> use an
cannam@95 152 <code>allocate</code> statement to allocate an L&nbsp;&times;&nbsp;local_M array,
cannam@95 153 however; you must allocate <code>alloc_local</code> complex numbers, which
cannam@95 154 may be greater than <code>L * local_M</code>, in order to reserve space for
cannam@95 155 intermediate steps of the transform.) Finally, we mention that
cannam@95 156 because C's array indices are zero-based, the <code>local_j_offset</code>
cannam@95 157 argument can conveniently be interpreted as an offset in the 1-based
cannam@95 158 <code>j</code> index (rather than as a starting index as in C).
cannam@95 159
cannam@95 160 <p>If instead you had used the <code>ior(FFTW_MEASURE,
cannam@95 161 FFTW_MPI_TRANSPOSED_OUT)</code> flag, the output of the transform would be a
cannam@95 162 transposed M&nbsp;&times;&nbsp;local_L array, associated with the <em>same</em>
cannam@95 163 <code>cdata</code> allocation (since the transform is in-place), and which
cannam@95 164 you could declare with:
cannam@95 165
cannam@95 166 <pre class="example"> complex(C_DOUBLE_COMPLEX), pointer :: tdata(:,:)
cannam@95 167 ...
cannam@95 168 call c_f_pointer(cdata, tdata, [M,local_L])
cannam@95 169 </pre>
cannam@95 170 <p>where <code>local_L</code> would have been obtained by changing the
cannam@95 171 <code>fftw_mpi_local_size_2d</code> call to:
cannam@95 172
cannam@95 173 <pre class="example"> alloc_local = fftw_mpi_local_size_2d_transposed(M, L, MPI_COMM_WORLD, &amp;
cannam@95 174 local_M, local_j_offset, local_L, local_i_offset)
cannam@95 175 </pre>
cannam@95 176 <div class="footnote">
cannam@95 177 <hr>
cannam@95 178 <h4>Footnotes</h4><p class="footnote"><small>[<a name="fn-1" href="#fnd-1">1</a>]</small> Technically, this is because you aren't
cannam@95 179 actually calling the C functions directly. You are calling wrapper
cannam@95 180 functions that translate the communicator with <code>MPI_Comm_f2c</code>
cannam@95 181 before calling the ordinary C interface. This is all done
cannam@95 182 transparently, however, since the <code>fftw3-mpi.f03</code> interface file
cannam@95 183 renames the wrappers so that they are called in Fortran with the same
cannam@95 184 names as the C interface functions.</p>
cannam@95 185
cannam@95 186 <hr></div>
cannam@95 187
cannam@95 188 </body></html>
cannam@95 189