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: /* plans for RDFT of rank >= 2 (multidimensional) */ Chris@42: Chris@42: /* FIXME: this solver cannot strictly be applied to multidimensional Chris@42: DHTs, since the latter are not separable...up to rnk-1 additional Chris@42: post-processing passes may be required. See also: Chris@42: Chris@42: R. N. Bracewell, O. Buneman, H. Hao, and J. Villasenor, "Fast Chris@42: two-dimensional Hartley transform," Proc. IEEE 74, 1282-1283 (1986). Chris@42: Chris@42: H. Hao and R. N. Bracewell, "A three-dimensional DFT algorithm Chris@42: using the fast Hartley transform," Proc. IEEE 75(2), 264-266 (1987). Chris@42: */ Chris@42: Chris@42: #include "rdft.h" Chris@42: Chris@42: typedef struct { Chris@42: solver super; Chris@42: int spltrnk; Chris@42: const int *buddies; Chris@42: size_t nbuddies; Chris@42: } S; Chris@42: Chris@42: typedef struct { Chris@42: plan_rdft super; Chris@42: Chris@42: plan *cld1, *cld2; Chris@42: const S *solver; Chris@42: } P; Chris@42: Chris@42: /* Compute multi-dimensional RDFT by applying the two cld plans Chris@42: (lower-rnk RDFTs). */ Chris@42: static void apply(const plan *ego_, R *I, R *O) Chris@42: { Chris@42: const P *ego = (const P *) ego_; Chris@42: plan_rdft *cld1, *cld2; Chris@42: Chris@42: cld1 = (plan_rdft *) ego->cld1; Chris@42: cld1->apply(ego->cld1, I, O); Chris@42: Chris@42: cld2 = (plan_rdft *) ego->cld2; Chris@42: cld2->apply(ego->cld2, O, O); 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: X(plan_awake)(ego->cld1, wakefulness); Chris@42: X(plan_awake)(ego->cld2, 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->cld2); Chris@42: X(plan_destroy_internal)(ego->cld1); 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: const S *s = ego->solver; Chris@42: p->print(p, "(rdft-rank>=2/%d%(%p%)%(%p%))", Chris@42: s->spltrnk, ego->cld1, ego->cld2); Chris@42: } Chris@42: Chris@42: static int picksplit(const S *ego, const tensor *sz, int *rp) Chris@42: { Chris@42: A(sz->rnk > 1); /* cannot split rnk <= 1 */ Chris@42: if (!X(pickdim)(ego->spltrnk, ego->buddies, ego->nbuddies, sz, 1, rp)) Chris@42: return 0; Chris@42: *rp += 1; /* convert from dim. index to rank */ Chris@42: if (*rp >= sz->rnk) /* split must reduce rank */ Chris@42: return 0; Chris@42: return 1; Chris@42: } Chris@42: Chris@42: static int applicable0(const solver *ego_, const problem *p_, int *rp) Chris@42: { Chris@42: const problem_rdft *p = (const problem_rdft *) p_; Chris@42: const S *ego = (const S *)ego_; Chris@42: return (1 Chris@42: && FINITE_RNK(p->sz->rnk) && FINITE_RNK(p->vecsz->rnk) Chris@42: && p->sz->rnk >= 2 Chris@42: && picksplit(ego, p->sz, rp) Chris@42: ); Chris@42: } Chris@42: Chris@42: /* TODO: revise this. */ Chris@42: static int applicable(const solver *ego_, const problem *p_, Chris@42: const planner *plnr, int *rp) Chris@42: { Chris@42: const S *ego = (const S *)ego_; Chris@42: Chris@42: if (!applicable0(ego_, p_, rp)) return 0; Chris@42: Chris@42: if (NO_RANK_SPLITSP(plnr) && (ego->spltrnk != ego->buddies[0])) Chris@42: return 0; Chris@42: Chris@42: if (NO_UGLYP(plnr)) { Chris@42: /* Heuristic: if the vector stride is greater than the transform Chris@42: sz, don't use (prefer to do the vector loop first with a Chris@42: vrank-geq1 plan). */ Chris@42: const problem_rdft *p = (const problem_rdft *) p_; Chris@42: Chris@42: if (p->vecsz->rnk > 0 && Chris@42: X(tensor_min_stride)(p->vecsz) > X(tensor_max_index)(p->sz)) 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: const S *ego = (const S *) ego_; Chris@42: const problem_rdft *p; Chris@42: P *pln; Chris@42: plan *cld1 = 0, *cld2 = 0; Chris@42: tensor *sz1, *sz2, *vecszi, *sz2i; Chris@42: int spltrnk; Chris@42: Chris@42: static const plan_adt padt = { Chris@42: X(rdft_solve), awake, print, destroy Chris@42: }; Chris@42: Chris@42: if (!applicable(ego_, p_, plnr, &spltrnk)) Chris@42: return (plan *) 0; Chris@42: Chris@42: p = (const problem_rdft *) p_; Chris@42: X(tensor_split)(p->sz, &sz1, spltrnk, &sz2); Chris@42: vecszi = X(tensor_copy_inplace)(p->vecsz, INPLACE_OS); Chris@42: sz2i = X(tensor_copy_inplace)(sz2, INPLACE_OS); Chris@42: Chris@42: cld1 = X(mkplan_d)(plnr, Chris@42: X(mkproblem_rdft_d)(X(tensor_copy)(sz2), Chris@42: X(tensor_append)(p->vecsz, sz1), Chris@42: p->I, p->O, p->kind + spltrnk)); Chris@42: if (!cld1) goto nada; Chris@42: Chris@42: cld2 = X(mkplan_d)(plnr, Chris@42: X(mkproblem_rdft_d)( Chris@42: X(tensor_copy_inplace)(sz1, INPLACE_OS), Chris@42: X(tensor_append)(vecszi, sz2i), Chris@42: p->O, p->O, p->kind)); Chris@42: if (!cld2) goto nada; Chris@42: Chris@42: pln = MKPLAN_RDFT(P, &padt, apply); Chris@42: Chris@42: pln->cld1 = cld1; Chris@42: pln->cld2 = cld2; Chris@42: Chris@42: pln->solver = ego; Chris@42: X(ops_add)(&cld1->ops, &cld2->ops, &pln->super.super.ops); Chris@42: Chris@42: X(tensor_destroy4)(sz2, sz1, vecszi, sz2i); Chris@42: Chris@42: return &(pln->super.super); Chris@42: Chris@42: nada: Chris@42: X(plan_destroy_internal)(cld2); Chris@42: X(plan_destroy_internal)(cld1); Chris@42: X(tensor_destroy4)(sz2, sz1, vecszi, sz2i); Chris@42: return (plan *) 0; Chris@42: } Chris@42: Chris@42: static solver *mksolver(int spltrnk, const int *buddies, size_t nbuddies) Chris@42: { Chris@42: static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; Chris@42: S *slv = MKSOLVER(S, &sadt); Chris@42: slv->spltrnk = spltrnk; Chris@42: slv->buddies = buddies; Chris@42: slv->nbuddies = nbuddies; Chris@42: return &(slv->super); Chris@42: } Chris@42: Chris@42: void X(rdft_rank_geq2_register)(planner *p) Chris@42: { Chris@42: static const int buddies[] = { 1, 0, -2 }; Chris@42: size_t i; Chris@42: Chris@42: for (i = 0; i < NELEM(buddies); ++i) Chris@42: REGISTER_SOLVER(p, mksolver(buddies[i], buddies, NELEM(buddies))); Chris@42: Chris@42: /* FIXME: Should we try more buddies? See also dft/rank-geq2. */ Chris@42: }