ug4
communication_policies.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2015: G-CSC, Goethe University Frankfurt
3  * Authors: Andreas Vogel, Sebastian Reiter
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 __H__LIB_ALGEBRA__PARALLELIZATION__COMMUNICATION_POLICIES__
34 #define __H__LIB_ALGEBRA__PARALLELIZATION__COMMUNICATION_POLICIES__
35 
36 #include "pcl/pcl.h"
37 #include "common/serialization.h"
38 #include "parallel_index_layout.h"
39 #include "algebra_id.h"
40 
41 
42 namespace ug{
43 
44 // predeclaration of block_traits
57 template <typename t> struct block_traits
58 {
59  enum{
60  is_static = 1
61  };
62 };
63 
76 
78 
86 template <class TVector>
87 class ComPol_VecCopy : public pcl::ICommunicationPolicy<IndexLayout>
88 {
89  public:
91  ComPol_VecCopy() : m_pVecDest(NULL), m_pVecSrc(NULL) {}
92 
94  ComPol_VecCopy(TVector* pVec): m_pVecDest(pVec), m_pVecSrc(pVec) {}
95 
97  ComPol_VecCopy(TVector* pVecDest, const TVector* pVecSrc)
98  : m_pVecDest(pVecDest), m_pVecSrc(pVecSrc) {}
99 
101  void set_vector(TVector* pVec) {m_pVecDest = pVec; m_pVecSrc = pVec;}
102 
104  void set_vector(TVector* pVecDest, const TVector* pVecSrc)
105  {m_pVecDest = pVecDest; m_pVecSrc = pVecSrc;}
106 
108 
117  virtual int
119  {
121  return interface.size() * sizeof(typename TVector::value_type);
122  else
123  return -1;
124  }
125 
127 
134  virtual bool
135  collect(ug::BinaryBuffer& buff, const Interface& interface)
136  {
137  PROFILE_BEGIN_GROUP(ComPol_VecCopy_collect, "algebra parallelization");
138  // check that vector has been set
139  if(m_pVecSrc == NULL) return false;
140 
141  // rename for convenience
142  const TVector& v = *m_pVecSrc;
143 
144  // loop interface
145  for(typename Interface::const_iterator iter = interface.begin();
146  iter != interface.end(); ++iter)
147  {
148  // get index
149  const size_t index = interface.get_element(iter);
150 
151  // write entry into buffer
152  Serialize(buff, v[index]);
153  }
154  return true;
155  }
156 
158 
164  virtual bool
165  extract(ug::BinaryBuffer& buff, const Interface& interface)
166  {
167  PROFILE_BEGIN_GROUP(ComPol_VecCopy_extract, "algebra parallelization");
168  // check that vector has been set
169  if(m_pVecDest == NULL) return false;
170 
171  // rename for convenience
172  TVector& v = *m_pVecDest;
173 
174  // loop interface
175  for(typename Interface::const_iterator iter = interface.begin();
176  iter != interface.end(); ++iter)
177  {
178  // get index
179  const size_t index = interface.get_element(iter);
180 
181  // write entry into vector
182  Deserialize(buff, v[index]);
183  }
184  return true;
185  }
186 
187  private:
188  // pointer to current vector
189  TVector* m_pVecDest;
190  const TVector* m_pVecSrc;
191 };
192 
194 
202 template <class TVector>
204 {
205  public:
207  ComPol_VecScaleCopy() : m_pVec(NULL), m_scale(1.0) {}
208 
210  ComPol_VecScaleCopy(TVector* pVec, number scale)
211  : m_pVec(pVec), m_scale(scale) {}
212 
214  void set_vector(TVector* pVec) {m_pVec = pVec;}
215 
217  void set_scale(number scale) {m_scale = scale;}
218 
220 
229  virtual int
231  {
233  return interface.size() * sizeof(typename TVector::value_type);
234  else
235  return -1;
236  }
237 
239 
246  virtual bool
247  collect(ug::BinaryBuffer& buff, const Interface& interface)
248  {
249  PROFILE_BEGIN_GROUP(ComPol_VecScaleCopy_collect, "algebra parallelization");
250  // check that vector has been set
251  if(m_pVec == NULL) return false;
252 
253  // rename for convenience
254  TVector& v = *m_pVec;
255 
256  // loop interface
257  for(typename Interface::const_iterator iter = interface.begin();
258  iter != interface.end(); ++iter)
259  {
260  // get index
261  const size_t index = interface.get_element(iter);
262 
263  // copy value into buffer
264  Serialize(buff, v[index]);
265  }
266  return true;
267  }
268 
270 
276  virtual bool
277  extract(ug::BinaryBuffer& buff, const Interface& interface)
278  {
279  PROFILE_BEGIN_GROUP(ComPol_VecScaleCopy_extract, "algebra parallelization");
280  // check that vector has been set
281  if(m_pVec == NULL) return false;
282 
283  // rename for convenience
284  TVector& v = *m_pVec;
285 
286  // loop interface
287  for(typename Interface::const_iterator iter = interface.begin();
288  iter != interface.end(); ++iter)
289  {
290  // get index
291  const size_t index = interface.get_element(iter);
292 
293  // copy value from buffer
294  Deserialize(buff, v[index]);
295 
296  // scale value
297  v[index] *= m_scale;
298  }
299  return true;
300  }
301 
302  private:
303  TVector* m_pVec;
304 
306 };
307 
309 
317 template <class TVector>
318 class ComPol_VecAdd : public pcl::ICommunicationPolicy<IndexLayout>
319 {
320  public:
322  ComPol_VecAdd() : m_pVecDest(NULL), m_pVecSrc(NULL) {}
323 
325  ComPol_VecAdd(TVector* pVec) : m_pVecDest(pVec), m_pVecSrc(pVec) {}
326 
328  ComPol_VecAdd(TVector* pVecDest, const TVector* pVecSrc)
329  : m_pVecDest(pVecDest), m_pVecSrc(pVecSrc) {}
330 
332  void set_vector(TVector* pVec) {m_pVecDest = pVec; m_pVecSrc = pVec;}
333 
335  void set_vector(TVector* pVecDest, const TVector* pVecSrc)
336  {m_pVecDest = pVecDest; m_pVecSrc = pVecSrc;}
337 
339 
348  virtual int
350  {
352  return interface.size() * sizeof(typename TVector::value_type);
353  else
354  return -1;
355  }
356 
358 
365  virtual bool
366  collect(ug::BinaryBuffer& buff, const Interface& interface)
367  {
368  PROFILE_BEGIN_GROUP(ComPol_VecAdd_collect, "algebra parallelization");
369  // check that vector has been set
370  if(m_pVecSrc == NULL) return false;
371 
372  // rename for convenience
373  const TVector& v = *m_pVecSrc;
374 
375  // loop interface
376  for(typename Interface::const_iterator iter = interface.begin();
377  iter != interface.end(); ++iter)
378  {
379  // get index
380  const size_t index = interface.get_element(iter);
381 
382  // copy entry
383  Serialize(buff, v[index]);
384  }
385  return true;
386  }
387 
389 
395  virtual bool
396  extract(ug::BinaryBuffer& buff, const Interface& interface)
397  {
398  PROFILE_BEGIN_GROUP(ComPol_VecAdd_extract, "algebra parallelization");
399  // check that vector has been set
400  if(m_pVecDest == NULL) return false;
401 
402  // rename for convenience
403  TVector& v = *m_pVecDest;
404 
405  // entry
406  typename TVector::value_type entry;
407 
408  // loop interface
409  for(typename Interface::const_iterator iter = interface.begin();
410  iter != interface.end(); ++iter)
411  {
412  // get index
413  const size_t index = interface.get_element(iter);
414 
415  // copy entry
416  Deserialize(buff, entry);
417 
418  // add entry
419  v[index] += entry;
420  }
421  return true;
422  }
423 
424  private:
425  TVector* m_pVecDest;
426  const TVector* m_pVecSrc;
427 };
428 
430 
438 template <class TVector>
440 {
441  public:
443  ComPol_VecScaleAdd() : m_pVec(NULL), m_scale(1.0) {}
444 
446  ComPol_VecScaleAdd(TVector* pVec) : m_pVec(pVec) {}
447 
449  void set_vector(TVector* pVec) {m_pVec = pVec;}
450 
452  void set_scale(number scale) {m_scale = scale;}
453 
455 
464  virtual int
466  {
468  return interface.size() * sizeof(typename TVector::value_type);
469  else
470  return -1;
471  }
472 
474 
481  virtual bool
482  collect(ug::BinaryBuffer& buff, const Interface& interface)
483  {
484  PROFILE_BEGIN_GROUP(ComPol_VecScaleAdd_collect, "algebra parallelization");
485  // check that vector has been set
486  if(m_pVec == NULL) return false;
487 
488  // rename for convenience
489  TVector& v = *m_pVec;
490 
491  // loop interface
492  for(typename Interface::const_iterator iter = interface.begin();
493  iter != interface.end(); ++iter)
494  {
495  // get index
496  const size_t index = interface.get_element(iter);
497 
498  // copy entry
499  Serialize(buff, v[index]);
500  }
501  return true;
502  }
503 
505 
511  virtual bool
512  extract(ug::BinaryBuffer& buff, const Interface& interface)
513  {
514  PROFILE_BEGIN_GROUP(ComPol_VecScaleAdd_extract, "algebra parallelization");
515  // check that vector has been set
516  if(m_pVec == NULL) return false;
517 
518  // rename for convenience
519  TVector& v = *m_pVec;
520 
521  // entry
522  typename TVector::value_type entry;
523 
524  // loop interface
525  for(typename Interface::const_iterator iter = interface.begin();
526  iter != interface.end(); ++iter)
527  {
528  // get index
529  const size_t index = interface.get_element(iter);
530 
531  // copy entry
532  Deserialize(buff, entry);
533 
534  // add entry
535  v[index] += entry * m_scale;
536  }
537  return true;
538  }
539 
540  private:
541  TVector* m_pVec;
542 
544 };
545 
546 
548 
562 template <class TVector>
564 {
565  public:
568 
570  ComPol_VecAddSetZero(TVector* pVec) : m_pVec(pVec) {}
571 
573  void set_vector(TVector* pVec) {m_pVec = pVec;}
574 
576 
585  virtual int
587  {
589  return interface.size() * sizeof(typename TVector::value_type);
590  else
591  return -1;
592  }
593 
596 
604  virtual bool
605  collect(ug::BinaryBuffer& buff, const Interface& interface)
606  {
607  PROFILE_BEGIN_GROUP(ComPol_VecAddSetZero_collect, "algebra parallelization");
608  // check that vector has been set
609  if(m_pVec == NULL) return false;
610 
611  // rename for convenience
612  TVector& v = *m_pVec;
613 
614  for(typename Interface::const_iterator iter = interface.begin();
615  iter != interface.end(); ++iter)
616  {
617  // get index
618  const size_t index = interface.get_element(iter);
619 
620  // copy values
621  Serialize(buff, v[index]);
622  v[index] *= 0;
623  }
624 
625  return true;
626  }
627 
629 
635  virtual bool
636  extract(ug::BinaryBuffer& buff, const Interface& interface)
637  {
638  PROFILE_BEGIN_GROUP(ComPol_VecAddSetZero_extract, "algebra parallelization");
639  // check that vector has been set
640  if(m_pVec == NULL) return false;
641 
642  // rename for convenience
643  TVector& v = *m_pVec;
644 
645  // entry
646  typename TVector::value_type entry;
647 
648  // loop interface
649  for(typename Interface::const_iterator iter = interface.begin();
650  iter != interface.end(); ++iter)
651  {
652  // get index
653  const size_t index = interface.get_element(iter);
654 
655  // copy values
656  Deserialize(buff, entry);
657 
658  // add entry
659  v[index] += entry;
660  }
661  return true;
662  }
663 
664  private:
665  TVector* m_pVec;
666 };
667 
669 
677 template <class TVector>
679 {
680  public:
683 
685  ComPol_VecSubtract(TVector* pVec) : m_pVec(pVec) {}
686 
688  void set_vector(TVector* pVec) {m_pVec = pVec;}
689 
691 
700  virtual int
702  {
704  return interface.size() * sizeof(typename TVector::value_type);
705  else
706  return -1;
707  }
708 
710 
717  virtual bool
718  collect(ug::BinaryBuffer& buff, const Interface& interface)
719  {
720  PROFILE_BEGIN_GROUP(ComPol_VecSubtract_collect, "algebra parallelization");
721  // check that vector has been set
722  if(m_pVec == NULL) return false;
723 
724  // rename for convenience
725  TVector& v = *m_pVec;
726 
727  // loop interface
728  for(typename Interface::const_iterator iter = interface.begin();
729  iter != interface.end(); ++iter)
730  {
731  // get index
732  const size_t index = interface.get_element(iter);
733 
734  // copy value
735  Serialize(buff, v[index]);
736  }
737  return true;
738  }
739 
741 
747  virtual bool
748  extract(ug::BinaryBuffer& buff, const Interface& interface)
749  {
750  PROFILE_BEGIN_GROUP(ComPol_VecSubtract_extract, "algebra parallelization");
751  // check that vector has been set
752  if(m_pVec == NULL) return false;
753 
754  // rename for convenience
755  TVector& v = *m_pVec;
756 
757  // entry
758  typename TVector::value_type entry;
759 
760  // loop interface
761  for(typename Interface::const_iterator iter = interface.begin();
762  iter != interface.end(); ++iter)
763  {
764  // get index
765  const size_t index = interface.get_element(iter);
766 
767  // copy vector
768  Deserialize(buff, entry);
769 
770  // subtract entry
771  v[index] -= entry;
772  }
773  return true;
774  }
775 
776  private:
777  TVector* m_pVec;
778 };
779 
781 template <class TVector>
783 {
784  public:
787 
789  ComPol_CheckConsistency(const TVector* pVec) : m_pVec(pVec) {}
790 
792  void set_vector(const TVector* pVec) {m_pVec = pVec;}
793 
795  virtual int
797  {
799  return interface.size() * sizeof(typename TVector::value_type);
800  else
801  return -1;
802  }
803 
805  virtual bool
806  collect(ug::BinaryBuffer& buff, const Interface& interface)
807  {
808  PROFILE_BEGIN_GROUP(ComPol_CheckConsistency_collect, "algebra parallelization");
809  // check that vector has been set
810  if(m_pVec == NULL) return false;
811 
812  // rename for convenience
813  const TVector& v = *m_pVec;
814 
815  // loop interface
816  for(typename Interface::const_iterator iter = interface.begin();
817  iter != interface.end(); ++iter)
818  {
819  // get index
820  const size_t index = interface.get_element(iter);
821 
822  // copy value
823  Serialize(buff, v[index]);
824  }
825  return true;
826  }
827 
829  virtual bool
830  extract(ug::BinaryBuffer& buff, const Interface& interface)
831  {
832  PROFILE_BEGIN_GROUP(ComPol_CheckConsistency_extract, "algebra parallelization");
833  // check that vector has been set
834  if(m_pVec == NULL) return false;
835 
836  // rename for convenience
837  const TVector& v = *m_pVec;
838 
839  // entry
840  typename TVector::value_type entry, diff;
841 
842  // loop interface
843  for(typename Interface::const_iterator iter = interface.begin();
844  iter != interface.end(); ++iter)
845  {
846  // get index
847  const size_t index = interface.get_element(iter);
848 
849  // copy vector
850  Deserialize(buff, entry);
851 
852  diff = v[index];
853  diff -= entry;
854 
855  // check
856  if(VecNormSquared(diff) > 1e-20 * VecNormSquared(entry)){
857  UG_LOG_ALL_PROCS("Slave and master value at index "<<index<<
858  " differ by: "<<VecNormSquared(diff)<<"\n");
859  }
860  }
861 
862  return true;
863  }
864 
865  private:
866  const TVector* m_pVec;
867 };
868 
870 
879 template <class TVector>
881 {
882  public:
885 
888  {
889  set_vector(pVec);
890  }
891 
893  void set_vector(TVector* pVec)
894  {
895  m_pVec = pVec;
896  m_vProcessed.resize(m_pVec->size(), false);
897  }
898 
900  void clear()
901  {
902  m_vProcessed.clear();
903  m_vProcessed.resize(m_pVec->size(), false);
904  }
905 
907 
916  virtual int
918  {
920  return interface.size() * sizeof(typename TVector::value_type);
921  else
922  return -1;
923  }
924 
926 
933  virtual bool
934  collect(ug::BinaryBuffer& buff, const Interface& interface)
935  {
936 
937  PROFILE_BEGIN_GROUP(ComPol_VecSubtractOnlyOneSlave_collect, "algebra parallelization");
938  // check that vector has been set
939  if(m_pVec == NULL) return false;
940 
941  // rename for convenience
942  TVector& v = *m_pVec;
943 
944  // loop interface
945  for(typename Interface::const_iterator iter = interface.begin();
946  iter != interface.end(); ++iter)
947  {
948  // get index
949  const size_t index = interface.get_element(iter);
950 
951  // copy value
952  Serialize(buff, v[index]);
953  }
954  return true;
955  }
956 
958 
964  virtual bool
965  extract(ug::BinaryBuffer& buff, const Interface& interface)
966  {
967  PROFILE_BEGIN_GROUP(ComPol_VecSubtractOnlyOneSlave_extract, "algebra parallelization");
968  // check that vector has been set
969  if(m_pVec == NULL) return false;
970 
971  // rename for convenience
972  TVector& v = *m_pVec;
973 
974  // entry
975  typename TVector::value_type entry;
976 
977  // loop interface
978  for(typename Interface::const_iterator iter = interface.begin();
979  iter != interface.end(); ++iter)
980  {
981  // get index
982  const size_t index = interface.get_element(iter);
983 
984  // copy vector
985  Deserialize(buff, entry);
986 
987  // subtract entry
988  if(m_vProcessed[index] == false)
989  {
990  v[index] -= entry;
991  m_vProcessed[index] = true;
992  }
993  }
994  return true;
995  }
996 
997  private:
998  TVector* m_pVec;
999  std::vector<bool> m_vProcessed;
1000 };
1001 
1002 
1003 
1005 
1015 template <class TAlgebra>
1017  : public pcl::ICommunicationPolicy<IndexLayout>
1018 {
1019  public:
1020 
1021  typedef typename TAlgebra::matrix_type TMatrix;
1022  typedef typename TAlgebra::vector_type TVector;
1023 
1025 
1028  ComPol_MatDistributeDiag(TMatrix& rMat, TVector &rWeight, double theta=1.0)
1029  : m_rMat(rMat), m_rWeight(rWeight), m_dTheta(theta)
1030  {
1031  UG_ASSERT( m_rMat.num_rows()==m_rWeight.size(), "sizes should match");
1032  }
1033 
1035 
1044  virtual int
1046  {
1048  return interface.size() * sizeof(typename TMatrix::value_type);
1049  else
1050  return -1;
1051  }
1052 
1053 
1055  virtual bool collect(ug::BinaryBuffer& buff, const Interface& interface)
1056  {
1057  PROFILE_BEGIN_GROUP(CMatDistributeDiag_collect, "algebra parallelization");
1058  typedef typename TMatrix::value_type block_type;
1059 
1060  // loop interface
1061  for(typename Interface::const_iterator iter = interface.begin();
1062  iter != interface.end(); ++iter)
1063  {
1064  // get index
1065  const size_t index = interface.get_element(iter);
1066  block_type& a_ii = m_rMat(index, index);
1067 
1068  // send rescaled diagonal entry
1069  block_type out_ii = a_ii;
1070  out_ii = out_ii * (1.0/BlockRef(m_rWeight[index], 0));
1071  // write diagonal entry to stream
1072  Serialize(buff, out_ii);
1073 
1074  // std::cerr << "Sending(" << index << ")=" << out_ii << " "<< BlockRef(m_rWeight[index], 0) << std::endl;
1075  // store average
1076  a_ii = out_ii;
1077 
1078  // after the first call, we need to reset the weight (on master)
1079  // in order to communicate correct entries in subsequent if calls
1080  m_rWeight[index] = 1.0;
1081 
1082  }
1083 
1085  return true;
1086  }
1087 
1088  virtual bool
1090  { return true; }
1091 
1093  virtual bool extract(ug::BinaryBuffer& buff, const Interface& interface)
1094  {
1095  PROFILE_BEGIN_GROUP(MatDistributeDiag_extract, "algebra parallelization");
1096  // block type of associated matrix
1097  typedef typename TMatrix::value_type block_type;
1098 
1099  block_type in_ii;
1100 
1101  // loop interface
1102  for(typename Interface::const_iterator iter = interface.begin();
1103  iter != interface.end(); ++iter)
1104  {
1105  // get index
1106  const size_t index = interface.get_element(iter);
1107 
1108  // read incoming diagonal
1109  Deserialize(buff, in_ii);
1110 
1111  // set matrix entry
1112  // std::cerr << "Recv'd(" << index << ")=" << in_ii << std::endl;
1113  m_rMat(index, index) += in_ii;
1114  }
1115 
1117  return true;
1118  }
1119 
1120  private:
1121  // pointer to current vector
1124  double m_dTheta;
1125 
1126 
1127 };
1128 
1130 
1140 template <class TMatrix>
1142  : public pcl::ICommunicationPolicy<IndexLayout>
1143 {
1144  public:
1146 
1149  ComPol_MatAddRowsOverlap0(TMatrix& rMat, AlgebraIDVec& vGlobalID)
1150  : m_rMat(rMat), m_vGlobalID(vGlobalID)
1151  {
1152  UG_ASSERT(vGlobalID.size() >= m_rMat.num_rows(), "too few Global ids");
1153  }
1154 
1156  virtual bool collect(ug::BinaryBuffer& buff, const Interface& interface)
1157  {
1158  PROFILE_BEGIN_GROUP(ComPol_MatAddRowsOverlap0_collect, "algebra parallelization");
1159  typedef typename TMatrix::row_iterator row_iterator;
1160  typedef typename TMatrix::value_type block_type;
1161 
1162  // loop interface
1163  for(typename Interface::const_iterator iter = interface.begin();
1164  iter != interface.end(); ++iter)
1165  {
1166  // get index
1167  const size_t index = interface.get_element(iter);
1168 
1169  // count number of row entries
1170  const row_iterator rowEnd = m_rMat.end_row(index);
1171  size_t numRowEntry = 0;
1172  for(row_iterator it_k = m_rMat.begin_row(index); it_k != rowEnd; ++it_k)
1173  numRowEntry++;
1174 
1175  // write number of row entries to stream
1176  Serialize(buff, numRowEntry);
1177 
1178  // write entries and global id to stream
1179  for(row_iterator it_k = m_rMat.begin_row(index); it_k != rowEnd; ++it_k)
1180  {
1181  const size_t k = it_k.index();
1182  block_type& a_ik = it_k.value();
1183 
1184  // write global entry to buffer
1185  Serialize(buff, m_vGlobalID[k]);
1186 
1187  // write entry into buffer
1188  Serialize(buff, a_ik);
1189  }
1190  }
1191 
1193  return true;
1194  }
1195 
1196  virtual bool
1198  {
1199  // fill the map global->local
1201  return true;
1202  }
1203 
1205  virtual bool extract(ug::BinaryBuffer& buff, const Interface& interface)
1206  {
1207  PROFILE_BEGIN_GROUP(ComPol_MatAddRowsOverlap0_extract, "algebra parallelization");
1208  // block type of associated matrix
1209  typedef typename TMatrix::value_type block_type;
1210 
1211  // we'll read global ids into this variable
1212  AlgebraID gID;
1213 
1214  // we'll read blocks into this var
1215  block_type block;
1216 
1217  // loop interface
1218  for(typename Interface::const_iterator iter = interface.begin();
1219  iter != interface.end(); ++iter)
1220  {
1221  // get index
1222  const size_t index = interface.get_element(iter);
1223 
1224  // read the number of connections
1225  size_t numConnections = 0;
1226  Deserialize(buff, numConnections);
1227 
1228  // read each connection
1229  for(size_t i_conn = 0; i_conn < numConnections; ++i_conn){
1230  Deserialize(buff, gID);
1231  Deserialize(buff, block);
1232 
1233  // if gID exists on this process, then add the connection to
1234  // the matrix.
1235  size_t conInd;
1236  if(m_algIDHash.get_entry(conInd, gID)){
1237  // add connection between index and conInd to matrix
1238  m_rMat(index, conInd) += block;
1239  }
1240  }
1241  }
1242 
1244  return true;
1245  }
1246 
1247  private:
1248  // pointer to current vector
1249  TMatrix& m_rMat;
1250 
1251  // map localID->globalID
1253 
1254  // map globalID->localID
1256 
1257 };
1258 
1259 
1261 
1270 template <class TMatrix>
1272  : public pcl::ICommunicationPolicy<IndexLayout>
1273 {
1274  public:
1276 
1279  ComPol_MatCopyRowsOverlap0(TMatrix& rMat, AlgebraIDVec& vGlobalID)
1280  : m_rMat(rMat), m_vGlobalID(vGlobalID)
1281  {
1282  UG_ASSERT(vGlobalID.size() >= m_rMat.num_rows(), "too few Global ids");
1283  }
1284 
1286  virtual bool collect(ug::BinaryBuffer& buff, const Interface& interface)
1287  {
1288  PROFILE_BEGIN_GROUP(ComPol_MatAddRowsOverlap0_collect, "algebra parallelization");
1289  typedef typename TMatrix::row_iterator row_iterator;
1290  typedef typename TMatrix::value_type block_type;
1291 
1292  // loop interface
1293  for(typename Interface::const_iterator iter = interface.begin();
1294  iter != interface.end(); ++iter)
1295  {
1296  // get index
1297  const size_t index = interface.get_element(iter);
1298 
1299  // count number of row entries
1300  const row_iterator rowEnd = m_rMat.end_row(index);
1301  size_t numRowEntry = 0;
1302  for(row_iterator it_k = m_rMat.begin_row(index); it_k != rowEnd; ++it_k)
1303  numRowEntry++;
1304 
1305  // write number of row entries to stream
1306  Serialize(buff, numRowEntry);
1307 
1308  // write entries and global id to stream
1309  for(row_iterator it_k = m_rMat.begin_row(index); it_k != rowEnd; ++it_k)
1310  {
1311  const size_t k = it_k.index();
1312  block_type& a_ik = it_k.value();
1313 
1314  // write global entry to buffer
1315  Serialize(buff, m_vGlobalID[k]);
1316 
1317  // write entry into buffer
1318  Serialize(buff, a_ik);
1319  }
1320  }
1321 
1323  return true;
1324  }
1325 
1326  virtual bool
1328  {
1329  // fill the map global->local
1331  return true;
1332  }
1333 
1335  virtual bool extract(ug::BinaryBuffer& buff, const Interface& interface)
1336  {
1337  PROFILE_BEGIN_GROUP(ComPol_MatAddRowsOverlap0_extract, "algebra parallelization");
1338  // block type of associated matrix
1339  typedef typename TMatrix::value_type block_type;
1340 
1341  // we'll read global ids into this variable
1342  AlgebraID gID;
1343 
1344  // we'll read blocks into this var
1345  block_type block;
1346 
1347  // loop interface
1348  for(typename Interface::const_iterator iter = interface.begin();
1349  iter != interface.end(); ++iter)
1350  {
1351  // get index
1352  const size_t index = interface.get_element(iter);
1353 
1354  // read the number of connections
1355  size_t numConnections = 0;
1356  Deserialize(buff, numConnections);
1357 
1358  // read each connection
1359  for(size_t i_conn = 0; i_conn < numConnections; ++i_conn){
1360  Deserialize(buff, gID);
1361  Deserialize(buff, block);
1362 
1363  // if gID exists on this process, then set the connection to
1364  // the new value.
1365  size_t conInd;
1366  if(m_algIDHash.get_entry(conInd, gID)){
1367  m_rMat(index, conInd) = block;
1368  }
1369  }
1370  }
1371 
1373  return true;
1374  }
1375 
1376  private:
1377  // pointer to current vector
1378  TMatrix& m_rMat;
1379 
1380  // map localID->globalID
1382 
1383  // map globalID->localID
1385 
1386 };
1387 
1388 
1390 
1393 template <class TMatrix>
1395  : public pcl::ICommunicationPolicy<IndexLayout>
1396 {
1397  public:
1400  : m_rMat(rMat)
1401  {}
1402 
1404  virtual bool collect(ug::BinaryBuffer& buff, const Interface& interface)
1405  {
1406  PROFILE_BEGIN_GROUP(ComPol_MatAddInnerInterfaceCouplings_collect, "algebra parallelization");
1407  typedef typename TMatrix::row_iterator row_iterator;
1408  typedef typename TMatrix::value_type block_type;
1409 
1410  // build map
1411  Hash<size_t, size_t> hash((size_t)(interface.size() * 1.1));
1412  size_t interfaceIndex = 0;
1413 
1414  for(typename Interface::const_iterator iter = interface.begin();
1415  iter != interface.end(); ++iter, ++interfaceIndex){
1416  hash.insert(interface.get_element(iter), interfaceIndex);
1417  }
1418 
1419  // loop interface
1420  for(typename Interface::const_iterator iter = interface.begin();
1421  iter != interface.end(); ++iter)
1422  {
1423  // get index
1424  const size_t index = interface.get_element(iter);
1425 
1426  // count number of row entries
1427  const row_iterator rowEnd = m_rMat.end_row(index);
1428  size_t numRowEntry = 0;
1429  for(row_iterator it_k = m_rMat.begin_row(index); it_k != rowEnd; ++it_k){
1430  if(!hash.has_entry(it_k.index())) continue;
1431  numRowEntry++;
1432  }
1433 
1434  // write number of row entries to stream
1435  Serialize(buff, numRowEntry);
1436 
1437  // write entries and global id to stream
1438  for(row_iterator it_k = m_rMat.begin_row(index); it_k != rowEnd; ++it_k)
1439  {
1440  const size_t target = it_k.index();
1441  if(!hash.get_entry(interfaceIndex, target)) continue;
1442  block_type& a_ik = it_k.value();
1443 
1444  // write global entry to buffer
1445  Serialize(buff, interfaceIndex);
1446 
1447  // write entry into buffer
1448  Serialize(buff, a_ik);
1449 
1450  // set entry to zero
1451  a_ik *= 0;
1452  }
1453  }
1454 
1455  return true;
1456  }
1457 
1459  virtual bool extract(ug::BinaryBuffer& buff, const Interface& interface)
1460  {
1461  PROFILE_BEGIN_GROUP(ComPol_MatAddRowsOverlap0_extract, "algebra parallelization");
1462  // block type of associated matrix
1463  typedef typename TMatrix::value_type block_type;
1464 
1465  // build map
1466  std::vector<size_t> vMapInterfaceToGlob(interface.size());
1467  int k = 0;
1468  for(typename Interface::const_iterator iter = interface.begin();
1469  iter != interface.end(); ++iter, ++k){
1470  vMapInterfaceToGlob[k] = interface.get_element(iter);
1471  }
1472 
1473  // we'll read blocks into this var
1474  block_type block;
1475  size_t locID;
1476 
1477  // loop interface
1478  for(typename Interface::const_iterator iter = interface.begin();
1479  iter != interface.end(); ++iter)
1480  {
1481  // get index
1482  const size_t index = interface.get_element(iter);
1483 
1484  // read the number of connections
1485  size_t numConnections = 0;
1486  Deserialize(buff, numConnections);
1487 
1488  // read each connection
1489  for(size_t i_conn = 0; i_conn < numConnections; ++i_conn){
1490  Deserialize(buff, locID);
1491  Deserialize(buff, block);
1492 
1493  UG_ASSERT(locID < vMapInterfaceToGlob.size(), "Invalid"
1494  " id: "<<locID<<", size: "<<vMapInterfaceToGlob.size());
1495  m_rMat(index, vMapInterfaceToGlob[locID]) += block;
1496  }
1497  }
1498 
1500  return true;
1501  }
1502 
1503  private:
1504  // pointer to current vector
1505  TMatrix& m_rMat;
1506 };
1507 
1508 
1509 
1511 
1512 }// end of namespace
1513 
1514 
1515 #endif /* __H__LIB_ALGEBRA__PARALLELIZATION__COMMUNICATION_POLICIES__ */
specializations are responsible to pack and unpack interface data during communication.
Definition: pcl_communication_structs.h:790
You may add elements to this interface and iterate over them.
Definition: pcl_communication_structs.h:207
ElemContainer::const_iterator const_iterator
Definition: pcl_communication_structs.h:237
iterator end()
Definition: pcl_communication_structs.h:293
iterator begin()
Definition: pcl_communication_structs.h:292
Element & get_element(iterator iter)
Definition: pcl_communication_structs.h:298
size_t size() const
returns the number of elements that are stored in the interface.
Definition: pcl_communication_structs.h:306
A Buffer for binary data.
Definition: binary_buffer.h:56
Communication Policy to check consistency of a vector.
Definition: communication_policies.h:783
const TVector * m_pVec
Definition: communication_policies.h:866
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:806
ComPol_CheckConsistency()
Default constructor.
Definition: communication_policies.h:786
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
checks if recieved values are equal to process local values
Definition: communication_policies.h:830
void set_vector(const TVector *pVec)
sets the vector used in communication
Definition: communication_policies.h:792
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:796
ComPol_CheckConsistency(const TVector *pVec)
Constructor setting the values.
Definition: communication_policies.h:789
Communication Policy to copy slave couplings to master row.
Definition: communication_policies.h:1143
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:1156
TMatrix & m_rMat
Definition: communication_policies.h:1249
virtual bool begin_layout_extraction(const Layout *pLayout)
signals the beginning of a layout extraction.
Definition: communication_policies.h:1197
ComPol_MatAddRowsOverlap0(TMatrix &rMat, AlgebraIDVec &vGlobalID)
Constructor setting the vector.
Definition: communication_policies.h:1149
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
writes values from a buffer into the interface
Definition: communication_policies.h:1205
AlgebraIDVec & m_vGlobalID
Definition: communication_policies.h:1252
AlgebraIDHashList m_algIDHash
Definition: communication_policies.h:1255
Comm-Pol to add matrix rows of inner-interface couplings.
Definition: communication_policies.h:1396
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:1404
TMatrix & m_rMat
Definition: communication_policies.h:1505
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
writes values from a buffer into the interface
Definition: communication_policies.h:1459
ComPol_MatAddSetZeroInnerInterfaceCouplings(TMatrix &rMat)
Constructor setting the matrix.
Definition: communication_policies.h:1399
Communication Policy to copy couplings between interfaces.
Definition: communication_policies.h:1273
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
writes values from a buffer into the interface
Definition: communication_policies.h:1335
virtual bool begin_layout_extraction(const Layout *pLayout)
signals the beginning of a layout extraction.
Definition: communication_policies.h:1327
AlgebraIDHashList m_algIDHash
Definition: communication_policies.h:1384
AlgebraIDVec & m_vGlobalID
Definition: communication_policies.h:1381
TMatrix & m_rMat
Definition: communication_policies.h:1378
ComPol_MatCopyRowsOverlap0(TMatrix &rMat, AlgebraIDVec &vGlobalID)
Constructor setting the vector.
Definition: communication_policies.h:1279
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:1286
Communication Policy sending fractions to.
Definition: communication_policies.h:1018
virtual bool begin_layout_extraction(const Layout *pLayout)
signals the beginning of a layout extraction.
Definition: communication_policies.h:1089
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:1045
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
writes values from a buffer into the interface
Definition: communication_policies.h:1093
TAlgebra::vector_type TVector
Definition: communication_policies.h:1022
TMatrix & m_rMat
Definition: communication_policies.h:1122
ComPol_MatDistributeDiag(TMatrix &rMat, TVector &rWeight, double theta=1.0)
Constructor setting the vector.
Definition: communication_policies.h:1028
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:1055
TAlgebra::matrix_type TMatrix
Definition: communication_policies.h:1021
TVector & m_rWeight
Definition: communication_policies.h:1123
double m_dTheta
Definition: communication_policies.h:1124
Communication Policy to add values of a vector.
Definition: communication_policies.h:319
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
adds values of a buffer to the interface values
Definition: communication_policies.h:396
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:349
TVector * m_pVecDest
Definition: communication_policies.h:425
ComPol_VecAdd(TVector *pVec)
Constructor setting the values.
Definition: communication_policies.h:325
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:366
ComPol_VecAdd()
Default constructor.
Definition: communication_policies.h:322
ComPol_VecAdd(TVector *pVecDest, const TVector *pVecSrc)
Constructor setting the values.
Definition: communication_policies.h:328
void set_vector(TVector *pVecDest, const TVector *pVecSrc)
sets the vector used in communication
Definition: communication_policies.h:335
const TVector * m_pVecSrc
Definition: communication_policies.h:426
void set_vector(TVector *pVec)
sets the vector used in communication
Definition: communication_policies.h:332
Communication Policy to add values of a vector and reset value to zero on sending interface.
Definition: communication_policies.h:564
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
Definition: communication_policies.h:605
ComPol_VecAddSetZero()
Default Constructor.
Definition: communication_policies.h:567
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
adds the values of a buffer to the values on the interface
Definition: communication_policies.h:636
ComPol_VecAddSetZero(TVector *pVec)
Constructor setting vector.
Definition: communication_policies.h:570
TVector * m_pVec
Definition: communication_policies.h:665
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:586
void set_vector(TVector *pVec)
sets the vector that we be used for communication
Definition: communication_policies.h:573
Communication Policy to copy values of a vector.
Definition: communication_policies.h:88
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
writes values from a buffer into the interface
Definition: communication_policies.h:165
ComPol_VecCopy(TVector *pVec)
Constructor setting the vector.
Definition: communication_policies.h:94
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:135
TVector * m_pVecDest
Definition: communication_policies.h:189
ComPol_VecCopy()
Default constructor.
Definition: communication_policies.h:91
void set_vector(TVector *pVec)
set the vector to work on
Definition: communication_policies.h:101
void set_vector(TVector *pVecDest, const TVector *pVecSrc)
set the vector to work on
Definition: communication_policies.h:104
ComPol_VecCopy(TVector *pVecDest, const TVector *pVecSrc)
Constructor setting the vector.
Definition: communication_policies.h:97
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:118
const TVector * m_pVecSrc
Definition: communication_policies.h:190
Communication Policy to add values of a vector.
Definition: communication_policies.h:440
TVector * m_pVec
Definition: communication_policies.h:541
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:482
ComPol_VecScaleAdd(TVector *pVec)
Constructor setting the values.
Definition: communication_policies.h:446
void set_vector(TVector *pVec)
sets the vector used in communication
Definition: communication_policies.h:449
number m_scale
Definition: communication_policies.h:543
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:465
ComPol_VecScaleAdd()
Default constructor.
Definition: communication_policies.h:443
void set_scale(number scale)
sets the scaling factor
Definition: communication_policies.h:452
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
scales and adds values of a buffer to the interface values
Definition: communication_policies.h:512
Communication Policy to copy scaled values of a vector.
Definition: communication_policies.h:204
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:230
TVector * m_pVec
Definition: communication_policies.h:303
void set_scale(number scale)
sets the scaling factor
Definition: communication_policies.h:217
ComPol_VecScaleCopy()
Default Constructor.
Definition: communication_policies.h:207
ComPol_VecScaleCopy(TVector *pVec, number scale)
Constructor setting vector and scaling factor.
Definition: communication_policies.h:210
void set_vector(TVector *pVec)
sets the vector that we be used for communication
Definition: communication_policies.h:214
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
scales values of a buffer and writes the into the interface
Definition: communication_policies.h:277
number m_scale
Definition: communication_policies.h:305
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:247
Communication Policy to subtract values of a vector.
Definition: communication_policies.h:679
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:701
ComPol_VecSubtract(TVector *pVec)
Constructor setting the values.
Definition: communication_policies.h:685
TVector * m_pVec
Definition: communication_policies.h:777
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:718
void set_vector(TVector *pVec)
sets the vector used in communication
Definition: communication_policies.h:688
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
subtracts values of a buffer to the interface values
Definition: communication_policies.h:748
ComPol_VecSubtract()
Default constructor.
Definition: communication_policies.h:682
Communication Policy to subtract only one slave value per master of a vector.
Definition: communication_policies.h:881
virtual int get_required_buffer_size(const Interface &interface)
returns the buffer size
Definition: communication_policies.h:917
virtual bool collect(ug::BinaryBuffer &buff, const Interface &interface)
writes the interface values into a buffer that will be sent
Definition: communication_policies.h:934
ComPol_VecSubtractOnlyOneSlave(TVector *pVec)
Constructor setting the values.
Definition: communication_policies.h:887
void set_vector(TVector *pVec)
sets the vector used in communication
Definition: communication_policies.h:893
ComPol_VecSubtractOnlyOneSlave()
Default constructor.
Definition: communication_policies.h:884
std::vector< bool > m_vProcessed
Definition: communication_policies.h:999
TVector * m_pVec
Definition: communication_policies.h:998
void clear()
clear processed flag
Definition: communication_policies.h:900
virtual bool extract(ug::BinaryBuffer &buff, const Interface &interface)
subtracts values of a buffer to the interface values
Definition: communication_policies.h:965
void insert(const key_t &key, const value_t &val)
Definition: hash_impl.hpp:199
value_t & get_entry(const key_t &key)
Definition: hash_impl.hpp:158
bool has_entry(const key_t &key) const
Definition: hash_impl.hpp:150
#define UG_LOG_ALL_PROCS(msg)
Definition: log.h:371
#define UG_ASSERT(expr, msg)
Definition: assert.h:70
double number
Definition: types.h:124
size_t target(SM_edge< typename T::value_type > const &e, ug::BidirectionalMatrix< T > const &m)
Definition: bidirectional_boost.h:100
the ug namespace
double & BlockRef(T &vec, size_t i)
Definition: blocks.h:66
void Deserialize(TIStream &buf, ParallelVector< T > &v)
Deerialize for ParallelVector<T>
Definition: restart_bridge.cpp:112
std::vector< AlgebraID > AlgebraIDVec
Definition: algebra_id.h:63
void GenerateAlgebraIDHashList(AlgebraIDHashList &hash, AlgebraIDVec &algebraIDs)
Creates a hash which allows a algebraID->localIndex mapping.
Definition: algebra_id.h:70
void Serialize(TOStream &buf, const ParallelVector< T > &v)
Serialize for ParallelVector<T>
Definition: restart_bridge.cpp:103
double VecNormSquared(const double &a)
returns norm_2^2(a)
Definition: operations_vec.h:100
#define PROFILE_BEGIN_GROUP(name, groups)
Definition: profiler.h:255
T value_type
Definition: sparsematrix_interface.h:2
this type is used to identify distributed objects.
Definition: algebra_id.h:46
Definition: communication_policies.h:58
@ is_static
Definition: communication_policies.h:60