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