annotate fft/fftw/fftw-3.3.4/mpi/dft-rank1.c @ 40:223f770b5341 kissfft-double tip

Try a double-precision kissfft
author Chris Cannam
date Wed, 07 Sep 2016 10:40:32 +0100
parents 26056e866c29
children
rev   line source
Chris@19 1 /*
Chris@19 2 * Copyright (c) 2003, 2007-14 Matteo Frigo
Chris@19 3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
Chris@19 4 *
Chris@19 5 * This program is free software; you can redistribute it and/or modify
Chris@19 6 * it under the terms of the GNU General Public License as published by
Chris@19 7 * the Free Software Foundation; either version 2 of the License, or
Chris@19 8 * (at your option) any later version.
Chris@19 9 *
Chris@19 10 * This program is distributed in the hope that it will be useful,
Chris@19 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@19 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@19 13 * GNU General Public License for more details.
Chris@19 14 *
Chris@19 15 * You should have received a copy of the GNU General Public License
Chris@19 16 * along with this program; if not, write to the Free Software
Chris@19 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@19 18 *
Chris@19 19 */
Chris@19 20
Chris@19 21 /* Complex DFTs of rank == 1 via six-step algorithm. */
Chris@19 22
Chris@19 23 #include "mpi-dft.h"
Chris@19 24 #include "mpi-transpose.h"
Chris@19 25 #include "dft.h"
Chris@19 26
Chris@19 27 typedef struct {
Chris@19 28 solver super;
Chris@19 29 rdftapply apply; /* apply_ddft_first or apply_ddft_last */
Chris@19 30 int preserve_input; /* preserve input even if DESTROY_INPUT was passed */
Chris@19 31 } S;
Chris@19 32
Chris@19 33 typedef struct {
Chris@19 34 plan_mpi_dft super;
Chris@19 35
Chris@19 36 triggen *t;
Chris@19 37 plan *cldt, *cld_ddft, *cld_dft;
Chris@19 38 INT roff, ioff;
Chris@19 39 int preserve_input;
Chris@19 40 INT vn, xmin, xmax, xs, m, r;
Chris@19 41 } P;
Chris@19 42
Chris@19 43 static void do_twiddle(triggen *t, INT ir, INT m, INT vn, R *xr, R *xi)
Chris@19 44 {
Chris@19 45 void (*rotate)(triggen *, INT, R, R, R *) = t->rotate;
Chris@19 46 INT im, iv;
Chris@19 47 for (im = 0; im < m; ++im)
Chris@19 48 for (iv = 0; iv < vn; ++iv) {
Chris@19 49 /* TODO: modify/inline rotate function
Chris@19 50 so that it can do whole vn vector at once? */
Chris@19 51 R c[2];
Chris@19 52 rotate(t, ir * im, *xr, *xi, c);
Chris@19 53 *xr = c[0]; *xi = c[1];
Chris@19 54 xr += 2; xi += 2;
Chris@19 55 }
Chris@19 56 }
Chris@19 57
Chris@19 58 /* radix-r DFT of size r*m. This is equivalent to an m x r 2d DFT,
Chris@19 59 plus twiddle factors between the size-m and size-r 1d DFTs, where
Chris@19 60 the m dimension is initially distributed. The output is transposed
Chris@19 61 to r x m where the r dimension is distributed.
Chris@19 62
Chris@19 63 This algorithm follows the general sequence:
Chris@19 64 global transpose (m x r -> r x m)
Chris@19 65 DFTs of size m
Chris@19 66 multiply by twiddles + global transpose (r x m -> m x r)
Chris@19 67 DFTs of size r
Chris@19 68 global transpose (m x r -> r x m)
Chris@19 69 where the multiplication by twiddles can come before or after
Chris@19 70 the middle transpose. The first/last transposes are omitted
Chris@19 71 for SCRAMBLED_IN/OUT formats, respectively.
Chris@19 72
Chris@19 73 However, we wish to exploit our dft-rank1-bigvec solver, which
Chris@19 74 solves a vector of distributed DFTs via transpose+dft+transpose.
Chris@19 75 Therefore, we can group *either* the DFTs of size m *or* the
Chris@19 76 DFTs of size r with their surrounding transposes as a single
Chris@19 77 distributed-DFT (ddft) plan. These two variations correspond to
Chris@19 78 apply_ddft_first or apply_ddft_last, respectively.
Chris@19 79 */
Chris@19 80
Chris@19 81 static void apply_ddft_first(const plan *ego_, R *I, R *O)
Chris@19 82 {
Chris@19 83 const P *ego = (const P *) ego_;
Chris@19 84 plan_dft *cld_dft;
Chris@19 85 plan_rdft *cldt, *cld_ddft;
Chris@19 86 INT roff, ioff, im, mmax, ms, r, vn;
Chris@19 87 triggen *t;
Chris@19 88 R *dI, *dO;
Chris@19 89
Chris@19 90 /* distributed size-m DFTs, with output in m x r format */
Chris@19 91 cld_ddft = (plan_rdft *) ego->cld_ddft;
Chris@19 92 cld_ddft->apply(ego->cld_ddft, I, O);
Chris@19 93
Chris@19 94 cldt = (plan_rdft *) ego->cldt;
Chris@19 95 if (ego->preserve_input || !cldt) I = O;
Chris@19 96
Chris@19 97 /* twiddle multiplications, followed by 1d DFTs of size-r */
Chris@19 98 cld_dft = (plan_dft *) ego->cld_dft;
Chris@19 99 roff = ego->roff; ioff = ego->ioff;
Chris@19 100 mmax = ego->xmax; ms = ego->xs;
Chris@19 101 t = ego->t; r = ego->r; vn = ego->vn;
Chris@19 102 dI = O; dO = I;
Chris@19 103 for (im = ego->xmin; im <= mmax; ++im) {
Chris@19 104 do_twiddle(t, im, r, vn, dI+roff, dI+ioff);
Chris@19 105 cld_dft->apply((plan *) cld_dft, dI+roff, dI+ioff, dO+roff, dO+ioff);
Chris@19 106 dI += ms; dO += ms;
Chris@19 107 }
Chris@19 108
Chris@19 109 /* final global transpose (m x r -> r x m), if not SCRAMBLED_OUT */
Chris@19 110 if (cldt)
Chris@19 111 cldt->apply((plan *) cldt, I, O);
Chris@19 112 }
Chris@19 113
Chris@19 114 static void apply_ddft_last(const plan *ego_, R *I, R *O)
Chris@19 115 {
Chris@19 116 const P *ego = (const P *) ego_;
Chris@19 117 plan_dft *cld_dft;
Chris@19 118 plan_rdft *cldt, *cld_ddft;
Chris@19 119 INT roff, ioff, ir, rmax, rs, m, vn;
Chris@19 120 triggen *t;
Chris@19 121 R *dI, *dO0, *dO;
Chris@19 122
Chris@19 123 /* initial global transpose (m x r -> r x m), if not SCRAMBLED_IN */
Chris@19 124 cldt = (plan_rdft *) ego->cldt;
Chris@19 125 if (cldt) {
Chris@19 126 cldt->apply((plan *) cldt, I, O);
Chris@19 127 dI = O;
Chris@19 128 }
Chris@19 129 else
Chris@19 130 dI = I;
Chris@19 131 if (ego->preserve_input) dO = O; else dO = I;
Chris@19 132 dO0 = dO;
Chris@19 133
Chris@19 134 /* 1d DFTs of size m, followed by twiddle multiplications */
Chris@19 135 cld_dft = (plan_dft *) ego->cld_dft;
Chris@19 136 roff = ego->roff; ioff = ego->ioff;
Chris@19 137 rmax = ego->xmax; rs = ego->xs;
Chris@19 138 t = ego->t; m = ego->m; vn = ego->vn;
Chris@19 139 for (ir = ego->xmin; ir <= rmax; ++ir) {
Chris@19 140 cld_dft->apply((plan *) cld_dft, dI+roff, dI+ioff, dO+roff, dO+ioff);
Chris@19 141 do_twiddle(t, ir, m, vn, dO+roff, dO+ioff);
Chris@19 142 dI += rs; dO += rs;
Chris@19 143 }
Chris@19 144
Chris@19 145 /* distributed size-r DFTs, with output in r x m format */
Chris@19 146 cld_ddft = (plan_rdft *) ego->cld_ddft;
Chris@19 147 cld_ddft->apply(ego->cld_ddft, dO0, O);
Chris@19 148 }
Chris@19 149
Chris@19 150 static int applicable(const S *ego, const problem *p_,
Chris@19 151 const planner *plnr,
Chris@19 152 INT *r, INT rblock[2], INT mblock[2])
Chris@19 153 {
Chris@19 154 const problem_mpi_dft *p = (const problem_mpi_dft *) p_;
Chris@19 155 int n_pes;
Chris@19 156 MPI_Comm_size(p->comm, &n_pes);
Chris@19 157 return (1
Chris@19 158 && p->sz->rnk == 1
Chris@19 159
Chris@19 160 && ONLY_SCRAMBLEDP(p->flags)
Chris@19 161
Chris@19 162 && (!ego->preserve_input || (!NO_DESTROY_INPUTP(plnr)
Chris@19 163 && p->I != p->O))
Chris@19 164
Chris@19 165 && (!(p->flags & SCRAMBLED_IN) || ego->apply == apply_ddft_last)
Chris@19 166 && (!(p->flags & SCRAMBLED_OUT) || ego->apply == apply_ddft_first)
Chris@19 167
Chris@19 168 && (!NO_SLOWP(plnr) /* slow if dft-serial is applicable */
Chris@19 169 || !XM(dft_serial_applicable)(p))
Chris@19 170
Chris@19 171 /* disallow if dft-rank1-bigvec is applicable since the
Chris@19 172 data distribution may be slightly different (ugh!) */
Chris@19 173 && (p->vn < n_pes || p->flags)
Chris@19 174
Chris@19 175 && (*r = XM(choose_radix)(p->sz->dims[0], n_pes,
Chris@19 176 p->flags, p->sign,
Chris@19 177 rblock, mblock))
Chris@19 178
Chris@19 179 /* ddft_first or last has substantial advantages in the
Chris@19 180 bigvec transpositions for the common case where
Chris@19 181 n_pes == n/r or r, respectively */
Chris@19 182 && (!NO_UGLYP(plnr)
Chris@19 183 || !(*r == n_pes && ego->apply == apply_ddft_first)
Chris@19 184 || !(p->sz->dims[0].n / *r == n_pes
Chris@19 185 && ego->apply == apply_ddft_last))
Chris@19 186 );
Chris@19 187 }
Chris@19 188
Chris@19 189 static void awake(plan *ego_, enum wakefulness wakefulness)
Chris@19 190 {
Chris@19 191 P *ego = (P *) ego_;
Chris@19 192 X(plan_awake)(ego->cldt, wakefulness);
Chris@19 193 X(plan_awake)(ego->cld_dft, wakefulness);
Chris@19 194 X(plan_awake)(ego->cld_ddft, wakefulness);
Chris@19 195
Chris@19 196 switch (wakefulness) {
Chris@19 197 case SLEEPY:
Chris@19 198 X(triggen_destroy)(ego->t); ego->t = 0;
Chris@19 199 break;
Chris@19 200 default:
Chris@19 201 ego->t = X(mktriggen)(AWAKE_SQRTN_TABLE, ego->r * ego->m);
Chris@19 202 break;
Chris@19 203 }
Chris@19 204 }
Chris@19 205
Chris@19 206 static void destroy(plan *ego_)
Chris@19 207 {
Chris@19 208 P *ego = (P *) ego_;
Chris@19 209 X(plan_destroy_internal)(ego->cldt);
Chris@19 210 X(plan_destroy_internal)(ego->cld_dft);
Chris@19 211 X(plan_destroy_internal)(ego->cld_ddft);
Chris@19 212 }
Chris@19 213
Chris@19 214 static void print(const plan *ego_, printer *p)
Chris@19 215 {
Chris@19 216 const P *ego = (const P *) ego_;
Chris@19 217 p->print(p, "(mpi-dft-rank1/%D%s%s%(%p%)%(%p%)%(%p%))",
Chris@19 218 ego->r,
Chris@19 219 ego->super.apply == apply_ddft_first ? "/first" : "/last",
Chris@19 220 ego->preserve_input==2 ?"/p":"",
Chris@19 221 ego->cld_ddft, ego->cld_dft, ego->cldt);
Chris@19 222 }
Chris@19 223
Chris@19 224 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
Chris@19 225 {
Chris@19 226 const S *ego = (const S *) ego_;
Chris@19 227 const problem_mpi_dft *p;
Chris@19 228 P *pln;
Chris@19 229 plan *cld_dft = 0, *cld_ddft = 0, *cldt = 0;
Chris@19 230 R *ri, *ii, *ro, *io, *I, *O;
Chris@19 231 INT r, rblock[2], m, mblock[2], rp, mp, mpblock[2], mpb;
Chris@19 232 int my_pe, n_pes, preserve_input, ddft_first;
Chris@19 233 dtensor *sz;
Chris@19 234 static const plan_adt padt = {
Chris@19 235 XM(dft_solve), awake, print, destroy
Chris@19 236 };
Chris@19 237
Chris@19 238 UNUSED(ego);
Chris@19 239
Chris@19 240 if (!applicable(ego, p_, plnr, &r, rblock, mblock))
Chris@19 241 return (plan *) 0;
Chris@19 242
Chris@19 243 p = (const problem_mpi_dft *) p_;
Chris@19 244
Chris@19 245 MPI_Comm_rank(p->comm, &my_pe);
Chris@19 246 MPI_Comm_size(p->comm, &n_pes);
Chris@19 247
Chris@19 248 m = p->sz->dims[0].n / r;
Chris@19 249
Chris@19 250 /* some hackery so that we can plan both ddft_first and ddft_last
Chris@19 251 as if they were ddft_first */
Chris@19 252 if ((ddft_first = (ego->apply == apply_ddft_first))) {
Chris@19 253 rp = r; mp = m;
Chris@19 254 mpblock[IB] = mblock[IB]; mpblock[OB] = mblock[OB];
Chris@19 255 mpb = XM(block)(mp, mpblock[OB], my_pe);
Chris@19 256 }
Chris@19 257 else {
Chris@19 258 rp = m; mp = r;
Chris@19 259 mpblock[IB] = rblock[IB]; mpblock[OB] = rblock[OB];
Chris@19 260 mpb = XM(block)(mp, mpblock[IB], my_pe);
Chris@19 261 }
Chris@19 262
Chris@19 263 preserve_input = ego->preserve_input ? 2 : NO_DESTROY_INPUTP(plnr);
Chris@19 264
Chris@19 265 sz = XM(mkdtensor)(1);
Chris@19 266 sz->dims[0].n = mp;
Chris@19 267 sz->dims[0].b[IB] = mpblock[IB];
Chris@19 268 sz->dims[0].b[OB] = mpblock[OB];
Chris@19 269 I = (ddft_first || !preserve_input) ? p->I : p->O;
Chris@19 270 O = p->O;
Chris@19 271 cld_ddft = X(mkplan_d)(plnr, XM(mkproblem_dft_d)(sz, rp * p->vn,
Chris@19 272 I, O, p->comm, p->sign,
Chris@19 273 RANK1_BIGVEC_ONLY));
Chris@19 274 if (XM(any_true)(!cld_ddft, p->comm)) goto nada;
Chris@19 275
Chris@19 276 I = TAINT((ddft_first || !p->flags) ? p->O : p->I, rp * p->vn * 2);
Chris@19 277 O = TAINT((preserve_input || (ddft_first && p->flags)) ? p->O : p->I,
Chris@19 278 rp * p->vn * 2);
Chris@19 279 X(extract_reim)(p->sign, I, &ri, &ii);
Chris@19 280 X(extract_reim)(p->sign, O, &ro, &io);
Chris@19 281 cld_dft = X(mkplan_d)(plnr,
Chris@19 282 X(mkproblem_dft_d)(X(mktensor_1d)(rp, p->vn*2,p->vn*2),
Chris@19 283 X(mktensor_1d)(p->vn, 2, 2),
Chris@19 284 ri, ii, ro, io));
Chris@19 285 if (XM(any_true)(!cld_dft, p->comm)) goto nada;
Chris@19 286
Chris@19 287 if (!p->flags) { /* !(SCRAMBLED_IN or SCRAMBLED_OUT) */
Chris@19 288 I = (ddft_first && preserve_input) ? p->O : p->I;
Chris@19 289 O = p->O;
Chris@19 290 cldt = X(mkplan_d)(plnr,
Chris@19 291 XM(mkproblem_transpose)(
Chris@19 292 m, r, p->vn * 2,
Chris@19 293 I, O,
Chris@19 294 ddft_first ? mblock[OB] : mblock[IB],
Chris@19 295 ddft_first ? rblock[OB] : rblock[IB],
Chris@19 296 p->comm, 0));
Chris@19 297 if (XM(any_true)(!cldt, p->comm)) goto nada;
Chris@19 298 }
Chris@19 299
Chris@19 300 pln = MKPLAN_MPI_DFT(P, &padt, ego->apply);
Chris@19 301
Chris@19 302 pln->cld_ddft = cld_ddft;
Chris@19 303 pln->cld_dft = cld_dft;
Chris@19 304 pln->cldt = cldt;
Chris@19 305 pln->preserve_input = preserve_input;
Chris@19 306 X(extract_reim)(p->sign, p->O, &ro, &io);
Chris@19 307 pln->roff = ro - p->O;
Chris@19 308 pln->ioff = io - p->O;
Chris@19 309 pln->vn = p->vn;
Chris@19 310 pln->m = m;
Chris@19 311 pln->r = r;
Chris@19 312 pln->xmin = (ddft_first ? mblock[OB] : rblock[IB]) * my_pe;
Chris@19 313 pln->xmax = pln->xmin + mpb - 1;
Chris@19 314 pln->xs = rp * p->vn * 2;
Chris@19 315 pln->t = 0;
Chris@19 316
Chris@19 317 X(ops_add)(&cld_ddft->ops, &cld_dft->ops, &pln->super.super.ops);
Chris@19 318 if (cldt) X(ops_add2)(&cldt->ops, &pln->super.super.ops);
Chris@19 319 {
Chris@19 320 double n0 = (1 + pln->xmax - pln->xmin) * (mp - 1) * pln->vn;
Chris@19 321 pln->super.super.ops.mul += 8 * n0;
Chris@19 322 pln->super.super.ops.add += 4 * n0;
Chris@19 323 pln->super.super.ops.other += 8 * n0;
Chris@19 324 }
Chris@19 325
Chris@19 326 return &(pln->super.super);
Chris@19 327
Chris@19 328 nada:
Chris@19 329 X(plan_destroy_internal)(cldt);
Chris@19 330 X(plan_destroy_internal)(cld_dft);
Chris@19 331 X(plan_destroy_internal)(cld_ddft);
Chris@19 332 return (plan *) 0;
Chris@19 333 }
Chris@19 334
Chris@19 335 static solver *mksolver(rdftapply apply, int preserve_input)
Chris@19 336 {
Chris@19 337 static const solver_adt sadt = { PROBLEM_MPI_DFT, mkplan, 0 };
Chris@19 338 S *slv = MKSOLVER(S, &sadt);
Chris@19 339 slv->apply = apply;
Chris@19 340 slv->preserve_input = preserve_input;
Chris@19 341 return &(slv->super);
Chris@19 342 }
Chris@19 343
Chris@19 344 void XM(dft_rank1_register)(planner *p)
Chris@19 345 {
Chris@19 346 rdftapply apply[] = { apply_ddft_first, apply_ddft_last };
Chris@19 347 unsigned int iapply;
Chris@19 348 int preserve_input;
Chris@19 349 for (iapply = 0; iapply < sizeof(apply) / sizeof(apply[0]); ++iapply)
Chris@19 350 for (preserve_input = 0; preserve_input <= 1; ++preserve_input)
Chris@19 351 REGISTER_SOLVER(p, mksolver(apply[iapply], preserve_input));
Chris@19 352 }