annotate src/fftw-3.3.3/doc/html/Allocating-aligned-memory-in-Fortran.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>Allocating aligned memory in Fortran - 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="Plan-execution-in-Fortran.html#Plan-execution-in-Fortran" title="Plan execution in Fortran">
cannam@95 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">
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="Allocating-aligned-memory-in-Fortran"></a>
cannam@95 50 <p>
cannam@95 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>,
cannam@95 52 Previous:&nbsp;<a rel="previous" accesskey="p" href="Plan-execution-in-Fortran.html#Plan-execution-in-Fortran">Plan execution in Fortran</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.5 Allocating aligned memory in Fortran</h3>
cannam@95 58
cannam@95 59 <p><a name="index-alignment-560"></a><a name="index-fftw_005falloc_005freal-561"></a><a name="index-fftw_005falloc_005fcomplex-562"></a>In order to obtain maximum performance in FFTW, you should store your
cannam@95 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
cannam@95 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
cannam@95 62 arrays. Unfortunately, standard Fortran arrays do <em>not</em> provide
cannam@95 63 any alignment guarantees. The <em>only</em> way to allocate aligned
cannam@95 64 memory in standard Fortran is to allocate it with an external C
cannam@95 65 function, like the <code>fftw_alloc_real</code> and
cannam@95 66 <code>fftw_alloc_complex</code> functions. Fortunately, Fortran 2003 provides
cannam@95 67 a simple way to associate such allocated memory with a standard Fortran
cannam@95 68 array pointer that you can then use normally.
cannam@95 69
cannam@95 70 <p>We therefore recommend allocating all your input/output arrays using
cannam@95 71 the following technique:
cannam@95 72
cannam@95 73 <ol type=1 start=1>
cannam@95 74
cannam@95 75 <li>Declare a <code>pointer</code>, <code>arr</code>, to your array of the desired type
cannam@95 76 and dimensions. For example, <code>real(C_DOUBLE), pointer :: a(:,:)</code>
cannam@95 77 for a 2d real array, or <code>complex(C_DOUBLE_COMPLEX), pointer ::
cannam@95 78 a(:,:,:)</code> for a 3d complex array.
cannam@95 79
cannam@95 80 <li>The number of elements to allocate must be an
cannam@95 81 <code>integer(C_SIZE_T)</code>. You can either declare a variable of this
cannam@95 82 type, e.g. <code>integer(C_SIZE_T) :: sz</code>, to store the number of
cannam@95 83 elements to allocate, or you can use the <code>int(..., C_SIZE_T)</code>
cannam@95 84 intrinsic function. e.g. set <code>sz = L * M * N</code> or use
cannam@95 85 <code>int(L * M * N, C_SIZE_T)</code> for an L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N array.
cannam@95 86
cannam@95 87 <li>Declare a <code>type(C_PTR) :: p</code> to hold the return value from
cannam@95 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.
cannam@95 89
cannam@95 90 <li><a name="index-c_005ff_005fpointer-563"></a>Associate your pointer <code>arr</code> with the allocated memory <code>p</code>
cannam@95 91 using the standard <code>c_f_pointer</code> subroutine: <code>call
cannam@95 92 c_f_pointer(p, arr, [...dimensions...])</code>, where
cannam@95 93 <code>[...dimensions...])</code> are an array of the dimensions of the array
cannam@95 94 (in the usual Fortran order). e.g. <code>call c_f_pointer(p, arr,
cannam@95 95 [L,M,N])</code> for an L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N array. (Alternatively, you can
cannam@95 96 omit the dimensions argument if you specified the shape explicitly
cannam@95 97 when declaring <code>arr</code>.) You can now use <code>arr</code> as a usual
cannam@95 98 multidimensional array.
cannam@95 99
cannam@95 100 <li>When you are done using the array, deallocate the memory by <code>call
cannam@95 101 fftw_free(p)</code> on <code>p</code>.
cannam@95 102
cannam@95 103 </ol>
cannam@95 104
cannam@95 105 <p>For example, here is how we would allocate an L&nbsp;&times;&nbsp;M 2d real array:
cannam@95 106
cannam@95 107 <pre class="example"> real(C_DOUBLE), pointer :: arr(:,:)
cannam@95 108 type(C_PTR) :: p
cannam@95 109 p = fftw_alloc_real(int(L * M, C_SIZE_T))
cannam@95 110 call c_f_pointer(p, arr, [L,M])
cannam@95 111 <em>...use arr and arr(i,j) as usual...</em>
cannam@95 112 call fftw_free(p)
cannam@95 113 </pre>
cannam@95 114 <p>and here is an L&nbsp;&times;&nbsp;M&nbsp;&times;&nbsp;N 3d complex array:
cannam@95 115
cannam@95 116 <pre class="example"> complex(C_DOUBLE_COMPLEX), pointer :: arr(:,:,:)
cannam@95 117 type(C_PTR) :: p
cannam@95 118 p = fftw_alloc_complex(int(L * M * N, C_SIZE_T))
cannam@95 119 call c_f_pointer(p, arr, [L,M,N])
cannam@95 120 <em>...use arr and arr(i,j,k) as usual...</em>
cannam@95 121 call fftw_free(p)
cannam@95 122 </pre>
cannam@95 123 <p>See <a href="Reversing-array-dimensions.html#Reversing-array-dimensions">Reversing array dimensions</a> for an example allocating a
cannam@95 124 single array and associating both real and complex array pointers with
cannam@95 125 it, for in-place real-to-complex transforms.
cannam@95 126
cannam@95 127 <!-- -->
cannam@95 128 </body></html>
cannam@95 129