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: Chris@42: /* buffering of rdft2. We always buffer the complex array */ Chris@42: Chris@42: #include "rdft.h" Chris@42: #include "dft.h" Chris@42: Chris@42: typedef struct { Chris@42: solver super; Chris@42: size_t maxnbuf_ndx; Chris@42: } S; Chris@42: Chris@42: static const INT maxnbufs[] = { 8, 256 }; Chris@42: Chris@42: typedef struct { Chris@42: plan_rdft2 super; Chris@42: Chris@42: plan *cld, *cldcpy, *cldrest; Chris@42: INT n, vl, nbuf, bufdist; Chris@42: INT ivs_by_nbuf, ovs_by_nbuf; Chris@42: INT ioffset, roffset; Chris@42: } P; Chris@42: Chris@42: /* transform a vector input with the help of bufs */ Chris@42: static void apply_r2hc(const plan *ego_, R *r0, R *r1, R *cr, R *ci) Chris@42: { Chris@42: const P *ego = (const P *) ego_; Chris@42: plan_rdft2 *cld = (plan_rdft2 *) ego->cld; Chris@42: plan_dft *cldcpy = (plan_dft *) ego->cldcpy; Chris@42: INT i, vl = ego->vl, nbuf = ego->nbuf; Chris@42: INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf; Chris@42: R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS); Chris@42: R *bufr = bufs + ego->roffset; Chris@42: R *bufi = bufs + ego->ioffset; Chris@42: plan_rdft2 *cldrest; Chris@42: Chris@42: for (i = nbuf; i <= vl; i += nbuf) { Chris@42: /* transform to bufs: */ Chris@42: cld->apply((plan *) cld, r0, r1, bufr, bufi); Chris@42: r0 += ivs_by_nbuf; r1 += ivs_by_nbuf; Chris@42: Chris@42: /* copy back */ Chris@42: cldcpy->apply((plan *) cldcpy, bufr, bufi, cr, ci); Chris@42: cr += ovs_by_nbuf; ci += ovs_by_nbuf; Chris@42: } Chris@42: Chris@42: X(ifree)(bufs); Chris@42: Chris@42: /* Do the remaining transforms, if any: */ Chris@42: cldrest = (plan_rdft2 *) ego->cldrest; Chris@42: cldrest->apply((plan *) cldrest, r0, r1, cr, ci); Chris@42: } Chris@42: Chris@42: /* for hc2r problems, copy the input into buffer, and then Chris@42: transform buffer->output, which allows for destruction of the Chris@42: buffer */ Chris@42: static void apply_hc2r(const plan *ego_, R *r0, R *r1, R *cr, R *ci) Chris@42: { Chris@42: const P *ego = (const P *) ego_; Chris@42: plan_rdft2 *cld = (plan_rdft2 *) ego->cld; Chris@42: plan_dft *cldcpy = (plan_dft *) ego->cldcpy; Chris@42: INT i, vl = ego->vl, nbuf = ego->nbuf; Chris@42: INT ivs_by_nbuf = ego->ivs_by_nbuf, ovs_by_nbuf = ego->ovs_by_nbuf; Chris@42: R *bufs = (R *)MALLOC(sizeof(R) * nbuf * ego->bufdist, BUFFERS); Chris@42: R *bufr = bufs + ego->roffset; Chris@42: R *bufi = bufs + ego->ioffset; Chris@42: plan_rdft2 *cldrest; Chris@42: Chris@42: for (i = nbuf; i <= vl; i += nbuf) { Chris@42: /* copy input into bufs: */ Chris@42: cldcpy->apply((plan *) cldcpy, cr, ci, bufr, bufi); Chris@42: cr += ivs_by_nbuf; ci += ivs_by_nbuf; Chris@42: Chris@42: /* transform to output */ Chris@42: cld->apply((plan *) cld, r0, r1, bufr, bufi); Chris@42: r0 += ovs_by_nbuf; r1 += ovs_by_nbuf; Chris@42: } Chris@42: Chris@42: X(ifree)(bufs); Chris@42: Chris@42: /* Do the remaining transforms, if any: */ Chris@42: cldrest = (plan_rdft2 *) ego->cldrest; Chris@42: cldrest->apply((plan *) cldrest, r0, r1, cr, ci); Chris@42: } Chris@42: Chris@42: Chris@42: static void awake(plan *ego_, enum wakefulness wakefulness) Chris@42: { Chris@42: P *ego = (P *) ego_; Chris@42: Chris@42: X(plan_awake)(ego->cld, wakefulness); Chris@42: X(plan_awake)(ego->cldcpy, wakefulness); Chris@42: X(plan_awake)(ego->cldrest, wakefulness); 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->cldrest); Chris@42: X(plan_destroy_internal)(ego->cldcpy); 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, "(rdft2-buffered-%D%v/%D-%D%(%p%)%(%p%)%(%p%))", Chris@42: ego->n, ego->nbuf, Chris@42: ego->vl, ego->bufdist % ego->n, Chris@42: ego->cld, ego->cldcpy, ego->cldrest); Chris@42: } Chris@42: Chris@42: static int applicable0(const S *ego, const problem *p_, const planner *plnr) Chris@42: { Chris@42: const problem_rdft2 *p = (const problem_rdft2 *) p_; Chris@42: iodim *d = p->sz->dims; Chris@42: Chris@42: if (1 Chris@42: && p->vecsz->rnk <= 1 Chris@42: && p->sz->rnk == 1 Chris@42: Chris@42: /* we assume even n throughout */ Chris@42: && (d[0].n % 2) == 0 Chris@42: Chris@42: /* and we only consider these two cases */ Chris@42: && (p->kind == R2HC || p->kind == HC2R) Chris@42: Chris@42: ) { Chris@42: INT vl, ivs, ovs; Chris@42: X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); Chris@42: Chris@42: if (X(toobig)(d[0].n) && CONSERVE_MEMORYP(plnr)) Chris@42: return 0; Chris@42: Chris@42: /* if this solver is redundant, in the sense that a solver Chris@42: of lower index generates the same plan, then prune this Chris@42: solver */ Chris@42: if (X(nbuf_redundant)(d[0].n, vl, Chris@42: ego->maxnbuf_ndx, Chris@42: maxnbufs, NELEM(maxnbufs))) Chris@42: return 0; Chris@42: Chris@42: if (p->r0 != p->cr) { Chris@42: if (p->kind == HC2R) { Chris@42: /* Allow HC2R problems only if the input is to be Chris@42: preserved. This solver sets NO_DESTROY_INPUT, Chris@42: which prevents infinite loops */ Chris@42: return (NO_DESTROY_INPUTP(plnr)); Chris@42: } else { Chris@42: /* Chris@42: In principle, the buffered transforms might be useful Chris@42: when working out of place. However, in order to Chris@42: prevent infinite loops in the planner, we require Chris@42: that the output stride of the buffered transforms be Chris@42: greater than 2. Chris@42: */ Chris@42: return (d[0].os > 2); Chris@42: } Chris@42: } Chris@42: Chris@42: /* Chris@42: * If the problem is in place, the input/output strides must Chris@42: * be the same or the whole thing must fit in the buffer. Chris@42: */ Chris@42: if (X(rdft2_inplace_strides(p, RNK_MINFTY))) Chris@42: return 1; Chris@42: Chris@42: if (/* fits into buffer: */ Chris@42: ((p->vecsz->rnk == 0) Chris@42: || Chris@42: (X(nbuf)(d[0].n, p->vecsz->dims[0].n, Chris@42: maxnbufs[ego->maxnbuf_ndx]) Chris@42: == p->vecsz->dims[0].n))) Chris@42: return 1; Chris@42: } Chris@42: Chris@42: return 0; Chris@42: } Chris@42: Chris@42: static int applicable(const S *ego, const problem *p_, const planner *plnr) Chris@42: { Chris@42: const problem_rdft2 *p; Chris@42: Chris@42: if (NO_BUFFERINGP(plnr)) return 0; Chris@42: Chris@42: if (!applicable0(ego, p_, plnr)) return 0; Chris@42: Chris@42: p = (const problem_rdft2 *) p_; Chris@42: if (p->kind == HC2R) { Chris@42: if (NO_UGLYP(plnr)) { Chris@42: /* UGLY if in-place and too big, since the problem Chris@42: could be solved via transpositions */ Chris@42: if (p->r0 == p->cr && X(toobig)(p->sz->dims[0].n)) Chris@42: return 0; Chris@42: } Chris@42: } else { Chris@42: if (NO_UGLYP(plnr)) { Chris@42: if (p->r0 != p->cr || X(toobig)(p->sz->dims[0].n)) Chris@42: return 0; Chris@42: } Chris@42: } Chris@42: return 1; Chris@42: } Chris@42: Chris@42: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) Chris@42: { Chris@42: P *pln; Chris@42: const S *ego = (const S *)ego_; Chris@42: plan *cld = (plan *) 0; Chris@42: plan *cldcpy = (plan *) 0; Chris@42: plan *cldrest = (plan *) 0; Chris@42: const problem_rdft2 *p = (const problem_rdft2 *) p_; Chris@42: R *bufs = (R *) 0; Chris@42: INT nbuf = 0, bufdist, n, vl; Chris@42: INT ivs, ovs, ioffset, roffset, id, od; Chris@42: Chris@42: static const plan_adt padt = { Chris@42: X(rdft2_solve), awake, print, destroy Chris@42: }; Chris@42: Chris@42: if (!applicable(ego, p_, plnr)) Chris@42: goto nada; Chris@42: Chris@42: n = X(tensor_sz)(p->sz); Chris@42: X(tensor_tornk1)(p->vecsz, &vl, &ivs, &ovs); Chris@42: Chris@42: nbuf = X(nbuf)(n, vl, maxnbufs[ego->maxnbuf_ndx]); Chris@42: bufdist = X(bufdist)(n + 2, vl); /* complex-side rdft2 stores N+2 Chris@42: real numbers */ Chris@42: A(nbuf > 0); Chris@42: Chris@42: /* attempt to keep real and imaginary part in the same order, Chris@42: so as to allow optimizations in the the copy plan */ Chris@42: roffset = (p->cr - p->ci > 0) ? (INT)1 : (INT)0; Chris@42: ioffset = 1 - roffset; Chris@42: Chris@42: /* initial allocation for the purpose of planning */ Chris@42: bufs = (R *) MALLOC(sizeof(R) * nbuf * bufdist, BUFFERS); Chris@42: Chris@42: id = ivs * (nbuf * (vl / nbuf)); Chris@42: od = ovs * (nbuf * (vl / nbuf)); Chris@42: Chris@42: if (p->kind == R2HC) { Chris@42: /* allow destruction of input if problem is in place */ Chris@42: cld = X(mkplan_f_d)( Chris@42: plnr, Chris@42: X(mkproblem_rdft2_d)( Chris@42: X(mktensor_1d)(n, p->sz->dims[0].is, 2), Chris@42: X(mktensor_1d)(nbuf, ivs, bufdist), Chris@42: TAINT(p->r0, ivs * nbuf), TAINT(p->r1, ivs * nbuf), Chris@42: bufs + roffset, bufs + ioffset, p->kind), Chris@42: 0, 0, (p->r0 == p->cr) ? NO_DESTROY_INPUT : 0); Chris@42: if (!cld) goto nada; Chris@42: Chris@42: /* copying back from the buffer is a rank-0 DFT: */ Chris@42: cldcpy = X(mkplan_d)( Chris@42: plnr, Chris@42: X(mkproblem_dft_d)( Chris@42: X(mktensor_0d)(), Chris@42: X(mktensor_2d)(nbuf, bufdist, ovs, Chris@42: n/2+1, 2, p->sz->dims[0].os), Chris@42: bufs + roffset, bufs + ioffset, Chris@42: TAINT(p->cr, ovs * nbuf), TAINT(p->ci, ovs * nbuf) )); Chris@42: if (!cldcpy) goto nada; Chris@42: Chris@42: X(ifree)(bufs); bufs = 0; Chris@42: Chris@42: cldrest = X(mkplan_d)(plnr, Chris@42: X(mkproblem_rdft2_d)( Chris@42: X(tensor_copy)(p->sz), Chris@42: X(mktensor_1d)(vl % nbuf, ivs, ovs), Chris@42: p->r0 + id, p->r1 + id, Chris@42: p->cr + od, p->ci + od, Chris@42: p->kind)); Chris@42: if (!cldrest) goto nada; Chris@42: pln = MKPLAN_RDFT2(P, &padt, apply_r2hc); Chris@42: } else { Chris@42: /* allow destruction of buffer */ Chris@42: cld = X(mkplan_f_d)( Chris@42: plnr, Chris@42: X(mkproblem_rdft2_d)( Chris@42: X(mktensor_1d)(n, 2, p->sz->dims[0].os), Chris@42: X(mktensor_1d)(nbuf, bufdist, ovs), Chris@42: TAINT(p->r0, ovs * nbuf), TAINT(p->r1, ovs * nbuf), Chris@42: bufs + roffset, bufs + ioffset, p->kind), Chris@42: 0, 0, NO_DESTROY_INPUT); Chris@42: if (!cld) goto nada; Chris@42: Chris@42: /* copying input into buffer is a rank-0 DFT: */ Chris@42: cldcpy = X(mkplan_d)( Chris@42: plnr, Chris@42: X(mkproblem_dft_d)( Chris@42: X(mktensor_0d)(), Chris@42: X(mktensor_2d)(nbuf, ivs, bufdist, Chris@42: n/2+1, p->sz->dims[0].is, 2), Chris@42: TAINT(p->cr, ivs * nbuf), TAINT(p->ci, ivs * nbuf), Chris@42: bufs + roffset, bufs + ioffset)); Chris@42: if (!cldcpy) goto nada; Chris@42: Chris@42: X(ifree)(bufs); bufs = 0; Chris@42: Chris@42: cldrest = X(mkplan_d)(plnr, Chris@42: X(mkproblem_rdft2_d)( Chris@42: X(tensor_copy)(p->sz), Chris@42: X(mktensor_1d)(vl % nbuf, ivs, ovs), Chris@42: p->r0 + od, p->r1 + od, Chris@42: p->cr + id, p->ci + id, Chris@42: p->kind)); Chris@42: if (!cldrest) goto nada; Chris@42: Chris@42: pln = MKPLAN_RDFT2(P, &padt, apply_hc2r); Chris@42: } Chris@42: Chris@42: pln->cld = cld; Chris@42: pln->cldcpy = cldcpy; Chris@42: pln->cldrest = cldrest; Chris@42: pln->n = n; Chris@42: pln->vl = vl; Chris@42: pln->ivs_by_nbuf = ivs * nbuf; Chris@42: pln->ovs_by_nbuf = ovs * nbuf; Chris@42: pln->roffset = roffset; Chris@42: pln->ioffset = ioffset; Chris@42: Chris@42: pln->nbuf = nbuf; Chris@42: pln->bufdist = bufdist; Chris@42: Chris@42: { Chris@42: opcnt t; Chris@42: X(ops_add)(&cld->ops, &cldcpy->ops, &t); Chris@42: X(ops_madd)(vl / nbuf, &t, &cldrest->ops, &pln->super.super.ops); Chris@42: } Chris@42: Chris@42: return &(pln->super.super); Chris@42: Chris@42: nada: Chris@42: X(ifree0)(bufs); Chris@42: X(plan_destroy_internal)(cldrest); Chris@42: X(plan_destroy_internal)(cldcpy); Chris@42: X(plan_destroy_internal)(cld); Chris@42: return (plan *) 0; Chris@42: } Chris@42: Chris@42: static solver *mksolver(size_t maxnbuf_ndx) Chris@42: { Chris@42: static const solver_adt sadt = { PROBLEM_RDFT2, mkplan, 0 }; Chris@42: S *slv = MKSOLVER(S, &sadt); Chris@42: slv->maxnbuf_ndx = maxnbuf_ndx; Chris@42: return &(slv->super); Chris@42: } Chris@42: Chris@42: void X(rdft2_buffered_register)(planner *p) Chris@42: { Chris@42: size_t i; Chris@42: for (i = 0; i < NELEM(maxnbufs); ++i) Chris@42: REGISTER_SOLVER(p, mksolver(i)); Chris@42: }