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