cannam@127
|
1 /*
|
cannam@127
|
2 * Copyright (c) 2003, 2007-14 Matteo Frigo
|
cannam@127
|
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
|
cannam@127
|
4 *
|
cannam@127
|
5 * This program is free software; you can redistribute it and/or modify
|
cannam@127
|
6 * it under the terms of the GNU General Public License as published by
|
cannam@127
|
7 * the Free Software Foundation; either version 2 of the License, or
|
cannam@127
|
8 * (at your option) any later version.
|
cannam@127
|
9 *
|
cannam@127
|
10 * This program is distributed in the hope that it will be useful,
|
cannam@127
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@127
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@127
|
13 * GNU General Public License for more details.
|
cannam@127
|
14 *
|
cannam@127
|
15 * You should have received a copy of the GNU General Public License
|
cannam@127
|
16 * along with this program; if not, write to the Free Software
|
cannam@127
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
cannam@127
|
18 *
|
cannam@127
|
19 */
|
cannam@127
|
20
|
cannam@127
|
21
|
cannam@127
|
22 /* Compute the complex DFT by combining R2HC RDFTs on the real
|
cannam@127
|
23 and imaginary parts. This could be useful for people just wanting
|
cannam@127
|
24 to link to the real codelets and not the complex ones. It could
|
cannam@127
|
25 also even be faster than the complex algorithms for split (as opposed
|
cannam@127
|
26 to interleaved) real/imag complex data. */
|
cannam@127
|
27
|
cannam@127
|
28 #include "rdft.h"
|
cannam@127
|
29 #include "dft.h"
|
cannam@127
|
30
|
cannam@127
|
31 typedef struct {
|
cannam@127
|
32 solver super;
|
cannam@127
|
33 } S;
|
cannam@127
|
34
|
cannam@127
|
35 typedef struct {
|
cannam@127
|
36 plan_dft super;
|
cannam@127
|
37 plan *cld;
|
cannam@127
|
38 INT ishift, oshift;
|
cannam@127
|
39 INT os;
|
cannam@127
|
40 INT n;
|
cannam@127
|
41 } P;
|
cannam@127
|
42
|
cannam@127
|
43 static void apply(const plan *ego_, R *ri, R *ii, R *ro, R *io)
|
cannam@127
|
44 {
|
cannam@127
|
45 const P *ego = (const P *) ego_;
|
cannam@127
|
46 INT n;
|
cannam@127
|
47
|
cannam@127
|
48 UNUSED(ii);
|
cannam@127
|
49
|
cannam@127
|
50 { /* transform vector of real & imag parts: */
|
cannam@127
|
51 plan_rdft *cld = (plan_rdft *) ego->cld;
|
cannam@127
|
52 cld->apply((plan *) cld, ri + ego->ishift, ro + ego->oshift);
|
cannam@127
|
53 }
|
cannam@127
|
54
|
cannam@127
|
55 n = ego->n;
|
cannam@127
|
56 if (n > 1) {
|
cannam@127
|
57 INT i, os = ego->os;
|
cannam@127
|
58 for (i = 1; i < (n + 1)/2; ++i) {
|
cannam@127
|
59 E rop, iop, iom, rom;
|
cannam@127
|
60 rop = ro[os * i];
|
cannam@127
|
61 iop = io[os * i];
|
cannam@127
|
62 rom = ro[os * (n - i)];
|
cannam@127
|
63 iom = io[os * (n - i)];
|
cannam@127
|
64 ro[os * i] = rop - iom;
|
cannam@127
|
65 io[os * i] = iop + rom;
|
cannam@127
|
66 ro[os * (n - i)] = rop + iom;
|
cannam@127
|
67 io[os * (n - i)] = iop - rom;
|
cannam@127
|
68 }
|
cannam@127
|
69 }
|
cannam@127
|
70 }
|
cannam@127
|
71
|
cannam@127
|
72 static void awake(plan *ego_, enum wakefulness wakefulness)
|
cannam@127
|
73 {
|
cannam@127
|
74 P *ego = (P *) ego_;
|
cannam@127
|
75 X(plan_awake)(ego->cld, wakefulness);
|
cannam@127
|
76 }
|
cannam@127
|
77
|
cannam@127
|
78 static void destroy(plan *ego_)
|
cannam@127
|
79 {
|
cannam@127
|
80 P *ego = (P *) ego_;
|
cannam@127
|
81 X(plan_destroy_internal)(ego->cld);
|
cannam@127
|
82 }
|
cannam@127
|
83
|
cannam@127
|
84 static void print(const plan *ego_, printer *p)
|
cannam@127
|
85 {
|
cannam@127
|
86 const P *ego = (const P *) ego_;
|
cannam@127
|
87 p->print(p, "(dft-r2hc-%D%(%p%))", ego->n, ego->cld);
|
cannam@127
|
88 }
|
cannam@127
|
89
|
cannam@127
|
90
|
cannam@127
|
91 static int applicable0(const problem *p_)
|
cannam@127
|
92 {
|
cannam@127
|
93 const problem_dft *p = (const problem_dft *) p_;
|
cannam@127
|
94 return ((p->sz->rnk == 1 && p->vecsz->rnk == 0)
|
cannam@127
|
95 || (p->sz->rnk == 0 && FINITE_RNK(p->vecsz->rnk))
|
cannam@127
|
96 );
|
cannam@127
|
97 }
|
cannam@127
|
98
|
cannam@127
|
99 static int splitp(R *r, R *i, INT n, INT s)
|
cannam@127
|
100 {
|
cannam@127
|
101 return ((r > i ? (r - i) : (i - r)) >= n * (s > 0 ? s : 0-s));
|
cannam@127
|
102 }
|
cannam@127
|
103
|
cannam@127
|
104 static int applicable(const problem *p_, const planner *plnr)
|
cannam@127
|
105 {
|
cannam@127
|
106 if (!applicable0(p_)) return 0;
|
cannam@127
|
107
|
cannam@127
|
108 {
|
cannam@127
|
109 const problem_dft *p = (const problem_dft *) p_;
|
cannam@127
|
110
|
cannam@127
|
111 /* rank-0 problems are always OK */
|
cannam@127
|
112 if (p->sz->rnk == 0) return 1;
|
cannam@127
|
113
|
cannam@127
|
114 /* this solver is ok for split arrays */
|
cannam@127
|
115 if (p->sz->rnk == 1 &&
|
cannam@127
|
116 splitp(p->ri, p->ii, p->sz->dims[0].n, p->sz->dims[0].is) &&
|
cannam@127
|
117 splitp(p->ro, p->io, p->sz->dims[0].n, p->sz->dims[0].os))
|
cannam@127
|
118 return 1;
|
cannam@127
|
119
|
cannam@127
|
120 return !(NO_DFT_R2HCP(plnr));
|
cannam@127
|
121 }
|
cannam@127
|
122 }
|
cannam@127
|
123
|
cannam@127
|
124 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
|
cannam@127
|
125 {
|
cannam@127
|
126 P *pln;
|
cannam@127
|
127 const problem_dft *p;
|
cannam@127
|
128 plan *cld;
|
cannam@127
|
129 INT ishift = 0, oshift = 0;
|
cannam@127
|
130
|
cannam@127
|
131 static const plan_adt padt = {
|
cannam@127
|
132 X(dft_solve), awake, print, destroy
|
cannam@127
|
133 };
|
cannam@127
|
134
|
cannam@127
|
135 UNUSED(ego_);
|
cannam@127
|
136 if (!applicable(p_, plnr))
|
cannam@127
|
137 return (plan *)0;
|
cannam@127
|
138
|
cannam@127
|
139 p = (const problem_dft *) p_;
|
cannam@127
|
140
|
cannam@127
|
141 {
|
cannam@127
|
142 tensor *ri_vec = X(mktensor_1d)(2, p->ii - p->ri, p->io - p->ro);
|
cannam@127
|
143 tensor *cld_vec = X(tensor_append)(ri_vec, p->vecsz);
|
cannam@127
|
144 int i;
|
cannam@127
|
145 for (i = 0; i < cld_vec->rnk; ++i) { /* make all istrides > 0 */
|
cannam@127
|
146 if (cld_vec->dims[i].is < 0) {
|
cannam@127
|
147 INT nm1 = cld_vec->dims[i].n - 1;
|
cannam@127
|
148 ishift -= nm1 * (cld_vec->dims[i].is *= -1);
|
cannam@127
|
149 oshift -= nm1 * (cld_vec->dims[i].os *= -1);
|
cannam@127
|
150 }
|
cannam@127
|
151 }
|
cannam@127
|
152 cld = X(mkplan_d)(plnr,
|
cannam@127
|
153 X(mkproblem_rdft_1)(p->sz, cld_vec,
|
cannam@127
|
154 p->ri + ishift,
|
cannam@127
|
155 p->ro + oshift, R2HC));
|
cannam@127
|
156 X(tensor_destroy2)(ri_vec, cld_vec);
|
cannam@127
|
157 }
|
cannam@127
|
158 if (!cld) return (plan *)0;
|
cannam@127
|
159
|
cannam@127
|
160 pln = MKPLAN_DFT(P, &padt, apply);
|
cannam@127
|
161
|
cannam@127
|
162 if (p->sz->rnk == 0) {
|
cannam@127
|
163 pln->n = 1;
|
cannam@127
|
164 pln->os = 0;
|
cannam@127
|
165 }
|
cannam@127
|
166 else {
|
cannam@127
|
167 pln->n = p->sz->dims[0].n;
|
cannam@127
|
168 pln->os = p->sz->dims[0].os;
|
cannam@127
|
169 }
|
cannam@127
|
170 pln->ishift = ishift;
|
cannam@127
|
171 pln->oshift = oshift;
|
cannam@127
|
172
|
cannam@127
|
173 pln->cld = cld;
|
cannam@127
|
174
|
cannam@127
|
175 pln->super.super.ops = cld->ops;
|
cannam@127
|
176 pln->super.super.ops.other += 8 * ((pln->n - 1)/2);
|
cannam@127
|
177 pln->super.super.ops.add += 4 * ((pln->n - 1)/2);
|
cannam@127
|
178 pln->super.super.ops.other += 1; /* estimator hack for nop plans */
|
cannam@127
|
179
|
cannam@127
|
180 return &(pln->super.super);
|
cannam@127
|
181 }
|
cannam@127
|
182
|
cannam@127
|
183 /* constructor */
|
cannam@127
|
184 static solver *mksolver(void)
|
cannam@127
|
185 {
|
cannam@127
|
186 static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
|
cannam@127
|
187 S *slv = MKSOLVER(S, &sadt);
|
cannam@127
|
188 return &(slv->super);
|
cannam@127
|
189 }
|
cannam@127
|
190
|
cannam@127
|
191 void X(dft_r2hc_register)(planner *p)
|
cannam@127
|
192 {
|
cannam@127
|
193 REGISTER_SOLVER(p, mksolver());
|
cannam@127
|
194 }
|