PaGMO  1.1.5
mpi_environment.h
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #ifndef PAGMO_MPI_ENVIRONMENT_H
26 #define PAGMO_MPI_ENVIRONMENT_H
27 
28 #include <boost/archive/text_iarchive.hpp>
29 #include <boost/archive/text_oarchive.hpp>
30 #include <boost/numeric/conversion/cast.hpp>
31 #include <boost/utility.hpp>
32 #include <mpi.h>
33 #include <sstream>
34 #include <string>
35 #include <vector>
36 
37 #include "config.h"
38 
161 namespace pagmo
162 {
163 
165 
174 class __PAGMO_VISIBLE mpi_environment: private boost::noncopyable
175 {
176  public:
177  mpi_environment();
178  ~mpi_environment();
179  static bool is_multithread();
180  static int get_size();
181  static int get_rank();
183 
190  template <class T>
191  static void recv(T &retval, int source)
192  {
193  check_init();
194  MPI_Status status;
195  // First receive the size.
196  int size;
197  MPI_Recv(static_cast<void *>(&size),1,MPI_INT,source,0,MPI_COMM_WORLD,&status);
198  // Prepare the vector of chars.
199  std::vector<char> buffer_char(boost::numeric_cast<std::vector<char>::size_type>(size),0);
200  // Receive the payload.
201  MPI_Recv(static_cast<void *>(&buffer_char[0]),size,MPI_CHAR,source,1,MPI_COMM_WORLD,&status);
202  // Build the string from the vector.
203  const std::string buffer_str(buffer_char.begin(),buffer_char.end());
204  // Unpickle the payload.
205  std::stringstream ss(buffer_str);
206  boost::archive::text_iarchive ia(ss);
207  ia >> retval;
208  }
210 
217  template <class T>
218  static void send(const T &payload, int destination)
219  {
220  check_init();
221  std::stringstream ss;
222  boost::archive::text_oarchive oa(ss);
223  oa << payload;
224  const std::string buffer_str(ss.str());
225  std::vector<char> buffer_char(buffer_str.begin(),buffer_str.end());
226  // Send the size.
227  int size = boost::numeric_cast<int>(buffer_char.size());
228  MPI_Send(static_cast<void *>(&size),1,MPI_INT,destination,0,MPI_COMM_WORLD);
229  // Send the string.
230  MPI_Send(static_cast<void *>(&buffer_char[0]),size,MPI_CHAR,destination,1,MPI_COMM_WORLD);
231  }
232  static bool iprobe(int);
233  private:
234  static void listen();
235  static void check_init();
236  static bool m_initialised;
237  static bool m_multithread;
238 };
239 
240 }
241 
242 #endif
static void recv(T &retval, int source)
Receive MPI payload.
Root PaGMO namespace.
MPI environment class.
static void send(const T &payload, int destination)
Send MPI payload.