comparison src/fftw-3.3.3/doc/html/FFTW-MPI-Fortran-Interface.html @ 10:37bf6b4a2645

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