cannam@167: /* cannam@167: * Copyright (c) 2003, 2007-14 Matteo Frigo cannam@167: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology cannam@167: * cannam@167: * This program is free software; you can redistribute it and/or modify cannam@167: * it under the terms of the GNU General Public License as published by cannam@167: * the Free Software Foundation; either version 2 of the License, or cannam@167: * (at your option) any later version. cannam@167: * cannam@167: * This program is distributed in the hope that it will be useful, cannam@167: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@167: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@167: * GNU General Public License for more details. cannam@167: * cannam@167: * You should have received a copy of the GNU General Public License cannam@167: * along with this program; if not, write to the Free Software cannam@167: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@167: * cannam@167: */ cannam@167: cannam@167: #include "ifftw-mpi.h" cannam@167: cannam@167: INT XM(num_blocks)(INT n, INT block) cannam@167: { cannam@167: return (n + block - 1) / block; cannam@167: } cannam@167: cannam@167: int XM(num_blocks_ok)(INT n, INT block, MPI_Comm comm) cannam@167: { cannam@167: int n_pes; cannam@167: MPI_Comm_size(comm, &n_pes); cannam@167: return n_pes >= XM(num_blocks)(n, block); cannam@167: } cannam@167: cannam@167: /* Pick a default block size for dividing a problem of size n among cannam@167: n_pes processes. Divide as equally as possible, while minimizing cannam@167: the maximum block size among the processes as well as the number of cannam@167: processes with nonzero blocks. */ cannam@167: INT XM(default_block)(INT n, int n_pes) cannam@167: { cannam@167: return ((n + n_pes - 1) / n_pes); cannam@167: } cannam@167: cannam@167: /* For a given block size and dimension n, compute the block size cannam@167: on the given process. */ cannam@167: INT XM(block)(INT n, INT block, int which_block) cannam@167: { cannam@167: INT d = n - which_block * block; cannam@167: return d <= 0 ? 0 : (d > block ? block : d); cannam@167: } cannam@167: cannam@167: static INT num_blocks_kind(const ddim *dim, block_kind k) cannam@167: { cannam@167: return XM(num_blocks)(dim->n, dim->b[k]); cannam@167: } cannam@167: cannam@167: INT XM(num_blocks_total)(const dtensor *sz, block_kind k) cannam@167: { cannam@167: if (FINITE_RNK(sz->rnk)) { cannam@167: int i; cannam@167: INT ntot = 1; cannam@167: for (i = 0; i < sz->rnk; ++i) cannam@167: ntot *= num_blocks_kind(sz->dims + i, k); cannam@167: return ntot; cannam@167: } cannam@167: else cannam@167: return 0; cannam@167: } cannam@167: cannam@167: int XM(idle_process)(const dtensor *sz, block_kind k, int which_pe) cannam@167: { cannam@167: return (which_pe >= XM(num_blocks_total)(sz, k)); cannam@167: } cannam@167: cannam@167: /* Given a non-idle process which_pe, computes the coordinate cannam@167: vector coords[rnk] giving the coordinates of a block in the cannam@167: matrix of blocks. k specifies whether we are talking about cannam@167: the input or output data distribution. */ cannam@167: void XM(block_coords)(const dtensor *sz, block_kind k, int which_pe, cannam@167: INT *coords) cannam@167: { cannam@167: int i; cannam@167: A(!XM(idle_process)(sz, k, which_pe) && FINITE_RNK(sz->rnk)); cannam@167: for (i = sz->rnk - 1; i >= 0; --i) { cannam@167: INT nb = num_blocks_kind(sz->dims + i, k); cannam@167: coords[i] = which_pe % nb; cannam@167: which_pe /= nb; cannam@167: } cannam@167: } cannam@167: cannam@167: INT XM(total_block)(const dtensor *sz, block_kind k, int which_pe) cannam@167: { cannam@167: if (XM(idle_process)(sz, k, which_pe)) cannam@167: return 0; cannam@167: else { cannam@167: int i; cannam@167: INT N = 1, *coords; cannam@167: STACK_MALLOC(INT*, coords, sizeof(INT) * sz->rnk); cannam@167: XM(block_coords)(sz, k, which_pe, coords); cannam@167: for (i = 0; i < sz->rnk; ++i) cannam@167: N *= XM(block)(sz->dims[i].n, sz->dims[i].b[k], coords[i]); cannam@167: STACK_FREE(coords); cannam@167: return N; cannam@167: } cannam@167: } cannam@167: cannam@167: /* returns whether sz is local for dims >= dim */ cannam@167: int XM(is_local_after)(int dim, const dtensor *sz, block_kind k) cannam@167: { cannam@167: if (FINITE_RNK(sz->rnk)) cannam@167: for (; dim < sz->rnk; ++dim) cannam@167: if (XM(num_blocks)(sz->dims[dim].n, sz->dims[dim].b[k]) > 1) cannam@167: return 0; cannam@167: return 1; cannam@167: } cannam@167: cannam@167: int XM(is_local)(const dtensor *sz, block_kind k) cannam@167: { cannam@167: return XM(is_local_after)(0, sz, k); cannam@167: } cannam@167: cannam@167: /* Return whether sz is distributed for k according to a simple cannam@167: 1d block distribution in the first or second dimensions */ cannam@167: int XM(is_block1d)(const dtensor *sz, block_kind k) cannam@167: { cannam@167: int i; cannam@167: if (!FINITE_RNK(sz->rnk)) return 0; cannam@167: for (i = 0; i < sz->rnk && num_blocks_kind(sz->dims + i, k) == 1; ++i) ; cannam@167: return(i < sz->rnk && i < 2 && XM(is_local_after)(i + 1, sz, k)); cannam@167: cannam@167: }