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