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: /* plans for distributed out-of-place transpose using MPI_Alltoall, Chris@42: and which destroy the input array (unless TRANSPOSED_IN is used) */ Chris@42: Chris@42: #include "mpi-transpose.h" Chris@42: #include Chris@42: Chris@42: typedef struct { Chris@42: solver super; Chris@42: int copy_transposed_in; /* whether to copy the input for TRANSPOSED_IN, Chris@42: which makes the final transpose out-of-place Chris@42: but costs an extra copy and requires us Chris@42: to destroy the input */ Chris@42: } S; Chris@42: Chris@42: typedef struct { Chris@42: plan_mpi_transpose super; Chris@42: Chris@42: plan *cld1, *cld2, *cld2rest, *cld3; Chris@42: Chris@42: MPI_Comm comm; Chris@42: int *send_block_sizes, *send_block_offsets; Chris@42: int *recv_block_sizes, *recv_block_offsets; Chris@42: Chris@42: INT rest_Ioff, rest_Ooff; Chris@42: Chris@42: int equal_blocks; 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_rdft *cld1, *cld2, *cld2rest, *cld3; Chris@42: Chris@42: /* transpose locally to get contiguous chunks */ Chris@42: cld1 = (plan_rdft *) ego->cld1; Chris@42: if (cld1) { Chris@42: cld1->apply(ego->cld1, I, O); Chris@42: Chris@42: /* transpose chunks globally */ Chris@42: if (ego->equal_blocks) Chris@42: MPI_Alltoall(O, ego->send_block_sizes[0], FFTW_MPI_TYPE, Chris@42: I, ego->recv_block_sizes[0], FFTW_MPI_TYPE, Chris@42: ego->comm); Chris@42: else Chris@42: MPI_Alltoallv(O, ego->send_block_sizes, ego->send_block_offsets, Chris@42: FFTW_MPI_TYPE, Chris@42: I, ego->recv_block_sizes, ego->recv_block_offsets, Chris@42: FFTW_MPI_TYPE, Chris@42: ego->comm); Chris@42: } Chris@42: else { /* TRANSPOSED_IN, no need to destroy input */ Chris@42: /* transpose chunks globally */ Chris@42: if (ego->equal_blocks) Chris@42: MPI_Alltoall(I, ego->send_block_sizes[0], FFTW_MPI_TYPE, Chris@42: O, ego->recv_block_sizes[0], FFTW_MPI_TYPE, Chris@42: ego->comm); Chris@42: else Chris@42: MPI_Alltoallv(I, ego->send_block_sizes, ego->send_block_offsets, Chris@42: FFTW_MPI_TYPE, Chris@42: O, ego->recv_block_sizes, ego->recv_block_offsets, Chris@42: FFTW_MPI_TYPE, Chris@42: ego->comm); Chris@42: I = O; /* final transpose (if any) is in-place */ Chris@42: } Chris@42: Chris@42: /* transpose locally, again, to get ordinary row-major */ Chris@42: cld2 = (plan_rdft *) ego->cld2; Chris@42: if (cld2) { Chris@42: cld2->apply(ego->cld2, I, O); Chris@42: cld2rest = (plan_rdft *) ego->cld2rest; Chris@42: if (cld2rest) { /* leftover from unequal block sizes */ Chris@42: cld2rest->apply(ego->cld2rest, Chris@42: I + ego->rest_Ioff, O + ego->rest_Ooff); Chris@42: cld3 = (plan_rdft *) ego->cld3; Chris@42: if (cld3) Chris@42: cld3->apply(ego->cld3, O, O); Chris@42: /* else TRANSPOSED_OUT is true and user wants O transposed */ Chris@42: } Chris@42: } 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_transpose *p = (const problem_mpi_transpose *) p_; Chris@42: return (1 Chris@42: && p->I != p->O Chris@42: && (!NO_DESTROY_INPUTP(plnr) || Chris@42: ((p->flags & TRANSPOSED_IN) && !ego->copy_transposed_in)) Chris@42: && ((p->flags & TRANSPOSED_IN) || !ego->copy_transposed_in) Chris@42: && ONLY_TRANSPOSEDP(p->flags) 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->cld2, wakefulness); Chris@42: X(plan_awake)(ego->cld2rest, wakefulness); Chris@42: X(plan_awake)(ego->cld3, wakefulness); Chris@42: } Chris@42: Chris@42: static void destroy(plan *ego_) Chris@42: { Chris@42: P *ego = (P *) ego_; Chris@42: X(ifree0)(ego->send_block_sizes); Chris@42: MPI_Comm_free(&ego->comm); Chris@42: X(plan_destroy_internal)(ego->cld3); Chris@42: X(plan_destroy_internal)(ego->cld2rest); Chris@42: X(plan_destroy_internal)(ego->cld2); 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-transpose-alltoall%s%(%p%)%(%p%)%(%p%)%(%p%))", Chris@42: ego->equal_blocks ? "/e" : "", Chris@42: ego->cld1, ego->cld2, ego->cld2rest, ego->cld3); 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_transpose *p; Chris@42: P *pln; Chris@42: plan *cld1 = 0, *cld2 = 0, *cld2rest = 0, *cld3 = 0; Chris@42: INT b, bt, vn, rest_Ioff, rest_Ooff; Chris@42: R *I; Chris@42: int *sbs, *sbo, *rbs, *rbo; Chris@42: int pe, my_pe, n_pes; Chris@42: int equal_blocks = 1; Chris@42: static const plan_adt padt = { Chris@42: XM(transpose_solve), awake, print, destroy Chris@42: }; Chris@42: Chris@42: if (!applicable(ego, p_, plnr)) Chris@42: return (plan *) 0; Chris@42: Chris@42: p = (const problem_mpi_transpose *) p_; Chris@42: vn = p->vn; Chris@42: Chris@42: MPI_Comm_rank(p->comm, &my_pe); Chris@42: MPI_Comm_size(p->comm, &n_pes); Chris@42: Chris@42: b = XM(block)(p->nx, p->block, my_pe); Chris@42: Chris@42: if (p->flags & TRANSPOSED_IN) { /* I is already transposed */ Chris@42: if (ego->copy_transposed_in) { Chris@42: cld1 = X(mkplan_f_d)(plnr, Chris@42: X(mkproblem_rdft_0_d)(X(mktensor_1d) Chris@42: (b * p->ny * vn, 1, 1), Chris@42: I = p->I, p->O), Chris@42: 0, 0, NO_SLOW); Chris@42: if (XM(any_true)(!cld1, p->comm)) goto nada; Chris@42: } Chris@42: else Chris@42: I = p->O; /* final transpose is in-place */ Chris@42: } Chris@42: else { /* transpose b x ny x vn -> ny x b x vn */ Chris@42: cld1 = X(mkplan_f_d)(plnr, Chris@42: X(mkproblem_rdft_0_d)(X(mktensor_3d) Chris@42: (b, p->ny * vn, vn, Chris@42: p->ny, vn, b * vn, Chris@42: vn, 1, 1), Chris@42: I = p->I, p->O), Chris@42: 0, 0, NO_SLOW); Chris@42: if (XM(any_true)(!cld1, p->comm)) goto nada; Chris@42: } Chris@42: Chris@42: if (XM(any_true)(!XM(mkplans_posttranspose)(p, plnr, I, p->O, my_pe, Chris@42: &cld2, &cld2rest, &cld3, Chris@42: &rest_Ioff, &rest_Ooff), Chris@42: p->comm)) goto nada; Chris@42: Chris@42: pln = MKPLAN_MPI_TRANSPOSE(P, &padt, apply); Chris@42: Chris@42: pln->cld1 = cld1; Chris@42: pln->cld2 = cld2; Chris@42: pln->cld2rest = cld2rest; Chris@42: pln->rest_Ioff = rest_Ioff; Chris@42: pln->rest_Ooff = rest_Ooff; Chris@42: pln->cld3 = cld3; Chris@42: Chris@42: MPI_Comm_dup(p->comm, &pln->comm); Chris@42: Chris@42: /* Compute sizes/offsets of blocks to send for all-to-all command. */ Chris@42: sbs = (int *) MALLOC(4 * n_pes * sizeof(int), PLANS); Chris@42: sbo = sbs + n_pes; Chris@42: rbs = sbo + n_pes; Chris@42: rbo = rbs + n_pes; Chris@42: b = XM(block)(p->nx, p->block, my_pe); Chris@42: bt = XM(block)(p->ny, p->tblock, my_pe); Chris@42: for (pe = 0; pe < n_pes; ++pe) { Chris@42: INT db, dbt; /* destination block sizes */ Chris@42: db = XM(block)(p->nx, p->block, pe); Chris@42: dbt = XM(block)(p->ny, p->tblock, pe); Chris@42: if (db != p->block || dbt != p->tblock) Chris@42: equal_blocks = 0; Chris@42: Chris@42: /* MPI requires type "int" here; apparently it Chris@42: has no 64-bit API? Grrr. */ Chris@42: sbs[pe] = (int) (b * dbt * vn); Chris@42: sbo[pe] = (int) (pe * (b * p->tblock) * vn); Chris@42: rbs[pe] = (int) (db * bt * vn); Chris@42: rbo[pe] = (int) (pe * (p->block * bt) * vn); Chris@42: } Chris@42: pln->send_block_sizes = sbs; Chris@42: pln->send_block_offsets = sbo; Chris@42: pln->recv_block_sizes = rbs; Chris@42: pln->recv_block_offsets = rbo; Chris@42: pln->equal_blocks = equal_blocks; Chris@42: Chris@42: X(ops_zero)(&pln->super.super.ops); Chris@42: if (cld1) X(ops_add2)(&cld1->ops, &pln->super.super.ops); Chris@42: if (cld2) X(ops_add2)(&cld2->ops, &pln->super.super.ops); Chris@42: if (cld2rest) X(ops_add2)(&cld2rest->ops, &pln->super.super.ops); Chris@42: if (cld3) X(ops_add2)(&cld3->ops, &pln->super.super.ops); Chris@42: /* FIXME: should MPI operations be counted in "other" somehow? */ Chris@42: Chris@42: return &(pln->super.super); Chris@42: Chris@42: nada: Chris@42: X(plan_destroy_internal)(cld3); Chris@42: X(plan_destroy_internal)(cld2rest); Chris@42: X(plan_destroy_internal)(cld2); Chris@42: X(plan_destroy_internal)(cld1); Chris@42: return (plan *) 0; Chris@42: } Chris@42: Chris@42: static solver *mksolver(int copy_transposed_in) Chris@42: { Chris@42: static const solver_adt sadt = { PROBLEM_MPI_TRANSPOSE, mkplan, 0 }; Chris@42: S *slv = MKSOLVER(S, &sadt); Chris@42: slv->copy_transposed_in = copy_transposed_in; Chris@42: return &(slv->super); Chris@42: } Chris@42: Chris@42: void XM(transpose_alltoall_register)(planner *p) Chris@42: { Chris@42: int cti; Chris@42: for (cti = 0; cti <= 1; ++cti) Chris@42: REGISTER_SOLVER(p, mksolver(cti)); Chris@42: }