2
* Copyright (c) 2003, 2007-11 Matteo Frigo
3
* Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include "ifftw-mpi.h"
23
INT XM(num_blocks)(INT n, INT block)
25
return (n + block - 1) / block;
28
int XM(num_blocks_ok)(INT n, INT block, MPI_Comm comm)
31
MPI_Comm_size(comm, &n_pes);
32
return n_pes >= XM(num_blocks)(n, block);
35
/* Pick a default block size for dividing a problem of size n among
36
n_pes processes. Divide as equally as possible, while minimizing
37
the maximum block size among the processes as well as the number of
38
processes with nonzero blocks. */
39
INT XM(default_block)(INT n, int n_pes)
41
return ((n + n_pes - 1) / n_pes);
44
/* For a given block size and dimension n, compute the block size
45
on the given process. */
46
INT XM(block)(INT n, INT block, int which_block)
48
INT n_blocks = XM(num_blocks)(n, block);
49
if (which_block >= n_blocks)
52
return ((which_block == n_blocks - 1)
53
? (n - which_block * block) : block);
56
static INT num_blocks_kind(const ddim *dim, block_kind k)
58
return XM(num_blocks)(dim->n, dim->b[k]);
61
INT XM(num_blocks_total)(const dtensor *sz, block_kind k)
63
if (FINITE_RNK(sz->rnk)) {
66
for (i = 0; i < sz->rnk; ++i)
67
ntot *= num_blocks_kind(sz->dims + i, k);
74
int XM(idle_process)(const dtensor *sz, block_kind k, int which_pe)
76
return (which_pe >= XM(num_blocks_total)(sz, k));
79
/* Given a non-idle process which_pe, computes the coordinate
80
vector coords[rnk] giving the coordinates of a block in the
81
matrix of blocks. k specifies whether we are talking about
82
the input or output data distribution. */
83
void XM(block_coords)(const dtensor *sz, block_kind k, int which_pe,
87
A(!XM(idle_process)(sz, k, which_pe) && FINITE_RNK(sz->rnk));
88
for (i = sz->rnk - 1; i >= 0; --i) {
89
INT nb = num_blocks_kind(sz->dims + i, k);
90
coords[i] = which_pe % nb;
95
INT XM(total_block)(const dtensor *sz, block_kind k, int which_pe)
97
if (XM(idle_process)(sz, k, which_pe))
102
STACK_MALLOC(INT*, coords, sizeof(INT) * sz->rnk);
103
XM(block_coords)(sz, k, which_pe, coords);
104
for (i = 0; i < sz->rnk; ++i)
105
N *= XM(block)(sz->dims[i].n, sz->dims[i].b[k], coords[i]);
111
/* returns whether sz is local for dims >= dim */
112
int XM(is_local_after)(int dim, const dtensor *sz, block_kind k)
114
if (FINITE_RNK(sz->rnk))
115
for (; dim < sz->rnk; ++dim)
116
if (XM(num_blocks)(sz->dims[dim].n, sz->dims[dim].b[k]) > 1)
121
int XM(is_local)(const dtensor *sz, block_kind k)
123
return XM(is_local_after)(0, sz, k);
126
/* Return whether sz is distributed for k according to a simple
127
1d block distribution in the first or second dimensions */
128
int XM(is_block1d)(const dtensor *sz, block_kind k)
131
if (!FINITE_RNK(sz->rnk)) return 0;
132
for (i = 0; i < sz->rnk && num_blocks_kind(sz->dims + i, k) == 1; ++i) ;
133
return(i < sz->rnk && i < 2 && XM(is_local_after)(i + 1, sz, k));