indirect-transpose.c 7.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/*
 * Copyright (c) 2003, 2007-11 Matteo Frigo
 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/* solvers/plans for vectors of DFTs corresponding to the columns
   of a matrix: first transpose the matrix so that the DFTs are
   contiguous, then do DFTs with transposed output.   In particular,
   we restrict ourselves to the case of a square transpose (or a
   sequence thereof). */

#include "dft.h"

typedef solver S;

typedef struct {
     plan_dft super;
     INT vl, ivs, ovs;
     plan *cldtrans, *cld, *cldrest;
} P;

/* initial transpose is out-of-place from input to output */
static void apply_op(const plan *ego_, R *ri, R *ii, R *ro, R *io)
{
     const P *ego = (const P *) ego_;
     INT vl = ego->vl, ivs = ego->ivs, ovs = ego->ovs, i;

     for (i = 0; i < vl; ++i) {
	  {
	       plan_dft *cldtrans = (plan_dft *) ego->cldtrans;
	       cldtrans->apply(ego->cldtrans, ri, ii, ro, io);
	  }
	  {
	       plan_dft *cld = (plan_dft *) ego->cld;
	       cld->apply(ego->cld, ro, io, ro, io);
	  }
	  ri += ivs; ii += ivs;
	  ro += ovs; io += ovs;
     }
     {
	  plan_dft *cldrest = (plan_dft *) ego->cldrest;
	  cldrest->apply(ego->cldrest, ri, ii, ro, io);
     }
}

static void destroy(plan *ego_)
{
     P *ego = (P *) ego_;
     X(plan_destroy_internal)(ego->cldrest);
     X(plan_destroy_internal)(ego->cld);
     X(plan_destroy_internal)(ego->cldtrans);
}

static void awake(plan *ego_, enum wakefulness wakefulness)
{
     P *ego = (P *) ego_;
     X(plan_awake)(ego->cldtrans, wakefulness);
     X(plan_awake)(ego->cld, wakefulness);
     X(plan_awake)(ego->cldrest, wakefulness);
}

static void print(const plan *ego_, printer *p)
{
     const P *ego = (const P *) ego_;
     p->print(p, "(indirect-transpose%v%(%p%)%(%p%)%(%p%))", 
	      ego->vl, ego->cldtrans, ego->cld, ego->cldrest);
}

static int pickdim(const tensor *vs, const tensor *s, int *pdim0, int *pdim1)
{
     int dim0, dim1;
     *pdim0 = *pdim1 = -1;
     for (dim0 = 0; dim0 < vs->rnk; ++dim0)
          for (dim1 = 0; dim1 < s->rnk; ++dim1) 
	       if (vs->dims[dim0].n * X(iabs)(vs->dims[dim0].is) <= X(iabs)(s->dims[dim1].is)
		   && vs->dims[dim0].n >= s->dims[dim1].n
		   && (*pdim0 == -1 
		       || (X(iabs)(vs->dims[dim0].is) <= X(iabs)(vs->dims[*pdim0].is)
			   && X(iabs)(s->dims[dim1].is) >= X(iabs)(s->dims[*pdim1].is)))) {
		    *pdim0 = dim0;
		    *pdim1 = dim1;
	       }
     return (*pdim0 != -1 && *pdim1 != -1);
}

static int applicable0(const solver *ego_, const problem *p_,
		       const planner *plnr,
		       int *pdim0, int *pdim1)
{
     const problem_dft *p = (const problem_dft *) p_;
     UNUSED(ego_); UNUSED(plnr);

     return (1
	     && FINITE_RNK(p->vecsz->rnk) && FINITE_RNK(p->sz->rnk)

	     /* FIXME: can/should we relax this constraint? */
	     && X(tensor_inplace_strides2)(p->vecsz, p->sz)

	     && pickdim(p->vecsz, p->sz, pdim0, pdim1)

	     /* output should not *already* include the transpose
		(in which case we duplicate the regular indirect.c) */
	     && (p->sz->dims[*pdim1].os != p->vecsz->dims[*pdim0].is)
	  );
}

