annotate src/fftw-3.3.8/doc/html/FFTW-MPI-Fortran-Interface.html @ 169:223a55898ab9 tip default

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