2
* Copyright (c) 2003, 2006 Matteo Frigo
3
* Copyright (c) 2003, 2006 Massachusetts Institute of Technology
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
/* $Id: redft00e-r2hc.c,v 1.32 2006-01-27 02:10:50 athena Exp $ */
23
/* Do a REDFT00 problem via an R2HC problem, with some pre/post-processing.
25
This code uses the trick from FFTPACK, also documented in a similar
26
form by Numerical Recipes. Unfortunately, this algorithm seems to
27
have intrinsic numerical problems (similar to those in
28
reodft11e-r2hc.c), possibly due to the fact that it multiplies its
29
input by a cosine, causing a loss of precision near the zero. For
30
transforms of 16k points, it has already lost three or four decimal
31
places of accuracy, which we deem unacceptable.
33
So, we have abandoned this algorithm in favor of the one in
34
redft00-r2hc-pad.c, which unfortunately sacrifices 30-50% in speed.
35
The only other alternative in the literature that does not have
36
similar numerical difficulties seems to be the direct adaptation of
37
the Cooley-Tukey decomposition for symmetric data, but this would
38
require a whole new set of codelets and it's not clear that it's
39
worth it at this point. However, we did implement the latter
40
algorithm for the specific case of odd n (logically adapting the
41
split-radix algorithm); see reodft00e-splitradix.c. */
59
static void apply(const plan *ego_, R *I, R *O)
61
const P *ego = (const P *) ego_;
62
INT is = ego->is, os = ego->os;
65
INT ivs = ego->ivs, ovs = ego->ovs;
70
buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
72
for (iv = 0; iv < vl; ++iv, I += ivs, O += ovs) {
73
buf[0] = I[0] + I[is * n];
74
csum = I[0] - I[is * n];
75
for (i = 1; i < n - i; ++i) {
79
csum += W[2*i] * (amb = K(2.0)*(a - b));
83
buf[n - i] = apb + amb;
86
buf[i] = K(2.0) * I[is * i];
90
plan_rdft *cld = (plan_rdft *) ego->cld;
91
cld->apply((plan *) cld, buf, buf);
94
/* FIXME: use recursive/cascade summation for better stability? */
97
for (i = 1; i + i < n; ++i) {
100
O[os * (k + 1)] = O[os * (k - 1)] - buf[n - i];
110
static void awake(plan *ego_, enum wakefulness wakefulness)
113
static const tw_instr redft00e_tw[] = {
119
X(plan_awake)(ego->cld, wakefulness);
120
X(twiddle_awake)(wakefulness,
121
&ego->td, redft00e_tw, 2*ego->n, 1, (ego->n+1)/2);
124
static void destroy(plan *ego_)
127
X(plan_destroy_internal)(ego->cld);
130
static void print(const plan *ego_, printer *p)
132
const P *ego = (const P *) ego_;
133
p->print(p, "(redft00e-r2hc-%D%v%(%p%))", ego->n + 1, ego->vl, ego->cld);
136
static int applicable0(const solver *ego_, const problem *p_)
138
const problem_rdft *p = (const problem_rdft *) p_;
143
&& p->vecsz->rnk <= 1
144
&& p->kind[0] == REDFT00
145
&& p->sz->dims[0].n > 1 /* n == 1 is not well-defined */
149
static int applicable(const solver *ego, const problem *p, const planner *plnr)
151
return (!NO_SLOWP(plnr) && applicable0(ego, p));
154
static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
157
const problem_rdft *p;
163
static const plan_adt padt = {
164
X(rdft_solve), awake, print, destroy
167
if (!applicable(ego_, p_, plnr))
170
p = (const problem_rdft *) p_;
172
n = p->sz->dims[0].n - 1;
174
buf = (R *) MALLOC(sizeof(R) * n, BUFFERS);
176
cld = X(mkplan_d)(plnr, X(mkproblem_rdft_1_d)(X(mktensor_1d)(n, 1, 1),
183
pln = MKPLAN_RDFT(P, &padt, apply);
186
pln->is = p->sz->dims[0].is;
187
pln->os = p->sz->dims[0].os;
191
X(tensor_tornk1)(p->vecsz, &pln->vl, &pln->ivs, &pln->ovs);
194
ops.other = 8 + (n-1)/2 * 11 + (1 - n % 2) * 5;
195
ops.add = 2 + (n-1)/2 * 5;
196
ops.mul = (n-1)/2 * 3 + (1 - n % 2) * 1;
198
X(ops_zero)(&pln->super.super.ops);
199
X(ops_madd2)(pln->vl, &ops, &pln->super.super.ops);
200
X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
202
return &(pln->super.super);
206
static solver *mksolver(void)
208
static const solver_adt sadt = { PROBLEM_RDFT, mkplan };
209
S *slv = MKSOLVER(S, &sadt);
210
return &(slv->super);
213
void X(redft00e_r2hc_register)(planner *p)
215
REGISTER_SOLVER(p, mksolver());