aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/AllGatherString.cc
blob: 5efa9611498fe917350c538ca3ec17a5a219250c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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