cannam@127
|
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
cannam@127
|
2 <html>
|
cannam@127
|
3 <!-- This manual is for FFTW
|
cannam@127
|
4 (version 3.3.5, 30 July 2016).
|
cannam@127
|
5
|
cannam@127
|
6 Copyright (C) 2003 Matteo Frigo.
|
cannam@127
|
7
|
cannam@127
|
8 Copyright (C) 2003 Massachusetts Institute of Technology.
|
cannam@127
|
9
|
cannam@127
|
10 Permission is granted to make and distribute verbatim copies of this
|
cannam@127
|
11 manual provided the copyright notice and this permission notice are
|
cannam@127
|
12 preserved on all copies.
|
cannam@127
|
13
|
cannam@127
|
14 Permission is granted to copy and distribute modified versions of this
|
cannam@127
|
15 manual under the conditions for verbatim copying, provided that the
|
cannam@127
|
16 entire resulting derived work is distributed under the terms of a
|
cannam@127
|
17 permission notice identical to this one.
|
cannam@127
|
18
|
cannam@127
|
19 Permission is granted to copy and distribute translations of this manual
|
cannam@127
|
20 into another language, under the above conditions for modified versions,
|
cannam@127
|
21 except that this permission notice may be stated in a translation
|
cannam@127
|
22 approved by the Free Software Foundation. -->
|
cannam@127
|
23 <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
|
cannam@127
|
24 <head>
|
cannam@127
|
25 <title>FFTW 3.3.5: Reversing array dimensions</title>
|
cannam@127
|
26
|
cannam@127
|
27 <meta name="description" content="FFTW 3.3.5: Reversing array dimensions">
|
cannam@127
|
28 <meta name="keywords" content="FFTW 3.3.5: Reversing array dimensions">
|
cannam@127
|
29 <meta name="resource-type" content="document">
|
cannam@127
|
30 <meta name="distribution" content="global">
|
cannam@127
|
31 <meta name="Generator" content="makeinfo">
|
cannam@127
|
32 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
cannam@127
|
33 <link href="index.html#Top" rel="start" title="Top">
|
cannam@127
|
34 <link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
|
cannam@127
|
35 <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
cannam@127
|
36 <link href="Calling-FFTW-from-Modern-Fortran.html#Calling-FFTW-from-Modern-Fortran" rel="up" title="Calling FFTW from Modern Fortran">
|
cannam@127
|
37 <link href="FFTW-Fortran-type-reference.html#FFTW-Fortran-type-reference" rel="next" title="FFTW Fortran type reference">
|
cannam@127
|
38 <link href="Extended-and-quadruple-precision-in-Fortran.html#Extended-and-quadruple-precision-in-Fortran" rel="prev" title="Extended and quadruple precision in Fortran">
|
cannam@127
|
39 <style type="text/css">
|
cannam@127
|
40 <!--
|
cannam@127
|
41 a.summary-letter {text-decoration: none}
|
cannam@127
|
42 blockquote.smallquotation {font-size: smaller}
|
cannam@127
|
43 div.display {margin-left: 3.2em}
|
cannam@127
|
44 div.example {margin-left: 3.2em}
|
cannam@127
|
45 div.indentedblock {margin-left: 3.2em}
|
cannam@127
|
46 div.lisp {margin-left: 3.2em}
|
cannam@127
|
47 div.smalldisplay {margin-left: 3.2em}
|
cannam@127
|
48 div.smallexample {margin-left: 3.2em}
|
cannam@127
|
49 div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
|
cannam@127
|
50 div.smalllisp {margin-left: 3.2em}
|
cannam@127
|
51 kbd {font-style:oblique}
|
cannam@127
|
52 pre.display {font-family: inherit}
|
cannam@127
|
53 pre.format {font-family: inherit}
|
cannam@127
|
54 pre.menu-comment {font-family: serif}
|
cannam@127
|
55 pre.menu-preformatted {font-family: serif}
|
cannam@127
|
56 pre.smalldisplay {font-family: inherit; font-size: smaller}
|
cannam@127
|
57 pre.smallexample {font-size: smaller}
|
cannam@127
|
58 pre.smallformat {font-family: inherit; font-size: smaller}
|
cannam@127
|
59 pre.smalllisp {font-size: smaller}
|
cannam@127
|
60 span.nocodebreak {white-space:nowrap}
|
cannam@127
|
61 span.nolinebreak {white-space:nowrap}
|
cannam@127
|
62 span.roman {font-family:serif; font-weight:normal}
|
cannam@127
|
63 span.sansserif {font-family:sans-serif; font-weight:normal}
|
cannam@127
|
64 ul.no-bullet {list-style: none}
|
cannam@127
|
65 -->
|
cannam@127
|
66 </style>
|
cannam@127
|
67
|
cannam@127
|
68
|
cannam@127
|
69 </head>
|
cannam@127
|
70
|
cannam@127
|
71 <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
|
cannam@127
|
72 <a name="Reversing-array-dimensions"></a>
|
cannam@127
|
73 <div class="header">
|
cannam@127
|
74 <p>
|
cannam@127
|
75 Next: <a href="FFTW-Fortran-type-reference.html#FFTW-Fortran-type-reference" accesskey="n" rel="next">FFTW Fortran type reference</a>, Previous: <a href="Overview-of-Fortran-interface.html#Overview-of-Fortran-interface" accesskey="p" rel="prev">Overview of Fortran interface</a>, Up: <a href="Calling-FFTW-from-Modern-Fortran.html#Calling-FFTW-from-Modern-Fortran" accesskey="u" rel="up">Calling FFTW from Modern Fortran</a> [<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@127
|
76 </div>
|
cannam@127
|
77 <hr>
|
cannam@127
|
78 <a name="Reversing-array-dimensions-1"></a>
|
cannam@127
|
79 <h3 class="section">7.2 Reversing array dimensions</h3>
|
cannam@127
|
80
|
cannam@127
|
81 <a name="index-row_002dmajor-6"></a>
|
cannam@127
|
82 <a name="index-column_002dmajor-1"></a>
|
cannam@127
|
83 <p>A minor annoyance in calling FFTW from Fortran is that FFTW’s array
|
cannam@127
|
84 dimensions are defined in the C convention (row-major order), while
|
cannam@127
|
85 Fortran’s array dimensions are the opposite convention (column-major
|
cannam@127
|
86 order). See <a href="Multi_002ddimensional-Array-Format.html#Multi_002ddimensional-Array-Format">Multi-dimensional Array Format</a>. This is just a
|
cannam@127
|
87 bookkeeping difference, with no effect on performance. The only
|
cannam@127
|
88 consequence of this is that, whenever you create an FFTW plan for a
|
cannam@127
|
89 multi-dimensional transform, you must always <em>reverse the
|
cannam@127
|
90 ordering of the dimensions</em>.
|
cannam@127
|
91 </p>
|
cannam@127
|
92 <p>For example, consider the three-dimensional (L × M × N) arrays:
|
cannam@127
|
93 </p>
|
cannam@127
|
94 <div class="example">
|
cannam@127
|
95 <pre class="example"> complex(C_DOUBLE_COMPLEX), dimension(L,M,N) :: in, out
|
cannam@127
|
96 </pre></div>
|
cannam@127
|
97
|
cannam@127
|
98 <p>To plan a DFT for these arrays using <code>fftw_plan_dft_3d</code>, you could do:
|
cannam@127
|
99 </p>
|
cannam@127
|
100 <a name="index-fftw_005fplan_005fdft_005f3d-2"></a>
|
cannam@127
|
101 <div class="example">
|
cannam@127
|
102 <pre class="example"> plan = fftw_plan_dft_3d(N,M,L, in,out, FFTW_FORWARD,FFTW_ESTIMATE)
|
cannam@127
|
103 </pre></div>
|
cannam@127
|
104
|
cannam@127
|
105 <p>That is, from FFTW’s perspective this is a N × M × L array.
|
cannam@127
|
106 <em>No data transposition need occur</em>, as this is <em>only
|
cannam@127
|
107 notation</em>. Similarly, to use the more generic routine
|
cannam@127
|
108 <code>fftw_plan_dft</code> with the same arrays, you could do:
|
cannam@127
|
109 </p>
|
cannam@127
|
110 <div class="example">
|
cannam@127
|
111 <pre class="example"> integer(C_INT), dimension(3) :: n = [N,M,L]
|
cannam@127
|
112 plan = fftw_plan_dft_3d(3, n, in,out, FFTW_FORWARD,FFTW_ESTIMATE)
|
cannam@127
|
113 </pre></div>
|
cannam@127
|
114
|
cannam@127
|
115 <p>Note, by the way, that this is different from the legacy Fortran
|
cannam@127
|
116 interface (see <a href="Fortran_002dinterface-routines.html#Fortran_002dinterface-routines">Fortran-interface routines</a>), which automatically
|
cannam@127
|
117 reverses the order of the array dimension for you. Here, you are
|
cannam@127
|
118 calling the C interface directly, so there is no “translation” layer.
|
cannam@127
|
119 </p>
|
cannam@127
|
120 <a name="index-r2c_002fc2r-multi_002ddimensional-array-format-2"></a>
|
cannam@127
|
121 <p>An important thing to keep in mind is the implication of this for
|
cannam@127
|
122 multidimensional real-to-complex transforms (see <a href="Multi_002dDimensional-DFTs-of-Real-Data.html#Multi_002dDimensional-DFTs-of-Real-Data">Multi-Dimensional DFTs of Real Data</a>). In C, a multidimensional real-to-complex DFT
|
cannam@127
|
123 chops the last dimension roughly in half (N × M × L real input
|
cannam@127
|
124 goes to N × M × L/2+1 complex output). In Fortran, because
|
cannam@127
|
125 the array dimension notation is reversed, the <em>first</em> dimension of
|
cannam@127
|
126 the complex data is chopped roughly in half. For example consider the
|
cannam@127
|
127 ‘<samp>r2c</samp>’ transform of L × M × N real input in Fortran:
|
cannam@127
|
128 </p>
|
cannam@127
|
129 <a name="index-fftw_005fplan_005fdft_005fr2c_005f3d-2"></a>
|
cannam@127
|
130 <a name="index-fftw_005fexecute_005fdft_005fr2c-1"></a>
|
cannam@127
|
131 <div class="example">
|
cannam@127
|
132 <pre class="example"> type(C_PTR) :: plan
|
cannam@127
|
133 real(C_DOUBLE), dimension(L,M,N) :: in
|
cannam@127
|
134 complex(C_DOUBLE_COMPLEX), dimension(L/2+1,M,N) :: out
|
cannam@127
|
135 plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE)
|
cannam@127
|
136 ...
|
cannam@127
|
137 call fftw_execute_dft_r2c(plan, in, out)
|
cannam@127
|
138 </pre></div>
|
cannam@127
|
139
|
cannam@127
|
140 <a name="index-in_002dplace-9"></a>
|
cannam@127
|
141 <a name="index-padding-5"></a>
|
cannam@127
|
142 <p>Alternatively, for an in-place r2c transform, as described in the C
|
cannam@127
|
143 documentation we must <em>pad</em> the <em>first</em> dimension of the
|
cannam@127
|
144 real input with an extra two entries (which are ignored by FFTW) so as
|
cannam@127
|
145 to leave enough space for the complex output. The input is
|
cannam@127
|
146 <em>allocated</em> as a 2[L/2+1] × M × N array, even though only
|
cannam@127
|
147 L × M × N of it is actually used. In this example, we will
|
cannam@127
|
148 allocate the array as a pointer type, using ‘<samp>fftw_alloc</samp>’ to
|
cannam@127
|
149 ensure aligned memory for maximum performance (see <a href="Allocating-aligned-memory-in-Fortran.html#Allocating-aligned-memory-in-Fortran">Allocating aligned memory in Fortran</a>); this also makes it easy to reference the
|
cannam@127
|
150 same memory as both a real array and a complex array.
|
cannam@127
|
151 </p>
|
cannam@127
|
152 <a name="index-fftw_005falloc_005fcomplex-4"></a>
|
cannam@127
|
153 <a name="index-c_005ff_005fpointer"></a>
|
cannam@127
|
154 <div class="example">
|
cannam@127
|
155 <pre class="example"> real(C_DOUBLE), pointer :: in(:,:,:)
|
cannam@127
|
156 complex(C_DOUBLE_COMPLEX), pointer :: out(:,:,:)
|
cannam@127
|
157 type(C_PTR) :: plan, data
|
cannam@127
|
158 data = fftw_alloc_complex(int((L/2+1) * M * N, C_SIZE_T))
|
cannam@127
|
159 call c_f_pointer(data, in, [2*(L/2+1),M,N])
|
cannam@127
|
160 call c_f_pointer(data, out, [L/2+1,M,N])
|
cannam@127
|
161 plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE)
|
cannam@127
|
162 ...
|
cannam@127
|
163 call fftw_execute_dft_r2c(plan, in, out)
|
cannam@127
|
164 ...
|
cannam@127
|
165 call fftw_destroy_plan(plan)
|
cannam@127
|
166 call fftw_free(data)
|
cannam@127
|
167 </pre></div>
|
cannam@127
|
168
|
cannam@127
|
169 <hr>
|
cannam@127
|
170 <div class="header">
|
cannam@127
|
171 <p>
|
cannam@127
|
172 Next: <a href="FFTW-Fortran-type-reference.html#FFTW-Fortran-type-reference" accesskey="n" rel="next">FFTW Fortran type reference</a>, Previous: <a href="Overview-of-Fortran-interface.html#Overview-of-Fortran-interface" accesskey="p" rel="prev">Overview of Fortran interface</a>, Up: <a href="Calling-FFTW-from-Modern-Fortran.html#Calling-FFTW-from-Modern-Fortran" accesskey="u" rel="up">Calling FFTW from Modern Fortran</a> [<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@127
|
173 </div>
|
cannam@127
|
174
|
cannam@127
|
175
|
cannam@127
|
176
|
cannam@127
|
177 </body>
|
cannam@127
|
178 </html>
|