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