static int applicable(const solver *ego_, const problem *p_,
		      const planner *plnr,
		      int *pdim0, int *pdim1)
{
     if (!applicable0(ego_, p_, plnr, pdim0, pdim1)) return 0;
     {
          const problem_dft *p = (const problem_dft *) p_;
	  INT u = p->ri == p->ii + 1 || p->ii == p->ri + 1 ? (INT)2 : (INT)1;

	  /* UGLY if does not result in contiguous transforms or
	     transforms of contiguous vectors (since the latter at
	     least have efficient transpositions) */
	  if (NO_UGLYP(plnr)
	      && p->vecsz->dims[*pdim0].is != u
	      && !(p->vecsz->rnk == 2
		   && p->vecsz->dims[1-*pdim0].is == u
		   && p->vecsz->dims[*pdim0].is
		      == u * p->vecsz->dims[1-*pdim0].n))
	       return 0;

	  if (NO_INDIRECT_OP_P(plnr) && p->ri != p->ro) return 0;
     }
     return 1;
}

static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
{
     const problem_dft *p = (const problem_dft *) p_;
     P *pln;
     plan *cld = 0, *cldtrans = 0, *cldrest = 0;
     int pdim0, pdim1;
     tensor *ts, *tv;
     INT vl, ivs, ovs;
     R *rit, *iit, *rot, *iot;

     static const plan_adt padt = {
	  X(dft_solve), awake, print, destroy
     };

     if (!applicable(ego_, p_, plnr, &pdim0, &pdim1))
          return (plan *) 0;

     vl = p->vecsz->dims[pdim0].n / p->sz->dims[pdim1].n;
     A(vl >= 1);
     ivs = p->sz->dims[pdim1].n * p->vecsz->dims[pdim0].is;
     ovs = p->sz->dims[pdim1].n * p->vecsz->dims[pdim0].os;
     rit = TAINT(p->ri, vl == 1 ? 0 : ivs);
     iit = TAINT(p->ii, vl == 1 ? 0 : ivs);
     rot = TAINT(p->ro, vl == 1 ? 0 : ovs);
     iot = TAINT(p->io, vl == 1 ? 0 : ovs);

     ts = X(tensor_copy_inplace)(p->sz, INPLACE_IS);
     ts->dims[pdim1].os = p->vecsz->dims[pdim0].is;
     tv = X(tensor_copy_inplace)(p->vecsz, INPLACE_IS);
     tv->dims[pdim0].os = p->sz->dims[pdim1].is;
     tv->dims[pdim0].n = p->sz->dims[pdim1].n;
     cldtrans = X(mkplan_d)(plnr, 
			    X(mkproblem_dft_d)(X(mktensor_0d)(),
					       X(tensor_append)(tv, ts),
					       rit, iit, 
					       rot, iot));
     X(tensor_destroy2)(ts, tv);
     if (!cldtrans) goto nada;

     ts = X(tensor_copy)(p->sz);
     ts->dims[pdim1].is = p->vecsz->dims[pdim0].is;
     tv = X(tensor_copy)(p->vecsz);
     tv->dims[pdim0].is = p->sz->dims[pdim1].is;
     tv->dims[pdim0].n = p->sz->dims[pdim1].n;
     cld = X(mkplan_d)(plnr, X(mkproblem_dft_d)(ts, tv,
						rot, iot,
						rot, iot));
     if (!cld) goto nada;

     tv = X(tensor_copy)(p->vecsz);
     tv->dims[pdim0].n -= vl * p->sz->dims[pdim1].n;
     cldrest = X(mkplan_d)(plnr, X(mkproblem_dft_d)(X(tensor_copy)(p->sz), tv,
						    p->ri + ivs * vl,
						    p->ii + ivs * vl,
						    p->ro + ovs * vl,
						    p->io + ovs * vl));
     if (!cldrest) goto nada;

     pln = MKPLAN_DFT(P, &padt, apply_op);
     pln->cldtrans = cldtrans;
     pln->cld = cld;
     pln->cldrest = cldrest;
     pln->vl = vl;
     pln->ivs = ivs;
     pln->ovs = ovs;
     X(ops_cpy)(&cldrest->ops, &pln->super.super.ops);
     X(ops_madd2)(vl, &cld->ops, &pln->super.super.ops);
     X(ops_madd2)(vl, &cldtrans->ops, &pln->super.super.ops);
     return &(pln->super.super);

 nada:
     X(plan_destroy_internal)(cldrest);
     X(plan_destroy_internal)(cld);
     X(plan_destroy_internal)(cldtrans);
     return (plan *)0;
}

static solver *mksolver(void)
{
     static const solver_adt sadt = { PROBLEM_DFT, mkplan, 0 };
     S *slv = MKSOLVER(S, &sadt);
     return slv;
}

void X(dft_indirect_transpose_register)(planner *p)
{
     REGISTER_SOLVER(p, mksolver());
}