annotate src/fftw-3.3.3/dft/direct.c @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
parents 37bf6b4a2645
children
rev   line source
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
Chris@10 22 /* direct DFT solver, if we have a codelet */
Chris@10 23
Chris@10 24 #include "dft.h"
Chris@10 25
Chris@10 26 typedef struct {
Chris@10 27 solver super;
Chris@10 28 const kdft_desc *desc;
Chris@10 29 kdft k;
Chris@10 30 int bufferedp;
Chris@10 31 } S;
Chris@10 32
Chris@10 33 typedef struct {
Chris@10 34 plan_dft super;
Chris@10 35
Chris@10 36 stride is, os, bufstride;
Chris@10 37 INT n, vl, ivs, ovs;
Chris@10 38 kdft k;
Chris@10 39 const S *slv;
Chris@10 40 } P;
Chris@10 41
Chris@10 42 static void dobatch(const P *ego, R *ri, R *ii, R *ro, R *io,
Chris@10 43 R *buf, INT batchsz)
Chris@10 44 {
Chris@10 45 X(cpy2d_pair_ci)(ri, ii, buf, buf+1,
Chris@10 46 ego->n, WS(ego->is, 1), WS(ego->bufstride, 1),
Chris@10 47 batchsz, ego->ivs, 2);
Chris@10 48
Chris@10 49 if (IABS(WS(ego->os, 1)) < IABS(ego->ovs)) {
Chris@10 50 /* transform directly to output */
Chris@10 51 ego->k(buf, buf+1, ro, io,
Chris@10 52 ego->bufstride, ego->os, batchsz, 2, ego->ovs);
Chris@10 53 } else {
Chris@10 54 /* transform to buffer and copy back */
Chris@10 55 ego->k(buf, buf+1, buf, buf+1,
Chris@10 56 ego->bufstride, ego->bufstride, batchsz, 2, 2);
Chris@10 57 X(cpy2d_pair_co)(buf, buf+1, ro, io,
Chris@10 58 ego->n, WS(ego->bufstride, 1), WS(ego->os, 1),
Chris@10 59 batchsz, 2, ego->ovs);
Chris@10 60 }
Chris@10 61 }
Chris@10 62
Chris@10 63 static INT compute_batchsize(INT n)
Chris@10 64 {
Chris@10 65 /* round up to multiple of 4 */
Chris@10 66 n += 3;
Chris@10 67 n &= -4;
Chris@10 68
Chris@10 69 return (n + 2);
Chris@10 70 }
Chris@10 71
Chris@10 72 static void apply_buf(const plan *ego_, R *ri, R *ii, R *ro, R *io)
Chris@10 73 {
Chris@10 74 const P *ego = (const P *) ego_;
Chris@10 75 R *buf;
Chris@10 76 INT vl = ego->vl, n = ego->n, batchsz = compute_batchsize(n);
Chris@10 77 INT i;
Chris@10 78 size_t bufsz = n * batchsz * 2 * sizeof(R);
Chris@10 79
Chris@10 80 BUF_ALLOC(R *, buf, bufsz);
Chris@10 81
Chris@10 82 for (i = 0; i < vl - batchsz; i += batchsz) {
Chris@10 83 dobatch(ego, ri, ii, ro, io, buf, batchsz);
Chris@10 84 ri += batchsz * ego->ivs; ii += batchsz * ego->ivs;
Chris@10 85 ro += batchsz * ego->ovs; io += batchsz * ego->ovs;
Chris@10 86 }
Chris@10 87 dobatch(ego, ri, ii, ro, io, buf, vl - i);
Chris@10 88
Chris@10 89 BUF_FREE(buf, bufsz);
Chris@10 90 }
Chris@10 91
Chris@10 92 static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
Chris@10 93 {
Chris@10 94 const P *ego = (const P *) ego_;
Chris@10 95 ASSERT_ALIGNED_DOUBLE;
Chris@10 96 ego->k(ri, ii, ro, io, ego->is, ego->os, ego->vl, ego->ivs, ego->ovs);
Chris@10 97 }
Chris@10 98
Chris@10 99 static void apply_extra_iter(const plan *ego_, R *ri, R *ii, R *ro, R *io)
Chris@10 100 {
Chris@10 101 const P *ego = (const P *) ego_;
Chris@10 102 INT vl = ego->vl;
Chris@10 103
Chris@10 104 ASSERT_ALIGNED_DOUBLE;
Chris@10 105
Chris@10 106 /* for 4-way SIMD when VL is odd: iterate over an
Chris@10 107 even vector length VL, and then execute the last
Chris@10 108 iteration as a 2-vector with vector stride 0. */
Chris@10 109 ego->k(ri, ii, ro, io, ego->is, ego->os, vl - 1, ego->ivs, ego->ovs);
Chris@10 110
Chris@10 111 ego->k(ri + (vl - 1) * ego->ivs, ii + (vl - 1) * ego->ivs,
Chris@10 112 ro + (vl - 1) * ego->ovs, io + (vl - 1) * ego->ovs,
Chris@10 113 ego->is, ego->os, 1, 0, 0);
Chris@10 114 }
Chris@10 115
Chris@10 116 static void destroy(plan *ego_)
Chris@10 117 {
Chris@10 118 P *ego = (P *) ego_;
Chris@10 119 X(stride_destroy)(ego->is);
Chris@10 120 X(stride_destroy)(ego->os);
Chris@10 121 X(stride_destroy)(ego->bufstride);
Chris@10 122 }
Chris@10 123
Chris@10 124 static void print(const plan *ego_, printer *p)
Chris@10 125 {
Chris@10 126 const P *ego = (const P *) ego_;
Chris@10 127 const S *s = ego->slv;
Chris@10 128 const kdft_desc *d = s->desc;
Chris@10 129
Chris@10 130 if (ego->slv->bufferedp)
Chris@10 131 p->print(p, "(dft-directbuf/%D-%D%v \"%s\")",
Chris@10 132 compute_batchsize(d->sz), d->sz, ego->vl, d->nam);
Chris@10 133 else
Chris@10 134 p->print(p, "(dft-direct-%D%v \"%s\")", d->sz, ego->vl, d->nam);
Chris@10 135 }
Chris@10 136
Chris@10 137 static int applicable_buf(const solver *ego_, const problem *p_,
Chris@10 138 const planner *plnr)
Chris@10 139 {
Chris@10 140 const S *ego = (const S *) ego_;
Chris@10 141 const problem_dft *p = (const problem_dft *) p_;
Chris@10 142 const kdft_desc *d = ego->desc;
Chris@10 143 INT vl;
Chris@10 144 INT ivs, ovs;
Chris@10 145 INT batchsz;
Chris@10 146
Chris@10 147 return (
Chris@10 148 1
Chris@10 149 && p->sz->rnk == 1
Chris@10 150 && p->vecsz->rnk == 1
Chris@10 151 && p->sz->dims[0].n == d->sz
Chris@10 152
Chris@10 153 /* check strides etc */
Chris@10 154 && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)
Chris@10 155
Chris@10 156 /* UGLY if IS <= IVS */
Chris@10 157 && !(NO_UGLYP(plnr) &&
Chris@10 158 X(iabs)(p->sz->dims[0].is) <= X(iabs)(ivs))
Chris@10 159
Chris@10 160 && (batchsz = compute_batchsize(d->sz), 1)
Chris@10 161 && (d->genus->okp(d, 0, ((const R *)0) + 1, p->ro, p->io,
Chris@10 162 2 * batchsz, p->sz->dims[0].os,
Chris@10 163 batchsz, 2, ovs, plnr))
Chris@10 164 && (d->genus->okp(d, 0, ((const R *)0) + 1, p->ro, p->io,
Chris@10 165 2 * batchsz, p->sz->dims[0].os,
Chris@10 166 vl % batchsz, 2, ovs, plnr))
Chris@10 167
Chris@10 168
Chris@10 169 && (0
Chris@10 170 /* can operate out-of-place */
Chris@10 171 || p->ri != p->ro
Chris@10 172
Chris@10 173 /* can operate in-place as long as strides are the same */
Chris@10 174 || X(tensor_inplace_strides2)(p->sz, p->vecsz)
Chris@10 175
Chris@10 176 /* can do it if the problem fits in the buffer, no matter
Chris@10 177 what the strides are */
Chris@10 178 || vl <= batchsz
Chris@10 179 )
Chris@10 180 );
Chris@10 181 }
Chris@10 182
Chris@10 183 static int applicable(const solver *ego_, const problem *p_,
Chris@10 184 const planner *plnr, int *extra_iterp)
Chris@10 185 {
Chris@10 186 const S *ego = (const S *) ego_;
Chris@10 187 const problem_dft *p = (const problem_dft *) p_;
Chris@10 188 const kdft_desc *d = ego->desc;
Chris@10 189 INT vl;
Chris@10 190 INT ivs, ovs;
Chris@10 191
Chris@10 192 return (
Chris@10 193 1
Chris@10 194 && p->sz->rnk == 1
Chris@10 195 && p->vecsz->rnk <= 1
Chris@10 196 && p->sz->dims[0].n == d->sz
Chris@10 197
Chris@10 198 /* check strides etc */
Chris@10 199 && X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs)
Chris@10 200
Chris@10 201 && ((*extra_iterp = 0,
Chris@10 202 (d->genus->okp(d, p->ri, p->ii, p->ro, p->io,
Chris@10 203 p->sz->dims[0].is, p->sz->dims[0].os,
Chris@10 204 vl, ivs, ovs, plnr)))
Chris@10 205 ||
Chris@10 206 (*extra_iterp = 1,
Chris@10 207 ((d->genus->okp(d, p->ri, p->ii, p->ro, p->io,
Chris@10 208 p->sz->dims[0].is, p->sz->dims[0].os,
Chris@10 209 vl - 1, ivs, ovs, plnr))
Chris@10 210 &&
Chris@10 211 (d->genus->okp(d, p->ri, p->ii, p->ro, p->io,
Chris@10 212 p->sz->dims[0].is, p->sz->dims[0].os,
Chris@10 213 2, 0, 0, plnr)))))
Chris@10 214
Chris@10 215 && (0
Chris@10 216 /* can operate out-of-place */
Chris@10 217 || p->ri != p->ro
Chris@10 218
Chris@10 219 /* can always compute one transform */
Chris@10 220 || vl == 1
Chris@10 221
Chris@10 222 /* can operate in-place as long as strides are the same */
Chris@10 223 || X(tensor_inplace_strides2)(p->sz, p->vecsz)
Chris@10 224 )
Chris@10 225 );
Chris@10 226 }
Chris@10 227
Chris@10 228
Chris@10 229 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@10 230 {
Chris@10 231 const S *ego = (const S *) ego_;
Chris@10 232 P *pln;
Chris@10 233 const problem_dft *p;
Chris@10 234 iodim *d;
Chris@10 235 const kdft_desc *e = ego->desc;
Chris@10 236
Chris@10 237 static const plan_adt padt = {
Chris@10 238 X(dft_solve), X(null_awake), print, destroy
Chris@10 239 };
Chris@10 240
Chris@10 241 UNUSED(plnr);
Chris@10 242
Chris@10 243 if (ego->bufferedp) {
Chris@10 244 if (!applicable_buf(ego_, p_, plnr))
Chris@10 245 return (plan *)0;
Chris@10 246 pln = MKPLAN_DFT(P, &padt, apply_buf);
Chris@10 247 } else {
Chris@10 248 int extra_iterp = 0;
Chris@10 249 if (!applicable(ego_, p_, plnr, &extra_iterp))
Chris@10 250 return (plan *)0;
Chris@10 251 pln = MKPLAN_DFT(P, &padt, extra_iterp ? apply_extra_iter : apply);
Chris@10 252 }
Chris@10 253
Chris@10 254 p = (const problem_dft *) p_;
Chris@10 255 d = p->sz->dims;
Chris@10 256 pln->k = ego->k;
Chris@10 257 pln->n = d[0].n;
Chris@10 258 pln->is = X(mkstride)(pln->n, d[0].is);
Chris@10 259 pln->os = X(mkstride)(pln->n, d[0].os);
Chris@10 260 pln->bufstride = X(mkstride)(pln->n, 2 * compute_batchsize(pln->n));
Chris@10 261
Chris@10 262 X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
Chris@10 263 pln->slv = ego;
Chris@10 264
Chris@10 265 X(ops_zero)(&pln->super.super.ops);
Chris@10 266 X(ops_madd2)(pln->vl / e->genus->vl, &e->ops, &pln->super.super.ops);
Chris@10 267
Chris@10 268 if (ego->bufferedp)
Chris@10 269 pln->super.super.ops.other += 4 * pln->n * pln->vl;
Chris@10 270
Chris@10 271 pln->super.super.could_prune_now_p = !ego->bufferedp;
Chris@10 272 return &(pln->super.super);
Chris@10 273 }
Chris@10 274
Chris@10 275 static solver *mksolver(kdft k, const kdft_desc *desc, int bufferedp)
Chris@10 276 {
Chris@10 277 static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
Chris@10 278 S *slv = MKSOLVER(S, &sadt);
Chris@10 279 slv->k = k;
Chris@10 280 slv->desc = desc;
Chris@10 281 slv->bufferedp = bufferedp;
Chris@10 282 return &(slv->super);
Chris@10 283 }
Chris@10 284
Chris@10 285 solver *X(mksolver_dft_direct)(kdft k, const kdft_desc *desc)
Chris@10 286 {
Chris@10 287 return mksolver(k, desc, 0);
Chris@10 288 }
Chris@10 289
Chris@10 290 solver *X(mksolver_dft_directbuf)(kdft k, const kdft_desc *desc)
Chris@10 291 {
Chris@10 292 return mksolver(k, desc, 1);
Chris@10 293 }