/src/fftw3/rdft/direct-r2c.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2003, 2007-14 Matteo Frigo |
3 | | * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation; either version 2 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this program; if not, write to the Free Software |
17 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | | * |
19 | | */ |
20 | | |
21 | | |
22 | | /* direct RDFT solver, using r2c codelets */ |
23 | | |
24 | | #include "rdft/rdft.h" |
25 | | |
26 | | typedef struct { |
27 | | solver super; |
28 | | const kr2c_desc *desc; |
29 | | kr2c k; |
30 | | int bufferedp; |
31 | | } S; |
32 | | |
33 | | typedef struct { |
34 | | plan_rdft super; |
35 | | |
36 | | stride rs, csr, csi; |
37 | | stride brs, bcsr, bcsi; |
38 | | INT n, vl, rs0, ivs, ovs, ioffset, bioffset; |
39 | | kr2c k; |
40 | | const S *slv; |
41 | | } P; |
42 | | |
43 | | /************************************************************* |
44 | | Nonbuffered code |
45 | | *************************************************************/ |
46 | | static void apply_r2hc(const plan *ego_, R *I, R *O) |
47 | 0 | { |
48 | 0 | const P *ego = (const P *) ego_; |
49 | 0 | ASSERT_ALIGNED_DOUBLE; |
50 | 0 | ego->k(I, I + ego->rs0, O, O + ego->ioffset, |
51 | 0 | ego->rs, ego->csr, ego->csi, |
52 | 0 | ego->vl, ego->ivs, ego->ovs); |
53 | 0 | } |
54 | | |
55 | | static void apply_hc2r(const plan *ego_, R *I, R *O) |
56 | 0 | { |
57 | 0 | const P *ego = (const P *) ego_; |
58 | 0 | ASSERT_ALIGNED_DOUBLE; |
59 | 0 | ego->k(O, O + ego->rs0, I, I + ego->ioffset, |
60 | 0 | ego->rs, ego->csr, ego->csi, |
61 | 0 | ego->vl, ego->ivs, ego->ovs); |
62 | 0 | } |
63 | | |
64 | | /************************************************************* |
65 | | Buffered code |
66 | | *************************************************************/ |
67 | | /* should not be 2^k to avoid associativity conflicts */ |
68 | | static INT compute_batchsize(INT radix) |
69 | 0 | { |
70 | | /* round up to multiple of 4 */ |
71 | 0 | radix += 3; |
72 | 0 | radix &= -4; |
73 | |
|
74 | 0 | return (radix + 2); |
75 | 0 | } |
76 | | |
77 | | static void dobatch_r2hc(const P *ego, R *I, R *O, R *buf, INT batchsz) |
78 | 0 | { |
79 | 0 | X(cpy2d_ci)(I, buf, |
80 | 0 | ego->n, ego->rs0, WS(ego->bcsr /* hack */, 1), |
81 | 0 | batchsz, ego->ivs, 1, 1); |
82 | |
|
83 | 0 | if (IABS(WS(ego->csr, 1)) < IABS(ego->ovs)) { |
84 | | /* transform directly to output */ |
85 | 0 | ego->k(buf, buf + WS(ego->bcsr /* hack */, 1), |
86 | 0 | O, O + ego->ioffset, |
87 | 0 | ego->brs, ego->csr, ego->csi, |
88 | 0 | batchsz, 1, ego->ovs); |
89 | 0 | } else { |
90 | | /* transform to buffer and copy back */ |
91 | 0 | ego->k(buf, buf + WS(ego->bcsr /* hack */, 1), |
92 | 0 | buf, buf + ego->bioffset, |
93 | 0 | ego->brs, ego->bcsr, ego->bcsi, |
94 | 0 | batchsz, 1, 1); |
95 | 0 | X(cpy2d_co)(buf, O, |
96 | 0 | ego->n, WS(ego->bcsr, 1), WS(ego->csr, 1), |
97 | 0 | batchsz, 1, ego->ovs, 1); |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | | static void dobatch_hc2r(const P *ego, R *I, R *O, R *buf, INT batchsz) |
102 | 0 | { |
103 | 0 | if (IABS(WS(ego->csr, 1)) < IABS(ego->ivs)) { |
104 | | /* transform directly from input */ |
105 | 0 | ego->k(buf, buf + WS(ego->bcsr /* hack */, 1), |
106 | 0 | I, I + ego->ioffset, |
107 | 0 | ego->brs, ego->csr, ego->csi, |
108 | 0 | batchsz, ego->ivs, 1); |
109 | 0 | } else { |
110 | | /* copy into buffer and transform in place */ |
111 | 0 | X(cpy2d_ci)(I, buf, |
112 | 0 | ego->n, WS(ego->csr, 1), WS(ego->bcsr, 1), |
113 | 0 | batchsz, ego->ivs, 1, 1); |
114 | 0 | ego->k(buf, buf + WS(ego->bcsr /* hack */, 1), |
115 | 0 | buf, buf + ego->bioffset, |
116 | 0 | ego->brs, ego->bcsr, ego->bcsi, |
117 | 0 | batchsz, 1, 1); |
118 | 0 | } |
119 | 0 | X(cpy2d_co)(buf, O, |
120 | 0 | ego->n, WS(ego->bcsr /* hack */, 1), ego->rs0, |
121 | 0 | batchsz, 1, ego->ovs, 1); |
122 | 0 | } |
123 | | |
124 | | static void iterate(const P *ego, R *I, R *O, |
125 | | void (*dobatch)(const P *ego, R *I, R *O, |
126 | | R *buf, INT batchsz)) |
127 | 0 | { |
128 | 0 | R *buf; |
129 | 0 | INT vl = ego->vl; |
130 | 0 | INT n = ego->n; |
131 | 0 | INT i; |
132 | 0 | INT batchsz = compute_batchsize(n); |
133 | 0 | size_t bufsz = n * batchsz * sizeof(R); |
134 | |
|
135 | 0 | BUF_ALLOC(R *, buf, bufsz); |
136 | |
|
137 | 0 | for (i = 0; i < vl - batchsz; i += batchsz) { |
138 | 0 | dobatch(ego, I, O, buf, batchsz); |
139 | 0 | I += batchsz * ego->ivs; |
140 | 0 | O += batchsz * ego->ovs; |
141 | 0 | } |
142 | 0 | dobatch(ego, I, O, buf, vl - i); |
143 | |
|
144 | 0 | BUF_FREE(buf, bufsz); |
145 | 0 | } |
146 | | |
147 | | static void apply_buf_r2hc(const plan *ego_, R *I, R *O) |
148 | 0 | { |
149 | 0 | iterate((const P *) ego_, I, O, dobatch_r2hc); |
150 | 0 | } |
151 | | |
152 | | static void apply_buf_hc2r(const plan *ego_, R *I, R *O) |
153 | 0 | { |
154 | 0 | iterate((const P *) ego_, I, O, dobatch_hc2r); |
155 | 0 | } |
156 | | |
157 | | static void destroy(plan *ego_) |
158 | 0 | { |
159 | 0 | P *ego = (P *) ego_; |
160 | 0 | X(stride_destroy)(ego->rs); |
161 | 0 | X(stride_destroy)(ego->csr); |
162 | 0 | X(stride_destroy)(ego->csi); |
163 | 0 | X(stride_destroy)(ego->brs); |
164 | 0 | X(stride_destroy)(ego->bcsr); |
165 | 0 | X(stride_destroy)(ego->bcsi); |
166 | 0 | } |
167 | | |
168 | | static void print(const plan *ego_, printer *p) |
169 | 0 | { |
170 | 0 | const P *ego = (const P *) ego_; |
171 | 0 | const S *s = ego->slv; |
172 | |
|
173 | 0 | if (ego->slv->bufferedp) |
174 | 0 | p->print(p, "(rdft-%s-directbuf/%D-r2c-%D%v \"%s\")", |
175 | 0 | X(rdft_kind_str)(s->desc->genus->kind), |
176 | 0 | /* hack */ WS(ego->bcsr, 1), ego->n, |
177 | 0 | ego->vl, s->desc->nam); |
178 | | |
179 | 0 | else |
180 | 0 | p->print(p, "(rdft-%s-direct-r2c-%D%v \"%s\")", |
181 | 0 | X(rdft_kind_str)(s->desc->genus->kind), ego->n, |
182 | 0 | ego->vl, s->desc->nam); |
183 | 0 | } |
184 | | |
185 | | static INT ioffset(rdft_kind kind, INT sz, INT s) |
186 | 0 | { |
187 | 0 | return(s * ((kind == R2HC || kind == HC2R) ? sz : (sz - 1))); |
188 | 0 | } |
189 | | |
190 | | static int applicable(const solver *ego_, const problem *p_) |
191 | 23.7k | { |
192 | 23.7k | const S *ego = (const S *) ego_; |
193 | 23.7k | const kr2c_desc *desc = ego->desc; |
194 | 23.7k | const problem_rdft *p = (const problem_rdft *) p_; |
195 | 23.7k | INT vl, ivs, ovs; |
196 | | |
197 | 23.7k | return ( |
198 | 23.7k | 1 |
199 | 23.7k | && p->sz->rnk == 1 |
200 | 23.7k | && p->vecsz->rnk <= 1 |
201 | 23.7k | && p->sz->dims[0].n == desc->n |
202 | 23.7k | && p->kind[0] == desc->genus->kind |
203 | | |
204 | | /* check strides etc */ |
205 | 23.7k | && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs) |
206 | | |
207 | 23.7k | && (0 |
208 | | /* can operate out-of-place */ |
209 | 0 | || p->I != p->O |
210 | | |
211 | | /* computing one transform */ |
212 | 0 | || vl == 1 |
213 | | |
214 | | /* can operate in-place as long as strides are the same */ |
215 | 0 | || X(tensor_inplace_strides2)(p->sz, p->vecsz) |
216 | 0 | ) |
217 | 23.7k | ); |
218 | 23.7k | } |
219 | | |
220 | | static int applicable_buf(const solver *ego_, const problem *p_) |
221 | 23.7k | { |
222 | 23.7k | const S *ego = (const S *) ego_; |
223 | 23.7k | const kr2c_desc *desc = ego->desc; |
224 | 23.7k | const problem_rdft *p = (const problem_rdft *) p_; |
225 | 23.7k | INT vl, ivs, ovs, batchsz; |
226 | | |
227 | 23.7k | return ( |
228 | 23.7k | 1 |
229 | 23.7k | && p->sz->rnk == 1 |
230 | 23.7k | && p->vecsz->rnk <= 1 |
231 | 23.7k | && p->sz->dims[0].n == desc->n |
232 | 23.7k | && p->kind[0] == desc->genus->kind |
233 | | |
234 | | /* check strides etc */ |
235 | 23.7k | && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs) |
236 | | |
237 | 23.7k | && (batchsz = compute_batchsize(desc->n), 1) |
238 | | |
239 | 23.7k | && (0 |
240 | | /* can operate out-of-place */ |
241 | 0 | || p->I != p->O |
242 | | |
243 | | /* can operate in-place as long as strides are the same */ |
244 | 0 | || X(tensor_inplace_strides2)(p->sz, p->vecsz) |
245 | | |
246 | | /* can do it if the problem fits in the buffer, no matter |
247 | | what the strides are */ |
248 | 0 | || vl <= batchsz |
249 | 0 | ) |
250 | 23.7k | ); |
251 | 23.7k | } |
252 | | |
253 | | static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) |
254 | 47.5k | { |
255 | 47.5k | const S *ego = (const S *) ego_; |
256 | 47.5k | P *pln; |
257 | 47.5k | const problem_rdft *p; |
258 | 47.5k | iodim *d; |
259 | 47.5k | INT rs, cs, b, n; |
260 | | |
261 | 47.5k | static const plan_adt padt = { |
262 | 47.5k | X(rdft_solve), X(null_awake), print, destroy |
263 | 47.5k | }; |
264 | | |
265 | 47.5k | UNUSED(plnr); |
266 | | |
267 | 47.5k | if (ego->bufferedp) { |
268 | 23.7k | if (!applicable_buf(ego_, p_)) |
269 | 23.7k | return (plan *)0; |
270 | 23.7k | } else { |
271 | 23.7k | if (!applicable(ego_, p_)) |
272 | 23.7k | return (plan *)0; |
273 | 23.7k | } |
274 | | |
275 | 0 | p = (const problem_rdft *) p_; |
276 | |
|
277 | 0 | if (R2HC_KINDP(p->kind[0])) { |
278 | 0 | rs = p->sz->dims[0].is; cs = p->sz->dims[0].os; |
279 | 0 | pln = MKPLAN_RDFT(P, &padt, |
280 | 0 | ego->bufferedp ? apply_buf_r2hc : apply_r2hc); |
281 | 0 | } else { |
282 | 0 | rs = p->sz->dims[0].os; cs = p->sz->dims[0].is; |
283 | 0 | pln = MKPLAN_RDFT(P, &padt, |
284 | 0 | ego->bufferedp ? apply_buf_hc2r : apply_hc2r); |
285 | 0 | } |
286 | |
|
287 | 0 | d = p->sz->dims; |
288 | 0 | n = d[0].n; |
289 | |
|
290 | 0 | pln->k = ego->k; |
291 | 0 | pln->n = n; |
292 | |
|
293 | 0 | pln->rs0 = rs; |
294 | 0 | pln->rs = X(mkstride)(n, 2 * rs); |
295 | 0 | pln->csr = X(mkstride)(n, cs); |
296 | 0 | pln->csi = X(mkstride)(n, -cs); |
297 | 0 | pln->ioffset = ioffset(p->kind[0], n, cs); |
298 | |
|
299 | 0 | b = compute_batchsize(n); |
300 | 0 | pln->brs = X(mkstride)(n, 2 * b); |
301 | 0 | pln->bcsr = X(mkstride)(n, b); |
302 | 0 | pln->bcsi = X(mkstride)(n, -b); |
303 | 0 | pln->bioffset = ioffset(p->kind[0], n, b); |
304 | |
|
305 | 0 | X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs); |
306 | |
|
307 | 0 | pln->slv = ego; |
308 | 0 | X(ops_zero)(&pln->super.super.ops); |
309 | |
|
310 | 0 | X(ops_madd2)(pln->vl / ego->desc->genus->vl, |
311 | 0 | &ego->desc->ops, |
312 | 0 | &pln->super.super.ops); |
313 | |
|
314 | 0 | if (ego->bufferedp) |
315 | 0 | pln->super.super.ops.other += 2 * n * pln->vl; |
316 | |
|
317 | 0 | pln->super.super.could_prune_now_p = !ego->bufferedp; |
318 | |
|
319 | 0 | return &(pln->super.super); |
320 | 47.5k | } |
321 | | |
322 | | /* constructor */ |
323 | | static solver *mksolver(kr2c k, const kr2c_desc *desc, int bufferedp) |
324 | 144 | { |
325 | 144 | static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; |
326 | 144 | S *slv = MKSOLVER(S, &sadt); |
327 | 144 | slv->k = k; |
328 | 144 | slv->desc = desc; |
329 | 144 | slv->bufferedp = bufferedp; |
330 | 144 | return &(slv->super); |
331 | 144 | } |
332 | | |
333 | | solver *X(mksolver_rdft_r2c_direct)(kr2c k, const kr2c_desc *desc) |
334 | 72 | { |
335 | 72 | return mksolver(k, desc, 0); |
336 | 72 | } |
337 | | |
338 | | solver *X(mksolver_rdft_r2c_directbuf)(kr2c k, const kr2c_desc *desc) |
339 | 72 | { |
340 | 72 | return mksolver(k, desc, 1); |
341 | 72 | } |