annotate fft/fftw/fftw-3.3.4/doc/html/Allocating-aligned-memory-in-Fortran.html @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 <html lang="en">
Chris@19 2 <head>
Chris@19 3 <title>Allocating aligned memory in Fortran - FFTW 3.3.4</title>
Chris@19 4 <meta http-equiv="Content-Type" content="text/html">
Chris@19 5 <meta name="description" content="FFTW 3.3.4">
Chris@19 6 <meta name="generator" content="makeinfo 4.13">
Chris@19 7 <link title="Top" rel="start" href="index.html#Top">
Chris@19 8 <link rel="up" href="Calling-FFTW-from-Modern-Fortran.html#Calling-FFTW-from-Modern-Fortran" title="Calling FFTW from Modern Fortran">
Chris@19 9 <link rel="prev" href="Plan-execution-in-Fortran.html#Plan-execution-in-Fortran" title="Plan execution in Fortran">
Chris@19 10 <link rel="next" href="Accessing-the-wisdom-API-from-Fortran.html#Accessing-the-wisdom-API-from-Fortran" title="Accessing the wisdom API from Fortran">
Chris@19 11 <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
Chris@19 12 <!--
Chris@19 13 This manual is for FFTW
Chris@19 14 (version 3.3.4, 20 September 2013).
Chris@19 15
Chris@19 16 Copyright (C) 2003 Matteo Frigo.
Chris@19 17
Chris@19 18 Copyright (C) 2003 Massachusetts Institute of Technology.
Chris@19 19
Chris@19 20 Permission is granted to make and distribute verbatim copies of
Chris@19 21 this manual provided the copyright notice and this permission
Chris@19 22 notice are preserved on all copies.
Chris@19 23
Chris@19 24 Permission is granted to copy and distribute modified versions of
Chris@19 25 this manual under the conditions for verbatim copying, provided
Chris@19 26 that the entire resulting derived work is distributed under the
Chris@19 27 terms of a permission notice identical to this one.
Chris@19 28
Chris@19 29 Permission is granted to copy and distribute translations of this
Chris@19 30 manual into another language, under the above conditions for
Chris@19 31 modified versions, except that this permission notice may be
Chris@19 32 stated in a translation approved by the Free Software Foundation.
Chris@19 33 -->
Chris@19 34 <meta http-equiv="Content-Style-Type" content="text/css">
Chris@19 35 <style type="text/css"><!--
Chris@19 36 pre.display { font-family:inherit }
Chris@19 37 pre.format { font-family:inherit }
Chris@19 38 pre.smalldisplay { font-family:inherit; font-size:smaller }
Chris@19 39 pre.smallformat { font-family:inherit; font-size:smaller }
Chris@19 40 pre.smallexample { font-size:smaller }
Chris@19 41 pre.smalllisp { font-size:smaller }
Chris@19 42 span.sc { font-variant:small-caps }
Chris@19 43 span.roman { font-family:serif; font-weight:normal; }
Chris@19 44 span.sansserif { font-family:sans-serif; font-weight:normal; }
Chris@19 45 --></style>
Chris@19 46 </head>
Chris@19 47 <body>
Chris@19 48 <div class="node">
Chris@19 49 <a name="Allocating-aligned-memory-in-Fortran"></a>
Chris@19 50 <p>
Chris@19 51 Next:&nbsp;<a rel="next" accesskey="n" href="Accessing-the-wisdom-API-from-Fortran.html#Accessing-the-wisdom-API-from-Fortran">Accessing the wisdom API from Fortran</a>,
Chris@19 52 Previous:&nbsp;<a rel="previous" accesskey="p" href="Plan-execution-in-Fortran.html#Plan-execution-in-Fortran">Plan execution in Fortran</a>,
Chris@19 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>
Chris@19 54 <hr>
Chris@19 55 </div>
Chris@19 56
Chris@19 57 <h3 class="section">7.5 Allocating aligned memory in Fortran</h3>
Chris@19 58
Chris@19 59 <p><a name="index-alignment-563"></a><a name="index-fftw_005falloc_005freal-564"></a><a name="index-fftw_005falloc_005fcomplex-565"></a>In order to obtain maximum performance in FFTW, you should store your
Chris@19 60 data in arrays that have been specially aligned in memory (see <a href="SIMD-alignment-and-fftw_005fmalloc.html#SIMD-alignment-and-fftw_005fmalloc">SIMD alignment and fftw_malloc</a>). Enforcing alignment also permits you to
Chris@19 61 safely use the new-array execute functions (see <a href="New_002darray-Execute-Functions.html#New_002darray-Execute-Functions">New-array Execute Functions</a>) to apply a given plan to more than one pair of in/out
Chris@19 62 arrays. Unfortunately, standard Fortran arrays do <em>not</em> provide
Chris@19 63 any alignment guarantees. The <em>only</em> way to allocate aligned
Chris@19 64 memory in standard Fortran is to allocate it with an external C
Chris@19 65 function, like the <code>fftw_alloc_real</code> and
Chris@19 66 <code>fftw_alloc_complex</code> functions. Fortunately, Fortran 2003 provides
Chris@19 67 a simple way to associate such allocated memory with a standard Fortran
Chris@19 68 array pointer that you can then use normally.
Chris@19 69
Chris@19 70 <p>We therefore recommend allocating all your input/output arrays using
Chris@19 71 the following technique:
Chris@19 72
Chris@19 73 <ol type=1 start=1>
Chris@19 74
Chris@19 75 <li>Declare a <code>pointer</code>, <code>arr</code>, to your array of the desired type
Chris@19 76 and dimensions. For example, <code>real(C_DOUBLE), pointer :: a(:,:)</code>
Chris@19 77 for a 2d real array, or <code>complex(C_DOUBLE_COMPLEX), pointer ::
Chris@19 78 a(:,:,:)</code> for a 3d complex array.
Chris@19 79
Chris@19 80 <li>The number of elements to allocate must be an
Chris@19 81 <code>integer(C_SIZE_T)</code>. You can either declare a variable of this
Chris@19 82 type, e.g. <code>integer(C_SIZE_T) :: sz</code>, to store the number of
Chris@19 83 elements to allocate, or you can use the <code>int(..., C_SIZE_T)</code>
Chris@19 84 intrinsic function. e.g. set <code>sz = L * M * N</code> or use
Chris@19 85 <code>int(L * M * N, C_SIZE_T)</code> for an L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N array.
Chris@19 86
Chris@19 87 <li>Declare a <code>type(C_PTR) :: p</code> to hold the return value from
Chris@19 88 FFTW's allocation routine. Set <code>p = fftw_alloc_real(sz)</code> for a real array, or <code>p = fftw_alloc_complex(sz)</code> for a complex array.
Chris@19 89
Chris@19 90 <li><a name="index-c_005ff_005fpointer-566"></a>Associate your pointer <code>arr</code> with the allocated memory <code>p</code>
Chris@19 91 using the standard <code>c_f_pointer</code> subroutine: <code>call
Chris@19 92 c_f_pointer(p, arr, [...dimensions...])</code>, where
Chris@19 93 <code>[...dimensions...])</code> are an array of the dimensions of the array
Chris@19 94 (in the usual Fortran order). e.g. <code>call c_f_pointer(p, arr,
Chris@19 95 [L,M,N])</code> for an L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N array. (Alternatively, you can
Chris@19 96 omit the dimensions argument if you specified the shape explicitly
Chris@19 97 when declaring <code>arr</code>.) You can now use <code>arr</code> as a usual
Chris@19 98 multidimensional array.
Chris@19 99
Chris@19 100 <li>When you are done using the array, deallocate the memory by <code>call
Chris@19 101 fftw_free(p)</code> on <code>p</code>.
Chris@19 102
Chris@19 103 </ol>
Chris@19 104
Chris@19 105 <p>For example, here is how we would allocate an L&nbsp;&times;&nbsp;M 2d real array:
Chris@19 106
Chris@19 107 <pre class="example"> real(C_DOUBLE), pointer :: arr(:,:)
Chris@19 108 type(C_PTR) :: p
Chris@19 109 p = fftw_alloc_real(int(L * M, C_SIZE_T))
Chris@19 110 call c_f_pointer(p, arr, [L,M])
Chris@19 111 <em>...use arr and arr(i,j) as usual...</em>
Chris@19 112 call fftw_free(p)
Chris@19 113 </pre>
Chris@19 114 <p>and here is an L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N 3d complex array:
Chris@19 115
Chris@19 116 <pre class="example"> complex(C_DOUBLE_COMPLEX), pointer :: arr(:,:,:)
Chris@19 117 type(C_PTR) :: p
Chris@19 118 p = fftw_alloc_complex(int(L * M * N, C_SIZE_T))
Chris@19 119 call c_f_pointer(p, arr, [L,M,N])
Chris@19 120 <em>...use arr and arr(i,j,k) as usual...</em>
Chris@19 121 call fftw_free(p)
Chris@19 122 </pre>
Chris@19 123 <p>See <a href="Reversing-array-dimensions.html#Reversing-array-dimensions">Reversing array dimensions</a> for an example allocating a
Chris@19 124 single array and associating both real and complex array pointers with
Chris@19 125 it, for in-place real-to-complex transforms.
Chris@19 126
Chris@19 127 <!-- -->
Chris@19 128 </body></html>
Chris@19 129