annotate fft/fftw/fftw-3.3.4/dft/direct.c @ 40:223f770b5341 kissfft-double tip

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