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: Chris@10: #include "rdft.h" Chris@10: Chris@10: typedef struct { Chris@10: solver super; Chris@10: int maxnbuf_ndx; Chris@10: } S; Chris@10: Chris@10: static const INT maxnbufs[] = { 8, 256 }; Chris@10: Chris@10: typedef struct { Chris@10: plan_rdft super; Chris@10: Chris@10: plan *cld, *cldcpy, *cldrest; Chris@10: INT n, vl, nbuf, bufdist; Chris@10: INT ivs_by_nbuf, ovs_by_nbuf; Chris@10: } P; Chris@10: Chris@10: /* transform a vector input with the help of bufs */ Chris@10: static void apply(const plan *ego_, R *I, R *O) Chris@10: { Chris@10: const P *ego = (const P *) ego_; Chris@10: plan_rdft *cld = (plan_rdft *) ego->cld; Chris@10: plan_rdft *cldcpy = (plan_rdft *) ego->cldcpy; Chris@10: plan_rdft *cldrest; Chris@10: INT i, vl = ego->vl, nbuf = ego->nbuf; Chris@10: INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf; Chris@10: R *bufs; Chris@10: Chris@10: bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS); Chris@10: Chris@10: for (i = nbuf; i <= vl; i += nbuf) { Chris@10: /* transform to bufs: */ Chris@10: cld->apply((plan *) cld, I, bufs); Chris@10: I += ivs_by_nbuf; Chris@10: Chris@10: /* copy back */ Chris@10: cldcpy->apply((plan *) cldcpy, bufs, O); Chris@10: O += ovs_by_nbuf; Chris@10: } Chris@10: Chris@10: X(ifree)(bufs); Chris@10: Chris@10: /* Do the remaining transforms, if any: */ Chris@10: cldrest = (plan_rdft *) ego->cldrest; Chris@10: cldrest->apply((plan *) cldrest, I, O); Chris@10: } Chris@10: Chris@10: /* for hc2r problems, copy the input into buffer, and then Chris@10: transform buffer->output, which allows for destruction of the Chris@10: buffer */ Chris@10: static void apply_hc2r(const plan *ego_, R *I, R *O) Chris@10: { Chris@10: const P *ego = (const P *) ego_; Chris@10: plan_rdft *cld = (plan_rdft *) ego->cld; Chris@10: plan_rdft *cldcpy = (plan_rdft *) ego->cldcpy; Chris@10: plan_rdft *cldrest; Chris@10: INT i, vl = ego->vl, nbuf = ego->nbuf; Chris@10: INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf; Chris@10: R *bufs; Chris@10: Chris@10: bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS); Chris@10: Chris@10: for (i = nbuf; i <= vl; i += nbuf) { Chris@10: /* copy input into bufs: */ Chris@10: cldcpy->apply((plan *) cldcpy, I, bufs); Chris@10: I += ivs_by_nbuf; Chris@10: Chris@10: /* transform to output */ Chris@10: cld->apply((plan *) cld, bufs, O); Chris@10: O += ovs_by_nbuf; Chris@10: } Chris@10: Chris@10: X(ifree)(bufs); Chris@10: Chris@10: /* Do the remaining transforms, if any: */ Chris@10: cldrest = (plan_rdft *) ego->cldrest; Chris@10: cldrest->apply((plan *) cldrest, I, O); Chris@10: } Chris@10: Chris@10: Chris@10: static void awake(plan *ego_, enum wakefulness wakefulness) Chris@10: { Chris@10: P *ego = (P *) ego_; Chris@10: Chris@10: X(plan_awake)(ego->cld, wakefulness); Chris@10: X(plan_awake)(ego->cldcpy, wakefulness); Chris@10: X(plan_awake)(ego->cldrest, wakefulness); 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->cldrest); Chris@10: X(plan_destroy_internal)(ego->cldcpy); 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, "(rdft-buffered-%D%v/%D-%D%(%p%)%(%p%)%(%p%))", Chris@10: ego->n, ego->nbuf, Chris@10: ego->vl, ego->bufdist % ego->n, Chris@10: ego->cld, ego->cldcpy, ego->cldrest); Chris@10: } Chris@10: Chris@10: static int applicable0(const S *ego, const problem *p_, const planner *plnr) Chris@10: { Chris@10: const problem_rdft *p = (const problem_rdft *) p_; Chris@10: iodim *d = p->sz->dims; Chris@10: Chris@10: if (1 Chris@10: && p->vecsz->rnk <= 1 Chris@10: && p->sz->rnk == 1 Chris@10: ) { Chris@10: INT vl, ivs, ovs; Chris@10: X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); Chris@10: Chris@10: if (X(toobig)(d[0].n) && CONSERVE_MEMORYP(plnr)) Chris@10: return 0; Chris@10: Chris@10: /* if this solver is redundant, in the sense that a solver Chris@10: of lower index generates the same plan, then prune this Chris@10: solver */ Chris@10: if (X(nbuf_redundant)(d[0].n, vl, Chris@10: ego->maxnbuf_ndx, Chris@10: maxnbufs, NELEM(maxnbufs))) Chris@10: return 0; Chris@10: Chris@10: if (p->I != p->O) { Chris@10: if (p->kind[0] == HC2R) { Chris@10: /* Allow HC2R problems only if the input is to be Chris@10: preserved. This solver sets NO_DESTROY_INPUT, Chris@10: which prevents infinite loops */ Chris@10: return (NO_DESTROY_INPUTP(plnr)); Chris@10: } else { Chris@10: /* Chris@10: In principle, the buffered transforms might be useful Chris@10: when working out of place. However, in order to Chris@10: prevent infinite loops in the planner, we require Chris@10: that the output stride of the buffered transforms be Chris@10: greater than 1. Chris@10: */ Chris@10: return (d[0].os > 1); Chris@10: } Chris@10: } Chris@10: Chris@10: /* Chris@10: * If the problem is in place, the input/output strides must Chris@10: * be the same or the whole thing must fit in the buffer. Chris@10: */ Chris@10: if (X(tensor_inplace_strides2)(p->sz, p->vecsz)) Chris@10: return 1; Chris@10: Chris@10: if (/* fits into buffer: */ Chris@10: ((p->vecsz->rnk == 0) Chris@10: || Chris@10: (X(nbuf)(d[0].n, p->vecsz->dims[0].n, Chris@10: maxnbufs[ego->maxnbuf_ndx]) Chris@10: == p->vecsz->dims[0].n))) Chris@10: return 1; Chris@10: } Chris@10: Chris@10: return 0; Chris@10: } Chris@10: Chris@10: static int applicable(const S *ego, const problem *p_, const planner *plnr) Chris@10: { Chris@10: const problem_rdft *p; Chris@10: Chris@10: if (NO_BUFFERINGP(plnr)) return 0; Chris@10: Chris@10: if (!applicable0(ego, p_, plnr)) return 0; Chris@10: Chris@10: p = (const problem_rdft *) p_; Chris@10: if (p->kind[0] == HC2R) { Chris@10: if (NO_UGLYP(plnr)) { Chris@10: /* UGLY if in-place and too big, since the problem Chris@10: could be solved via transpositions */ Chris@10: if (p->I == p->O && X(toobig)(p->sz->dims[0].n)) Chris@10: return 0; Chris@10: } Chris@10: } else { Chris@10: if (NO_UGLYP(plnr)) { Chris@10: if (p->I != p->O) return 0; Chris@10: if (X(toobig)(p->sz->dims[0].n)) return 0; Chris@10: } Chris@10: } Chris@10: return 1; Chris@10: } Chris@10: Chris@10: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) Chris@10: { Chris@10: P *pln; Chris@10: const S *ego = (const S *)ego_; Chris@10: plan *cld = (plan *) 0; Chris@10: plan *cldcpy = (plan *) 0; Chris@10: plan *cldrest = (plan *) 0; Chris@10: const problem_rdft *p = (const problem_rdft *) p_; Chris@10: R *bufs = (R *) 0; Chris@10: INT nbuf = 0, bufdist, n, vl; Chris@10: INT ivs, ovs; Chris@10: int hc2rp; Chris@10: Chris@10: static const plan_adt padt = { Chris@10: X(rdft_solve), awake, print, destroy Chris@10: }; Chris@10: Chris@10: if (!applicable(ego, p_, plnr)) Chris@10: goto nada; Chris@10: Chris@10: n = X(tensor_sz)(p->sz); Chris@10: X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); Chris@10: hc2rp = (p->kind[0] == HC2R); Chris@10: Chris@10: nbuf = X(nbuf)(n, vl, maxnbufs[ego->maxnbuf_ndx]); Chris@10: bufdist = X(bufdist)(n, vl); Chris@10: A(nbuf > 0); Chris@10: Chris@10: /* initial allocation for the purpose of planning */ Chris@10: bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS); Chris@10: Chris@10: if (hc2rp) { Chris@10: /* allow destruction of buffer */ Chris@10: cld = X(mkplan_f_d)(plnr, Chris@10: X(mkproblem_rdft_d)( Chris@10: X(mktensor_1d)(n, 1, p->sz->dims[0].os), Chris@10: X(mktensor_1d)(nbuf, bufdist, ovs), Chris@10: bufs, TAINT(p->O, ovs * nbuf), p->kind), Chris@10: 0, 0, NO_DESTROY_INPUT); Chris@10: if (!cld) goto nada; Chris@10: Chris@10: /* copying input into buffer buffer is a rank-0 transform: */ Chris@10: cldcpy = X(mkplan_d)(plnr, Chris@10: X(mkproblem_rdft_0_d)( Chris@10: X(mktensor_2d)(nbuf, ivs, bufdist, Chris@10: n, p->sz->dims[0].is, 1), Chris@10: TAINT(p->I, ivs * nbuf), bufs)); Chris@10: if (!cldcpy) goto nada; Chris@10: } else { Chris@10: /* allow destruction of input if problem is in place */ Chris@10: cld = X(mkplan_f_d)(plnr, Chris@10: X(mkproblem_rdft_d)( Chris@10: X(mktensor_1d)(n, p->sz->dims[0].is, 1), Chris@10: X(mktensor_1d)(nbuf, ivs, bufdist), Chris@10: TAINT(p->I, ivs * nbuf), bufs, p->kind), Chris@10: 0, 0, (p->I == p->O) ? NO_DESTROY_INPUT : 0); Chris@10: if (!cld) goto nada; Chris@10: Chris@10: /* copying back from the buffer is a rank-0 transform: */ Chris@10: cldcpy = X(mkplan_d)(plnr, Chris@10: X(mkproblem_rdft_0_d)( Chris@10: X(mktensor_2d)(nbuf, bufdist, ovs, Chris@10: n, 1, p->sz->dims[0].os), Chris@10: bufs, TAINT(p->O, ovs * nbuf))); Chris@10: if (!cldcpy) goto nada; Chris@10: } Chris@10: Chris@10: /* deallocate buffers, let apply() allocate them for real */ Chris@10: X(ifree)(bufs); Chris@10: bufs = 0; Chris@10: Chris@10: /* plan the leftover transforms (cldrest): */ Chris@10: { Chris@10: INT id = ivs * (nbuf * (vl / nbuf)); Chris@10: INT od = ovs * (nbuf * (vl / nbuf)); Chris@10: cldrest = X(mkplan_d)(plnr, Chris@10: X(mkproblem_rdft_d)( Chris@10: X(tensor_copy)(p->sz), Chris@10: X(mktensor_1d)(vl % nbuf, ivs, ovs), Chris@10: p->I + id, p->O + od, p->kind)); Chris@10: } Chris@10: if (!cldrest) goto nada; Chris@10: Chris@10: pln = MKPLAN_RDFT(P, &padt, hc2rp ? apply_hc2r : apply); Chris@10: pln->cld = cld; Chris@10: pln->cldcpy = cldcpy; Chris@10: pln->cldrest = cldrest; Chris@10: pln->n = n; Chris@10: pln->vl = vl; Chris@10: pln->ivs_by_nbuf = ivs * nbuf; Chris@10: pln->ovs_by_nbuf = ovs * nbuf; Chris@10: Chris@10: pln->nbuf = nbuf; Chris@10: pln->bufdist = bufdist; Chris@10: Chris@10: { Chris@10: opcnt t; Chris@10: X(ops_add)(&cld->ops, &cldcpy->ops, &t); Chris@10: X(ops_madd)(vl / nbuf, &t, &cldrest->ops, &pln->super.super.ops); Chris@10: } Chris@10: Chris@10: return &(pln->super.super); Chris@10: Chris@10: nada: Chris@10: X(ifree0)(bufs); Chris@10: X(plan_destroy_internal)(cldrest); Chris@10: X(plan_destroy_internal)(cldcpy); Chris@10: X(plan_destroy_internal)(cld); Chris@10: return (plan *) 0; Chris@10: } Chris@10: Chris@10: static solver *mksolver(int maxnbuf_ndx) Chris@10: { Chris@10: static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; Chris@10: S *slv = MKSOLVER(S, &sadt); Chris@10: slv->maxnbuf_ndx = maxnbuf_ndx; Chris@10: return &(slv->super); Chris@10: } Chris@10: Chris@10: void X(rdft_buffered_register)(planner *p) Chris@10: { Chris@10: size_t i; Chris@10: for (i = 0; i < NELEM(maxnbufs); ++i) Chris@10: REGISTER_SOLVER(p, mksolver(i)); Chris@10: }