annotate fft/fftw/fftw-3.3.4/rdft/hc2hc-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 #include "hc2hc.h"
Chris@19 23
Chris@19 24 typedef struct {
Chris@19 25 hc2hc_solver super;
Chris@19 26 const hc2hc_desc *desc;
Chris@19 27 khc2hc k;
Chris@19 28 int bufferedp;
Chris@19 29 } S;
Chris@19 30
Chris@19 31 typedef struct {
Chris@19 32 plan_hc2hc super;
Chris@19 33 khc2hc k;
Chris@19 34 plan *cld0, *cldm; /* children for 0th and middle butterflies */
Chris@19 35 INT r, m, v;
Chris@19 36 INT ms, vs, mb, me;
Chris@19 37 stride rs, brs;
Chris@19 38 twid *td;
Chris@19 39 const S *slv;
Chris@19 40 } P;
Chris@19 41
Chris@19 42 /*************************************************************
Chris@19 43 Nonbuffered code
Chris@19 44 *************************************************************/
Chris@19 45 static void apply(const plan *ego_, R *IO)
Chris@19 46 {
Chris@19 47 const P *ego = (const P *) ego_;
Chris@19 48 plan_rdft *cld0 = (plan_rdft *) ego->cld0;
Chris@19 49 plan_rdft *cldm = (plan_rdft *) ego->cldm;
Chris@19 50 INT i, m = ego->m, v = ego->v;
Chris@19 51 INT mb = ego->mb, me = ego->me;
Chris@19 52 INT ms = ego->ms, vs = ego->vs;
Chris@19 53
Chris@19 54 for (i = 0; i < v; ++i, IO += vs) {
Chris@19 55 cld0->apply((plan *) cld0, IO, IO);
Chris@19 56 ego->k(IO + ms * mb, IO + (m - mb) * ms,
Chris@19 57 ego->td->W, ego->rs, mb, me, ms);
Chris@19 58 cldm->apply((plan *) cldm, IO + (m/2) * ms, IO + (m/2) * ms);
Chris@19 59 }
Chris@19 60 }
Chris@19 61
Chris@19 62 /*************************************************************
Chris@19 63 Buffered code
Chris@19 64 *************************************************************/
Chris@19 65
Chris@19 66 /* should not be 2^k to avoid associativity conflicts */
Chris@19 67 static INT compute_batchsize(INT radix)
Chris@19 68 {
Chris@19 69 /* round up to multiple of 4 */
Chris@19 70 radix += 3;
Chris@19 71 radix &= -4;
Chris@19 72
Chris@19 73 return (radix + 2);
Chris@19 74 }
Chris@19 75
Chris@19 76 static void dobatch(const P *ego, R *IOp, R *IOm,
Chris@19 77 INT mb, INT me, R *bufp)
Chris@19 78 {
Chris@19 79 INT b = WS(ego->brs, 1);
Chris@19 80 INT rs = WS(ego->rs, 1);
Chris@19 81 INT r = ego->r;
Chris@19 82 INT ms = ego->ms;
Chris@19 83 R *bufm = bufp + b - 1;
Chris@19 84
Chris@19 85 X(cpy2d_ci)(IOp + mb * ms, bufp, r, rs, b, me - mb, ms, 1, 1);
Chris@19 86 X(cpy2d_ci)(IOm - mb * ms, bufm, r, rs, b, me - mb, -ms, -1, 1);
Chris@19 87
Chris@19 88 ego->k(bufp, bufm, ego->td->W, ego->brs, mb, me, 1);
Chris@19 89
Chris@19 90 X(cpy2d_co)(bufp, IOp + mb * ms, r, b, rs, me - mb, 1, ms, 1);
Chris@19 91 X(cpy2d_co)(bufm, IOm - mb * ms, r, b, rs, me - mb, -1, -ms, 1);
Chris@19 92 }
Chris@19 93
Chris@19 94 static void apply_buf(const plan *ego_, R *IO)
Chris@19 95 {
Chris@19 96 const P *ego = (const P *) ego_;
Chris@19 97 plan_rdft *cld0 = (plan_rdft *) ego->cld0;
Chris@19 98 plan_rdft *cldm = (plan_rdft *) ego->cldm;
Chris@19 99 INT i, j, m = ego->m, v = ego->v, r = ego->r;
Chris@19 100 INT mb = ego->mb, me = ego->me, ms = ego->ms;
Chris@19 101 INT batchsz = compute_batchsize(r);
Chris@19 102 R *buf;
Chris@19 103 size_t bufsz = r * batchsz * 2 * sizeof(R);
Chris@19 104
Chris@19 105 BUF_ALLOC(R *, buf, bufsz);
Chris@19 106
Chris@19 107 for (i = 0; i < v; ++i, IO += ego->vs) {
Chris@19 108 R *IOp = IO;
Chris@19 109 R *IOm = IO + m * ms;
Chris@19 110
Chris@19 111 cld0->apply((plan *) cld0, IO, IO);
Chris@19 112
Chris@19 113 for (j = mb; j + batchsz < me; j += batchsz)
Chris@19 114 dobatch(ego, IOp, IOm, j, j + batchsz, buf);
Chris@19 115
Chris@19 116 dobatch(ego, IOp, IOm, j, me, buf);
Chris@19 117
Chris@19 118 cldm->apply((plan *) cldm, IO + ms * (m/2), IO + ms * (m/2));
Chris@19 119 }
Chris@19 120
Chris@19 121 BUF_FREE(buf, bufsz);
Chris@19 122 }
Chris@19 123
Chris@19 124 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@19 125 {
Chris@19 126 P *ego = (P *) ego_;
Chris@19 127
Chris@19 128 X(plan_awake)(ego->cld0, wakefulness);
Chris@19 129 X(plan_awake)(ego->cldm, wakefulness);
Chris@19 130 X(twiddle_awake)(wakefulness, &ego->td, ego->slv->desc->tw,
Chris@19 131 ego->r * ego->m, ego->r, (ego->m - 1) / 2);
Chris@19 132 }
Chris@19 133
Chris@19 134 static void destroy(plan *ego_)
Chris@19 135 {
Chris@19 136 P *ego = (P *) ego_;
Chris@19 137 X(plan_destroy_internal)(ego->cld0);
Chris@19 138 X(plan_destroy_internal)(ego->cldm);
Chris@19 139 X(stride_destroy)(ego->rs);
Chris@19 140 X(stride_destroy)(ego->brs);
Chris@19 141 }
Chris@19 142
Chris@19 143 static void print(const plan *ego_, printer *p)
Chris@19 144 {
Chris@19 145 const P *ego = (const P *) ego_;
Chris@19 146 const S *slv = ego->slv;
Chris@19 147 const hc2hc_desc *e = slv->desc;
Chris@19 148 INT batchsz = compute_batchsize(ego->r);
Chris@19 149
Chris@19 150 if (slv->bufferedp)
Chris@19 151 p->print(p, "(hc2hc-directbuf/%D-%D/%D%v \"%s\"%(%p%)%(%p%))",
Chris@19 152 batchsz, ego->r, X(twiddle_length)(ego->r, e->tw),
Chris@19 153 ego->v, e->nam, ego->cld0, ego->cldm);
Chris@19 154 else
Chris@19 155 p->print(p, "(hc2hc-direct-%D/%D%v \"%s\"%(%p%)%(%p%))",
Chris@19 156 ego->r, X(twiddle_length)(ego->r, e->tw), ego->v, e->nam,
Chris@19 157 ego->cld0, ego->cldm);
Chris@19 158 }
Chris@19 159
Chris@19 160 static int applicable0(const S *ego, rdft_kind kind, INT r)
Chris@19 161 {
Chris@19 162 const hc2hc_desc *e = ego->desc;
Chris@19 163
Chris@19 164 return (1
Chris@19 165 && r == e->radix
Chris@19 166 && kind == e->genus->kind
Chris@19 167 );
Chris@19 168 }
Chris@19 169
Chris@19 170 static int applicable(const S *ego, rdft_kind kind, INT r, INT m, INT v,
Chris@19 171 const planner *plnr)
Chris@19 172 {
Chris@19 173 if (!applicable0(ego, kind, r))
Chris@19 174 return 0;
Chris@19 175
Chris@19 176 if (NO_UGLYP(plnr) && X(ct_uglyp)((ego->bufferedp? (INT)512 : (INT)16),
Chris@19 177 v, m * r, r))
Chris@19 178 return 0;
Chris@19 179
Chris@19 180 return 1;
Chris@19 181 }
Chris@19 182
Chris@19 183 #define CLDMP(m, mstart, mcount) (2 * ((mstart) + (mcount)) == (m) + 2)
Chris@19 184 #define CLD0P(mstart) ((mstart) == 0)
Chris@19 185
Chris@19 186 static plan *mkcldw(const hc2hc_solver *ego_,
Chris@19 187 rdft_kind kind, INT r, INT m, INT ms, INT v, INT vs,
Chris@19 188 INT mstart, INT mcount,
Chris@19 189 R *IO, planner *plnr)
Chris@19 190 {
Chris@19 191 const S *ego = (const S *) ego_;
Chris@19 192 P *pln;
Chris@19 193 const hc2hc_desc *e = ego->desc;
Chris@19 194 plan *cld0 = 0, *cldm = 0;
Chris@19 195 INT imid = (m / 2) * ms;
Chris@19 196 INT rs = m * ms;
Chris@19 197
Chris@19 198 static const plan_adt padt = {
Chris@19 199 0, awake, print, destroy
Chris@19 200 };
Chris@19 201
Chris@19 202 if (!applicable(ego, kind, r, m, v, plnr))
Chris@19 203 return (plan *)0;
Chris@19 204
Chris@19 205 cld0 = X(mkplan_d)(
Chris@19 206 plnr,
Chris@19 207 X(mkproblem_rdft_1_d)((CLD0P(mstart) ?
Chris@19 208 X(mktensor_1d)(r, rs, rs) : X(mktensor_0d)()),
Chris@19 209 X(mktensor_0d)(),
Chris@19 210 TAINT(IO, vs), TAINT(IO, vs),
Chris@19 211 kind));
Chris@19 212 if (!cld0) goto nada;
Chris@19 213
Chris@19 214 cldm = X(mkplan_d)(
Chris@19 215 plnr,
Chris@19 216 X(mkproblem_rdft_1_d)((CLDMP(m, mstart, mcount) ?
Chris@19 217 X(mktensor_1d)(r, rs, rs) : X(mktensor_0d)()),
Chris@19 218 X(mktensor_0d)(),
Chris@19 219 TAINT(IO + imid, vs), TAINT(IO + imid, vs),
Chris@19 220 kind == R2HC ? R2HCII : HC2RIII));
Chris@19 221 if (!cldm) goto nada;
Chris@19 222
Chris@19 223 pln = MKPLAN_HC2HC(P, &padt, ego->bufferedp ? apply_buf : apply);
Chris@19 224
Chris@19 225 pln->k = ego->k;
Chris@19 226 pln->td = 0;
Chris@19 227 pln->r = r; pln->rs = X(mkstride)(r, rs);
Chris@19 228 pln->m = m; pln->ms = ms;
Chris@19 229 pln->v = v; pln->vs = vs;
Chris@19 230 pln->slv = ego;
Chris@19 231 pln->brs = X(mkstride)(r, 2 * compute_batchsize(r));
Chris@19 232 pln->cld0 = cld0;
Chris@19 233 pln->cldm = cldm;
Chris@19 234 pln->mb = mstart + CLD0P(mstart);
Chris@19 235 pln->me = mstart + mcount - CLDMP(m, mstart, mcount);
Chris@19 236
Chris@19 237 X(ops_zero)(&pln->super.super.ops);
Chris@19 238 X(ops_madd2)(v * ((pln->me - pln->mb) / e->genus->vl),
Chris@19 239 &e->ops, &pln->super.super.ops);
Chris@19 240 X(ops_madd2)(v, &cld0->ops, &pln->super.super.ops);
Chris@19 241 X(ops_madd2)(v, &cldm->ops, &pln->super.super.ops);
Chris@19 242
Chris@19 243 if (ego->bufferedp)
Chris@19 244 pln->super.super.ops.other += 4 * r * (pln->me - pln->mb) * v;
Chris@19 245
Chris@19 246 pln->super.super.could_prune_now_p =
Chris@19 247 (!ego->bufferedp && r >= 5 && r < 64 && m >= r);
Chris@19 248
Chris@19 249 return &(pln->super.super);
Chris@19 250
Chris@19 251 nada:
Chris@19 252 X(plan_destroy_internal)(cld0);
Chris@19 253 X(plan_destroy_internal)(cldm);
Chris@19 254 return 0;
Chris@19 255 }
Chris@19 256
Chris@19 257 static void regone(planner *plnr, khc2hc codelet, const hc2hc_desc *desc,
Chris@19 258 int bufferedp)
Chris@19 259 {
Chris@19 260 S *slv = (S *)X(mksolver_hc2hc)(sizeof(S), desc->radix, mkcldw);
Chris@19 261 slv->k = codelet;
Chris@19 262 slv->desc = desc;
Chris@19 263 slv->bufferedp = bufferedp;
Chris@19 264 REGISTER_SOLVER(plnr, &(slv->super.super));
Chris@19 265 if (X(mksolver_hc2hc_hook)) {
Chris@19 266 slv = (S *)X(mksolver_hc2hc_hook)(sizeof(S), desc->radix, mkcldw);
Chris@19 267 slv->k = codelet;
Chris@19 268 slv->desc = desc;
Chris@19 269 slv->bufferedp = bufferedp;
Chris@19 270 REGISTER_SOLVER(plnr, &(slv->super.super));
Chris@19 271 }
Chris@19 272 }
Chris@19 273
Chris@19 274 void X(regsolver_hc2hc_direct)(planner *plnr, khc2hc codelet,
Chris@19 275 const hc2hc_desc *desc)
Chris@19 276 {
Chris@19 277 regone(plnr, codelet, desc, /* bufferedp */0);
Chris@19 278 regone(plnr, codelet, desc, /* bufferedp */1);
Chris@19 279 }