annotate src/fftw-3.3.5/dft/direct.c @ 42:2cd0e3b3e1fd

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