vSMC
vSMC: Scalable Monte Carlo
mpi_manager.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/mpi/mpi_manager.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_MPI_MPI_MANAGER_HPP
33 #define VSMC_MPI_MPI_MANAGER_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 #include <vsmc/rng/seed.hpp>
37 #include <boost/mpi.hpp>
38 
39 namespace vsmc {
40 
41 namespace internal {
42 
43 template <typename ResultType, typename IntType1, typename IntType2>
44 inline void mpi_init_seed (ResultType &, IntType1 D, IntType2 R)
45 {
46  Seed::instance().modulo(
47  static_cast<Seed::skip_type>(D), static_cast<Seed::skip_type>(R));
48 }
49 
50 template <typename T, std::size_t K, typename IntType1, typename IntType2>
51 inline void mpi_init_seed (vsmc::Array<T, K> &s, IntType1, IntType2 R)
52 {s.back() = static_cast<Seed::skip_type>(R);}
53 
54 } // namespace vsmc::internal
55 
63 {
64  public :
65 
66 #ifdef BOOST_MPI_HAS_NOARG_INITIALIZATION
67  explicit MPIEnvironment (bool abort_on_exception = true) :
68  env_(abort_on_exception) {init_seed();}
69 #endif
70 
71  MPIEnvironment(int &argc, char **&argv, bool abort_on_exception = true) :
72  env_(argc, argv, abort_on_exception) {init_seed();}
73 
74  private :
75 
76  ::boost::mpi::environment env_;
77 
78  void init_seed () const
79  {
80  ::boost::mpi::communicator world;
82  internal::mpi_init_seed(s, world.size(), world.rank());
83  Seed::instance().set(s);
84  world.barrier();
85  }
86 }; // class MPIEnvironment
87 
93 template <typename ID>
94 class MPICommunicator
95 {
96  public :
97 
99  {
100  static MPICommunicator<ID> comm;
101 
102  return comm;
103  }
104 
105  const MPI_Comm &get () const {return comm_;}
106 
107  void set (const MPI_Comm &comm) {comm_ = comm;}
108 
109  private :
110 
111  MPI_Comm comm_;
112 
113  MPICommunicator () : comm_(MPI_COMM_WORLD) {}
114  MPICommunicator (const MPICommunicator<ID> &);
115  MPICommunicator<ID> &operator= (const MPICommunicator<ID> &);
116 }; // class MPICommunicator
117 
118 } // namespace vsmc
119 
120 #endif // VSMC_MPI_MPI_MANAGER_HPP
static SeedGenerator< ID, ResultType > & instance()
Definition: seed.hpp:94
Definition: adapter.hpp:37
static MPICommunicator< ID > & instance()
Definition: mpi_manager.hpp:98
ResultType result_type
Definition: seed.hpp:91
MPIEnvironment(int &argc, char **&argv, bool abort_on_exception=true)
Definition: mpi_manager.hpp:71
reference back()
Definition: array.hpp:123
void set(const MPI_Comm &comm)
ResultType skip_type
Definition: seed.hpp:92
MPI Communicator.
Definition: forward.hpp:70
MPI Environment.
Definition: mpi_manager.hpp:62
void mpi_init_seed(ResultType &, IntType1 D, IntType2 R)
Definition: mpi_manager.hpp:44