ALPSCore reference
mpi_vector.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1998-2018 ALPS Collaboration. See COPYRIGHT.TXT
3  * All rights reserved. Use is subject to license terms. See LICENSE.TXT
4  * For use in publications, see ACKNOWLEDGE.TXT
5  */
6 
12 #ifndef ALPS_UTILITIES_MPI_VECTOR_HPP_INCLUDED_ba202cf9fb0040e493596702a707ca9b
13 #define ALPS_UTILITIES_MPI_VECTOR_HPP_INCLUDED_ba202cf9fb0040e493596702a707ca9b
14 
15 #include <vector>
16 #include <alps/utilities/mpi.hpp>
17 #include <string>
18 
19 #include <boost/container/vector.hpp>
20 
21 namespace alps {
22  namespace mpi {
23 
25  // FIXME!: make a test
26  // FIXME: what is exception safety status?
27  // FIXME: implement generically for non-contiguous types
28  // FIXME: inline to have it header-only. A tad too complex to be inlined?
29  inline void broadcast(const communicator& comm, std::vector<std::string>& vec, int root)
30  {
31  // FIXME? it might be better to trade traffic to memory and first combine elements in a vector
33  std::size_t root_sz=vec.size();
34  broadcast(comm, root_sz, root);
35  if (comm.rank() != root) {
36  vec.resize(root_sz);
37  }
38  for(std::string& elem: vec) {
39  broadcast(comm, elem, root);
40  }
41  }
42 
43 
45  // FIXME!: make a test
46  // FIXME: what is exception safety status?
47  // FIXME: verify that it is a "primitive" (or, at least, contiguous) type!
48  template <typename T>
49  inline void broadcast(const communicator& comm, std::vector<T>& vec, int root)
50  {
51  std::size_t root_sz=vec.size();
52  broadcast(comm, root_sz, root);
53  if (comm.rank() != root) {
54  vec.resize(root_sz);
55  }
56  broadcast(comm, &vec[0], vec.size(), root);
57  }
58 
60  // FIXME!: make a test
61  // FIXME: what is exception safety status?
62  // FIXME: implement generically for non-contiguous types
63  inline void broadcast(const communicator& comm, std::vector<bool>& vec, int root)
64  {
65  // FIXME? it might be better to trade traffic to memory and first combine elements in a vector
67  typedef std::vector<bool> vector_type;
68  typedef vector_type::iterator iter_type;
69 
70  std::size_t root_sz=vec.size();
71  broadcast(comm, root_sz, root);
72  if (comm.rank() != root) {
73  vec.resize(root_sz);
74  }
75 
76  for (iter_type it=vec.begin(); it!=vec.end(); ++it) {
77  bool elem=*it;
78  broadcast(comm, elem, root);
79  *it=elem;
80  }
81  }
82 
83 
84  } // mpi::
85 } // alps::
86 
87 
88 #endif /* ALPS_UTILITIES_MPI_VECTOR_HPP_INCLUDED_ba202cf9fb0040e493596702a707ca9b */
Header for object-oriented interface to MPI (similar to boost::mpi)
Encapsulation of an MPI communicator and some communicator-related operations.
Definition: mpi.hpp:111
int rank() const
Returns process rank in this communicator.
Definition: mpi.hpp:156
void broadcast(const communicator &comm, T *vals, std::size_t count, int root)
Broadcasts array vals of a primitive type T, length count on communicator comm with root root ...
Definition: mpi.hpp:270