ALPSCore reference
mpi_optional.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_OPTIONAL_HPP_INCLUDED_765ecb98a17c4ace9754b3d3b1c1f567
13 #define ALPS_UTILITIES_MPI_OPTIONAL_HPP_INCLUDED_765ecb98a17c4ace9754b3d3b1c1f567
14 
15 #include <alps/utilities/mpi.hpp>
16 #include <boost/optional.hpp>
17 
18 namespace alps {
19  namespace mpi {
20 
22  // FIXME: what is exception safety status?
23  template <typename T>
24  inline void broadcast(const communicator& comm, boost::optional<T>& val, int root)
25  {
26  bool is_valid=!!val;
27  bool is_root=(comm.rank()==root);
28  broadcast(comm, is_valid, root);
29  if (!is_valid) {
30  if (!is_root) {
31  val=boost::none;
32  }
33  return;
34  }
35 
36  if (!is_root) {
37  val=T();
38  }
39  broadcast(comm, *val, root);
40  }
41 
42  } // mpi::
43 } // alps::
44 #endif /* ALPS_UTILITIES_MPI_OPTIONAL_HPP_INCLUDED_765ecb98a17c4ace9754b3d3b1c1f567 */
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