LBIBCell
 All Classes Functions Variables Friends Pages
MPIProxy.hpp
1 /* Copyright (c) 2013 David Sichau <mail"at"sichau"dot"eu>
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
18  * THE SOFTWARE.
19  *
20  */
21 
22 #ifndef UTILIB_MPIPROXY_HPP_
23 #define UTILIB_MPIPROXY_HPP_
24 #include <UtilLib/config.hpp>
25 #include <UtilLib/include/Log.hpp>
26 #include <UtilLib/include/Exception.hpp>
27 #include <UtilLib/include/Singleton.hpp>
28 #ifdef ENABLE_MPI
29 #include <boost/mpi/collectives.hpp>
30 #include <boost/mpi/communicator.hpp>
31 #include <boost/mpi/nonblocking.hpp>
32 #include <boost/mpi/request.hpp>
33 namespace mpi = boost::mpi;
34 #endif
35 #include <vector>
36 
37 
38 namespace UtilLib {
47 class MPIProxy_ {
48  public:
52  virtual ~MPIProxy_();
53 
58  int getRank() const;
59 
64  int getSize() const;
65 
69  void barrier();
70 
74  void waitAll();
75 
81  template <typename T>
82  void broadcast(
83  T& value,
84  int root);
85 
92  template <typename T>
93  void irecv(
94  int source,
95  int tag,
96  T& value) const;
97 
104  template <typename T>
105  void isend(
106  int dest,
107  int tag,
108  const T& value) const;
109 
110  private:
114  friend class Singleton<MPIProxy_>;
118  MPIProxy_();
119 
120 #ifdef ENABLE_MPI
121 
124  static std::vector<boost::mpi::request> _mpiStatus;
125 #endif
126 
130  static int _rank;
131 
135  static int _size;
136 };
137 
138 template <typename T>
140  T& value,
141  int root) {
142 #ifdef ENABLE_MPI
143  mpi::communicator world;
144  boost::mpi::broadcast(world, value, root);
145 #endif
146 }
147 
148 template <typename T>
150  int source,
151  int tag,
152  T& value) const {
153 #ifdef ENABLE_MPI
154  mpi::communicator world;
155  _mpiStatus.push_back(world.irecv(source, tag, value));
156  LOG(utilities::logDEBUG4) << "recv source: " << source << "; tag: " <<
157  tag << "; value: " << value;
158 #else
159  UtilLib::Exception("MPI Code called from serial code in irecv");
160 #endif
161 }
162 
163 template <typename T>
165  int dest,
166  int tag,
167  const T& value) const {
168 #ifdef ENABLE_MPI
169  mpi::communicator world;
170  _mpiStatus.push_back(world.isend(dest, tag, value));
171  LOG(utilities::logDEBUG4) << "send destination: " << dest << "; tag: " <<
172  tag << "; value: " << value;
173 #else
174  UtilLib::Exception("MPI Code called from serial code in isend");
175 #endif
176 }
177 
182 
188 inline MPIProxy_& MPIProxy() {
190 }
191 } /* end namespace */
192 #endif /* UTILIB_MPIPROXY_HPP_ */
A class to handle all MPI related code. It also provides works if MPI is disabled.
Definition: MPIProxy.hpp:47
virtual ~MPIProxy_()
Definition: MPIProxy.cpp:38
void broadcast(T &value, int root)
Definition: MPIProxy.hpp:139
int getRank() const
Definition: MPIProxy.cpp:40
static T & instance()
Definition: Singleton.hpp:90
void irecv(int source, int tag, T &value) const
Definition: MPIProxy.hpp:149
void isend(int dest, int tag, const T &value) const
Definition: MPIProxy.hpp:164
int getSize() const
Definition: MPIProxy.cpp:44