annotate src/fftw-3.3.5/mpi/dft-rank1.c @ 43:5ea0608b923f

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