comparison src/fftw-3.3.3/mpi/transpose-alltoall.c @ 10:37bf6b4a2645

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