IFPACK  Development
Ifpack_SupportGraph.h
1 /*@HEADER
2 // ***********************************************************************
3 //
4 // Ifpack: Object-Oriented Algebraic Preconditioner Package
5 // Copyright (2002) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 //@HEADER
41 */
42 
43 #ifndef IFPACK_SUPPORTGRAPH_H
44 #define IFPACK_SUPPORTGRAPH_H
45 
46 #include "Ifpack_ConfigDefs.h"
47 #include "Ifpack_Condest.h"
48 #include "Ifpack_Preconditioner.h"
49 #include "Ifpack_Amesos.h"
50 #include "Ifpack_Condest.h"
51 #include "Epetra_Map.h"
52 #include "Epetra_Comm.h"
53 #include "Epetra_Time.h"
54 #include "Epetra_Vector.h"
55 #include "Epetra_MultiVector.h"
56 #include "Epetra_LinearProblem.h"
57 #include "Epetra_RowMatrix.h"
58 #include "Epetra_CrsMatrix.h"
59 
60 #include "Teuchos_ParameterList.hpp"
61 #include "Teuchos_RefCountPtr.hpp"
62 
63 #include <boost/graph/adjacency_list.hpp>
64 #include <boost/graph/kruskal_min_spanning_tree.hpp>
65 #include <boost/graph/prim_minimum_spanning_tree.hpp>
66 #include <boost/config.hpp>
67 
68 using Teuchos::RefCountPtr;
69 using Teuchos::rcp;
70 typedef std::pair<int, int> E;
71 using namespace boost;
72 
73 typedef adjacency_list < vecS, vecS, undirectedS,
74  no_property, property < edge_weight_t, double > > Graph;
75 typedef graph_traits < Graph >::edge_descriptor Edge;
76 typedef graph_traits < Graph >::vertex_descriptor Vertex;
77 
78 
79 
80 template<typename T=Ifpack_Amesos> class Ifpack_SupportGraph :
81 public virtual Ifpack_Preconditioner
82 {
83 
84  public:
85 
87 
89  Ifpack_SupportGraph(Epetra_RowMatrix* Matrix_in);
90 
92 
93 
95 
105  virtual int SetUseTranspose(bool UseTranspose_in);
106 
108 
109 
111 
113 
121  virtual int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
122 
124 
135  virtual int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
136 
138  virtual double NormInf() const {return(0.0);}
139 
141 
142 
144 
146  virtual const char * Label() const;
147 
149  virtual bool UseTranspose() const {return(UseTranspose_);};
150 
152  virtual bool HasNormInf() const {return(false);};
153 
155  virtual const Epetra_Comm & Comm() const {return(Matrix_->Comm());};
156 
158  virtual const Epetra_Map & OperatorDomainMap() const {return(Matrix_->OperatorDomainMap());};
159 
161  virtual const Epetra_Map & OperatorRangeMap() const {return(Matrix_->OperatorRangeMap());};
162 
164 
165 
167 
169  virtual bool IsInitialized() const
170  {
171  return(IsInitialized_);
172  }
173 
175  virtual bool IsComputed() const
176  {
177  return(IsComputed_);
178  }
179 
181 
193  virtual int SetParameters(Teuchos::ParameterList& List);
194 
196 
199  virtual int Initialize();
201 
204  virtual int Compute();
205 
207 
208 
210 
211 
213 
216  virtual double Condest(const Ifpack_CondestType CT = Ifpack_Cheap,
217  const int MaxIters = 1550,
218  const double Tol = 1e-9,
219  Epetra_RowMatrix* Matrix_in = 0);
220 
222  virtual double Condest() const
223  {
224  return(Condest_);
225  }
226 
228  virtual const Epetra_RowMatrix& Matrix() const
229  {
230  return(*Matrix_);
231  }
232 
234  virtual std::ostream& Print(std::ostream&) const;
235 
237  virtual int NumInitialize() const
238  {
239  return(NumInitialize_);
240  }
241 
243  virtual int NumCompute() const
244  {
245  return(NumCompute_);
246  }
247 
249  virtual int NumApplyInverse() const
250  {
251  return(NumApplyInverse_);
252  }
253 
255  virtual double InitializeTime() const
256  {
257  return(InitializeTime_);
258  }
259 
261  virtual double ComputeTime() const
262  {
263  return(ComputeTime_);
264  }
265 
267  virtual double ApplyInverseTime() const
268  {
269  return(ApplyInverseTime_);
270  }
271 
273  virtual double InitializeFlops() const
274  {
275  return(InitializeFlops_);
276  }
277 
279  virtual double ComputeFlops() const
280  {
281  return(ComputeFlops_);
282  }
283 
285  virtual double ApplyInverseFlops() const
286  {
287  return(ApplyInverseFlops_);
288  }
289 
290 
292 
293  protected:
294 
295  // Finds the size of a subtree rooted at a vertex (work in progress)
296  int treecount(const std::vector<Vertex>& v, int *subtreesize, int node);
297 
298  // Partitions the spanning tree (work in progress)
299  int treepartition(int *table, int* children, int *subtreesize, std::vector<int>& roots,
300  int node, int n, int t);
301 
302  // Finds the largest edge between two trees (work in progress)
303  int largestbetween(int *table, int* children, const Graph& graph, const property_map<Graph, edge_weight_t>::type& map,
304  int tree1, int tree2, double *largest, int *extrasource, int *extratarget, int num_verts);
305 
306  // Finds a list of all the vertices of a tree rooted at input vertex (work in progress)
307  int findall(std::vector<int>& tree, int root, int *table, int *children, int num_verts);
308 
309 
311  int AMST();
312 
314  int FindSupport();
315 
317  Teuchos::RefCountPtr<const Epetra_RowMatrix> Matrix_;
318 
320  Teuchos::RefCountPtr<Epetra_CrsMatrix> Support_;
321 
323  string Label_;
324 
327 
330 
333 
335  Teuchos::ParameterList List_;
336 
338  double Condest_;
339 
342 
345 
347  mutable int NumApplyInverse_;
348 
351 
353  double ComputeTime_;
354 
356  mutable double ApplyInverseTime_;
357 
360 
363 
365  mutable double ApplyInverseFlops_;
366 
368  Teuchos::RefCountPtr<Epetra_Time> Time_;
369 
371  Teuchos::RefCountPtr<T> Inverse_;
372 
375 
377  double DiagPertRel_;
378 
380  double DiagPertAbs_;
381 
383  double KeepDiag_;
384 
387 
388 }; // class Ifpack_SupportGraph<T>
389 
390 
391 
392 //==============================================================================
393 template<typename T>
394 Ifpack_SupportGraph<T>::Ifpack_SupportGraph(Epetra_RowMatrix* Matrix_in):
395 Matrix_(rcp(Matrix_in,false)),
396  IsInitialized_(false),
397  IsComputed_(false),
398  UseTranspose_(false),
399  Condest_(-1.0),
400  NumInitialize_(0),
401  NumCompute_(0),
402  NumApplyInverse_(0),
403  InitializeTime_(0.0),
404  ComputeTime_(0.0),
405  ApplyInverseTime_(0.0),
406  InitializeFlops_(0.0),
407  ComputeFlops_(0.0),
408  ApplyInverseFlops_(0.0),
409  NumForests_(1),
410  DiagPertRel_(1.0),
411  DiagPertAbs_(0.0),
412  KeepDiag_(1.0),
413  Randomize_(0)
414 {
415 
416  Teuchos::ParameterList List_in;
417  SetParameters(List_in);
418 }
419 //==============================================================================
420 template<typename T>
421 int Ifpack_SupportGraph<T>::treecount(const std::vector<Vertex>& v, int *subtreesize, int node)
422 {
423  int parent = v[node];
424 
425  if(node != parent)
426  {
427  subtreesize[node] = subtreesize[node] + 1;
428  treecount(v, subtreesize, parent);
429  }
430 
431  return 0;
432 }
433 //==============================================================================
434 template<typename T>
435 int Ifpack_SupportGraph<T>::treepartition(int *table, int* children, int *subtreesize,
436  std::vector<int>& roots,int node, int n, int t)
437 {
438  subtreesize[node] = 1;
439  for(int i = table[node]; i < table[node+1]; i++)
440  {
441  int child = children[i];
442 
443  if(subtreesize[child] > (n/t + 1))
444  {
445  treepartition(table, children, subtreesize, roots, child, n, t);
446  }
447 
448  if(subtreesize[child] > (n/t))
449  {
450  children[i] = -children[i];
451  roots.push_back(child);
452 
453  }
454  else
455  {
456  subtreesize[node] = subtreesize[node] + subtreesize[child];
457  }
458  }
459  return 0;
460 }
461 //==============================================================================
462 template<typename T>
463 int Ifpack_SupportGraph<T>::largestbetween(int* table, int* children, const Graph& graph,
464  const property_map<Graph, edge_weight_t>::type& map,
465  int tree1, int tree2,
466  double *largest, int *extrasource, int *extratarget, int num_verts)
467 {
468  std::vector <int> subtree1;
469  std::vector <int> subtree2;
470 
471  if(tree1 < 0)
472  {
473  tree1 = -tree1;
474  }
475 
476  if(tree2 < 0)
477  {
478  tree2 = -tree2;
479  }
480 
481  subtree1.push_back(tree1);
482  subtree2.push_back(tree2);
483 
484 
485  findall(subtree1, tree1, table, children, num_verts);
486  findall(subtree2, tree2, table, children, num_verts);
487 
488 
489  for(int i = 0; i < subtree1.size(); i++)
490  {
491  for(int j = 0; j < subtree2.size(); j++)
492  {
493 
494  if(edge(subtree1[i], subtree2[j], graph).second)
495  {
496 
497  double temp = get(map, edge(subtree1[i],subtree2[j], graph).first);
498 
499  if(temp < *largest)
500  {
501  *largest = temp;
502  *extrasource = subtree1[i];
503  *extratarget = subtree2[j];
504  }
505 
506  }
507  }
508  }
509 
510 
511 
512  return 0;
513 }
514 //==============================================================================
515 template<typename T>
516 int Ifpack_SupportGraph<T>::findall(std::vector<int>& tree, int root, int *table, int *children, int num_verts)
517 {
518  int upper;
519  if(root == num_verts-1)
520  {
521  upper = num_verts;
522  }
523  else
524  {
525  upper = table[root+1];
526  }
527  for(int i = table[root]; i < upper; i++)
528  {
529  int child = children[i];
530 
531  if(child > 0)
532  {
533  tree.push_back(child);
534  findall(tree, child, table, children,num_verts);
535  }
536  }
537 }
538 //==============================================================================
539 template<typename T>
541 {
542 
543 
544 
545  // Extract matrix dimensions
546  int rows = (*Matrix_).NumGlobalRows();
547  int cols = (*Matrix_).NumGlobalCols();
548  int num_edges = ((*Matrix_).NumMyNonzeros() - (*Matrix_).NumMyDiagonals())/2;
549 
550  // Assert square matrix
551  IFPACK_CHK_ERR((rows == cols));
552 
553  // Rename for clarity
554  int num_verts = rows;
555 
556  double fill = .9;
557  int t = fill*pow(num_verts,.5);
558 
559  // Create data structures for the BGL code and temp data structures for extraction
560  E *edge_array = new E[num_edges];
561  double *weights = new double[num_edges];
562  double *shiftedweights = new double[num_edges];
563 
564  int num_entries;
565  int max_num_entries = (*Matrix_).MaxNumEntries();
566  double *values = new double[max_num_entries];
567  int *indices = new int[max_num_entries];
568 
569  double * diagonal = new double[num_verts];
570  double shift = 0;
571 
572  for(int i = 0; i < max_num_entries; i++)
573  {
574  values[i]=0;
575  indices[i]=0;
576  }
577 
578  // Extract from the epetra matrix keeping only one edge per pair (assume symmetric)
579  int k = 0;
580  for(int i = 0; i < num_verts; i++)
581  {
582  (*Matrix_).ExtractMyRowCopy(i,max_num_entries,num_entries,values,indices);
583 
584  for(int j = 0; j < num_entries; j++)
585  {
586 
587  if(i == indices[j])
588  {
589  diagonal[i] = values[j];
590  }
591 
592  if(i < indices[j])
593  {
594  edge_array[k] = E(i,indices[j]);
595  if(values[j] < shift)
596  shift = values[j];
597 
598  weights[k] = values[j];
599 
600  k++;
601  }
602  }
603  }
604 
605  shift = shift - 1;
606 
607  for(int i = 0; i < num_edges; i++)
608  {
609  shiftedweights[i] = weights[i] - shift;
610  }
611 
612 
613  std::vector<int> TreeNz(num_verts,1);
614 
615  int *TotalNz = new int[num_verts];
616  for(int i = 0; i < num_verts; i++)
617  {
618  TotalNz[i] = 1;
619  }
620 
621 
622  Graph gtemp(edge_array, edge_array + num_edges, shiftedweights, num_verts);
623  //gtemp = Graph(edge_array, edge_array + num_edges, shiftedweights, num_verts);
624 
625  property_map<Graph, edge_weight_t>::type weightmap = get(edge_weight, gtemp);
626 
627  //weightmap = get(edge_weight, gtemp);
628 
629  std::vector < Vertex > p(num_vertices(gtemp));
630  prim_minimum_spanning_tree(gtemp, &p[0]);
631 
632  std::vector<int> roots;
633  int numchildren[num_verts];
634  int table[num_verts];
635  int children[num_verts];
636  int *subtreesize = new int[num_verts];
637  for(std::size_t i = 0; i != p.size(); ++i)
638  {
639  numchildren[i] = 0;
640  table[i] = 0;
641  children[i] = 0;
642  subtreesize[i] = 0;
643  }
644 
645  for (std::size_t i = 0; i != p.size(); ++i)
646  {
647 
648  if (p[i] != i)
649  {
650  numchildren[p[i]] = numchildren[p[i]] + 1;
651  TreeNz[p[i]] = TreeNz[p[i]] + 1;
652  TreeNz[i] = TreeNz[i] + 1;
653  TotalNz[p[i]] = TotalNz[p[i]] + 1;
654  TotalNz[i] = TotalNz[i] + 1;
655  //std::cout << "parent[" << i << "] = " << p[i] << std::endl;
656  }
657  else
658  {
659  roots.push_back(i);
660  }
661  }
662 
663 
664 
665 
666  k = 0;
667 
668  for (std::size_t i = 0; i != p.size(); ++i)
669  {
670  table[i] = k;
671  k = k + numchildren[i];
672  }
673 
674 
675  for (std::size_t i = 0; i != p.size(); ++i)
676  {
677  if(i != p[i])
678  {
679  // std::cout << table[p[i]] << " " << numchildren[p[i]] << " " << table[p[i]] + numchildren[i] - 1 << std::endl;
680  children[table[p[i]] + numchildren[p[i]] - 1] = i;
681  numchildren[p[i]] = numchildren[p[i]] - 1;
682  }
683  }
684 
685 
686  for(std::size_t i = 0; i != p.size(); ++i)
687  {
688  treecount(p,subtreesize, i);
689  }
690 
691 
692  for(int i = 0; i < roots.size(); i++)
693  {
694  treepartition(table, children, subtreesize, roots, roots[i], num_verts, t);
695  }
696 
697  /*
698  for(int i = 0; i < roots.size(); i++)
699  {
700  p[roots[i]] = i;
701  std::cout << roots[i] << std::endl;
702  }*/
703  /*
704  std::cout << "children" << std::endl;
705  for(std::size_t i = 0; i != p.size(); ++i)
706  {
707  std::cout << children[i] << std::endl;
708  }*/
709 
710  std::vector<int> ExtraIndices[num_verts];
711  std::vector<double> ExtraValues[num_verts];
712 
713  for(int i = 0; i < num_verts; i++)
714  {
715  std::vector<int> temp;
716  std::vector<double> temp2;
717  ExtraIndices[i] = temp;
718  ExtraValues[i] = temp2;
719  }
720 
721 
722 
723  for(int i = 0; i < roots.size(); i++)
724  {
725  for(int j = i+1; j < roots.size(); j++)
726  {
727  double largest = -shift;
728  int extrasource = -1;
729  int extratarget = -1;
730 
731 
732  largestbetween(table, children, gtemp, weightmap, roots[i],roots[j],&largest,&extrasource,&extratarget,num_verts);
733 
734  if(largest < -shift)
735  {
736 
737  if((p[extrasource] != extratarget) && (p[extratarget] != extrasource))
738  {
739 
740  ExtraIndices[extrasource].push_back(extratarget);
741  ExtraIndices[extratarget].push_back(extrasource);
742  ExtraValues[extrasource].push_back(largest+shift);
743  ExtraValues[extratarget].push_back(largest+shift);
744  TotalNz[extrasource] = TotalNz[extrasource] + 1;
745  TotalNz[extratarget] = TotalNz[extratarget] + 1;
746  }
747  }
748 
749  }
750 
751 
752 
753  }
754 
755 
756 
757  Support_ = rcp(new Epetra_CrsMatrix(Copy, Matrix().RowMatrixRowMap(),TotalNz, true));
758 
759 
760  for(int i = 0; i < num_verts; i++)
761  {
762 
763  std::vector<int> Indices(TotalNz[i],0);
764  std::vector<double> Values(TotalNz[i],0);
765 
766  int other;
767  int upper;
768  if(p[i] == i)
769  {
770  upper = TreeNz[i] - 1;
771 
772  }
773  else
774  {
775 
776  std::cout << TreeNz[i] << std::endl;
777  upper = TreeNz[i] - 2;
778  Indices[TreeNz[i]-1] = p[i];
779 
780  if(!edge(i,p[i],gtemp).second)
781  {
782  std::cout << "WTFFFFFF" << std::endl;
783  }
784  Values[TreeNz[i]-1] = get(weightmap, edge(i, p[i], gtemp).first) + shift;
785  }
786 
787  for(int j = 0; j < upper; j++)
788  {
789 
790  other = children[table[i]+j];
791  if(other < 0)
792  other = -other;
793 
794  Indices[j+1] = other;
795  Values[j+1] = get(weightmap, edge(i, other, gtemp).first) + shift;
796  }
797 
798  int s = 0;
799  for(int j = TreeNz[i] + 1; j < TotalNz[i]; j++)
800  {
801  std::cout << "extra" << std::endl;
802  Indices[j] = ExtraIndices[i][s];
803  Values[j] = ExtraValues[i][s];
804 
805  s++;
806 
807  }
808 
809 
810  Indices[0] = i;
811  Values[0] = diagonal[i];
812 
813  (*Support_).InsertGlobalValues(i,TotalNz[i],&Values[0],&Indices[0]);
814 
815  }
816 
817  (*Support_).FillComplete();
818 
819  //(*Support_).Print(std::cout);
820 
821  delete subtreesize;
822  delete TotalNz;
823 
824  return 0;
825 }
826 //==============================================================================
827 template<typename T>
829 {
830 
831  // Extract matrix dimensions
832  int rows = (*Matrix_).NumGlobalRows();
833  int cols = (*Matrix_).NumGlobalCols();
834  int num_edges = ((*Matrix_).NumMyNonzeros() - (*Matrix_).NumMyDiagonals())/2;
835 
836  // Assert square matrix
837  IFPACK_CHK_ERR((rows == cols));
838 
839  // Rename for clarity
840  int num_verts = rows;
841 
842  // Create data structures for the BGL code and temp data structures for extraction
843  E *edge_array = new E[num_edges];
844  double *weights = new double[num_edges];
845 
846  int num_entries;
847  int max_num_entries = (*Matrix_).MaxNumEntries();
848  double *values = new double[max_num_entries];
849  int *indices = new int[max_num_entries];
850 
851  double * diagonal = new double[num_verts];
852 
853 
854  for(int i = 0; i < max_num_entries; i++)
855  {
856  values[i]=0;
857  indices[i]=0;
858  }
859 
860  // Extract from the epetra matrix keeping only one edge per pair (assume symmetric)
861  int k = 0;
862  for(int i = 0; i < num_verts; i++)
863  {
864  (*Matrix_).ExtractMyRowCopy(i,max_num_entries,num_entries,values,indices);
865 
866  for(int j = 0; j < num_entries; j++)
867  {
868 
869  if(i == indices[j])
870  {
871  diagonal[i] = values[j];
872  // Diagonal pertubation, only if requested
873  if (DiagPertRel_)
874  diagonal[i] *= DiagPertRel_;
875  if (DiagPertAbs_)
876  diagonal[i] += DiagPertAbs_;
877  }
878 
879  if(i < indices[j])
880  {
881  edge_array[k] = E(i,indices[j]);
882  weights[k] = values[j];
883  if (Randomize_)
884  {
885  // Add small random pertubation.
886  weights[k] *= (1.0 + 1e-8 * drand48());
887  }
888 
889  k++;
890  }
891  }
892  }
893 
894  // Create BGL graph
895  Graph g(edge_array, edge_array + num_edges, weights, num_verts);
896 
897 
898  property_map < Graph, edge_weight_t >::type weight = get(edge_weight, g);
899 
900  std::vector < Edge > spanning_tree;
901 
902  // Run Kruskal, actually maximal weight ST since edges are negative
903  kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree));
904 
905 
906  std::vector<int> NumNz(num_verts,1);
907 
908  //Find the degree of all the vertices
909  for (std::vector < Edge >::iterator ei = spanning_tree.begin();
910  ei != spanning_tree.end(); ++ei)
911  {
912  NumNz[source(*ei,g)] = NumNz[source(*ei,g)] + 1;
913  NumNz[target(*ei,g)] = NumNz[target(*ei,g)] + 1;
914  }
915 
916 
917  // Create an stl vector of stl vectors to hold indices and values (neighbour edges)
918  std::vector< std::vector< int > > Indices(num_verts);
919  // TODO: Optimize for performance, may use arrays instead of vectors
920  //std::vector<int> Indices[num_verts];
921  //std::vector<double> Values[num_verts];
922 
923  std::vector< std::vector< double > > Values(num_verts);
924 
925  for(int i = 0; i < num_verts; i++)
926  {
927  std::vector<int> temp(NumNz[i],0);
928  std::vector<double> temp2(NumNz[i],0);
929  Indices[i] = temp;
930  Values[i] = temp2;
931  }
932 
933  int *l = new int[num_verts];
934  for(int i = 0; i < num_verts; i++)
935  {
936  l[i] = 1;
937  }
938 
939  // Add each spanning forest (tree) to the support graph and
940  // remove it from original graph
941  for(int i = 0; i < NumForests_; i++)
942  {
943  if(i > 0)
944  {
945  spanning_tree.clear();
946  kruskal_minimum_spanning_tree(g,std::back_inserter(spanning_tree));
947  for(std::vector < Edge >::iterator ei = spanning_tree.begin();
948  ei != spanning_tree.end(); ++ei)
949  {
950  NumNz[source(*ei,g)] = NumNz[source(*ei,g)] + 1;
951  NumNz[target(*ei,g)] = NumNz[target(*ei,g)] + 1;
952  }
953  for(int i = 0; i < num_verts; i++)
954  {
955  Indices[i].resize(NumNz[i]);
956  Values[i].resize(NumNz[i]);
957  }
958  }
959 
960  for (std::vector < Edge >::iterator ei = spanning_tree.begin();
961  ei != spanning_tree.end(); ++ei)
962  {
963  // Assume standard Laplacian with constant row-sum.
964  // Edge weights are negative, so subtract to make diagonal positive
965  Indices[source(*ei,g)][0] = source(*ei,g);
966  Values[source(*ei,g)][0] = Values[source(*ei,g)][0] - weight[*ei];
967  Indices[target(*ei,g)][0] = target(*ei,g);
968  Values[target(*ei,g)][0] = Values[target(*ei,g)][0] - weight[*ei];
969 
970  Indices[source(*ei,g)][l[source(*ei,g)]] = target(*ei,g);
971  Values[source(*ei,g)][l[source(*ei,g)]] = weight[*ei];
972  l[source(*ei,g)] = l[source(*ei,g)] + 1;
973 
974  Indices[target(*ei,g)][l[target(*ei,g)]] = source(*ei,g);
975  Values[target(*ei,g)][l[target(*ei,g)]] = weight[*ei];
976  l[target(*ei,g)] = l[target(*ei,g)] + 1;
977 
978  remove_edge(*ei,g);
979  }
980 
981  }
982 
983 
984  // Set diagonal to weighted average of Laplacian preconditioner
985  // and the original matrix
986 
987  // First compute the "diagonal surplus" (in the original input matrix)
988  // If input is a (pure, Dirichlet) graph Laplacian , this will be 0
989  Epetra_Vector ones(Matrix_->OperatorDomainMap());
990  Epetra_Vector surplus(Matrix_->OperatorRangeMap());
991 
992  ones.PutScalar(1.0);
993  Matrix_->Multiply(false, ones, surplus);
994 
995  for(int i = 0; i < num_verts; i++)
996  {
997  Values[i][0] += surplus[i];
998  Values[i][0] = KeepDiag_*diagonal[i] +
999  (1.-KeepDiag_) * Values[i][0];
1000  }
1001 
1002  // Create the CrsMatrix for the support graph
1003  Support_ = rcp(new Epetra_CrsMatrix(Copy, Matrix().RowMatrixRowMap(),l, true));
1004 
1005 
1006  // Fill in the matrix with the stl vectors for each row
1007  for(int i = 0; i < num_verts; i++)
1008  {
1009  (*Support_).InsertGlobalValues(i,l[i],&Values[i][0],&Indices[i][0]);
1010  }
1011 
1012  (*Support_).FillComplete();
1013 
1014  //(*Support_).Print(std::cout);
1015 
1016  delete edge_array;
1017  delete weights;
1018  delete values;
1019  delete indices;
1020  delete l;
1021  delete diagonal;
1022 
1023 
1024  return(0);
1025 }
1026 //==============================================================================
1027 template<typename T>
1028 int Ifpack_SupportGraph<T>::SetParameters(Teuchos::ParameterList& List_in)
1029 {
1030  List_ = List_in;
1031  NumForests_ = List_in.get("MST: forest number", NumForests_);
1032  KeepDiag_ = List_in.get("MST: keep diagonal", KeepDiag_);
1033  Randomize_ = List_in.get("MST: randomize", Randomize_);
1034  // Diagonal pertubation parameters have weird names to be compatible with rest of Ifpack!
1035  DiagPertRel_ = List_in.get("fact: relative threshold", DiagPertRel_);
1036  DiagPertAbs_ = List_in.get("fact: absolute threshold", DiagPertAbs_);
1037 
1038  return(0);
1039 }
1040 //==============================================================================
1041 template<typename T>
1043 {
1044  IsInitialized_ = false;
1045  IsComputed_ = false;
1046 
1047 
1048  if (Time_ == Teuchos::null)
1049  {
1050  Time_ = Teuchos::rcp( new Epetra_Time(Comm()) );
1051  }
1052 
1053 
1054  Time_->ResetStartTime();
1055 
1056  FindSupport();
1057  //AMST();
1058  Inverse_ = Teuchos::rcp(new T(Support_.get()));
1059 
1060  IFPACK_CHK_ERR(Inverse_->Initialize());
1061 
1062  IsInitialized_ = true;
1063  ++NumInitialize_;
1064  InitializeTime_ += Time_->ElapsedTime();
1065 
1066  return(0);
1067 
1068 }
1069 //==============================================================================
1070 template<typename T>
1072 {
1073  if (IsInitialized() == false)
1074  IFPACK_CHK_ERR(Initialize());
1075 
1076  Time_->ResetStartTime();
1077  IsComputed_ = false;
1078  Condest_ = -1.0;
1079 
1080  IFPACK_CHK_ERR(Inverse_->Compute());
1081 
1082  IsComputed_ = true;
1083  ++NumCompute_;
1084  ComputeTime_ += Time_->ElapsedTime();
1085 
1086 
1087  return(0);
1088 }
1089 //==============================================================================
1090 template<typename T>
1092 {
1093  // store the flag -- it will be set in Initialize() if Inverse_ does not
1094  // exist.
1095  UseTranspose_ = UseTranspose_in;
1096 
1097  // If Inverse_ exists, pass it right now.
1098  if (Inverse_!=Teuchos::null)
1099  IFPACK_CHK_ERR(Inverse_->SetUseTranspose(UseTranspose_in));
1100 
1101  return(0);
1102 }
1103 //==============================================================================
1104 template<typename T>
1106 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
1107 {
1108  IFPACK_CHK_ERR(Matrix_->Apply(X,Y));
1109  return(0);
1110 }
1111 //==============================================================================
1112 template<typename T>
1113 const char * Ifpack_SupportGraph<T>::Label() const
1114 {
1115  return(Label_.c_str());
1116 }
1117 //==============================================================================
1118 template<typename T>
1120 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
1121 {
1122  if (!IsComputed())
1123  IFPACK_CHK_ERR(-3);
1124 
1125  Time_->ResetStartTime();
1126 
1127  Inverse_->ApplyInverse(X,Y);
1128 
1129  ++NumApplyInverse_;
1130  ApplyInverseTime_ += Time_->ElapsedTime();
1131 
1132  return(0);
1133 }
1134 //==============================================================================
1135 template<typename T>
1136 std::ostream& Ifpack_SupportGraph<T>::
1137 Print(std::ostream& os) const
1138 {
1139  os << "================================================================================" << std::endl;
1140  os << "Ifpack_SupportGraph: " << Label () << endl << endl;
1141  os << "Condition number estimate = " << Condest() << endl;
1142  os << "Global number of rows = " << Matrix_->NumGlobalRows() << endl;
1143  os << "Number of off diagonal entries in support graph matrix = " << Support_->NumGlobalNonzeros()-Support_->NumGlobalDiagonals() << endl;
1144  os << "Fraction of off diagonals of support graph/off diagonals of original = "
1145  << ((double)Support_->NumGlobalNonzeros()-Support_->NumGlobalDiagonals())/(Matrix_->NumGlobalNonzeros()-Matrix_->NumGlobalDiagonals());
1146  os << endl;
1147  os << "Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
1148  os << "----- ------- -------------- ------------ --------" << endl;
1149  os << "Initialize() " << std::setw(10) << NumInitialize_
1150  << " " << std::setw(15) << InitializeTime_
1151  << " 0.0 0.0" << endl;
1152  os << "Compute() " << std::setw(10) << NumCompute_
1153  << " " << std::setw(22) << ComputeTime_
1154  << " " << std::setw(15) << 1.0e-6 * ComputeFlops_;
1155  if (ComputeTime_ != 0.0)
1156  os << " " << std::setw(15) << 1.0e-6 * ComputeFlops_ / ComputeTime_ << endl;
1157  else
1158  os << " " << std::setw(15) << 0.0 << endl;
1159  os << "ApplyInverse() " << std::setw(10) << NumApplyInverse_
1160  << " " << std::setw(22) << ApplyInverseTime_
1161  << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_;
1162  if (ApplyInverseTime_ != 0.0)
1163  os << " " << std::setw(15) << 1.0e-6 * ApplyInverseFlops_ / ApplyInverseTime_ << endl;
1164  else
1165  os << " " << std::setw(15) << 0.0 << endl;
1166 
1167  os << std::endl << std::endl;
1168  os << "Now calling the underlying preconditioner's print()" << std::endl;
1169 
1170  Inverse_->Print(os);
1171 }
1172 //==============================================================================
1173 template<typename T>
1175 Condest(const Ifpack_CondestType CT, const int MaxIters,
1176  const double Tol, Epetra_RowMatrix* Matrix_in)
1177 {
1178  if (!IsComputed()) // cannot compute right now
1179  {
1180  return(-1.0);
1181  }
1182 
1183  Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix_in);
1184 
1185  return(Condest_);
1186 }
1187 
1188 #endif // IFPACK_SUPPORTGRAPH_H
double ComputeTime_
Contains the time for all successful calls to Compute().
virtual double Condest() const
Returns the computed condition number.
Teuchos::RefCountPtr< Epetra_Time > Time_
Object used for timing purposes.
int NumApplyInverse_
Contains the number of successful call to ApplyInverse().
bool IsComputed_
If true, the preconditioner has been successfully computed.
virtual const char * Label() const
Returns a character string describing the operator.
virtual int NumApplyInverse() const
Returns the number of calls to ApplyInverse().
int AMST()
Compute AMST support graph (work in progress).
virtual const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this operator.
virtual double ApplyInverseFlops() const
Returns the total number of flops to apply the preconditioner.
int NumInitialize_
Contains the number of successful calls to Initialize().
Teuchos::ParameterList List_
Stores a copy of the list given in SetParameters()
virtual double ApplyInverseTime() const
Returns the total time spent in ApplyInverse().
int Randomize_
Option to add random pertubation to edge weights, to get random spanning trees.
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the preconditioner to X, returns the result in Y.
Ifpack_SupportGraph(Epetra_RowMatrix *Matrix_in)
Constructor.
virtual double InitializeTime() const
Returns the total time spent in Initialize().
Teuchos::RefCountPtr< Epetra_CrsMatrix > Support_
Pointers to the matrix of the support graph.
virtual const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
virtual bool UseTranspose() const
Returns the current UseTranspose setting.
virtual double ComputeFlops() const
Returns the total number of flops to compute the preconditioner.
virtual int SetParameters(Teuchos::ParameterList &List)
Sets all the parameters for the preconditioner.
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the matrix to an Epetra_MultiVector.
string Label_
Contains the label of this object.
double Condest_
Contains the estimated condition number.
virtual bool HasNormInf() const
Returns true if this object can provide an approximate Inf-norm, false otherwise. ...
virtual int SetUseTranspose(bool UseTranspose_in)
If set true, transpose of this operator will be applied (not implemented).
virtual const Epetra_RowMatrix & Matrix() const
Returns a const reference to the internally stored matrix.
Teuchos::RefCountPtr< const Epetra_RowMatrix > Matrix_
Pointers to the matrix to be preconditioned.
int NumCompute_
Contains the number of successful call to Compute().
int NumForests_
Contains the number of forests in the support graph.
Teuchos::RefCountPtr< T > Inverse_
Pointer to the local solver.
Ifpack_Preconditioner: basic class for preconditioning in Ifpack.
virtual double NormInf() const
Returns the infinity norm of the global matrix (not implemented)
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this operator.
virtual int NumInitialize() const
Returns the number of calls to Initialize().
double InitializeFlops_
Contains the number of flops for Initialize().
virtual int NumCompute() const
Returns the number of calls to Compute().
virtual std::ostream & Print(std::ostream &) const
Prints on ostream basic information about this object.
double ComputeFlops_
Contains the number of flops for Compute().
double KeepDiag_
Contains the option to keep the diagonal of original matrix, or weighted average. ...
double InitializeTime_
Contains the time for all successful calls to Initialize().
bool UseTranspose_
If true, solve with the transpose (not supported by all solvers).
bool IsInitialized_
If true, the preconditioner has been successfully initialized.
virtual bool IsComputed() const
Returns true if the preconditioner has been successfully computed.
virtual int Initialize()
Initialize the preconditioner.
double ApplyInverseTime_
Contains the time for all successful calls to ApplyInverse().
double ApplyInverseFlops_
Contain sthe number of flops for ApplyInverse().
virtual bool IsInitialized() const
Returns true if the preconditioner has been successfully initialized.
virtual int Compute()
Computes the preconditioners.
double DiagPertAbs_
Absolute diagonal pertubation.
virtual double ComputeTime() const
Returns the total time spent in Compute().
int FindSupport()
Compute the support graph.
double DiagPertRel_
Relative diagonal pertubation.
virtual double InitializeFlops() const
Returns the number of flops in the initialization phase.