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