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