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 "dft.h" Chris@42: Chris@42: /* Chris@42: * Compute transforms of prime sizes using Rader's trick: turn them Chris@42: * into convolutions of size n - 1, which you then perform via a pair Chris@42: * of FFTs. Chris@42: */ Chris@42: Chris@42: typedef struct { Chris@42: solver super; Chris@42: } S; Chris@42: Chris@42: typedef struct { Chris@42: plan_dft super; Chris@42: Chris@42: plan *cld1, *cld2; Chris@42: R *omega; Chris@42: INT n, 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: static R *mkomega(enum wakefulness wakefulness, plan *p_, INT n, INT ginv) Chris@42: { Chris@42: plan_dft *p = (plan_dft *) 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, n, ginv, omegas))) Chris@42: return omega; Chris@42: Chris@42: omega = (R *)MALLOC(sizeof(R) * (n - 1) * 2, TWIDDLES); Chris@42: Chris@42: scale = n - 1.0; /* 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[2*i] = w[0] / scale; Chris@42: omega[2*i+1] = FFT_SIGN * w[1] / scale; Chris@42: } Chris@42: X(triggen_destroy)(t); Chris@42: A(gpower == 1); Chris@42: Chris@42: p->apply(p_, omega, omega + 1, omega, omega + 1); Chris@42: Chris@42: X(rader_tl_insert)(n, n, 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: Chris@42: /* Below, we extensively use the identity that fft(x*)* = ifft(x) in Chris@42: order to share data between forward and backward transforms and to Chris@42: obviate the necessity of having separate forward and backward Chris@42: plans. (Although we often compute separate plans these days anyway Chris@42: due to the differing strides, etcetera.) Chris@42: Chris@42: Of course, since the new FFTW gives us separate pointers to Chris@42: the real and imaginary parts, we could have instead used the Chris@42: fft(r,i) = ifft(i,r) form of this identity, but it was easier to Chris@42: reuse the code from our old version. */ Chris@42: Chris@42: static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io) Chris@42: { Chris@42: const P *ego = (const P *) ego_; Chris@42: INT is, os; Chris@42: INT k, gpower, g, r; Chris@42: R *buf; Chris@42: R r0 = ri[0], i0 = ii[0]; Chris@42: Chris@42: r = ego->n; is = ego->is; os = ego->os; g = ego->g; Chris@42: buf = (R *) MALLOC(sizeof(R) * (r - 1) * 2, BUFFERS); Chris@42: Chris@42: /* First, permute the input, storing in buf: */ Chris@42: for (gpower = 1, k = 0; k < r - 1; ++k, gpower = MULMOD(gpower, g, r)) { Chris@42: R rA, iA; Chris@42: rA = ri[gpower * is]; Chris@42: iA = ii[gpower * is]; Chris@42: buf[2*k] = rA; buf[2*k + 1] = iA; Chris@42: } Chris@42: /* gpower == g^(r-1) mod r == 1 */; Chris@42: Chris@42: Chris@42: /* compute DFT of buf, storing in output (except DC): */ Chris@42: { Chris@42: plan_dft *cld = (plan_dft *) ego->cld1; Chris@42: cld->apply(ego->cld1, buf, buf+1, ro+os, io+os); Chris@42: } Chris@42: Chris@42: /* set output DC component: */ Chris@42: { Chris@42: ro[0] = r0 + ro[os]; Chris@42: io[0] = i0 + io[os]; Chris@42: } Chris@42: Chris@42: /* now, multiply by omega: */ Chris@42: { Chris@42: const R *omega = ego->omega; Chris@42: for (k = 0; k < r - 1; ++k) { Chris@42: E rB, iB, rW, iW; Chris@42: rW = omega[2*k]; Chris@42: iW = omega[2*k+1]; Chris@42: rB = ro[(k+1)*os]; Chris@42: iB = io[(k+1)*os]; Chris@42: ro[(k+1)*os] = rW * rB - iW * iB; Chris@42: io[(k+1)*os] = -(rW * iB + iW * rB); Chris@42: } Chris@42: } Chris@42: Chris@42: /* this will add input[0] to all of the outputs after the ifft */ Chris@42: ro[os] += r0; Chris@42: io[os] -= i0; Chris@42: Chris@42: /* inverse FFT: */ Chris@42: { Chris@42: plan_dft *cld = (plan_dft *) ego->cld2; Chris@42: cld->apply(ego->cld2, ro+os, io+os, buf, buf+1); Chris@42: } Chris@42: Chris@42: /* finally, do inverse permutation to unshuffle the output: */ Chris@42: { Chris@42: INT ginv = ego->ginv; Chris@42: gpower = 1; Chris@42: for (k = 0; k < r - 1; ++k, gpower = MULMOD(gpower, ginv, r)) { Chris@42: ro[gpower * os] = buf[2*k]; Chris@42: io[gpower * os] = -buf[2*k+1]; Chris@42: } Chris@42: A(gpower == 1); Chris@42: } Chris@42: Chris@42: Chris@42: X(ifree)(buf); 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: ego->omega = mkomega(wakefulness, Chris@42: ego->cld_omega, ego->n, 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: p->print(p, "(dft-rader-%D%ois=%oos=%(%p%)", Chris@42: ego->n, 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_, Chris@42: const planner *plnr) Chris@42: { Chris@42: const problem_dft *p = (const problem_dft *) p_; Chris@42: UNUSED(ego_); Chris@42: return (1 Chris@42: && p->sz->rnk == 1 Chris@42: && p->vecsz->rnk == 0 Chris@42: && CIMPLIES(NO_SLOWP(plnr), p->sz->dims[0].n > RADER_MAX_SLOW) Chris@42: && X(is_prime)(p->sz->dims[0].n) Chris@42: Chris@42: /* proclaim the solver SLOW if p-1 is not easily factorizable. Chris@42: Bluestein should take care of this case. */ 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 mkP(P *pln, INT n, INT is, INT os, R *ro, R *io, Chris@42: planner *plnr) Chris@42: { 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: Chris@42: /* initial allocation for the purpose of planning */ Chris@42: buf = (R *) MALLOC(sizeof(R) * (n - 1) * 2, BUFFERS); Chris@42: Chris@42: cld1 = X(mkplan_f_d)(plnr, Chris@42: X(mkproblem_dft_d)(X(mktensor_1d)(n - 1, 2, os), Chris@42: X(mktensor_1d)(1, 0, 0), Chris@42: buf, buf + 1, ro + os, io + os), Chris@42: NO_SLOW, 0, 0); Chris@42: if (!cld1) goto nada; Chris@42: Chris@42: cld2 = X(mkplan_f_d)(plnr, Chris@42: X(mkproblem_dft_d)(X(mktensor_1d)(n - 1, os, 2), Chris@42: X(mktensor_1d)(1, 0, 0), Chris@42: ro + os, io + os, buf, buf + 1), Chris@42: NO_SLOW, 0, 0); Chris@42: Chris@42: if (!cld2) goto nada; Chris@42: Chris@42: /* plan for omega array */ Chris@42: cld_omega = X(mkplan_f_d)(plnr, Chris@42: X(mkproblem_dft_d)(X(mktensor_1d)(n - 1, 2, 2), Chris@42: X(mktensor_1d)(1, 0, 0), Chris@42: buf, buf + 1, buf, buf + 1), 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->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->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 += (n - 1) * (4 * 2 + 6) + 6; Chris@42: pln->super.super.ops.add += (n - 1) * 2 + 4; Chris@42: pln->super.super.ops.mul += (n - 1) * 4; Chris@42: Chris@42: return 1; 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: static plan *mkplan(const solver *ego, const problem *p_, planner *plnr) Chris@42: { Chris@42: const problem_dft *p = (const problem_dft *) p_; Chris@42: P *pln; Chris@42: INT n; Chris@42: INT is, os; Chris@42: Chris@42: static const plan_adt padt = { Chris@42: X(dft_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: pln = MKPLAN_DFT(P, &padt, apply); Chris@42: if (!mkP(pln, n, is, os, p->ro, p->io, plnr)) { Chris@42: X(ifree)(pln); Chris@42: return (plan *) 0; Chris@42: } Chris@42: return &(pln->super.super); Chris@42: } Chris@42: Chris@42: static solver *mksolver(void) Chris@42: { Chris@42: static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 }; Chris@42: S *slv = MKSOLVER(S, &sadt); Chris@42: return &(slv->super); Chris@42: } Chris@42: Chris@42: void X(dft_rader_register)(planner *p) Chris@42: { Chris@42: REGISTER_SOLVER(p, mksolver()); Chris@42: }