#include #include //#include //#include #include #include #include #include "cctk.h" #include "functions.hh" namespace Carpet { using namespace std; vector AllGatherString (MPI_Comm const world, string const & data) { // Get the total number of processors int num_procs; MPI_Comm_size (world, & num_procs); // Gather the lengths of the data strings int const length = data.length(); vector lengths (num_procs); MPI_Allgather (const_cast (& length), 1, MPI_INT, & lengths.front(), 1, MPI_INT, world); // Allocate space for all data strings vector offsets (num_procs + 1); offsets.at(0) = 0; for (int n = 0; n < num_procs; ++ n) { offsets.at(n + 1) = offsets.at(n) + lengths.at(n); } int const total_length = offsets.at(num_procs); // Gather all data strings vector alldata_buffer (total_length); MPI_Allgatherv (const_cast (data.c_str()), length, MPI_CHAR, & alldata_buffer.front(), const_cast (& lengths.front()), const_cast (& offsets.front()), MPI_CHAR, world); // Convert data buffer with C strings to C++ strings vector alldata (num_procs); for (int n = 0; n < num_procs; ++ n) { alldata.at(n) = string (& alldata_buffer.at (offsets.at(n)), lengths.at(n)); } return alldata; } } // namespace Carpet