diff options
author | Erik Schnetter <schnetter@cct.lsu.edu> | 2010-08-27 16:30:51 -0400 |
---|---|---|
committer | Barry Wardell <barry.wardell@gmail.com> | 2011-12-14 18:25:16 +0000 |
commit | d5194ae8988416f0c4237068697a5031068febc9 (patch) | |
tree | 2663a71e7e816b5c46fc33f4390a440ac88cf3c2 /Carpet | |
parent | 1b2da9ca286f1c15e10c0a6bd578ff3b19907b7b (diff) |
Carpet: Implement new routine gather_string
Diffstat (limited to 'Carpet')
-rw-r--r-- | Carpet/CarpetLib/src/mpi_string.cc | 75 | ||||
-rw-r--r-- | Carpet/CarpetLib/src/mpi_string.hh | 4 |
2 files changed, 79 insertions, 0 deletions
diff --git a/Carpet/CarpetLib/src/mpi_string.cc b/Carpet/CarpetLib/src/mpi_string.cc index e0022e3e0..b7e1f9b0e 100644 --- a/Carpet/CarpetLib/src/mpi_string.cc +++ b/Carpet/CarpetLib/src/mpi_string.cc @@ -22,6 +22,81 @@ namespace CarpetLib vector <string> + gather_string (MPI_Comm const comm, + int const root, + string const & data) + { + // Get my rank + int rank; + MPI_Comm_rank (comm, & rank); + + if (rank == root) { + + // Get the total number of processors + int num_procs; + MPI_Comm_size (comm, & num_procs); + + // Gather the lengths of the data strings + int const length = data.length(); + vector <int> lengths (num_procs); + + MPI_Gather (const_cast <int *> (& length), 1, MPI_INT, + & lengths.front(), 1, MPI_INT, + root, comm); + + // Allocate space for all data strings + vector <int> 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); + vector <char> alldata_buffer (total_length); + + // Gather all data strings + MPI_Gatherv (const_cast <char *> (data.c_str()), length, MPI_CHAR, + & alldata_buffer.front(), + const_cast <int *> (& lengths.front()), + const_cast <int *> (& offsets.front()), + MPI_CHAR, + root, comm); + + // Convert data buffer with C strings to C++ strings + vector <string> 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; + + } else { + + // Gather the lengths of the data strings + int const length = data.length(); + + MPI_Gather (const_cast <int *> (& length), 1, MPI_INT, + NULL, 1, MPI_INT, + root, comm); + + // Gather all data strings + MPI_Gatherv (const_cast <char *> (data.c_str()), length, MPI_CHAR, + NULL, NULL, NULL, MPI_CHAR, + root, comm); + + // Convert data buffer with C strings to C++ strings + vector <string> alldata; + + return alldata; + + } + } + + + + vector <string> allgather_string (MPI_Comm const comm, string const & data) { diff --git a/Carpet/CarpetLib/src/mpi_string.hh b/Carpet/CarpetLib/src/mpi_string.hh index 6ed8d447d..f05910139 100644 --- a/Carpet/CarpetLib/src/mpi_string.hh +++ b/Carpet/CarpetLib/src/mpi_string.hh @@ -17,6 +17,10 @@ namespace CarpetLib // String communication vector <string> + gather_string (MPI_Comm comm, int root, + string const & data); + + vector <string> allgather_string (MPI_Comm comm, string const & data); |