File: direct-r2c.c

package info (click to toggle)
fftw3 3.3.8-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 28,428 kB
  • sloc: ansic: 259,592; ml: 5,474; sh: 4,442; perl: 1,648; makefile: 1,156; fortran: 110
file content (341 lines) | stat: -rw-r--r-- 8,971 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * Copyright (c) 2003, 2007-14 Matteo Frigo
 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */


/* direct RDFT solver, using r2c codelets */

#include "rdft/rdft.h"

typedef struct {
     solver super;
     const kr2c_desc *desc;
     kr2c k;
     int bufferedp;
} S;

typedef struct {
     plan_rdft super;

     stride rs, csr, csi;
     stride brs, bcsr, bcsi;
     INT n, vl, rs0, ivs, ovs, ioffset, bioffset;
     kr2c k;
     const S *slv;
} P;

/*************************************************************
  Nonbuffered code
 *************************************************************/
static void apply_r2hc(const plan *ego_, R *I, R *O)
{
     const P *ego = (const P *) ego_;
     ASSERT_ALIGNED_DOUBLE;
     ego->k(I, I + ego->rs0, O, O + ego->ioffset, 
	    ego->rs, ego->csr, ego->csi,
	    ego->vl, ego->ivs, ego->ovs);
}

static void apply_hc2r(const plan *ego_, R *I, R *O)
{
     const P *ego = (const P *) ego_;
     ASSERT_ALIGNED_DOUBLE;
     ego->k(O, O + ego->rs0, I, I + ego->ioffset, 
	    ego->rs, ego->csr, ego->csi,
	    ego->vl, ego->ivs, ego->ovs);
}

/*************************************************************
  Buffered code
 *************************************************************/
/* should not be 2^k to avoid associativity conflicts */
static INT compute_batchsize(INT radix)
{
     /* round up to multiple of 4 */
     radix += 3;
     radix &= -4;

     return (radix + 2);
}

static void dobatch_r2hc(const P *ego, R *I, R *O, R *buf, INT batchsz)
{
     X(cpy2d_ci)(I, buf,
		 ego->n, ego->rs0, WS(ego->bcsr /* hack */, 1),
		 batchsz, ego->ivs, 1, 1);

     if (IABS(WS(ego->csr, 1)) < IABS(ego->ovs)) {
	  /* transform directly to output */
	  ego->k(buf, buf + WS(ego->bcsr /* hack */, 1), 
		 O, O + ego->ioffset, 
		 ego->brs, ego->csr, ego->csi,
		 batchsz, 1, ego->ovs);
     } else {
	  /* transform to buffer and copy back */
	  ego->k(buf, buf + WS(ego->bcsr /* hack */, 1), 
		 buf, buf + ego->bioffset, 
		 ego->brs, ego->bcsr, ego->bcsi,
		 batchsz, 1, 1);
	  X(cpy2d_co)(buf, O,
		      ego->n, WS(ego->bcsr, 1), WS(ego->csr, 1),  
		      batchsz, 1, ego->ovs, 1);
     }
}

static void dobatch_hc2r(const P *ego, R *I, R *O, R *buf, INT batchsz)
{
     if (IABS(WS(ego->csr, 1)) < IABS(ego->ivs)) {
	  /* transform directly from input */
	  ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
		 I, I + ego->ioffset, 
		 ego->brs, ego->csr, ego->csi,
		 batchsz, ego->ivs, 1);
     } else {
	  /* copy into buffer and transform in place */
	  X(cpy2d_ci)(I, buf,
		      ego->n, WS(ego->csr, 1), WS(ego->bcsr, 1),
		      batchsz, ego->ivs, 1, 1);
	  ego->k(buf, buf + WS(ego->bcsr /* hack */, 1),
		 buf, buf + ego->bioffset, 
		 ego->brs, ego->bcsr, ego->bcsi,
		 batchsz, 1, 1);
     }
     X(cpy2d_co)(buf, O,
		 ego->n, WS(ego->bcsr /* hack */, 1), ego->rs0,
		 batchsz, 1, ego->ovs, 1);
}

static void iterate(const P *ego, R *I, R *O,
		    void (*dobatch)(const P *ego, R *I, R *O, 
				    R *buf, INT batchsz))
{
     R *buf;
     INT vl = ego->vl;
     INT n = ego->n;
     INT i;
     INT batchsz = compute_batchsize(n);
     size_t bufsz = n * batchsz * sizeof(R);

     BUF_ALLOC(R *, buf, bufsz);

     for (i = 0; i < vl - batchsz; i += batchsz) {
	  dobatch(ego, I, O, buf, batchsz);
	  I += batchsz * ego->ivs;
	  O += batchsz * ego->ovs;
     }
     dobatch(ego, I, O, buf, vl - i);

     BUF_FREE(buf, bufsz);
}

static void apply_buf_r2hc(const plan *ego_, R *I, R *O)
{
     iterate((const P *) ego_, I, O, dobatch_r2hc);
}

static void apply_buf_hc2r(const plan *ego_, R *I, R *O)
{
     iterate((const P *) ego_, I, O, dobatch_hc2r);
}

static void destroy(plan *ego_)
{
     P *ego = (P *) ego_;
     X(stride_destroy)(ego->rs);
     X(stride_destroy)(ego->csr);
     X(stride_destroy)(ego->csi);
     X(stride_destroy)(ego->brs);
     X(stride_destroy)(ego->bcsr);
     X(stride_destroy)(ego->bcsi);
}

