Chris@19: /* Chris@19: * Copyright (c) 2003, 2007-14 Matteo Frigo Chris@19: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology Chris@19: * Chris@19: * This program is free software; you can redistribute it and/or modify Chris@19: * it under the terms of the GNU General Public License as published by Chris@19: * the Free Software Foundation; either version 2 of the License, or Chris@19: * (at your option) any later version. Chris@19: * Chris@19: * This program is distributed in the hope that it will be useful, Chris@19: * but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@19: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@19: * GNU General Public License for more details. Chris@19: * Chris@19: * You should have received a copy of the GNU General Public License Chris@19: * along with this program; if not, write to the Free Software Chris@19: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Chris@19: * Chris@19: */ Chris@19: Chris@19: #include "fftw3-mpi.h" Chris@19: #include "ifftw-mpi.h" Chris@19: #include Chris@19: Chris@19: #if SIZEOF_SIZE_T == SIZEOF_UNSIGNED_INT Chris@19: # define FFTW_MPI_SIZE_T MPI_UNSIGNED Chris@19: #elif SIZEOF_SIZE_T == SIZEOF_UNSIGNED_LONG Chris@19: # define FFTW_MPI_SIZE_T MPI_UNSIGNED_LONG Chris@19: #elif SIZEOF_SIZE_T == SIZEOF_UNSIGNED_LONG_LONG Chris@19: # define FFTW_MPI_SIZE_T MPI_UNSIGNED_LONG_LONG Chris@19: #else Chris@19: # error MPI type for size_t is unknown Chris@19: # define FFTW_MPI_SIZE_T MPI_UNSIGNED_LONG Chris@19: #endif Chris@19: Chris@19: /* Import wisdom from all processes to process 0, as prelude to Chris@19: exporting a single wisdom file (this is convenient when we are Chris@19: running on identical processors, to avoid the annoyance of having Chris@19: per-process wisdom files). In order to make the time for this Chris@19: operation logarithmic in the number of processors (rather than Chris@19: linear), we employ a tree reduction algorithm. This means that the Chris@19: wisdom is modified on processes other than root, which shouldn't Chris@19: matter in practice. */ Chris@19: void XM(gather_wisdom)(MPI_Comm comm_) Chris@19: { Chris@19: MPI_Comm comm, comm2; Chris@19: int my_pe, n_pes; Chris@19: char *wis; Chris@19: size_t wislen; Chris@19: MPI_Status status; Chris@19: Chris@19: MPI_Comm_dup(comm_, &comm); Chris@19: MPI_Comm_rank(comm, &my_pe); Chris@19: MPI_Comm_size(comm, &n_pes); Chris@19: Chris@19: if (n_pes > 2) { /* recursively split into even/odd processes */ Chris@19: MPI_Comm_split(comm, my_pe % 2, my_pe, &comm2); Chris@19: XM(gather_wisdom)(comm2); Chris@19: MPI_Comm_free(&comm2); Chris@19: } Chris@19: if (n_pes > 1 && my_pe < 2) { /* import process 1 -> 0 */ Chris@19: if (my_pe == 1) { Chris@19: wis = X(export_wisdom_to_string)(); Chris@19: wislen = strlen(wis) + 1; Chris@19: MPI_Send(&wislen, 1, FFTW_MPI_SIZE_T, 0, 111, comm); Chris@19: MPI_Send(wis, wislen, MPI_CHAR, 0, 222, comm); Chris@19: free(wis); Chris@19: } Chris@19: else /* my_pe == 0 */ { Chris@19: MPI_Recv(&wislen, 1, FFTW_MPI_SIZE_T, 1, 111, comm, &status); Chris@19: wis = (char *) MALLOC(wislen * sizeof(char), OTHER); Chris@19: MPI_Recv(wis, wislen, MPI_CHAR, 1, 222, comm, &status); Chris@19: if (!X(import_wisdom_from_string)(wis)) Chris@19: MPI_Abort(comm, 1); Chris@19: X(ifree)(wis); Chris@19: } Chris@19: } Chris@19: MPI_Comm_free(&comm); Chris@19: } Chris@19: Chris@19: /* broadcast wisdom from process 0 to all other processes; this Chris@19: is useful so that we can import wisdom once and not worry Chris@19: about parallel I/O or process-specific wisdom, although of Chris@19: course it assumes that all the processes have identical Chris@19: performance characteristics (i.e. identical hardware). */ Chris@19: void XM(broadcast_wisdom)(MPI_Comm comm_) Chris@19: { Chris@19: MPI_Comm comm; Chris@19: int my_pe; Chris@19: char *wis; Chris@19: size_t wislen; Chris@19: Chris@19: MPI_Comm_dup(comm_, &comm); Chris@19: MPI_Comm_rank(comm, &my_pe); Chris@19: Chris@19: if (my_pe != 0) { Chris@19: MPI_Bcast(&wislen, 1, FFTW_MPI_SIZE_T, 0, comm); Chris@19: wis = (char *) MALLOC(wislen * sizeof(char), OTHER); Chris@19: MPI_Bcast(wis, wislen, MPI_CHAR, 0, comm); Chris@19: if (!X(import_wisdom_from_string)(wis)) Chris@19: MPI_Abort(comm, 1); Chris@19: X(ifree)(wis); Chris@19: } Chris@19: else /* my_pe == 0 */ { Chris@19: wis = X(export_wisdom_to_string)(); Chris@19: wislen = strlen(wis) + 1; Chris@19: MPI_Bcast(&wislen, 1, FFTW_MPI_SIZE_T, 0, comm); Chris@19: MPI_Bcast(wis, wislen, MPI_CHAR, 0, comm); Chris@19: X(free)(wis); Chris@19: } Chris@19: MPI_Comm_free(&comm); Chris@19: }