OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
MasterNode.h
Go to the documentation of this file.
1 #ifndef __MASTER_NODE__
2 #define __MASTER_NODE__
3 
4 #include <set>
5 #include <cmath>
6 #include <vector>
7 
8 #include <string>
9 #include <sstream>
10 
11 #include "mpi.h"
12 
13 #include <boost/archive/text_oarchive.hpp>
14 #include <boost/archive/text_iarchive.hpp>
15 #include <boost/serialization/vector.hpp>
16 #include <boost/serialization/utility.hpp>
17 
18 
19 //XXX: SolutionState_t must be serializable! (call
20 // SerializableSolutionState_t?)
34 template <
35  class SolutionState_t
36  , class NeighborStrategy_t
37 >
38 class MasterNode : public NeighborStrategy_t {
39 
40 
41 public:
42 
43  MasterNode(MPI_Comm master_comm, size_t buf_size_upper_bound, size_t dim,
44  int island_id)
45  : buf_size_upper_bound_(buf_size_upper_bound)
46  , master_comm_(master_comm) {
47 
48  int tmp = 0;
49  MPI_Comm_rank(master_comm, &tmp);
50  myID_ = static_cast<size_t>(tmp);
51 
52  MPI_Comm_size(master_comm, &tmp);
53  numMasters_ = static_cast<size_t>(tmp);
54  revision_state_.resize(numMasters_, 0);
55 
56  // better to use MPI-2 memory allocation methods
57  MPI_Alloc_mem(sizeof(char) * buf_size_upper_bound,
58  MPI_INFO_NULL, &serialized_best_values_);
59 
60  // expose our shared memory holding our best values
61  MPI_Win_create(serialized_best_values_, buf_size_upper_bound,
62  sizeof(char), MPI_INFO_NULL, master_comm, &win_);
63 
64  MPI_Win_create(&revision_, 1, sizeof(size_t), MPI_INFO_NULL,
65  master_comm, &win_rev_);
66 
67 
68  // execute neighbor strategy to learn which neighbors have to be
69  // updated with our solution state (and we collect from)
70  collectFrom_ = this->execute(numMasters_, dim, myID_, island_id);
71  }
72 
74  MPI_Win_free(&win_);
75  MPI_Free_mem(serialized_best_values_);
76  MPI_Win_free(&win_rev_);
77  }
78 
79 
81  void store(char *local_state, size_t buffer_size) {
82 
83  revision_++;
84  MPI_Win_fence(MPI_MODE_NOPUT, win_rev_);
85 
86  size_t buf_size = buffer_size;
87  if(buf_size > buf_size_upper_bound_)
88  std::cout << "windows too small: " << buffer_size << " / "
90 
91  memcpy(serialized_best_values_, local_state, buf_size);
92  MPI_Win_fence(MPI_MODE_NOPUT, win_);
93  //MPI_Win_fence((MPI_MODE_NOPUT | MPI_MODE_NOPRECEDE), win_);
94  }
95 
96 
98  void collect(std::ostringstream &states) {
99 
100  char *buffer;
101  MPI_Alloc_mem(buf_size_upper_bound_, MPI_INFO_NULL, &buffer);
102  SolutionState_t tmp_states;
103 
104 
105  for(size_t i=0; i < numMasters_; i++) {
106  // ignore all except for selected master PIDs
107  if(i == myID_) continue;
108  if(collectFrom_.count(i) == 0) continue;
109 
110  // only continue if new values are available on master i
111  size_t revision = 0;
112  MPI_Get(&revision, 1, MPI_UNSIGNED_LONG, i, 0, 1, MPI_UNSIGNED_LONG, win_rev_);
113  MPI_Win_fence(0, win_rev_);
114 
115  if(revision <= revision_state_[i]) continue;
116  revision_state_[i] = revision;
117 
118  MPI_Get(buffer, buf_size_upper_bound_, MPI_CHAR, i, 0,
119  buf_size_upper_bound_, MPI_CHAR, win_);
120  MPI_Win_fence(0, win_);
121 
122  std::istringstream is(buffer);
123  boost::archive::text_iarchive ia(is);
124 
125  //XXX: ugly that we have to know the SolutionState_t here
126  SolutionState_t state;
127  ia >> state;
128  tmp_states.insert(tmp_states.end(), state.begin(), state.end());
129  }
130 
131  boost::archive::text_oarchive oa(states);
132  oa << tmp_states;
133 
134  MPI_Free_mem(buffer);
135  }
136 
137 
138 private:
139 
142 
145  size_t numMasters_;
146  MPI_Comm master_comm_;
147 
148  // windows for storing revision and solution state
149  MPI_Win win_;
150  MPI_Win win_rev_;
151 
152  size_t myID_;
153 
155  std::set<size_t> collectFrom_;
157  size_t revision_;
159  std::vector<size_t> revision_state_;
160 };
161 
162 #endif
Implements a node in the network of all pilots, exposing store and collect operations on a specific s...
Definition: MasterNode.h:38
std::vector< size_t > revision_state_
revision numbers of my neighbors
Definition: MasterNode.h:159
size_t myID_
Definition: MasterNode.h:152
void store(char *local_state, size_t buffer_size)
store my best values
Definition: MasterNode.h:81
MPI_Win win_rev_
Definition: MasterNode.h:150
size_t revision_
my solution state revision number
Definition: MasterNode.h:157
MPI_Win win_
Definition: MasterNode.h:149
std::set< size_t > collectFrom_
neighbors we collect solution states from
Definition: MasterNode.h:155
MasterNode(MPI_Comm master_comm, size_t buf_size_upper_bound, size_t dim, int island_id)
Definition: MasterNode.h:43
MPI_Comm master_comm_
Definition: MasterNode.h:146
size_t numMasters_
Definition: MasterNode.h:145
size_t buf_size_upper_bound_
and upper bound on the allocated memory in the MPI window
Definition: MasterNode.h:144
char * serialized_best_values_
pointer to MPI window holding current best solution state
Definition: MasterNode.h:141
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
void collect(std::ostringstream &states)
collect all best values from all other masters
Definition: MasterNode.h:98