annotate src/fftw-3.3.3/doc/html/Reversing-array-dimensions.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>Reversing array dimensions - 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="Calling-FFTW-from-Modern-Fortran.html#Calling-FFTW-from-Modern-Fortran" title="Calling FFTW from Modern Fortran">
cannam@95 9 <link rel="prev" href="Overview-of-Fortran-interface.html#Overview-of-Fortran-interface" title="Overview of Fortran interface">
cannam@95 10 <link rel="next" href="FFTW-Fortran-type-reference.html#FFTW-Fortran-type-reference" title="FFTW Fortran type reference">
cannam@95 11 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
cannam@95 12 <!--
cannam@95 13 This manual is for FFTW
cannam@95 14 (version 3.3.3, 25 November 2012).
cannam@95 15
cannam@95 16 Copyright (C) 2003 Matteo Frigo.
cannam@95 17
cannam@95 18 Copyright (C) 2003 Massachusetts Institute of Technology.
cannam@95 19
cannam@95 20 Permission is granted to make and distribute verbatim copies of
cannam@95 21 this manual provided the copyright notice and this permission
cannam@95 22 notice are preserved on all copies.
cannam@95 23
cannam@95 24 Permission is granted to copy and distribute modified versions of
cannam@95 25 this manual under the conditions for verbatim copying, provided
cannam@95 26 that the entire resulting derived work is distributed under the
cannam@95 27 terms of a permission notice identical to this one.
cannam@95 28
cannam@95 29 Permission is granted to copy and distribute translations of this
cannam@95 30 manual into another language, under the above conditions for
cannam@95 31 modified versions, except that this permission notice may be
cannam@95 32 stated in a translation approved by the Free Software Foundation.
cannam@95 33 -->
cannam@95 34 <meta http-equiv="Content-Style-Type" content="text/css">
cannam@95 35 <style type="text/css"><!--
cannam@95 36 pre.display { font-family:inherit }
cannam@95 37 pre.format { font-family:inherit }
cannam@95 38 pre.smalldisplay { font-family:inherit; font-size:smaller }
cannam@95 39 pre.smallformat { font-family:inherit; font-size:smaller }
cannam@95 40 pre.smallexample { font-size:smaller }
cannam@95 41 pre.smalllisp { font-size:smaller }
cannam@95 42 span.sc { font-variant:small-caps }
cannam@95 43 span.roman { font-family:serif; font-weight:normal; }
cannam@95 44 span.sansserif { font-family:sans-serif; font-weight:normal; }
cannam@95 45 --></style>
cannam@95 46 </head>
cannam@95 47 <body>
cannam@95 48 <div class="node">
cannam@95 49 <a name="Reversing-array-dimensions"></a>
cannam@95 50 <p>
cannam@95 51 Next:&nbsp;<a rel="next" accesskey="n" href="FFTW-Fortran-type-reference.html#FFTW-Fortran-type-reference">FFTW Fortran type reference</a>,
cannam@95 52 Previous:&nbsp;<a rel="previous" accesskey="p" href="Overview-of-Fortran-interface.html#Overview-of-Fortran-interface">Overview of Fortran interface</a>,
cannam@95 53 Up:&nbsp;<a rel="up" accesskey="u" href="Calling-FFTW-from-Modern-Fortran.html#Calling-FFTW-from-Modern-Fortran">Calling FFTW from Modern Fortran</a>
cannam@95 54 <hr>
cannam@95 55 </div>
cannam@95 56
cannam@95 57 <h3 class="section">7.2 Reversing array dimensions</h3>
cannam@95 58
cannam@95 59 <p><a name="index-row_002dmajor-517"></a><a name="index-column_002dmajor-518"></a>A minor annoyance in calling FFTW from Fortran is that FFTW's array
cannam@95 60 dimensions are defined in the C convention (row-major order), while
cannam@95 61 Fortran's array dimensions are the opposite convention (column-major
cannam@95 62 order). See <a href="Multi_002ddimensional-Array-Format.html#Multi_002ddimensional-Array-Format">Multi-dimensional Array Format</a>. This is just a
cannam@95 63 bookkeeping difference, with no effect on performance. The only
cannam@95 64 consequence of this is that, whenever you create an FFTW plan for a
cannam@95 65 multi-dimensional transform, you must always <em>reverse the
cannam@95 66 ordering of the dimensions</em>.
cannam@95 67
cannam@95 68 <p>For example, consider the three-dimensional (L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N) arrays:
cannam@95 69
cannam@95 70 <pre class="example"> complex(C_DOUBLE_COMPLEX), dimension(L,M,N) :: in, out
cannam@95 71 </pre>
cannam@95 72 <p>To plan a DFT for these arrays using <code>fftw_plan_dft_3d</code>, you could do:
cannam@95 73
cannam@95 74 <p><a name="index-fftw_005fplan_005fdft_005f3d-519"></a>
cannam@95 75 <pre class="example"> plan = fftw_plan_dft_3d(N,M,L, in,out, FFTW_FORWARD,FFTW_ESTIMATE)
cannam@95 76 </pre>
cannam@95 77 <p>That is, from FFTW's perspective this is a N&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;L array.
cannam@95 78 <em>No data transposition need occur</em>, as this is <em>only
cannam@95 79 notation</em>. Similarly, to use the more generic routine
cannam@95 80 <code>fftw_plan_dft</code> with the same arrays, you could do:
cannam@95 81
cannam@95 82 <pre class="example"> integer(C_INT), dimension(3) :: n = [N,M,L]
cannam@95 83 plan = fftw_plan_dft_3d(3, n, in,out, FFTW_FORWARD,FFTW_ESTIMATE)
cannam@95 84 </pre>
cannam@95 85 <p>Note, by the way, that this is different from the legacy Fortran
cannam@95 86 interface (see <a href="Fortran_002dinterface-routines.html#Fortran_002dinterface-routines">Fortran-interface routines</a>), which automatically
cannam@95 87 reverses the order of the array dimension for you. Here, you are
cannam@95 88 calling the C interface directly, so there is no &ldquo;translation&rdquo; layer.
cannam@95 89
cannam@95 90 <p><a name="index-r2c_002fc2r-multi_002ddimensional-array-format-520"></a>An important thing to keep in mind is the implication of this for
cannam@95 91 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@95 92 chops the last dimension roughly in half (N&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;L real input
cannam@95 93 goes to N&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;L/2+1 complex output). In Fortran, because
cannam@95 94 the array dimension notation is reversed, the <em>first</em> dimension of
cannam@95 95 the complex data is chopped roughly in half. For example consider the
cannam@95 96 &lsquo;<samp><span class="samp">r2c</span></samp>&rsquo; transform of L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N real input in Fortran:
cannam@95 97
cannam@95 98 <p><a name="index-fftw_005fplan_005fdft_005fr2c_005f3d-521"></a><a name="index-fftw_005fexecute_005fdft_005fr2c-522"></a>
cannam@95 99 <pre class="example"> type(C_PTR) :: plan
cannam@95 100 real(C_DOUBLE), dimension(L,M,N) :: in
cannam@95 101 complex(C_DOUBLE_COMPLEX), dimension(L/2+1,M,N) :: out
cannam@95 102 plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE)
cannam@95 103 ...
cannam@95 104 call fftw_execute_dft_r2c(plan, in, out)
cannam@95 105 </pre>
cannam@95 106 <p><a name="index-in_002dplace-523"></a><a name="index-padding-524"></a>Alternatively, for an in-place r2c transform, as described in the C
cannam@95 107 documentation we must <em>pad</em> the <em>first</em> dimension of the
cannam@95 108 real input with an extra two entries (which are ignored by FFTW) so as
cannam@95 109 to leave enough space for the complex output. The input is
cannam@95 110 <em>allocated</em> as a 2[L/2+1]&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N array, even though only
cannam@95 111 L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N of it is actually used. In this example, we will
cannam@95 112 allocate the array as a pointer type, using &lsquo;<samp><span class="samp">fftw_alloc</span></samp>&rsquo; to
cannam@95 113 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@95 114 same memory as both a real array and a complex array.
cannam@95 115
cannam@95 116 <p><a name="index-fftw_005falloc_005fcomplex-525"></a><a name="index-c_005ff_005fpointer-526"></a>
cannam@95 117 <pre class="example"> real(C_DOUBLE), pointer :: in(:,:,:)
cannam@95 118 complex(C_DOUBLE_COMPLEX), pointer :: out(:,:,:)
cannam@95 119 type(C_PTR) :: plan, data
cannam@95 120 data = fftw_alloc_complex(int((L/2+1) * M * N, C_SIZE_T))
cannam@95 121 call c_f_pointer(data, in, [2*(L/2+1),M,N])
cannam@95 122 call c_f_pointer(data, out, [L/2+1,M,N])
cannam@95 123 plan = fftw_plan_dft_r2c_3d(N,M,L, in,out, FFTW_ESTIMATE)
cannam@95 124 ...
cannam@95 125 call fftw_execute_dft_r2c(plan, in, out)
cannam@95 126 ...
cannam@95 127 call fftw_destroy_plan(plan)
cannam@95 128 call fftw_free(data)
cannam@95 129 </pre>
cannam@95 130 <!-- -->
cannam@95 131 </body></html>
cannam@95 132