Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
applicationmpi.hpp
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <christophe.prudhomme@feelpp.org>
6  Date: 2005-10-18
7 
8  Copyright (C) 2005,2006,2009 EPFL
9  Copyright (C) 2007 Université Joseph Fourier (Grenoble I)
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
30 #ifndef __Application_H
31 #define __Application_H 1
32 
33 #include <iostream>
34 #include <boost/archive/binary_iarchive.hpp>
35 #include <boost/archive/binary_oarchive.hpp>
36 #include <boost/archive/text_iarchive.hpp>
37 #include <boost/archive/text_oarchive.hpp>
38 #include <boost/serialization/string.hpp> // Needed to send/receive strings!
39 
40 #include <boost/mpi.hpp>
41 #include <feel/feelconfig.h>
42 
43 #if defined(FEELPP_HAS_MPI_H)
44 #include <mpi.h>
45 #endif /* FEELPP_HAS_MPI_H */
46 
47 
48 
49 #include <feel/feelcore/feel.hpp>
51 
52 namespace Feel
53 {
54 namespace mpi = boost::mpi;
55 
64 class Application : public Application
65 {
66  typedef Application super;
67 public:
68 
69 
73 
74 
76 
80 
89 #if defined( FEELPP_HAS_MPI )
90  Application( int argc, char** argv, AboutData const& ad, MPI_Comm Comm = MPI_COMM_WORLD );
91 #else
92  Application( int argc, char** argv, AboutData const& ad );
93 #endif
94 
104 #if defined( FEELPP_HAS_MPI )
105  Application( int argc, char** argv, AboutData const& ad, po::options_description const& od, MPI_Comm Comm = MPI_COMM_WORLD );
106 #else
107  Application( int argc, char** argv, AboutData const& ad, po::options_description const& od );
108 #endif
109 
110 
111  ~Application();
112 
114 
118 
119 
121 
125 
129  bool isMPIInitialized() const
130  {
131  return _S_is_mpi_initialized;
132  }
133 
140  static bool initialized()
141  {
142  return mpi::environment::initialized();
143  }
144 
151  static bool finalized()
152  {
153  return mpi::environment::finalized();
154  }
155 
165  static std::string processorName()
166  {
167 #if defined( FEELPP_HAS_MPI )
168  return mpi::environment::processor_name();
169 #else
170  // fallback
171  return std::string( "localhost" );
172 #endif
173  }
174 
175 
177 
181 
182 
184 
188 
189 
190 
191 
192 #if defined( FEELPP_HAS_MPI )
193  template<class T>
194  static void Send( const T& obj, int proc, int tag, const MPI_Comm& comm = COMM_WORLD )
195  {
196  std::ostringstream oss;
197  boost::archive::binary_oarchive oa( oss );
198  oa << obj;
199 
200  std::string str( oss.str() );
201  int size = str.size();
202  MPI_Ssend( &size, 1, MPI_INT, proc, tag, comm );
203  MPI_Ssend( &str[0], size, MPI_CHAR, proc, tag, comm );
204  }
205 #else
206  template<class T>
207  static void Send( const T& obj, int proc, int tag )
208  {
209  // dummy function, don't have to do anything
210  }
211 #endif // FEELPP_HAS_MPI
212 
213 
214 #if defined( FEELPP_HAS_MPI )
215  template<class T>
216  static void Broadcast( T& obj, int root = 0, const MPI_Comm& comm = COMM_WORLD )
217  {
218  std::ostringstream oss;
219  boost::archive::binary_oarchive oa( oss );
220  oa << obj;
221 
222  std::string str( oss.str() );
223  int size = str.size();
224  MPI_Bcast( &size, 1, MPI_INT, root, comm );
225 
226  if ( Application::processId() != root )
227  str.resize( size );
228 
229  DVLOG(2) << "[Application::Broadcast] str.size = " << str.size() << "\n";
230  DVLOG(2) << "[Application::Broadcast] before str = " << str << "\n";
231 
232  MPI_Bcast( &str[0], size, MPI_CHAR, root, comm );
233  DVLOG(2) << "[Application::Broadcast] after str = " << str << "\n";
234 
235  // deserialize for processId() != root
236  if ( Application::processId() != root )
237  {
238  std::istringstream iss( str );
239  boost::archive::binary_iarchive ia( iss );
240  ia >> obj;
241  }
242  }
243 #else
244  template<class T>
245  static void Broadcast( T& obj )
246  {
247  // dummy function, don't have to do anything
248  }
249 #endif // FEELPP_HAS_MP
250 
251 
252 
253 
254 #if defined( FEELPP_HAS_MPI )
255  template<class T>
256  static void Recv( T& obj, int proc, int tag, const MPI_Comm& comm = COMM_WORLD )
257  {
258 
259  int size;
260  MPI_Status status;
261  MPI_Recv( &size, 1, MPI_INT, proc, tag, comm, &status );
262  std::string buf( size, ' ' );
263  MPI_Recv( &buf[0], size, MPI_CHAR, proc, tag, comm, &status );
264 
265  std::istringstream iss( buf );
266  boost::archive::binary_iarchive ia( iss );
267  ia >> obj;
268  }
269 #else
270  template<class T>
271  static void Recv( T& obj, int proc, int tag )
272  {
273  // dummy function, don't have to do anything
274  }
275 #endif // FEELPP_HAS_MPI
276 
277 
278 #if defined( FEELPP_HAS_MPI )
279  static MPI_Comm COMM_WORLD;
280 #endif // FEELPP_HAS_MPI
281 
285  static mpi::communicator const& comm()
286  {
287  return S_world;
288  }
289 
293  static void barrier()
294  {
295  S_world.barrier();
296  }
297 
298 
299 protected:
300 
301 
302 private:
303 
304  Application( Application const & );
305 
306 private:
307 
308 
309 
310  static bool _S_is_mpi_initialized;
311  boost::shared_ptr<mpi::environment> M_env;
312  static mpi::communicator S_world;
313 };
314 
315 
316 }
317 #endif /* __Application_H */
static mpi::communicator const & comm()
Definition: applicationmpi.hpp:285
provides information about the Application
Definition: application.hpp:71
static bool initialized()
Definition: applicationmpi.hpp:140
static bool finalized()
Definition: applicationmpi.hpp:151
WorldComm & comm()
Definition: application.hpp:356
static std::string processorName()
Definition: applicationmpi.hpp:165
static void barrier()
Definition: applicationmpi.hpp:293
bool isMPIInitialized() const
Definition: applicationmpi.hpp:129
uint16_type processId()
Definition: application.hpp:227

Generated on Sun Dec 22 2013 13:10:54 for Feel++ by doxygen 1.8.5