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 #include "rdft.h"
|
Chris@10
|
23
|
Chris@10
|
24 typedef struct {
|
Chris@10
|
25 solver super;
|
Chris@10
|
26 } S;
|
Chris@10
|
27
|
Chris@10
|
28 typedef struct {
|
Chris@10
|
29 plan_rdft2 super;
|
Chris@10
|
30
|
Chris@10
|
31 plan *cld, *cldrest;
|
Chris@10
|
32 INT n, vl, nbuf, bufdist;
|
Chris@10
|
33 INT cs, ivs, ovs;
|
Chris@10
|
34 } P;
|
Chris@10
|
35
|
Chris@10
|
36 /***************************************************************************/
|
Chris@10
|
37
|
Chris@10
|
38 /* FIXME: have alternate copy functions that push a vector loop inside
|
Chris@10
|
39 the n loops? */
|
Chris@10
|
40
|
Chris@10
|
41 /* copy halfcomplex array r (contiguous) to complex (strided) array rio/iio. */
|
Chris@10
|
42 static void hc2c(INT n, R *r, R *rio, R *iio, INT os)
|
Chris@10
|
43 {
|
Chris@10
|
44 INT i;
|
Chris@10
|
45
|
Chris@10
|
46 rio[0] = r[0];
|
Chris@10
|
47 iio[0] = 0;
|
Chris@10
|
48
|
Chris@10
|
49 for (i = 1; i + i < n; ++i) {
|
Chris@10
|
50 rio[i * os] = r[i];
|
Chris@10
|
51 iio[i * os] = r[n - i];
|
Chris@10
|
52 }
|
Chris@10
|
53
|
Chris@10
|
54 if (i + i == n) { /* store the Nyquist frequency */
|
Chris@10
|
55 rio[i * os] = r[i];
|
Chris@10
|
56 iio[i * os] = K(0.0);
|
Chris@10
|
57 }
|
Chris@10
|
58 }
|
Chris@10
|
59
|
Chris@10
|
60 /* reverse of hc2c */
|
Chris@10
|
61 static void c2hc(INT n, R *rio, R *iio, INT is, R *r)
|
Chris@10
|
62 {
|
Chris@10
|
63 INT i;
|
Chris@10
|
64
|
Chris@10
|
65 r[0] = rio[0];
|
Chris@10
|
66
|
Chris@10
|
67 for (i = 1; i + i < n; ++i) {
|
Chris@10
|
68 r[i] = rio[i * is];
|
Chris@10
|
69 r[n - i] = iio[i * is];
|
Chris@10
|
70 }
|
Chris@10
|
71
|
Chris@10
|
72 if (i + i == n) /* store the Nyquist frequency */
|
Chris@10
|
73 r[i] = rio[i * is];
|
Chris@10
|
74 }
|
Chris@10
|
75
|
Chris@10
|
76 /***************************************************************************/
|
Chris@10
|
77
|
Chris@10
|
78 static void apply_r2hc(const plan *ego_, R *r0, R *r1, R *cr, R *ci)
|
Chris@10
|
79 {
|
Chris@10
|
80 const P *ego = (const P *) ego_;
|
Chris@10
|
81 plan_rdft *cld = (plan_rdft *) ego->cld;
|
Chris@10
|
82 INT i, j, vl = ego->vl, nbuf = ego->nbuf, bufdist = ego->bufdist;
|
Chris@10
|
83 INT n = ego->n;
|
Chris@10
|
84 INT ivs = ego->ivs, ovs = ego->ovs, os = ego->cs;
|
Chris@10
|
85 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS);
|
Chris@10
|
86 plan_rdft2 *cldrest;
|
Chris@10
|
87
|
Chris@10
|
88 for (i = nbuf; i <= vl; i += nbuf) {
|
Chris@10
|
89 /* transform to bufs: */
|
Chris@10
|
90 cld->apply((plan *) cld, r0, bufs);
|
Chris@10
|
91 r0 += ivs * nbuf; r1 += ivs * nbuf;
|
Chris@10
|
92
|
Chris@10
|
93 /* copy back */
|
Chris@10
|
94 for (j = 0; j < nbuf; ++j, cr += ovs, ci += ovs)
|
Chris@10
|
95 hc2c(n, bufs + j*bufdist, cr, ci, os);
|
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 static void apply_hc2r(const plan *ego_, R *r0, R *r1, R *cr, R *ci)
|
Chris@10
|
106 {
|
Chris@10
|
107 const P *ego = (const P *) ego_;
|
Chris@10
|
108 plan_rdft *cld = (plan_rdft *) ego->cld;
|
Chris@10
|
109 INT i, j, vl = ego->vl, nbuf = ego->nbuf, bufdist = ego->bufdist;
|
Chris@10
|
110 INT n = ego->n;
|
Chris@10
|
111 INT ivs = ego->ivs, ovs = ego->ovs, is = ego->cs;
|
Chris@10
|
112 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS);
|
Chris@10
|
113 plan_rdft2 *cldrest;
|
Chris@10
|
114
|
Chris@10
|
115 for (i = nbuf; i <= vl; i += nbuf) {
|
Chris@10
|
116 /* copy to bufs */
|
Chris@10
|
117 for (j = 0; j < nbuf; ++j, cr += ivs, ci += ivs)
|
Chris@10
|
118 c2hc(n, cr, ci, is, bufs + j*bufdist);
|
Chris@10
|
119
|
Chris@10
|
120 /* transform back: */
|
Chris@10
|
121 cld->apply((plan *) cld, bufs, r0);
|
Chris@10
|
122 r0 += ovs * nbuf; r1 += ovs * nbuf;
|
Chris@10
|
123 }
|
Chris@10
|
124
|
Chris@10
|
125 X(ifree)(bufs);
|
Chris@10
|
126
|
Chris@10
|
127 /* Do the remaining transforms, if any: */
|
Chris@10
|
128 cldrest = (plan_rdft2 *) ego->cldrest;
|
Chris@10
|
129 cldrest->apply((plan *) cldrest, r0, r1, cr, ci);
|
Chris@10
|
130 }
|
Chris@10
|
131
|
Chris@10
|
132 static void awake(plan *ego_, enum wakefulness wakefulness)
|
Chris@10
|
133 {
|
Chris@10
|
134 P *ego = (P *) ego_;
|
Chris@10
|
135
|
Chris@10
|
136 X(plan_awake)(ego->cld, wakefulness);
|
Chris@10
|
137 X(plan_awake)(ego->cldrest, wakefulness);
|
Chris@10
|
138 }
|
Chris@10
|
139
|
Chris@10
|
140 static void destroy(plan *ego_)
|
Chris@10
|
141 {
|
Chris@10
|
142 P *ego = (P *) ego_;
|
Chris@10
|
143 X(plan_destroy_internal)(ego->cldrest);
|
Chris@10
|
144 X(plan_destroy_internal)(ego->cld);
|
Chris@10
|
145 }
|
Chris@10
|
146
|
Chris@10
|
147 static void print(const plan *ego_, printer *p)
|
Chris@10
|
148 {
|
Chris@10
|
149 const P *ego = (const P *) ego_;
|
Chris@10
|
150 p->print(p, "(rdft2-rdft-%s-%D%v/%D-%D%(%p%)%(%p%))",
|
Chris@10
|
151 ego->super.apply == apply_r2hc ? "r2hc" : "hc2r",
|
Chris@10
|
152 ego->n, ego->nbuf,
|
Chris@10
|
153 ego->vl, ego->bufdist % ego->n,
|
Chris@10
|
154 ego->cld, ego->cldrest);
|
Chris@10
|
155 }
|
Chris@10
|
156
|
Chris@10
|
157 static INT min_nbuf(const problem_rdft2 *p, INT n, INT vl)
|
Chris@10
|
158 {
|
Chris@10
|
159 INT is, os, ivs, ovs;
|
Chris@10
|
160
|
Chris@10
|
161 if (p->r0 != p->cr)
|
Chris@10
|
162 return 1;
|
Chris@10
|
163 if (X(rdft2_inplace_strides(p, RNK_MINFTY)))
|
Chris@10
|
164 return 1;
|
Chris@10
|
165 A(p->vecsz->rnk == 1); /* rank 0 and MINFTY are inplace */
|
Chris@10
|
166
|
Chris@10
|
167 X(rdft2_strides)(p->kind, p->sz->dims, &is, &os);
|
Chris@10
|
168 X(rdft2_strides)(p->kind, p->vecsz->dims, &ivs, &ovs);
|
Chris@10
|
169
|
Chris@10
|
170 /* handle one potentially common case: "contiguous" real and
|
Chris@10
|
171 complex arrays, which overlap because of the differing sizes. */
|
Chris@10
|
172 if (n * X(iabs)(is) <= X(iabs)(ivs)
|
Chris@10
|
173 && (n/2 + 1) * X(iabs)(os) <= X(iabs)(ovs)
|
Chris@10
|
174 && ( ((p->cr - p->ci) <= X(iabs)(os)) ||
|
Chris@10
|
175 ((p->ci - p->cr) <= X(iabs)(os)) )
|
Chris@10
|
176 && ivs > 0 && ovs > 0) {
|
Chris@10
|
177 INT vsmin = X(imin)(ivs, ovs);
|
Chris@10
|
178 INT vsmax = X(imax)(ivs, ovs);
|
Chris@10
|
179 return(((vsmax - vsmin) * vl + vsmin - 1) / vsmin);
|
Chris@10
|
180 }
|
Chris@10
|
181
|
Chris@10
|
182 return vl; /* punt: just buffer the whole vector */
|
Chris@10
|
183 }
|
Chris@10
|
184
|
Chris@10
|
185 static int applicable0(const problem *p_, const S *ego, const planner *plnr)
|
Chris@10
|
186 {
|
Chris@10
|
187 const problem_rdft2 *p = (const problem_rdft2 *) p_;
|
Chris@10
|
188 UNUSED(ego);
|
Chris@10
|
189 return(1
|
Chris@10
|
190 && p->vecsz->rnk <= 1
|
Chris@10
|
191 && p->sz->rnk == 1
|
Chris@10
|
192
|
Chris@10
|
193 /* FIXME: does it make sense to do R2HCII ? */
|
Chris@10
|
194 && (p->kind == R2HC || p->kind == HC2R)
|
Chris@10
|
195
|
Chris@10
|
196 /* real strides must allow for reduction to rdft */
|
Chris@10
|
197 && (2 * (p->r1 - p->r0) ==
|
Chris@10
|
198 (((p->kind == R2HC) ? p->sz->dims[0].is : p->sz->dims[0].os)))
|
Chris@10
|
199
|
Chris@10
|
200 && !(X(toobig)(p->sz->dims[0].n) && CONSERVE_MEMORYP(plnr))
|
Chris@10
|
201 );
|
Chris@10
|
202 }
|
Chris@10
|
203
|
Chris@10
|
204 static int applicable(const problem *p_, const S *ego, const planner *plnr)
|
Chris@10
|
205 {
|
Chris@10
|
206 const problem_rdft2 *p;
|
Chris@10
|
207
|
Chris@10
|
208 if (NO_BUFFERINGP(plnr)) return 0;
|
Chris@10
|
209
|
Chris@10
|
210 if (!applicable0(p_, ego, plnr)) return 0;
|
Chris@10
|
211
|
Chris@10
|
212 p = (const problem_rdft2 *) p_;
|
Chris@10
|
213 if (NO_UGLYP(plnr)) {
|
Chris@10
|
214 if (p->r0 != p->cr) return 0;
|
Chris@10
|
215 if (X(toobig)(p->sz->dims[0].n)) return 0;
|
Chris@10
|
216 }
|
Chris@10
|
217 return 1;
|
Chris@10
|
218 }
|
Chris@10
|
219
|
Chris@10
|
220 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
|
Chris@10
|
221 {
|
Chris@10
|
222 const S *ego = (const S *) ego_;
|
Chris@10
|
223 P *pln;
|
Chris@10
|
224 plan *cld = (plan *) 0;
|
Chris@10
|
225 plan *cldrest = (plan *) 0;
|
Chris@10
|
226 const problem_rdft2 *p = (const problem_rdft2 *) p_;
|
Chris@10
|
227 R *bufs = (R *) 0;
|
Chris@10
|
228 INT nbuf = 0, bufdist, n, vl;
|
Chris@10
|
229 INT ivs, ovs, rs, id, od;
|
Chris@10
|
230
|
Chris@10
|
231 static const plan_adt padt = {
|
Chris@10
|
232 X(rdft2_solve), awake, print, destroy
|
Chris@10
|
233 };
|
Chris@10
|
234
|
Chris@10
|
235 if (!applicable(p_, ego, plnr))
|
Chris@10
|
236 goto nada;
|
Chris@10
|
237
|
Chris@10
|
238 n = p->sz->dims[0].n;
|
Chris@10
|
239 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs);
|
Chris@10
|
240
|
Chris@10
|
241 nbuf = X(imax)(X(nbuf)(n, vl, 0), min_nbuf(p, n, vl));
|
Chris@10
|
242 bufdist = X(bufdist)(n, vl);
|
Chris@10
|
243 A(nbuf > 0);
|
Chris@10
|
244
|
Chris@10
|
245 /* initial allocation for the purpose of planning */
|
Chris@10
|
246 bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS);
|
Chris@10
|
247
|
Chris@10
|
248 id = ivs * (nbuf * (vl / nbuf));
|
Chris@10
|
249 od = ovs * (nbuf * (vl / nbuf));
|
Chris@10
|
250
|
Chris@10
|
251 if (p->kind == R2HC) {
|
Chris@10
|
252 cld = X(mkplan_f_d)(
|
Chris@10
|
253 plnr,
|
Chris@10
|
254 X(mkproblem_rdft_d)(
|
Chris@10
|
255 X(mktensor_1d)(n, p->sz->dims[0].is/2, 1),
|
Chris@10
|
256 X(mktensor_1d)(nbuf, ivs, bufdist),
|
Chris@10
|
257 TAINT(p->r0, ivs * nbuf), bufs, &p->kind),
|
Chris@10
|
258 0, 0, (p->r0 == p->cr) ? NO_DESTROY_INPUT : 0);
|
Chris@10
|
259 if (!cld) goto nada;
|
Chris@10
|
260 X(ifree)(bufs); bufs = 0;
|
Chris@10
|
261
|
Chris@10
|
262 cldrest = X(mkplan_d)(plnr,
|
Chris@10
|
263 X(mkproblem_rdft2_d)(
|
Chris@10
|
264 X(tensor_copy)(p->sz),
|
Chris@10
|
265 X(mktensor_1d)(vl % nbuf, ivs, ovs),
|
Chris@10
|
266 p->r0 + id, p->r1 + id,
|
Chris@10
|
267 p->cr + od, p->ci + od,
|
Chris@10
|
268 p->kind));
|
Chris@10
|
269 if (!cldrest) goto nada;
|
Chris@10
|
270
|
Chris@10
|
271 pln = MKPLAN_RDFT2(P, &padt, apply_r2hc);
|
Chris@10
|
272 } else {
|
Chris@10
|
273 A(p->kind == HC2R);
|
Chris@10
|
274 cld = X(mkplan_f_d)(
|
Chris@10
|
275 plnr,
|
Chris@10
|
276 X(mkproblem_rdft_d)(
|
Chris@10
|
277 X(mktensor_1d)(n, 1, p->sz->dims[0].os/2),
|
Chris@10
|
278 X(mktensor_1d)(nbuf, bufdist, ovs),
|
Chris@10
|
279 bufs, TAINT(p->r0, ovs * nbuf), &p->kind),
|
Chris@10
|
280 0, 0, NO_DESTROY_INPUT); /* always ok to destroy bufs */
|
Chris@10
|
281 if (!cld) goto nada;
|
Chris@10
|
282 X(ifree)(bufs); bufs = 0;
|
Chris@10
|
283
|
Chris@10
|
284 cldrest = X(mkplan_d)(plnr,
|
Chris@10
|
285 X(mkproblem_rdft2_d)(
|
Chris@10
|
286 X(tensor_copy)(p->sz),
|
Chris@10
|
287 X(mktensor_1d)(vl % nbuf, ivs, ovs),
|
Chris@10
|
288 p->r0 + od, p->r1 + od,
|
Chris@10
|
289 p->cr + id, p->ci + id,
|
Chris@10
|
290 p->kind));
|
Chris@10
|
291 if (!cldrest) goto nada;
|
Chris@10
|
292 pln = MKPLAN_RDFT2(P, &padt, apply_hc2r);
|
Chris@10
|
293 }
|
Chris@10
|
294
|
Chris@10
|
295 pln->cld = cld;
|
Chris@10
|
296 pln->cldrest = cldrest;
|
Chris@10
|
297 pln->n = n;
|
Chris@10
|
298 pln->vl = vl;
|
Chris@10
|
299 pln->ivs = ivs;
|
Chris@10
|
300 pln->ovs = ovs;
|
Chris@10
|
301 X(rdft2_strides)(p->kind, &p->sz->dims[0], &rs, &pln->cs);
|
Chris@10
|
302 pln->nbuf = nbuf;
|
Chris@10
|
303 pln->bufdist = bufdist;
|
Chris@10
|
304
|
Chris@10
|
305 X(ops_madd)(vl / nbuf, &cld->ops, &cldrest->ops,
|
Chris@10
|
306 &pln->super.super.ops);
|
Chris@10
|
307 pln->super.super.ops.other += (p->kind == R2HC ? (n + 2) : n) * vl;
|
Chris@10
|
308
|
Chris@10
|
309 return &(pln->super.super);
|
Chris@10
|
310
|
Chris@10
|
311 nada:
|
Chris@10
|
312 X(ifree0)(bufs);
|
Chris@10
|
313 X(plan_destroy_internal)(cldrest);
|
Chris@10
|
314 X(plan_destroy_internal)(cld);
|
Chris@10
|
315 return (plan *) 0;
|
Chris@10
|
316 }
|
Chris@10
|
317
|
Chris@10
|
318 static solver *mksolver(void)
|
Chris@10
|
319 {
|
Chris@10
|
320 static const solver_adt sadt = { PROBLEM_RDFT2, mkplan, 0 };
|
Chris@10
|
321 S *slv = MKSOLVER(S, &sadt);
|
Chris@10
|
322 return &(slv->super);
|
Chris@10
|
323 }
|
Chris@10
|
324
|
Chris@10
|
325 void X(rdft2_rdft_register)(planner *p)
|
Chris@10
|
326 {
|
Chris@10
|
327 REGISTER_SOLVER(p, mksolver());
|
Chris@10
|
328 }
|