Chris@10
|
1 /*
|
Chris@10
|
2 * Copyright (c) 2003, 2007-11 Matteo Frigo
|
Chris@10
|
3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
|
Chris@10
|
4 *
|
Chris@10
|
5 * This program is free software; you can redistribute it and/or modify
|
Chris@10
|
6 * it under the terms of the GNU General Public License as published by
|
Chris@10
|
7 * the Free Software Foundation; either version 2 of the License, or
|
Chris@10
|
8 * (at your option) any later version.
|
Chris@10
|
9 *
|
Chris@10
|
10 * This program is distributed in the hope that it will be useful,
|
Chris@10
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@10
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@10
|
13 * GNU General Public License for more details.
|
Chris@10
|
14 *
|
Chris@10
|
15 * You should have received a copy of the GNU General Public License
|
Chris@10
|
16 * along with this program; if not, write to the Free Software
|
Chris@10
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Chris@10
|
18 *
|
Chris@10
|
19 */
|
Chris@10
|
20
|
Chris@10
|
21
|
Chris@10
|
22 /* buffering of rdft2. We always buffer the complex array */
|
Chris@10
|
23
|
Chris@10
|
24 #include "rdft.h"
|
Chris@10
|
25 #include "dft.h"
|
Chris@10
|
26
|
Chris@10
|
27 typedef struct {
|
Chris@10
|
28 solver super;
|
Chris@10
|
29 int maxnbuf_ndx;
|
Chris@10
|
30 } S;
|
Chris@10
|
31
|
Chris@10
|
32 static const INT maxnbufs[] = { 8, 256 };
|
Chris@10
|
33
|
Chris@10
|
34 typedef struct {
|
Chris@10
|
35 plan_rdft2 super;
|
Chris@10
|
36
|
Chris@10
|
37 plan *cld, *cldcpy, *cldrest;
|
Chris@10
|
38 INT n, vl, nbuf, bufdist;
|
Chris@10
|
39 INT ivs_by_nbuf, ovs_by_nbuf;
|
Chris@10
|
40 INT ioffset, roffset;
|
Chris@10
|
41 } P;
|
Chris@10
|
42
|
Chris@10
|
43 /* transform a vector input with the help of bufs */
|
Chris@10
|
44 static void apply_r2hc(const plan *ego_, R *r0, R *r1, R *cr, R *ci)
|
Chris@10
|
45 {
|
Chris@10
|
46 const P *ego = (const P *) ego_;
|
Chris@10
|
47 plan_rdft2 *cld = (plan_rdft2 *) ego->cld;
|
Chris@10
|
48 plan_dft *cldcpy = (plan_dft *) ego->cldcpy;
|
Chris@10
|
49 INT i, vl = ego->vl, nbuf = ego->nbuf;
|
Chris@10
|
50 INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf;
|
Chris@10
|
51 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS);
|
Chris@10
|
52 R *bufr = bufs + ego->roffset;
|
Chris@10
|
53 R *bufi = bufs + ego->ioffset;
|
Chris@10
|
54 plan_rdft2 *cldrest;
|
Chris@10
|
55
|
Chris@10
|
56 for (i = nbuf; i <= vl; i += nbuf) {
|
Chris@10
|
57 /* transform to bufs: */
|
Chris@10
|
58 cld->apply((plan *) cld, r0, r1, bufr, bufi);
|
Chris@10
|
59 r0 += ivs_by_nbuf; r1 += ivs_by_nbuf;
|
Chris@10
|
60
|
Chris@10
|
61 /* copy back */
|
Chris@10
|
62 cldcpy->apply((plan *) cldcpy, bufr, bufi, cr, ci);
|
Chris@10
|
63 cr += ovs_by_nbuf; ci += ovs_by_nbuf;
|
Chris@10
|
64 }
|
Chris@10
|
65
|
Chris@10
|
66 X(ifree)(bufs);
|
Chris@10
|
67
|
Chris@10
|
68 /* Do the remaining transforms, if any: */
|
Chris@10
|
69 cldrest = (plan_rdft2 *) ego->cldrest;
|
Chris@10
|
70 cldrest->apply((plan *) cldrest, r0, r1, cr, ci);
|
Chris@10
|
71 }
|
Chris@10
|
72
|
Chris@10
|
73 /* for hc2r problems, copy the input into buffer, and then
|
Chris@10
|
74 transform buffer->output, which allows for destruction of the
|
Chris@10
|
75 buffer */
|
Chris@10
|
76 static void apply_hc2r(const plan *ego_, R *r0, R *r1, R *cr, R *ci)
|
Chris@10
|
77 {
|
Chris@10
|
78 const P *ego = (const P *) ego_;
|
Chris@10
|
79 plan_rdft2 *cld = (plan_rdft2 *) ego->cld;
|
Chris@10
|
80 plan_dft *cldcpy = (plan_dft *) ego->cldcpy;
|
Chris@10
|
81 INT i, vl = ego->vl, nbuf = ego->nbuf;
|
Chris@10
|
82 INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf;
|
Chris@10
|
83 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS);
|
Chris@10
|
84 R *bufr = bufs + ego->roffset;
|
Chris@10
|
85 R *bufi = bufs + ego->ioffset;
|
Chris@10
|
86 plan_rdft2 *cldrest;
|
Chris@10
|
87
|
Chris@10
|
88 for (i = nbuf; i <= vl; i += nbuf) {
|
Chris@10
|
89 /* copy input into bufs: */
|
Chris@10
|
90 cldcpy->apply((plan *) cldcpy, cr, ci, bufr, bufi);
|
Chris@10
|
91 cr += ivs_by_nbuf; ci += ivs_by_nbuf;
|
Chris@10
|
92
|
Chris@10
|
93 /* transform to output */
|
Chris@10
|
94 cld->apply((plan *) cld, r0, r1, bufr, bufi);
|
Chris@10
|
95 r0 += ovs_by_nbuf; r1 += ovs_by_nbuf;
|
Chris@10
|
96 }
|
Chris@10
|
97
|
Chris@10
|
98 X(ifree)(bufs);
|
Chris@10
|
99
|
Chris@10
|
100 /* Do the remaining transforms, if any: */
|
Chris@10
|
101 cldrest = (plan_rdft2 *) ego->cldrest;
|
Chris@10
|
102 cldrest->apply((plan *) cldrest, r0, r1, cr, ci);
|
Chris@10
|
103 }
|
Chris@10
|
104
|
Chris@10
|
105
|
Chris@10
|
106 static void awake(plan *ego_, enum wakefulness wakefulness)
|
Chris@10
|
107 {
|
Chris@10
|
108 P *ego = (P *) ego_;
|
Chris@10
|
109
|
Chris@10
|
110 X(plan_awake)(ego->cld, wakefulness);
|
Chris@10
|
111 X(plan_awake)(ego->cldcpy, wakefulness);
|
Chris@10
|
112 X(plan_awake)(ego->cldrest, wakefulness);
|
Chris@10
|
113 }
|
Chris@10
|
114
|
Chris@10
|
115 static void destroy(plan *ego_)
|
Chris@10
|
116 {
|
Chris@10
|
117 P *ego = (P *) ego_;
|
Chris@10
|
118 X(plan_destroy_internal)(ego->cldrest);
|
Chris@10
|
119 X(plan_destroy_internal)(ego->cldcpy);
|
Chris@10
|
120 X(plan_destroy_internal)(ego->cld);
|
Chris@10
|
121 }
|
Chris@10
|
122
|
Chris@10
|
123 static void print(const plan *ego_, printer *p)
|
Chris@10
|
124 {
|
Chris@10
|
125 const P *ego = (const P *) ego_;
|
Chris@10
|
126 p->print(p, "(rdft2-buffered-%D%v/%D-%D%(%p%)%(%p%)%(%p%))",
|
Chris@10
|
127 ego->n, ego->nbuf,
|
Chris@10
|
128 ego->vl, ego->bufdist % ego->n,
|
Chris@10
|
129 ego->cld, ego->cldcpy, ego->cldrest);
|
Chris@10
|
130 }
|
Chris@10
|
131
|
Chris@10
|
132 static int applicable0(const S *ego, const problem *p_, const planner *plnr)
|
Chris@10
|
133 {
|
Chris@10
|
134 const problem_rdft2 *p = (const problem_rdft2 *) p_;
|
Chris@10
|
135 iodim *d = p->sz->dims;
|
Chris@10
|
136
|
Chris@10
|
137 if (1
|
Chris@10
|
138 && p->vecsz->rnk <= 1
|
Chris@10
|
139 && p->sz->rnk == 1
|
Chris@10
|
140
|
Chris@10
|
141 /* we assume even n throughout */
|
Chris@10
|
142 && (d[0].n % 2) == 0
|
Chris@10
|
143
|
Chris@10
|
144 /* and we only consider these two cases */
|
Chris@10
|
145 && (p->kind == R2HC || p->kind == HC2R)
|
Chris@10
|
146
|
Chris@10
|
147 ) {
|
Chris@10
|
148 INT vl, ivs, ovs;
|
Chris@10
|
149 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs);
|
Chris@10
|
150
|
Chris@10
|
151 if (X(toobig)(d[0].n) && CONSERVE_MEMORYP(plnr))
|
Chris@10
|
152 return 0;
|
Chris@10
|
153
|
Chris@10
|
154 /* if this solver is redundant, in the sense that a solver
|
Chris@10
|
155 of lower index generates the same plan, then prune this
|
Chris@10
|
156 solver */
|
Chris@10
|
157 if (X(nbuf_redundant)(d[0].n, vl,
|
Chris@10
|
158 ego->maxnbuf_ndx,
|
Chris@10
|
159 maxnbufs, NELEM(maxnbufs)))
|
Chris@10
|
160 return 0;
|
Chris@10
|
161
|
Chris@10
|
162 if (p->r0 != p->cr) {
|
Chris@10
|
163 if (p->kind == HC2R) {
|
Chris@10
|
164 /* Allow HC2R problems only if the input is to be
|
Chris@10
|
165 preserved. This solver sets NO_DESTROY_INPUT,
|
Chris@10
|
166 which prevents infinite loops */
|
Chris@10
|
167 return (NO_DESTROY_INPUTP(plnr));
|
Chris@10
|
168 } else {
|
Chris@10
|
169 /*
|
Chris@10
|
170 In principle, the buffered transforms might be useful
|
Chris@10
|
171 when working out of place. However, in order to
|
Chris@10
|
172 prevent infinite loops in the planner, we require
|
Chris@10
|
173 that the output stride of the buffered transforms be
|
Chris@10
|
174 greater than 2.
|
Chris@10
|
175 */
|
Chris@10
|
176 return (d[0].os > 2);
|
Chris@10
|
177 }
|
Chris@10
|
178 }
|
Chris@10
|
179
|
Chris@10
|
180 /*
|
Chris@10
|
181 * If the problem is in place, the input/output strides must
|
Chris@10
|
182 * be the same or the whole thing must fit in the buffer.
|
Chris@10
|
183 */
|
Chris@10
|
184 if (X(rdft2_inplace_strides(p, RNK_MINFTY)))
|
Chris@10
|
185 return 1;
|
Chris@10
|
186
|
Chris@10
|
187 if (/* fits into buffer: */
|
Chris@10
|
188 ((p->vecsz->rnk == 0)
|
Chris@10
|
189 ||
|
Chris@10
|
190 (X(nbuf)(d[0].n, p->vecsz->dims[0].n,
|
Chris@10
|
191 maxnbufs[ego->maxnbuf_ndx])
|
Chris@10
|
192 == p->vecsz->dims[0].n)))
|
Chris@10
|
193 return 1;
|
Chris@10
|
194 }
|
Chris@10
|
195
|
Chris@10
|
196 return 0;
|
Chris@10
|
197 }
|
Chris@10
|
198
|
Chris@10
|
199 static int applicable(const S *ego, const problem *p_, const planner *plnr)
|
Chris@10
|
200 {
|
Chris@10
|
201 const problem_rdft2 *p;
|
Chris@10
|
202
|
Chris@10
|
203 if (NO_BUFFERINGP(plnr)) return 0;
|
Chris@10
|
204
|
Chris@10
|
205 if (!applicable0(ego, p_, plnr)) return 0;
|
Chris@10
|
206
|
Chris@10
|
207 p = (const problem_rdft2 *) p_;
|
Chris@10
|
208 if (p->kind == HC2R) {
|
Chris@10
|
209 if (NO_UGLYP(plnr)) {
|
Chris@10
|
210 /* UGLY if in-place and too big, since the problem
|
Chris@10
|
211 could be solved via transpositions */
|
Chris@10
|
212 if (p->r0 == p->cr && X(toobig)(p->sz->dims[0].n))
|
Chris@10
|
213 return 0;
|
Chris@10
|
214 }
|
Chris@10
|
215 } else {
|
Chris@10
|
216 if (NO_UGLYP(plnr)) {
|
Chris@10
|
217 if (p->r0 != p->cr || X(toobig)(p->sz->dims[0].n))
|
Chris@10
|
218 return 0;
|
Chris@10
|
219 }
|
Chris@10
|
220 }
|
Chris@10
|
221 return 1;
|
Chris@10
|
222 }
|
Chris@10
|
223
|
Chris@10
|
224 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
|
Chris@10
|
225 {
|
Chris@10
|
226 P *pln;
|
Chris@10
|
227 const S *ego = (const S *)ego_;
|
Chris@10
|
228 plan *cld = (plan *) 0;
|
Chris@10
|
229 plan *cldcpy = (plan *) 0;
|
Chris@10
|
230 plan *cldrest = (plan *) 0;
|
Chris@10
|
231 const problem_rdft2 *p = (const problem_rdft2 *) p_;
|
Chris@10
|
232 R *bufs = (R *) 0;
|
Chris@10
|
233 INT nbuf = 0, bufdist, n, vl;
|
Chris@10
|
234 INT ivs, ovs, ioffset, roffset, id, od;
|
Chris@10
|
235
|
Chris@10
|
236 static const plan_adt padt = {
|
Chris@10
|
237 X(rdft2_solve), awake, print, destroy
|
Chris@10
|
238 };
|
Chris@10
|
239
|
Chris@10
|
240 if (!applicable(ego, p_, plnr))
|
Chris@10
|
241 goto nada;
|
Chris@10
|
242
|
Chris@10
|
243 n = X(tensor_sz)(p->sz);
|
Chris@10
|
244 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs);
|
Chris@10
|
245
|
Chris@10
|
246 nbuf = X(nbuf)(n, vl, maxnbufs[ego->maxnbuf_ndx]);
|
Chris@10
|
247 bufdist = X(bufdist)(n + 2, vl); /* complex-side rdft2 stores N+2
|
Chris@10
|
248 real numbers */
|
Chris@10
|
249 A(nbuf > 0);
|
Chris@10
|
250
|
Chris@10
|
251 /* attempt to keep real and imaginary part in the same order,
|
Chris@10
|
252 so as to allow optimizations in the the copy plan */
|
Chris@10
|
253 roffset = (p->cr - p->ci > 0) ? (INT)1 : (INT)0;
|
Chris@10
|
254 ioffset = 1 - roffset;
|
Chris@10
|
255
|
Chris@10
|
256 /* initial allocation for the purpose of planning */
|
Chris@10
|
257 bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS);
|
Chris@10
|
258
|
Chris@10
|
259 id = ivs * (nbuf * (vl / nbuf));
|
Chris@10
|
260 od = ovs * (nbuf * (vl / nbuf));
|
Chris@10
|
261
|
Chris@10
|
262 if (p->kind == R2HC) {
|
Chris@10
|
263 /* allow destruction of input if problem is in place */
|
Chris@10
|
264 cld = X(mkplan_f_d)(
|
Chris@10
|
265 plnr,
|
Chris@10
|
266 X(mkproblem_rdft2_d)(
|
Chris@10
|
267 X(mktensor_1d)(n, p->sz->dims[0].is, 2),
|
Chris@10
|
268 X(mktensor_1d)(nbuf, ivs, bufdist),
|
Chris@10
|
269 TAINT(p->r0, ivs * nbuf), TAINT(p->r1, ivs * nbuf),
|
Chris@10
|
270 bufs + roffset, bufs + ioffset, p->kind),
|
Chris@10
|
271 0, 0, (p->r0 == p->cr) ? NO_DESTROY_INPUT : 0);
|
Chris@10
|
272 if (!cld) goto nada;
|
Chris@10
|
273
|
Chris@10
|
274 /* copying back from the buffer is a rank-0 DFT: */
|
Chris@10
|
275 cldcpy = X(mkplan_d)(
|
Chris@10
|
276 plnr,
|
Chris@10
|
277 X(mkproblem_dft_d)(
|
Chris@10
|
278 X(mktensor_0d)(),
|
Chris@10
|
279 X(mktensor_2d)(nbuf, bufdist, ovs,
|
Chris@10
|
280 n/2+1, 2, p->sz->dims[0].os),
|
Chris@10
|
281 bufs + roffset, bufs + ioffset,
|
Chris@10
|
282 TAINT(p->cr, ovs * nbuf), TAINT(p->ci, ovs * nbuf) ));
|
Chris@10
|
283 if (!cldcpy) goto nada;
|
Chris@10
|
284
|
Chris@10
|
285 X(ifree)(bufs); bufs = 0;
|
Chris@10
|
286
|
Chris@10
|
287 cldrest = X(mkplan_d)(plnr,
|
Chris@10
|
288 X(mkproblem_rdft2_d)(
|
Chris@10
|
289 X(tensor_copy)(p->sz),
|
Chris@10
|
290 X(mktensor_1d)(vl % nbuf, ivs, ovs),
|
Chris@10
|
291 p->r0 + id, p->r1 + id,
|
Chris@10
|
292 p->cr + od, p->ci + od,
|
Chris@10
|
293 p->kind));
|
Chris@10
|
294 if (!cldrest) goto nada;
|
Chris@10
|
295 pln = MKPLAN_RDFT2(P, &padt, apply_r2hc);
|
Chris@10
|
296 } else {
|
Chris@10
|
297 /* allow destruction of buffer */
|
Chris@10
|
298 cld = X(mkplan_f_d)(
|
Chris@10
|
299 plnr,
|
Chris@10
|
300 X(mkproblem_rdft2_d)(
|
Chris@10
|
301 X(mktensor_1d)(n, 2, p->sz->dims[0].os),
|
Chris@10
|
302 X(mktensor_1d)(nbuf, bufdist, ovs),
|
Chris@10
|
303 TAINT(p->r0, ovs * nbuf), TAINT(p->r1, ovs * nbuf),
|
Chris@10
|
304 bufs + roffset, bufs + ioffset, p->kind),
|
Chris@10
|
305 0, 0, NO_DESTROY_INPUT);
|
Chris@10
|
306 if (!cld) goto nada;
|
Chris@10
|
307
|
Chris@10
|
308 /* copying input into buffer is a rank-0 DFT: */
|
Chris@10
|
309 cldcpy = X(mkplan_d)(
|
Chris@10
|
310 plnr,
|
Chris@10
|
311 X(mkproblem_dft_d)(
|
Chris@10
|
312 X(mktensor_0d)(),
|
Chris@10
|
313 X(mktensor_2d)(nbuf, ivs, bufdist,
|
Chris@10
|
314 n/2+1, p->sz->dims[0].is, 2),
|
Chris@10
|
315 TAINT(p->cr, ivs * nbuf), TAINT(p->ci, ivs * nbuf),
|
Chris@10
|
316 bufs + roffset, bufs + ioffset));
|
Chris@10
|
317 if (!cldcpy) goto nada;
|
Chris@10
|
318
|
Chris@10
|
319 X(ifree)(bufs); bufs = 0;
|
Chris@10
|
320
|
Chris@10
|
321 cldrest = X(mkplan_d)(plnr,
|
Chris@10
|
322 X(mkproblem_rdft2_d)(
|
Chris@10
|
323 X(tensor_copy)(p->sz),
|
Chris@10
|
324 X(mktensor_1d)(vl % nbuf, ivs, ovs),
|
Chris@10
|
325 p->r0 + od, p->r1 + od,
|
Chris@10
|
326 p->cr + id, p->ci + id,
|
Chris@10
|
327 p->kind));
|
Chris@10
|
328 if (!cldrest) goto nada;
|
Chris@10
|
329
|
Chris@10
|
330 pln = MKPLAN_RDFT2(P, &padt, apply_hc2r);
|
Chris@10
|
331 }
|
Chris@10
|
332
|
Chris@10
|
333 pln->cld = cld;
|
Chris@10
|
334 pln->cldcpy = cldcpy;
|
Chris@10
|
335 pln->cldrest = cldrest;
|
Chris@10
|
336 pln->n = n;
|
Chris@10
|
337 pln->vl = vl;
|
Chris@10
|
338 pln->ivs_by_nbuf = ivs * nbuf;
|
Chris@10
|
339 pln->ovs_by_nbuf = ovs * nbuf;
|
Chris@10
|
340 pln->roffset = roffset;
|
Chris@10
|
341 pln->ioffset = ioffset;
|
Chris@10
|
342
|
Chris@10
|
343 pln->nbuf = nbuf;
|
Chris@10
|
344 pln->bufdist = bufdist;
|
Chris@10
|
345
|
Chris@10
|
346 {
|
Chris@10
|
347 opcnt t;
|
Chris@10
|
348 X(ops_add)(&cld->ops, &cldcpy->ops, &t);
|
Chris@10
|
349 X(ops_madd)(vl / nbuf, &t, &cldrest->ops, &pln->super.super.ops);
|
Chris@10
|
350 }
|
Chris@10
|
351
|
Chris@10
|
352 return &(pln->super.super);
|
Chris@10
|
353
|
Chris@10
|
354 nada:
|
Chris@10
|
355 X(ifree0)(bufs);
|
Chris@10
|
356 X(plan_destroy_internal)(cldrest);
|
Chris@10
|
357 X(plan_destroy_internal)(cldcpy);
|
Chris@10
|
358 X(plan_destroy_internal)(cld);
|
Chris@10
|
359 return (plan *) 0;
|
Chris@10
|
360 }
|
Chris@10
|
361
|
Chris@10
|
362 static solver *mksolver(int maxnbuf_ndx)
|
Chris@10
|
363 {
|
Chris@10
|
364 static const solver_adt sadt = { PROBLEM_RDFT2, mkplan, 0 };
|
Chris@10
|
365 S *slv = MKSOLVER(S, &sadt);
|
Chris@10
|
366 slv->maxnbuf_ndx = maxnbuf_ndx;
|
Chris@10
|
367 return &(slv->super);
|
Chris@10
|
368 }
|
Chris@10
|
369
|
Chris@10
|
370 void X(rdft2_buffered_register)(planner *p)
|
Chris@10
|
371 {
|
Chris@10
|
372 size_t i;
|
Chris@10
|
373 for (i = 0; i < NELEM(maxnbufs); ++i)
|
Chris@10
|
374 REGISTER_SOLVER(p, mksolver(i));
|
Chris@10
|
375 }
|