angel  mercurial changeset: <a href="http://mercurial.mcs.anl.gov/ad/angel/rev/b39b3ee16224" target="_parent">b39b3ee16224</a>
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
eliminations.hpp
Go to the documentation of this file.
1 /*
2 #############################################################
3 # This file is part of angel released under the BSD license #
4 # The full COPYRIGHT notice can be found in the top #
5 # level directory of the angel distribution #
6 #############################################################
7 */
8 
9 #ifndef _eliminations_include_
10 #define _eliminations_include_
11 
12 #include "angel_types.hpp"
13 #include "angel_io.hpp"
14 
15 #ifdef USEXAIFBOOSTER
16  #include "xaifBooster/algorithms/CrossCountryInterface/inc/AwarenessLevel.hpp"
17  using std::list;
18 #endif
19 
20  namespace angel {
21 
22  using std::vector;
23  using std::cout;
24  using boost::tie;
25 
26 // =========================================================================
27 // eliminations in c-graph
28 // =========================================================================
29 
30 // -------------------------------------------------------------------------
31 // elimination of a single vertex in compute graph
32 // -------------------------------------------------------------------------
33 
38 int vertex_elimination (c_graph_t::vertex_t v, c_graph_t& cg);
39 
44 inline int vertex_elimination (int j, c_graph_t& cg) {
45  return vertex_elimination (vertex (j, cg), cg); }
46 
47 // -------------------------------------------------------------------------
48 // elimination of a single edge in compute graph
49 // -------------------------------------------------------------------------
50 
56  c_graph_t& cg);
57 
64  c_graph_t& cg) {
65  bool found_ij;
66  c_graph_t::edge_t edge_ij;
67  tie (edge_ij, found_ij)= edge (i, j, cg);
68  return found_ij ? front_edge_elimination (edge_ij, cg) : 0;
69 }
70 
76 inline int front_edge_elimination (int i, int j, c_graph_t& cg) {
77  return front_edge_elimination (vertex (i, cg), vertex (j, cg), cg); }
78 
79 
85  c_graph_t& cg);
86 
92  c_graph_t& cg) {
93  bool found_ij;
94  c_graph_t::edge_t edge_ij;
95  tie (edge_ij, found_ij)= edge (i, j, cg);
96  return found_ij ? back_edge_elimination (edge_ij, cg) : 0;
97 }
98 
103 inline int back_edge_elimination (int i, int j, c_graph_t& cg) {
104  return back_edge_elimination (vertex (i, cg), vertex (j, cg), cg); }
105 
109 inline int edge_elimination (c_graph_t::edge_t e, bool front,
110  c_graph_t& cg) {
111  return front ? front_edge_elimination (e, cg)
112  : back_edge_elimination (e, cg);
113 }
114 
119  return e.second ? front_edge_elimination (e.first, cg)
120  : back_edge_elimination (e.first, cg);
121 }
122 
129  bool front, c_graph_t& cg) {
130  return front ? front_edge_elimination (i, j, cg)
131  : back_edge_elimination (i, j, cg);
132 }
133 
138 inline int edge_elimination (int i, int j, bool front, c_graph_t& cg) {
139  return front ? front_edge_elimination (i, j, cg)
140  : back_edge_elimination (i, j, cg);
141 }
142 
145  return edge_elimination (ee.i, ee.j, ee.front, cg); }
146 
147 // -------------------------------------------------------------------------
148 // elimination sequences of vertices in compute graph
149 // -------------------------------------------------------------------------
150 
155 int vertex_elimination (const vector<int>& seq, c_graph_t& cg);
156 
157 
161 inline int edge_elimination (const edge_elim_seq_t& seq, c_graph_t& cg) {
162  int costs= 0;
163  for (size_t i= 0; i < seq.size(); i++)
164  costs+= edge_elimination (seq[i].edge, seq[i].front, cg);
165  return costs;
166 }
167 
171 inline int edge_elimination (const edge_pair_elim_seq_t& seq, c_graph_t& cg) {
172  int costs= 0;
173  for (size_t k= 0; k < seq.size(); k++)
174  costs+= edge_elimination (seq[k].i, seq[k].j, seq[k].front, cg);
175  return costs;
176 }
177 
181 inline int edge_elimination (const edge_ij_elim_seq_t& seq, c_graph_t& cg) {
182  int costs= 0;
183  for (size_t k= 0; k < seq.size(); k++)
184  costs+= edge_elimination (seq[k].i, seq[k].j, seq[k].front, cg);
185  return costs;
186 }
187 
188 // =========================================================================
189 // eliminations in line graph
190 // =========================================================================
191 
192 // -------------------------------------------------------------------------
193 // elimination of a single vertex in line graph
194 // -------------------------------------------------------------------------
195 
201 int vertex_elimination (int j, line_graph_t& lg);
202 
203 // -------------------------------------------------------------------------
204 // elimination of a single edge in line graph
205 // -------------------------------------------------------------------------
206 
212 int front_edge_elimination (int i, int j, line_graph_t& lg);
213 
219 int front_edge_elimination (line_graph_t::edge_t vij, line_graph_t& lg);
220 
226 int back_edge_elimination (int i, int j, line_graph_t& lg);
227 
233 int back_edge_elimination (line_graph_t::edge_t vij, line_graph_t& lg);
234 
237 inline int edge_elimination (int i, int j, bool front, line_graph_t& lg) {
238  return front ? front_edge_elimination (i, j, lg)
239  : back_edge_elimination (i, j, lg);
240 }
241 
244  bool front, line_graph_t& lg) {
245  return front ? front_edge_elimination (vij, lg)
246  : back_edge_elimination (vij, lg);
247 }
248 
249 // -------------------------------------------------------------------------
250 // elimination sequences of edges in line graph
251 // -------------------------------------------------------------------------
252 
253 // additional data structures
254 // --------------------------
255 
260  bool front;
261 };
262 
264 typedef vector<edge_vertex_elim_t> edge_vertex_elim_seq_t;
265 
268  int costs= 0;
269  for (size_t k= 0; k < seq.size(); k++)
270  costs+= edge_elimination (seq[k].edge, seq[k].front, lg);
271  return costs;
272 }
273 
274 // -------------------------------------------------------------------------
275 // elimination of a single face in line graph
276 // -------------------------------------------------------------------------
277 
291 int face_elimination (line_graph_t::face_t f, int kr, line_graph_t& lg, accu_graph_t& ac);
292 
306  accu_graph_t dummy (*lg.cgp, lg);
307  return face_elimination (f, kr, lg, dummy); }
308 
313  return face_elimination (f, -1, lg); }
314 
321 inline int face_elimination (int i, int j, line_graph_t& lg) {
322  line_graph_t::face_t f; bool found;
323  tie (f, found)= edge (i, j, lg);
324  return found ? face_elimination (f, lg) : -1;
325 }
326 
340 inline int face_elimination (int i, int j, int kr, line_graph_t& lg) {
341  line_graph_t::face_t f; bool found;
342  tie (f, found)= edge (i, j, lg);
343  return found ? face_elimination (f, kr, lg) : -1;
344 }
345 
346 inline int face_elimination (int i, int j, int kr, line_graph_t& lg, accu_graph_t& ac) {
347  line_graph_t::face_t f; bool found;
348  tie (f, found)= edge (i, j, lg);
349  return found ? face_elimination (f, kr, lg, ac) : -1;
350 }
351 
362  int k= face_elimination (t.i, t.j, t.k, lg);
363  if (k != -1) t.k= k; return k != -1;
364 }
365 
379  int k= face_elimination (t.i, t.j, t.k, lg, ac);
380  if (k != -1) t.k= k; return k != -1;
381 }
382 
393 inline int was_non_trivial_elimination (int i, int j, int k, const line_graph_t& lg) {
394  line_graph_t::const_ed_t el= get(boost::vertex_degree, lg); // edge label
395  return ((k != -1 && el[i] != 1 && el[j] != 1) || k < lg.v()-1); // absorption -> a+= b in accumulation
396 }
397 
408  line_graph_t::edge_t i= source (f, lg), j= target (f, lg);
409  return was_non_trivial_elimination (i, j, k, lg);
410 }
411 
412 // -------------------------------------------------------------------------
413 // elimination sequences of faces
414 // -------------------------------------------------------------------------
415 
416 
417 // =========================================================================
418 // overloaded elimination functions for templates
419 // =========================================================================
420 
423  return vertex_elimination (v, cg); }
424 
426 inline int eliminate (int j, c_graph_t& cg) {
427  return vertex_elimination (j, cg); }
428 
430 inline int eliminate (int j, line_graph_t& lg) {
431  return vertex_elimination (j, lg); }
432 
434 inline int eliminate (edge_bool_t e, c_graph_t& cg) {
435  return edge_elimination (e, cg);
436 }
437 
439 inline int eliminate (edge_ij_elim_t ee, c_graph_t& cg) {
440  return edge_elimination (ee, cg);
441 }
442 
449  int k= face_elimination (f, -1, lg);
450  return was_non_trivial_elimination (f, k, lg);
451 }
452 
460 inline int eliminate (triplet_t& t, line_graph_t& lg) {
461  return face_elimination (t, lg);
462 }
463 
464 // =========================================================================
465 // which vertices, edges or faces can be eliminated
466 // =========================================================================
467 
469 int eliminatable_vertices (const c_graph_t& cg,
470  vector<c_graph_t::vertex_t>& vv);
471 
476 int semi_eliminatable_vertices (const c_graph_t& cg, vector<c_graph_t::vertex_t>& vv);
477 
482 int eliminatable_edges (const c_graph_t& cg,
483  vector<c_graph_t::edge_t>& ev);
484 
486 int front_eliminatable_edges (const c_graph_t& cg,
487  vector<c_graph_t::edge_t>& ev);
488 
490 int back_eliminatable_edges (const c_graph_t& cg,
491  vector<c_graph_t::edge_t>& ev);
492 
499 int eliminatable_edges (const c_graph_t& cg,
500  vector<edge_bool_t>& ev);
501 
508 int eliminatable_edges (const c_graph_t& cg,
509  vector<edge_ij_elim_t>& ev);
510 
512 unsigned int eliminatable_edges(const c_graph_t& cg,
513  vector<EdgeElim>& ev);
514 
516 int eliminatable_faces (const line_graph_t& lg,
517  vector<line_graph_t::face_t>& fv);
518 
525 inline int eliminatable_triplets (const line_graph_t& lg, vector<triplet_t>& tv) {
526  vector<line_graph_t::face_t> fv;
527  eliminatable_faces (lg, fv);
528  tv.resize (0);
529  for (size_t c= 0; c < fv.size(); c++) {
530  int s= source (fv[c], lg), t= target (fv[c], lg);
531  triplet_t tr (s, t, -1); tv.push_back (tr); }
532  return tv.size();
533 }
534 
535 // =========================================================================
536 // which vertices, edges or faces can be eliminated (overloaded versions)
537 // =========================================================================
538 
540 inline int eliminatable_objects (const c_graph_t& cg,
541  vector<c_graph_t::vertex_t>& vv) {
542  return eliminatable_vertices (cg, vv);
543 }
544 
546 inline int eliminatable_objects (const c_graph_t& cg,
547  vector<c_graph_t::edge_t>& ev) {
548  return eliminatable_edges (cg, ev);
549 }
550 
552 inline int eliminatable_objects (const c_graph_t& cg,
553  vector<edge_bool_t>& ev) {
554  return eliminatable_edges (cg, ev);
555 }
556 
558 inline int eliminatable_objects (const c_graph_t& cg,
559  vector<edge_ij_elim_t>& ev) {
560  return eliminatable_edges (cg, ev);
561 }
562 
564 inline int eliminatable_objects (const line_graph_t& lg,
565  vector<line_graph_t::face_t>& fv) {
566  return eliminatable_faces (lg, fv);
567 }
568 
570 inline int eliminatable_objects (const line_graph_t& lg,
571  vector<triplet_t>& tv) {
572  return eliminatable_triplets (lg, tv);
573 }
574 
575 
576 
585 template <typename Ad_graph_t,
586  typename El_spec_t>
588 private:
589 
590 public:
591  const Ad_graph_t og;
592  vector<El_spec_t> seq;
593  Ad_graph_t cg;
594  int ccosts;
595 
596  typedef Ad_graph_t ad_graph_t;
597  typedef El_spec_t el_spec_t;
598 
604 
608  elimination_history_t (const Ad_graph_t& _cg) :
609  og (_cg), cg (_cg), ccosts (0) {
610  THROW_EXCEPT_MACRO (!check (), consistency_exception, "Inconsistent input graph");}
611 
619  elimination_history_t (const Ad_graph_t& _og, const vector<El_spec_t>& _seq,
620  const Ad_graph_t& _cg, int _ccosts= 0) :
621  og (_og), seq (_seq), cg (_cg), ccosts (_ccosts) {
622  THROW_EXCEPT_MACRO (!check (), consistency_exception, "Inconsistent input graphs");}
623 
630  elimination_history_t (const Ad_graph_t& _og, const vector<El_spec_t>& _seq) :
631  og (_og), seq (_seq) {
632  THROW_EXCEPT_MACRO (!og.check (), consistency_exception, "Inconsistent input graph");
633  bool seq_ok= rebuild_graph ();
634  THROW_EXCEPT_MACRO (!seq_ok, consistency_exception, "Inconsistent elimination sequence");
635  }
636 
639  og (eh.og), seq (eh.seq), cg (eh.cg), ccosts (eh.ccosts) {
640  THROW_EXCEPT_MACRO (!og.check (), consistency_exception, "Inconsistent start graph");
641  THROW_EXCEPT_MACRO (!cg.check (), consistency_exception, "Inconsistent current graph");
642  THROW_EXCEPT_MACRO (!check_sequence(), consistency_exception, "Inconsistent elimination sequence");
643  }
644 
647  (Ad_graph_t&) og= eh.og; seq= eh.seq; cg= eh.cg; ccosts= eh.ccosts;
648  THROW_DEBUG_EXCEPT_MACRO (!check (), consistency_exception, "Inconsistent graphs after assignment");
649  return *this; }
650 
653  ((Ad_graph_t&) og).swap ((Ad_graph_t&) eh.og);
654  seq.swap (eh.seq); cg.swap (eh.cg); std::swap (ccosts, eh.ccosts);
655  THROW_DEBUG_EXCEPT_MACRO (!check (), consistency_exception, "Inconsistent graphs after swapping");}
656 
668  int elimination (El_spec_t el) {
669  int costs= eliminate (el, cg);
670  if (costs > 0) {seq.push_back (el); ccosts+= costs; }
671  return costs; }
672 
674  bool check_sequence () const {
675  Ad_graph_t og_copy (og);
676  int costs= 0;
677  for (size_t c= 0; c < seq.size(); c++) {
678  El_spec_t el= seq[c]; // copied from seq because some eliminations change el
679  int el_costs= eliminate (el, og_copy);
680  if (el_costs == 0) {
681  cout << "check_sequence failed";
682  write_vector (" with elimination sequence", seq);
683  cout << " at " << c << "th entry, which is " << seq[c] << std::endl;
684  write_graph ("at this moment the graph is", og_copy);
685  cout << "it is " << (og_copy.check() ? "" : "not ") << "consistent\n";
686  write_vector ("complete elimination sequence is", seq);
687  return false; }
688  else costs+= el_costs; }
689  if (cg != og_copy) {
690  cout << "check_sequence failed because of different resulting graphs.\n";
691  write_graph ("current graph is", cg);
692  write_graph ("seq applied to og results in", og_copy);
693  write_graph ("original graph was", og);
694  write_vector ("elimination sequence is", seq);
695  return false; }
696  if (ccosts != costs) {
697  cout << "check_sequence failed because of different elimination costs.\n";
698  cout << "current costs are " << ccosts << std::endl;
699  cout << "seq applied to og requires " << costs << std::endl;
700  write_graph ("original graph was", og);
701  write_vector ("elimination sequence is", seq);
702  write_graph ("current graph is", cg);
703  return false; }
704  return true;
705  }
706 
708  bool check () const {
709  if (!og.check ()) return false; // original graph inconsistent
710  if (!cg.check ()) return false; // current graph inconsistent
711  return check_sequence (); }
712 
714  bool rebuild_graph () {
715  Ad_graph_t og_copy (og);
716  int costs= 0;
717  for (size_t c= 0; c < seq.size(); c++) {
718  int el_costs= eliminate (seq[c], og_copy);
719  if (el_costs == 0) return false; else costs+= el_costs; }
720  cg= og_copy; ccosts= costs; return true; }
721 
727  template <typename Heuristic_t>
728  void complete_sequence (Heuristic_t h) {
729  vector<El_spec_t> v1, v2;
730  for (eliminatable_objects (cg, v1); v1.size() > 0; eliminatable_objects (cg, v1)) {
731  v2.resize (0); h (v1, cg, v2); elimination (v2[0]); }}
732 };
733 
746 
748 bool convert_elimination_sequence (const vector<c_graph_t::vertex_t>& vv,
749  const c_graph_t& cg,
750  vector<edge_ij_elim_t>& ev);
751 
753 bool convert_elimination_sequence (const vector<edge_ij_elim_t>& ev,
754  const line_graph_t& lg,
755  vector<triplet_t>& tv);
756 
757 #ifdef USEXAIFBOOSTER
758 
760  const c_graph_t& angelLCG,
761  const list<EdgeRef_t>& edge_ref_list);
762 
763 const xaifBoosterCrossCountryInterface::LinearizedComputationalGraphEdge* getLCG_p (const c_graph_t::edge_t e,
764  const c_graph_t& angelLCG,
765  const list<EdgeRef_t>& edge_ref_list);
766 
767 xaifBoosterCrossCountryInterface::JacobianAccumulationExpressionVertex* getJAE_p (const c_graph_t::edge_t e,
768  const c_graph_t& angelLCG,
769  const list<EdgeRef_t>& edge_ref_list);
770 
771 void setJaevRef (const c_graph_t::edge_t e,
772  xaifBoosterCrossCountryInterface::JacobianAccumulationExpressionVertex& jaev,
773  const c_graph_t& angelLCG,
774  const list<EdgeRef_t>& edge_ref_list);
775 
776 void removeRef (const c_graph_t::edge_t e,
777  const c_graph_t& angelLCG,
778  list<EdgeRef_t>& edge_ref_list);
779 
795 unsigned int multiply_edge_pair_directly (const c_graph_t::edge_t e1,
796  const c_graph_t::edge_t e2,
797  c_graph_t& angelLCG,
798  list<EdgeRef_t>& edge_ref_list,
799  xaifBoosterCrossCountryInterface::JacobianAccumulationExpressionList& jae_list);
800 
817  c_graph_t& angelLCG,
818  list<EdgeRef_t>& edge_ref_list,
819  xaifBoosterCrossCountryInterface::JacobianAccumulationExpressionList& jae_list);
820 
837  c_graph_t& angelLCG,
838  list<EdgeRef_t>& edge_ref_list,
839  xaifBoosterCrossCountryInterface::JacobianAccumulationExpressionList& jae_list);
840 
841 unsigned int pair_elim (c_graph_t::edge_t e1,
843  c_graph_t& angelLCG,
844  const elimSeq_cost_t& currentElimSeq,
845  refillDependenceMap_t& refillDependences);
846 
847 unsigned int front_elim (c_graph_t::edge_t e,
848  c_graph_t& angelLCG,
849  const elimSeq_cost_t& currentElimSeq,
850  refillDependenceMap_t& refillDependences);
851 
852 unsigned int back_elim (c_graph_t::edge_t e,
853  c_graph_t& angelLCG,
854  const elimSeq_cost_t& currentElimSeq,
855  refillDependenceMap_t& refillDependences);
856 
857 unsigned int pairElim_noJAE (c_graph_t::edge_t e1,
859  c_graph_t& angelLCG,
860  const transformationSeq_cost_t* currentTransformationSequence,
861  refillDependenceMap_t& refillDependences);
862 
864  c_graph_t& angelLCG,
865  const transformationSeq_cost_t* currentTransformationSequence,
866  refillDependenceMap_t& refillDependences);
867 
869  c_graph_t& angelLCG,
870  const transformationSeq_cost_t* currentTransformationSequence,
871  refillDependenceMap_t& refillDependences);
872 
873 #endif // USEXAIFBOOSTER
874 
875 } // namespace angel
876 
877 #endif // _eliminations_include_
878