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