annotate fft/fftw/fftw-3.3.4/dft/buffered.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 #include "dft.h"
Chris@19 23
Chris@19 24 typedef struct {
Chris@19 25 solver super;
Chris@19 26 int maxnbuf_ndx;
Chris@19 27 } S;
Chris@19 28
Chris@19 29 static const INT maxnbufs[] = { 8, 256 };
Chris@19 30
Chris@19 31 typedef struct {
Chris@19 32 plan_dft super;
Chris@19 33
Chris@19 34 plan *cld, *cldcpy, *cldrest;
Chris@19 35 INT n, vl, nbuf, bufdist;
Chris@19 36 INT ivs_by_nbuf, ovs_by_nbuf;
Chris@19 37 INT roffset, ioffset;
Chris@19 38 } P;
Chris@19 39
Chris@19 40 /* transform a vector input with the help of bufs */
Chris@19 41 static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
Chris@19 42 {
Chris@19 43 const P *ego = (const P *) ego_;
Chris@19 44 INT nbuf = ego->nbuf;
Chris@19 45 R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist * 2, BUFFERS);
Chris@19 46
Chris@19 47 plan_dft *cld = (plan_dft *) ego->cld;
Chris@19 48 plan_dft *cldcpy = (plan_dft *) ego->cldcpy;
Chris@19 49 plan_dft *cldrest;
Chris@19 50 INT i, vl = ego->vl;
Chris@19 51 INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf;
Chris@19 52 INT roffset = ego->roffset, ioffset = ego->ioffset;
Chris@19 53
Chris@19 54 for (i = nbuf; i <= vl; i += nbuf) {
Chris@19 55 /* transform to bufs: */
Chris@19 56 cld->apply((plan *) cld, ri, ii, bufs + roffset, bufs + ioffset);
Chris@19 57 ri += ivs_by_nbuf; ii += ivs_by_nbuf;
Chris@19 58
Chris@19 59 /* copy back */
Chris@19 60 cldcpy->apply((plan *) cldcpy, bufs+roffset, bufs+ioffset, ro, io);
Chris@19 61 ro += ovs_by_nbuf; io += ovs_by_nbuf;
Chris@19 62 }
Chris@19 63
Chris@19 64 X(ifree)(bufs);
Chris@19 65
Chris@19 66 /* Do the remaining transforms, if any: */
Chris@19 67 cldrest = (plan_dft *) ego->cldrest;
Chris@19 68 cldrest->apply((plan *) cldrest, ri, ii, ro, io);
Chris@19 69 }
Chris@19 70
Chris@19 71
Chris@19 72 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@19 73 {
Chris@19 74 P *ego = (P *) ego_;
Chris@19 75
Chris@19 76 X(plan_awake)(ego->cld, wakefulness);
Chris@19 77 X(plan_awake)(ego->cldcpy, wakefulness);
Chris@19 78 X(plan_awake)(ego->cldrest, wakefulness);
Chris@19 79 }
Chris@19 80
Chris@19 81 static void destroy(plan *ego_)
Chris@19 82 {
Chris@19 83 P *ego = (P *) ego_;
Chris@19 84 X(plan_destroy_internal)(ego->cldrest);
Chris@19 85 X(plan_destroy_internal)(ego->cldcpy);
Chris@19 86 X(plan_destroy_internal)(ego->cld);
Chris@19 87 }
Chris@19 88
Chris@19 89 static void print(const plan *ego_, printer *p)
Chris@19 90 {
Chris@19 91 const P *ego = (const P *) ego_;
Chris@19 92 p->print(p, "(dft-buffered-%D%v/%D-%D%(%p%)%(%p%)%(%p%))",
Chris@19 93 ego->n, ego->nbuf,
Chris@19 94 ego->vl, ego->bufdist % ego->n,
Chris@19 95 ego->cld, ego->cldcpy, ego->cldrest);
Chris@19 96 }
Chris@19 97
Chris@19 98 static int applicable0(const S *ego, const problem *p_, const planner *plnr)
Chris@19 99 {
Chris@19 100 const problem_dft *p = (const problem_dft *) p_;
Chris@19 101 const iodim *d = p->sz->dims;
Chris@19 102
Chris@19 103 if (1
Chris@19 104 && p->vecsz->rnk <= 1
Chris@19 105 && p->sz->rnk == 1
Chris@19 106 ) {
Chris@19 107 INT vl, ivs, ovs;
Chris@19 108 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs);
Chris@19 109
Chris@19 110 if (X(toobig)(p->sz->dims[0].n) && CONSERVE_MEMORYP(plnr))
Chris@19 111 return 0;
Chris@19 112
Chris@19 113 /* if this solver is redundant, in the sense that a solver
Chris@19 114 of lower index generates the same plan, then prune this
Chris@19 115 solver */
Chris@19 116 if (X(nbuf_redundant)(d[0].n, vl,
Chris@19 117 ego->maxnbuf_ndx,
Chris@19 118 maxnbufs, NELEM(maxnbufs)))
Chris@19 119 return 0;
Chris@19 120
Chris@19 121 /*
Chris@19 122 In principle, the buffered transforms might be useful
Chris@19 123 when working out of place. However, in order to
Chris@19 124 prevent infinite loops in the planner, we require
Chris@19 125 that the output stride of the buffered transforms be
Chris@19 126 greater than 2.
Chris@19 127 */
Chris@19 128 if (p->ri != p->ro)
Chris@19 129 return (d[0].os > 2);
Chris@19 130
Chris@19 131 /*
Chris@19 132 * If the problem is in place, the input/output strides must
Chris@19 133 * be the same or the whole thing must fit in the buffer.
Chris@19 134 */
Chris@19 135 if (X(tensor_inplace_strides2)(p->sz, p->vecsz))
Chris@19 136 return 1;
Chris@19 137
Chris@19 138 if (/* fits into buffer: */
Chris@19 139 ((p->vecsz->rnk == 0)
Chris@19 140 ||
Chris@19 141 (X(nbuf)(d[0].n, p->vecsz->dims[0].n,
Chris@19 142 maxnbufs[ego->maxnbuf_ndx])
Chris@19 143 == p->vecsz->dims[0].n)))
Chris@19 144 return 1;
Chris@19 145 }
Chris@19 146
Chris@19 147 return 0;
Chris@19 148 }
Chris@19 149
Chris@19 150 static int applicable(const S *ego, const problem *p_, const planner *plnr)
Chris@19 151 {
Chris@19 152 if (NO_BUFFERINGP(plnr)) return 0;
Chris@19 153 if (!applicable0(ego, p_, plnr)) return 0;
Chris@19 154
Chris@19 155 if (NO_UGLYP(plnr)) {
Chris@19 156 const problem_dft *p = (const problem_dft *) p_;
Chris@19 157 if (p->ri != p->ro) return 0;
Chris@19 158 if (X(toobig)(p->sz->dims[0].n)) return 0;
Chris@19 159 }
Chris@19 160 return 1;
Chris@19 161 }
Chris@19 162
Chris@19 163 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@19 164 {
Chris@19 165 P *pln;
Chris@19 166 const S *ego = (const S *)ego_;
Chris@19 167 plan *cld = (plan *) 0;
Chris@19 168 plan *cldcpy = (plan *) 0;
Chris@19 169 plan *cldrest = (plan *) 0;
Chris@19 170 const problem_dft *p = (const problem_dft *) p_;
Chris@19 171 R *bufs = (R *) 0;
Chris@19 172 INT nbuf = 0, bufdist, n, vl;
Chris@19 173 INT ivs, ovs, roffset, ioffset;
Chris@19 174
Chris@19 175 static const plan_adt padt = {
Chris@19 176 X(dft_solve), awake, print, destroy
Chris@19 177 };
Chris@19 178
Chris@19 179 if (!applicable(ego, p_, plnr))
Chris@19 180 goto nada;
Chris@19 181
Chris@19 182 n = X(tensor_sz)(p->sz);
Chris@19 183
Chris@19 184 X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs);
Chris@19 185
Chris@19 186 nbuf = X(nbuf)(n, vl, maxnbufs[ego->maxnbuf_ndx]);
Chris@19 187 bufdist = X(bufdist)(n, vl);
Chris@19 188 A(nbuf > 0);
Chris@19 189
Chris@19 190 /* attempt to keep real and imaginary part in the same order,
Chris@19 191 so as to allow optimizations in the the copy plan */
Chris@19 192 roffset = (p->ri - p->ii > 0) ? (INT)1 : (INT)0;
Chris@19 193 ioffset = 1 - roffset;
Chris@19 194
Chris@19 195 /* initial allocation for the purpose of planning */
Chris@19 196 bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist * 2, BUFFERS);
Chris@19 197
Chris@19 198 /* allow destruction of input if problem is in place */
Chris@19 199 cld = X(mkplan_f_d)(plnr,
Chris@19 200 X(mkproblem_dft_d)(
Chris@19 201 X(mktensor_1d)(n, p->sz->dims[0].is, 2),
Chris@19 202 X(mktensor_1d)(nbuf, ivs, bufdist * 2),
Chris@19 203 TAINT(p->ri, ivs * nbuf),
Chris@19 204 TAINT(p->ii, ivs * nbuf),
Chris@19 205 bufs + roffset,
Chris@19 206 bufs + ioffset),
Chris@19 207 0, 0, (p->ri == p->ro) ? NO_DESTROY_INPUT : 0);
Chris@19 208 if (!cld)
Chris@19 209 goto nada;
Chris@19 210
Chris@19 211 /* copying back from the buffer is a rank-0 transform: */
Chris@19 212 cldcpy = X(mkplan_d)(plnr,
Chris@19 213 X(mkproblem_dft_d)(
Chris@19 214 X(mktensor_0d)(),
Chris@19 215 X(mktensor_2d)(nbuf, bufdist * 2, ovs,
Chris@19 216 n, 2, p->sz->dims[0].os),
Chris@19 217 bufs + roffset,
Chris@19 218 bufs + ioffset,
Chris@19 219 TAINT(p->ro, ovs * nbuf),
Chris@19 220 TAINT(p->io, ovs * nbuf)));
Chris@19 221 if (!cldcpy)
Chris@19 222 goto nada;
Chris@19 223
Chris@19 224 /* deallocate buffers, let apply() allocate them for real */
Chris@19 225 X(ifree)(bufs);
Chris@19 226 bufs = 0;
Chris@19 227
Chris@19 228 /* plan the leftover transforms (cldrest): */
Chris@19 229 {
Chris@19 230 INT id = ivs * (nbuf * (vl / nbuf));
Chris@19 231 INT od = ovs * (nbuf * (vl / nbuf));
Chris@19 232 cldrest = X(mkplan_d)(plnr,
Chris@19 233 X(mkproblem_dft_d)(
Chris@19 234 X(tensor_copy)(p->sz),
Chris@19 235 X(mktensor_1d)(vl % nbuf, ivs, ovs),
Chris@19 236 p->ri+id, p->ii+id, p->ro+od, p->io+od));
Chris@19 237 }
Chris@19 238 if (!cldrest)
Chris@19 239 goto nada;
Chris@19 240
Chris@19 241 pln = MKPLAN_DFT(P, &padt, apply);
Chris@19 242 pln->cld = cld;
Chris@19 243 pln->cldcpy = cldcpy;
Chris@19 244 pln->cldrest = cldrest;
Chris@19 245 pln->n = n;
Chris@19 246 pln->vl = vl;
Chris@19 247 pln->ivs_by_nbuf = ivs * nbuf;
Chris@19 248 pln->ovs_by_nbuf = ovs * nbuf;
Chris@19 249 pln->roffset = roffset;
Chris@19 250 pln->ioffset = ioffset;
Chris@19 251
Chris@19 252 pln->nbuf = nbuf;
Chris@19 253 pln->bufdist = bufdist;
Chris@19 254
Chris@19 255 {
Chris@19 256 opcnt t;
Chris@19 257 X(ops_add)(&cld->ops, &cldcpy->ops, &t);
Chris@19 258 X(ops_madd)(vl / nbuf, &t, &cldrest->ops, &pln->super.super.ops);
Chris@19 259 }
Chris@19 260
Chris@19 261 return &(pln->super.super);
Chris@19 262
Chris@19 263 nada:
Chris@19 264 X(ifree0)(bufs);
Chris@19 265 X(plan_destroy_internal)(cldrest);
Chris@19 266 X(plan_destroy_internal)(cldcpy);
Chris@19 267 X(plan_destroy_internal)(cld);
Chris@19 268 return (plan *) 0;
Chris@19 269 }
Chris@19 270
Chris@19 271 static solver *mksolver(int maxnbuf_ndx)
Chris@19 272 {
Chris@19 273 static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
Chris@19 274 S *slv = MKSOLVER(S, &sadt);
Chris@19 275 slv->maxnbuf_ndx = maxnbuf_ndx;
Chris@19 276 return &(slv->super);
Chris@19 277 }
Chris@19 278
Chris@19 279 void X(dft_buffered_register)(planner *p)
Chris@19 280 {
Chris@19 281 size_t i;
Chris@19 282 for (i = 0; i < NELEM(maxnbufs); ++i)
Chris@19 283 REGISTER_SOLVER(p, mksolver(i));
Chris@19 284 }