| Chris@10 | 1 /* | 
| Chris@10 | 2  * Copyright (c) 2003, 2007-11 Matteo Frigo | 
| Chris@10 | 3  * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology | 
| Chris@10 | 4  * | 
| Chris@10 | 5  * This program is free software; you can redistribute it and/or modify | 
| Chris@10 | 6  * it under the terms of the GNU General Public License as published by | 
| Chris@10 | 7  * the Free Software Foundation; either version 2 of the License, or | 
| Chris@10 | 8  * (at your option) any later version. | 
| Chris@10 | 9  * | 
| Chris@10 | 10  * This program is distributed in the hope that it will be useful, | 
| Chris@10 | 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| Chris@10 | 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| Chris@10 | 13  * GNU General Public License for more details. | 
| Chris@10 | 14  * | 
| Chris@10 | 15  * You should have received a copy of the GNU General Public License | 
| Chris@10 | 16  * along with this program; if not, write to the Free Software | 
| Chris@10 | 17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA | 
| Chris@10 | 18  * | 
| Chris@10 | 19  */ | 
| Chris@10 | 20 | 
| Chris@10 | 21 /* plans for distributed out-of-place transpose using MPI_Alltoall, | 
| Chris@10 | 22    and which destroy the input array (unless TRANSPOSED_IN is used) */ | 
| Chris@10 | 23 | 
| Chris@10 | 24 #include "mpi-transpose.h" | 
| Chris@10 | 25 #include <string.h> | 
| Chris@10 | 26 | 
| Chris@10 | 27 typedef struct { | 
| Chris@10 | 28      solver super; | 
| Chris@10 | 29      int copy_transposed_in; /* whether to copy the input for TRANSPOSED_IN, | 
| Chris@10 | 30 				which makes the final transpose out-of-place | 
| Chris@10 | 31 				but costs an extra copy and requires us | 
| Chris@10 | 32 				to destroy the input */ | 
| Chris@10 | 33 } S; | 
| Chris@10 | 34 | 
| Chris@10 | 35 typedef struct { | 
| Chris@10 | 36      plan_mpi_transpose super; | 
| Chris@10 | 37 | 
| Chris@10 | 38      plan *cld1, *cld2, *cld2rest, *cld3; | 
| Chris@10 | 39 | 
| Chris@10 | 40      MPI_Comm comm; | 
| Chris@10 | 41      int *send_block_sizes, *send_block_offsets; | 
| Chris@10 | 42      int *recv_block_sizes, *recv_block_offsets; | 
| Chris@10 | 43 | 
| Chris@10 | 44      INT rest_Ioff, rest_Ooff; | 
| Chris@10 | 45 | 
| Chris@10 | 46      int equal_blocks; | 
| Chris@10 | 47 } P; | 
| Chris@10 | 48 | 
| Chris@10 | 49 static void apply(const plan *ego_, R *I, R *O) | 
| Chris@10 | 50 { | 
| Chris@10 | 51      const P *ego = (const P *) ego_; | 
| Chris@10 | 52      plan_rdft *cld1, *cld2, *cld2rest, *cld3; | 
| Chris@10 | 53 | 
| Chris@10 | 54      /* transpose locally to get contiguous chunks */ | 
| Chris@10 | 55      cld1 = (plan_rdft *) ego->cld1; | 
| Chris@10 | 56      if (cld1) { | 
| Chris@10 | 57 	  cld1->apply(ego->cld1, I, O); | 
| Chris@10 | 58 | 
| Chris@10 | 59 	  /* transpose chunks globally */ | 
| Chris@10 | 60 	  if (ego->equal_blocks) | 
| Chris@10 | 61 	       MPI_Alltoall(O, ego->send_block_sizes[0], FFTW_MPI_TYPE, | 
| Chris@10 | 62 			    I, ego->recv_block_sizes[0], FFTW_MPI_TYPE, | 
| Chris@10 | 63 			    ego->comm); | 
| Chris@10 | 64 	  else | 
| Chris@10 | 65 	       MPI_Alltoallv(O, ego->send_block_sizes, ego->send_block_offsets, | 
| Chris@10 | 66 			     FFTW_MPI_TYPE, | 
| Chris@10 | 67 			     I, ego->recv_block_sizes, ego->recv_block_offsets, | 
| Chris@10 | 68 			     FFTW_MPI_TYPE, | 
| Chris@10 | 69 			     ego->comm); | 
| Chris@10 | 70      } | 
| Chris@10 | 71      else { /* TRANSPOSED_IN, no need to destroy input */ | 
| Chris@10 | 72 	  /* transpose chunks globally */ | 
| Chris@10 | 73 	  if (ego->equal_blocks) | 
| Chris@10 | 74 	       MPI_Alltoall(I, ego->send_block_sizes[0], FFTW_MPI_TYPE, | 
| Chris@10 | 75 			    O, ego->recv_block_sizes[0], FFTW_MPI_TYPE, | 
| Chris@10 | 76 			    ego->comm); | 
| Chris@10 | 77 	  else | 
| Chris@10 | 78 	       MPI_Alltoallv(I, ego->send_block_sizes, ego->send_block_offsets, | 
| Chris@10 | 79 			     FFTW_MPI_TYPE, | 
| Chris@10 | 80 			     O, ego->recv_block_sizes, ego->recv_block_offsets, | 
| Chris@10 | 81 			     FFTW_MPI_TYPE, | 
| Chris@10 | 82 			     ego->comm); | 
| Chris@10 | 83 	  I = O; /* final transpose (if any) is in-place */ | 
| Chris@10 | 84      } | 
| Chris@10 | 85 | 
| Chris@10 | 86      /* transpose locally, again, to get ordinary row-major */ | 
| Chris@10 | 87      cld2 = (plan_rdft *) ego->cld2; | 
| Chris@10 | 88      if (cld2) { | 
| Chris@10 | 89 	  cld2->apply(ego->cld2, I, O); | 
| Chris@10 | 90 	  cld2rest = (plan_rdft *) ego->cld2rest; | 
| Chris@10 | 91 	  if (cld2rest) { /* leftover from unequal block sizes */ | 
| Chris@10 | 92 	       cld2rest->apply(ego->cld2rest, | 
| Chris@10 | 93 			       I + ego->rest_Ioff, O + ego->rest_Ooff); | 
| Chris@10 | 94 	       cld3 = (plan_rdft *) ego->cld3; | 
| Chris@10 | 95 	       if (cld3) | 
| Chris@10 | 96 		    cld3->apply(ego->cld3, O, O); | 
| Chris@10 | 97 	       /* else TRANSPOSED_OUT is true and user wants O transposed */ | 
| Chris@10 | 98 	  } | 
| Chris@10 | 99      } | 
| Chris@10 | 100 } | 
| Chris@10 | 101 | 
| Chris@10 | 102 static int applicable(const S *ego, const problem *p_, | 
| Chris@10 | 103 		      const planner *plnr) | 
| Chris@10 | 104 { | 
| Chris@10 | 105      const problem_mpi_transpose *p = (const problem_mpi_transpose *) p_; | 
| Chris@10 | 106      return (1 | 
| Chris@10 | 107 	     && p->I != p->O | 
| Chris@10 | 108 	     && (!NO_DESTROY_INPUTP(plnr) || | 
| Chris@10 | 109 		 ((p->flags & TRANSPOSED_IN) && !ego->copy_transposed_in)) | 
| Chris@10 | 110 	     && ((p->flags & TRANSPOSED_IN) || !ego->copy_transposed_in) | 
| Chris@10 | 111 	     && ONLY_TRANSPOSEDP(p->flags) | 
| Chris@10 | 112 	  ); | 
| Chris@10 | 113 } | 
| Chris@10 | 114 | 
| Chris@10 | 115 static void awake(plan *ego_, enum wakefulness wakefulness) | 
| Chris@10 | 116 { | 
| Chris@10 | 117      P *ego = (P *) ego_; | 
| Chris@10 | 118      X(plan_awake)(ego->cld1, wakefulness); | 
| Chris@10 | 119      X(plan_awake)(ego->cld2, wakefulness); | 
| Chris@10 | 120      X(plan_awake)(ego->cld2rest, wakefulness); | 
| Chris@10 | 121      X(plan_awake)(ego->cld3, wakefulness); | 
| Chris@10 | 122 } | 
| Chris@10 | 123 | 
| Chris@10 | 124 static void destroy(plan *ego_) | 
| Chris@10 | 125 { | 
| Chris@10 | 126      P *ego = (P *) ego_; | 
| Chris@10 | 127      X(ifree0)(ego->send_block_sizes); | 
| Chris@10 | 128      MPI_Comm_free(&ego->comm); | 
| Chris@10 | 129      X(plan_destroy_internal)(ego->cld3); | 
| Chris@10 | 130      X(plan_destroy_internal)(ego->cld2rest); | 
| Chris@10 | 131      X(plan_destroy_internal)(ego->cld2); | 
| Chris@10 | 132      X(plan_destroy_internal)(ego->cld1); | 
| Chris@10 | 133 } | 
| Chris@10 | 134 | 
| Chris@10 | 135 static void print(const plan *ego_, printer *p) | 
| Chris@10 | 136 { | 
| Chris@10 | 137      const P *ego = (const P *) ego_; | 
| Chris@10 | 138      p->print(p, "(mpi-transpose-alltoall%s%(%p%)%(%p%)%(%p%)%(%p%))", | 
| Chris@10 | 139 	      ego->equal_blocks ? "/e" : "", | 
| Chris@10 | 140 	      ego->cld1, ego->cld2, ego->cld2rest, ego->cld3); | 
| Chris@10 | 141 } | 
| Chris@10 | 142 | 
| Chris@10 | 143 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr) | 
| Chris@10 | 144 { | 
| Chris@10 | 145      const S *ego = (const S *) ego_; | 
| Chris@10 | 146      const problem_mpi_transpose *p; | 
| Chris@10 | 147      P *pln; | 
| Chris@10 | 148      plan *cld1 = 0, *cld2 = 0, *cld2rest = 0, *cld3 = 0; | 
| Chris@10 | 149      INT b, bt, vn, rest_Ioff, rest_Ooff; | 
| Chris@10 | 150      R *I; | 
| Chris@10 | 151      int *sbs, *sbo, *rbs, *rbo; | 
| Chris@10 | 152      int pe, my_pe, n_pes; | 
| Chris@10 | 153      int equal_blocks = 1; | 
| Chris@10 | 154      static const plan_adt padt = { | 
| Chris@10 | 155           XM(transpose_solve), awake, print, destroy | 
| Chris@10 | 156      }; | 
| Chris@10 | 157 | 
| Chris@10 | 158      if (!applicable(ego, p_, plnr)) | 
| Chris@10 | 159           return (plan *) 0; | 
| Chris@10 | 160 | 
| Chris@10 | 161      p = (const problem_mpi_transpose *) p_; | 
| Chris@10 | 162      vn = p->vn; | 
| Chris@10 | 163 | 
| Chris@10 | 164      MPI_Comm_rank(p->comm, &my_pe); | 
| Chris@10 | 165      MPI_Comm_size(p->comm, &n_pes); | 
| Chris@10 | 166 | 
| Chris@10 | 167      b = XM(block)(p->nx, p->block, my_pe); | 
| Chris@10 | 168 | 
| Chris@10 | 169      if (p->flags & TRANSPOSED_IN) { /* I is already transposed */ | 
| Chris@10 | 170 	  if (ego->copy_transposed_in) { | 
| Chris@10 | 171 	       cld1 = X(mkplan_f_d)(plnr, | 
| Chris@10 | 172 				  X(mkproblem_rdft_0_d)(X(mktensor_1d) | 
| Chris@10 | 173 							(b * p->ny * vn, 1, 1), | 
| Chris@10 | 174 							I = p->I, p->O), | 
| Chris@10 | 175 				    0, 0, NO_SLOW); | 
| Chris@10 | 176 	       if (XM(any_true)(!cld1, p->comm)) goto nada; | 
| Chris@10 | 177 	  } | 
| Chris@10 | 178 	  else | 
| Chris@10 | 179 	       I = p->O; /* final transpose is in-place */ | 
| Chris@10 | 180      } | 
| Chris@10 | 181      else { /* transpose b x ny x vn -> ny x b x vn */ | 
| Chris@10 | 182 	  cld1 = X(mkplan_f_d)(plnr, | 
| Chris@10 | 183 			       X(mkproblem_rdft_0_d)(X(mktensor_3d) | 
| Chris@10 | 184 						     (b, p->ny * vn, vn, | 
| Chris@10 | 185 						      p->ny, vn, b * vn, | 
| Chris@10 | 186 						      vn, 1, 1), | 
| Chris@10 | 187 						     I = p->I, p->O), | 
| Chris@10 | 188 			       0, 0, NO_SLOW); | 
| Chris@10 | 189 	  if (XM(any_true)(!cld1, p->comm)) goto nada; | 
| Chris@10 | 190      } | 
| Chris@10 | 191 | 
| Chris@10 | 192      if (XM(any_true)(!XM(mkplans_posttranspose)(p, plnr, I, p->O, my_pe, | 
| Chris@10 | 193 						 &cld2, &cld2rest, &cld3, | 
| Chris@10 | 194 						 &rest_Ioff, &rest_Ooff), | 
| Chris@10 | 195 		      p->comm)) goto nada; | 
| Chris@10 | 196 | 
| Chris@10 | 197      pln = MKPLAN_MPI_TRANSPOSE(P, &padt, apply); | 
| Chris@10 | 198 | 
| Chris@10 | 199      pln->cld1 = cld1; | 
| Chris@10 | 200      pln->cld2 = cld2; | 
| Chris@10 | 201      pln->cld2rest = cld2rest; | 
| Chris@10 | 202      pln->rest_Ioff = rest_Ioff; | 
| Chris@10 | 203      pln->rest_Ooff = rest_Ooff; | 
| Chris@10 | 204      pln->cld3 = cld3; | 
| Chris@10 | 205 | 
| Chris@10 | 206      MPI_Comm_dup(p->comm, &pln->comm); | 
| Chris@10 | 207 | 
| Chris@10 | 208      /* Compute sizes/offsets of blocks to send for all-to-all command. */ | 
| Chris@10 | 209      sbs = (int *) MALLOC(4 * n_pes * sizeof(int), PLANS); | 
| Chris@10 | 210      sbo = sbs + n_pes; | 
| Chris@10 | 211      rbs = sbo + n_pes; | 
| Chris@10 | 212      rbo = rbs + n_pes; | 
| Chris@10 | 213      b = XM(block)(p->nx, p->block, my_pe); | 
| Chris@10 | 214      bt = XM(block)(p->ny, p->tblock, my_pe); | 
| Chris@10 | 215      for (pe = 0; pe < n_pes; ++pe) { | 
| Chris@10 | 216 	  INT db, dbt; /* destination block sizes */ | 
| Chris@10 | 217 	  db = XM(block)(p->nx, p->block, pe); | 
| Chris@10 | 218 	  dbt = XM(block)(p->ny, p->tblock, pe); | 
| Chris@10 | 219 	  if (db != p->block || dbt != p->tblock) | 
| Chris@10 | 220 	       equal_blocks = 0; | 
| Chris@10 | 221 | 
| Chris@10 | 222 	  /* MPI requires type "int" here; apparently it | 
| Chris@10 | 223 	     has no 64-bit API?  Grrr. */ | 
| Chris@10 | 224 	  sbs[pe] = (int) (b * dbt * vn); | 
| Chris@10 | 225 	  sbo[pe] = (int) (pe * (b * p->tblock) * vn); | 
| Chris@10 | 226 	  rbs[pe] = (int) (db * bt * vn); | 
| Chris@10 | 227 	  rbo[pe] = (int) (pe * (p->block * bt) * vn); | 
| Chris@10 | 228      } | 
| Chris@10 | 229      pln->send_block_sizes = sbs; | 
| Chris@10 | 230      pln->send_block_offsets = sbo; | 
| Chris@10 | 231      pln->recv_block_sizes = rbs; | 
| Chris@10 | 232      pln->recv_block_offsets = rbo; | 
| Chris@10 | 233      pln->equal_blocks = equal_blocks; | 
| Chris@10 | 234 | 
| Chris@10 | 235      X(ops_zero)(&pln->super.super.ops); | 
| Chris@10 | 236      if (cld1) X(ops_add2)(&cld1->ops, &pln->super.super.ops); | 
| Chris@10 | 237      if (cld2) X(ops_add2)(&cld2->ops, &pln->super.super.ops); | 
| Chris@10 | 238      if (cld2rest) X(ops_add2)(&cld2rest->ops, &pln->super.super.ops); | 
| Chris@10 | 239      if (cld3) X(ops_add2)(&cld3->ops, &pln->super.super.ops); | 
| Chris@10 | 240      /* FIXME: should MPI operations be counted in "other" somehow? */ | 
| Chris@10 | 241 | 
| Chris@10 | 242      return &(pln->super.super); | 
| Chris@10 | 243 | 
| Chris@10 | 244  nada: | 
| Chris@10 | 245      X(plan_destroy_internal)(cld3); | 
| Chris@10 | 246      X(plan_destroy_internal)(cld2rest); | 
| Chris@10 | 247      X(plan_destroy_internal)(cld2); | 
| Chris@10 | 248      X(plan_destroy_internal)(cld1); | 
| Chris@10 | 249      return (plan *) 0; | 
| Chris@10 | 250 } | 
| Chris@10 | 251 | 
| Chris@10 | 252 static solver *mksolver(int copy_transposed_in) | 
| Chris@10 | 253 { | 
| Chris@10 | 254      static const solver_adt sadt = { PROBLEM_MPI_TRANSPOSE, mkplan, 0 }; | 
| Chris@10 | 255      S *slv = MKSOLVER(S, &sadt); | 
| Chris@10 | 256      slv->copy_transposed_in = copy_transposed_in; | 
| Chris@10 | 257      return &(slv->super); | 
| Chris@10 | 258 } | 
| Chris@10 | 259 | 
| Chris@10 | 260 void XM(transpose_alltoall_register)(planner *p) | 
| Chris@10 | 261 { | 
| Chris@10 | 262      int cti; | 
| Chris@10 | 263      for (cti = 0; cti <= 1; ++cti) | 
| Chris@10 | 264 	  REGISTER_SOLVER(p, mksolver(cti)); | 
| Chris@10 | 265 } |