25 #include <boost/shared_ptr.hpp>
31 #include "exceptions.h"
32 #include "algorithm/base.h"
33 #include "population.h"
34 #include "mpi_environment.h"
39 bool mpi_environment::m_initialised =
false;
40 bool mpi_environment::m_multithread =
false;
54 pagmo_throw(std::runtime_error,
"cannot re-initialise the MPI environment");
57 int thread_level_provided;
58 MPI_Init_thread(NULL,NULL,MPI_THREAD_MULTIPLE,&thread_level_provided);
59 if (thread_level_provided >= MPI_THREAD_MULTIPLE) {
68 if (thread_level_provided < MPI_THREAD_SERIALIZED &&
get_rank() == 0) {
69 pagmo_throw(std::runtime_error,
"the master node must support at least the MPI_THREAD_SERIALIZED thread level");
73 pagmo_throw(std::runtime_error,
"the size of the MPI world must be at least 2");
85 pagmo_assert(m_initialised);
87 for (
int i = 1; i <
get_size(); ++i) {
89 send(shutdown_payload,i);
92 m_initialised =
false;
95 void mpi_environment::check_init()
98 pagmo_throw(std::runtime_error,
"MPI environment has not been initialised");
117 MPI_Iprobe(source,0,MPI_COMM_WORLD,&flag,&status);
133 MPI_Comm_size(MPI_COMM_WORLD,&retval);
149 MPI_Comm_rank(MPI_COMM_WORLD,&retval);
165 return m_multithread;
168 void mpi_environment::listen()
175 if (payload.first.get() == 0) {
176 pagmo_assert(payload.second.get() == 0);
181 const boost::shared_ptr<population> pop_copy(
new population(*payload.first));
184 payload.second->evolve(*payload.first);
185 }
catch (
const std::exception &e) {
186 std::cout <<
"MPI Remote Error during island evolution using " << payload.second->get_name() <<
": " << e.what() << std::endl;
188 std::cout <<
"MPI Remote Error during island evolution using " << payload.second->get_name() <<
", unknown exception caught. :(" << std::endl;
193 send(payload.first,0);
194 }
catch (
const boost::archive::archive_exception &e) {
195 std::cout <<
"MPI Send Error during island evolution using " << payload.second->get_name() <<
": " << e.what() << std::endl;
199 std::cout <<
"MPI Send Error during island evolution using " << payload.second->get_name() <<
", unknown exception caught. :(" << std::endl;
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
static bool is_multithread()
Thread-safety of the MPI implementation.
static void recv(T &retval, int source)
Receive MPI payload.
static bool iprobe(int)
Probe for message.
static int get_size()
MPI world size.
~mpi_environment()
Destructor.
static void send(const T &payload, int destination)
Send MPI payload.
mpi_environment()
Default constructor.
static int get_rank()
MPI rank.