Chris@42: /* Chris@42: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@42: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@42: * Chris@42: * This program is free software; you can redistribute it and/or modify Chris@42: * it under the terms of the GNU General Public License as published by Chris@42: * the Free Software Foundation; either version 2 of the License, or Chris@42: * (at your option) any later version. Chris@42: * Chris@42: * This program is distributed in the hope that it will be useful, Chris@42: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@42: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@42: * GNU General Public License for more details. Chris@42: * Chris@42: * You should have received a copy of the GNU General Public License Chris@42: * along with this program; if not, write to the Free Software Chris@42: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@42: * Chris@42: */ Chris@42: Chris@42: /* Complex DFTs of rank >= 2, for the case where we are distributed Chris@42: across the first dimension only, and the output is transposed both Chris@42: in data distribution and in ordering (for the first 2 dimensions). Chris@42: Chris@42: (Note that we don't have to handle the case where the input is Chris@42: transposed, since this is equivalent to transposed output with the Chris@42: first two dimensions swapped, and is automatically canonicalized as Chris@42: such by dft-problem.c. */ Chris@42: Chris@42: #include "mpi-dft.h" Chris@42: #include "mpi-transpose.h" Chris@42: #include "dft.h" Chris@42: Chris@42: typedef struct { Chris@42: solver super; Chris@42: int preserve_input; /* preserve input even if DESTROY_INPUT was passed */ Chris@42: } S; Chris@42: Chris@42: typedef struct { Chris@42: plan_mpi_dft super; Chris@42: Chris@42: plan *cld1, *cldt, *cld2; Chris@42: INT roff, ioff; Chris@42: int preserve_input; Chris@42: } P; Chris@42: Chris@42: static void apply(const plan *ego_, R *I, R *O) Chris@42: { Chris@42: const P *ego = (const P *) ego_; Chris@42: plan_dft *cld1, *cld2; Chris@42: plan_rdft *cldt; Chris@42: INT roff = ego->roff, ioff = ego->ioff; Chris@42: Chris@42: /* DFT local dimensions */ Chris@42: cld1 = (plan_dft *) ego->cld1; Chris@42: if (ego->preserve_input) { Chris@42: cld1->apply(ego->cld1, I+roff, I+ioff, O+roff, O+ioff); Chris@42: I = O; Chris@42: } Chris@42: else Chris@42: cld1->apply(ego->cld1, I+roff, I+ioff, I+roff, I+ioff); Chris@42: Chris@42: /* global transpose */ Chris@42: cldt = (plan_rdft *) ego->cldt; Chris@42: cldt->apply(ego->cldt, I, O); Chris@42: Chris@42: /* DFT final local dimension */ Chris@42: cld2 = (plan_dft *) ego->cld2; Chris@42: cld2->apply(ego->cld2, O+roff, O+ioff, O+roff, O+ioff); Chris@42: } Chris@42: Chris@42: static int applicable(const S *ego, const problem *p_, Chris@42: const planner *plnr) Chris@42: { Chris@42: const problem_mpi_dft *p = (const problem_mpi_dft *) p_; Chris@42: return (1 Chris@42: && p->sz->rnk > 1 Chris@42: && p->flags == TRANSPOSED_OUT Chris@42: && (!ego->preserve_input || (!NO_DESTROY_INPUTP(plnr) Chris@42: && p->I != p->O)) Chris@42: && XM(is_local_after)(1, p->sz, IB) Chris@42: && XM(is_local_after)(2, p->sz, OB) Chris@42: && XM(num_blocks)(p->sz->dims[0].n, p->sz->dims[0].b[OB]) == 1 Chris@42: && (!NO_SLOWP(plnr) /* slow if dft-serial is applicable */ Chris@42: || !XM(dft_serial_applicable)(p)) Chris@42: ); Chris@42: } Chris@42: Chris@42: static void awake(plan *ego_, enum wakefulness wakefulness) Chris@42: { Chris@42: P *ego = (P *) ego_; Chris@42: X(plan_awake)(ego->cld1, wakefulness); Chris@42: X(plan_awake)(ego->cldt, wakefulness); Chris@42: X(plan_awake)(ego->cld2, wakefulness); Chris@42: } Chris@42: Chris@42: static void destroy(plan *ego_) Chris@42: { Chris@42: P *ego = (P *) ego_; Chris@42: X(plan_destroy_internal)(ego->cld2); Chris@42: X(plan_destroy_internal)(ego->cldt); Chris@42: X(plan_destroy_internal)(ego->cld1); Chris@42: } Chris@42: Chris@42: static void print(const plan *ego_, printer *p) Chris@42: { Chris@42: const P *ego = (const P *) ego_; Chris@42: p->print(p, "(mpi-dft-rank-geq2-transposed%s%(%p%)%(%p%)%(%p%))", Chris@42: ego->preserve_input==2 ?"/p":"", Chris@42: ego->cld1, ego->cldt, ego->cld2); Chris@42: } Chris@42: Chris@42: static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) Chris@42: { Chris@42: const S *ego = (const S *) ego_; Chris@42: const problem_mpi_dft *p; Chris@42: P *pln; Chris@42: plan *cld1 = 0, *cldt = 0, *cld2 = 0; Chris@42: R *ri, *ii, *ro, *io, *I, *O; Chris@42: tensor *sz; Chris@42: int i, my_pe, n_pes; Chris@42: INT nrest; Chris@42: static const plan_adt padt = { Chris@42: XM(dft_solve), awake, print, destroy Chris@42: }; Chris@42: Chris@42: UNUSED(ego); Chris@42: Chris@42: if (!applicable(ego, p_, plnr)) Chris@42: return (plan *) 0; Chris@42: Chris@42: p = (const problem_mpi_dft *) p_; Chris@42: Chris@42: X(extract_reim)(p->sign, I = p->I, &ri, &ii); Chris@42: X(extract_reim)(p->sign, O = p->O, &ro, &io); Chris@42: if (ego->preserve_input || NO_DESTROY_INPUTP(plnr)) Chris@42: I = O; Chris@42: else { Chris@42: ro = ri; Chris@42: io = ii; Chris@42: } Chris@42: MPI_Comm_rank(p->comm, &my_pe); Chris@42: MPI_Comm_size(p->comm, &n_pes); Chris@42: Chris@42: sz = X(mktensor)(p->sz->rnk - 1); /* tensor of last rnk-1 dimensions */ Chris@42: i = p->sz->rnk - 2; A(i >= 0); Chris@42: sz->dims[i].n = p->sz->dims[i+1].n; Chris@42: sz->dims[i].is = sz->dims[i].os = 2 * p->vn; Chris@42: for (--i; i >= 0; --i) { Chris@42: sz->dims[i].n = p->sz->dims[i+1].n; Chris@42: sz->dims[i].is = sz->dims[i].os = sz->dims[i+1].n * sz->dims[i+1].is; Chris@42: } Chris@42: nrest = 1; for (i = 1; i < sz->rnk; ++i) nrest *= sz->dims[i].n; Chris@42: { Chris@42: INT is = sz->dims[0].n * sz->dims[0].is; Chris@42: INT b = XM(block)(p->sz->dims[0].n, p->sz->dims[0].b[IB], my_pe); Chris@42: cld1 = X(mkplan_d)(plnr, Chris@42: X(mkproblem_dft_d)(sz, Chris@42: X(mktensor_2d)(b, is, is, Chris@42: p->vn, 2, 2), Chris@42: ri, ii, ro, io)); Chris@42: if (XM(any_true)(!cld1, p->comm)) goto nada; Chris@42: } Chris@42: Chris@42: nrest *= p->vn; Chris@42: cldt = X(mkplan_d)(plnr, Chris@42: XM(mkproblem_transpose)( Chris@42: p->sz->dims[0].n, p->sz->dims[1].n, nrest * 2, Chris@42: I, O, Chris@42: p->sz->dims[0].b[IB], p->sz->dims[1].b[OB], Chris@42: p->comm, 0)); Chris@42: if (XM(any_true)(!cldt, p->comm)) goto nada; Chris@42: Chris@42: X(extract_reim)(p->sign, O, &ro, &io); Chris@42: { Chris@42: INT is = p->sz->dims[0].n * nrest * 2; Chris@42: INT b = XM(block)(p->sz->dims[1].n, p->sz->dims[1].b[OB], my_pe); Chris@42: cld2 = X(mkplan_d)(plnr, Chris@42: X(mkproblem_dft_d)(X(mktensor_1d)( Chris@42: p->sz->dims[0].n, Chris@42: nrest * 2, nrest * 2), Chris@42: X(mktensor_2d)(b, is, is, Chris@42: nrest, 2, 2), Chris@42: ro, io, ro, io)); Chris@42: if (XM(any_true)(!cld2, p->comm)) goto nada; Chris@42: } Chris@42: Chris@42: pln = MKPLAN_MPI_DFT(P, &padt, apply); Chris@42: pln->cld1 = cld1; Chris@42: pln->cldt = cldt; Chris@42: pln->cld2 = cld2; Chris@42: pln->preserve_input = ego->preserve_input ? 2 : NO_DESTROY_INPUTP(plnr); Chris@42: pln->roff = ri - p->I; Chris@42: pln->ioff = ii - p->I; Chris@42: Chris@42: X(ops_add)(&cld1->ops, &cld2->ops, &pln->super.super.ops); Chris@42: X(ops_add2)(&cldt->ops, &pln->super.super.ops); Chris@42: Chris@42: return &(pln->super.super); Chris@42: Chris@42: nada: Chris@42: X(plan_destroy_internal)(cld2); Chris@42: X(plan_destroy_internal)(cldt); Chris@42: X(plan_destroy_internal)(cld1); Chris@42: return (plan *) 0; Chris@42: } Chris@42: Chris@42: static solver *mksolver(int preserve_input) Chris@42: { Chris@42: static const solver_adt sadt = { PROBLEM_MPI_DFT, mkplan, 0 }; Chris@42: S *slv = MKSOLVER(S, &sadt); Chris@42: slv->preserve_input = preserve_input; Chris@42: return &(slv->super); Chris@42: } Chris@42: Chris@42: void XM(dft_rank_geq2_transposed_register)(planner *p) Chris@42: { Chris@42: int preserve_input; Chris@42: for (preserve_input = 0; preserve_input <= 1; ++preserve_input) Chris@42: REGISTER_SOLVER(p, mksolver(preserve_input)); Chris@42: }