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 |
|
27 #include "oct-fftw.h" |
|
28 #include "lo-error.h" |
4773
|
29 #include <iostream> |
3828
|
30 |
|
31 // Helper class to create and cache fftw plans for both 1d and 2d. This |
|
32 // implementation uses FFTW_ESTIMATE to create the plans, which in theory |
4773
|
33 // is suboptimal, but provides quite reasonable performance. |
|
34 |
|
35 // Also note that if FFTW_ESTIMATE is not used the planner in FFTW3 |
|
36 // destroys the input and output arrays. So with the form of the |
|
37 // current code we definitely want FFTW_ESTIMATE!! However, we use |
|
38 // any wsidom that is available, either in a FFTW3 system wide file |
|
39 // or as supplied by the user. |
|
40 |
|
41 // XXX FIXME XXX If we can ensure 16 byte alignment in Array<T> (<T> *data) |
|
42 // the FFTW3 can use SIMD instructions for further acceleration. |
|
43 |
|
44 // Note that it is profitable to store the FFTW3 plans, for small ffts |
3828
|
45 |
|
46 class |
|
47 octave_fftw_planner |
|
48 { |
|
49 public: |
|
50 octave_fftw_planner (); |
|
51 |
4773
|
52 fftw_plan create_plan (int dir, const int rank, const dim_vector dims, |
|
53 int howmany, int stride, int dist, |
|
54 const Complex *in, Complex *out); |
|
55 fftw_plan create_plan (const int rank, const dim_vector dims, |
|
56 int howmany, int stride, int dist, |
|
57 const double *in, Complex *out); |
3828
|
58 |
|
59 private: |
|
60 int plan_flags; |
|
61 |
4773
|
62 // Plan for fft and ifft of complex values |
3828
|
63 fftw_plan plan[2]; |
4773
|
64 int d[2]; // dist |
|
65 int s[2]; // stride |
|
66 int r[2]; // rank |
|
67 int h[2]; // howmany |
|
68 dim_vector n[2]; // dims |
|
69 char ialign[2]; |
|
70 char oalign[2]; |
3828
|
71 |
4773
|
72 // Plan for fft of real values |
|
73 fftw_plan rplan; |
|
74 int rd; // dist |
|
75 int rs; // stride |
|
76 int rr; // rank |
|
77 int rh; // howmany |
|
78 dim_vector rn; // dims |
|
79 char rialign; |
|
80 char roalign; |
3828
|
81 }; |
|
82 |
|
83 octave_fftw_planner::octave_fftw_planner () |
|
84 { |
|
85 plan_flags = FFTW_ESTIMATE; |
|
86 |
|
87 plan[0] = plan[1] = 0; |
4773
|
88 d[0] = d[1] = s[0] = s[1] = r[0] = r[1] = h[0] = h[1] = 0; |
|
89 ialign[0] = ialign[1] = oalign[0] = oalign[1] = 0; |
|
90 n[0] = n[1] = dim_vector(); |
|
91 |
|
92 rplan = 0; |
|
93 rd = rs = rr = rh = 0; |
|
94 rialign = roalign = 0; |
|
95 rn = dim_vector (); |
|
96 |
|
97 // If we have a system wide wisdom file, import it |
|
98 fftw_import_system_wisdom ( ); |
3828
|
99 } |
|
100 |
|
101 fftw_plan |
4773
|
102 octave_fftw_planner::create_plan (int dir, const int rank, |
|
103 const dim_vector dims, int howmany, |
|
104 int stride, int dist, |
|
105 const Complex *in, Complex *out) |
3828
|
106 { |
4773
|
107 int which = (dir == FFTW_FORWARD) ? 0 : 1; |
3828
|
108 fftw_plan *cur_plan_p = &plan[which]; |
|
109 bool create_new_plan = false; |
4773
|
110 char in_align = ((int) in) & 0xF; |
|
111 char out_align = ((int) out) & 0xF; |
3828
|
112 |
4773
|
113 if (plan[which] == 0 || d[which] != dist || s[which] != stride || |
|
114 r[which] != rank || h[which] != howmany || |
|
115 ialign[which] != in_align || oalign[which] != out_align) |
|
116 create_new_plan = true; |
|
117 else |
|
118 // We still might not have the same shape of array |
|
119 for (int i = 0; i < rank; i++) |
|
120 if (dims(i) != n[which](i)) |
|
121 { |
|
122 create_new_plan = true; |
|
123 break; |
|
124 } |
3828
|
125 |
|
126 if (create_new_plan) |
|
127 { |
4773
|
128 d[which] = dist; |
|
129 s[which] = stride; |
|
130 r[which] = rank; |
|
131 h[which] = howmany; |
|
132 ialign[which] = in_align; |
|
133 oalign[which] = out_align; |
|
134 n[which] = dims; |
|
135 |
3828
|
136 if (*cur_plan_p) |
|
137 fftw_destroy_plan (*cur_plan_p); |
|
138 |
4773
|
139 // Note reversal of dimensions for column major storage in FFTW |
|
140 OCTAVE_LOCAL_BUFFER (int, tmp, rank); |
|
141 for (int i = 0, j = rank-1; i < rank; i++, j--) |
|
142 tmp[i] = dims(j); |
|
143 |
|
144 *cur_plan_p = |
|
145 fftw_plan_many_dft (rank, tmp, howmany, |
|
146 reinterpret_cast<fftw_complex *> (const_cast<Complex *> (in)), |
|
147 NULL, stride, dist, reinterpret_cast<fftw_complex *> (out), |
|
148 NULL, stride, dist, dir, plan_flags); |
3828
|
149 |
|
150 if (*cur_plan_p == 0) |
|
151 (*current_liboctave_error_handler) ("Error creating fftw plan"); |
|
152 } |
|
153 |
|
154 return *cur_plan_p; |
|
155 } |
|
156 |
4773
|
157 fftw_plan |
|
158 octave_fftw_planner::create_plan (const int rank, const dim_vector dims, |
|
159 int howmany, int stride, int dist, |
|
160 const double *in, Complex *out) |
3828
|
161 { |
4773
|
162 fftw_plan *cur_plan_p = &rplan; |
3828
|
163 bool create_new_plan = false; |
4773
|
164 char in_align = ((int) in) & 0xF; |
|
165 char out_align = ((int) out) & 0xF; |
3828
|
166 |
4773
|
167 if (rplan == 0 || rd != dist || rs != stride || |
|
168 rr != rank || rh != howmany || |
|
169 rialign != in_align || roalign != out_align) |
|
170 create_new_plan = true; |
|
171 else |
|
172 // We still might not have the same shape of array |
|
173 for (int i = 0; i < rank; i++) |
|
174 if (dims(i) != rn(i)) |
|
175 { |
|
176 create_new_plan = true; |
|
177 break; |
|
178 } |
3828
|
179 |
|
180 if (create_new_plan) |
|
181 { |
4773
|
182 rd = dist; |
|
183 rs = stride; |
|
184 rr = rank; |
|
185 rh = howmany; |
|
186 rialign = in_align; |
|
187 roalign = out_align; |
|
188 rn = dims; |
|
189 |
3828
|
190 if (*cur_plan_p) |
4773
|
191 fftw_destroy_plan (*cur_plan_p); |
3828
|
192 |
4773
|
193 // Note reversal of dimensions for column major storage in FFTW |
|
194 OCTAVE_LOCAL_BUFFER (int, tmp, rank); |
|
195 for (int i = 0, j = rank-1; i < rank; i++, j--) |
|
196 tmp[i] = dims(j); |
|
197 |
|
198 *cur_plan_p = |
|
199 fftw_plan_many_dft_r2c (rank, tmp, howmany, |
|
200 (const_cast<double *> (in)), |
|
201 NULL, stride, dist, reinterpret_cast<fftw_complex *> (out), |
|
202 NULL, stride, dist, plan_flags); |
3828
|
203 |
|
204 if (*cur_plan_p == 0) |
4773
|
205 (*current_liboctave_error_handler) ("Error creating fftw plan"); |
3828
|
206 } |
|
207 |
|
208 return *cur_plan_p; |
|
209 } |
|
210 |
|
211 static octave_fftw_planner fftw_planner; |
|
212 |
4773
|
213 static inline void convert_packcomplex_1d (Complex *out, size_t nr, |
|
214 size_t nc, int stride, int dist) |
|
215 { |
|
216 // Fill in the missing data |
|
217 for (size_t i = 0; i < nr; i++) |
|
218 for (size_t j = nc/2+1; j < nc; j++) |
|
219 out[j*stride + i*dist] = conj(out[(nc - j)*stride + i*dist]); |
|
220 } |
|
221 |
|
222 static inline void convert_packcomplex_Nd (Complex *out, |
|
223 const dim_vector &dv) |
3828
|
224 { |
4773
|
225 size_t nc = dv(0); |
|
226 size_t nr = dv(1); |
|
227 size_t np = (dv.length() > 2 ? dv.numel () / nc / nr : 1); |
|
228 size_t nrp = nr * np; |
|
229 Complex *ptr1, *ptr2; |
|
230 |
|
231 // Create space for the missing elements |
|
232 for (size_t i = 0; i < nrp; i++) |
|
233 { |
|
234 ptr1 = out + i * (nc/2 + 1) + nrp*((nc-1)/2); |
|
235 ptr2 = out + i * nc; |
|
236 for (size_t j = 0; j < nc/2+1; j++) |
|
237 *ptr2++ = *ptr1++; |
|
238 } |
|
239 |
|
240 // Fill in the missing data for the rank = 2 case directly for speed |
|
241 for (size_t i = 0; i < np; i++) |
|
242 { |
|
243 for (size_t j = 1; j < nr; j++) |
|
244 for (size_t k = nc/2+1; k < nc; k++) |
|
245 out[k + (j + i*nr)*nc] = conj(out[nc - k + ((i+1)*nr - j)*nc]); |
|
246 |
|
247 for (size_t j = nc/2+1; j < nc; j++) |
|
248 out[j + i*nr*nc] = conj(out[(i*nr+1)*nc - j]); |
|
249 } |
|
250 |
|
251 // Now do the permutations needed for rank > 2 cases |
|
252 size_t jstart = dv(0) * dv(1); |
|
253 size_t kstep = dv(0); |
|
254 size_t nel = dv.numel (); |
|
255 for (int inner = 2; inner < dv.length(); inner++) |
|
256 { |
|
257 size_t jmax = jstart * dv(inner); |
|
258 for (size_t i = 0; i < nel; i+=jmax) |
|
259 for (size_t j = jstart, jj = jmax-jstart; j < jj; |
|
260 j+=jstart, jj-=jstart) |
|
261 for (size_t k = 0; k < jstart; k+= kstep) |
|
262 for (size_t l = nc/2+1; l < nc; l++) |
|
263 { |
|
264 Complex tmp = out[i+ j + k + l]; |
|
265 out[i + j + k + l] = out[i + jj + k + l]; |
|
266 out[i + jj + k + l] = tmp; |
|
267 } |
|
268 jstart = jmax; |
|
269 } |
|
270 } |
|
271 |
|
272 int |
|
273 octave_fftw::fft (const double *in, Complex *out, size_t npts, |
|
274 size_t nsamples, int stride, int dist) |
|
275 { |
|
276 dist = (dist < 0 ? npts : dist); |
|
277 |
|
278 dim_vector dv (npts); |
|
279 fftw_plan plan = fftw_planner.create_plan (1, dv, nsamples, stride, dist, |
|
280 in, out); |
|
281 |
|
282 fftw_execute_dft_r2c (plan, (const_cast<double *>(in)), |
|
283 reinterpret_cast<fftw_complex *> (out)); |
|
284 |
|
285 // Need to create other half of the transform |
|
286 convert_packcomplex_1d (out, nsamples, npts, stride, dist); |
3828
|
287 |
|
288 return 0; |
|
289 } |
|
290 |
|
291 int |
4773
|
292 octave_fftw::fft (const Complex *in, Complex *out, size_t npts, |
|
293 size_t nsamples, int stride, int dist) |
3828
|
294 { |
4773
|
295 dist = (dist < 0 ? npts : dist); |
|
296 |
|
297 dim_vector dv (npts); |
|
298 fftw_plan plan = fftw_planner.create_plan (FFTW_FORWARD, 1, dv, nsamples, |
|
299 stride, dist, in, out); |
|
300 |
|
301 fftw_execute_dft (plan, |
|
302 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
303 reinterpret_cast<fftw_complex *> (out)); |
|
304 |
|
305 return 0; |
|
306 } |
|
307 |
|
308 int |
|
309 octave_fftw::ifft (const Complex *in, Complex *out, size_t npts, |
|
310 size_t nsamples, int stride, int dist) |
|
311 { |
|
312 dist = (dist < 0 ? npts : dist); |
|
313 |
|
314 dim_vector dv (npts); |
|
315 fftw_plan plan = fftw_planner.create_plan (FFTW_BACKWARD, 1, dv, nsamples, |
|
316 stride, dist, in, out); |
|
317 |
|
318 fftw_execute_dft (plan, |
|
319 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
320 reinterpret_cast<fftw_complex *> (out)); |
3828
|
321 |
|
322 const Complex scale = npts; |
4773
|
323 for (size_t j = 0; j < nsamples; j++) |
|
324 for (size_t i = 0; i < npts; i++) |
|
325 out[i*stride + j*dist] /= scale; |
3828
|
326 |
|
327 return 0; |
|
328 } |
|
329 |
|
330 int |
4773
|
331 octave_fftw::fftNd (const double *in, Complex *out, const int rank, |
|
332 const dim_vector &dv) |
3828
|
333 { |
4773
|
334 int dist = 1; |
|
335 for (int i = 0; i < rank; i++) |
|
336 dist *= dv(i); |
|
337 |
|
338 // Fool with the position of the start of the output matrix, so that |
|
339 // creating other half of the matrix won't cause cache problems |
|
340 int offset = (dv.numel () / dv(0)) * ((dv(0) - 1) / 2); |
|
341 |
|
342 fftw_plan plan = fftw_planner.create_plan (rank, dv, 1, 1, dist, |
|
343 in, out + offset); |
|
344 |
|
345 fftw_execute_dft_r2c (plan, (const_cast<double *>(in)), |
|
346 reinterpret_cast<fftw_complex *> (out+ offset)); |
|
347 |
|
348 // Need to create other half of the transform |
|
349 convert_packcomplex_Nd (out, dv); |
3828
|
350 |
|
351 return 0; |
|
352 } |
|
353 |
|
354 int |
4773
|
355 octave_fftw::fftNd (const Complex *in, Complex *out, const int rank, |
|
356 const dim_vector &dv) |
3828
|
357 { |
4773
|
358 int dist = 1; |
|
359 for (int i = 0; i < rank; i++) |
|
360 dist *= dv(i); |
|
361 |
|
362 fftw_plan plan = fftw_planner.create_plan (FFTW_FORWARD, rank, dv, 1, 1, |
|
363 dist, in, out); |
|
364 |
|
365 fftw_execute_dft (plan, |
|
366 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
367 reinterpret_cast<fftw_complex *> (out)); |
|
368 |
|
369 return 0; |
|
370 } |
3828
|
371 |
4773
|
372 int |
|
373 octave_fftw::ifftNd (const Complex *in, Complex *out, const int rank, |
|
374 const dim_vector &dv) |
|
375 { |
|
376 int dist = 1; |
|
377 for (int i = 0; i < rank; i++) |
|
378 dist *= dv(i); |
|
379 |
|
380 fftw_plan plan = fftw_planner.create_plan (FFTW_BACKWARD, rank, dv, 1, 1, |
|
381 dist, in, out); |
|
382 |
|
383 fftw_execute_dft (plan, |
|
384 reinterpret_cast<fftw_complex *> (const_cast<Complex *>(in)), |
|
385 reinterpret_cast<fftw_complex *> (out)); |
|
386 |
|
387 const size_t npts = dv.numel (); |
3828
|
388 const Complex scale = npts; |
|
389 for (size_t i = 0; i < npts; i++) |
4773
|
390 out[i] /= scale; |
3828
|
391 |
|
392 return 0; |
|
393 } |
|
394 |
|
395 #endif |
|
396 |
|
397 /* |
|
398 ;;; Local Variables: *** |
|
399 ;;; mode: C++ *** |
|
400 ;;; End: *** |
|
401 */ |
|
402 |