OpenFPM_pdata  1.1.0
Project that contain the implementation of distributed structures
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Pages
nn_processor.hpp
1 /*
2  * nn_processor.hpp
3  *
4  * Created on: Aug 9, 2015
5  * Author: i-bird
6  */
7 
8 #ifndef SRC_DECOMPOSITION_NN_PROCESSOR_HPP_
9 #define SRC_DECOMPOSITION_NN_PROCESSOR_HPP_
10 
11 #include "common.hpp"
12 #include <unordered_map>
13 
22 template<unsigned int dim, typename T>
23 class nn_prcs
24 {
27 
30 
32  std::unordered_map<size_t, N_box<dim,T>> nn_processor_subdomains;
33 
35  std::unordered_map<size_t, N_box<dim,T>> nn_processor_subdomains_tmp;
36 
41 
44 
46  size_t recv_cnt;
47 
49  bool aBC;
50 
93  inline void consistent_shift(Box<dim,T> & box, const Box<dim,T> & domain, const Point<dim,T> & shift)
94  {
95  for (size_t k = 0 ; k < dim ; k++)
96  {
97  // if it touch on the left and shift on the right
98  if (box.getLow(k) == domain.getLow(k) && shift.get(k) > 0)
99  {
100  box.setLow(k,domain.getHigh(k));
101  box.setHigh(k,box.getHigh(k) + shift.get(k));
102  }
103  else if (box.getLow(k) == domain.getHigh(k) && shift.get(k) < 0)
104  {
105  box.setLow(k,domain.getLow(k));
106  box.setHigh(k,box.getHigh(k) + shift.get(k));
107  }
108  else if (box.getHigh(k) == domain.getHigh(k) && shift.get(k) < 0)
109  {
110  box.setHigh(k,domain.getLow(k));
111  box.setLow(k,box.getLow(k) + shift.get(k));
112  }
113  else if (box.getHigh(k) == domain.getLow(k) && shift.get(k) > 0)
114  {
115  box.setHigh(k,domain.getHigh(k));
116  box.setLow(k,box.getLow(k) + shift.get(k));
117  }
118  else
119  {
120  box.setHigh(k,box.getHigh(k) + shift.get(k));
121  box.setLow(k,box.getLow(k) + shift.get(k));
122  }
123  }
124  }
125 
139  static void * message_alloc(size_t msg_i ,size_t total_msg, size_t total_p, size_t i, size_t ri, void * ptr)
140  {
141  // cast the pointer
142  nn_prcs<dim,T> * cd = static_cast< nn_prcs<dim,T> *>(ptr);
143 
144  cd->nn_processor_subdomains[i].bx.resize(msg_i / sizeof(::Box<dim,T>) );
145 
146  // Return the receive pointer
147  return cd->nn_processor_subdomains[i].bx.getPointer();
148  }
149 
158  inline void add_nn_subdomain(size_t i, size_t r_sub, const Box<dim,T> & bx, const comb<dim> & c)
159  {
161  nnpst.bx.add(bx);
162  nnpst.pos.add(c);
163  nnpst.r_sub.add(r_sub);
164  }
165 
173  void add_box_periodic(const Box<dim,T> & domain, const Ghost<dim,T> & ghost, const size_t (&bc)[dim])
174  {
175  HyperCube<dim> hyp;
176 
177  // first we create boxes at the border of the domain used to detect the sub-domain
178  // that must be adjusted, each of this boxes define a shift in case of periodic boundary condition
179  for (long int i = dim-1 ; i >= 0 ; i--)
180  {
181  std::vector<comb<dim>> cmbs = hyp.getCombinations_R_bc(i,bc);
182 
183  for (size_t j = 0 ; j < cmbs.size() ; j++)
184  {
185  if (check_valid(cmbs[j],bc) == false)
186  continue;
187 
188  // Calculate the sector box
189  Box<dim,T> bp;
191 
192  for (size_t k = 0 ; k < dim ; k++)
193  {
194  switch (cmbs[j][k])
195  {
196  case 1:
197  bp.setLow(k,domain.getHigh(k)+ghost.getLow(k));
198  bp.setHigh(k,domain.getHigh(k));
199  shift.get(k) = -domain.getHigh(k)+domain.getLow(k);
200  break;
201  case 0:
202  bp.setLow(k,domain.getLow(k));
203  bp.setHigh(k,domain.getHigh(k));
204  shift.get(k) = 0;
205  break;
206  case -1:
207  bp.setLow(k,domain.getLow(k));
208  bp.setHigh(k,domain.getLow(k)+ghost.getHigh(k));
209  shift.get(k) = domain.getHigh(k)-domain.getLow(k);
210  break;
211  }
212  }
213 
214  // Detect all the sub-domain involved, shift them and add to the list
215  // Detection is performed intersecting the sub-domains with the ghost
216  // parts near the domain borders
217  for (size_t k = 0 ; k < getNNProcessors() ; k++)
218  {
219  // sub-domains of the near processor
221 
222  for (size_t l = 0 ; l < nn_sub.size(); l++)
223  {
224  Box<dim,T> sub = nn_sub.get(l);
225  Box<dim,T> b_int;
226 
227  if (sub.Intersect(bp,b_int) == true)
228  {
229  Box<dim,T> sub2 = sub;
230  sub2 += shift;
231 
232  // Here we have to be careful of rounding off problems, in particular if any part
233  // of the sub-domain touch the border of the domain
234 
235  consistent_shift(sub,domain,shift);
236 
237  add_nn_subdomain(IDtoProc(k),l,sub,cmbs[j]);
238  }
239  }
240  }
241  }
242  }
243 
244  flush();
245  }
246 
251  void flush()
252  {
253  for ( auto it = nn_processor_subdomains_tmp.begin(); it != nn_processor_subdomains_tmp.end(); ++it )
254  {
255  const N_box<dim,T> & nnp_bx = it->second;
256 
257  for (size_t i = 0 ; i < nnp_bx.bx.size() ; i++)
258  {
259  N_box<dim,T> & nnps = nn_processor_subdomains[it->first];
260  const N_box<dim,T> & nnps_tmp = nn_processor_subdomains_tmp[it->first];
261 
262  nnps.bx.add(nnps_tmp.bx.get(i));
263  nnps.pos.add(nnps_tmp.pos.get(i));
264  nnps.r_sub.add(nnps_tmp.r_sub.get(i));
265  }
266  }
267 
269  }
270 
271 public:
272 
275  :v_cl(v_cl),recv_cnt(0),aBC(false)
276  {}
277 
279  nn_prcs(const nn_prcs<dim,T> & ilg)
280  :v_cl(ilg.v_cl),recv_cnt(0),aBC(false)
281  {
282  this->operator=(ilg);
283  };
284 
287  :v_cl(ilg.v_cl),recv_cnt(0),aBC(false)
288  {
289  this->operator=(ilg);
290  }
291 
303  static bool inline check_valid(comb<dim> cmb,const size_t (& bc)[dim])
304  {
305  // the combination 0 is not valid
306  if (cmb.n_zero() == dim)
307  return false;
308 
309  for (size_t i = 0 ; i < dim ; i++)
310  {
311  if (bc[i] == NON_PERIODIC && cmb.getComb()[i] != 0)
312  return false;
313  }
314  return true;
315  }
316 
325  {
329  boxes = nnp.boxes;
330 
331  return *this;
332  }
333 
342  {
343  nn_processors.swap(nnp.nn_processors);
344  nn_processor_subdomains.swap(nnp.nn_processor_subdomains);
345  proc_adj_box.swap(nnp.proc_adj_box);
346  boxes = nnp.boxes;
347 
348  return *this;
349  }
350 
357  void create(const openfpm::vector<openfpm::vector<long unsigned int> > & box_nn_processor, const openfpm::vector<SpaceBox<dim,T>> & sub_domains)
358  {
359  // produce the list of the adjacent processor (nn_processors) list
360  for (size_t i = 0 ; i < box_nn_processor.size() ; i++)
361  {
362  for (size_t j = 0 ; j < box_nn_processor.get(i).size() ; j++)
363  {
364  nn_processors.add(box_nn_processor.get(i).get(j));
365  }
366  }
367 
368  // make the list of the processor sort and unique
369  std::sort(nn_processors.begin(), nn_processors.end());
370  auto last = std::unique(nn_processors.begin(), nn_processors.end());
371  nn_processors.erase(last, nn_processors.end());
372 
373  // link nn_processor_subdomains to nn_processors
374  // it is used to quickly convert the Processor rank to the position in the list of the
375  // near processors
376  for (size_t i = 0 ; i < box_nn_processor.size() ; i++)
377  {
378  for (size_t j = 0 ; j < box_nn_processor.get(i).size() ; j++)
379  {
380  // processor id adjacent to this sub-domain
381  size_t proc_id = box_nn_processor.get(i).get(j);
382 
383  size_t k = 0;
384  // search inside near processor list
385  for (k = 0 ; k < nn_processors.size() ; k++)
386  if (nn_processors.get(k) == proc_id) break;
387 
388  nn_processor_subdomains[proc_id].id = k;
389  }
390  }
391 
392  // create a buffer with the sub-domains that can have an intersection with
393  // the near processors
394  proc_adj_box.resize(getNNProcessors());
395  boxes.resize(getNNProcessors());
396 
397  for (size_t b = 0 ; b < box_nn_processor.size() ; b++)
398  {
399  for (size_t p = 0 ; p < box_nn_processor.get(b).size() ; p++)
400  {
401  size_t prc = box_nn_processor.get(b).get(p);
402 
403  // id of the processor in the processor list
404  // [value between 0 and the number of the near processors]
405  size_t id = ProctoID(prc);
406 
407  boxes.get(id).add(sub_domains.get(b));
408  proc_adj_box.get(id).add(b);
409  }
410  }
411 
413 
414  // Get the sub-domains of the near processors
416 
417  // Add to all the received sub-domains the information that they live in the central sector
418  for ( auto it = nn_processor_subdomains.begin(); it != nn_processor_subdomains.end(); ++it )
419  {
420  const N_box<dim,T> & nnp_bx = it->second;
421 
422  for (size_t i = 0 ; i < nnp_bx.bx.size() ; i++)
423  {
424  comb<dim> c;
425  c.zero();
426 
427  N_box<dim,T> & nnps = nn_processor_subdomains[it->first];
428 
429  nnps.pos.add(c);
430  nnps.r_sub.add(i);
431  nnps.n_real_sub = nnps.bx.size();
432  }
433  }
434  }
435 
441  inline size_t getNNProcessors() const
442  {
443  return nn_processors.size();
444  }
445 
453  inline size_t IDtoProc(size_t id) const
454  {
455  return nn_processors.get(id);
456  }
457 
465  inline const openfpm::vector< size_t > & getNearSubdomainsRealId(size_t p_id) const
466  {
467  auto key = nn_processor_subdomains.find(p_id);
468 #ifdef SE_CLASS1
469  if (key == nn_processor_subdomains.end())
470  {
471  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " error this process rank is not adjacent to the local processor";
472  }
473 #endif
474 
475  return key->second.r_sub;
476  }
477 
485  inline const openfpm::vector< ::Box<dim,T> > & getNearSubdomains(size_t p_id) const
486  {
487  auto key = nn_processor_subdomains.find(p_id);
488 #ifdef SE_CLASS1
489  if (key == nn_processor_subdomains.end())
490  {
491  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " error this process rank is not adjacent to the local processor";
492  }
493 #endif
494 
495  return key->second.bx;
496  }
497 
507  inline size_t getNRealSubdomains(size_t p_id) const
508  {
509  auto key = nn_processor_subdomains.find(p_id);
510 #ifdef SE_CLASS1
511  if (key == nn_processor_subdomains.end())
512  {
513  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " error this process rank is not adjacent to the local processor";
514  }
515 #endif
516 
517  return key->second.n_real_sub;
518  }
519 
527  inline const openfpm::vector< comb<dim> > & getNearSubdomainsPos(size_t p_id) const
528  {
529  auto key = nn_processor_subdomains.find(p_id);
530 #ifdef SE_CLASS1
531  if (key == nn_processor_subdomains.end())
532  {
533  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " error this process rank is not adjacent to the local processor";
534  }
535 #endif
536  return key->second.pos;
537  }
538 
546  inline size_t getNearProcessor(size_t p_id) const
547  {
548  auto key = nn_processor_subdomains.find(p_id);
549 #ifdef SE_CLASS1
550  if (key == nn_processor_subdomains.end())
551  {
552  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " error this process rank is not adjacent to the local processor";
553  }
554 #endif
555  return key->second.id;
556  }
557 
558 
567  inline const openfpm::vector<size_t> & getSentSubdomains(size_t p_id) const
568  {
569  return proc_adj_box.get(p_id);
570  }
571 
579  inline size_t ProctoID(size_t p) const
580  {
581  auto key = nn_processor_subdomains.find(p);
582 #ifdef SE_CLASS1
583  if (key == nn_processor_subdomains.end())
584  {
585  std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " error this process rank is not adjacent to the local processor";
586  }
587 #endif
588 
589  return key->second.id;
590  }
591 
605  bool write(std::string output) const
606  {
608  VTKWriter<openfpm::vector<::Box<dim,T>>,VECTOR_BOX> vtk_box2;
609  for (size_t p = 0 ; p < nn_processors.size() ; p++)
610  {
611  size_t prc = nn_processors.get(p);
612  auto it = nn_processor_subdomains.find(prc);
613  if (it != nn_processor_subdomains.end())
614  vtk_box2.add(nn_processor_subdomains.at(prc).bx);
615  }
616  vtk_box2.write(output + std::string("subdomains_adjacent_") + std::to_string(v_cl.getProcessUnitID()) + std::string(".vtk"));
617 
618  return true;
619  }
620 
628  void applyBC(const Box<dim,T> & domain, const Ghost<dim,T> & ghost, const size_t (&bc)[dim])
629  {
630  if (aBC == true)
631  {
632  std::cerr << "Warning " << __FILE__ << ":" << __LINE__ << " apply BC is suppose to be called only one time\n";
633  return;
634  }
635 
636  aBC=true;
637 
638  add_box_periodic(domain,ghost,bc);
639  }
640 
649  {
650  if (np.getNNProcessors() != getNNProcessors())
651  return false;
652 
653  for (size_t p = 0 ; p < getNNProcessors() ; p++)
654  {
656  return false;
658  return false;
659  if (getSentSubdomains(p) != np.getSentSubdomains(p))
660  return false;
661  }
662 
663  return true;
664  }
665 
669  void reset()
670  {
671  nn_processors.clear();
672  nn_processor_subdomains.clear();
674  proc_adj_box.clear();
675  boxes.clear();
676  recv_cnt = 0;
677  aBC = false;
678  }
679 
681  std::unordered_map<size_t, N_box<dim,T>> & get_nn_processor_subdomains()
682  {
684  }
685 
688  {
689  return nn_processors;
690  }
691 };
692 
693 
694 #endif /* SRC_DECOMPOSITION_NN_PROCESSOR_HPP_ */
size_t ProctoID(size_t p) const
Convert the processor rank to the id in the list.
nn_prcs(nn_prcs< dim, T > &&ilg)
Constructor from temporal ie_loc_ghost.
std::unordered_map< size_t, N_box< dim, T > > & get_nn_processor_subdomains()
Used for testing porpose do not use.
bool aBC
applyBC function is suppose to be called only one time
This class represent an N-dimensional box.
Definition: SpaceBox.hpp:26
size_t IDtoProc(size_t id) const
Return the processor id of the near processor list at place id.
bool is_equal(nn_prcs< dim, T > &np)
Check if the nn_prcs contain the same information.
T getLow(int i) const
get the i-coordinate of the low bound interval of the box
Definition: Box.hpp:479
Position of the element of dimension d in the hyper-cube of dimension dim.
Definition: comb.hpp:34
size_t getProcessUnitID()
Get the process unit id.
size_t getNNProcessors() const
Get the number of Near processors.
openfpm::vector< openfpm::vector< size_t > > proc_adj_box
Vcluster & v_cl
Virtual cluster.
T getHigh(int i) const
get the high interval of the box
Definition: Box.hpp:490
void setHigh(int i, T val)
set the high interval of the box
Definition: Box.hpp:467
size_t size()
Stub size.
Definition: map_vector.hpp:70
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:22
static bool check_valid(comb< dim > cmb, const size_t(&bc)[dim])
static std::vector< comb< dim > > getCombinations_R_bc(size_t d, const size_t(&bc)[dim])
Definition: HyperCube.hpp:151
openfpm::vector< size_t > r_sub
Definition: common.hpp:179
nn_prcs(const nn_prcs< dim, T > &ilg)
Constructor from another nn_prcs.
void sendrecvMultipleMessagesNBX(openfpm::vector< size_t > &prc, openfpm::vector< T > &data, openfpm::vector< size_t > prc_recv, openfpm::vector< size_t > &recv_sz, void *(*msg_alloc)(size_t, size_t, size_t, size_t, size_t, void *), void *ptr_arg, long int opt=NONE)
Send and receive multiple messages.
bool write(std::string output) const
Write the decomposition as VTK file.
Definition: Ghost.hpp:39
Implementation of VCluster class.
Definition: VCluster.hpp:36
std::unordered_map< size_t, N_box< dim, T > > nn_processor_subdomains_tmp
when we add new boxes, are added here
void add_box_periodic(const Box< dim, T > &domain, const Ghost< dim, T > &ghost, const size_t(&bc)[dim])
In case of periodic boundary conditions we replicate the sub-domains at the border.
bool Intersect(const Box< dim, T > &b, Box< dim, T > &b_out) const
Intersect.
Definition: Box.hpp:88
const openfpm::vector< size_t > & getNearSubdomainsRealId(size_t p_id) const
Get the real-id of the sub-domains of a near processor.
char * getComb()
get the combination array pointer
Definition: comb.hpp:260
size_t recv_cnt
Receive counter.
void consistent_shift(Box< dim, T > &box, const Box< dim, T > &domain, const Point< dim, T > &shift)
It shift a box but it does consistently.
const T & get(size_t i) const
Get coordinate.
Definition: Point.hpp:142
openfpm::vector< size_t > nn_processors
List of adjacent processors.
void setLow(int i, T val)
set the low interval of the box
Definition: Box.hpp:456
nn_prcs(Vcluster &v_cl)
Constructor require Vcluster.
void zero()
Set all the elements to zero.
Definition: comb.hpp:83
openfpm::vector< openfpm::vector< ::SpaceBox< dim, T > > > boxes
contain the set of sub-domains sent to the other processors
This class represent an N-dimensional box.
Definition: Box.hpp:56
nn_prcs< dim, T > & operator=(nn_prcs< dim, T > &&nnp)
Copy the object.
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
size_t getNearProcessor(size_t p_id) const
Get the near processor id.
static void * message_alloc(size_t msg_i, size_t total_msg, size_t total_p, size_t i, size_t ri, void *ptr)
Message allocation.
void flush()
Flush the temporal added sub-domain to the processor sub-domain.
nn_prcs< dim, T > & operator=(const nn_prcs< dim, T > &nnp)
Copy the object.
openfpm::vector< comb< dim > > pos
near processor sector position (or where they live outside the domain)
Definition: common.hpp:171
const openfpm::vector< comb< dim > > & getNearSubdomainsPos(size_t p_id) const
Get the sub-domains sector position of a near processor.
openfpm::vector< size_t > & get_nn_processors()
Used for testing porpose do not use.
This class calculate elements of the hyper-cube.
Definition: HyperCube.hpp:57
void applyBC(const Box< dim, T > &domain, const Ghost< dim, T > &ghost, const size_t(&bc)[dim])
Apply boundary conditions.
const openfpm::vector< size_t > & getSentSubdomains(size_t p_id) const
For each near processor it give a vector with the id of the local sub-domain sent to that processor...
openfpm::vector<::Box< dim, T > > bx
near processor sub-domains
Definition: common.hpp:168
size_t n_real_sub
Number of real sub-domains or sub-domain in the central sector.
Definition: common.hpp:174
void reset()
Reset the nn_prcs structure.
const openfpm::vector< ::Box< dim, T > > & getNearSubdomains(size_t p_id) const
Get the sub-domains of a near processor.
void add_nn_subdomain(size_t i, size_t r_sub, const Box< dim, T > &bx, const comb< dim > &c)
add sub-domains to processor for a near processor i
std::unordered_map< size_t, N_box< dim, T > > nn_processor_subdomains
for each near processor store the sub-domains of the near processors
size_t getNRealSubdomains(size_t p_id) const
Get the number of real sub-domains of a near processor.
void create(const openfpm::vector< openfpm::vector< long unsigned int > > &box_nn_processor, const openfpm::vector< SpaceBox< dim, T >> &sub_domains)
Create the list of adjacent processors and the list of adjacent sub-domains.
This class store the adjacent processors and the adjacent sub_domains.