3828
|
1 /* |
|
2 |
|
3 This file is part of Octave. |
|
4 |
|
5 Octave is free software; you can redistribute it and/or modify it |
|
6 under the terms of the GNU General Public License as published by the |
|
7 Free Software Foundation; either version 2, or (at your option) any |
|
8 later version. |
|
9 |
|
10 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 for more details. |
|
14 |
|
15 You should have received a copy of the GNU General Public License |
|
16 along with Octave; see the file COPYING. If not, write to the Free |
|
17 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
18 |
|
19 */ |
|
20 |
|
21 #ifdef HAVE_CONFIG_H |
|
22 #include <config.h> |
|
23 #endif |
|
24 |
4773
|
25 #if defined (HAVE_FFTW3) |
3828
|
26 |
4775
|
27 #include <iostream> |
|
28 #include <vector> |
|
29 |
4786
|
30 #include "lo-error.h" |
3828
|
31 #include "oct-fftw.h" |
4786
|
32 #include "quit.h" |
3828
|
33 |
4809
|
34 // Helper class to create and cache fftw plans for both 1d and |
|
35 // 2d. This implementation uses FFTW_ESTIMATE to create the plans, |
|
36 // which in theory is suboptimal, but provides quite reasonable |
|
37 // performance. |
4773
|
38 |
|
39 // Also note that if FFTW_ESTIMATE is not used the planner in FFTW3 |
4809
|
40 // destroys the input and output arrays. So with the form of the |
|
41 // current code we definitely want FFTW_ESTIMATE!! However, we use any |
|
42 // wsidom that is available, either in a FFTW3 system wide file or as |
|
43 // supplied by the user. |
4773
|
44 |
4809
|
45 // XXX FIXME XXX -- if we can ensure 16 byte alignment in Array<T> |
|
46 // (<T> *data) the FFTW3 can use SIMD instructions for further |
|
47 // acceleration. |
4773
|
48 |
4809
|
49 // Note that it is profitable to store the FFTW3 plans, for small |
|
50 // ffts. |
3828
|
51 |
|
52 class |
|
53 octave_fftw_planner |
|
54 { |
|
55 public: |
4808
|
56 |
|
57 octave_fftw_planner (void); |
3828
|
58 |
4773
|
59 fftw_plan create_plan (int dir, const int rank, const dim_vector dims, |
|
60 int howmany, int stride, int dist, |
|
61 const Complex *in, Complex *out); |
4808
|
62 |
4773
|
63 fftw_plan create_plan (const int rank, const dim_vector dims, |
|
64 int howmany, int stride, int dist, |
|
65 const double *in, Complex *out); |
3828
|
66 |
|
67 private: |
4808
|
68 |
3828
|
69 int plan_flags; |
|
70 |
4809
|
71 // XXX FIXME XXX -- perhaps this should be split into two classes? |
|
72 |
4773
|
73 // Plan for fft and ifft of complex values |
3828
|
74 fftw_plan plan[2]; |
4809
|
75 |
|
76 // dist |
|
77 int d[2]; |
|
78 |
|
79 // stride |
|
80 int s[2]; |
|
81 |
|
82 // rank |
|
83 int r[2]; |
|
84 |
|
85 // howmany |
|
86 int h[2]; |
|
87 |
|
88 // dims |
|
89 dim_vector n[2]; |
|
90 |
4808
|
91 bool simd_align[2]; |
5044
|
92 bool inplace[2]; |
4809
|
93 |
4773
|
94 // Plan for fft of real values |
|
95 fftw_plan rplan; |
4809
|
96 |
|
97 // dist |
|
98 int rd; |
|
99 |
|
100 // stride |
|
101 int rs; |
|
102 |
|
103 // rank |
|
104 int rr; |
|
105 |
|
106 // howmany |
|
107 int rh; |
|
108 |
|
109 // dims |
|
110 dim_vector rn; |
|
111 |
4808
|
112 bool rsimd_align; |
3828
|
113 }; |
|
114 |
4808
|
115 octave_fftw_planner::octave_fftw_planner (void) |
3828
|
116 { |
|
117 plan_flags = FFTW_ESTIMATE; |
|
118 |
|
119 plan[0] = plan[1] = 0; |
4773
|
120 d[0] = d[1] = s[0] = s[1] = r[0] = r[1] = h[0] = h[1] = 0; |
4808
|
121 simd_align[0] = simd_align[1] = false; |
5044
|
122 inplace[0] = inplace[1] = false; |
4808
|
123 n[0] = n[1] = dim_vector (); |
4773
|
124 |
|
125 rplan = 0; |
|
126 rd = rs = rr = rh = 0; |
4808
|
127 rsimd_align = false; |
4773
|
128 rn = dim_vector (); |
5044
|
129 |
4809
|
130 // If we have a system wide wisdom file, import it. |
4808
|
131 fftw_import_system_wisdom (); |
3828
|
132 } |
|
133 |
4808
|
134 #define CHECK_SIMD_ALIGNMENT(x) \ |
|
135 ((reinterpret_cast<ptrdiff_t> (x)) & 0xF == 0) |
|
136 |
3828
|
137 fftw_plan |
4773
|
138 octave_fftw_planner::create_plan (int dir, const int rank, |
|
139 const dim_vector dims, int howmany, |
|
140 int stride, int dist, |
|
141 const Complex *in, Complex *out) |
3828
|
142 { |
4773
|
143 int which = (dir == FFTW_FORWARD) ? 0 : 1; |
3828
|
144 fftw_plan *cur_plan_p = &plan[which]; |
|
145 bool create_new_plan = false; |
4808
|
146 bool ioalign = CHECK_SIMD_ALIGNMENT (in) && CHECK_SIMD_ALIGNMENT (out); |
5044
|
147 bool ioinplace = (in == out); |
3828
|
148 |
4809
|
149 // Don't create a new plan if we have a non SIMD plan already but |
|
150 // can do SIMD. This prevents endlessly recreating plans if we |
|
151 // change the alignment. |
|
152 |
4783
|
153 if (plan[which] == 0 || d[which] != dist || s[which] != stride |
5044
|
154 || r[which] != rank || h[which] != howmany |
|
155 || ioinplace != inplace[which] |
4808
|
156 || ((ioalign != simd_align[which]) ? !ioalign : false)) |
4773
|
157 create_new_plan = true; |
|
158 else |
4809
|
159 { |
|
160 // We still might not have the same shape of array. |
|
161 |
|
162 for (int i = 0; i < rank; i++) |
|
163 if (dims(i) != n[which](i)) |
|
164 { |
|
165 create_new_plan = true; |
|
166 break; |
|
167 } |
|
168 } |
3828
|
169 |
|
170 if (create_new_plan) |
|
171 { |
4773
|
172 d[which] = dist; |
|
173 s[which] = stride; |
|
174 r[which] = rank; |
|
175 h[which] = howmany; |
4808
|
176 simd_align[which] = ioalign; |
5044
|
177 inplace[which] = ioinplace; |
4773
|
178 n[which] = dims; |
|
179 |
4808
|
180 if (ioalign) |
|
181 plan_flags &= ~FFTW_UNALIGNED; |
|
182 else |
|
183 plan_flags |= FFTW_UNALIGNED; |
|
184 |
3828
|
185 if (*cur_plan_p) |
|
186 fftw_destroy_plan (*cur_plan_p); |
|
187 |
4809
|
188 // Note reversal of dimensions for column major storage in FFTW. |
|
189 |
4773
|
190 OCTAVE_LOCAL_BUFFER (int, tmp, rank); |
4809
|
191 |
4773
|
192 for (int i = 0, j = rank-1; i < rank; i++, j--) |
|
193 tmp[i] = dims(j); |
|
194 |
|
195 *cur_plan_p = |
|
196 fftw_plan_many_dft (rank, tmp, howmany, |
|
197 reinterpret_cast<fftw_complex *> (const_cast<Complex *> (in)), |
4774
|
198 0, stride, dist, reinterpret_cast<fftw_complex *> (out), |
|
199 0, stride, dist, dir, plan_flags); |
3828
|
200 |
|
201 if (*cur_plan_p == 0) |
|
202 (*current_liboctave_error_handler) ("Error creating fftw plan"); |
|
203 } |
|
204 |
|
205 return *cur_plan_p; |
|
206 } |
|
207 |
4773
|
208 fftw_plan |
|
209 octave_fftw_planner::create_plan (const int rank, const dim_vector dims, |
|
210 int howmany, int stride, int dist, |
|
211 const double *in, Complex *out) |
3828
|
212 { |
4773
|
213 fftw_plan *cur_plan_p = &rplan; |
3828
|
214 bool create_new_plan = false; |
4808
|
215 bool ioalign = CHECK_SIMD_ALIGNMENT (in) && CHECK_SIMD_ALIGNMENT (out); |
3828
|
216 |
4809
|
217 // Don't create a new plan if we have a non SIMD plan already but |
|
218 // can do SIMD. This prevents endlessly recreating plans if we |
|
219 // change the alignment. |
|
220 |
4783
|
221 if (rplan == 0 || rd != dist || rs != stride || rr != rank |
4808
|
222 || rh != howmany || ((ioalign != rsimd_align) ? !ioalign : false)) |
4773
|
223 create_new_plan = true; |
|
224 else |
4809
|
225 { |
|
226 // We still might not have the same shape of array. |
|
227 |
|
228 for (int i = 0; i < rank; i++) |
|
229 if (dims(i) != rn(i)) |
|
230 { |
|
231 create_new_plan = true; |
|
232 break; |
|
233 } |
|
234 } |
3828
|
235 |
|
236 if (create_new_plan) |
|
237 { |
4773
|
238 rd = dist; |
|
239 rs = stride; |
|
240 rr = rank; |
|
241 rh = howmany; |
4808
|
242 rsimd_align = ioalign; |
4773
|
243 rn = dims; |
|
244 |
4808
|
245 if (ioalign) |
|
246 plan_flags &= ~FFTW_UNALIGNED; |
|
247 else |
|
248 plan_flags |= FFTW_UNALIGNED; |
|
249 |
3828
|
250 if (*cur_plan_p) |
4773
|
251 fftw_destroy_plan (*cur_plan_p); |
3828
|
252 |
4809
|
253 // Note reversal of dimensions for column major storage in FFTW. |
|
254 |
4773
|
255 OCTAVE_LOCAL_BUFFER (int, tmp, rank); |
4809
|
256 |
4773
|
257 for (int i = 0, j = rank-1; i < rank; i++, j--) |
|
258 tmp[i] = dims(j); |
|
259 |
|
260 *cur_plan_p = |
|
261 fftw_plan_many_dft_r2c (rank, tmp, howmany, |
|
262 (const_cast<double *> (in)), |
4774
|
263 0, stride, dist, reinterpret_cast<fftw_complex *> (out), |
|
264 0, stride, dist, plan_flags); |
3828
|
265 |
|
266 if (*cur_plan_p == 0) |
4773
|
267 (*current_liboctave_error_handler) ("Error creating fftw plan"); |
3828
|
268 } |
|
269 |
|
270 return *cur_plan_p; |
|
271 } |
|
272 |
|
273 static octave_fftw_planner fftw_planner; |
|
274 |
4775
|
275 static inline void |
|
276 convert_packcomplex_1d (Complex *out, size_t nr, size_t nc, |
|
277 int stride, int dist) |
4773
|
278 { |
4785
|
279 OCTAVE_QUIT; |
|
280 |
|
281 // Fill in the missing data. |
|
282 |
4773
|
283 for (size_t i = 0; i < nr; i++) |
|
284 for (size_t j = nc/2+1; j < nc; j++) |
|
285 out[j*stride + i*dist] = conj(out[(nc - j)*stride + i*dist]); |
4785
|
286 |
|
287 OCTAVE_QUIT; |
4773
|
288 } |
|
289 |
4775
|
290 static inline void |
|
291 convert_packcomplex_Nd (Complex *out, const dim_vector &dv) |
3828
|
292 { |
4773
|
293 size_t nc = dv(0); |
|
294 size_t nr = dv(1); |
4808
|
295 size_t np = (dv.length () > 2 ? dv.numel () / nc / nr : 1); |
4773
|
296 size_t nrp = nr * np; |
|
297 Complex *ptr1, *ptr2; |
|
298 |
4785
|
299 OCTAVE_QUIT; |
|
300 |
|
301 // Create space for the missing elements. |
|
302 |
4773
|
303 for (size_t i = 0; i < nrp; i++) |
|
304 { |
|
305 ptr1 = out + i * (nc/2 + 1) + nrp*((nc-1)/2); |
|
306 ptr2 = out + i * nc; |
|
307 for (size_t j = 0; j < nc/2+1; j++) |
|
308 *ptr2++ = *ptr1++; |
|
309 } |
|
310 |
4785
|
311 OCTAVE_QUIT; |
|
312 |
|
313 // Fill in the missing data for the rank = 2 case directly for speed. |
|
314 |
4773
|
315 for (size_t i = 0; i < np; i++) |
|
316 { |
|
317 for (size_t j = 1; j < nr; j++) |
|
318 for (size_t k = nc/2+1; k < nc; k++) |
|
319 out[k + (j + i*nr)*nc] = conj(out[nc - k + ((i+1)*nr - j)*nc]); |
|
320 |
|
321 for (size_t j = nc/2+1; j < nc; j++) |
|
322 out[j + i*nr*nc] = conj(out[(i*nr+1)*nc - j]); |
|
323 } |
|
324 |
4785
|
325 OCTAVE_QUIT; |
|
326 |
|
327 // Now do the permutations needed for rank > 2 cases. |
|
328 |
4773
|
329 size_t jstart = dv(0) * dv(1); |
|
330 size_t kstep = dv(0); |
|
331 size_t nel = dv.numel (); |
4785
|
332 |
4808
|
333 for (int inner = 2; inner < dv.length (); inner++) |
4773
|
334 { |
|
335 size_t jmax = jstart * dv(inner); |
|
336 for (size_t i = 0; i < nel; i+=jmax) |
|
337 for (size_t j = jstart, jj = jmax-jstart; j < jj; |
|
338 j+=jstart, jj-=jstart) |
|
339 for (size_t k = 0; k < jstart; k+= kstep) |
|
340 for (size_t l = nc/2+1; l < nc; l++) |
|
341 { |
|
342 Complex tmp = out[i+ j + k + l]; |
|
343 out[i + j + k + l] = out[i + jj + k + l]; |
|
344 out[i + jj + k + l] = tmp; |
|
345 } |
|
346 jstart = jmax; |
|
347 } |
4785
|
348 |
|
349 OCTAVE_QUIT; |
4773
|
350 } |
|
351 |
|
352 int |
|
353 octave_fftw::fft (const double *in, Complex *out, size_t npts, |
|
354 size_t nsamples, int stride, int dist) |
|
355 { |
|
356 dist = (dist < 0 ? npts : dist); |
|
357 |
|
358 dim_vector dv (npts); |
|
359 fftw_plan plan = fftw_planner.create_plan (1, dv, nsamples, stride, dist, |
|
360 in, out); |
|
361 |
|
362 fftw_execute_dft_r2c (plan, (const_cast<double *>(in)), |
|
363 reinterpret_cast<fftw_complex *> (out)); |
|
364 |
4809
|
365 // Need to create other half of the transform. |
|
366 |
4773
|
367 convert_packcomplex_1d (out, nsamples, npts, stride, dist); |
3828
|
368 |
|
369 return 0; |
|
370 } |
|
371 |
|
372 int |
4773
|
373 octave_fftw::fft (const Complex *in, Complex *out, size_t npts, |
|
374 size_t nsamples, int stride, int dist) |
3828
|
375 { |
4773
|
376 dist = (dist < 0 ? npts : dist); |
|
377 |
|
378 dim_vector dv (npts); |
|
379 fftw_plan plan = fftw_planner.create_plan (FFTW_FORWARD, 1, dv, nsamples, |
|
380 stride, dist, in, out); |
|
381 |
|
382 fftw_execute_dft (plan, |
|
383 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
384 reinterpret_cast<fftw_complex *> (out)); |
|
385 |
|
386 return 0; |
|
387 } |
|
388 |
|
389 int |
|
390 octave_fftw::ifft (const Complex *in, Complex *out, size_t npts, |
|
391 size_t nsamples, int stride, int dist) |
|
392 { |
|
393 dist = (dist < 0 ? npts : dist); |
|
394 |
|
395 dim_vector dv (npts); |
|
396 fftw_plan plan = fftw_planner.create_plan (FFTW_BACKWARD, 1, dv, nsamples, |
|
397 stride, dist, in, out); |
|
398 |
|
399 fftw_execute_dft (plan, |
|
400 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
401 reinterpret_cast<fftw_complex *> (out)); |
3828
|
402 |
|
403 const Complex scale = npts; |
4773
|
404 for (size_t j = 0; j < nsamples; j++) |
|
405 for (size_t i = 0; i < npts; i++) |
|
406 out[i*stride + j*dist] /= scale; |
3828
|
407 |
|
408 return 0; |
|
409 } |
|
410 |
|
411 int |
4773
|
412 octave_fftw::fftNd (const double *in, Complex *out, const int rank, |
|
413 const dim_vector &dv) |
3828
|
414 { |
4773
|
415 int dist = 1; |
|
416 for (int i = 0; i < rank; i++) |
|
417 dist *= dv(i); |
|
418 |
|
419 // Fool with the position of the start of the output matrix, so that |
4809
|
420 // creating other half of the matrix won't cause cache problems. |
|
421 |
4773
|
422 int offset = (dv.numel () / dv(0)) * ((dv(0) - 1) / 2); |
|
423 |
|
424 fftw_plan plan = fftw_planner.create_plan (rank, dv, 1, 1, dist, |
|
425 in, out + offset); |
|
426 |
|
427 fftw_execute_dft_r2c (plan, (const_cast<double *>(in)), |
|
428 reinterpret_cast<fftw_complex *> (out+ offset)); |
|
429 |
4809
|
430 // Need to create other half of the transform. |
|
431 |
4773
|
432 convert_packcomplex_Nd (out, dv); |
3828
|
433 |
|
434 return 0; |
|
435 } |
|
436 |
|
437 int |
4773
|
438 octave_fftw::fftNd (const Complex *in, Complex *out, const int rank, |
|
439 const dim_vector &dv) |
3828
|
440 { |
4773
|
441 int dist = 1; |
|
442 for (int i = 0; i < rank; i++) |
|
443 dist *= dv(i); |
|
444 |
|
445 fftw_plan plan = fftw_planner.create_plan (FFTW_FORWARD, rank, dv, 1, 1, |
|
446 dist, in, out); |
|
447 |
|
448 fftw_execute_dft (plan, |
|
449 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
450 reinterpret_cast<fftw_complex *> (out)); |
|
451 |
|
452 return 0; |
|
453 } |
3828
|
454 |
4773
|
455 int |
|
456 octave_fftw::ifftNd (const Complex *in, Complex *out, const int rank, |
4784
|
457 const dim_vector &dv) |
4773
|
458 { |
|
459 int dist = 1; |
|
460 for (int i = 0; i < rank; i++) |
|
461 dist *= dv(i); |
|
462 |
|
463 fftw_plan plan = fftw_planner.create_plan (FFTW_BACKWARD, rank, dv, 1, 1, |
|
464 dist, in, out); |
|
465 |
|
466 fftw_execute_dft (plan, |
|
467 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
468 reinterpret_cast<fftw_complex *> (out)); |
|
469 |
|
470 const size_t npts = dv.numel (); |
3828
|
471 const Complex scale = npts; |
|
472 for (size_t i = 0; i < npts; i++) |
4773
|
473 out[i] /= scale; |
3828
|
474 |
|
475 return 0; |
|
476 } |
|
477 |
|
478 #endif |
|
479 |
|
480 /* |
|
481 ;;; Local Variables: *** |
|
482 ;;; mode: C++ *** |
|
483 ;;; End: *** |
|
484 */ |
|
485 |