annotate src/fftw-3.3.3/doc/other.texi @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 89f5e221ed7b
children
rev   line source
cannam@95 1 @node Other Important Topics, FFTW Reference, Tutorial, Top
cannam@95 2 @chapter Other Important Topics
cannam@95 3 @menu
cannam@95 4 * SIMD alignment and fftw_malloc::
cannam@95 5 * Multi-dimensional Array Format::
cannam@95 6 * Words of Wisdom-Saving Plans::
cannam@95 7 * Caveats in Using Wisdom::
cannam@95 8 @end menu
cannam@95 9
cannam@95 10 @c ------------------------------------------------------------
cannam@95 11 @node SIMD alignment and fftw_malloc, Multi-dimensional Array Format, Other Important Topics, Other Important Topics
cannam@95 12 @section SIMD alignment and fftw_malloc
cannam@95 13
cannam@95 14 SIMD, which stands for ``Single Instruction Multiple Data,'' is a set of
cannam@95 15 special operations supported by some processors to perform a single
cannam@95 16 operation on several numbers (usually 2 or 4) simultaneously. SIMD
cannam@95 17 floating-point instructions are available on several popular CPUs:
cannam@95 18 SSE/SSE2/AVX on recent x86/x86-64 processors, AltiVec (single precision)
cannam@95 19 on some PowerPCs (Apple G4 and higher), NEON on some ARM models, and MIPS Paired Single
cannam@95 20 (currently only in FFTW 3.2.x). FFTW can be compiled to support the
cannam@95 21 SIMD instructions on any of these systems.
cannam@95 22 @cindex SIMD
cannam@95 23 @cindex SSE
cannam@95 24 @cindex SSE2
cannam@95 25 @cindex AVX
cannam@95 26 @cindex AltiVec
cannam@95 27 @cindex MIPS PS
cannam@95 28 @cindex precision
cannam@95 29
cannam@95 30
cannam@95 31 A program linking to an FFTW library compiled with SIMD support can
cannam@95 32 obtain a nonnegligible speedup for most complex and r2c/c2r
cannam@95 33 transforms. In order to obtain this speedup, however, the arrays of
cannam@95 34 complex (or real) data passed to FFTW must be specially aligned in
cannam@95 35 memory (typically 16-byte aligned), and often this alignment is more
cannam@95 36 stringent than that provided by the usual @code{malloc} (etc.)
cannam@95 37 allocation routines.
cannam@95 38
cannam@95 39 @cindex portability
cannam@95 40 In order to guarantee proper alignment for SIMD, therefore, in case
cannam@95 41 your program is ever linked against a SIMD-using FFTW, we recommend
cannam@95 42 allocating your transform data with @code{fftw_malloc} and
cannam@95 43 de-allocating it with @code{fftw_free}.
cannam@95 44 @findex fftw_malloc
cannam@95 45 @findex fftw_free
cannam@95 46 These have exactly the same interface and behavior as
cannam@95 47 @code{malloc}/@code{free}, except that for a SIMD FFTW they ensure
cannam@95 48 that the returned pointer has the necessary alignment (by calling
cannam@95 49 @code{memalign} or its equivalent on your OS).
cannam@95 50
cannam@95 51 You are not @emph{required} to use @code{fftw_malloc}. You can
cannam@95 52 allocate your data in any way that you like, from @code{malloc} to
cannam@95 53 @code{new} (in C++) to a fixed-size array declaration. If the array
cannam@95 54 happens not to be properly aligned, FFTW will not use the SIMD
cannam@95 55 extensions.
cannam@95 56 @cindex C++
cannam@95 57
cannam@95 58 @findex fftw_alloc_real
cannam@95 59 @findex fftw_alloc_complex
cannam@95 60 Since @code{fftw_malloc} only ever needs to be used for real and
cannam@95 61 complex arrays, we provide two convenient wrapper routines
cannam@95 62 @code{fftw_alloc_real(N)} and @code{fftw_alloc_complex(N)} that are
cannam@95 63 equivalent to @code{(double*)fftw_malloc(sizeof(double) * N)} and
cannam@95 64 @code{(fftw_complex*)fftw_malloc(sizeof(fftw_complex) * N)},
cannam@95 65 respectively (or their equivalents in other precisions).
cannam@95 66
cannam@95 67 @c ------------------------------------------------------------
cannam@95 68 @node Multi-dimensional Array Format, Words of Wisdom-Saving Plans, SIMD alignment and fftw_malloc, Other Important Topics
cannam@95 69 @section Multi-dimensional Array Format
cannam@95 70
cannam@95 71 This section describes the format in which multi-dimensional arrays
cannam@95 72 are stored in FFTW. We felt that a detailed discussion of this topic
cannam@95 73 was necessary. Since several different formats are common, this topic
cannam@95 74 is often a source of confusion.
cannam@95 75
cannam@95 76 @menu
cannam@95 77 * Row-major Format::
cannam@95 78 * Column-major Format::
cannam@95 79 * Fixed-size Arrays in C::
cannam@95 80 * Dynamic Arrays in C::
cannam@95 81 * Dynamic Arrays in C-The Wrong Way::
cannam@95 82 @end menu
cannam@95 83
cannam@95 84 @c =========>
cannam@95 85 @node Row-major Format, Column-major Format, Multi-dimensional Array Format, Multi-dimensional Array Format
cannam@95 86 @subsection Row-major Format
cannam@95 87 @cindex row-major
cannam@95 88
cannam@95 89 The multi-dimensional arrays passed to @code{fftw_plan_dft} etcetera
cannam@95 90 are expected to be stored as a single contiguous block in
cannam@95 91 @dfn{row-major} order (sometimes called ``C order''). Basically, this
cannam@95 92 means that as you step through adjacent memory locations, the first
cannam@95 93 dimension's index varies most slowly and the last dimension's index
cannam@95 94 varies most quickly.
cannam@95 95
cannam@95 96 To be more explicit, let us consider an array of rank @math{d} whose
cannam@95 97 dimensions are @ndims{}. Now, we specify a location in the array by a
cannam@95 98 sequence of @math{d} (zero-based) indices, one for each dimension:
cannam@95 99 @tex
cannam@95 100 $(i_0, i_1, i_2, \ldots, i_{d-1})$.
cannam@95 101 @end tex
cannam@95 102 @ifinfo
cannam@95 103 (i[0], i[1], ..., i[d-1]).
cannam@95 104 @end ifinfo
cannam@95 105 @html
cannam@95 106 (i<sub>0</sub>, i<sub>1</sub>, i<sub>2</sub>,..., i<sub>d-1</sub>).
cannam@95 107 @end html
cannam@95 108 If the array is stored in row-major
cannam@95 109 order, then this element is located at the position
cannam@95 110 @tex
cannam@95 111 $i_{d-1} + n_{d-1} (i_{d-2} + n_{d-2} (\ldots + n_1 i_0))$.
cannam@95 112 @end tex
cannam@95 113 @ifinfo
cannam@95 114 i[d-1] + n[d-1] * (i[d-2] + n[d-2] * (... + n[1] * i[0])).
cannam@95 115 @end ifinfo
cannam@95 116 @html
cannam@95 117 i<sub>d-1</sub> + n<sub>d-1</sub> * (i<sub>d-2</sub> + n<sub>d-2</sub> * (... + n<sub>1</sub> * i<sub>0</sub>)).
cannam@95 118 @end html
cannam@95 119
cannam@95 120 Note that, for the ordinary complex DFT, each element of the array
cannam@95 121 must be of type @code{fftw_complex}; i.e. a (real, imaginary) pair of
cannam@95 122 (double-precision) numbers.
cannam@95 123
cannam@95 124 In the advanced FFTW interface, the physical dimensions @math{n} from
cannam@95 125 which the indices are computed can be different from (larger than)
cannam@95 126 the logical dimensions of the transform to be computed, in order to
cannam@95 127 transform a subset of a larger array.
cannam@95 128 @cindex advanced interface
cannam@95 129 Note also that, in the advanced interface, the expression above is
cannam@95 130 multiplied by a @dfn{stride} to get the actual array index---this is
cannam@95 131 useful in situations where each element of the multi-dimensional array
cannam@95 132 is actually a data structure (or another array), and you just want to
cannam@95 133 transform a single field. In the basic interface, however, the stride
cannam@95 134 is 1.
cannam@95 135 @cindex stride
cannam@95 136
cannam@95 137 @c =========>
cannam@95 138 @node Column-major Format, Fixed-size Arrays in C, Row-major Format, Multi-dimensional Array Format
cannam@95 139 @subsection Column-major Format
cannam@95 140 @cindex column-major
cannam@95 141
cannam@95 142 Readers from the Fortran world are used to arrays stored in
cannam@95 143 @dfn{column-major} order (sometimes called ``Fortran order''). This is
cannam@95 144 essentially the exact opposite of row-major order in that, here, the
cannam@95 145 @emph{first} dimension's index varies most quickly.
cannam@95 146
cannam@95 147 If you have an array stored in column-major order and wish to
cannam@95 148 transform it using FFTW, it is quite easy to do. When creating the
cannam@95 149 plan, simply pass the dimensions of the array to the planner in
cannam@95 150 @emph{reverse order}. For example, if your array is a rank three
cannam@95 151 @code{N x M x L} matrix in column-major order, you should pass the
cannam@95 152 dimensions of the array as if it were an @code{L x M x N} matrix
cannam@95 153 (which it is, from the perspective of FFTW). This is done for you
cannam@95 154 @emph{automatically} by the FFTW legacy-Fortran interface
cannam@95 155 (@pxref{Calling FFTW from Legacy Fortran}), but you must do it
cannam@95 156 manually with the modern Fortran interface (@pxref{Reversing array
cannam@95 157 dimensions}).
cannam@95 158 @cindex Fortran interface
cannam@95 159
cannam@95 160 @c =========>
cannam@95 161 @node Fixed-size Arrays in C, Dynamic Arrays in C, Column-major Format, Multi-dimensional Array Format
cannam@95 162 @subsection Fixed-size Arrays in C
cannam@95 163 @cindex C multi-dimensional arrays
cannam@95 164
cannam@95 165 A multi-dimensional array whose size is declared at compile time in C
cannam@95 166 is @emph{already} in row-major order. You don't have to do anything
cannam@95 167 special to transform it. For example:
cannam@95 168
cannam@95 169 @example
cannam@95 170 @{
cannam@95 171 fftw_complex data[N0][N1][N2];
cannam@95 172 fftw_plan plan;
cannam@95 173 ...
cannam@95 174 plan = fftw_plan_dft_3d(N0, N1, N2, &data[0][0][0], &data[0][0][0],
cannam@95 175 FFTW_FORWARD, FFTW_ESTIMATE);
cannam@95 176 ...
cannam@95 177 @}
cannam@95 178 @end example
cannam@95 179
cannam@95 180 This will plan a 3d in-place transform of size @code{N0 x N1 x N2}.
cannam@95 181 Notice how we took the address of the zero-th element to pass to the
cannam@95 182 planner (we could also have used a typecast).
cannam@95 183
cannam@95 184 However, we tend to @emph{discourage} users from declaring their
cannam@95 185 arrays in this way, for two reasons. First, this allocates the array
cannam@95 186 on the stack (``automatic'' storage), which has a very limited size on
cannam@95 187 most operating systems (declaring an array with more than a few
cannam@95 188 thousand elements will often cause a crash). (You can get around this
cannam@95 189 limitation on many systems by declaring the array as
cannam@95 190 @code{static} and/or global, but that has its own drawbacks.)
cannam@95 191 Second, it may not optimally align the array for use with a SIMD
cannam@95 192 FFTW (@pxref{SIMD alignment and fftw_malloc}). Instead, we recommend
cannam@95 193 using @code{fftw_malloc}, as described below.
cannam@95 194
cannam@95 195 @c =========>
cannam@95 196 @node Dynamic Arrays in C, Dynamic Arrays in C-The Wrong Way, Fixed-size Arrays in C, Multi-dimensional Array Format
cannam@95 197 @subsection Dynamic Arrays in C
cannam@95 198
cannam@95 199 We recommend allocating most arrays dynamically, with
cannam@95 200 @code{fftw_malloc}. This isn't too hard to do, although it is not as
cannam@95 201 straightforward for multi-dimensional arrays as it is for
cannam@95 202 one-dimensional arrays.
cannam@95 203
cannam@95 204 Creating the array is simple: using a dynamic-allocation routine like
cannam@95 205 @code{fftw_malloc}, allocate an array big enough to store N
cannam@95 206 @code{fftw_complex} values (for a complex DFT), where N is the product
cannam@95 207 of the sizes of the array dimensions (i.e. the total number of complex
cannam@95 208 values in the array). For example, here is code to allocate a
cannam@95 209 @threedims{5,12,27} rank-3 array:
cannam@95 210 @findex fftw_malloc
cannam@95 211
cannam@95 212 @example
cannam@95 213 fftw_complex *an_array;
cannam@95 214 an_array = (fftw_complex*) fftw_malloc(5*12*27 * sizeof(fftw_complex));
cannam@95 215 @end example
cannam@95 216
cannam@95 217 Accessing the array elements, however, is more tricky---you can't
cannam@95 218 simply use multiple applications of the @samp{[]} operator like you
cannam@95 219 could for fixed-size arrays. Instead, you have to explicitly compute
cannam@95 220 the offset into the array using the formula given earlier for
cannam@95 221 row-major arrays. For example, to reference the @math{(i,j,k)}-th
cannam@95 222 element of the array allocated above, you would use the expression
cannam@95 223 @code{an_array[k + 27 * (j + 12 * i)]}.
cannam@95 224
cannam@95 225 This pain can be alleviated somewhat by defining appropriate macros,
cannam@95 226 or, in C++, creating a class and overloading the @samp{()} operator.
cannam@95 227 The recent C99 standard provides a way to reinterpret the dynamic
cannam@95 228 array as a ``variable-length'' multi-dimensional array amenable to
cannam@95 229 @samp{[]}, but this feature is not yet widely supported by compilers.
cannam@95 230 @cindex C99
cannam@95 231 @cindex C++
cannam@95 232
cannam@95 233 @c =========>
cannam@95 234 @node Dynamic Arrays in C-The Wrong Way, , Dynamic Arrays in C, Multi-dimensional Array Format
cannam@95 235 @subsection Dynamic Arrays in C---The Wrong Way
cannam@95 236
cannam@95 237 A different method for allocating multi-dimensional arrays in C is
cannam@95 238 often suggested that is incompatible with FFTW: @emph{using it will
cannam@95 239 cause FFTW to die a painful death}. We discuss the technique here,
cannam@95 240 however, because it is so commonly known and used. This method is to
cannam@95 241 create arrays of pointers of arrays of pointers of @dots{}etcetera.
cannam@95 242 For example, the analogue in this method to the example above is:
cannam@95 243
cannam@95 244 @example
cannam@95 245 int i,j;
cannam@95 246 fftw_complex ***a_bad_array; /* @r{another way to make a 5x12x27 array} */
cannam@95 247
cannam@95 248 a_bad_array = (fftw_complex ***) malloc(5 * sizeof(fftw_complex **));
cannam@95 249 for (i = 0; i < 5; ++i) @{
cannam@95 250 a_bad_array[i] =
cannam@95 251 (fftw_complex **) malloc(12 * sizeof(fftw_complex *));
cannam@95 252 for (j = 0; j < 12; ++j)
cannam@95 253 a_bad_array[i][j] =
cannam@95 254 (fftw_complex *) malloc(27 * sizeof(fftw_complex));
cannam@95 255 @}
cannam@95 256 @end example
cannam@95 257
cannam@95 258 As you can see, this sort of array is inconvenient to allocate (and
cannam@95 259 deallocate). On the other hand, it has the advantage that the
cannam@95 260 @math{(i,j,k)}-th element can be referenced simply by
cannam@95 261 @code{a_bad_array[i][j][k]}.
cannam@95 262
cannam@95 263 If you like this technique and want to maximize convenience in accessing
cannam@95 264 the array, but still want to pass the array to FFTW, you can use a
cannam@95 265 hybrid method. Allocate the array as one contiguous block, but also
cannam@95 266 declare an array of arrays of pointers that point to appropriate places
cannam@95 267 in the block. That sort of trick is beyond the scope of this
cannam@95 268 documentation; for more information on multi-dimensional arrays in C,
cannam@95 269 see the @code{comp.lang.c}
cannam@95 270 @uref{http://c-faq.com/aryptr/dynmuldimary.html, FAQ}.
cannam@95 271
cannam@95 272 @c ------------------------------------------------------------
cannam@95 273 @node Words of Wisdom-Saving Plans, Caveats in Using Wisdom, Multi-dimensional Array Format, Other Important Topics
cannam@95 274 @section Words of Wisdom---Saving Plans
cannam@95 275 @cindex wisdom
cannam@95 276 @cindex saving plans to disk
cannam@95 277
cannam@95 278 FFTW implements a method for saving plans to disk and restoring them.
cannam@95 279 In fact, what FFTW does is more general than just saving and loading
cannam@95 280 plans. The mechanism is called @dfn{wisdom}. Here, we describe
cannam@95 281 this feature at a high level. @xref{FFTW Reference}, for a less casual
cannam@95 282 but more complete discussion of how to use wisdom in FFTW.
cannam@95 283
cannam@95 284 Plans created with the @code{FFTW_MEASURE}, @code{FFTW_PATIENT}, or
cannam@95 285 @code{FFTW_EXHAUSTIVE} options produce near-optimal FFT performance,
cannam@95 286 but may require a long time to compute because FFTW must measure the
cannam@95 287 runtime of many possible plans and select the best one. This setup is
cannam@95 288 designed for the situations where so many transforms of the same size
cannam@95 289 must be computed that the start-up time is irrelevant. For short
cannam@95 290 initialization times, but slower transforms, we have provided
cannam@95 291 @code{FFTW_ESTIMATE}. The @code{wisdom} mechanism is a way to get the
cannam@95 292 best of both worlds: you compute a good plan once, save it to
cannam@95 293 disk, and later reload it as many times as necessary. The wisdom
cannam@95 294 mechanism can actually save and reload many plans at once, not just
cannam@95 295 one.
cannam@95 296 @ctindex FFTW_MEASURE
cannam@95 297 @ctindex FFTW_PATIENT
cannam@95 298 @ctindex FFTW_EXHAUSTIVE
cannam@95 299 @ctindex FFTW_ESTIMATE
cannam@95 300
cannam@95 301
cannam@95 302 Whenever you create a plan, the FFTW planner accumulates wisdom, which
cannam@95 303 is information sufficient to reconstruct the plan. After planning,
cannam@95 304 you can save this information to disk by means of the function:
cannam@95 305 @example
cannam@95 306 int fftw_export_wisdom_to_filename(const char *filename);
cannam@95 307 @end example
cannam@95 308 @findex fftw_export_wisdom_to_filename
cannam@95 309 (This function returns non-zero on success.)
cannam@95 310
cannam@95 311 The next time you run the program, you can restore the wisdom with
cannam@95 312 @code{fftw_import_wisdom_from_filename} (which also returns non-zero on success),
cannam@95 313 and then recreate the plan using the same flags as before.
cannam@95 314 @example
cannam@95 315 int fftw_import_wisdom_from_filename(const char *filename);
cannam@95 316 @end example
cannam@95 317 @findex fftw_import_wisdom_from_filename
cannam@95 318
cannam@95 319 Wisdom is automatically used for any size to which it is applicable, as
cannam@95 320 long as the planner flags are not more ``patient'' than those with which
cannam@95 321 the wisdom was created. For example, wisdom created with
cannam@95 322 @code{FFTW_MEASURE} can be used if you later plan with
cannam@95 323 @code{FFTW_ESTIMATE} or @code{FFTW_MEASURE}, but not with
cannam@95 324 @code{FFTW_PATIENT}.
cannam@95 325
cannam@95 326 The @code{wisdom} is cumulative, and is stored in a global, private
cannam@95 327 data structure managed internally by FFTW. The storage space required
cannam@95 328 is minimal, proportional to the logarithm of the sizes the wisdom was
cannam@95 329 generated from. If memory usage is a concern, however, the wisdom can
cannam@95 330 be forgotten and its associated memory freed by calling:
cannam@95 331 @example
cannam@95 332 void fftw_forget_wisdom(void);
cannam@95 333 @end example
cannam@95 334 @findex fftw_forget_wisdom
cannam@95 335
cannam@95 336 Wisdom can be exported to a file, a string, or any other medium.
cannam@95 337 For details, see @ref{Wisdom}.
cannam@95 338
cannam@95 339 @node Caveats in Using Wisdom, , Words of Wisdom-Saving Plans, Other Important Topics
cannam@95 340 @section Caveats in Using Wisdom
cannam@95 341 @cindex wisdom, problems with
cannam@95 342
cannam@95 343 @quotation
cannam@95 344 @html
cannam@95 345 <i>
cannam@95 346 @end html
cannam@95 347 For in much wisdom is much grief, and he that increaseth knowledge
cannam@95 348 increaseth sorrow.
cannam@95 349 @html
cannam@95 350 </i>
cannam@95 351 @end html
cannam@95 352 [Ecclesiastes 1:18]
cannam@95 353 @cindex Ecclesiastes
cannam@95 354 @end quotation
cannam@95 355 @iftex
cannam@95 356 @medskip
cannam@95 357 @end iftex
cannam@95 358
cannam@95 359 @cindex portability
cannam@95 360 There are pitfalls to using wisdom, in that it can negate FFTW's
cannam@95 361 ability to adapt to changing hardware and other conditions. For
cannam@95 362 example, it would be perfectly possible to export wisdom from a
cannam@95 363 program running on one processor and import it into a program running
cannam@95 364 on another processor. Doing so, however, would mean that the second
cannam@95 365 program would use plans optimized for the first processor, instead of
cannam@95 366 the one it is running on.
cannam@95 367
cannam@95 368 It should be safe to reuse wisdom as long as the hardware and program
cannam@95 369 binaries remain unchanged. (Actually, the optimal plan may change even
cannam@95 370 between runs of the same binary on identical hardware, due to
cannam@95 371 differences in the virtual memory environment, etcetera. Users
cannam@95 372 seriously interested in performance should worry about this problem,
cannam@95 373 too.) It is likely that, if the same wisdom is used for two
cannam@95 374 different program binaries, even running on the same machine, the
cannam@95 375 plans may be sub-optimal because of differing code alignments. It is
cannam@95 376 therefore wise to recreate wisdom every time an application is
cannam@95 377 recompiled. The more the underlying hardware and software changes
cannam@95 378 between the creation of wisdom and its use, the greater grows
cannam@95 379 the risk of sub-optimal plans.
cannam@95 380
cannam@95 381 Nevertheless, if the choice is between using @code{FFTW_ESTIMATE} or
cannam@95 382 using possibly-suboptimal wisdom (created on the same machine, but for a
cannam@95 383 different binary), the wisdom is likely to be better. For this reason,
cannam@95 384 we provide a function to import wisdom from a standard system-wide
cannam@95 385 location (@code{/etc/fftw/wisdom} on Unix):
cannam@95 386 @cindex wisdom, system-wide
cannam@95 387
cannam@95 388 @example
cannam@95 389 int fftw_import_system_wisdom(void);
cannam@95 390 @end example
cannam@95 391 @findex fftw_import_system_wisdom
cannam@95 392
cannam@95 393 FFTW also provides a standalone program, @code{fftw-wisdom} (described
cannam@95 394 by its own @code{man} page on Unix) with which users can create wisdom,
cannam@95 395 e.g. for a canonical set of sizes to store in the system wisdom file.
cannam@95 396 @xref{Wisdom Utilities}.
cannam@95 397 @cindex fftw-wisdom utility
cannam@95 398