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: Chris@82: #include "dft/dft.h" Chris@82: Chris@82: typedef struct { Chris@82: solver super; Chris@82: size_t maxnbuf_ndx; Chris@82: } S; Chris@82: Chris@82: static const INT maxnbufs[] = { 8, 256 }; Chris@82: Chris@82: typedef struct { Chris@82: plan_dft super; Chris@82: Chris@82: plan *cld, *cldcpy, *cldrest; Chris@82: INT n, vl, nbuf, bufdist; Chris@82: INT ivs_by_nbuf, ovs_by_nbuf; Chris@82: INT roffset, ioffset; Chris@82: } P; Chris@82: Chris@82: /* transform a vector input with the help of bufs */ Chris@82: static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io) Chris@82: { Chris@82: const P *ego = (const P *) ego_; Chris@82: INT nbuf = ego->nbuf; Chris@82: R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist * 2, BUFFERS); Chris@82: Chris@82: plan_dft *cld = (plan_dft *) ego->cld; Chris@82: plan_dft *cldcpy = (plan_dft *) ego->cldcpy; Chris@82: plan_dft *cldrest; Chris@82: INT i, vl = ego->vl; Chris@82: INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf; Chris@82: INT roffset = ego->roffset, ioffset = ego->ioffset; Chris@82: Chris@82: for (i = nbuf; i <= vl; i += nbuf) { Chris@82: /* transform to bufs: */ Chris@82: cld->apply((plan *) cld, ri, ii, bufs + roffset, bufs + ioffset); Chris@82: ri += ivs_by_nbuf; ii += ivs_by_nbuf; Chris@82: Chris@82: /* copy back */ Chris@82: cldcpy->apply((plan *) cldcpy, bufs+roffset, bufs+ioffset, ro, io); Chris@82: ro += ovs_by_nbuf; io += ovs_by_nbuf; Chris@82: } Chris@82: Chris@82: X(ifree)(bufs); Chris@82: Chris@82: /* Do the remaining transforms, if any: */ Chris@82: cldrest = (plan_dft *) ego->cldrest; Chris@82: cldrest->apply((plan *) cldrest, ri, ii, ro, io); Chris@82: } Chris@82: Chris@82: Chris@82: static void awake(plan *ego_, enum wakefulness wakefulness) Chris@82: { Chris@82: P *ego = (P *) ego_; Chris@82: Chris@82: X(plan_awake)(ego->cld, wakefulness); Chris@82: X(plan_awake)(ego->cldcpy, wakefulness); Chris@82: X(plan_awake)(ego->cldrest, wakefulness); 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->cldrest); Chris@82: X(plan_destroy_internal)(ego->cldcpy); 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, "(dft-buffered-%D%v/%D-%D%(%p%)%(%p%)%(%p%))", Chris@82: ego->n, ego->nbuf, Chris@82: ego->vl, ego->bufdist % ego->n, Chris@82: ego->cld, ego->cldcpy, ego->cldrest); Chris@82: } Chris@82: Chris@82: static int applicable0(const S *ego, const problem *p_, const planner *plnr) Chris@82: { Chris@82: const problem_dft *p = (const problem_dft *) p_; Chris@82: const iodim *d = p->sz->dims; Chris@82: Chris@82: if (1 Chris@82: && p->vecsz->rnk <= 1 Chris@82: && p->sz->rnk == 1 Chris@82: ) { Chris@82: INT vl, ivs, ovs; Chris@82: X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); Chris@82: Chris@82: if (X(toobig)(p->sz->dims[0].n) && CONSERVE_MEMORYP(plnr)) Chris@82: return 0; Chris@82: Chris@82: /* if this solver is redundant, in the sense that a solver Chris@82: of lower index generates the same plan, then prune this Chris@82: solver */ Chris@82: if (X(nbuf_redundant)(d[0].n, vl, Chris@82: ego->maxnbuf_ndx, Chris@82: maxnbufs, NELEM(maxnbufs))) Chris@82: return 0; Chris@82: Chris@82: /* Chris@82: In principle, the buffered transforms might be useful Chris@82: when working out of place. However, in order to Chris@82: prevent infinite loops in the planner, we require Chris@82: that the output stride of the buffered transforms be Chris@82: greater than 2. Chris@82: */ Chris@82: if (p->ri != p->ro) Chris@82: return (d[0].os > 2); Chris@82: Chris@82: /* Chris@82: * If the problem is in place, the input/output strides must Chris@82: * be the same or the whole thing must fit in the buffer. Chris@82: */ Chris@82: if (X(tensor_inplace_strides2)(p->sz, p->vecsz)) Chris@82: return 1; Chris@82: Chris@82: if (/* fits into buffer: */ Chris@82: ((p->vecsz->rnk == 0) Chris@82: || Chris@82: (X(nbuf)(d[0].n, p->vecsz->dims[0].n, Chris@82: maxnbufs[ego->maxnbuf_ndx]) Chris@82: == p->vecsz->dims[0].n))) Chris@82: return 1; Chris@82: } Chris@82: Chris@82: return 0; Chris@82: } Chris@82: Chris@82: static int applicable(const S *ego, const problem *p_, const planner *plnr) Chris@82: { Chris@82: if (NO_BUFFERINGP(plnr)) return 0; Chris@82: if (!applicable0(ego, p_, plnr)) return 0; Chris@82: Chris@82: if (NO_UGLYP(plnr)) { Chris@82: const problem_dft *p = (const problem_dft *) p_; Chris@82: if (p->ri != p->ro) return 0; Chris@82: if (X(toobig)(p->sz->dims[0].n)) return 0; Chris@82: } Chris@82: return 1; Chris@82: } Chris@82: Chris@82: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) Chris@82: { Chris@82: P *pln; Chris@82: const S *ego = (const S *)ego_; Chris@82: plan *cld = (plan *) 0; Chris@82: plan *cldcpy = (plan *) 0; Chris@82: plan *cldrest = (plan *) 0; Chris@82: const problem_dft *p = (const problem_dft *) p_; Chris@82: R *bufs = (R *) 0; Chris@82: INT nbuf = 0, bufdist, n, vl; Chris@82: INT ivs, ovs, roffset, ioffset; Chris@82: Chris@82: static const plan_adt padt = { Chris@82: X(dft_solve), awake, print, destroy Chris@82: }; Chris@82: Chris@82: if (!applicable(ego, p_, plnr)) Chris@82: goto nada; Chris@82: Chris@82: n = X(tensor_sz)(p->sz); Chris@82: Chris@82: X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); Chris@82: Chris@82: nbuf = X(nbuf)(n, vl, maxnbufs[ego->maxnbuf_ndx]); Chris@82: bufdist = X(bufdist)(n, vl); Chris@82: A(nbuf > 0); Chris@82: Chris@82: /* attempt to keep real and imaginary part in the same order, Chris@82: so as to allow optimizations in the the copy plan */ Chris@82: roffset = (p->ri - p->ii > 0) ? (INT)1 : (INT)0; Chris@82: ioffset = 1 - roffset; Chris@82: Chris@82: /* initial allocation for the purpose of planning */ Chris@82: bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist * 2, BUFFERS); Chris@82: Chris@82: /* allow destruction of input if problem is in place */ Chris@82: cld = X(mkplan_f_d)(plnr, Chris@82: X(mkproblem_dft_d)( Chris@82: X(mktensor_1d)(n, p->sz->dims[0].is, 2), Chris@82: X(mktensor_1d)(nbuf, ivs, bufdist * 2), Chris@82: TAINT(p->ri, ivs * nbuf), Chris@82: TAINT(p->ii, ivs * nbuf), Chris@82: bufs + roffset, Chris@82: bufs + ioffset), Chris@82: 0, 0, (p->ri == p->ro) ? NO_DESTROY_INPUT : 0); Chris@82: if (!cld) Chris@82: goto nada; Chris@82: Chris@82: /* copying back from the buffer is a rank-0 transform: */ Chris@82: cldcpy = X(mkplan_d)(plnr, Chris@82: X(mkproblem_dft_d)( Chris@82: X(mktensor_0d)(), Chris@82: X(mktensor_2d)(nbuf, bufdist * 2, ovs, Chris@82: n, 2, p->sz->dims[0].os), Chris@82: bufs + roffset, Chris@82: bufs + ioffset, Chris@82: TAINT(p->ro, ovs * nbuf), Chris@82: TAINT(p->io, ovs * nbuf))); Chris@82: if (!cldcpy) Chris@82: goto nada; Chris@82: Chris@82: /* deallocate buffers, let apply() allocate them for real */ Chris@82: X(ifree)(bufs); Chris@82: bufs = 0; Chris@82: Chris@82: /* plan the leftover transforms (cldrest): */ Chris@82: { Chris@82: INT id = ivs * (nbuf * (vl / nbuf)); Chris@82: INT od = ovs * (nbuf * (vl / nbuf)); Chris@82: cldrest = X(mkplan_d)(plnr, Chris@82: X(mkproblem_dft_d)( Chris@82: X(tensor_copy)(p->sz), Chris@82: X(mktensor_1d)(vl % nbuf, ivs, ovs), Chris@82: p->ri+id, p->ii+id, p->ro+od, p->io+od)); Chris@82: } Chris@82: if (!cldrest) Chris@82: goto nada; Chris@82: Chris@82: pln = MKPLAN_DFT(P, &padt, apply); Chris@82: pln->cld = cld; Chris@82: pln->cldcpy = cldcpy; Chris@82: pln->cldrest = cldrest; Chris@82: pln->n = n; Chris@82: pln->vl = vl; Chris@82: pln->ivs_by_nbuf = ivs * nbuf; Chris@82: pln->ovs_by_nbuf = ovs * nbuf; Chris@82: pln->roffset = roffset; Chris@82: pln->ioffset = ioffset; Chris@82: Chris@82: pln->nbuf = nbuf; Chris@82: pln->bufdist = bufdist; Chris@82: Chris@82: { Chris@82: opcnt t; Chris@82: X(ops_add)(&cld->ops, &cldcpy->ops, &t); Chris@82: X(ops_madd)(vl / nbuf, &t, &cldrest->ops, &pln->super.super.ops); Chris@82: } Chris@82: Chris@82: return &(pln->super.super); Chris@82: Chris@82: nada: Chris@82: X(ifree0)(bufs); Chris@82: X(plan_destroy_internal)(cldrest); Chris@82: X(plan_destroy_internal)(cldcpy); Chris@82: X(plan_destroy_internal)(cld); Chris@82: return (plan *) 0; Chris@82: } Chris@82: Chris@82: static solver *mksolver(size_t maxnbuf_ndx) Chris@82: { Chris@82: static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 }; Chris@82: S *slv = MKSOLVER(S, &sadt); Chris@82: slv->maxnbuf_ndx = maxnbuf_ndx; Chris@82: return &(slv->super); Chris@82: } Chris@82: Chris@82: void X(dft_buffered_register)(planner *p) Chris@82: { Chris@82: size_t i; Chris@82: for (i = 0; i < NELEM(maxnbufs); ++i) Chris@82: REGISTER_SOLVER(p, mksolver(i)); Chris@82: }