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 RDFT2s.
25 
26    They form a wrapper around solvers that don't have apply functions
27    for non-null vectors.
28 
29    vrank-geq1-rdft2 plans also recursively handle the case of
30    multi-dimensional vectors, obviating the need for most solvers to
31    deal with this.  We can also play games here, such as reordering
32    the vector loops.
33 
34    Each vrank-geq1-rdft2 plan reduces the vector rank by 1, picking out a
35    dimension determined by the vecloop_dim field of the solver. */
36 
37 #include "rdft/rdft.h"
38 
39 typedef struct {
40      solver super;
41      int vecloop_dim;
42      const int *buddies;
43      size_t nbuddies;
44 } S;
45 
46 typedef struct {
47      plan_rdft2 super;
48 
49      plan *cld;
50      INT vl;
51      INT rvs, cvs;
52      const S *solver;
53 } P;
54 
apply(const plan * ego_,R * r0,R * r1,R * cr,R * ci)55 static void apply(const plan *ego_, R *r0, R *r1, R *cr, R *ci)
56 {
57      const P *ego = (const P *) ego_;
58      INT i, vl = ego->vl;
59      INT rvs = ego->rvs, cvs = ego->cvs;
60      rdft2apply cldapply = ((plan_rdft2 *) ego->cld)->apply;
61 
62      for (i = 0; i < vl; ++i) {
63           cldapply(ego->cld, r0 + i * rvs, r1 + i * rvs,
64 		   cr + i * cvs, ci + i * cvs);
65      }
66 }
67 
awake(plan * ego_,enum wakefulness wakefulness)68 static void awake(plan *ego_, enum wakefulness wakefulness)
69 {
70      P *ego = (P *) ego_;
71      X(plan_awake)(ego->cld, wakefulness);
72 }
73 
destroy(plan * ego_)74 static void destroy(plan *ego_)
75 {
76      P *ego = (P *) ego_;
77      X(plan_destroy_internal)(ego->cld);
78 }
79 
print(const plan * ego_,printer * p)80 static void print(const plan *ego_, printer *p)
81 {
82      const P *ego = (const P *) ego_;
83      const S *s = ego->solver;
84      p->print(p, "(rdft2-vrank>=1-x%D/%d%(%p%))",
85 	      ego->vl, s->vecloop_dim, ego->cld);
86 }
87 
pickdim(const S * ego,const tensor * vecsz,int oop,int * dp)88 static int pickdim(const S *ego, const tensor *vecsz, int oop, int *dp)
89 {
90      return X(pickdim)(ego->vecloop_dim, ego->buddies, ego->nbuddies,
91 		       vecsz, oop, dp);
92 }
93 
applicable0(const solver * ego_,const problem * p_,int * dp)94 static int applicable0(const solver *ego_, const problem *p_, int *dp)
95 {
96      const S *ego = (const S *) ego_;
97      const problem_rdft2 *p = (const problem_rdft2 *) p_;
98      if (FINITE_RNK(p->vecsz->rnk)
99 	 && p->vecsz->rnk > 0
100 	 && pickdim(ego, p->vecsz, p->r0 != p->cr, dp)) {
101 	  if (p->r0 != p->cr)
102 	       return 1;  /* can always operate out-of-place */
103 
104 	  return(X(rdft2_inplace_strides)(p, *dp));
105      }
106 
107      return 0;
108 }
109 
110 
applicable(const solver * ego_,const problem * p_,const planner * plnr,int * dp)111 static int applicable(const solver *ego_, const problem *p_,
112 		      const planner *plnr, int *dp)
113 {
114      const S *ego = (const S *)ego_;
115      if (!applicable0(ego_, p_, dp)) return 0;
116 
117      /* fftw2 behavior */
118      if (NO_VRANK_SPLITSP(plnr) && (ego->vecloop_dim != ego->buddies[0]))
119 	  return 0;
120 
121      if (NO_UGLYP(plnr)) {
122 	  const problem_rdft2 *p = (const problem_rdft2 *) p_;
123 	  iodim *d = p->vecsz->dims + *dp;
124 
125 	  /* Heuristic: if the transform is multi-dimensional, and the
126 	     vector stride is less than the transform size, then we
127 	     probably want to use a rank>=2 plan first in order to combine
128 	     this vector with the transform-dimension vectors. */
129 	  if (p->sz->rnk > 1
130 	      && X(imin)(X(iabs)(d->is), X(iabs)(d->os))
131 	      < X(rdft2_tensor_max_index)(p->sz, p->kind)
132 	       )
133 	       return 0;
134 
135 	  /* Heuristic: don't use a vrank-geq1 for rank-0 vrank-1
136 	     transforms, since this case is better handled by rank-0
137 	     solvers. */
138 	  if (p->sz->rnk == 0 && p->vecsz->rnk == 1) return 0;
139 
140 	  if (NO_NONTHREADEDP(plnr))
141 	       return 0; /* prefer threaded version */
142      }
143 
144      return 1;
145 }
146 
mkplan(const solver * ego_,const problem * p_,planner * plnr)147 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
148 {
149      const S *ego = (const S *) ego_;
150      const problem_rdft2 *p;
151      P *pln;
152      plan *cld;
153      int vdim;
154      iodim *d;
155      INT rvs, cvs;
156 
157      static const plan_adt padt = {
158 	  X(rdft2_solve), awake, print, destroy
159      };
160 
161      if (!applicable(ego_, p_, plnr, &vdim))
162           return (plan *) 0;
163      p = (const problem_rdft2 *) p_;
164 
165      d = p->vecsz->dims + vdim;
166 
167      A(d->n > 1);  /* or else, p->ri + d->is etc. are invalid */
168 
169      X(rdft2_strides)(p->kind, d, &rvs, &cvs);
170 
171      cld = X(mkplan_d)(plnr,
172 		       X(mkproblem_rdft2_d)(
173 			    X(tensor_copy)(p->sz),
174 			    X(tensor_copy_except)(p->vecsz, vdim),
175 			    TAINT(p->r0, rvs), TAINT(p->r1, rvs),
176 			    TAINT(p->cr, cvs), TAINT(p->ci, cvs),
177 			    p->kind));
178      if (!cld) return (plan *) 0;
179 
180      pln = MKPLAN_RDFT2(P, &padt, apply);
181 
182      pln->cld = cld;
183      pln->vl = d->n;
184      pln->rvs = rvs;
185      pln->cvs = cvs;
186 
187      pln->solver = ego;
188      X(ops_zero)(&pln->super.super.ops);
189      pln->super.super.ops.other = 3.14159; /* magic to prefer codelet loops */
190      X(ops_madd2)(pln->vl, &cld->ops, &pln->super.super.ops);
191 
192      if (p->sz->rnk != 1 || (p->sz->dims[0].n > 128))
193 	  pln->super.super.pcost = pln->vl * cld->pcost;
194 
195      return &(pln->super.super);
196 }
197 
mksolver(int vecloop_dim,const int * buddies,size_t nbuddies)198 static solver *mksolver(int vecloop_dim, const int *buddies, size_t nbuddies)
199 {
200      static const solver_adt sadt = { PROBLEM_RDFT2, mkplan, 0 };
201      S *slv = MKSOLVER(S, &sadt);
202      slv->vecloop_dim = vecloop_dim;
203      slv->buddies = buddies;
204      slv->nbuddies = nbuddies;
205      return &(slv->super);
206 }
207 
X(rdft2_vrank_geq1_register)208 void X(rdft2_vrank_geq1_register)(planner *p)
209 {
210      /* FIXME: Should we try other vecloop_dim values? */
211      static const int buddies[] = { 1, -1 };
212      size_t i;
213 
214      for (i = 0; i < NELEM(buddies); ++i)
215           REGISTER_SOLVER(p, mksolver(buddies[i], buddies, NELEM(buddies)));
216 }
217