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 /* express a twiddle problem in terms of dft + multiplication by
|
Chris@10
|
22 twiddle factors */
|
Chris@10
|
23
|
Chris@10
|
24 #include "ct.h"
|
Chris@10
|
25
|
Chris@10
|
26 typedef struct {
|
Chris@10
|
27 ct_solver super;
|
Chris@10
|
28 INT batchsz;
|
Chris@10
|
29 } S;
|
Chris@10
|
30
|
Chris@10
|
31 typedef struct {
|
Chris@10
|
32 plan_dftw super;
|
Chris@10
|
33
|
Chris@10
|
34 INT r, rs, m, ms, v, vs, mb, me;
|
Chris@10
|
35 INT batchsz;
|
Chris@10
|
36 plan *cld;
|
Chris@10
|
37
|
Chris@10
|
38 triggen *t;
|
Chris@10
|
39 const S *slv;
|
Chris@10
|
40 } P;
|
Chris@10
|
41
|
Chris@10
|
42
|
Chris@10
|
43 #define BATCHDIST(r) ((r) + 16)
|
Chris@10
|
44
|
Chris@10
|
45 /**************************************************************/
|
Chris@10
|
46 static void bytwiddle(const P *ego, INT mb, INT me, R *buf, R *rio, R *iio)
|
Chris@10
|
47 {
|
Chris@10
|
48 INT j, k;
|
Chris@10
|
49 INT r = ego->r, rs = ego->rs, ms = ego->ms;
|
Chris@10
|
50 triggen *t = ego->t;
|
Chris@10
|
51 for (j = 0; j < r; ++j) {
|
Chris@10
|
52 for (k = mb; k < me; ++k)
|
Chris@10
|
53 t->rotate(t, j * k,
|
Chris@10
|
54 rio[j * rs + k * ms],
|
Chris@10
|
55 iio[j * rs + k * ms],
|
Chris@10
|
56 &buf[j * 2 + 2 * BATCHDIST(r) * (k - mb) + 0]);
|
Chris@10
|
57 }
|
Chris@10
|
58 }
|
Chris@10
|
59
|
Chris@10
|
60 static int applicable0(const S *ego,
|
Chris@10
|
61 INT r, INT irs, INT ors,
|
Chris@10
|
62 INT m, INT v,
|
Chris@10
|
63 INT mcount)
|
Chris@10
|
64 {
|
Chris@10
|
65 return (1
|
Chris@10
|
66 && v == 1
|
Chris@10
|
67 && irs == ors
|
Chris@10
|
68 && mcount >= ego->batchsz
|
Chris@10
|
69 && mcount % ego->batchsz == 0
|
Chris@10
|
70 && r >= 64
|
Chris@10
|
71 && m >= r
|
Chris@10
|
72 );
|
Chris@10
|
73 }
|
Chris@10
|
74
|
Chris@10
|
75 static int applicable(const S *ego,
|
Chris@10
|
76 INT r, INT irs, INT ors,
|
Chris@10
|
77 INT m, INT v,
|
Chris@10
|
78 INT mcount,
|
Chris@10
|
79 const planner *plnr)
|
Chris@10
|
80 {
|
Chris@10
|
81 if (!applicable0(ego, r, irs, ors, m, v, mcount))
|
Chris@10
|
82 return 0;
|
Chris@10
|
83 if (NO_UGLYP(plnr) && m * r < 65536)
|
Chris@10
|
84 return 0;
|
Chris@10
|
85
|
Chris@10
|
86 return 1;
|
Chris@10
|
87 }
|
Chris@10
|
88
|
Chris@10
|
89 static void dobatch(const P *ego, INT mb, INT me, R *buf, R *rio, R *iio)
|
Chris@10
|
90 {
|
Chris@10
|
91 plan_dft *cld;
|
Chris@10
|
92 INT ms = ego->ms;
|
Chris@10
|
93
|
Chris@10
|
94 bytwiddle(ego, mb, me, buf, rio, iio);
|
Chris@10
|
95
|
Chris@10
|
96 cld = (plan_dft *) ego->cld;
|
Chris@10
|
97 cld->apply(ego->cld, buf, buf + 1, buf, buf + 1);
|
Chris@10
|
98 X(cpy2d_pair_co)(buf, buf + 1,
|
Chris@10
|
99 rio + ms * mb, iio + ms * mb,
|
Chris@10
|
100 me-mb, 2 * BATCHDIST(ego->r), ms,
|
Chris@10
|
101 ego->r, 2, ego->rs);
|
Chris@10
|
102 }
|
Chris@10
|
103
|
Chris@10
|
104 static void apply(const plan *ego_, R *rio, R *iio)
|
Chris@10
|
105 {
|
Chris@10
|
106 const P *ego = (const P *) ego_;
|
Chris@10
|
107 R *buf = (R *) MALLOC(sizeof(R) * 2 * BATCHDIST(ego->r) * ego->batchsz,
|
Chris@10
|
108 BUFFERS);
|
Chris@10
|
109 INT m;
|
Chris@10
|
110
|
Chris@10
|
111 for (m = ego->mb; m < ego->me; m += ego->batchsz)
|
Chris@10
|
112 dobatch(ego, m, m + ego->batchsz, buf, rio, iio);
|
Chris@10
|
113
|
Chris@10
|
114 A(m == ego->me);
|
Chris@10
|
115
|
Chris@10
|
116 X(ifree)(buf);
|
Chris@10
|
117 }
|
Chris@10
|
118
|
Chris@10
|
119 static void awake(plan *ego_, enum wakefulness wakefulness)
|
Chris@10
|
120 {
|
Chris@10
|
121 P *ego = (P *) ego_;
|
Chris@10
|
122 X(plan_awake)(ego->cld, wakefulness);
|
Chris@10
|
123
|
Chris@10
|
124 switch (wakefulness) {
|
Chris@10
|
125 case SLEEPY:
|
Chris@10
|
126 X(triggen_destroy)(ego->t); ego->t = 0;
|
Chris@10
|
127 break;
|
Chris@10
|
128 default:
|
Chris@10
|
129 ego->t = X(mktriggen)(AWAKE_SQRTN_TABLE, ego->r * ego->m);
|
Chris@10
|
130 break;
|
Chris@10
|
131 }
|
Chris@10
|
132 }
|
Chris@10
|
133
|
Chris@10
|
134 static void destroy(plan *ego_)
|
Chris@10
|
135 {
|
Chris@10
|
136 P *ego = (P *) ego_;
|
Chris@10
|
137 X(plan_destroy_internal)(ego->cld);
|
Chris@10
|
138 }
|
Chris@10
|
139
|
Chris@10
|
140 static void print(const plan *ego_, printer *p)
|
Chris@10
|
141 {
|
Chris@10
|
142 const P *ego = (const P *) ego_;
|
Chris@10
|
143 p->print(p, "(dftw-genericbuf/%D-%D-%D%(%p%))",
|
Chris@10
|
144 ego->batchsz, ego->r, ego->m, ego->cld);
|
Chris@10
|
145 }
|
Chris@10
|
146
|
Chris@10
|
147 static plan *mkcldw(const ct_solver *ego_,
|
Chris@10
|
148 INT r, INT irs, INT ors,
|
Chris@10
|
149 INT m, INT ms,
|
Chris@10
|
150 INT v, INT ivs, INT ovs,
|
Chris@10
|
151 INT mstart, INT mcount,
|
Chris@10
|
152 R *rio, R *iio,
|
Chris@10
|
153 planner *plnr)
|
Chris@10
|
154 {
|
Chris@10
|
155 const S *ego = (const S *)ego_;
|
Chris@10
|
156 P *pln;
|
Chris@10
|
157 plan *cld = 0;
|
Chris@10
|
158 R *buf;
|
Chris@10
|
159
|
Chris@10
|
160 static const plan_adt padt = {
|
Chris@10
|
161 0, awake, print, destroy
|
Chris@10
|
162 };
|
Chris@10
|
163
|
Chris@10
|
164 UNUSED(ivs); UNUSED(ovs); UNUSED(rio); UNUSED(iio);
|
Chris@10
|
165
|
Chris@10
|
166 A(mstart >= 0 && mstart + mcount <= m);
|
Chris@10
|
167 if (!applicable(ego, r, irs, ors, m, v, mcount, plnr))
|
Chris@10
|
168 return (plan *)0;
|
Chris@10
|
169
|
Chris@10
|
170 buf = (R *) MALLOC(sizeof(R) * 2 * BATCHDIST(r) * ego->batchsz, BUFFERS);
|
Chris@10
|
171 cld = X(mkplan_d)(plnr,
|
Chris@10
|
172 X(mkproblem_dft_d)(
|
Chris@10
|
173 X(mktensor_1d)(r, 2, 2),
|
Chris@10
|
174 X(mktensor_1d)(ego->batchsz,
|
Chris@10
|
175 2 * BATCHDIST(r),
|
Chris@10
|
176 2 * BATCHDIST(r)),
|
Chris@10
|
177 buf, buf + 1, buf, buf + 1
|
Chris@10
|
178 )
|
Chris@10
|
179 );
|
Chris@10
|
180 X(ifree)(buf);
|
Chris@10
|
181 if (!cld) goto nada;
|
Chris@10
|
182
|
Chris@10
|
183 pln = MKPLAN_DFTW(P, &padt, apply);
|
Chris@10
|
184 pln->slv = ego;
|
Chris@10
|
185 pln->cld = cld;
|
Chris@10
|
186 pln->r = r;
|
Chris@10
|
187 pln->m = m;
|
Chris@10
|
188 pln->ms = ms;
|
Chris@10
|
189 pln->rs = irs;
|
Chris@10
|
190 pln->batchsz = ego->batchsz;
|
Chris@10
|
191 pln->mb = mstart;
|
Chris@10
|
192 pln->me = mstart + mcount;
|
Chris@10
|
193
|
Chris@10
|
194 {
|
Chris@10
|
195 double n0 = (r - 1) * (mcount - 1);
|
Chris@10
|
196 pln->super.super.ops = cld->ops;
|
Chris@10
|
197 pln->super.super.ops.mul += 8 * n0;
|
Chris@10
|
198 pln->super.super.ops.add += 4 * n0;
|
Chris@10
|
199 pln->super.super.ops.other += 8 * n0;
|
Chris@10
|
200 }
|
Chris@10
|
201 return &(pln->super.super);
|
Chris@10
|
202
|
Chris@10
|
203 nada:
|
Chris@10
|
204 X(plan_destroy_internal)(cld);
|
Chris@10
|
205 return (plan *) 0;
|
Chris@10
|
206 }
|
Chris@10
|
207
|
Chris@10
|
208 static void regsolver(planner *plnr, INT r, INT batchsz)
|
Chris@10
|
209 {
|
Chris@10
|
210 S *slv = (S *)X(mksolver_ct)(sizeof(S), r, DECDIT, mkcldw, 0);
|
Chris@10
|
211 slv->batchsz = batchsz;
|
Chris@10
|
212 REGISTER_SOLVER(plnr, &(slv->super.super));
|
Chris@10
|
213
|
Chris@10
|
214 if (X(mksolver_ct_hook)) {
|
Chris@10
|
215 slv = (S *)X(mksolver_ct_hook)(sizeof(S), r, DECDIT, mkcldw, 0);
|
Chris@10
|
216 slv->batchsz = batchsz;
|
Chris@10
|
217 REGISTER_SOLVER(plnr, &(slv->super.super));
|
Chris@10
|
218 }
|
Chris@10
|
219
|
Chris@10
|
220 }
|
Chris@10
|
221
|
Chris@10
|
222 void X(ct_genericbuf_register)(planner *p)
|
Chris@10
|
223 {
|
Chris@10
|
224 static const INT radices[] = { -1, -2, -4, -8, -16, -32, -64 };
|
Chris@10
|
225 static const INT batchsizes[] = { 4, 8, 16, 32, 64 };
|
Chris@10
|
226 unsigned i, j;
|
Chris@10
|
227
|
Chris@10
|
228 for (i = 0; i < sizeof(radices) / sizeof(radices[0]); ++i)
|
Chris@10
|
229 for (j = 0; j < sizeof(batchsizes) / sizeof(batchsizes[0]); ++j)
|
Chris@10
|
230 regsolver(p, radices[i], batchsizes[j]);
|
Chris@10
|
231 }
|