Chris@10: /* Chris@10: * Copyright (c) 2003, 2007-11 Matteo Frigo Chris@10: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology Chris@10: * Chris@10: * This program is free software; you can redistribute it and/or modify Chris@10: * it under the terms of the GNU General Public License as published by Chris@10: * the Free Software Foundation; either version 2 of the License, or Chris@10: * (at your option) any later version. Chris@10: * Chris@10: * This program is distributed in the hope that it will be useful, Chris@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@10: * GNU General Public License for more details. Chris@10: * Chris@10: * You should have received a copy of the GNU General Public License Chris@10: * along with this program; if not, write to the Free Software Chris@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@10: * Chris@10: */ Chris@10: Chris@10: /* express a twiddle problem in terms of dft + multiplication by Chris@10: twiddle factors */ Chris@10: Chris@10: #include "ct.h" Chris@10: Chris@10: typedef struct { Chris@10: ct_solver super; Chris@10: INT batchsz; Chris@10: } S; Chris@10: Chris@10: typedef struct { Chris@10: plan_dftw super; Chris@10: Chris@10: INT r, rs, m, ms, v, vs, mb, me; Chris@10: INT batchsz; Chris@10: plan *cld; Chris@10: Chris@10: triggen *t; Chris@10: const S *slv; Chris@10: } P; Chris@10: Chris@10: Chris@10: #define BATCHDIST(r) ((r) + 16) Chris@10: Chris@10: /**************************************************************/ Chris@10: static void bytwiddle(const P *ego, INT mb, INT me, R *buf, R *rio, R *iio) Chris@10: { Chris@10: INT j, k; Chris@10: INT r = ego->r, rs = ego->rs, ms = ego->ms; Chris@10: triggen *t = ego->t; Chris@10: for (j = 0; j < r; ++j) { Chris@10: for (k = mb; k < me; ++k) Chris@10: t->rotate(t, j * k, Chris@10: rio[j * rs + k * ms], Chris@10: iio[j * rs + k * ms], Chris@10: &buf[j * 2 + 2 * BATCHDIST(r) * (k - mb) + 0]); Chris@10: } Chris@10: } Chris@10: Chris@10: static int applicable0(const S *ego, Chris@10: INT r, INT irs, INT ors, Chris@10: INT m, INT v, Chris@10: INT mcount) Chris@10: { Chris@10: return (1 Chris@10: && v == 1 Chris@10: && irs == ors Chris@10: && mcount >= ego->batchsz Chris@10: && mcount % ego->batchsz == 0 Chris@10: && r >= 64 Chris@10: && m >= r Chris@10: ); Chris@10: } Chris@10: Chris@10: static int applicable(const S *ego, Chris@10: INT r, INT irs, INT ors, Chris@10: INT m, INT v, Chris@10: INT mcount, Chris@10: const planner *plnr) Chris@10: { Chris@10: if (!applicable0(ego, r, irs, ors, m, v, mcount)) Chris@10: return 0; Chris@10: if (NO_UGLYP(plnr) && m * r < 65536) Chris@10: return 0; Chris@10: Chris@10: return 1; Chris@10: } Chris@10: Chris@10: static void dobatch(const P *ego, INT mb, INT me, R *buf, R *rio, R *iio) Chris@10: { Chris@10: plan_dft *cld; Chris@10: INT ms = ego->ms; Chris@10: Chris@10: bytwiddle(ego, mb, me, buf, rio, iio); Chris@10: Chris@10: cld = (plan_dft *) ego->cld; Chris@10: cld->apply(ego->cld, buf, buf + 1, buf, buf + 1); Chris@10: X(cpy2d_pair_co)(buf, buf + 1, Chris@10: rio + ms * mb, iio + ms * mb, Chris@10: me-mb, 2 * BATCHDIST(ego->r), ms, Chris@10: ego->r, 2, ego->rs); Chris@10: } Chris@10: Chris@10: static void apply(const plan *ego_, R *rio, R *iio) Chris@10: { Chris@10: const P *ego = (const P *) ego_; Chris@10: R *buf = (R *) MALLOC(sizeof(R) * 2 * BATCHDIST(ego->r) * ego->batchsz, Chris@10: BUFFERS); Chris@10: INT m; Chris@10: Chris@10: for (m = ego->mb; m < ego->me; m += ego->batchsz) Chris@10: dobatch(ego, m, m + ego->batchsz, buf, rio, iio); Chris@10: Chris@10: A(m == ego->me); Chris@10: Chris@10: X(ifree)(buf); Chris@10: } Chris@10: Chris@10: static void awake(plan *ego_, enum wakefulness wakefulness) Chris@10: { Chris@10: P *ego = (P *) ego_; Chris@10: X(plan_awake)(ego->cld, wakefulness); Chris@10: Chris@10: switch (wakefulness) { Chris@10: case SLEEPY: Chris@10: X(triggen_destroy)(ego->t); ego->t = 0; Chris@10: break; Chris@10: default: Chris@10: ego->t = X(mktriggen)(AWAKE_SQRTN_TABLE, ego->r * ego->m); Chris@10: break; Chris@10: } Chris@10: } Chris@10: Chris@10: static void destroy(plan *ego_) Chris@10: { Chris@10: P *ego = (P *) ego_; Chris@10: X(plan_destroy_internal)(ego->cld); Chris@10: } Chris@10: Chris@10: static void print(const plan *ego_, printer *p) Chris@10: { Chris@10: const P *ego = (const P *) ego_; Chris@10: p->print(p, "(dftw-genericbuf/%D-%D-%D%(%p%))", Chris@10: ego->batchsz, ego->r, ego->m, ego->cld); Chris@10: } Chris@10: Chris@10: static plan *mkcldw(const ct_solver *ego_, Chris@10: INT r, INT irs, INT ors, Chris@10: INT m, INT ms, Chris@10: INT v, INT ivs, INT ovs, Chris@10: INT mstart, INT mcount, Chris@10: R *rio, R *iio, Chris@10: planner *plnr) Chris@10: { Chris@10: const S *ego = (const S *)ego_; Chris@10: P *pln; Chris@10: plan *cld = 0; Chris@10: R *buf; Chris@10: Chris@10: static const plan_adt padt = { Chris@10: 0, awake, print, destroy Chris@10: }; Chris@10: Chris@10: UNUSED(ivs); UNUSED(ovs); UNUSED(rio); UNUSED(iio); Chris@10: Chris@10: A(mstart >= 0 && mstart + mcount <= m); Chris@10: if (!applicable(ego, r, irs, ors, m, v, mcount, plnr)) Chris@10: return (plan *)0; Chris@10: Chris@10: buf = (R *) MALLOC(sizeof(R) * 2 * BATCHDIST(r) * ego->batchsz, BUFFERS); Chris@10: cld = X(mkplan_d)(plnr, Chris@10: X(mkproblem_dft_d)( Chris@10: X(mktensor_1d)(r, 2, 2), Chris@10: X(mktensor_1d)(ego->batchsz, Chris@10: 2 * BATCHDIST(r), Chris@10: 2 * BATCHDIST(r)), Chris@10: buf, buf + 1, buf, buf + 1 Chris@10: ) Chris@10: ); Chris@10: X(ifree)(buf); Chris@10: if (!cld) goto nada; Chris@10: Chris@10: pln = MKPLAN_DFTW(P, &padt, apply); Chris@10: pln->slv = ego; Chris@10: pln->cld = cld; Chris@10: pln->r = r; Chris@10: pln->m = m; Chris@10: pln->ms = ms; Chris@10: pln->rs = irs; Chris@10: pln->batchsz = ego->batchsz; Chris@10: pln->mb = mstart; Chris@10: pln->me = mstart + mcount; Chris@10: Chris@10: { Chris@10: double n0 = (r - 1) * (mcount - 1); Chris@10: pln->super.super.ops = cld->ops; Chris@10: pln->super.super.ops.mul += 8 * n0; Chris@10: pln->super.super.ops.add += 4 * n0; Chris@10: pln->super.super.ops.other += 8 * n0; Chris@10: } Chris@10: return &(pln->super.super); Chris@10: Chris@10: nada: Chris@10: X(plan_destroy_internal)(cld); Chris@10: return (plan *) 0; Chris@10: } Chris@10: Chris@10: static void regsolver(planner *plnr, INT r, INT batchsz) Chris@10: { Chris@10: S *slv = (S *)X(mksolver_ct)(sizeof(S), r, DECDIT, mkcldw, 0); Chris@10: slv->batchsz = batchsz; Chris@10: REGISTER_SOLVER(plnr, &(slv->super.super)); Chris@10: Chris@10: if (X(mksolver_ct_hook)) { Chris@10: slv = (S *)X(mksolver_ct_hook)(sizeof(S), r, DECDIT, mkcldw, 0); Chris@10: slv->batchsz = batchsz; Chris@10: REGISTER_SOLVER(plnr, &(slv->super.super)); Chris@10: } Chris@10: Chris@10: } Chris@10: Chris@10: void X(ct_genericbuf_register)(planner *p) Chris@10: { Chris@10: static const INT radices[] = { -1, -2, -4, -8, -16, -32, -64 }; Chris@10: static const INT batchsizes[] = { 4, 8, 16, 32, 64 }; Chris@10: unsigned i, j; Chris@10: Chris@10: for (i = 0; i < sizeof(radices) / sizeof(radices[0]); ++i) Chris@10: for (j = 0; j < sizeof(batchsizes) / sizeof(batchsizes[0]); ++j) Chris@10: regsolver(p, radices[i], batchsizes[j]); Chris@10: }