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 #include "hc2hc.h"
|
cannam@127
|
22
|
cannam@127
|
23 hc2hc_solver *(*X(mksolver_hc2hc_hook))(size_t, INT, hc2hc_mkinferior) = 0;
|
cannam@127
|
24
|
cannam@127
|
25 typedef struct {
|
cannam@127
|
26 plan_rdft super;
|
cannam@127
|
27 plan *cld;
|
cannam@127
|
28 plan *cldw;
|
cannam@127
|
29 INT r;
|
cannam@127
|
30 } P;
|
cannam@127
|
31
|
cannam@127
|
32 static void apply_dit(const plan *ego_, R *I, R *O)
|
cannam@127
|
33 {
|
cannam@127
|
34 const P *ego = (const P *) ego_;
|
cannam@127
|
35 plan_rdft *cld;
|
cannam@127
|
36 plan_hc2hc *cldw;
|
cannam@127
|
37
|
cannam@127
|
38 cld = (plan_rdft *) ego->cld;
|
cannam@127
|
39 cld->apply(ego->cld, I, O);
|
cannam@127
|
40
|
cannam@127
|
41 cldw = (plan_hc2hc *) ego->cldw;
|
cannam@127
|
42 cldw->apply(ego->cldw, O);
|
cannam@127
|
43 }
|
cannam@127
|
44
|
cannam@127
|
45 static void apply_dif(const plan *ego_, R *I, R *O)
|
cannam@127
|
46 {
|
cannam@127
|
47 const P *ego = (const P *) ego_;
|
cannam@127
|
48 plan_rdft *cld;
|
cannam@127
|
49 plan_hc2hc *cldw;
|
cannam@127
|
50
|
cannam@127
|
51 cldw = (plan_hc2hc *) ego->cldw;
|
cannam@127
|
52 cldw->apply(ego->cldw, I);
|
cannam@127
|
53
|
cannam@127
|
54 cld = (plan_rdft *) ego->cld;
|
cannam@127
|
55 cld->apply(ego->cld, I, O);
|
cannam@127
|
56 }
|
cannam@127
|
57
|
cannam@127
|
58 static void awake(plan *ego_, enum wakefulness wakefulness)
|
cannam@127
|
59 {
|
cannam@127
|
60 P *ego = (P *) ego_;
|
cannam@127
|
61 X(plan_awake)(ego->cld, wakefulness);
|
cannam@127
|
62 X(plan_awake)(ego->cldw, wakefulness);
|
cannam@127
|
63 }
|
cannam@127
|
64
|
cannam@127
|
65 static void destroy(plan *ego_)
|
cannam@127
|
66 {
|
cannam@127
|
67 P *ego = (P *) ego_;
|
cannam@127
|
68 X(plan_destroy_internal)(ego->cldw);
|
cannam@127
|
69 X(plan_destroy_internal)(ego->cld);
|
cannam@127
|
70 }
|
cannam@127
|
71
|
cannam@127
|
72 static void print(const plan *ego_, printer *p)
|
cannam@127
|
73 {
|
cannam@127
|
74 const P *ego = (const P *) ego_;
|
cannam@127
|
75 p->print(p, "(rdft-ct-%s/%D%(%p%)%(%p%))",
|
cannam@127
|
76 ego->super.apply == apply_dit ? "dit" : "dif",
|
cannam@127
|
77 ego->r, ego->cldw, ego->cld);
|
cannam@127
|
78 }
|
cannam@127
|
79
|
cannam@127
|
80 static int applicable0(const hc2hc_solver *ego, const problem *p_, planner *plnr)
|
cannam@127
|
81 {
|
cannam@127
|
82 const problem_rdft *p = (const problem_rdft *) p_;
|
cannam@127
|
83 INT r;
|
cannam@127
|
84
|
cannam@127
|
85 return (1
|
cannam@127
|
86 && p->sz->rnk == 1
|
cannam@127
|
87 && p->vecsz->rnk <= 1
|
cannam@127
|
88
|
cannam@127
|
89 && (/* either the problem is R2HC, which is solved by DIT */
|
cannam@127
|
90 (p->kind[0] == R2HC)
|
cannam@127
|
91 ||
|
cannam@127
|
92 /* or the problem is HC2R, in which case it is solved
|
cannam@127
|
93 by DIF, which destroys the input */
|
cannam@127
|
94 (p->kind[0] == HC2R &&
|
cannam@127
|
95 (p->I == p->O || !NO_DESTROY_INPUTP(plnr))))
|
cannam@127
|
96
|
cannam@127
|
97 && ((r = X(choose_radix)(ego->r, p->sz->dims[0].n)) > 0)
|
cannam@127
|
98 && p->sz->dims[0].n > r);
|
cannam@127
|
99 }
|
cannam@127
|
100
|
cannam@127
|
101 int X(hc2hc_applicable)(const hc2hc_solver *ego, const problem *p_, planner *plnr)
|
cannam@127
|
102 {
|
cannam@127
|
103 const problem_rdft *p;
|
cannam@127
|
104
|
cannam@127
|
105 if (!applicable0(ego, p_, plnr))
|
cannam@127
|
106 return 0;
|
cannam@127
|
107
|
cannam@127
|
108 p = (const problem_rdft *) p_;
|
cannam@127
|
109
|
cannam@127
|
110 return (0
|
cannam@127
|
111 || p->vecsz->rnk == 0
|
cannam@127
|
112 || !NO_VRECURSEP(plnr)
|
cannam@127
|
113 );
|
cannam@127
|
114 }
|
cannam@127
|
115
|
cannam@127
|
116 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
|
cannam@127
|
117 {
|
cannam@127
|
118 const hc2hc_solver *ego = (const hc2hc_solver *) ego_;
|
cannam@127
|
119 const problem_rdft *p;
|
cannam@127
|
120 P *pln = 0;
|
cannam@127
|
121 plan *cld = 0, *cldw = 0;
|
cannam@127
|
122 INT n, r, m, v, ivs, ovs;
|
cannam@127
|
123 iodim *d;
|
cannam@127
|
124
|
cannam@127
|
125 static const plan_adt padt = {
|
cannam@127
|
126 X(rdft_solve), awake, print, destroy
|
cannam@127
|
127 };
|
cannam@127
|
128
|
cannam@127
|
129 if (NO_NONTHREADEDP(plnr) || !X(hc2hc_applicable)(ego, p_, plnr))
|
cannam@127
|
130 return (plan *) 0;
|
cannam@127
|
131
|
cannam@127
|
132 p = (const problem_rdft *) p_;
|
cannam@127
|
133 d = p->sz->dims;
|
cannam@127
|
134 n = d[0].n;
|
cannam@127
|
135 r = X(choose_radix)(ego->r, n);
|
cannam@127
|
136 m = n / r;
|
cannam@127
|
137
|
cannam@127
|
138 X(tensor_tornk1)(p->vecsz, &v, &ivs, &ovs);
|
cannam@127
|
139
|
cannam@127
|
140 switch (p->kind[0]) {
|
cannam@127
|
141 case R2HC:
|
cannam@127
|
142 cldw = ego->mkcldw(ego,
|
cannam@127
|
143 R2HC, r, m, d[0].os, v, ovs, 0, (m+2)/2,
|
cannam@127
|
144 p->O, plnr);
|
cannam@127
|
145 if (!cldw) goto nada;
|
cannam@127
|
146
|
cannam@127
|
147 cld = X(mkplan_d)(plnr,
|
cannam@127
|
148 X(mkproblem_rdft_d)(
|
cannam@127
|
149 X(mktensor_1d)(m, r * d[0].is, d[0].os),
|
cannam@127
|
150 X(mktensor_2d)(r, d[0].is, m * d[0].os,
|
cannam@127
|
151 v, ivs, ovs),
|
cannam@127
|
152 p->I, p->O, p->kind)
|
cannam@127
|
153 );
|
cannam@127
|
154 if (!cld) goto nada;
|
cannam@127
|
155
|
cannam@127
|
156 pln = MKPLAN_RDFT(P, &padt, apply_dit);
|
cannam@127
|
157 break;
|
cannam@127
|
158
|
cannam@127
|
159 case HC2R:
|
cannam@127
|
160 cldw = ego->mkcldw(ego,
|
cannam@127
|
161 HC2R, r, m, d[0].is, v, ivs, 0, (m+2)/2,
|
cannam@127
|
162 p->I, plnr);
|
cannam@127
|
163 if (!cldw) goto nada;
|
cannam@127
|
164
|
cannam@127
|
165 cld = X(mkplan_d)(plnr,
|
cannam@127
|
166 X(mkproblem_rdft_d)(
|
cannam@127
|
167 X(mktensor_1d)(m, d[0].is, r * d[0].os),
|
cannam@127
|
168 X(mktensor_2d)(r, m * d[0].is, d[0].os,
|
cannam@127
|
169 v, ivs, ovs),
|
cannam@127
|
170 p->I, p->O, p->kind)
|
cannam@127
|
171 );
|
cannam@127
|
172 if (!cld) goto nada;
|
cannam@127
|
173
|
cannam@127
|
174 pln = MKPLAN_RDFT(P, &padt, apply_dif);
|
cannam@127
|
175 break;
|
cannam@127
|
176
|
cannam@127
|
177 default:
|
cannam@127
|
178 A(0);
|
cannam@127
|
179 }
|
cannam@127
|
180
|
cannam@127
|
181 pln->cld = cld;
|
cannam@127
|
182 pln->cldw = cldw;
|
cannam@127
|
183 pln->r = r;
|
cannam@127
|
184 X(ops_add)(&cld->ops, &cldw->ops, &pln->super.super.ops);
|
cannam@127
|
185
|
cannam@127
|
186 /* inherit could_prune_now_p attribute from cldw */
|
cannam@127
|
187 pln->super.super.could_prune_now_p = cldw->could_prune_now_p;
|
cannam@127
|
188
|
cannam@127
|
189 return &(pln->super.super);
|
cannam@127
|
190
|
cannam@127
|
191 nada:
|
cannam@127
|
192 X(plan_destroy_internal)(cldw);
|
cannam@127
|
193 X(plan_destroy_internal)(cld);
|
cannam@127
|
194 return (plan *) 0;
|
cannam@127
|
195 }
|
cannam@127
|
196
|
cannam@127
|
197 hc2hc_solver *X(mksolver_hc2hc)(size_t size, INT r, hc2hc_mkinferior mkcldw)
|
cannam@127
|
198 {
|
cannam@127
|
199 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
|
cannam@127
|
200 hc2hc_solver *slv = (hc2hc_solver *)X(mksolver)(size, &sadt);
|
cannam@127
|
201 slv->r = r;
|
cannam@127
|
202 slv->mkcldw = mkcldw;
|
cannam@127
|
203 return slv;
|
cannam@127
|
204 }
|
cannam@127
|
205
|
cannam@127
|
206 plan *X(mkplan_hc2hc)(size_t size, const plan_adt *adt, hc2hcapply apply)
|
cannam@127
|
207 {
|
cannam@127
|
208 plan_hc2hc *ego;
|
cannam@127
|
209
|
cannam@127
|
210 ego = (plan_hc2hc *) X(mkplan)(size, adt);
|
cannam@127
|
211 ego->apply = apply;
|
cannam@127
|
212
|
cannam@127
|
213 return &(ego->super);
|
cannam@127
|
214 }
|