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