cannam@127
|
1 /*
|
cannam@127
|
2 * Copyright (c) 2003, 2007-14 Matteo Frigo
|
cannam@127
|
3 * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
|
cannam@127
|
4 *
|
cannam@127
|
5 * This program is free software; you can redistribute it and/or modify
|
cannam@127
|
6 * it under the terms of the GNU General Public License as published by
|
cannam@127
|
7 * the Free Software Foundation; either version 2 of the License, or
|
cannam@127
|
8 * (at your option) any later version.
|
cannam@127
|
9 *
|
cannam@127
|
10 * This program is distributed in the hope that it will be useful,
|
cannam@127
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
cannam@127
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
cannam@127
|
13 * GNU General Public License for more details.
|
cannam@127
|
14 *
|
cannam@127
|
15 * You should have received a copy of the GNU General Public License
|
cannam@127
|
16 * along with this program; if not, write to the Free Software
|
cannam@127
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
cannam@127
|
18 *
|
cannam@127
|
19 */
|
cannam@127
|
20
|
cannam@127
|
21 #include "ifftw-mpi.h"
|
cannam@127
|
22
|
cannam@127
|
23 INT XM(num_blocks)(INT n, INT block)
|
cannam@127
|
24 {
|
cannam@127
|
25 return (n + block - 1) / block;
|
cannam@127
|
26 }
|
cannam@127
|
27
|
cannam@127
|
28 int XM(num_blocks_ok)(INT n, INT block, MPI_Comm comm)
|
cannam@127
|
29 {
|
cannam@127
|
30 int n_pes;
|
cannam@127
|
31 MPI_Comm_size(comm, &n_pes);
|
cannam@127
|
32 return n_pes >= XM(num_blocks)(n, block);
|
cannam@127
|
33 }
|
cannam@127
|
34
|
cannam@127
|
35 /* Pick a default block size for dividing a problem of size n among
|
cannam@127
|
36 n_pes processes. Divide as equally as possible, while minimizing
|
cannam@127
|
37 the maximum block size among the processes as well as the number of
|
cannam@127
|
38 processes with nonzero blocks. */
|
cannam@127
|
39 INT XM(default_block)(INT n, int n_pes)
|
cannam@127
|
40 {
|
cannam@127
|
41 return ((n + n_pes - 1) / n_pes);
|
cannam@127
|
42 }
|
cannam@127
|
43
|
cannam@127
|
44 /* For a given block size and dimension n, compute the block size
|
cannam@127
|
45 on the given process. */
|
cannam@127
|
46 INT XM(block)(INT n, INT block, int which_block)
|
cannam@127
|
47 {
|
cannam@127
|
48 INT d = n - which_block * block;
|
cannam@127
|
49 return d <= 0 ? 0 : (d > block ? block : d);
|
cannam@127
|
50 }
|
cannam@127
|
51
|
cannam@127
|
52 static INT num_blocks_kind(const ddim *dim, block_kind k)
|
cannam@127
|
53 {
|
cannam@127
|
54 return XM(num_blocks)(dim->n, dim->b[k]);
|
cannam@127
|
55 }
|
cannam@127
|
56
|
cannam@127
|
57 INT XM(num_blocks_total)(const dtensor *sz, block_kind k)
|
cannam@127
|
58 {
|
cannam@127
|
59 if (FINITE_RNK(sz->rnk)) {
|
cannam@127
|
60 int i;
|
cannam@127
|
61 INT ntot = 1;
|
cannam@127
|
62 for (i = 0; i < sz->rnk; ++i)
|
cannam@127
|
63 ntot *= num_blocks_kind(sz->dims + i, k);
|
cannam@127
|
64 return ntot;
|
cannam@127
|
65 }
|
cannam@127
|
66 else
|
cannam@127
|
67 return 0;
|
cannam@127
|
68 }
|
cannam@127
|
69
|
cannam@127
|
70 int XM(idle_process)(const dtensor *sz, block_kind k, int which_pe)
|
cannam@127
|
71 {
|
cannam@127
|
72 return (which_pe >= XM(num_blocks_total)(sz, k));
|
cannam@127
|
73 }
|
cannam@127
|
74
|
cannam@127
|
75 /* Given a non-idle process which_pe, computes the coordinate
|
cannam@127
|
76 vector coords[rnk] giving the coordinates of a block in the
|
cannam@127
|
77 matrix of blocks. k specifies whether we are talking about
|
cannam@127
|
78 the input or output data distribution. */
|
cannam@127
|
79 void XM(block_coords)(const dtensor *sz, block_kind k, int which_pe,
|
cannam@127
|
80 INT *coords)
|
cannam@127
|
81 {
|
cannam@127
|
82 int i;
|
cannam@127
|
83 A(!XM(idle_process)(sz, k, which_pe) && FINITE_RNK(sz->rnk));
|
cannam@127
|
84 for (i = sz->rnk - 1; i >= 0; --i) {
|
cannam@127
|
85 INT nb = num_blocks_kind(sz->dims + i, k);
|
cannam@127
|
86 coords[i] = which_pe % nb;
|
cannam@127
|
87 which_pe /= nb;
|
cannam@127
|
88 }
|
cannam@127
|
89 }
|
cannam@127
|
90
|
cannam@127
|
91 INT XM(total_block)(const dtensor *sz, block_kind k, int which_pe)
|
cannam@127
|
92 {
|
cannam@127
|
93 if (XM(idle_process)(sz, k, which_pe))
|
cannam@127
|
94 return 0;
|
cannam@127
|
95 else {
|
cannam@127
|
96 int i;
|
cannam@127
|
97 INT N = 1, *coords;
|
cannam@127
|
98 STACK_MALLOC(INT*, coords, sizeof(INT) * sz->rnk);
|
cannam@127
|
99 XM(block_coords)(sz, k, which_pe, coords);
|
cannam@127
|
100 for (i = 0; i < sz->rnk; ++i)
|
cannam@127
|
101 N *= XM(block)(sz->dims[i].n, sz->dims[i].b[k], coords[i]);
|
cannam@127
|
102 STACK_FREE(coords);
|
cannam@127
|
103 return N;
|
cannam@127
|
104 }
|
cannam@127
|
105 }
|
cannam@127
|
106
|
cannam@127
|
107 /* returns whether sz is local for dims >= dim */
|
cannam@127
|
108 int XM(is_local_after)(int dim, const dtensor *sz, block_kind k)
|
cannam@127
|
109 {
|
cannam@127
|
110 if (FINITE_RNK(sz->rnk))
|
cannam@127
|
111 for (; dim < sz->rnk; ++dim)
|
cannam@127
|
112 if (XM(num_blocks)(sz->dims[dim].n, sz->dims[dim].b[k]) > 1)
|
cannam@127
|
113 return 0;
|
cannam@127
|
114 return 1;
|
cannam@127
|
115 }
|
cannam@127
|
116
|
cannam@127
|
117 int XM(is_local)(const dtensor *sz, block_kind k)
|
cannam@127
|
118 {
|
cannam@127
|
119 return XM(is_local_after)(0, sz, k);
|
cannam@127
|
120 }
|
cannam@127
|
121
|
cannam@127
|
122 /* Return whether sz is distributed for k according to a simple
|
cannam@127
|
123 1d block distribution in the first or second dimensions */
|
cannam@127
|
124 int XM(is_block1d)(const dtensor *sz, block_kind k)
|
cannam@127
|
125 {
|
cannam@127
|
126 int i;
|
cannam@127
|
127 if (!FINITE_RNK(sz->rnk)) return 0;
|
cannam@127
|
128 for (i = 0; i < sz->rnk && num_blocks_kind(sz->dims + i, k) == 1; ++i) ;
|
cannam@127
|
129 return(i < sz->rnk && i < 2 && XM(is_local_after)(i + 1, sz, k));
|
cannam@127
|
130
|
cannam@127
|
131 }
|