static void print(const plan *ego_, printer *p)
{
     const P *ego = (const P *) ego_;
     const S *s = ego->slv;

     if (ego->slv->bufferedp)
	  p->print(p, "(rdft-%s-directbuf/%D-r2c-%D%v \"%s\")", 
		   X(rdft_kind_str)(s->desc->genus->kind), 
		   /* hack */ WS(ego->bcsr, 1), ego->n, 
		   ego->vl, s->desc->nam);

     else 
	  p->print(p, "(rdft-%s-direct-r2c-%D%v \"%s\")", 
		   X(rdft_kind_str)(s->desc->genus->kind), ego->n, 
		   ego->vl, s->desc->nam);
}

static INT ioffset(rdft_kind kind, INT sz, INT s)
{
     return(s * ((kind == R2HC || kind == HC2R) ? sz : (sz - 1)));
}

static int applicable(const solver *ego_, const problem *p_)
{
     const S *ego = (const S *) ego_;
     const kr2c_desc *desc = ego->desc;
     const problem_rdft *p = (const problem_rdft *) p_;
     INT vl, ivs, ovs;

     return (
	  1
	  && p->sz->rnk == 1
	  && p->vecsz->rnk <= 1
	  && p->sz->dims[0].n == desc->n
	  && p->kind[0] == desc->genus->kind

	  /* check strides etc */
	  && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)

	  && (0
	      /* can operate out-of-place */
	      || p->I != p->O

	      /* computing one transform */
	      || vl == 1

	      /* can operate in-place as long as strides are the same */
	      || X(tensor_inplace_strides2)(p->sz, p->vecsz)
	       )
	  );
}

static int applicable_buf(const solver *ego_, const problem *p_)
{
     const S *ego = (const S *) ego_;
     const kr2c_desc *desc = ego->desc;
     const problem_rdft *p = (const problem_rdft *) p_;
     INT vl, ivs, ovs, batchsz;

     return (
	  1
	  && p->sz->rnk == 1
	  && p->vecsz->rnk <= 1
	  && p->sz->dims[0].n == desc->n
	  && p->kind[0] == desc->genus->kind

	  /* check strides etc */
	  && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)

	  && (batchsz = compute_batchsize(desc->n), 1)

	  && (0
	      /* can operate out-of-place */
	      || p->I != p->O

	      /* can operate in-place as long as strides are the same */
	      || X(tensor_inplace_strides2)(p->sz, p->vecsz)

	      /* can do it if the problem fits in the buffer, no matter
		 what the strides are */
	      || vl <= batchsz
	       )
	  );
}

static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
{
     const S *ego = (const S *) ego_;
     P *pln;
     const problem_rdft *p;
     iodim *d;
     INT rs, cs, b, n;

     static const plan_adt padt = {
	  X(rdft_solve), X(null_awake), print, destroy
     };

     UNUSED(plnr);

     if (ego->bufferedp) {
	  if (!applicable_buf(ego_, p_))
	       return (plan *)0;
     } else {
	  if (!applicable(ego_, p_))
	       return (plan *)0;
     }

     p = (const problem_rdft *) p_;

     if (R2HC_KINDP(p->kind[0])) {
	  rs = p->sz->dims[0].is; cs = p->sz->dims[0].os;
	  pln = MKPLAN_RDFT(P, &padt, 
			    ego->bufferedp ? apply_buf_r2hc : apply_r2hc);
     } else {
	  rs = p->sz->dims[0].os; cs = p->sz->dims[0].is;
	  pln = MKPLAN_RDFT(P, &padt, 
			    ego->bufferedp ? apply_buf_hc2r : apply_hc2r);
     }

     d = p->sz->dims;
     n = d[0].n;

     pln->k = ego->k;
     pln->n = n;

     pln->rs0 = rs;
     pln->rs = X(mkstride)(n, 2 * rs);
     pln->csr = X(mkstride)(n, cs);
     pln->csi = X(mkstride)(n, -cs);
     pln->ioffset = ioffset(p->kind[0], n, cs);

     b = compute_batchsize(n);
     pln->brs = X(mkstride)(n, 2 * b);
     pln->bcsr = X(mkstride)(n, b);
     pln->bcsi = X(mkstride)(n, -b);
     pln->bioffset = ioffset(p->kind[0], n, b);

     X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);

     pln->slv = ego;
     X(ops_zero)(&pln->super.super.ops);

     X(ops_madd2)(pln->vl / ego->desc->genus->vl,
		  &ego->desc->ops,
		  &pln->super.super.ops);

     if (ego->bufferedp) 
	  pln->super.super.ops.other += 2 * n * pln->vl;

     pln->super.super.could_prune_now_p = !ego->bufferedp;

     return &(pln->super.super);
}

/* constructor */
static solver *mksolver(kr2c k, const kr2c_desc *desc, int bufferedp)
{
     static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
     S *slv = MKSOLVER(S, &sadt);
     slv->k = k;
     slv->desc = desc;
     slv->bufferedp = bufferedp;
     return &(slv->super);
}

solver *X(mksolver_rdft_r2c_direct)(kr2c k, const kr2c_desc *desc)
{
     return mksolver(k, desc, 0);
}

solver *X(mksolver_rdft_r2c_directbuf)(kr2c k, const kr2c_desc *desc)
{
     return mksolver(k, desc, 1);
}