Chris@10: /* Chris@10: * Copyright (c) 2003, 2007-11 Matteo Frigo Chris@10: * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology Chris@10: * Chris@10: * This program is free software; you can redistribute it and/or modify Chris@10: * it under the terms of the GNU General Public License as published by Chris@10: * the Free Software Foundation; either version 2 of the License, or Chris@10: * (at your option) any later version. Chris@10: * Chris@10: * This program is distributed in the hope that it will be useful, Chris@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@10: * GNU General Public License for more details. Chris@10: * Chris@10: * You should have received a copy of the GNU General Public License Chris@10: * along with this program; if not, write to the Free Software Chris@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@10: * Chris@10: */ Chris@10: Chris@10: /* Complex DFTs of rank == 1 via six-step algorithm. */ Chris@10: Chris@10: #include "mpi-dft.h" Chris@10: #include "mpi-transpose.h" Chris@10: #include "dft.h" Chris@10: Chris@10: typedef struct { Chris@10: solver super; Chris@10: rdftapply apply; /* apply_ddft_first or apply_ddft_last */ Chris@10: int preserve_input; /* preserve input even if DESTROY_INPUT was passed */ Chris@10: } S; Chris@10: Chris@10: typedef struct { Chris@10: plan_mpi_dft super; Chris@10: Chris@10: triggen *t; Chris@10: plan *cldt, *cld_ddft, *cld_dft; Chris@10: INT roff, ioff; Chris@10: int preserve_input; Chris@10: INT vn, xmin, xmax, xs, m, r; Chris@10: } P; Chris@10: Chris@10: static void do_twiddle(triggen *t, INT ir, INT m, INT vn, R *xr, R *xi) Chris@10: { Chris@10: void (*rotate)(triggen *, INT, R, R, R *) = t->rotate; Chris@10: INT im, iv; Chris@10: for (im = 0; im < m; ++im) Chris@10: for (iv = 0; iv < vn; ++iv) { Chris@10: /* TODO: modify/inline rotate function Chris@10: so that it can do whole vn vector at once? */ Chris@10: R c[2]; Chris@10: rotate(t, ir * im, *xr, *xi, c); Chris@10: *xr = c[0]; *xi = c[1]; Chris@10: xr += 2; xi += 2; Chris@10: } Chris@10: } Chris@10: Chris@10: /* radix-r DFT of size r*m. This is equivalent to an m x r 2d DFT, Chris@10: plus twiddle factors between the size-m and size-r 1d DFTs, where Chris@10: the m dimension is initially distributed. The output is transposed Chris@10: to r x m where the r dimension is distributed. Chris@10: Chris@10: This algorithm follows the general sequence: Chris@10: global transpose (m x r -> r x m) Chris@10: DFTs of size m Chris@10: multiply by twiddles + global transpose (r x m -> m x r) Chris@10: DFTs of size r Chris@10: global transpose (m x r -> r x m) Chris@10: where the multiplication by twiddles can come before or after Chris@10: the middle transpose. The first/last transposes are omitted Chris@10: for SCRAMBLED_IN/OUT formats, respectively. Chris@10: Chris@10: However, we wish to exploit our dft-rank1-bigvec solver, which Chris@10: solves a vector of distributed DFTs via transpose+dft+transpose. Chris@10: Therefore, we can group *either* the DFTs of size m *or* the Chris@10: DFTs of size r with their surrounding transposes as a single Chris@10: distributed-DFT (ddft) plan. These two variations correspond to Chris@10: apply_ddft_first or apply_ddft_last, respectively. Chris@10: */ Chris@10: Chris@10: static void apply_ddft_first(const plan *ego_, R *I, R *O) Chris@10: { Chris@10: const P *ego = (const P *) ego_; Chris@10: plan_dft *cld_dft; Chris@10: plan_rdft *cldt, *cld_ddft; Chris@10: INT roff, ioff, im, mmax, ms, r, vn; Chris@10: triggen *t; Chris@10: R *dI, *dO; Chris@10: Chris@10: /* distributed size-m DFTs, with output in m x r format */ Chris@10: cld_ddft = (plan_rdft *) ego->cld_ddft; Chris@10: cld_ddft->apply(ego->cld_ddft, I, O); Chris@10: Chris@10: cldt = (plan_rdft *) ego->cldt; Chris@10: if (ego->preserve_input || !cldt) I = O; Chris@10: Chris@10: /* twiddle multiplications, followed by 1d DFTs of size-r */ Chris@10: cld_dft = (plan_dft *) ego->cld_dft; Chris@10: roff = ego->roff; ioff = ego->ioff; Chris@10: mmax = ego->xmax; ms = ego->xs; Chris@10: t = ego->t; r = ego->r; vn = ego->vn; Chris@10: dI = O; dO = I; Chris@10: for (im = ego->xmin; im <= mmax; ++im) { Chris@10: do_twiddle(t, im, r, vn, dI+roff, dI+ioff); Chris@10: cld_dft->apply((plan *) cld_dft, dI+roff, dI+ioff, dO+roff, dO+ioff); Chris@10: dI += ms; dO += ms; Chris@10: } Chris@10: Chris@10: /* final global transpose (m x r -> r x m), if not SCRAMBLED_OUT */ Chris@10: if (cldt) Chris@10: cldt->apply((plan *) cldt, I, O); Chris@10: } Chris@10: Chris@10: static void apply_ddft_last(const plan *ego_, R *I, R *O) Chris@10: { Chris@10: const P *ego = (const P *) ego_; Chris@10: plan_dft *cld_dft; Chris@10: plan_rdft *cldt, *cld_ddft; Chris@10: INT roff, ioff, ir, rmax, rs, m, vn; Chris@10: triggen *t; Chris@10: R *dI, *dO0, *dO; Chris@10: Chris@10: /* initial global transpose (m x r -> r x m), if not SCRAMBLED_IN */ Chris@10: cldt = (plan_rdft *) ego->cldt; Chris@10: if (cldt) { Chris@10: cldt->apply((plan *) cldt, I, O); Chris@10: dI = O; Chris@10: } Chris@10: else Chris@10: dI = I; Chris@10: if (ego->preserve_input) dO = O; else dO = I; Chris@10: dO0 = dO; Chris@10: Chris@10: /* 1d DFTs of size m, followed by twiddle multiplications */ Chris@10: cld_dft = (plan_dft *) ego->cld_dft; Chris@10: roff = ego->roff; ioff = ego->ioff; Chris@10: rmax = ego->xmax; rs = ego->xs; Chris@10: t = ego->t; m = ego->m; vn = ego->vn; Chris@10: for (ir = ego->xmin; ir <= rmax; ++ir) { Chris@10: cld_dft->apply((plan *) cld_dft, dI+roff, dI+ioff, dO+roff, dO+ioff); Chris@10: do_twiddle(t, ir, m, vn, dO+roff, dO+ioff); Chris@10: dI += rs; dO += rs; Chris@10: } Chris@10: Chris@10: /* distributed size-r DFTs, with output in r x m format */ Chris@10: cld_ddft = (plan_rdft *) ego->cld_ddft; Chris@10: cld_ddft->apply(ego->cld_ddft, dO0, O); Chris@10: } Chris@10: Chris@10: static int applicable(const S *ego, const problem *p_, Chris@10: const planner *plnr, Chris@10: INT *r, INT rblock[2], INT mblock[2]) Chris@10: { Chris@10: const problem_mpi_dft *p = (const problem_mpi_dft *) p_; Chris@10: int n_pes; Chris@10: MPI_Comm_size(p->comm, &n_pes); Chris@10: return (1 Chris@10: && p->sz->rnk == 1 Chris@10: Chris@10: && ONLY_SCRAMBLEDP(p->flags) Chris@10: Chris@10: && (!ego->preserve_input || (!NO_DESTROY_INPUTP(plnr) Chris@10: && p->I != p->O)) Chris@10: Chris@10: && (!(p->flags & SCRAMBLED_IN) || ego->apply == apply_ddft_last) Chris@10: && (!(p->flags & SCRAMBLED_OUT) || ego->apply == apply_ddft_first) Chris@10: Chris@10: && (!NO_SLOWP(plnr) /* slow if dft-serial is applicable */ Chris@10: || !XM(dft_serial_applicable)(p)) Chris@10: Chris@10: /* disallow if dft-rank1-bigvec is applicable since the Chris@10: data distribution may be slightly different (ugh!) */ Chris@10: && (p->vn < n_pes || p->flags) Chris@10: Chris@10: && (*r = XM(choose_radix)(p->sz->dims[0], n_pes, Chris@10: p->flags, p->sign, Chris@10: rblock, mblock)) Chris@10: Chris@10: /* ddft_first or last has substantial advantages in the Chris@10: bigvec transpositions for the common case where Chris@10: n_pes == n/r or r, respectively */ Chris@10: && (!NO_UGLYP(plnr) Chris@10: || !(*r == n_pes && ego->apply == apply_ddft_first) Chris@10: || !(p->sz->dims[0].n / *r == n_pes Chris@10: && ego->apply == apply_ddft_last)) Chris@10: ); Chris@10: } Chris@10: Chris@10: static void awake(plan *ego_, enum wakefulness wakefulness) Chris@10: { Chris@10: P *ego = (P *) ego_; Chris@10: X(plan_awake)(ego->cldt, wakefulness); Chris@10: X(plan_awake)(ego->cld_dft, wakefulness); Chris@10: X(plan_awake)(ego->cld_ddft, wakefulness); Chris@10: Chris@10: switch (wakefulness) { Chris@10: case SLEEPY: Chris@10: X(triggen_destroy)(ego->t); ego->t = 0; Chris@10: break; Chris@10: default: Chris@10: ego->t = X(mktriggen)(AWAKE_SQRTN_TABLE, ego->r * ego->m); Chris@10: break; Chris@10: } Chris@10: } Chris@10: Chris@10: static void destroy(plan *ego_) Chris@10: { Chris@10: P *ego = (P *) ego_; Chris@10: X(plan_destroy_internal)(ego->cldt); Chris@10: X(plan_destroy_internal)(ego->cld_dft); Chris@10: X(plan_destroy_internal)(ego->cld_ddft); Chris@10: } Chris@10: Chris@10: static void print(const plan *ego_, printer *p) Chris@10: { Chris@10: const P *ego = (const P *) ego_; Chris@10: p->print(p, "(mpi-dft-rank1/%D%s%s%(%p%)%(%p%)%(%p%))", Chris@10: ego->r, Chris@10: ego->super.apply == apply_ddft_first ? "/first" : "/last", Chris@10: ego->preserve_input==2 ?"/p":"", Chris@10: ego->cld_ddft, ego->cld_dft, ego->cldt); Chris@10: } Chris@10: Chris@10: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) Chris@10: { Chris@10: const S *ego = (const S *) ego_; Chris@10: const problem_mpi_dft *p; Chris@10: P *pln; Chris@10: plan *cld_dft = 0, *cld_ddft = 0, *cldt = 0; Chris@10: R *ri, *ii, *ro, *io, *I, *O; Chris@10: INT r, rblock[2], m, mblock[2], rp, mp, mpblock[2], mpb; Chris@10: int my_pe, n_pes, preserve_input, ddft_first; Chris@10: dtensor *sz; Chris@10: static const plan_adt padt = { Chris@10: XM(dft_solve), awake, print, destroy Chris@10: }; Chris@10: Chris@10: UNUSED(ego); Chris@10: Chris@10: if (!applicable(ego, p_, plnr, &r, rblock, mblock)) Chris@10: return (plan *) 0; Chris@10: Chris@10: p = (const problem_mpi_dft *) p_; Chris@10: Chris@10: MPI_Comm_rank(p->comm, &my_pe); Chris@10: MPI_Comm_size(p->comm, &n_pes); Chris@10: Chris@10: m = p->sz->dims[0].n / r; Chris@10: Chris@10: /* some hackery so that we can plan both ddft_first and ddft_last Chris@10: as if they were ddft_first */ Chris@10: if ((ddft_first = (ego->apply == apply_ddft_first))) { Chris@10: rp = r; mp = m; Chris@10: mpblock[IB] = mblock[IB]; mpblock[OB] = mblock[OB]; Chris@10: mpb = XM(block)(mp, mpblock[OB], my_pe); Chris@10: } Chris@10: else { Chris@10: rp = m; mp = r; Chris@10: mpblock[IB] = rblock[IB]; mpblock[OB] = rblock[OB]; Chris@10: mpb = XM(block)(mp, mpblock[IB], my_pe); Chris@10: } Chris@10: Chris@10: preserve_input = ego->preserve_input ? 2 : NO_DESTROY_INPUTP(plnr); Chris@10: Chris@10: sz = XM(mkdtensor)(1); Chris@10: sz->dims[0].n = mp; Chris@10: sz->dims[0].b[IB] = mpblock[IB]; Chris@10: sz->dims[0].b[OB] = mpblock[OB]; Chris@10: I = (ddft_first || !preserve_input) ? p->I : p->O; Chris@10: O = p->O; Chris@10: cld_ddft = X(mkplan_d)(plnr, XM(mkproblem_dft_d)(sz, rp * p->vn, Chris@10: I, O, p->comm, p->sign, Chris@10: RANK1_BIGVEC_ONLY)); Chris@10: if (XM(any_true)(!cld_ddft, p->comm)) goto nada; Chris@10: Chris@10: I = TAINT((ddft_first || !p->flags) ? p->O : p->I, rp * p->vn * 2); Chris@10: O = TAINT((preserve_input || (ddft_first && p->flags)) ? p->O : p->I, Chris@10: rp * p->vn * 2); Chris@10: X(extract_reim)(p->sign, I, &ri, &ii); Chris@10: X(extract_reim)(p->sign, O, &ro, &io); Chris@10: cld_dft = X(mkplan_d)(plnr, Chris@10: X(mkproblem_dft_d)(X(mktensor_1d)(rp, p->vn*2,p->vn*2), Chris@10: X(mktensor_1d)(p->vn, 2, 2), Chris@10: ri, ii, ro, io)); Chris@10: if (XM(any_true)(!cld_dft, p->comm)) goto nada; Chris@10: Chris@10: if (!p->flags) { /* !(SCRAMBLED_IN or SCRAMBLED_OUT) */ Chris@10: I = (ddft_first && preserve_input) ? p->O : p->I; Chris@10: O = p->O; Chris@10: cldt = X(mkplan_d)(plnr, Chris@10: XM(mkproblem_transpose)( Chris@10: m, r, p->vn * 2, Chris@10: I, O, Chris@10: ddft_first ? mblock[OB] : mblock[IB], Chris@10: ddft_first ? rblock[OB] : rblock[IB], Chris@10: p->comm, 0)); Chris@10: if (XM(any_true)(!cldt, p->comm)) goto nada; Chris@10: } Chris@10: Chris@10: pln = MKPLAN_MPI_DFT(P, &padt, ego->apply); Chris@10: Chris@10: pln->cld_ddft = cld_ddft; Chris@10: pln->cld_dft = cld_dft; Chris@10: pln->cldt = cldt; Chris@10: pln->preserve_input = preserve_input; Chris@10: X(extract_reim)(p->sign, p->O, &ro, &io); Chris@10: pln->roff = ro - p->O; Chris@10: pln->ioff = io - p->O; Chris@10: pln->vn = p->vn; Chris@10: pln->m = m; Chris@10: pln->r = r; Chris@10: pln->xmin = (ddft_first ? mblock[OB] : rblock[IB]) * my_pe; Chris@10: pln->xmax = pln->xmin + mpb - 1; Chris@10: pln->xs = rp * p->vn * 2; Chris@10: pln->t = 0; Chris@10: Chris@10: X(ops_add)(&cld_ddft->ops, &cld_dft->ops, &pln->super.super.ops); Chris@10: if (cldt) X(ops_add2)(&cldt->ops, &pln->super.super.ops); Chris@10: { Chris@10: double n0 = (1 + pln->xmax - pln->xmin) * (mp - 1) * pln->vn; Chris@10: pln->super.super.ops.mul += 8 * n0; Chris@10: pln->super.super.ops.add += 4 * n0; Chris@10: pln->super.super.ops.other += 8 * n0; Chris@10: } Chris@10: Chris@10: return &(pln->super.super); Chris@10: Chris@10: nada: Chris@10: X(plan_destroy_internal)(cldt); Chris@10: X(plan_destroy_internal)(cld_dft); Chris@10: X(plan_destroy_internal)(cld_ddft); Chris@10: return (plan *) 0; Chris@10: } Chris@10: Chris@10: static solver *mksolver(rdftapply apply, int preserve_input) Chris@10: { Chris@10: static const solver_adt sadt = { PROBLEM_MPI_DFT, mkplan, 0 }; Chris@10: S *slv = MKSOLVER(S, &sadt); Chris@10: slv->apply = apply; Chris@10: slv->preserve_input = preserve_input; Chris@10: return &(slv->super); Chris@10: } Chris@10: Chris@10: void XM(dft_rank1_register)(planner *p) Chris@10: { Chris@10: rdftapply apply[] = { apply_ddft_first, apply_ddft_last }; Chris@10: unsigned int iapply; Chris@10: int preserve_input; Chris@10: for (iapply = 0; iapply < sizeof(apply) / sizeof(apply[0]); ++iapply) Chris@10: for (preserve_input = 0; preserve_input <= 1; ++preserve_input) Chris@10: REGISTER_SOLVER(p, mksolver(apply[iapply], preserve_input)); Chris@10: }