cannam@95
|
1 /*
|
cannam@95
|
2 * Copyright (c) 2003, 2007-11 Matteo Frigo
|
cannam@95
|
3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
|
cannam@95
|
4 *
|
cannam@95
|
5 * This program is free software; you can redistribute it and/or modify
|
cannam@95
|
6 * it under the terms of the GNU General Public License as published by
|
cannam@95
|
7 * the Free Software Foundation; either version 2 of the License, or
|
cannam@95
|
8 * (at your option) any later version.
|
cannam@95
|
9 *
|
cannam@95
|
10 * This program is distributed in the hope that it will be useful,
|
cannam@95
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@95
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@95
|
13 * GNU General Public License for more details.
|
cannam@95
|
14 *
|
cannam@95
|
15 * You should have received a copy of the GNU General Public License
|
cannam@95
|
16 * along with this program; if not, write to the Free Software
|
cannam@95
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
cannam@95
|
18 *
|
cannam@95
|
19 */
|
cannam@95
|
20
|
cannam@95
|
21 /* express a twiddle problem in terms of dft + multiplication by
|
cannam@95
|
22 twiddle factors */
|
cannam@95
|
23
|
cannam@95
|
24 #include "ct.h"
|
cannam@95
|
25
|
cannam@95
|
26 typedef struct {
|
cannam@95
|
27 ct_solver super;
|
cannam@95
|
28 INT batchsz;
|
cannam@95
|
29 } S;
|
cannam@95
|
30
|
cannam@95
|
31 typedef struct {
|
cannam@95
|
32 plan_dftw super;
|
cannam@95
|
33
|
cannam@95
|
34 INT r, rs, m, ms, v, vs, mb, me;
|
cannam@95
|
35 INT batchsz;
|
cannam@95
|
36 plan *cld;
|
cannam@95
|
37
|
cannam@95
|
38 triggen *t;
|
cannam@95
|
39 const S *slv;
|
cannam@95
|
40 } P;
|
cannam@95
|
41
|
cannam@95
|
42
|
cannam@95
|
43 #define BATCHDIST(r) ((r) + 16)
|
cannam@95
|
44
|
cannam@95
|
45 /**************************************************************/
|
cannam@95
|
46 static void bytwiddle(const P *ego, INT mb, INT me, R *buf, R *rio, R *iio)
|
cannam@95
|
47 {
|
cannam@95
|
48 INT j, k;
|
cannam@95
|
49 INT r = ego->r, rs = ego->rs, ms = ego->ms;
|
cannam@95
|
50 triggen *t = ego->t;
|
cannam@95
|
51 for (j = 0; j < r; ++j) {
|
cannam@95
|
52 for (k = mb; k < me; ++k)
|
cannam@95
|
53 t->rotate(t, j * k,
|
cannam@95
|
54 rio[j * rs + k * ms],
|
cannam@95
|
55 iio[j * rs + k * ms],
|
cannam@95
|
56 &buf[j * 2 + 2 * BATCHDIST(r) * (k - mb) + 0]);
|
cannam@95
|
57 }
|
cannam@95
|
58 }
|
cannam@95
|
59
|
cannam@95
|
60 static int applicable0(const S *ego,
|
cannam@95
|
61 INT r, INT irs, INT ors,
|
cannam@95
|
62 INT m, INT v,
|
cannam@95
|
63 INT mcount)
|
cannam@95
|
64 {
|
cannam@95
|
65 return (1
|
cannam@95
|
66 && v == 1
|
cannam@95
|
67 && irs == ors
|
cannam@95
|
68 && mcount >= ego->batchsz
|
cannam@95
|
69 && mcount % ego->batchsz == 0
|
cannam@95
|
70 && r >= 64
|
cannam@95
|
71 && m >= r
|
cannam@95
|
72 );
|
cannam@95
|
73 }
|
cannam@95
|
74
|
cannam@95
|
75 static int applicable(const S *ego,
|
cannam@95
|
76 INT r, INT irs, INT ors,
|
cannam@95
|
77 INT m, INT v,
|
cannam@95
|
78 INT mcount,
|
cannam@95
|
79 const planner *plnr)
|
cannam@95
|
80 {
|
cannam@95
|
81 if (!applicable0(ego, r, irs, ors, m, v, mcount))
|
cannam@95
|
82 return 0;
|
cannam@95
|
83 if (NO_UGLYP(plnr) && m * r < 65536)
|
cannam@95
|
84 return 0;
|
cannam@95
|
85
|
cannam@95
|
86 return 1;
|
cannam@95
|
87 }
|
cannam@95
|
88
|
cannam@95
|
89 static void dobatch(const P *ego, INT mb, INT me, R *buf, R *rio, R *iio)
|
cannam@95
|
90 {
|
cannam@95
|
91 plan_dft *cld;
|
cannam@95
|
92 INT ms = ego->ms;
|
cannam@95
|
93
|
cannam@95
|
94 bytwiddle(ego, mb, me, buf, rio, iio);
|
cannam@95
|
95
|
cannam@95
|
96 cld = (plan_dft *) ego->cld;
|
cannam@95
|
97 cld->apply(ego->cld, buf, buf + 1, buf, buf + 1);
|
cannam@95
|
98 X(cpy2d_pair_co)(buf, buf + 1,
|
cannam@95
|
99 rio + ms * mb, iio + ms * mb,
|
cannam@95
|
100 me-mb, 2 * BATCHDIST(ego->r), ms,
|
cannam@95
|
101 ego->r, 2, ego->rs);
|
cannam@95
|
102 }
|
cannam@95
|
103
|
cannam@95
|
104 static void apply(const plan *ego_, R *rio, R *iio)
|
cannam@95
|
105 {
|
cannam@95
|
106 const P *ego = (const P *) ego_;
|
cannam@95
|
107 R *buf = (R *) MALLOC(sizeof(R) * 2 * BATCHDIST(ego->r) * ego->batchsz,
|
cannam@95
|
108 BUFFERS);
|
cannam@95
|
109 INT m;
|
cannam@95
|
110
|
cannam@95
|
111 for (m = ego->mb; m < ego->me; m += ego->batchsz)
|
cannam@95
|
112 dobatch(ego, m, m + ego->batchsz, buf, rio, iio);
|
cannam@95
|
113
|
cannam@95
|
114 A(m == ego->me);
|
cannam@95
|
115
|
cannam@95
|
116 X(ifree)(buf);
|
cannam@95
|
117 }
|
cannam@95
|
118
|
cannam@95
|
119 static void awake(plan *ego_, enum wakefulness wakefulness)
|
cannam@95
|
120 {
|
cannam@95
|
121 P *ego = (P *) ego_;
|
cannam@95
|
122 X(plan_awake)(ego->cld, wakefulness);
|
cannam@95
|
123
|
cannam@95
|
124 switch (wakefulness) {
|
cannam@95
|
125 case SLEEPY:
|
cannam@95
|
126 X(triggen_destroy)(ego->t); ego->t = 0;
|
cannam@95
|
127 break;
|
cannam@95
|
128 default:
|
cannam@95
|
129 ego->t = X(mktriggen)(AWAKE_SQRTN_TABLE, ego->r * ego->m);
|
cannam@95
|
130 break;
|
cannam@95
|
131 }
|
cannam@95
|
132 }
|
cannam@95
|
133
|
cannam@95
|
134 static void destroy(plan *ego_)
|
cannam@95
|
135 {
|
cannam@95
|
136 P *ego = (P *) ego_;
|
cannam@95
|
137 X(plan_destroy_internal)(ego->cld);
|
cannam@95
|
138 }
|
cannam@95
|
139
|
cannam@95
|
140 static void print(const plan *ego_, printer *p)
|
cannam@95
|
141 {
|
cannam@95
|
142 const P *ego = (const P *) ego_;
|
cannam@95
|
143 p->print(p, "(dftw-genericbuf/%D-%D-%D%(%p%))",
|
cannam@95
|
144 ego->batchsz, ego->r, ego->m, ego->cld);
|
cannam@95
|
145 }
|
cannam@95
|
146
|
cannam@95
|
147 static plan *mkcldw(const ct_solver *ego_,
|
cannam@95
|
148 INT r, INT irs, INT ors,
|
cannam@95
|
149 INT m, INT ms,
|
cannam@95
|
150 INT v, INT ivs, INT ovs,
|
cannam@95
|
151 INT mstart, INT mcount,
|
cannam@95
|
152 R *rio, R *iio,
|
cannam@95
|
153 planner *plnr)
|
cannam@95
|
154 {
|
cannam@95
|
155 const S *ego = (const S *)ego_;
|
cannam@95
|
156 P *pln;
|
cannam@95
|
157 plan *cld = 0;
|
cannam@95
|
158 R *buf;
|
cannam@95
|
159
|
cannam@95
|
160 static const plan_adt padt = {
|
cannam@95
|
161 0, awake, print, destroy
|
cannam@95
|
162 };
|
cannam@95
|
163
|
cannam@95
|
164 UNUSED(ivs); UNUSED(ovs); UNUSED(rio); UNUSED(iio);
|
cannam@95
|
165
|
cannam@95
|
166 A(mstart >= 0 && mstart + mcount <= m);
|
cannam@95
|
167 if (!applicable(ego, r, irs, ors, m, v, mcount, plnr))
|
cannam@95
|
168 return (plan *)0;
|
cannam@95
|
169
|
cannam@95
|
170 buf = (R *) MALLOC(sizeof(R) * 2 * BATCHDIST(r) * ego->batchsz, BUFFERS);
|
cannam@95
|
171 cld = X(mkplan_d)(plnr,
|
cannam@95
|
172 X(mkproblem_dft_d)(
|
cannam@95
|
173 X(mktensor_1d)(r, 2, 2),
|
cannam@95
|
174 X(mktensor_1d)(ego->batchsz,
|
cannam@95
|
175 2 * BATCHDIST(r),
|
cannam@95
|
176 2 * BATCHDIST(r)),
|
cannam@95
|
177 buf, buf + 1, buf, buf + 1
|
cannam@95
|
178 )
|
cannam@95
|
179 );
|
cannam@95
|
180 X(ifree)(buf);
|
cannam@95
|
181 if (!cld) goto nada;
|
cannam@95
|
182
|
cannam@95
|
183 pln = MKPLAN_DFTW(P, &padt, apply);
|
cannam@95
|
184 pln->slv = ego;
|
cannam@95
|
185 pln->cld = cld;
|
cannam@95
|
186 pln->r = r;
|
cannam@95
|
187 pln->m = m;
|
cannam@95
|
188 pln->ms = ms;
|
cannam@95
|
189 pln->rs = irs;
|
cannam@95
|
190 pln->batchsz = ego->batchsz;
|
cannam@95
|
191 pln->mb = mstart;
|
cannam@95
|
192 pln->me = mstart + mcount;
|
cannam@95
|
193
|
cannam@95
|
194 {
|
cannam@95
|
195 double n0 = (r - 1) * (mcount - 1);
|
cannam@95
|
196 pln->super.super.ops = cld->ops;
|
cannam@95
|
197 pln->super.super.ops.mul += 8 * n0;
|
cannam@95
|
198 pln->super.super.ops.add += 4 * n0;
|
cannam@95
|
199 pln->super.super.ops.other += 8 * n0;
|
cannam@95
|
200 }
|
cannam@95
|
201 return &(pln->super.super);
|
cannam@95
|
202
|
cannam@95
|
203 nada:
|
cannam@95
|
204 X(plan_destroy_internal)(cld);
|
cannam@95
|
205 return (plan *) 0;
|
cannam@95
|
206 }
|
cannam@95
|
207
|
cannam@95
|
208 static void regsolver(planner *plnr, INT r, INT batchsz)
|
cannam@95
|
209 {
|
cannam@95
|
210 S *slv = (S *)X(mksolver_ct)(sizeof(S), r, DECDIT, mkcldw, 0);
|
cannam@95
|
211 slv->batchsz = batchsz;
|
cannam@95
|
212 REGISTER_SOLVER(plnr, &(slv->super.super));
|
cannam@95
|
213
|
cannam@95
|
214 if (X(mksolver_ct_hook)) {
|
cannam@95
|
215 slv = (S *)X(mksolver_ct_hook)(sizeof(S), r, DECDIT, mkcldw, 0);
|
cannam@95
|
216 slv->batchsz = batchsz;
|
cannam@95
|
217 REGISTER_SOLVER(plnr, &(slv->super.super));
|
cannam@95
|
218 }
|
cannam@95
|
219
|
cannam@95
|
220 }
|
cannam@95
|
221
|
cannam@95
|
222 void X(ct_genericbuf_register)(planner *p)
|
cannam@95
|
223 {
|
cannam@95
|
224 static const INT radices[] = { -1, -2, -4, -8, -16, -32, -64 };
|
cannam@95
|
225 static const INT batchsizes[] = { 4, 8, 16, 32, 64 };
|
cannam@95
|
226 unsigned i, j;
|
cannam@95
|
227
|
cannam@95
|
228 for (i = 0; i < sizeof(radices) / sizeof(radices[0]); ++i)
|
cannam@95
|
229 for (j = 0; j < sizeof(batchsizes) / sizeof(batchsizes[0]); ++j)
|
cannam@95
|
230 regsolver(p, radices[i], batchsizes[j]);
|
cannam@95
|
231 }
|