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 /* Recursive "radix-r" distributed transpose, which breaks a transpose
|
Chris@10
|
22 over p processes into p/r transposes over r processes plus r
|
Chris@10
|
23 transposes over p/r processes. If performed recursively, this
|
Chris@10
|
24 produces a total of O(p log p) messages vs. O(p^2) messages for a
|
Chris@10
|
25 direct approach.
|
Chris@10
|
26
|
Chris@10
|
27 However, this is not necessarily an improvement. The total size of
|
Chris@10
|
28 all the messages is actually increased from O(N) to O(N log p)
|
Chris@10
|
29 where N is the total data size. Also, the amount of local data
|
Chris@10
|
30 rearrangement is increased. So, it's not clear, a priori, what the
|
Chris@10
|
31 best algorithm will be, and we'll leave it to the planner. (In
|
Chris@10
|
32 theory and practice, it looks like this becomes advantageous for
|
Chris@10
|
33 large p, in the limit where the message sizes are small and
|
Chris@10
|
34 latency-dominated.)
|
Chris@10
|
35 */
|
Chris@10
|
36
|
Chris@10
|
37 #include "mpi-transpose.h"
|
Chris@10
|
38 #include <string.h>
|
Chris@10
|
39
|
Chris@10
|
40 typedef struct {
|
Chris@10
|
41 solver super;
|
Chris@10
|
42 int (*radix)(int np);
|
Chris@10
|
43 const char *nam;
|
Chris@10
|
44 int preserve_input; /* preserve input even if DESTROY_INPUT was passed */
|
Chris@10
|
45 } S;
|
Chris@10
|
46
|
Chris@10
|
47 typedef struct {
|
Chris@10
|
48 plan_mpi_transpose super;
|
Chris@10
|
49
|
Chris@10
|
50 plan *cld1, *cldtr, *cldtm;
|
Chris@10
|
51 int preserve_input;
|
Chris@10
|
52
|
Chris@10
|
53 int r; /* "radix" */
|
Chris@10
|
54 const char *nam;
|
Chris@10
|
55 } P;
|
Chris@10
|
56
|
Chris@10
|
57 static void apply(const plan *ego_, R *I, R *O)
|
Chris@10
|
58 {
|
Chris@10
|
59 const P *ego = (const P *) ego_;
|
Chris@10
|
60 plan_rdft *cld1, *cldtr, *cldtm;
|
Chris@10
|
61
|
Chris@10
|
62 cld1 = (plan_rdft *) ego->cld1;
|
Chris@10
|
63 if (cld1) cld1->apply((plan *) cld1, I, O);
|
Chris@10
|
64
|
Chris@10
|
65 if (ego->preserve_input) I = O;
|
Chris@10
|
66
|
Chris@10
|
67 cldtr = (plan_rdft *) ego->cldtr;
|
Chris@10
|
68 if (cldtr) cldtr->apply((plan *) cldtr, O, I);
|
Chris@10
|
69
|
Chris@10
|
70 cldtm = (plan_rdft *) ego->cldtm;
|
Chris@10
|
71 if (cldtm) cldtm->apply((plan *) cldtm, I, O);
|
Chris@10
|
72 }
|
Chris@10
|
73
|
Chris@10
|
74 static int radix_sqrt(int np)
|
Chris@10
|
75 {
|
Chris@10
|
76 int r;
|
Chris@10
|
77 for (r = (int) (X(isqrt)(np)); np % r != 0; ++r)
|
Chris@10
|
78 ;
|
Chris@10
|
79 return r;
|
Chris@10
|
80 }
|
Chris@10
|
81
|
Chris@10
|
82 static int radix_first(int np)
|
Chris@10
|
83 {
|
Chris@10
|
84 int r = (int) (X(first_divisor)(np));
|
Chris@10
|
85 return (r >= (int) (X(isqrt)(np)) ? 0 : r);
|
Chris@10
|
86 }
|
Chris@10
|
87
|
Chris@10
|
88 /* the local allocated space on process pe required for the given transpose
|
Chris@10
|
89 dimensions and block sizes */
|
Chris@10
|
90 static INT transpose_space(INT nx, INT ny, INT block, INT tblock, int pe)
|
Chris@10
|
91 {
|
Chris@10
|
92 return X(imax)(XM(block)(nx, block, pe) * ny,
|
Chris@10
|
93 nx * XM(block)(ny, tblock, pe));
|
Chris@10
|
94 }
|
Chris@10
|
95
|
Chris@10
|
96 /* check whether the recursive transposes fit within the space
|
Chris@10
|
97 that must have been allocated on each process for this transpose;
|
Chris@10
|
98 this must be modified if the subdivision in mkplan is changed! */
|
Chris@10
|
99 static int enough_space(INT nx, INT ny, INT block, INT tblock,
|
Chris@10
|
100 int r, int n_pes)
|
Chris@10
|
101 {
|
Chris@10
|
102 int pe;
|
Chris@10
|
103 int m = n_pes / r;
|
Chris@10
|
104 for (pe = 0; pe < n_pes; ++pe) {
|
Chris@10
|
105 INT space = transpose_space(nx, ny, block, tblock, pe);
|
Chris@10
|
106 INT b1 = XM(block)(nx, r * block, pe / r);
|
Chris@10
|
107 INT b2 = XM(block)(ny, m * tblock, pe % r);
|
Chris@10
|
108 if (transpose_space(b1, ny, block, m*tblock, pe % r) > space
|
Chris@10
|
109 || transpose_space(nx, b2, r*block, tblock, pe / r) > space)
|
Chris@10
|
110 return 0;
|
Chris@10
|
111 }
|
Chris@10
|
112 return 1;
|
Chris@10
|
113 }
|
Chris@10
|
114
|
Chris@10
|
115 /* In theory, transpose-recurse becomes advantageous for message sizes
|
Chris@10
|
116 below some minimum, assuming that the time is dominated by
|
Chris@10
|
117 communications. In practice, we want to constrain the minimum
|
Chris@10
|
118 message size for transpose-recurse to keep the planning time down.
|
Chris@10
|
119 I've set this conservatively according to some simple experiments
|
Chris@10
|
120 on a Cray XT3 where the crossover message size was 128, although on
|
Chris@10
|
121 a larger-latency machine the crossover will be larger. */
|
Chris@10
|
122 #define SMALL_MESSAGE 2048
|
Chris@10
|
123
|
Chris@10
|
124 static int applicable(const S *ego, const problem *p_,
|
Chris@10
|
125 const planner *plnr, int *r)
|
Chris@10
|
126 {
|
Chris@10
|
127 const problem_mpi_transpose *p = (const problem_mpi_transpose *) p_;
|
Chris@10
|
128 int n_pes;
|
Chris@10
|
129 MPI_Comm_size(p->comm, &n_pes);
|
Chris@10
|
130 return (1
|
Chris@10
|
131 && p->tblock * n_pes == p->ny
|
Chris@10
|
132 && (!ego->preserve_input || (!NO_DESTROY_INPUTP(plnr)
|
Chris@10
|
133 && p->I != p->O))
|
Chris@10
|
134 && (*r = ego->radix(n_pes)) && *r < n_pes && *r > 1
|
Chris@10
|
135 && enough_space(p->nx, p->ny, p->block, p->tblock, *r, n_pes)
|
Chris@10
|
136 && (!CONSERVE_MEMORYP(plnr) || *r > 8
|
Chris@10
|
137 || !X(toobig)((p->nx * (p->ny / n_pes) * p->vn) / *r))
|
Chris@10
|
138 && (!NO_SLOWP(plnr) ||
|
Chris@10
|
139 (p->nx * (p->ny / n_pes) * p->vn) / n_pes <= SMALL_MESSAGE)
|
Chris@10
|
140 && ONLY_TRANSPOSEDP(p->flags)
|
Chris@10
|
141 );
|
Chris@10
|
142 }
|
Chris@10
|
143
|
Chris@10
|
144 static void awake(plan *ego_, enum wakefulness wakefulness)
|
Chris@10
|
145 {
|
Chris@10
|
146 P *ego = (P *) ego_;
|
Chris@10
|
147 X(plan_awake)(ego->cld1, wakefulness);
|
Chris@10
|
148 X(plan_awake)(ego->cldtr, wakefulness);
|
Chris@10
|
149 X(plan_awake)(ego->cldtm, wakefulness);
|
Chris@10
|
150 }
|
Chris@10
|
151
|
Chris@10
|
152 static void destroy(plan *ego_)
|
Chris@10
|
153 {
|
Chris@10
|
154 P *ego = (P *) ego_;
|
Chris@10
|
155 X(plan_destroy_internal)(ego->cldtm);
|
Chris@10
|
156 X(plan_destroy_internal)(ego->cldtr);
|
Chris@10
|
157 X(plan_destroy_internal)(ego->cld1);
|
Chris@10
|
158 }
|
Chris@10
|
159
|
Chris@10
|
160 static void print(const plan *ego_, printer *p)
|
Chris@10
|
161 {
|
Chris@10
|
162 const P *ego = (const P *) ego_;
|
Chris@10
|
163 p->print(p, "(mpi-transpose-recurse/%s/%d%s%(%p%)%(%p%)%(%p%))",
|
Chris@10
|
164 ego->nam, ego->r, ego->preserve_input==2 ?"/p":"",
|
Chris@10
|
165 ego->cld1, ego->cldtr, ego->cldtm);
|
Chris@10
|
166 }
|
Chris@10
|
167
|
Chris@10
|
168 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
|
Chris@10
|
169 {
|
Chris@10
|
170 const S *ego = (const S *) ego_;
|
Chris@10
|
171 const problem_mpi_transpose *p;
|
Chris@10
|
172 P *pln;
|
Chris@10
|
173 plan *cld1 = 0, *cldtr = 0, *cldtm = 0;
|
Chris@10
|
174 R *I, *O;
|
Chris@10
|
175 int me, np, r, m;
|
Chris@10
|
176 INT b;
|
Chris@10
|
177 MPI_Comm comm2;
|
Chris@10
|
178 static const plan_adt padt = {
|
Chris@10
|
179 XM(transpose_solve), awake, print, destroy
|
Chris@10
|
180 };
|
Chris@10
|
181
|
Chris@10
|
182 UNUSED(ego);
|
Chris@10
|
183
|
Chris@10
|
184 if (!applicable(ego, p_, plnr, &r))
|
Chris@10
|
185 return (plan *) 0;
|
Chris@10
|
186
|
Chris@10
|
187 p = (const problem_mpi_transpose *) p_;
|
Chris@10
|
188
|
Chris@10
|
189 MPI_Comm_size(p->comm, &np);
|
Chris@10
|
190 MPI_Comm_rank(p->comm, &me);
|
Chris@10
|
191 m = np / r;
|
Chris@10
|
192 A(r * m == np);
|
Chris@10
|
193
|
Chris@10
|
194 I = p->I; O = p->O;
|
Chris@10
|
195
|
Chris@10
|
196 b = XM(block)(p->nx, p->block, me);
|
Chris@10
|
197 A(p->tblock * np == p->ny); /* this is currently required for cld1 */
|
Chris@10
|
198 if (p->flags & TRANSPOSED_IN) {
|
Chris@10
|
199 /* m x r x (bt x b x vn) -> r x m x (bt x b x vn) */
|
Chris@10
|
200 INT vn = p->vn * b * p->tblock;
|
Chris@10
|
201 cld1 = X(mkplan_f_d)(plnr,
|
Chris@10
|
202 X(mkproblem_rdft_0_d)(X(mktensor_3d)
|
Chris@10
|
203 (m, r*vn, vn,
|
Chris@10
|
204 r, vn, m*vn,
|
Chris@10
|
205 vn, 1, 1),
|
Chris@10
|
206 I, O),
|
Chris@10
|
207 0, 0, NO_SLOW);
|
Chris@10
|
208 }
|
Chris@10
|
209 else if (I != O) { /* combine cld1 with TRANSPOSED_IN permutation */
|
Chris@10
|
210 /* b x m x r x bt x vn -> r x m x bt x b x vn */
|
Chris@10
|
211 INT vn = p->vn;
|
Chris@10
|
212 INT bt = p->tblock;
|
Chris@10
|
213 cld1 = X(mkplan_f_d)(plnr,
|
Chris@10
|
214 X(mkproblem_rdft_0_d)(X(mktensor_5d)
|
Chris@10
|
215 (b, m*r*bt*vn, vn,
|
Chris@10
|
216 m, r*bt*vn, bt*b*vn,
|
Chris@10
|
217 r, bt*vn, m*bt*b*vn,
|
Chris@10
|
218 bt, vn, b*vn,
|
Chris@10
|
219 vn, 1, 1),
|
Chris@10
|
220 I, O),
|
Chris@10
|
221 0, 0, NO_SLOW);
|
Chris@10
|
222 }
|
Chris@10
|
223 else { /* TRANSPOSED_IN permutation must be separate for in-place */
|
Chris@10
|
224 /* b x (m x r) x bt x vn -> b x (r x m) x bt x vn */
|
Chris@10
|
225 INT vn = p->vn * p->tblock;
|
Chris@10
|
226 cld1 = X(mkplan_f_d)(plnr,
|
Chris@10
|
227 X(mkproblem_rdft_0_d)(X(mktensor_4d)
|
Chris@10
|
228 (m, r*vn, vn,
|
Chris@10
|
229 r, vn, m*vn,
|
Chris@10
|
230 vn, 1, 1,
|
Chris@10
|
231 b, np*vn, np*vn),
|
Chris@10
|
232 I, O),
|
Chris@10
|
233 0, 0, NO_SLOW);
|
Chris@10
|
234 }
|
Chris@10
|
235 if (XM(any_true)(!cld1, p->comm)) goto nada;
|
Chris@10
|
236
|
Chris@10
|
237 if (ego->preserve_input || NO_DESTROY_INPUTP(plnr)) I = O;
|
Chris@10
|
238
|
Chris@10
|
239 b = XM(block)(p->nx, r * p->block, me / r);
|
Chris@10
|
240 MPI_Comm_split(p->comm, me / r, me, &comm2);
|
Chris@10
|
241 if (b)
|
Chris@10
|
242 cldtr = X(mkplan_d)(plnr, XM(mkproblem_transpose)
|
Chris@10
|
243 (b, p->ny, p->vn,
|
Chris@10
|
244 O, I, p->block, m * p->tblock, comm2,
|
Chris@10
|
245 p->I != p->O
|
Chris@10
|
246 ? TRANSPOSED_IN : (p->flags & TRANSPOSED_IN)));
|
Chris@10
|
247 MPI_Comm_free(&comm2);
|
Chris@10
|
248 if (XM(any_true)(b && !cldtr, p->comm)) goto nada;
|
Chris@10
|
249
|
Chris@10
|
250 b = XM(block)(p->ny, m * p->tblock, me % r);
|
Chris@10
|
251 MPI_Comm_split(p->comm, me % r, me, &comm2);
|
Chris@10
|
252 if (b)
|
Chris@10
|
253 cldtm = X(mkplan_d)(plnr, XM(mkproblem_transpose)
|
Chris@10
|
254 (p->nx, b, p->vn,
|
Chris@10
|
255 I, O, r * p->block, p->tblock, comm2,
|
Chris@10
|
256 TRANSPOSED_IN | (p->flags & TRANSPOSED_OUT)));
|
Chris@10
|
257 MPI_Comm_free(&comm2);
|
Chris@10
|
258 if (XM(any_true)(b && !cldtm, p->comm)) goto nada;
|
Chris@10
|
259
|
Chris@10
|
260 pln = MKPLAN_MPI_TRANSPOSE(P, &padt, apply);
|
Chris@10
|
261
|
Chris@10
|
262 pln->cld1 = cld1;
|
Chris@10
|
263 pln->cldtr = cldtr;
|
Chris@10
|
264 pln->cldtm = cldtm;
|
Chris@10
|
265 pln->preserve_input = ego->preserve_input ? 2 : NO_DESTROY_INPUTP(plnr);
|
Chris@10
|
266 pln->r = r;
|
Chris@10
|
267 pln->nam = ego->nam;
|
Chris@10
|
268
|
Chris@10
|
269 pln->super.super.ops = cld1->ops;
|
Chris@10
|
270 if (cldtr) X(ops_add2)(&cldtr->ops, &pln->super.super.ops);
|
Chris@10
|
271 if (cldtm) X(ops_add2)(&cldtm->ops, &pln->super.super.ops);
|
Chris@10
|
272
|
Chris@10
|
273 return &(pln->super.super);
|
Chris@10
|
274
|
Chris@10
|
275 nada:
|
Chris@10
|
276 X(plan_destroy_internal)(cldtm);
|
Chris@10
|
277 X(plan_destroy_internal)(cldtr);
|
Chris@10
|
278 X(plan_destroy_internal)(cld1);
|
Chris@10
|
279 return (plan *) 0;
|
Chris@10
|
280 }
|
Chris@10
|
281
|
Chris@10
|
282 static solver *mksolver(int preserve_input,
|
Chris@10
|
283 int (*radix)(int np), const char *nam)
|
Chris@10
|
284 {
|
Chris@10
|
285 static const solver_adt sadt = { PROBLEM_MPI_TRANSPOSE, mkplan, 0 };
|
Chris@10
|
286 S *slv = MKSOLVER(S, &sadt);
|
Chris@10
|
287 slv->preserve_input = preserve_input;
|
Chris@10
|
288 slv->radix = radix;
|
Chris@10
|
289 slv->nam = nam;
|
Chris@10
|
290 return &(slv->super);
|
Chris@10
|
291 }
|
Chris@10
|
292
|
Chris@10
|
293 void XM(transpose_recurse_register)(planner *p)
|
Chris@10
|
294 {
|
Chris@10
|
295 int preserve_input;
|
Chris@10
|
296 for (preserve_input = 0; preserve_input <= 1; ++preserve_input) {
|
Chris@10
|
297 REGISTER_SOLVER(p, mksolver(preserve_input, radix_sqrt, "sqrt"));
|
Chris@10
|
298 REGISTER_SOLVER(p, mksolver(preserve_input, radix_first, "first"));
|
Chris@10
|
299 }
|
Chris@10
|
300 }
|