/src/fftw3/rdft/vrank-geq1.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2003, 2007-14 Matteo Frigo |
3 | | * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology |
4 | | * |
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. |
9 | | * |
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. |
14 | | * |
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | | * |
19 | | */ |
20 | | |
21 | | |
22 | | |
23 | | /* Plans for handling vector transform loops. These are *just* the |
24 | | loops, and rely on child plans for the actual RDFTs. |
25 | | |
26 | | They form a wrapper around solvers that don't have apply functions |
27 | | for non-null vectors. |
28 | | |
29 | | vrank-geq1 plans also recursively handle the case of multi-dimensional |
30 | | vectors, obviating the need for most solvers to deal with this. We |
31 | | can also play games here, such as reordering the vector loops. |
32 | | |
33 | | Each vrank-geq1 plan reduces the vector rank by 1, picking out a |
34 | | dimension determined by the vecloop_dim field of the solver. */ |
35 | | |
36 | | #include "rdft/rdft.h" |
37 | | |
38 | | typedef struct { |
39 | | solver super; |
40 | | int vecloop_dim; |
41 | | const int *buddies; |
42 | | size_t nbuddies; |
43 | | } S; |
44 | | |
45 | | typedef struct { |
46 | | plan_rdft super; |
47 | | |
48 | | plan *cld; |
49 | | INT vl; |
50 | | INT ivs, ovs; |
51 | | const S *solver; |
52 | | } P; |
53 | | |
54 | | static void apply(const plan *ego_, R *I, R *O) |
55 | | { |
56 | | const P *ego = (const P *) ego_; |
57 | | INT i, vl = ego->vl; |
58 | | INT ivs = ego->ivs, ovs = ego->ovs; |
59 | | rdftapply cldapply = ((plan_rdft *) ego->cld)->apply; |
60 | | |
61 | | for (i = 0; i < vl; ++i) { |
62 | | cldapply(ego->cld, I + i * ivs, O + i * ovs); |
63 | | } |
64 | | } |
65 | | |
66 | | static void awake(plan *ego_, enum wakefulness wakefulness) |
67 | | { |
68 | | P *ego = (P *) ego_; |
69 | | X(plan_awake)(ego->cld, wakefulness); |
70 | | } |
71 | | |
72 | | static void destroy(plan *ego_) |
73 | | { |
74 | | P *ego = (P *) ego_; |
75 | | X(plan_destroy_internal)(ego->cld); |
76 | | } |
77 | | |
78 | | static void print(const plan *ego_, printer *p) |
79 | | { |
80 | | const P *ego = (const P *) ego_; |
81 | | const S *s = ego->solver; |
82 | | p->print(p, "(rdft-vrank>=1-x%D/%d%(%p%))", |
83 | | ego->vl, s->vecloop_dim, ego->cld); |
84 | | } |
85 | | |
86 | | static int pickdim(const S *ego, const tensor *vecsz, int oop, int *dp) |
87 | | { |
88 | | return X(pickdim)(ego->vecloop_dim, ego->buddies, ego->nbuddies, |
89 | | vecsz, oop, dp); |
90 | | } |
91 | | |
92 | | static int applicable0(const solver *ego_, const problem *p_, int *dp) |
93 | | { |
94 | | const S *ego = (const S *) ego_; |
95 | | const problem_rdft *p = (const problem_rdft *) p_; |
96 | | |
97 | | return (1 |
98 | | && FINITE_RNK(p->vecsz->rnk) |
99 | | && p->vecsz->rnk > 0 |
100 | | |
101 | | && p->sz->rnk >= 0 |
102 | | |
103 | | && pickdim(ego, p->vecsz, p->I != p->O, dp) |
104 | | ); |
105 | | } |
106 | | |
107 | | static int applicable(const solver *ego_, const problem *p_, |
108 | | const planner *plnr, int *dp) |
109 | | { |
110 | | const S *ego = (const S *)ego_; |
111 | | const problem_rdft *p; |
112 | | |
113 | | if (!applicable0(ego_, p_, dp)) return 0; |
114 | | |
115 | | /* fftw2 behavior */ |
116 | | if (NO_VRANK_SPLITSP(plnr) && (ego->vecloop_dim != ego->buddies[0])) |
117 | | return 0; |
118 | | |
119 | | p = (const problem_rdft *) p_; |
120 | | |
121 | | if (NO_UGLYP(plnr)) { |
122 | | /* the rank-0 solver deals with the general case most of the |
123 | | time (an exception is loops of non-square transposes) */ |
124 | | if (NO_SLOWP(plnr) && p->sz->rnk == 0) |
125 | | return 0; |
126 | | |
127 | | /* Heuristic: if the transform is multi-dimensional, and the |
128 | | vector stride is less than the transform size, then we |
129 | | probably want to use a rank>=2 plan first in order to combine |
130 | | this vector with the transform-dimension vectors. */ |
131 | | { |
132 | | iodim *d = p->vecsz->dims + *dp; |
133 | | if (1 |
134 | | && p->sz->rnk > 1 |
135 | | && X(imin)(X(iabs)(d->is), X(iabs)(d->os)) |
136 | | < X(tensor_max_index)(p->sz) |
137 | | ) |
138 | | return 0; |
139 | | } |
140 | | |
141 | | /* prefer threaded version */ |
142 | | if (NO_NONTHREADEDP(plnr)) return 0; |
143 | | |
144 | | /* exploit built-in vecloops of (ugly) r{e,o}dft solvers */ |
145 | | if (p->vecsz->rnk == 1 && p->sz->rnk == 1 |
146 | | && REODFT_KINDP(p->kind[0])) |
147 | | return 0; |
148 | | } |
149 | | |
150 | | return 1; |
151 | | } |
152 | | |
153 | | static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) |
154 | | { |
155 | | const S *ego = (const S *) ego_; |
156 | | const problem_rdft *p; |
157 | | P *pln; |
158 | | plan *cld; |
159 | | int vdim; |
160 | | iodim *d; |
161 | | |
162 | | static const plan_adt padt = { |
163 | | X(rdft_solve), awake, print, destroy |
164 | | }; |
165 | | |
166 | | if (!applicable(ego_, p_, plnr, &vdim)) |
167 | | return (plan *) 0; |
168 | | p = (const problem_rdft *) p_; |
169 | | |
170 | | d = p->vecsz->dims + vdim; |
171 | | |
172 | | A(d->n > 1); |
173 | | |
174 | | cld = X(mkplan_d)(plnr, |
175 | | X(mkproblem_rdft_d)( |
176 | | X(tensor_copy)(p->sz), |
177 | | X(tensor_copy_except)(p->vecsz, vdim), |
178 | | TAINT(p->I, d->is), TAINT(p->O, d->os), |
179 | | p->kind)); |
180 | | if (!cld) return (plan *) 0; |
181 | | |
182 | | pln = MKPLAN_RDFT(P, &padt, apply); |
183 | | |
184 | | pln->cld = cld; |
185 | | pln->vl = d->n; |
186 | | pln->ivs = d->is; |
187 | | pln->ovs = d->os; |
188 | | |
189 | | pln->solver = ego; |
190 | | X(ops_zero)(&pln->super.super.ops); |
191 | | pln->super.super.ops.other = 3.14159; /* magic to prefer codelet loops */ |
192 | | X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops); |
193 | | |
194 | | if (p->sz->rnk != 1 || (p->sz->dims[0].n > 128)) |
195 | | pln->super.super.pcost = pln->vl * cld->pcost; |
196 | | |
197 | | return &(pln->super.super); |
198 | | } |
199 | | |
200 | | static solver *mksolver(int vecloop_dim, const int *buddies, size_t nbuddies) |
201 | | { |
202 | | static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 }; |
203 | | S *slv = MKSOLVER(S, &sadt); |
204 | | slv->vecloop_dim = vecloop_dim; |
205 | | slv->buddies = buddies; |
206 | | slv->nbuddies = nbuddies; |
207 | | return &(slv->super); |
208 | | } |
209 | | |
210 | | void X(rdft_vrank_geq1_register)(planner *p) |
211 | 1 | { |
212 | | /* FIXME: Should we try other vecloop_dim values? */ |
213 | 1 | static const int buddies[] = { 1, -1 }; |
214 | 1 | size_t i; |
215 | | |
216 | 3 | for (i = 0; i < NELEM(buddies); ++i) |
217 | 2 | REGISTER_SOLVER(p, mksolver(buddies[i], buddies, NELEM(buddies))); |
218 | 1 | } |