annotate src/fftw-3.3.3/rdft/vrank3-transpose.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 89f5e221ed7b
children
rev   line source
cannam@95 1 /*
cannam@95 2 * Copyright (c) 2003, 2007-11 Matteo Frigo
cannam@95 3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
cannam@95 4 *
cannam@95 5 * This program is free software; you can redistribute it and/or modify
cannam@95 6 * it under the terms of the GNU General Public License as published by
cannam@95 7 * the Free Software Foundation; either version 2 of the License, or
cannam@95 8 * (at your option) any later version.
cannam@95 9 *
cannam@95 10 * This program is distributed in the hope that it will be useful,
cannam@95 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
cannam@95 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cannam@95 13 * GNU General Public License for more details.
cannam@95 14 *
cannam@95 15 * You should have received a copy of the GNU General Public License
cannam@95 16 * along with this program; if not, write to the Free Software
cannam@95 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
cannam@95 18 *
cannam@95 19 */
cannam@95 20
cannam@95 21
cannam@95 22 /* rank-0, vector-rank-3, non-square in-place transposition
cannam@95 23 (see rank0.c for square transposition) */
cannam@95 24
cannam@95 25 #include "rdft.h"
cannam@95 26
cannam@95 27 #ifdef HAVE_STRING_H
cannam@95 28 #include <string.h> /* for memcpy() */
cannam@95 29 #endif
cannam@95 30
cannam@95 31 struct P_s;
cannam@95 32
cannam@95 33 typedef struct {
cannam@95 34 rdftapply apply;
cannam@95 35 int (*applicable)(const problem_rdft *p, planner *plnr,
cannam@95 36 int dim0, int dim1, int dim2, INT *nbuf);
cannam@95 37 int (*mkcldrn)(const problem_rdft *p, planner *plnr, struct P_s *ego);
cannam@95 38 const char *nam;
cannam@95 39 } transpose_adt;
cannam@95 40
cannam@95 41 typedef struct {
cannam@95 42 solver super;
cannam@95 43 const transpose_adt *adt;
cannam@95 44 } S;
cannam@95 45
cannam@95 46 typedef struct P_s {
cannam@95 47 plan_rdft super;
cannam@95 48 INT n, m, vl; /* transpose n x m matrix of vl-tuples */
cannam@95 49 INT nbuf; /* buffer size */
cannam@95 50 INT nd, md, d; /* transpose-gcd params */
cannam@95 51 INT nc, mc; /* transpose-cut params */
cannam@95 52 plan *cld1, *cld2, *cld3; /* children, null if unused */
cannam@95 53 const S *slv;
cannam@95 54 } P;
cannam@95 55
cannam@95 56
cannam@95 57 /*************************************************************************/
cannam@95 58 /* some utilities for the solvers */
cannam@95 59
cannam@95 60 static INT gcd(INT a, INT b)
cannam@95 61 {
cannam@95 62 INT r;
cannam@95 63 do {
cannam@95 64 r = a % b;
cannam@95 65 a = b;
cannam@95 66 b = r;
cannam@95 67 } while (r != 0);
cannam@95 68
cannam@95 69 return a;
cannam@95 70 }
cannam@95 71
cannam@95 72 /* whether we can transpose with one of our routines expecting
cannam@95 73 contiguous Ntuples */
cannam@95 74 static int Ntuple_transposable(const iodim *a, const iodim *b, INT vl, INT vs)
cannam@95 75 {
cannam@95 76 return (vs == 1 && b->is == vl && a->os == vl &&
cannam@95 77 ((a->n == b->n && a->is == b->os
cannam@95 78 && a->is >= b->n && a->is % vl == 0)
cannam@95 79 || (a->is == b->n * vl && b->os == a->n * vl)));
cannam@95 80 }
cannam@95 81
cannam@95 82 /* check whether a and b correspond to the first and second dimensions
cannam@95 83 of a transpose of tuples with vector length = vl, stride = vs. */
cannam@95 84 static int transposable(const iodim *a, const iodim *b, INT vl, INT vs)
cannam@95 85 {
cannam@95 86 return ((a->n == b->n && a->os == b->is && a->is == b->os)
cannam@95 87 || Ntuple_transposable(a, b, vl, vs));
cannam@95 88 }
cannam@95 89
cannam@95 90 static int pickdim(const tensor *s, int *pdim0, int *pdim1, int *pdim2)
cannam@95 91 {
cannam@95 92 int dim0, dim1;
cannam@95 93
cannam@95 94 for (dim0 = 0; dim0 < s->rnk; ++dim0)
cannam@95 95 for (dim1 = 0; dim1 < s->rnk; ++dim1) {
cannam@95 96 int dim2 = 3 - dim0 - dim1;
cannam@95 97 if (dim0 == dim1) continue;
cannam@95 98 if ((s->rnk == 2 || s->dims[dim2].is == s->dims[dim2].os)
cannam@95 99 && transposable(s->dims + dim0, s->dims + dim1,
cannam@95 100 s->rnk == 2 ? (INT)1 : s->dims[dim2].n,
cannam@95 101 s->rnk == 2 ? (INT)1 : s->dims[dim2].is)) {
cannam@95 102 *pdim0 = dim0;
cannam@95 103 *pdim1 = dim1;
cannam@95 104 *pdim2 = dim2;
cannam@95 105 return 1;
cannam@95 106 }
cannam@95 107 }
cannam@95 108 return 0;
cannam@95 109 }
cannam@95 110
cannam@95 111 #define MINBUFDIV 9 /* min factor by which buffer is smaller than data */
cannam@95 112 #define MAXBUF 65536 /* maximum non-ugly buffer */
cannam@95 113
cannam@95 114 /* generic applicability function */
cannam@95 115 static int applicable(const solver *ego_, const problem *p_, planner *plnr,
cannam@95 116 int *dim0, int *dim1, int *dim2, INT *nbuf)
cannam@95 117 {
cannam@95 118 const S *ego = (const S *) ego_;
cannam@95 119 const problem_rdft *p = (const problem_rdft *) p_;
cannam@95 120
cannam@95 121 return (1
cannam@95 122 && p->I == p->O
cannam@95 123 && p->sz->rnk == 0
cannam@95 124 && (p->vecsz->rnk == 2 || p->vecsz->rnk == 3)
cannam@95 125
cannam@95 126 && pickdim(p->vecsz, dim0, dim1, dim2)
cannam@95 127
cannam@95 128 /* UGLY if vecloop in wrong order for locality */
cannam@95 129 && (!NO_UGLYP(plnr) ||
cannam@95 130 p->vecsz->rnk == 2 ||
cannam@95 131 X(iabs)(p->vecsz->dims[*dim2].is)
cannam@95 132 < X(imax)(X(iabs)(p->vecsz->dims[*dim0].is),
cannam@95 133 X(iabs)(p->vecsz->dims[*dim0].os)))
cannam@95 134
cannam@95 135 /* SLOW if non-square */
cannam@95 136 && (!NO_SLOWP(plnr)
cannam@95 137 || p->vecsz->dims[*dim0].n == p->vecsz->dims[*dim1].n)
cannam@95 138
cannam@95 139 && ego->adt->applicable(p, plnr, *dim0,*dim1,*dim2,nbuf)
cannam@95 140
cannam@95 141 /* buffers too big are UGLY */
cannam@95 142 && ((!NO_UGLYP(plnr) && !CONSERVE_MEMORYP(plnr))
cannam@95 143 || *nbuf <= MAXBUF
cannam@95 144 || *nbuf * MINBUFDIV <= X(tensor_sz)(p->vecsz))
cannam@95 145 );
cannam@95 146 }
cannam@95 147
cannam@95 148 static void get_transpose_vec(const problem_rdft *p, int dim2, INT *vl,INT *vs)
cannam@95 149 {
cannam@95 150 if (p->vecsz->rnk == 2) {
cannam@95 151 *vl = 1; *vs = 1;
cannam@95 152 }
cannam@95 153 else {
cannam@95 154 *vl = p->vecsz->dims[dim2].n;
cannam@95 155 *vs = p->vecsz->dims[dim2].is; /* == os */
cannam@95 156 }
cannam@95 157 }
cannam@95 158
cannam@95 159 /*************************************************************************/
cannam@95 160 /* Cache-oblivious in-place transpose of non-square matrices, based
cannam@95 161 on transposes of blocks given by the gcd of the dimensions.
cannam@95 162
cannam@95 163 This algorithm is related to algorithm V5 from Murray Dow,
cannam@95 164 "Transposing a matrix on a vector computer," Parallel Computing 21
cannam@95 165 (12), 1997-2005 (1995), with the modification that we use
cannam@95 166 cache-oblivious recursive transpose subroutines (and we derived
cannam@95 167 it independently).
cannam@95 168
cannam@95 169 For a p x q matrix, this requires scratch space equal to the size
cannam@95 170 of the matrix divided by gcd(p,q). Alternatively, see also the
cannam@95 171 "cut" algorithm below, if |p-q| * gcd(p,q) < max(p,q). */
cannam@95 172
cannam@95 173 static void apply_gcd(const plan *ego_, R *I, R *O)
cannam@95 174 {
cannam@95 175 const P *ego = (const P *) ego_;
cannam@95 176 INT n = ego->nd, m = ego->md, d = ego->d;
cannam@95 177 INT vl = ego->vl;
cannam@95 178 R *buf = (R *)MALLOC(sizeof(R) * ego->nbuf, BUFFERS);
cannam@95 179 INT i, num_el = n*m*d*vl;
cannam@95 180
cannam@95 181 A(ego->n == n * d && ego->m == m * d);
cannam@95 182 UNUSED(O);
cannam@95 183
cannam@95 184 /* Transpose the matrix I in-place, where I is an (n*d) x (m*d) matrix
cannam@95 185 of vl-tuples and buf contains n*m*d*vl elements.
cannam@95 186
cannam@95 187 In general, to transpose a p x q matrix, you should call this
cannam@95 188 routine with d = gcd(p, q), n = p/d, and m = q/d. */
cannam@95 189
cannam@95 190 A(n > 0 && m > 0 && vl > 0);
cannam@95 191 A(d > 1);
cannam@95 192
cannam@95 193 /* treat as (d x n) x (d' x m) matrix. (d' = d) */
cannam@95 194
cannam@95 195 /* First, transpose d x (n x d') x m to d x (d' x n) x m,
cannam@95 196 using the buf matrix. This consists of d transposes
cannam@95 197 of contiguous n x d' matrices of m-tuples. */
cannam@95 198 if (n > 1) {
cannam@95 199 rdftapply cldapply = ((plan_rdft *) ego->cld1)->apply;
cannam@95 200 for (i = 0; i < d; ++i) {
cannam@95 201 cldapply(ego->cld1, I + i*num_el, buf);
cannam@95 202 memcpy(I + i*num_el, buf, num_el*sizeof(R));
cannam@95 203 }
cannam@95 204 }
cannam@95 205
cannam@95 206 /* Now, transpose (d x d') x (n x m) to (d' x d) x (n x m), which
cannam@95 207 is a square in-place transpose of n*m-tuples: */
cannam@95 208 {
cannam@95 209 rdftapply cldapply = ((plan_rdft *) ego->cld2)->apply;
cannam@95 210 cldapply(ego->cld2, I, I);
cannam@95 211 }
cannam@95 212
cannam@95 213 /* Finally, transpose d' x ((d x n) x m) to d' x (m x (d x n)),
cannam@95 214 using the buf matrix. This consists of d' transposes
cannam@95 215 of contiguous d*n x m matrices. */
cannam@95 216 if (m > 1) {
cannam@95 217 rdftapply cldapply = ((plan_rdft *) ego->cld3)->apply;
cannam@95 218 for (i = 0; i < d; ++i) {
cannam@95 219 cldapply(ego->cld3, I + i*num_el, buf);
cannam@95 220 memcpy(I + i*num_el, buf, num_el*sizeof(R));
cannam@95 221 }
cannam@95 222 }
cannam@95 223
cannam@95 224 X(ifree)(buf);
cannam@95 225 }
cannam@95 226
cannam@95 227 static int applicable_gcd(const problem_rdft *p, planner *plnr,
cannam@95 228 int dim0, int dim1, int dim2, INT *nbuf)
cannam@95 229 {
cannam@95 230 INT n = p->vecsz->dims[dim0].n;
cannam@95 231 INT m = p->vecsz->dims[dim1].n;
cannam@95 232 INT d, vl, vs;
cannam@95 233 get_transpose_vec(p, dim2, &vl, &vs);
cannam@95 234 d = gcd(n, m);
cannam@95 235 *nbuf = n * (m / d) * vl;
cannam@95 236 return (!NO_SLOWP(plnr) /* FIXME: not really SLOW for large 1d ffts */
cannam@95 237 && n != m
cannam@95 238 && d > 1
cannam@95 239 && Ntuple_transposable(p->vecsz->dims + dim0,
cannam@95 240 p->vecsz->dims + dim1,
cannam@95 241 vl, vs));
cannam@95 242 }
cannam@95 243
cannam@95 244 static int mkcldrn_gcd(const problem_rdft *p, planner *plnr, P *ego)
cannam@95 245 {
cannam@95 246 INT n = ego->nd, m = ego->md, d = ego->d;
cannam@95 247 INT vl = ego->vl;
cannam@95 248 R *buf = (R *)MALLOC(sizeof(R) * ego->nbuf, BUFFERS);
cannam@95 249 INT num_el = n*m*d*vl;
cannam@95 250
cannam@95 251 if (n > 1) {
cannam@95 252 ego->cld1 = X(mkplan_d)(plnr,
cannam@95 253 X(mkproblem_rdft_0_d)(
cannam@95 254 X(mktensor_3d)(n, d*m*vl, m*vl,
cannam@95 255 d, m*vl, n*m*vl,
cannam@95 256 m*vl, 1, 1),
cannam@95 257 TAINT(p->I, num_el), buf));
cannam@95 258 if (!ego->cld1)
cannam@95 259 goto nada;
cannam@95 260 X(ops_madd)(d, &ego->cld1->ops, &ego->super.super.ops,
cannam@95 261 &ego->super.super.ops);
cannam@95 262 ego->super.super.ops.other += num_el * d * 2;
cannam@95 263 }
cannam@95 264
cannam@95 265 ego->cld2 = X(mkplan_d)(plnr,
cannam@95 266 X(mkproblem_rdft_0_d)(
cannam@95 267 X(mktensor_3d)(d, d*n*m*vl, n*m*vl,
cannam@95 268 d, n*m*vl, d*n*m*vl,
cannam@95 269 n*m*vl, 1, 1),
cannam@95 270 p->I, p->I));
cannam@95 271 if (!ego->cld2)
cannam@95 272 goto nada;
cannam@95 273 X(ops_add2)(&ego->cld2->ops, &ego->super.super.ops);
cannam@95 274
cannam@95 275 if (m > 1) {
cannam@95 276 ego->cld3 = X(mkplan_d)(plnr,
cannam@95 277 X(mkproblem_rdft_0_d)(
cannam@95 278 X(mktensor_3d)(d*n, m*vl, vl,
cannam@95 279 m, vl, d*n*vl,
cannam@95 280 vl, 1, 1),
cannam@95 281 TAINT(p->I, num_el), buf));
cannam@95 282 if (!ego->cld3)
cannam@95 283 goto nada;
cannam@95 284 X(ops_madd2)(d, &ego->cld3->ops, &ego->super.super.ops);
cannam@95 285 ego->super.super.ops.other += num_el * d * 2;
cannam@95 286 }
cannam@95 287
cannam@95 288 X(ifree)(buf);
cannam@95 289 return 1;
cannam@95 290
cannam@95 291 nada:
cannam@95 292 X(ifree)(buf);
cannam@95 293 return 0;
cannam@95 294 }
cannam@95 295
cannam@95 296 static const transpose_adt adt_gcd =
cannam@95 297 {
cannam@95 298 apply_gcd, applicable_gcd, mkcldrn_gcd,
cannam@95 299 "rdft-transpose-gcd"
cannam@95 300 };
cannam@95 301
cannam@95 302 /*************************************************************************/
cannam@95 303 /* Cache-oblivious in-place transpose of non-square n x m matrices,
cannam@95 304 based on transposing a sub-matrix first and then transposing the
cannam@95 305 remainder(s) with the help of a buffer. See also transpose-gcd,
cannam@95 306 above, if gcd(n,m) is large.
cannam@95 307
cannam@95 308 This algorithm is related to algorithm V3 from Murray Dow,
cannam@95 309 "Transposing a matrix on a vector computer," Parallel Computing 21
cannam@95 310 (12), 1997-2005 (1995), with the modifications that we use
cannam@95 311 cache-oblivious recursive transpose subroutines and we have the
cannam@95 312 generalization for large |n-m| below.
cannam@95 313
cannam@95 314 The best case, and the one described by Dow, is for |n-m| small, in
cannam@95 315 which case we transpose a square sub-matrix of size min(n,m),
cannam@95 316 handling the remainder via a buffer. This requires scratch space
cannam@95 317 equal to the size of the matrix times |n-m| / max(n,m).
cannam@95 318
cannam@95 319 As a generalization when |n-m| is not small, we also support cutting
cannam@95 320 *both* dimensions to an nc x mc matrix which is *not* necessarily
cannam@95 321 square, but has a large gcd (and can therefore use transpose-gcd).
cannam@95 322 */
cannam@95 323
cannam@95 324 static void apply_cut(const plan *ego_, R *I, R *O)
cannam@95 325 {
cannam@95 326 const P *ego = (const P *) ego_;
cannam@95 327 INT n = ego->n, m = ego->m, nc = ego->nc, mc = ego->mc, vl = ego->vl;
cannam@95 328 INT i;
cannam@95 329 R *buf1 = (R *)MALLOC(sizeof(R) * ego->nbuf, BUFFERS);
cannam@95 330 UNUSED(O);
cannam@95 331
cannam@95 332 if (m > mc) {
cannam@95 333 ((plan_rdft *) ego->cld1)->apply(ego->cld1, I + mc*vl, buf1);
cannam@95 334 for (i = 0; i < nc; ++i)
cannam@95 335 memmove(I + (mc*vl) * i, I + (m*vl) * i, sizeof(R) * (mc*vl));
cannam@95 336 }
cannam@95 337
cannam@95 338 ((plan_rdft *) ego->cld2)->apply(ego->cld2, I, I); /* nc x mc transpose */
cannam@95 339
cannam@95 340 if (n > nc) {
cannam@95 341 R *buf2 = buf1 + (m-mc)*(nc*vl); /* FIXME: force better alignment? */
cannam@95 342 memcpy(buf2, I + nc*(m*vl), (n-nc)*(m*vl)*sizeof(R));
cannam@95 343 for (i = mc-1; i >= 0; --i)
cannam@95 344 memmove(I + (n*vl) * i, I + (nc*vl) * i, sizeof(R) * (n*vl));
cannam@95 345 ((plan_rdft *) ego->cld3)->apply(ego->cld3, buf2, I + nc*vl);
cannam@95 346 }
cannam@95 347
cannam@95 348 if (m > mc) {
cannam@95 349 if (n > nc)
cannam@95 350 for (i = mc; i < m; ++i)
cannam@95 351 memcpy(I + i*(n*vl), buf1 + (i-mc)*(nc*vl),
cannam@95 352 (nc*vl)*sizeof(R));
cannam@95 353 else
cannam@95 354 memcpy(I + mc*(n*vl), buf1, (m-mc)*(n*vl)*sizeof(R));
cannam@95 355 }
cannam@95 356
cannam@95 357 X(ifree)(buf1);
cannam@95 358 }
cannam@95 359
cannam@95 360 /* only cut one dimension if the resulting buffer is small enough */
cannam@95 361 static int cut1(INT n, INT m, INT vl)
cannam@95 362 {
cannam@95 363 return (X(imax)(n,m) >= X(iabs)(n-m) * MINBUFDIV
cannam@95 364 || X(imin)(n,m) * X(iabs)(n-m) * vl <= MAXBUF);
cannam@95 365 }
cannam@95 366
cannam@95 367 #define CUT_NSRCH 32 /* range of sizes to search for possible cuts */
cannam@95 368
cannam@95 369 static int applicable_cut(const problem_rdft *p, planner *plnr,
cannam@95 370 int dim0, int dim1, int dim2, INT *nbuf)
cannam@95 371 {
cannam@95 372 INT n = p->vecsz->dims[dim0].n;
cannam@95 373 INT m = p->vecsz->dims[dim1].n;
cannam@95 374 INT vl, vs;
cannam@95 375 get_transpose_vec(p, dim2, &vl, &vs);
cannam@95 376 *nbuf = 0; /* always small enough to be non-UGLY (?) */
cannam@95 377 A(MINBUFDIV <= CUT_NSRCH); /* assumed to avoid inf. loops below */
cannam@95 378 return (!NO_SLOWP(plnr) /* FIXME: not really SLOW for large 1d ffts? */
cannam@95 379 && n != m
cannam@95 380
cannam@95 381 /* Don't call transpose-cut recursively (avoid inf. loops):
cannam@95 382 the non-square sub-transpose produced when !cut1
cannam@95 383 should always have gcd(n,m) >= min(CUT_NSRCH,n,m),
cannam@95 384 for which transpose-gcd is applicable */
cannam@95 385 && (cut1(n, m, vl)
cannam@95 386 || gcd(n, m) < X(imin)(MINBUFDIV, X(imin)(n,m)))
cannam@95 387
cannam@95 388 && Ntuple_transposable(p->vecsz->dims + dim0,
cannam@95 389 p->vecsz->dims + dim1,
cannam@95 390 vl, vs));
cannam@95 391 }
cannam@95 392
cannam@95 393 static int mkcldrn_cut(const problem_rdft *p, planner *plnr, P *ego)
cannam@95 394 {
cannam@95 395 INT n = ego->n, m = ego->m, nc, mc;
cannam@95 396 INT vl = ego->vl;
cannam@95 397 R *buf;
cannam@95 398
cannam@95 399 /* pick the "best" cut */
cannam@95 400 if (cut1(n, m, vl)) {
cannam@95 401 nc = mc = X(imin)(n,m);
cannam@95 402 }
cannam@95 403 else {
cannam@95 404 INT dc, ns, ms;
cannam@95 405 dc = gcd(m, n); nc = n; mc = m;
cannam@95 406 /* search for cut with largest gcd
cannam@95 407 (TODO: different optimality criteria? different search range?) */
cannam@95 408 for (ms = m; ms > 0 && ms > m - CUT_NSRCH; --ms) {
cannam@95 409 for (ns = n; ns > 0 && ns > n - CUT_NSRCH; --ns) {
cannam@95 410 INT ds = gcd(ms, ns);
cannam@95 411 if (ds > dc) {
cannam@95 412 dc = ds; nc = ns; mc = ms;
cannam@95 413 if (dc == X(imin)(ns, ms))
cannam@95 414 break; /* cannot get larger than this */
cannam@95 415 }
cannam@95 416 }
cannam@95 417 if (dc == X(imin)(n, ms))
cannam@95 418 break; /* cannot get larger than this */
cannam@95 419 }
cannam@95 420 A(dc >= X(imin)(CUT_NSRCH, X(imin)(n, m)));
cannam@95 421 }
cannam@95 422 ego->nc = nc;
cannam@95 423 ego->mc = mc;
cannam@95 424 ego->nbuf = (m-mc)*(nc*vl) + (n-nc)*(m*vl);
cannam@95 425
cannam@95 426 buf = (R *)MALLOC(sizeof(R) * ego->nbuf, BUFFERS);
cannam@95 427
cannam@95 428 if (m > mc) {
cannam@95 429 ego->cld1 = X(mkplan_d)(plnr,
cannam@95 430 X(mkproblem_rdft_0_d)(
cannam@95 431 X(mktensor_3d)(nc, m*vl, vl,
cannam@95 432 m-mc, vl, nc*vl,
cannam@95 433 vl, 1, 1),
cannam@95 434 p->I + mc*vl, buf));
cannam@95 435 if (!ego->cld1)
cannam@95 436 goto nada;
cannam@95 437 X(ops_add2)(&ego->cld1->ops, &ego->super.super.ops);
cannam@95 438 }
cannam@95 439
cannam@95 440 ego->cld2 = X(mkplan_d)(plnr,
cannam@95 441 X(mkproblem_rdft_0_d)(
cannam@95 442 X(mktensor_3d)(nc, mc*vl, vl,
cannam@95 443 mc, vl, nc*vl,
cannam@95 444 vl, 1, 1),
cannam@95 445 p->I, p->I));
cannam@95 446 if (!ego->cld2)
cannam@95 447 goto nada;
cannam@95 448 X(ops_add2)(&ego->cld2->ops, &ego->super.super.ops);
cannam@95 449
cannam@95 450 if (n > nc) {
cannam@95 451 ego->cld3 = X(mkplan_d)(plnr,
cannam@95 452 X(mkproblem_rdft_0_d)(
cannam@95 453 X(mktensor_3d)(n-nc, m*vl, vl,
cannam@95 454 m, vl, n*vl,
cannam@95 455 vl, 1, 1),
cannam@95 456 buf + (m-mc)*(nc*vl), p->I + nc*vl));
cannam@95 457 if (!ego->cld3)
cannam@95 458 goto nada;
cannam@95 459 X(ops_add2)(&ego->cld3->ops, &ego->super.super.ops);
cannam@95 460 }
cannam@95 461
cannam@95 462 /* memcpy/memmove operations */
cannam@95 463 ego->super.super.ops.other += 2 * vl * (nc*mc * ((m > mc) + (n > nc))
cannam@95 464 + (n-nc)*m + (m-mc)*nc);
cannam@95 465
cannam@95 466 X(ifree)(buf);
cannam@95 467 return 1;
cannam@95 468
cannam@95 469 nada:
cannam@95 470 X(ifree)(buf);
cannam@95 471 return 0;
cannam@95 472 }
cannam@95 473
cannam@95 474 static const transpose_adt adt_cut =
cannam@95 475 {
cannam@95 476 apply_cut, applicable_cut, mkcldrn_cut,
cannam@95 477 "rdft-transpose-cut"
cannam@95 478 };
cannam@95 479
cannam@95 480 /*************************************************************************/
cannam@95 481 /* In-place transpose routine from TOMS, which follows the cycles of
cannam@95 482 the permutation so that it writes to each location only once.
cannam@95 483 Because of cache-line and other issues, however, this routine is
cannam@95 484 typically much slower than transpose-gcd or transpose-cut, even
cannam@95 485 though the latter do some extra writes. On the other hand, if the
cannam@95 486 vector length is large then the TOMS routine is best.
cannam@95 487
cannam@95 488 The TOMS routine also has the advantage of requiring less buffer
cannam@95 489 space for the case of gcd(nx,ny) small. However, in this case it
cannam@95 490 has been superseded by the combination of the generalized
cannam@95 491 transpose-cut method with the transpose-gcd method, which can
cannam@95 492 always transpose with buffers a small fraction of the array size
cannam@95 493 regardless of gcd(nx,ny). */
cannam@95 494
cannam@95 495 /*
cannam@95 496 * TOMS Transpose. Algorithm 513 (Revised version of algorithm 380).
cannam@95 497 *
cannam@95 498 * These routines do in-place transposes of arrays.
cannam@95 499 *
cannam@95 500 * [ Cate, E.G. and Twigg, D.W., ACM Transactions on Mathematical Software,
cannam@95 501 * vol. 3, no. 1, 104-110 (1977) ]
cannam@95 502 *
cannam@95 503 * C version by Steven G. Johnson (February 1997).
cannam@95 504 */
cannam@95 505
cannam@95 506 /*
cannam@95 507 * "a" is a 1D array of length ny*nx*N which constains the nx x ny
cannam@95 508 * matrix of N-tuples to be transposed. "a" is stored in row-major
cannam@95 509 * order (last index varies fastest). move is a 1D array of length
cannam@95 510 * move_size used to store information to speed up the process. The
cannam@95 511 * value move_size=(ny+nx)/2 is recommended. buf should be an array
cannam@95 512 * of length 2*N.
cannam@95 513 *
cannam@95 514 */
cannam@95 515
cannam@95 516 static void transpose_toms513(R *a, INT nx, INT ny, INT N,
cannam@95 517 char *move, INT move_size, R *buf)
cannam@95 518 {
cannam@95 519 INT i, im, mn;
cannam@95 520 R *b, *c, *d;
cannam@95 521 INT ncount;
cannam@95 522 INT k;
cannam@95 523
cannam@95 524 /* check arguments and initialize: */
cannam@95 525 A(ny > 0 && nx > 0 && N > 0 && move_size > 0);
cannam@95 526
cannam@95 527 b = buf;
cannam@95 528
cannam@95 529 /* Cate & Twigg have a special case for nx == ny, but we don't
cannam@95 530 bother, since we already have special code for this case elsewhere. */
cannam@95 531
cannam@95 532 c = buf + N;
cannam@95 533 ncount = 2; /* always at least 2 fixed points */
cannam@95 534 k = (mn = ny * nx) - 1;
cannam@95 535
cannam@95 536 for (i = 0; i < move_size; ++i)
cannam@95 537 move[i] = 0;
cannam@95 538
cannam@95 539 if (ny >= 3 && nx >= 3)
cannam@95 540 ncount += gcd(ny - 1, nx - 1) - 1; /* # fixed points */
cannam@95 541
cannam@95 542 i = 1;
cannam@95 543 im = ny;
cannam@95 544
cannam@95 545 while (1) {
cannam@95 546 INT i1, i2, i1c, i2c;
cannam@95 547 INT kmi;
cannam@95 548
cannam@95 549 /** Rearrange the elements of a loop
cannam@95 550 and its companion loop: **/
cannam@95 551
cannam@95 552 i1 = i;
cannam@95 553 kmi = k - i;
cannam@95 554 i1c = kmi;
cannam@95 555 switch (N) {
cannam@95 556 case 1:
cannam@95 557 b[0] = a[i1];
cannam@95 558 c[0] = a[i1c];
cannam@95 559 break;
cannam@95 560 case 2:
cannam@95 561 b[0] = a[2*i1];
cannam@95 562 b[1] = a[2*i1+1];
cannam@95 563 c[0] = a[2*i1c];
cannam@95 564 c[1] = a[2*i1c+1];
cannam@95 565 break;
cannam@95 566 default:
cannam@95 567 memcpy(b, &a[N * i1], N * sizeof(R));
cannam@95 568 memcpy(c, &a[N * i1c], N * sizeof(R));
cannam@95 569 }
cannam@95 570 while (1) {
cannam@95 571 i2 = ny * i1 - k * (i1 / nx);
cannam@95 572 i2c = k - i2;
cannam@95 573 if (i1 < move_size)
cannam@95 574 move[i1] = 1;
cannam@95 575 if (i1c < move_size)
cannam@95 576 move[i1c] = 1;
cannam@95 577 ncount += 2;
cannam@95 578 if (i2 == i)
cannam@95 579 break;
cannam@95 580 if (i2 == kmi) {
cannam@95 581 d = b;
cannam@95 582 b = c;
cannam@95 583 c = d;
cannam@95 584 break;
cannam@95 585 }
cannam@95 586 switch (N) {
cannam@95 587 case 1:
cannam@95 588 a[i1] = a[i2];
cannam@95 589 a[i1c] = a[i2c];
cannam@95 590 break;
cannam@95 591 case 2:
cannam@95 592 a[2*i1] = a[2*i2];
cannam@95 593 a[2*i1+1] = a[2*i2+1];
cannam@95 594 a[2*i1c] = a[2*i2c];
cannam@95 595 a[2*i1c+1] = a[2*i2c+1];
cannam@95 596 break;
cannam@95 597 default:
cannam@95 598 memcpy(&a[N * i1], &a[N * i2],
cannam@95 599 N * sizeof(R));
cannam@95 600 memcpy(&a[N * i1c], &a[N * i2c],
cannam@95 601 N * sizeof(R));
cannam@95 602 }
cannam@95 603 i1 = i2;
cannam@95 604 i1c = i2c;
cannam@95 605 }
cannam@95 606 switch (N) {
cannam@95 607 case 1:
cannam@95 608 a[i1] = b[0];
cannam@95 609 a[i1c] = c[0];
cannam@95 610 break;
cannam@95 611 case 2:
cannam@95 612 a[2*i1] = b[0];
cannam@95 613 a[2*i1+1] = b[1];
cannam@95 614 a[2*i1c] = c[0];
cannam@95 615 a[2*i1c+1] = c[1];
cannam@95 616 break;
cannam@95 617 default:
cannam@95 618 memcpy(&a[N * i1], b, N * sizeof(R));
cannam@95 619 memcpy(&a[N * i1c], c, N * sizeof(R));
cannam@95 620 }
cannam@95 621 if (ncount >= mn)
cannam@95 622 break; /* we've moved all elements */
cannam@95 623
cannam@95 624 /** Search for loops to rearrange: **/
cannam@95 625
cannam@95 626 while (1) {
cannam@95 627 INT max = k - i;
cannam@95 628 ++i;
cannam@95 629 A(i <= max);
cannam@95 630 im += ny;
cannam@95 631 if (im > k)
cannam@95 632 im -= k;
cannam@95 633 i2 = im;
cannam@95 634 if (i == i2)
cannam@95 635 continue;
cannam@95 636 if (i >= move_size) {
cannam@95 637 while (i2 > i && i2 < max) {
cannam@95 638 i1 = i2;
cannam@95 639 i2 = ny * i1 - k * (i1 / nx);
cannam@95 640 }
cannam@95 641 if (i2 == i)
cannam@95 642 break;
cannam@95 643 } else if (!move[i])
cannam@95 644 break;
cannam@95 645 }
cannam@95 646 }
cannam@95 647 }
cannam@95 648
cannam@95 649 static void apply_toms513(const plan *ego_, R *I, R *O)
cannam@95 650 {
cannam@95 651 const P *ego = (const P *) ego_;
cannam@95 652 INT n = ego->n, m = ego->m;
cannam@95 653 INT vl = ego->vl;
cannam@95 654 R *buf = (R *)MALLOC(sizeof(R) * ego->nbuf, BUFFERS);
cannam@95 655 UNUSED(O);
cannam@95 656 transpose_toms513(I, n, m, vl, (char *) (buf + 2*vl), (n+m)/2, buf);
cannam@95 657 X(ifree)(buf);
cannam@95 658 }
cannam@95 659
cannam@95 660 static int applicable_toms513(const problem_rdft *p, planner *plnr,
cannam@95 661 int dim0, int dim1, int dim2, INT *nbuf)
cannam@95 662 {
cannam@95 663 INT n = p->vecsz->dims[dim0].n;
cannam@95 664 INT m = p->vecsz->dims[dim1].n;
cannam@95 665 INT vl, vs;
cannam@95 666 get_transpose_vec(p, dim2, &vl, &vs);
cannam@95 667 *nbuf = 2*vl
cannam@95 668 + ((n + m) / 2 * sizeof(char) + sizeof(R) - 1) / sizeof(R);
cannam@95 669 return (!NO_SLOWP(plnr)
cannam@95 670 && (vl > 8 || !NO_UGLYP(plnr)) /* UGLY for small vl */
cannam@95 671 && n != m
cannam@95 672 && Ntuple_transposable(p->vecsz->dims + dim0,
cannam@95 673 p->vecsz->dims + dim1,
cannam@95 674 vl, vs));
cannam@95 675 }
cannam@95 676
cannam@95 677 static int mkcldrn_toms513(const problem_rdft *p, planner *plnr, P *ego)
cannam@95 678 {
cannam@95 679 UNUSED(p); UNUSED(plnr);
cannam@95 680 /* heuristic so that TOMS algorithm is last resort for small vl */
cannam@95 681 ego->super.super.ops.other += ego->n * ego->m * 2 * (ego->vl + 30);
cannam@95 682 return 1;
cannam@95 683 }
cannam@95 684
cannam@95 685 static const transpose_adt adt_toms513 =
cannam@95 686 {
cannam@95 687 apply_toms513, applicable_toms513, mkcldrn_toms513,
cannam@95 688 "rdft-transpose-toms513"
cannam@95 689 };
cannam@95 690
cannam@95 691 /*-----------------------------------------------------------------------*/
cannam@95 692 /*-----------------------------------------------------------------------*/
cannam@95 693 /* generic stuff: */
cannam@95 694
cannam@95 695 static void awake(plan *ego_, enum wakefulness wakefulness)
cannam@95 696 {
cannam@95 697 P *ego = (P *) ego_;
cannam@95 698 X(plan_awake)(ego->cld1, wakefulness);
cannam@95 699 X(plan_awake)(ego->cld2, wakefulness);
cannam@95 700 X(plan_awake)(ego->cld3, wakefulness);
cannam@95 701 }
cannam@95 702
cannam@95 703 static void print(const plan *ego_, printer *p)
cannam@95 704 {
cannam@95 705 const P *ego = (const P *) ego_;
cannam@95 706 p->print(p, "(%s-%Dx%D%v", ego->slv->adt->nam,
cannam@95 707 ego->n, ego->m, ego->vl);
cannam@95 708 if (ego->cld1) p->print(p, "%(%p%)", ego->cld1);
cannam@95 709 if (ego->cld2) p->print(p, "%(%p%)", ego->cld2);
cannam@95 710 if (ego->cld3) p->print(p, "%(%p%)", ego->cld3);
cannam@95 711 p->print(p, ")");
cannam@95 712 }
cannam@95 713
cannam@95 714 static void destroy(plan *ego_)
cannam@95 715 {
cannam@95 716 P *ego = (P *) ego_;
cannam@95 717 X(plan_destroy_internal)(ego->cld3);
cannam@95 718 X(plan_destroy_internal)(ego->cld2);
cannam@95 719 X(plan_destroy_internal)(ego->cld1);
cannam@95 720 }
cannam@95 721
cannam@95 722 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
cannam@95 723 {
cannam@95 724 const S *ego = (const S *) ego_;
cannam@95 725 const problem_rdft *p;
cannam@95 726 int dim0, dim1, dim2;
cannam@95 727 INT nbuf, vs;
cannam@95 728 P *pln;
cannam@95 729
cannam@95 730 static const plan_adt padt = {
cannam@95 731 X(rdft_solve), awake, print, destroy
cannam@95 732 };
cannam@95 733
cannam@95 734 if (!applicable(ego_, p_, plnr, &dim0, &dim1, &dim2, &nbuf))
cannam@95 735 return (plan *) 0;
cannam@95 736
cannam@95 737 p = (const problem_rdft *) p_;
cannam@95 738 pln = MKPLAN_RDFT(P, &padt, ego->adt->apply);
cannam@95 739
cannam@95 740 pln->n = p->vecsz->dims[dim0].n;
cannam@95 741 pln->m = p->vecsz->dims[dim1].n;
cannam@95 742 get_transpose_vec(p, dim2, &pln->vl, &vs);
cannam@95 743 pln->nbuf = nbuf;
cannam@95 744 pln->d = gcd(pln->n, pln->m);
cannam@95 745 pln->nd = pln->n / pln->d;
cannam@95 746 pln->md = pln->m / pln->d;
cannam@95 747 pln->slv = ego;
cannam@95 748
cannam@95 749 X(ops_zero)(&pln->super.super.ops); /* mkcldrn is responsible for ops */
cannam@95 750
cannam@95 751 pln->cld1 = pln->cld2 = pln->cld3 = 0;
cannam@95 752 if (!ego->adt->mkcldrn(p, plnr, pln)) {
cannam@95 753 X(plan_destroy_internal)(&(pln->super.super));
cannam@95 754 return 0;
cannam@95 755 }
cannam@95 756
cannam@95 757 return &(pln->super.super);
cannam@95 758 }
cannam@95 759
cannam@95 760 static solver *mksolver(const transpose_adt *adt)
cannam@95 761 {
cannam@95 762 static const solver_adt sadt = { PROBLEM_RDFT, mkplan, 0 };
cannam@95 763 S *slv = MKSOLVER(S, &sadt);
cannam@95 764 slv->adt = adt;
cannam@95 765 return &(slv->super);
cannam@95 766 }
cannam@95 767
cannam@95 768 void X(rdft_vrank3_transpose_register)(planner *p)
cannam@95 769 {
cannam@95 770 unsigned i;
cannam@95 771 static const transpose_adt *const adts[] = {
cannam@95 772 &adt_gcd, &adt_cut,
cannam@95 773 &adt_toms513
cannam@95 774 };
cannam@95 775 for (i = 0; i < sizeof(adts) / sizeof(adts[0]); ++i)
cannam@95 776 REGISTER_SOLVER(p, mksolver(adts[i]));
cannam@95 777 }