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 /* Distributed transposes using a sequence of carefully scheduled
|
Chris@10
|
22 pairwise exchanges. This has the advantage that it can be done
|
Chris@10
|
23 in-place, or out-of-place while preserving the input, using buffer
|
Chris@10
|
24 space proportional to the local size divided by the number of
|
Chris@10
|
25 processes (i.e. to the total array size divided by the number of
|
Chris@10
|
26 processes squared). */
|
Chris@10
|
27
|
Chris@10
|
28 #include "mpi-transpose.h"
|
Chris@10
|
29 #include <string.h>
|
Chris@10
|
30
|
Chris@10
|
31 typedef struct {
|
Chris@10
|
32 solver super;
|
Chris@10
|
33 int preserve_input; /* preserve input even if DESTROY_INPUT was passed */
|
Chris@10
|
34 } S;
|
Chris@10
|
35
|
Chris@10
|
36 typedef struct {
|
Chris@10
|
37 plan_mpi_transpose super;
|
Chris@10
|
38
|
Chris@10
|
39 plan *cld1, *cld2, *cld2rest, *cld3;
|
Chris@10
|
40 INT rest_Ioff, rest_Ooff;
|
Chris@10
|
41
|
Chris@10
|
42 int n_pes, my_pe, *sched;
|
Chris@10
|
43 INT *send_block_sizes, *send_block_offsets;
|
Chris@10
|
44 INT *recv_block_sizes, *recv_block_offsets;
|
Chris@10
|
45 MPI_Comm comm;
|
Chris@10
|
46 int preserve_input;
|
Chris@10
|
47 } P;
|
Chris@10
|
48
|
Chris@10
|
49 static void transpose_chunks(int *sched, int n_pes, int my_pe,
|
Chris@10
|
50 INT *sbs, INT *sbo, INT *rbs, INT *rbo,
|
Chris@10
|
51 MPI_Comm comm,
|
Chris@10
|
52 R *I, R *O)
|
Chris@10
|
53 {
|
Chris@10
|
54 if (sched) {
|
Chris@10
|
55 int i;
|
Chris@10
|
56 MPI_Status status;
|
Chris@10
|
57
|
Chris@10
|
58 /* TODO: explore non-synchronous send/recv? */
|
Chris@10
|
59
|
Chris@10
|
60 if (I == O) {
|
Chris@10
|
61 R *buf = (R*) MALLOC(sizeof(R) * sbs[0], BUFFERS);
|
Chris@10
|
62
|
Chris@10
|
63 for (i = 0; i < n_pes; ++i) {
|
Chris@10
|
64 int pe = sched[i];
|
Chris@10
|
65 if (my_pe == pe) {
|
Chris@10
|
66 if (rbo[pe] != sbo[pe])
|
Chris@10
|
67 memmove(O + rbo[pe], O + sbo[pe],
|
Chris@10
|
68 sbs[pe] * sizeof(R));
|
Chris@10
|
69 }
|
Chris@10
|
70 else {
|
Chris@10
|
71 memcpy(buf, O + sbo[pe], sbs[pe] * sizeof(R));
|
Chris@10
|
72 MPI_Sendrecv(buf, (int) (sbs[pe]), FFTW_MPI_TYPE,
|
Chris@10
|
73 pe, (my_pe * n_pes + pe) & 0xffff,
|
Chris@10
|
74 O + rbo[pe], (int) (rbs[pe]),
|
Chris@10
|
75 FFTW_MPI_TYPE,
|
Chris@10
|
76 pe, (pe * n_pes + my_pe) & 0xffff,
|
Chris@10
|
77 comm, &status);
|
Chris@10
|
78 }
|
Chris@10
|
79 }
|
Chris@10
|
80
|
Chris@10
|
81 X(ifree)(buf);
|
Chris@10
|
82 }
|
Chris@10
|
83 else { /* I != O */
|
Chris@10
|
84 for (i = 0; i < n_pes; ++i) {
|
Chris@10
|
85 int pe = sched[i];
|
Chris@10
|
86 if (my_pe == pe)
|
Chris@10
|
87 memcpy(O + rbo[pe], I + sbo[pe], sbs[pe] * sizeof(R));
|
Chris@10
|
88 else
|
Chris@10
|
89 MPI_Sendrecv(I + sbo[pe], (int) (sbs[pe]),
|
Chris@10
|
90 FFTW_MPI_TYPE,
|
Chris@10
|
91 pe, (my_pe * n_pes + pe) & 0xffff,
|
Chris@10
|
92 O + rbo[pe], (int) (rbs[pe]),
|
Chris@10
|
93 FFTW_MPI_TYPE,
|
Chris@10
|
94 pe, (pe * n_pes + my_pe) & 0xffff,
|
Chris@10
|
95 comm, &status);
|
Chris@10
|
96 }
|
Chris@10
|
97 }
|
Chris@10
|
98 }
|
Chris@10
|
99 }
|
Chris@10
|
100
|
Chris@10
|
101 static void apply(const plan *ego_, R *I, R *O)
|
Chris@10
|
102 {
|
Chris@10
|
103 const P *ego = (const P *) ego_;
|
Chris@10
|
104 plan_rdft *cld1, *cld2, *cld2rest, *cld3;
|
Chris@10
|
105
|
Chris@10
|
106 /* transpose locally to get contiguous chunks */
|
Chris@10
|
107 cld1 = (plan_rdft *) ego->cld1;
|
Chris@10
|
108 if (cld1) {
|
Chris@10
|
109 cld1->apply(ego->cld1, I, O);
|
Chris@10
|
110
|
Chris@10
|
111 if (ego->preserve_input) I = O;
|
Chris@10
|
112
|
Chris@10
|
113 /* transpose chunks globally */
|
Chris@10
|
114 transpose_chunks(ego->sched, ego->n_pes, ego->my_pe,
|
Chris@10
|
115 ego->send_block_sizes, ego->send_block_offsets,
|
Chris@10
|
116 ego->recv_block_sizes, ego->recv_block_offsets,
|
Chris@10
|
117 ego->comm, O, I);
|
Chris@10
|
118 }
|
Chris@10
|
119 else if (ego->preserve_input) {
|
Chris@10
|
120 /* transpose chunks globally */
|
Chris@10
|
121 transpose_chunks(ego->sched, ego->n_pes, ego->my_pe,
|
Chris@10
|
122 ego->send_block_sizes, ego->send_block_offsets,
|
Chris@10
|
123 ego->recv_block_sizes, ego->recv_block_offsets,
|
Chris@10
|
124 ego->comm, I, O);
|
Chris@10
|
125
|
Chris@10
|
126 I = O;
|
Chris@10
|
127 }
|
Chris@10
|
128 else {
|
Chris@10
|
129 /* transpose chunks globally */
|
Chris@10
|
130 transpose_chunks(ego->sched, ego->n_pes, ego->my_pe,
|
Chris@10
|
131 ego->send_block_sizes, ego->send_block_offsets,
|
Chris@10
|
132 ego->recv_block_sizes, ego->recv_block_offsets,
|
Chris@10
|
133 ego->comm, I, I);
|
Chris@10
|
134 }
|
Chris@10
|
135
|
Chris@10
|
136 /* transpose locally, again, to get ordinary row-major;
|
Chris@10
|
137 this may take two transposes if the block sizes are unequal
|
Chris@10
|
138 (3 subplans, two of which operate on disjoint data) */
|
Chris@10
|
139 cld2 = (plan_rdft *) ego->cld2;
|
Chris@10
|
140 cld2->apply(ego->cld2, I, O);
|
Chris@10
|
141 cld2rest = (plan_rdft *) ego->cld2rest;
|
Chris@10
|
142 if (cld2rest) {
|
Chris@10
|
143 cld2rest->apply(ego->cld2rest,
|
Chris@10
|
144 I + ego->rest_Ioff, O + ego->rest_Ooff);
|
Chris@10
|
145 cld3 = (plan_rdft *) ego->cld3;
|
Chris@10
|
146 if (cld3)
|
Chris@10
|
147 cld3->apply(ego->cld3, O, O);
|
Chris@10
|
148 /* else TRANSPOSED_OUT is true and user wants O transposed */
|
Chris@10
|
149 }
|
Chris@10
|
150 }
|
Chris@10
|
151
|
Chris@10
|
152 static int applicable(const S *ego, const problem *p_,
|
Chris@10
|
153 const planner *plnr)
|
Chris@10
|
154 {
|
Chris@10
|
155 const problem_mpi_transpose *p = (const problem_mpi_transpose *) p_;
|
Chris@10
|
156 /* Note: this is *not* UGLY for out-of-place, destroy-input plans;
|
Chris@10
|
157 the planner often prefers transpose-pairwise to transpose-alltoall,
|
Chris@10
|
158 at least with LAM MPI on my machine. */
|
Chris@10
|
159 return (1
|
Chris@10
|
160 && (!ego->preserve_input || (!NO_DESTROY_INPUTP(plnr)
|
Chris@10
|
161 && p->I != p->O))
|
Chris@10
|
162 && ONLY_TRANSPOSEDP(p->flags));
|
Chris@10
|
163 }
|
Chris@10
|
164
|
Chris@10
|
165 static void awake(plan *ego_, enum wakefulness wakefulness)
|
Chris@10
|
166 {
|
Chris@10
|
167 P *ego = (P *) ego_;
|
Chris@10
|
168 X(plan_awake)(ego->cld1, wakefulness);
|
Chris@10
|
169 X(plan_awake)(ego->cld2, wakefulness);
|
Chris@10
|
170 X(plan_awake)(ego->cld2rest, wakefulness);
|
Chris@10
|
171 X(plan_awake)(ego->cld3, wakefulness);
|
Chris@10
|
172 }
|
Chris@10
|
173
|
Chris@10
|
174 static void destroy(plan *ego_)
|
Chris@10
|
175 {
|
Chris@10
|
176 P *ego = (P *) ego_;
|
Chris@10
|
177 X(ifree0)(ego->sched);
|
Chris@10
|
178 X(ifree0)(ego->send_block_sizes);
|
Chris@10
|
179 MPI_Comm_free(&ego->comm);
|
Chris@10
|
180 X(plan_destroy_internal)(ego->cld3);
|
Chris@10
|
181 X(plan_destroy_internal)(ego->cld2rest);
|
Chris@10
|
182 X(plan_destroy_internal)(ego->cld2);
|
Chris@10
|
183 X(plan_destroy_internal)(ego->cld1);
|
Chris@10
|
184 }
|
Chris@10
|
185
|
Chris@10
|
186 static void print(const plan *ego_, printer *p)
|
Chris@10
|
187 {
|
Chris@10
|
188 const P *ego = (const P *) ego_;
|
Chris@10
|
189 p->print(p, "(mpi-transpose-pairwise%s%(%p%)%(%p%)%(%p%)%(%p%))",
|
Chris@10
|
190 ego->preserve_input==2 ?"/p":"",
|
Chris@10
|
191 ego->cld1, ego->cld2, ego->cld2rest, ego->cld3);
|
Chris@10
|
192 }
|
Chris@10
|
193
|
Chris@10
|
194 /* Given a process which_pe and a number of processes npes, fills
|
Chris@10
|
195 the array sched[npes] with a sequence of processes to communicate
|
Chris@10
|
196 with for a deadlock-free, optimum-overlap all-to-all communication.
|
Chris@10
|
197 (All processes must call this routine to get their own schedules.)
|
Chris@10
|
198 The schedule can be re-ordered arbitrarily as long as all processes
|
Chris@10
|
199 apply the same permutation to their schedules.
|
Chris@10
|
200
|
Chris@10
|
201 The algorithm here is based upon the one described in:
|
Chris@10
|
202 J. A. M. Schreuder, "Constructing timetables for sport
|
Chris@10
|
203 competitions," Mathematical Programming Study 13, pp. 58-67 (1980).
|
Chris@10
|
204 In a sport competition, you have N teams and want every team to
|
Chris@10
|
205 play every other team in as short a time as possible (maximum overlap
|
Chris@10
|
206 between games). This timetabling problem is therefore identical
|
Chris@10
|
207 to that of an all-to-all communications problem. In our case, there
|
Chris@10
|
208 is one wrinkle: as part of the schedule, the process must do
|
Chris@10
|
209 some data transfer with itself (local data movement), analogous
|
Chris@10
|
210 to a requirement that each team "play itself" in addition to other
|
Chris@10
|
211 teams. With this wrinkle, it turns out that an optimal timetable
|
Chris@10
|
212 (N parallel games) can be constructed for any N, not just for even
|
Chris@10
|
213 N as in the original problem described by Schreuder.
|
Chris@10
|
214 */
|
Chris@10
|
215 static void fill1_comm_sched(int *sched, int which_pe, int npes)
|
Chris@10
|
216 {
|
Chris@10
|
217 int pe, i, n, s = 0;
|
Chris@10
|
218 A(which_pe >= 0 && which_pe < npes);
|
Chris@10
|
219 if (npes % 2 == 0) {
|
Chris@10
|
220 n = npes;
|
Chris@10
|
221 sched[s++] = which_pe;
|
Chris@10
|
222 }
|
Chris@10
|
223 else
|
Chris@10
|
224 n = npes + 1;
|
Chris@10
|
225 for (pe = 0; pe < n - 1; ++pe) {
|
Chris@10
|
226 if (npes % 2 == 0) {
|
Chris@10
|
227 if (pe == which_pe) sched[s++] = npes - 1;
|
Chris@10
|
228 else if (npes - 1 == which_pe) sched[s++] = pe;
|
Chris@10
|
229 }
|
Chris@10
|
230 else if (pe == which_pe) sched[s++] = pe;
|
Chris@10
|
231
|
Chris@10
|
232 if (pe != which_pe && which_pe < n - 1) {
|
Chris@10
|
233 i = (pe - which_pe + (n - 1)) % (n - 1);
|
Chris@10
|
234 if (i < n/2)
|
Chris@10
|
235 sched[s++] = (pe + i) % (n - 1);
|
Chris@10
|
236
|
Chris@10
|
237 i = (which_pe - pe + (n - 1)) % (n - 1);
|
Chris@10
|
238 if (i < n/2)
|
Chris@10
|
239 sched[s++] = (pe - i + (n - 1)) % (n - 1);
|
Chris@10
|
240 }
|
Chris@10
|
241 }
|
Chris@10
|
242 A(s == npes);
|
Chris@10
|
243 }
|
Chris@10
|
244
|
Chris@10
|
245 /* Sort the communication schedule sched for npes so that the schedule
|
Chris@10
|
246 on process sortpe is ascending or descending (!ascending). This is
|
Chris@10
|
247 necessary to allow in-place transposes when the problem does not
|
Chris@10
|
248 divide equally among the processes. In this case there is one
|
Chris@10
|
249 process where the incoming blocks are bigger/smaller than the
|
Chris@10
|
250 outgoing blocks and thus have to be received in
|
Chris@10
|
251 descending/ascending order, respectively, to avoid overwriting data
|
Chris@10
|
252 before it is sent. */
|
Chris@10
|
253 static void sort1_comm_sched(int *sched, int npes, int sortpe, int ascending)
|
Chris@10
|
254 {
|
Chris@10
|
255 int *sortsched, i;
|
Chris@10
|
256 sortsched = (int *) MALLOC(npes * sizeof(int) * 2, OTHER);
|
Chris@10
|
257 fill1_comm_sched(sortsched, sortpe, npes);
|
Chris@10
|
258 if (ascending)
|
Chris@10
|
259 for (i = 0; i < npes; ++i)
|
Chris@10
|
260 sortsched[npes + sortsched[i]] = sched[i];
|
Chris@10
|
261 else
|
Chris@10
|
262 for (i = 0; i < npes; ++i)
|
Chris@10
|
263 sortsched[2*npes - 1 - sortsched[i]] = sched[i];
|
Chris@10
|
264 for (i = 0; i < npes; ++i)
|
Chris@10
|
265 sched[i] = sortsched[npes + i];
|
Chris@10
|
266 X(ifree)(sortsched);
|
Chris@10
|
267 }
|
Chris@10
|
268
|
Chris@10
|
269 /* make the plans to do the post-MPI transpositions (shared with
|
Chris@10
|
270 transpose-alltoall) */
|
Chris@10
|
271 int XM(mkplans_posttranspose)(const problem_mpi_transpose *p, planner *plnr,
|
Chris@10
|
272 R *I, R *O, int my_pe,
|
Chris@10
|
273 plan **cld2, plan **cld2rest, plan **cld3,
|
Chris@10
|
274 INT *rest_Ioff, INT *rest_Ooff)
|
Chris@10
|
275 {
|
Chris@10
|
276 INT vn = p->vn;
|
Chris@10
|
277 INT b = p->block;
|
Chris@10
|
278 INT bt = XM(block)(p->ny, p->tblock, my_pe);
|
Chris@10
|
279 INT nxb = p->nx / b; /* number of equal-sized blocks */
|
Chris@10
|
280 INT nxr = p->nx - nxb * b; /* leftover rows after equal blocks */
|
Chris@10
|
281
|
Chris@10
|
282 *cld2 = *cld2rest = *cld3 = NULL;
|
Chris@10
|
283 *rest_Ioff = *rest_Ooff = 0;
|
Chris@10
|
284
|
Chris@10
|
285 if (!(p->flags & TRANSPOSED_OUT) && (nxr == 0 || I != O)) {
|
Chris@10
|
286 INT nx = p->nx * vn;
|
Chris@10
|
287 b *= vn;
|
Chris@10
|
288 *cld2 = X(mkplan_f_d)(plnr,
|
Chris@10
|
289 X(mkproblem_rdft_0_d)(X(mktensor_3d)
|
Chris@10
|
290 (nxb, bt * b, b,
|
Chris@10
|
291 bt, b, nx,
|
Chris@10
|
292 b, 1, 1),
|
Chris@10
|
293 I, O),
|
Chris@10
|
294 0, 0, NO_SLOW);
|
Chris@10
|
295 if (!*cld2) goto nada;
|
Chris@10
|
296
|
Chris@10
|
297 if (nxr > 0) {
|
Chris@10
|
298 *rest_Ioff = nxb * bt * b;
|
Chris@10
|
299 *rest_Ooff = nxb * b;
|
Chris@10
|
300 b = nxr * vn;
|
Chris@10
|
301 *cld2rest = X(mkplan_f_d)(plnr,
|
Chris@10
|
302 X(mkproblem_rdft_0_d)(X(mktensor_2d)
|
Chris@10
|
303 (bt, b, nx,
|
Chris@10
|
304 b, 1, 1),
|
Chris@10
|
305 I + *rest_Ioff,
|
Chris@10
|
306 O + *rest_Ooff),
|
Chris@10
|
307 0, 0, NO_SLOW);
|
Chris@10
|
308 if (!*cld2rest) goto nada;
|
Chris@10
|
309 }
|
Chris@10
|
310 }
|
Chris@10
|
311 else {
|
Chris@10
|
312 *cld2 = X(mkplan_f_d)(plnr,
|
Chris@10
|
313 X(mkproblem_rdft_0_d)(
|
Chris@10
|
314 X(mktensor_4d)
|
Chris@10
|
315 (nxb, bt * b * vn, bt * b * vn,
|
Chris@10
|
316 bt, b * vn, vn,
|
Chris@10
|
317 b, vn, bt * vn,
|
Chris@10
|
318 vn, 1, 1),
|
Chris@10
|
319 I, O),
|
Chris@10
|
320 0, 0, NO_SLOW);
|
Chris@10
|
321 if (!*cld2) goto nada;
|
Chris@10
|
322
|
Chris@10
|
323 *rest_Ioff = *rest_Ooff = nxb * bt * b * vn;
|
Chris@10
|
324 *cld2rest = X(mkplan_f_d)(plnr,
|
Chris@10
|
325 X(mkproblem_rdft_0_d)(
|
Chris@10
|
326 X(mktensor_3d)
|
Chris@10
|
327 (bt, nxr * vn, vn,
|
Chris@10
|
328 nxr, vn, bt * vn,
|
Chris@10
|
329 vn, 1, 1),
|
Chris@10
|
330 I + *rest_Ioff, O + *rest_Ooff),
|
Chris@10
|
331 0, 0, NO_SLOW);
|
Chris@10
|
332 if (!*cld2rest) goto nada;
|
Chris@10
|
333
|
Chris@10
|
334 if (!(p->flags & TRANSPOSED_OUT)) {
|
Chris@10
|
335 *cld3 = X(mkplan_f_d)(plnr,
|
Chris@10
|
336 X(mkproblem_rdft_0_d)(
|
Chris@10
|
337 X(mktensor_3d)
|
Chris@10
|
338 (p->nx, bt * vn, vn,
|
Chris@10
|
339 bt, vn, p->nx * vn,
|
Chris@10
|
340 vn, 1, 1),
|
Chris@10
|
341 O, O),
|
Chris@10
|
342 0, 0, NO_SLOW);
|
Chris@10
|
343 if (!*cld3) goto nada;
|
Chris@10
|
344 }
|
Chris@10
|
345 }
|
Chris@10
|
346
|
Chris@10
|
347 return 1;
|
Chris@10
|
348
|
Chris@10
|
349 nada:
|
Chris@10
|
350 X(plan_destroy_internal)(*cld3);
|
Chris@10
|
351 X(plan_destroy_internal)(*cld2rest);
|
Chris@10
|
352 X(plan_destroy_internal)(*cld2);
|
Chris@10
|
353 return 0;
|
Chris@10
|
354 }
|
Chris@10
|
355
|
Chris@10
|
356 static plan *mkplan(const solver *ego_, const problem *p_, planner *plnr)
|
Chris@10
|
357 {
|
Chris@10
|
358 const S *ego = (const S *) ego_;
|
Chris@10
|
359 const problem_mpi_transpose *p;
|
Chris@10
|
360 P *pln;
|
Chris@10
|
361 plan *cld1 = 0, *cld2 = 0, *cld2rest = 0, *cld3 = 0;
|
Chris@10
|
362 INT b, bt, vn, rest_Ioff, rest_Ooff;
|
Chris@10
|
363 INT *sbs, *sbo, *rbs, *rbo;
|
Chris@10
|
364 int pe, my_pe, n_pes, sort_pe = -1, ascending = 1;
|
Chris@10
|
365 R *I, *O;
|
Chris@10
|
366 static const plan_adt padt = {
|
Chris@10
|
367 XM(transpose_solve), awake, print, destroy
|
Chris@10
|
368 };
|
Chris@10
|
369
|
Chris@10
|
370 UNUSED(ego);
|
Chris@10
|
371
|
Chris@10
|
372 if (!applicable(ego, p_, plnr))
|
Chris@10
|
373 return (plan *) 0;
|
Chris@10
|
374
|
Chris@10
|
375 p = (const problem_mpi_transpose *) p_;
|
Chris@10
|
376 vn = p->vn;
|
Chris@10
|
377 I = p->I; O = p->O;
|
Chris@10
|
378
|
Chris@10
|
379 MPI_Comm_rank(p->comm, &my_pe);
|
Chris@10
|
380 MPI_Comm_size(p->comm, &n_pes);
|
Chris@10
|
381
|
Chris@10
|
382 b = XM(block)(p->nx, p->block, my_pe);
|
Chris@10
|
383
|
Chris@10
|
384 if (!(p->flags & TRANSPOSED_IN)) { /* b x ny x vn -> ny x b x vn */
|
Chris@10
|
385 cld1 = X(mkplan_f_d)(plnr,
|
Chris@10
|
386 X(mkproblem_rdft_0_d)(X(mktensor_3d)
|
Chris@10
|
387 (b, p->ny * vn, vn,
|
Chris@10
|
388 p->ny, vn, b * vn,
|
Chris@10
|
389 vn, 1, 1),
|
Chris@10
|
390 I, O),
|
Chris@10
|
391 0, 0, NO_SLOW);
|
Chris@10
|
392 if (XM(any_true)(!cld1, p->comm)) goto nada;
|
Chris@10
|
393 }
|
Chris@10
|
394 if (ego->preserve_input || NO_DESTROY_INPUTP(plnr)) I = O;
|
Chris@10
|
395
|
Chris@10
|
396 if (XM(any_true)(!XM(mkplans_posttranspose)(p, plnr, I, O, my_pe,
|
Chris@10
|
397 &cld2, &cld2rest, &cld3,
|
Chris@10
|
398 &rest_Ioff, &rest_Ooff),
|
Chris@10
|
399 p->comm)) goto nada;
|
Chris@10
|
400
|
Chris@10
|
401 pln = MKPLAN_MPI_TRANSPOSE(P, &padt, apply);
|
Chris@10
|
402
|
Chris@10
|
403 pln->cld1 = cld1;
|
Chris@10
|
404 pln->cld2 = cld2;
|
Chris@10
|
405 pln->cld2rest = cld2rest;
|
Chris@10
|
406 pln->rest_Ioff = rest_Ioff;
|
Chris@10
|
407 pln->rest_Ooff = rest_Ooff;
|
Chris@10
|
408 pln->cld3 = cld3;
|
Chris@10
|
409 pln->preserve_input = ego->preserve_input ? 2 : NO_DESTROY_INPUTP(plnr);
|
Chris@10
|
410
|
Chris@10
|
411 MPI_Comm_dup(p->comm, &pln->comm);
|
Chris@10
|
412
|
Chris@10
|
413 n_pes = (int) X(imax)(XM(num_blocks)(p->nx, p->block),
|
Chris@10
|
414 XM(num_blocks)(p->ny, p->tblock));
|
Chris@10
|
415
|
Chris@10
|
416 /* Compute sizes/offsets of blocks to exchange between processors */
|
Chris@10
|
417 sbs = (INT *) MALLOC(4 * n_pes * sizeof(INT), PLANS);
|
Chris@10
|
418 sbo = sbs + n_pes;
|
Chris@10
|
419 rbs = sbo + n_pes;
|
Chris@10
|
420 rbo = rbs + n_pes;
|
Chris@10
|
421 b = XM(block)(p->nx, p->block, my_pe);
|
Chris@10
|
422 bt = XM(block)(p->ny, p->tblock, my_pe);
|
Chris@10
|
423 for (pe = 0; pe < n_pes; ++pe) {
|
Chris@10
|
424 INT db, dbt; /* destination block sizes */
|
Chris@10
|
425 db = XM(block)(p->nx, p->block, pe);
|
Chris@10
|
426 dbt = XM(block)(p->ny, p->tblock, pe);
|
Chris@10
|
427
|
Chris@10
|
428 sbs[pe] = b * dbt * vn;
|
Chris@10
|
429 sbo[pe] = pe * (b * p->tblock) * vn;
|
Chris@10
|
430 rbs[pe] = db * bt * vn;
|
Chris@10
|
431 rbo[pe] = pe * (p->block * bt) * vn;
|
Chris@10
|
432
|
Chris@10
|
433 if (db * dbt > 0 && db * p->tblock != p->block * dbt) {
|
Chris@10
|
434 A(sort_pe == -1); /* only one process should need sorting */
|
Chris@10
|
435 sort_pe = pe;
|
Chris@10
|
436 ascending = db * p->tblock > p->block * dbt;
|
Chris@10
|
437 }
|
Chris@10
|
438 }
|
Chris@10
|
439 pln->n_pes = n_pes;
|
Chris@10
|
440 pln->my_pe = my_pe;
|
Chris@10
|
441 pln->send_block_sizes = sbs;
|
Chris@10
|
442 pln->send_block_offsets = sbo;
|
Chris@10
|
443 pln->recv_block_sizes = rbs;
|
Chris@10
|
444 pln->recv_block_offsets = rbo;
|
Chris@10
|
445
|
Chris@10
|
446 if (my_pe >= n_pes) {
|
Chris@10
|
447 pln->sched = 0; /* this process is not doing anything */
|
Chris@10
|
448 }
|
Chris@10
|
449 else {
|
Chris@10
|
450 pln->sched = (int *) MALLOC(n_pes * sizeof(int), PLANS);
|
Chris@10
|
451 fill1_comm_sched(pln->sched, my_pe, n_pes);
|
Chris@10
|
452 if (sort_pe >= 0)
|
Chris@10
|
453 sort1_comm_sched(pln->sched, n_pes, sort_pe, ascending);
|
Chris@10
|
454 }
|
Chris@10
|
455
|
Chris@10
|
456 X(ops_zero)(&pln->super.super.ops);
|
Chris@10
|
457 if (cld1) X(ops_add2)(&cld1->ops, &pln->super.super.ops);
|
Chris@10
|
458 if (cld2) X(ops_add2)(&cld2->ops, &pln->super.super.ops);
|
Chris@10
|
459 if (cld2rest) X(ops_add2)(&cld2rest->ops, &pln->super.super.ops);
|
Chris@10
|
460 if (cld3) X(ops_add2)(&cld3->ops, &pln->super.super.ops);
|
Chris@10
|
461 /* FIXME: should MPI operations be counted in "other" somehow? */
|
Chris@10
|
462
|
Chris@10
|
463 return &(pln->super.super);
|
Chris@10
|
464
|
Chris@10
|
465 nada:
|
Chris@10
|
466 X(plan_destroy_internal)(cld3);
|
Chris@10
|
467 X(plan_destroy_internal)(cld2rest);
|
Chris@10
|
468 X(plan_destroy_internal)(cld2);
|
Chris@10
|
469 X(plan_destroy_internal)(cld1);
|
Chris@10
|
470 return (plan *) 0;
|
Chris@10
|
471 }
|
Chris@10
|
472
|
Chris@10
|
473 static solver *mksolver(int preserve_input)
|
Chris@10
|
474 {
|
Chris@10
|
475 static const solver_adt sadt = { PROBLEM_MPI_TRANSPOSE, mkplan, 0 };
|
Chris@10
|
476 S *slv = MKSOLVER(S, &sadt);
|
Chris@10
|
477 slv->preserve_input = preserve_input;
|
Chris@10
|
478 return &(slv->super);
|
Chris@10
|
479 }
|
Chris@10
|
480
|
Chris@10
|
481 void XM(transpose_pairwise_register)(planner *p)
|
Chris@10
|
482 {
|
Chris@10
|
483 int preserve_input;
|
Chris@10
|
484 for (preserve_input = 0; preserve_input <= 1; ++preserve_input)
|
Chris@10
|
485 REGISTER_SOLVER(p, mksolver(preserve_input));
|
Chris@10
|
486 }
|