annotate src/fftw-3.3.8/doc/other.texi @ 83:ae30d91d2ffe

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