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: #include "rdft.h" Chris@42: Chris@42: /* Chris@42: * Compute DHTs of prime sizes using Rader's trick: turn them Chris@42: * into convolutions of size n - 1, which we then perform via a pair Chris@42: * of FFTs. (We can then do prime real FFTs via rdft-dht.c.) Chris@42: * Chris@42: * Optionally (determined by the "pad" field of the solver), we can Chris@42: * perform the (cyclic) convolution by zero-padding to a size Chris@42: * >= 2*(n-1) - 1. This is advantageous if n-1 has large prime factors. Chris@42: * Chris@42: */ Chris@42: Chris@42: typedef struct { Chris@42: solver super; Chris@42: int pad; Chris@42: } S; Chris@42: Chris@42: typedef struct { Chris@42: plan_rdft super; Chris@42: Chris@42: plan *cld1, *cld2; Chris@42: R *omega; Chris@42: INT n, npad, g, ginv; Chris@42: INT is, os; Chris@42: plan *cld_omega; Chris@42: } P; Chris@42: Chris@42: static rader_tl *omegas = 0; Chris@42: Chris@42: /***************************************************************************/ Chris@42: Chris@42: /* If R2HC_ONLY_CONV is 1, we use a trick to perform the convolution Chris@42: purely in terms of R2HC transforms, as opposed to R2HC followed by H2RC. Chris@42: This requires a few more operations, but allows us to share the same Chris@42: plan/codelets for both Rader children. */ Chris@42: #define R2HC_ONLY_CONV 1 Chris@42: Chris@42: static void apply(const plan *ego_, R *I, R *O) Chris@42: { Chris@42: const P *ego = (const P *) ego_; Chris@42: INT n = ego->n; /* prime */ Chris@42: INT npad = ego->npad; /* == n - 1 for unpadded Rader; always even */ Chris@42: INT is = ego->is, os; Chris@42: INT k, gpower, g; Chris@42: R *buf, *omega; Chris@42: R r0; Chris@42: Chris@42: buf = (R *) MALLOC(sizeof(R) * npad, BUFFERS); Chris@42: Chris@42: /* First, permute the input, storing in buf: */ Chris@42: g = ego->g; Chris@42: for (gpower = 1, k = 0; k < n - 1; ++k, gpower = MULMOD(gpower, g, n)) { Chris@42: buf[k] = I[gpower * is]; Chris@42: } Chris@42: /* gpower == g^(n-1) mod n == 1 */; Chris@42: Chris@42: A(n - 1 <= npad); Chris@42: for (k = n - 1; k < npad; ++k) /* optionally, zero-pad convolution */ Chris@42: buf[k] = 0; Chris@42: Chris@42: os = ego->os; Chris@42: Chris@42: /* compute RDFT of buf, storing in buf (i.e., in-place): */ Chris@42: { Chris@42: plan_rdft *cld = (plan_rdft *) ego->cld1; Chris@42: cld->apply((plan *) cld, buf, buf); Chris@42: } Chris@42: Chris@42: /* set output DC component: */ Chris@42: O[0] = (r0 = I[0]) + buf[0]; Chris@42: Chris@42: /* now, multiply by omega: */ Chris@42: omega = ego->omega; Chris@42: buf[0] *= omega[0]; Chris@42: for (k = 1; k < npad/2; ++k) { Chris@42: E rB, iB, rW, iW, a, b; Chris@42: rW = omega[k]; Chris@42: iW = omega[npad - k]; Chris@42: rB = buf[k]; Chris@42: iB = buf[npad - k]; Chris@42: a = rW * rB - iW * iB; Chris@42: b = rW * iB + iW * rB; Chris@42: #if R2HC_ONLY_CONV Chris@42: buf[k] = a + b; Chris@42: buf[npad - k] = a - b; Chris@42: #else Chris@42: buf[k] = a; Chris@42: buf[npad - k] = b; Chris@42: #endif Chris@42: } Chris@42: /* Nyquist component: */ Chris@42: A(k + k == npad); /* since npad is even */ Chris@42: buf[k] *= omega[k]; Chris@42: Chris@42: /* this will add input[0] to all of the outputs after the ifft */ Chris@42: buf[0] += r0; Chris@42: Chris@42: /* inverse FFT: */ Chris@42: { Chris@42: plan_rdft *cld = (plan_rdft *) ego->cld2; Chris@42: cld->apply((plan *) cld, buf, buf); Chris@42: } Chris@42: Chris@42: /* do inverse permutation to unshuffle the output: */ Chris@42: A(gpower == 1); Chris@42: #if R2HC_ONLY_CONV Chris@42: O[os] = buf[0]; Chris@42: gpower = g = ego->ginv; Chris@42: A(npad == n - 1 || npad/2 >= n - 1); Chris@42: if (npad == n - 1) { Chris@42: for (k = 1; k < npad/2; ++k, gpower = MULMOD(gpower, g, n)) { Chris@42: O[gpower * os] = buf[k] + buf[npad - k]; Chris@42: } Chris@42: O[gpower * os] = buf[k]; Chris@42: ++k, gpower = MULMOD(gpower, g, n); Chris@42: for (; k < npad; ++k, gpower = MULMOD(gpower, g, n)) { Chris@42: O[gpower * os] = buf[npad - k] - buf[k]; Chris@42: } Chris@42: } Chris@42: else { Chris@42: for (k = 1; k < n - 1; ++k, gpower = MULMOD(gpower, g, n)) { Chris@42: O[gpower * os] = buf[k] + buf[npad - k]; Chris@42: } Chris@42: } Chris@42: #else Chris@42: g = ego->ginv; Chris@42: for (k = 0; k < n - 1; ++k, gpower = MULMOD(gpower, g, n)) { Chris@42: O[gpower * os] = buf[k]; Chris@42: } Chris@42: #endif Chris@42: A(gpower == 1); Chris@42: Chris@42: X(ifree)(buf); Chris@42: } Chris@42: Chris@42: static R *mkomega(enum wakefulness wakefulness, Chris@42: plan *p_, INT n, INT npad, INT ginv) Chris@42: { Chris@42: plan_rdft *p = (plan_rdft *) p_; Chris@42: R *omega; Chris@42: INT i, gpower; Chris@42: trigreal scale; Chris@42: triggen *t; Chris@42: Chris@42: if ((omega = X(rader_tl_find)(n, npad + 1, ginv, omegas))) Chris@42: return omega; Chris@42: Chris@42: omega = (R *)MALLOC(sizeof(R) * npad, TWIDDLES); Chris@42: Chris@42: scale = npad; /* normalization for convolution */ Chris@42: Chris@42: t = X(mktriggen)(wakefulness, n); Chris@42: for (i = 0, gpower = 1; i < n-1; ++i, gpower = MULMOD(gpower, ginv, n)) { Chris@42: trigreal w[2]; Chris@42: t->cexpl(t, gpower, w); Chris@42: omega[i] = (w[0] + w[1]) / scale; Chris@42: } Chris@42: X(triggen_destroy)(t); Chris@42: A(gpower == 1); Chris@42: Chris@42: A(npad == n - 1 || npad >= 2*(n - 1) - 1); Chris@42: Chris@42: for (; i < npad; ++i) Chris@42: omega[i] = K(0.0); Chris@42: if (npad > n - 1) Chris@42: for (i = 1; i < n-1; ++i) Chris@42: omega[npad - i] = omega[n - 1 - i]; Chris@42: Chris@42: p->apply(p_, omega, omega); Chris@42: Chris@42: X(rader_tl_insert)(n, npad + 1, ginv, omega, &omegas); Chris@42: return omega; Chris@42: } Chris@42: Chris@42: static void free_omega(R *omega) Chris@42: { Chris@42: X(rader_tl_delete)(omega, &omegas); Chris@42: } 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->cld1, wakefulness); Chris@42: X(plan_awake)(ego->cld2, wakefulness); Chris@42: X(plan_awake)(ego->cld_omega, wakefulness); Chris@42: Chris@42: switch (wakefulness) { Chris@42: case SLEEPY: Chris@42: free_omega(ego->omega); Chris@42: ego->omega = 0; Chris@42: break; Chris@42: default: Chris@42: ego->g = X(find_generator)(ego->n); Chris@42: ego->ginv = X(power_mod)(ego->g, ego->n - 2, ego->n); Chris@42: A(MULMOD(ego->g, ego->ginv, ego->n) == 1); Chris@42: Chris@42: A(!ego->omega); Chris@42: ego->omega = mkomega(wakefulness, Chris@42: ego->cld_omega,ego->n,ego->npad,ego->ginv); Chris@42: break; Chris@42: } 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->cld_omega); 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: Chris@42: p->print(p, "(dht-rader-%D/%D%ois=%oos=%(%p%)", Chris@42: ego->n, ego->npad, ego->is, ego->os, ego->cld1); Chris@42: if (ego->cld2 != ego->cld1) Chris@42: p->print(p, "%(%p%)", ego->cld2); Chris@42: if (ego->cld_omega != ego->cld1 && ego->cld_omega != ego->cld2) Chris@42: p->print(p, "%(%p%)", ego->cld_omega); Chris@42: p->putchr(p, ')'); Chris@42: } Chris@42: Chris@42: static int applicable(const solver *ego, const problem *p_, const planner *plnr) Chris@42: { Chris@42: const problem_rdft *p = (const problem_rdft *) p_; Chris@42: UNUSED(ego); Chris@42: return (1 Chris@42: && p->sz->rnk == 1 Chris@42: && p->vecsz->rnk == 0 Chris@42: && p->kind[0] == DHT Chris@42: && X(is_prime)(p->sz->dims[0].n) Chris@42: && p->sz->dims[0].n > 2 Chris@42: && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > RADER_MAX_SLOW) Chris@42: /* proclaim the solver SLOW if p-1 is not easily Chris@42: factorizable. Unlike in the complex case where Chris@42: Bluestein can solve the problem, in the DHT case we Chris@42: may have no other choice */ Chris@42: && CIMPLIES(NO_SLOWP(plnr), X(factors_into_small_primes)(p->sz->dims[0].n - 1)) Chris@42: ); Chris@42: } Chris@42: Chris@42: static INT choose_transform_size(INT minsz) Chris@42: { Chris@42: static const INT primes[] = { 2, 3, 5, 0 }; Chris@42: while (!X(factors_into)(minsz, primes) || minsz % 2) Chris@42: ++minsz; Chris@42: return minsz; 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 = (const problem_rdft *) p_; Chris@42: P *pln; Chris@42: INT n, npad; Chris@42: INT is, os; Chris@42: plan *cld1 = (plan *) 0; Chris@42: plan *cld2 = (plan *) 0; Chris@42: plan *cld_omega = (plan *) 0; Chris@42: R *buf = (R *) 0; Chris@42: problem *cldp; 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)) Chris@42: return (plan *) 0; Chris@42: Chris@42: n = p->sz->dims[0].n; Chris@42: is = p->sz->dims[0].is; Chris@42: os = p->sz->dims[0].os; Chris@42: Chris@42: if (ego->pad) Chris@42: npad = choose_transform_size(2 * (n - 1) - 1); Chris@42: else Chris@42: npad = n - 1; Chris@42: Chris@42: /* initial allocation for the purpose of planning */ Chris@42: buf = (R *) MALLOC(sizeof(R) * npad, BUFFERS); Chris@42: Chris@42: cld1 = X(mkplan_f_d)(plnr, Chris@42: X(mkproblem_rdft_1_d)(X(mktensor_1d)(npad, 1, 1), Chris@42: X(mktensor_1d)(1, 0, 0), Chris@42: buf, buf, Chris@42: R2HC), Chris@42: NO_SLOW, 0, 0); Chris@42: if (!cld1) goto nada; Chris@42: Chris@42: cldp = Chris@42: X(mkproblem_rdft_1_d)( Chris@42: X(mktensor_1d)(npad, 1, 1), Chris@42: X(mktensor_1d)(1, 0, 0), Chris@42: buf, buf, Chris@42: #if R2HC_ONLY_CONV Chris@42: R2HC Chris@42: #else Chris@42: HC2R Chris@42: #endif Chris@42: ); Chris@42: if (!(cld2 = X(mkplan_f_d)(plnr, cldp, NO_SLOW, 0, 0))) Chris@42: goto nada; Chris@42: Chris@42: /* plan for omega */ Chris@42: cld_omega = X(mkplan_f_d)(plnr, Chris@42: X(mkproblem_rdft_1_d)( Chris@42: X(mktensor_1d)(npad, 1, 1), Chris@42: X(mktensor_1d)(1, 0, 0), Chris@42: buf, buf, R2HC), Chris@42: NO_SLOW, ESTIMATE, 0); Chris@42: if (!cld_omega) goto nada; Chris@42: Chris@42: /* deallocate buffers; let awake() or apply() allocate them for real */ Chris@42: X(ifree)(buf); Chris@42: buf = 0; Chris@42: Chris@42: pln = MKPLAN_RDFT(P, &padt, apply); Chris@42: pln->cld1 = cld1; Chris@42: pln->cld2 = cld2; Chris@42: pln->cld_omega = cld_omega; Chris@42: pln->omega = 0; Chris@42: pln->n = n; Chris@42: pln->npad = npad; Chris@42: pln->is = is; Chris@42: pln->os = os; Chris@42: Chris@42: X(ops_add)(&cld1->ops, &cld2->ops, &pln->super.super.ops); Chris@42: pln->super.super.ops.other += (npad/2-1)*6 + npad + n + (n-1) * ego->pad; Chris@42: pln->super.super.ops.add += (npad/2-1)*2 + 2 + (n-1) * ego->pad; Chris@42: pln->super.super.ops.mul += (npad/2-1)*4 + 2 + ego->pad; Chris@42: #if R2HC_ONLY_CONV Chris@42: pln->super.super.ops.other += n-2 - ego->pad; Chris@42: pln->super.super.ops.add += (npad/2-1)*2 + (n-2) - ego->pad; Chris@42: #endif Chris@42: Chris@42: return &(pln->super.super); Chris@42: Chris@42: nada: Chris@42: X(ifree0)(buf); Chris@42: X(plan_destroy_internal)(cld_omega); Chris@42: X(plan_destroy_internal)(cld2); Chris@42: X(plan_destroy_internal)(cld1); Chris@42: return 0; Chris@42: } Chris@42: Chris@42: /* constructors */ Chris@42: Chris@42: static solver *mksolver(int pad) Chris@42: { Chris@42: static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; Chris@42: S *slv = MKSOLVER(S, &sadt); Chris@42: slv->pad = pad; Chris@42: return &(slv->super); Chris@42: } Chris@42: Chris@42: void X(dht_rader_register)(planner *p) Chris@42: { Chris@42: REGISTER_SOLVER(p, mksolver(0)); Chris@42: REGISTER_SOLVER(p, mksolver(1)); Chris@42: }