diff options
author | Erik Schnetter <schnetter@cct.lsu.edu> | 2007-04-23 18:40:00 +0000 |
---|---|---|
committer | Erik Schnetter <schnetter@cct.lsu.edu> | 2007-04-23 18:40:00 +0000 |
commit | aed232234341500ef097d2176c4047462389c08e (patch) | |
tree | 3342286993721e4356c2e6ee793ede7b4ee2c82e /Carpet | |
parent | 05da9014611ff445033fd2e1d2f3712b2efd3b7d (diff) |
Carpet: Add function AllGatherString
Add function AllGatherString, which gathers strings from multiple
processors. Use this function when determining the models.
darcs-hash:20070423184045-dae7b-0364dcfe7ba6c79ca986e83da380dae0565c1a7b.gz
Diffstat (limited to 'Carpet')
-rw-r--r-- | Carpet/Carpet/src/AllGatherString.cc | 69 | ||||
-rw-r--r-- | Carpet/Carpet/src/MultiModel.cc | 36 | ||||
-rw-r--r-- | Carpet/Carpet/src/functions.hh | 9 | ||||
-rw-r--r-- | Carpet/Carpet/src/make.code.defn | 3 |
4 files changed, 80 insertions, 37 deletions
diff --git a/Carpet/Carpet/src/AllGatherString.cc b/Carpet/Carpet/src/AllGatherString.cc new file mode 100644 index 000000000..5efa96114 --- /dev/null +++ b/Carpet/Carpet/src/AllGatherString.cc @@ -0,0 +1,69 @@ +#include <cassert> +#include <cstring> +//#include <iostream> +//#include <map> +#include <string> +#include <vector> + +#include <mpi.h> + +#include "cctk.h" + +#include "functions.hh" + + + +namespace Carpet +{ + + using namespace std; + + + + vector <string> + 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 <int> lengths (num_procs); + + MPI_Allgather (const_cast <int *> (& length), 1, MPI_INT, + & lengths.front(), 1, MPI_INT, + world); + + // 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); + + // Gather all data strings + vector <char> alldata_buffer (total_length); + + MPI_Allgatherv (const_cast <char *> (data.c_str()), length, MPI_CHAR, + & alldata_buffer.front(), + const_cast <int *> (& lengths.front()), + const_cast <int *> (& offsets.front()), + MPI_CHAR, + world); + + // 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; + } + +} // namespace Carpet diff --git a/Carpet/Carpet/src/MultiModel.cc b/Carpet/Carpet/src/MultiModel.cc index 492e953ea..aa2d7c096 100644 --- a/Carpet/Carpet/src/MultiModel.cc +++ b/Carpet/Carpet/src/MultiModel.cc @@ -71,40 +71,8 @@ namespace Carpet int my_proc; MPI_Comm_rank (world, & my_proc); - // Gather the lengths of the model strings - int const length = model.length(); - vector <int> lengths (num_procs); - - MPI_Allgather (const_cast <int *> (& length), 1, MPI_INT, - & lengths.front(), 1, MPI_INT, - world); - - // Allocate space for all model 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); - - // Gather all model strings - vector <char> models_buffer (total_length); - - MPI_Allgatherv (const_cast <char *> (model.c_str()), length, MPI_CHAR, - & models_buffer.front(), - const_cast <int *> (& lengths.front()), - const_cast <int *> (& offsets.front()), - MPI_CHAR, - world); - - // Convert model name buffer with C strings to C++ strings - models.resize (num_procs); - for (int n = 0; n < num_procs; ++ n) - { - models.at(n) - = string (& models_buffer.at (offsets.at(n)), lengths.at(n)); - } + // Gather all model names + models = AllGatherString (world, model); // Map model strings to small integers int num_models = 0; diff --git a/Carpet/Carpet/src/functions.hh b/Carpet/Carpet/src/functions.hh index 89bf3eff1..0f43ff281 100644 --- a/Carpet/Carpet/src/functions.hh +++ b/Carpet/Carpet/src/functions.hh @@ -45,8 +45,13 @@ namespace Carpet { void Restrict (const cGH* cgh); - - + + + // Strings + vector <string> + AllGatherString (MPI_Comm const world, + string const & data); + // Multi-Model void SplitUniverse (MPI_Comm const world, string const model, MPI_Comm & comm, diff --git a/Carpet/Carpet/src/make.code.defn b/Carpet/Carpet/src/make.code.defn index 46dd195a8..1259cc1c8 100644 --- a/Carpet/Carpet/src/make.code.defn +++ b/Carpet/Carpet/src/make.code.defn @@ -1,7 +1,8 @@ # Main make.code.defn file for thorn Carpet -*-Makefile-*- # Source files in this directory -SRCS = CallFunction.cc \ +SRCS = AllGatherString.cc \ + CallFunction.cc \ CarpetParamCheck.cc \ CarpetStartup.cc \ Checksum.cc \ |