annotate src/fftw-3.3.5/mpi/block.c @ 42:2cd0e3b3e1fd

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