ug4
parallel_nodes.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011-2015: G-CSC, Goethe University Frankfurt
3  * Author: Martin Rupp
4  *
5  * This file is part of UG4.
6  *
7  * UG4 is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License version 3 (as published by the
9  * Free Software Foundation) with the following additional attribution
10  * requirements (according to LGPL/GPL v3 §7):
11  *
12  * (1) The following notice must be displayed in the Appropriate Legal Notices
13  * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14  *
15  * (2) The following notice must be displayed at a prominent place in the
16  * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17  *
18  * (3) The following bibliography is recommended for citation and must be
19  * preserved in all covered files:
20  * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21  * parallel geometric multigrid solver on hierarchically distributed grids.
22  * Computing and visualization in science 16, 4 (2013), 151-164"
23  * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24  * flexible software system for simulating pde based models on high performance
25  * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26  *
27  * This program is distributed in the hope that it will be useful,
28  * but WITHOUT ANY WARRANTY; without even the implied warranty of
29  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30  * GNU Lesser General Public License for more details.
31  */
32 
33 #ifndef UG_PARALLEL_NODES_H_
34 #define UG_PARALLEL_NODES_H_
35 
36 #include "pcl/pcl.h"
37 #include <vector>
38 #include <set>
39 #include <map>
40 
43 
44 #include "common/util/sort_util.h"
46 #include "common/serialization.h"
47 
48 
49 #define NAE_APPEND
50 // serializes data from a vector<bool> into a binary stream
51 /*template <class TOStream>
52 void Serialize(TOStream& buf, const std::vector<bool>& vec)
53 {
54  size_t size = vec.size();
55  Serialize(buf, size);
56  size_t sm8 = size%8;
57  size_t s8 = size - sm8;
58  for(size_t i=0; i<s8; i+=8)
59  {
60  char a=0;
61  for(size_t j=0; j<8; j++)
62  if(vec[i+j])
63  a &= (1 << j);
64  Serialize(buf, a);
65  }
66  if(sm8)
67  {
68  char a=0;
69  for(size_t j=0; j<sm8; j++)
70  if(vec[s8+j])
71  a &= 1 << j;
72  Serialize(buf, a);
73  }
74 }
75 
77 template <class TIStream>
78 void Deserialize(TIStream& buf, std::vector<bool> &vec)
79 {
80  size_t size = 0;
81  Deserialize(buf, size);
82  vec.resize(size);
83  size_t sm8 = size%8;
84  size_t s8 = size - sm8;
85  char a;
86  for(size_t i=0; i<s8; i+=8)
87  {
88  Deserialize(buf, a);
89  char a=0;
90  for(size_t j=0; j<8; j++)
91  vec[i+j] = (a & (1 << j));
92  }
93  if(sm8)
94  {
95  Deserialize(buf, a);
96  for(size_t j=0; j<sm8; j++)
97  vec[s8+j] = (a & (1 << j));
98  }
99 }*/
100 
101 namespace ug
102 {
103 
112 {
113 private:
114  typedef std::map<AlgebraID,size_t>::iterator iterator;
115  typedef std::map<AlgebraID,size_t>::const_iterator const_iterator;
116 
117 public:
118  struct OverlapType
119  {
120 #define OT_SLAVE_FLAG 4
121 #define OT_MASTER_FLAG 2
122 #define OT_INNER_FLAG 1
123 
125  {
127  };
129  {
130  // create as inner node
131  set_inner();
132  }
133 
134  OverlapType(int distanceToMasterOrInner)
135  {
136  set_distance_to_master_or_inner(distanceToMasterOrInner);
137  }
138 
139  bool is_master_or_inner() const
140  {
141  return is_master() || is_inner();
142  }
143 
144  bool is_master() const
145  {
146  return type == OT_MASTER_FLAG;
147  }
148  bool is_slave() const
149  {
150  return type == (OT_SLAVE_FLAG & (1 << 3));
151  }
152 
153  bool is_inner() const
154  {
155  return type & OT_INNER_FLAG;
156  }
157 
158  void set_inner()
159  {
161  }
162 
163  void set_master()
164  {
166  }
167 
168  void set_slave()
169  {
170  type = OT_SLAVE_FLAG & (1 << 3);
171  }
172 
174  {
175  type = (i << 3);
176  }
177 
179  {
180  return type >> 3;
181  }
182 
183  friend std::ostream &operator << (std::ostream &out, const OverlapType &o)
184  {
185  if(o.is_master())
186  out << "master ";
187  if(o.is_slave())
188  out << "slave ";
189  if(o.is_inner())
190  out << "inner ";
191  out << "dTMI=" << o.distance_to_master_or_inner();
192  return out;
193  }
194 
195  int type;
196  };
197 private:
204  {
206  NewSlaveNotification(const AlgebraID &_id, int _newSlaveOnPID)
207  {
208  id = _id;
209  newSlaveOnPID = _newSlaveOnPID;
210  }
211  friend std::ostream &operator << (std::ostream &out, const NewSlaveNotification &n)
212  {
213  out << "notification that node " << n.id << " is slave on " << n.newSlaveOnPID << " ";
214  return out;
215  }
218  };
219 
220 private:
222 
223  std::map<AlgebraID, size_t> m_globalToLocal;
224  std::vector<AlgebraID> m_localToGlobal;
225 
228 
230  std::vector<OverlapType> m_OLtype;
231 
232  typedef std::map<int, BinaryBuffer> BufferMap;
233 
234  std::set<int> masterPIDs;
235  std::set<int> slavePIDs;
236 
238 
240 
258  std::map<int, std::set<size_t> > notified; // notified[pid] is the set of indices which are slave on pid.
259  std::map<int, std::vector<NewSlaveNotification> > newSlaveNotifications; // newSlaveNotifications is a vector of pids
260  std::map<int, std::set<size_t> > newMasters;
261  std::map<int, std::set<size_t> > newSlaves;
262 
263 public:
267 
269  {
270  return m_originalSize;
271  }
272 
273  size_t local_size() const
274  {
275  return m_localToGlobal.size();
276  }
277 
278  const AlgebraID &local_to_global(size_t i) const
279  {
280  UG_COND_THROW(i >= local_size(), i << " >= " << local_size());
281  return m_localToGlobal[i];
282  }
283 
284  const AlgebraID &operator [] (size_t i) const
285  {
286  return local_to_global(i);
287  }
288 
290  size_t get_local_index_if_available(const AlgebraID &globalIndex, bool &bHasIndex) const;
291 
293  size_t get_local_index_or_create_new(const AlgebraID &globalIndex, int distanceToMasterOrInner, bool &bCreated);
294 
295 
296  size_t get_local_index_or_create_new(const AlgebraID &globalIndex, int distanceToMasterOrInner);
297 
298  size_t global_to_local(const AlgebraID &globalIndex) const;
299 
300  size_t operator [] (const AlgebraID &globalIndex) const
301  {
302  return global_to_local(globalIndex);
303  }
304 
305 
306  void sort_by_global_id(std::vector<size_t> &v)
307  {
308  sort(v.begin(), v.end(), CompareIndicesBy(m_localToGlobal) );
309  }
310 
311  void insert_into_interface_sorted(std::vector<size_t> &v, IndexLayout::Interface &interface);
312 
313  void insert_into_layout_sorted(std::map<int, std::set<size_t> > &m, IndexLayout &layout);
314 
315  void insert_into_layout_sorted(std::map<int, std::vector<size_t> > &m, IndexLayout &layout);
316 
317  void sort_interface(IndexLayout::Interface &interface);
318 
319  void sort_layout(IndexLayout &layout);
320 
321 
323  {
324  return totalMasterLayout;
325  }
327  {
328  return totalSlaveLayout;
329  }
330 
331  const IndexLayout &master_layout() const
332  {
333  return m_layout->master();
334  }
335 
336  const IndexLayout &slave_layout() const
337  {
338  return m_layout->slave();
339  }
340 
342  {
343  return m_layout->comm();
344  }
345 
347  {
348  return m_layout->proc_comm();
349  }
350 
351  bool is_master_or_inner(size_t i) const
352  {
353  return m_OLtype[i].is_master_or_inner();
354  }
355 
356  bool is_inner(size_t i) const
357  {
358  return m_OLtype[i].is_inner();
359  }
360  bool is_slave(size_t i) const
361  {
362  return m_OLtype[i].is_slave();
363  }
364  bool is_master(size_t i) const
365  {
366  return m_OLtype[i].is_master();
367  }
368  size_t distance_to_master_or_inner(size_t i) const
369  {
370  return m_OLtype[i].distance_to_master_or_inner();
371  }
372  void print() const
373  {
374  for(size_t i=0; i<local_size(); i++)
375  UG_LOG(i << ": " << m_OLtype[i] << " globalID = " << local_to_global(i) << "\n");
376 
377  }
378 
380  const OverlapType &overlap_type(size_t i)
381  {
382  return m_OLtype[i];
383  }
384 
392  size_t create_slave_node(const AlgebraID &globalID, int distanceToMasterOrInner);
394 
408  void create_node(const AlgebraID &globalID, size_t localIndex, int pid);
409 
415  void create_node(const AlgebraID &globalID, int pid)
416  {
417  create_node(globalID, global_to_local(globalID), pid);
418  }
419 
425  void create_node(size_t localIndex, int pid)
426  {
427  create_node(local_to_global(localIndex), localIndex, pid);
428  }
429 
430 
439 
446  void process();
447 
454  void add_new_layouts_to(IndexLayout &newMasterLayout, IndexLayout &newSlaveLayout);
455 #ifdef NAE_APPEND
456  void append_nodes_without_comm(size_t n);
457 #endif
458 private:
459  void create_mark_map(const IndexLayout &masterLayout);
460 
461  /*void check_new_nodes(pcl::InterfaceCommunicator<IndexLayout> &communicator,
462  IndexLayout &sendingLayout, IndexLayout &receivingLayout)
463  {
464  std::set<int> sendingPIDs;
465  for(IndexLayout::iterator it = sendingLayout.begin(); it != sendingLayout.end(); ++it)
466  sendingPIDs.insert(sendingLayout.proc_id(it));
467 
468  std::set<int> receivingPIDs;
469  for(IndexLayout::iterator it = receivingLayout.begin(); it != receivingLayout.end(); ++it)
470  receivingPIDs.insert(receivingLayout.proc_id(it));
471  check_new_nodes(commnunicator, sendingPIDs, receivingPIDs);
472  }
473 
474  void check_new_nodes(std::vector<int> sendingPIDs, std::vector<int> receivingPIDs)
475  {
476  for(size_t i=0; i<sendingPIDs.size(); i++)
477  {
478  BinaryBuffer buf;
479  int pid = sendingPIDs[i];
480  Serialize(buf, newNodesOn[pid]);
481  communicator.send_raw(pid, buf.buffer(), buf.write_pos(), false);
482  }
483 
484  for(size_t i=0; i<receivingPIDs.size(); i++)
485  communicator.receive_raw(pid, newNodesBufferMap[receivingPIDs[i]]);
486 
487  communicator.communicate();
488  }*/
489 
490 
491 };
492 
493 
494 }
495 #endif
496 
Definition: smart_pointer.h:296
Performs communication between interfaces on different processes.
Definition: pcl_interface_communicator.h:68
You may add elements to this interface and iterate over them.
Definition: pcl_communication_structs.h:207
Definition: pcl_process_communicator.h:70
pcl::OrderedInterface< size_t, std::vector > Interface
Interface type.
Definition: pcl_communication_structs.h:467
Definition: parallel_nodes.h:112
const AlgebraID & local_to_global(size_t i) const
Definition: parallel_nodes.h:278
std::map< int, std::set< size_t > > newSlaves
Definition: parallel_nodes.h:261
size_t m_originalSize
Definition: parallel_nodes.h:229
std::vector< OverlapType > m_OLtype
Definition: parallel_nodes.h:230
std::set< int > slavePIDs
Definition: parallel_nodes.h:235
std::map< int, std::vector< NewSlaveNotification > > newSlaveNotifications
Definition: parallel_nodes.h:259
bool is_inner(size_t i) const
Definition: parallel_nodes.h:356
void issue(pcl::InterfaceCommunicator< IndexLayout > &communicator)
Definition: parallel_nodes.cpp:297
void process()
Definition: parallel_nodes.cpp:320
IndexLayout totalMasterLayout
Definition: parallel_nodes.h:226
void insert_into_layout_sorted(std::map< int, std::set< size_t > > &m, IndexLayout &layout)
void append_nodes_without_comm(size_t n)
Appending masters (without comm skills)
Definition: parallel_nodes.cpp:393
std::map< int, std::set< size_t > > notified
map for marking nodes
Definition: parallel_nodes.h:258
const pcl::ProcessCommunicator & proc_comm() const
Definition: parallel_nodes.h:346
const OverlapType & overlap_type(size_t i)
returns the overlap type (inner, master, slave or distanceToMasterOrInner=X)
Definition: parallel_nodes.h:380
std::set< int > masterPIDs
Definition: parallel_nodes.h:234
void sort_layout(IndexLayout &layout)
Definition: parallel_nodes.cpp:193
BufferMap notificationBufferMap
Definition: parallel_nodes.h:237
const IndexLayout & slave_layout() const
Definition: parallel_nodes.h:336
std::vector< AlgebraID > m_localToGlobal
Definition: parallel_nodes.h:224
std::map< AlgebraID, size_t >::const_iterator const_iterator
Definition: parallel_nodes.h:115
void insert_into_interface_sorted(std::vector< size_t > &v, IndexLayout::Interface &interface)
Definition: parallel_nodes.cpp:150
std::map< int, BinaryBuffer > BufferMap
Definition: parallel_nodes.h:232
ConstSmartPtr< AlgebraLayouts > m_layout
Definition: parallel_nodes.h:221
void create_node(const AlgebraID &globalID, size_t localIndex, int pid)
Definition: parallel_nodes.cpp:232
void sort_interface(IndexLayout::Interface &interface)
Definition: parallel_nodes.cpp:181
size_t get_local_index_or_create_new(const AlgebraID &globalIndex, int distanceToMasterOrInner, bool &bCreated)
get_index_or_create_new: returns a local index by creating and saving a new one or returning an old
Definition: parallel_nodes.cpp:107
size_t get_original_size()
Definition: parallel_nodes.h:268
bool is_master(size_t i) const
Definition: parallel_nodes.h:364
std::map< int, std::set< size_t > > newMasters
Definition: parallel_nodes.h:260
IndexLayout & get_total_slave_layout()
Definition: parallel_nodes.h:326
size_t local_size() const
Definition: parallel_nodes.h:273
pcl::InterfaceCommunicator< IndexLayout > & comm() const
Definition: parallel_nodes.h:341
void insert_into_layout_sorted(std::map< int, std::vector< size_t > > &m, IndexLayout &layout)
void sort_by_global_id(std::vector< size_t > &v)
Definition: parallel_nodes.h:306
IndexLayout totalSlaveLayout
Definition: parallel_nodes.h:227
size_t global_to_local(const AlgebraID &globalIndex) const
Definition: parallel_nodes.cpp:141
void create_node(size_t localIndex, int pid)
Definition: parallel_nodes.h:425
bool is_slave(size_t i) const
Definition: parallel_nodes.h:360
std::map< AlgebraID, size_t >::iterator iterator
Definition: parallel_nodes.h:114
void create_node(const AlgebraID &globalID, int pid)
Definition: parallel_nodes.h:415
IndexLayout & get_total_master_layout()
Definition: parallel_nodes.h:322
const AlgebraID & operator[](size_t i) const
Definition: parallel_nodes.h:284
size_t get_local_index_if_available(const AlgebraID &globalIndex, bool &bHasIndex) const
returns a local index by returning a old local one or a saved created one
Definition: parallel_nodes.cpp:83
size_t distance_to_master_or_inner(size_t i) const
Definition: parallel_nodes.h:368
void create_mark_map(const IndexLayout &masterLayout)
Definition: parallel_nodes.cpp:200
ParallelNodes(const ParallelNodes &)
void print() const
Definition: parallel_nodes.h:372
bool is_master_or_inner(size_t i) const
Definition: parallel_nodes.h:351
size_t create_slave_node(const AlgebraID &globalID, int distanceToMasterOrInner)
Definition: parallel_nodes.cpp:219
std::map< AlgebraID, size_t > m_globalToLocal
Definition: parallel_nodes.h:223
void add_new_layouts_to(IndexLayout &newMasterLayout, IndexLayout &newSlaveLayout)
Definition: parallel_nodes.cpp:382
const IndexLayout & master_layout() const
Definition: parallel_nodes.h:331
CompareIndicesByClass< TCompareValues, false > CompareIndicesBy(const TCompareValues &values)
Definition: sort_util.h:114
#define UG_LOG(msg)
Definition: log.h:367
#define UG_COND_THROW(cond, msg)
UG_COND_THROW(cond, msg) : performs a UG_THROW(msg) if cond == true.
Definition: error.h:61
the ug namespace
#define OT_INNER_FLAG
Definition: parallel_nodes.h:122
#define OT_SLAVE_FLAG
Definition: parallel_nodes.h:120
#define OT_MASTER_FLAG
Definition: parallel_nodes.h:121
this type is used to identify distributed objects.
Definition: algebra_id.h:46
Definition: parallel_nodes.h:204
friend std::ostream & operator<<(std::ostream &out, const NewSlaveNotification &n)
Definition: parallel_nodes.h:211
NewSlaveNotification()
Definition: parallel_nodes.h:205
NewSlaveNotification(const AlgebraID &_id, int _newSlaveOnPID)
Definition: parallel_nodes.h:206
int newSlaveOnPID
Definition: parallel_nodes.h:217
AlgebraID id
Definition: parallel_nodes.h:216
Definition: parallel_nodes.h:119
int type
Definition: parallel_nodes.h:195
void set_inner()
Definition: parallel_nodes.h:158
bool is_master_or_inner() const
Definition: parallel_nodes.h:139
friend std::ostream & operator<<(std::ostream &out, const OverlapType &o)
Definition: parallel_nodes.h:183
size_t distance_to_master_or_inner() const
Definition: parallel_nodes.h:178
OverlapType()
Definition: parallel_nodes.h:128
bool is_master() const
Definition: parallel_nodes.h:144
bool is_inner() const
Definition: parallel_nodes.h:153
void set_distance_to_master_or_inner(size_t i)
Definition: parallel_nodes.h:173
eOverlapType
Definition: parallel_nodes.h:125
@ OT_MASTER
Definition: parallel_nodes.h:126
@ OT_SLAVE
Definition: parallel_nodes.h:126
@ OT_OUTER
Definition: parallel_nodes.h:126
void set_master()
Definition: parallel_nodes.h:163
OverlapType(int distanceToMasterOrInner)
Definition: parallel_nodes.h:134
bool is_slave() const
Definition: parallel_nodes.h:148
void set_slave()
Definition: parallel_nodes.h:168