PaGMO  1.1.5
mpi_island.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_ISLAND_H
26 #define PAGMO_MPI_ISLAND_H
27 
28 #include <boost/scoped_ptr.hpp>
29 #include <boost/thread/condition_variable.hpp>
30 #include <boost/thread/mutex.hpp>
31 #include <list>
32 #include <set>
33 #include <string>
34 
35 #include "base_island.h"
36 #include "config.h"
37 #include "algorithm/base.h"
38 #include "migration/base_r_policy.h"
39 #include "migration/base_s_policy.h"
40 #include "migration/best_s_policy.h"
41 #include "migration/fair_r_policy.h"
42 #include "population.h"
43 #include "problem/base.h"
44 #include "serialization.h"
45 
46 // Forward declarations.
47 namespace pagmo {
48 
49 class mpi_island;
50 
51 }
52 
53 namespace boost { namespace serialization {
54 
55 template <class Archive>
56 void save_construct_data(Archive &, const pagmo::mpi_island *, const unsigned int);
57 
58 template <class Archive>
59 inline void load_construct_data(Archive &, pagmo::mpi_island *, const unsigned int);
60 
61 }}
62 
63 namespace pagmo
64 {
65 
67 
77 class __PAGMO_VISIBLE mpi_island: public base_island
78 {
79  template <class Archive>
80  friend void boost::serialization::save_construct_data(Archive &, const pagmo::mpi_island *, const unsigned int);
81  template <class Archive>
82  friend void boost::serialization::load_construct_data(Archive &, pagmo::mpi_island *, const unsigned int);
83  public:
84  mpi_island(const mpi_island &);
85  explicit mpi_island(const algorithm::base &, const problem::base &, int = 0,
88  explicit mpi_island(const algorithm::base &, const population &,
91  mpi_island &operator=(const mpi_island &);
92  base_island_ptr clone() const;
93  protected:
94  void perform_evolution(const algorithm::base &, population &) const;
95  public:
96  std::string get_name() const;
97  private:
98  friend class boost::serialization::access;
99  template <class Archive>
100  void serialize(Archive &ar, const unsigned int)
101  {
102  // Join is already done in base_island.
103  ar & boost::serialization::base_object<base_island>(*this);
104  }
105  static void init_processors();
106  int acquire_processor() const;
107  void release_processor(int) const;
108  private:
109  static boost::mutex m_proc_mutex;
110  static boost::condition_variable m_proc_cond;
111  static boost::mutex m_mpi_mutex;
112  static boost::scoped_ptr<std::set<int> > m_available_processors;
113  static std::list<mpi_island const *> m_queue;
114 };
115 
116 }
117 
118 namespace boost { namespace serialization {
119 
120 template <class Archive>
121 inline void save_construct_data(Archive &ar, const pagmo::mpi_island *isl, const unsigned int)
122 {
123  // Save data required to construct instance.
124  pagmo::algorithm::base_ptr algo = isl->m_algo->clone();
125  pagmo::problem::base_ptr prob = isl->m_pop.problem().clone();
126  ar << algo;
127  ar << prob;
128 }
129 
130 template <class Archive>
131 inline void load_construct_data(Archive &ar, pagmo::mpi_island *isl, const unsigned int)
132 {
133  // Retrieve data from archive required to construct new instance.
136  ar >> algo;
137  ar >> prob;
138  // Invoke inplace constructor to initialize instance of the algorithm.
139  ::new(isl)pagmo::mpi_island(*algo,*prob);
140 }
141 
142 }} //namespaces
143 
144 BOOST_CLASS_EXPORT_KEY(pagmo::mpi_island)
145 
146 #endif
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
Root PaGMO namespace.
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
Definition: problem/base.h:62
Base class for migration replacement policies.
Definition: base_r_policy.h:57
Base class for migration selection policies.
Definition: base_s_policy.h:54
Base algorithm class.
Base problem class.
Definition: problem/base.h:148
Population class.
Definition: population.h:70
"Choose best" migration selection policy.
Definition: best_s_policy.h:48
Fair replacement policy.
Definition: fair_r_policy.h:49
virtual base_ptr clone() const =0
Clone method.
algorithm::base_ptr m_algo
Algorithm.
Definition: base_island.h:159
Base island class.
Definition: base_island.h:81
MPI island class.
Definition: mpi_island.h:77
boost::shared_ptr< base_island > base_island_ptr
Alias for the shared pointer to a pagmo::base_island.
Definition: base_island.h:49
population m_pop
Population.
Definition: base_island.h:161