43 #ifndef IFPACK_SUPPORTGRAPH_H 44 #define IFPACK_SUPPORTGRAPH_H 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" 60 #include "Teuchos_ParameterList.hpp" 61 #include "Teuchos_RefCountPtr.hpp" 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> 68 using Teuchos::RefCountPtr;
70 typedef std::pair<int, int> E;
71 using namespace boost;
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;
105 virtual int SetUseTranspose(
bool UseTranspose_in);
121 virtual int Apply(
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const;
135 virtual int ApplyInverse(
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const;
146 virtual const char * Label()
const;
155 virtual const Epetra_Comm &
Comm()
const {
return(Matrix_->Comm());};
161 virtual const Epetra_Map &
OperatorRangeMap()
const {
return(Matrix_->OperatorRangeMap());};
171 return(IsInitialized_);
193 virtual int SetParameters(Teuchos::ParameterList& List);
199 virtual int Initialize();
204 virtual int Compute();
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);
228 virtual const Epetra_RowMatrix&
Matrix()
const 234 virtual std::ostream& Print(std::ostream&)
const;
239 return(NumInitialize_);
251 return(NumApplyInverse_);
257 return(InitializeTime_);
263 return(ComputeTime_);
269 return(ApplyInverseTime_);
275 return(InitializeFlops_);
281 return(ComputeFlops_);
287 return(ApplyInverseFlops_);
296 int treecount(
const std::vector<Vertex>& v,
int *subtreesize,
int node);
299 int treepartition(
int *table,
int* children,
int *subtreesize, std::vector<int>& roots,
300 int node,
int n,
int t);
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);
307 int findall(std::vector<int>& tree,
int root,
int *table,
int *children,
int num_verts);
317 Teuchos::RefCountPtr<const Epetra_RowMatrix>
Matrix_;
368 Teuchos::RefCountPtr<Epetra_Time>
Time_;
395 Matrix_(rcp(Matrix_in,false)),
396 IsInitialized_(false),
398 UseTranspose_(false),
403 InitializeTime_(0.0),
405 ApplyInverseTime_(0.0),
406 InitializeFlops_(0.0),
408 ApplyInverseFlops_(0.0),
416 Teuchos::ParameterList List_in;
423 int parent = v[node];
427 subtreesize[node] = subtreesize[node] + 1;
428 treecount(v, subtreesize, parent);
436 std::vector<int>& roots,
int node,
int n,
int t)
438 subtreesize[node] = 1;
439 for(
int i = table[node]; i < table[node+1]; i++)
441 int child = children[i];
443 if(subtreesize[child] > (n/t + 1))
445 treepartition(table, children, subtreesize, roots, child, n, t);
448 if(subtreesize[child] > (n/t))
450 children[i] = -children[i];
451 roots.push_back(child);
456 subtreesize[node] = subtreesize[node] + subtreesize[child];
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)
468 std::vector <int> subtree1;
469 std::vector <int> subtree2;
481 subtree1.push_back(tree1);
482 subtree2.push_back(tree2);
485 findall(subtree1, tree1, table, children, num_verts);
486 findall(subtree2, tree2, table, children, num_verts);
489 for(
int i = 0; i < subtree1.size(); i++)
491 for(
int j = 0; j < subtree2.size(); j++)
494 if(edge(subtree1[i], subtree2[j], graph).second)
497 double temp =
get(map, edge(subtree1[i],subtree2[j], graph).first);
502 *extrasource = subtree1[i];
503 *extratarget = subtree2[j];
519 if(root == num_verts-1)
525 upper = table[root+1];
527 for(
int i = table[root]; i < upper; i++)
529 int child = children[i];
533 tree.push_back(child);
534 findall(tree, child, table, children,num_verts);
546 int rows = (*Matrix_).NumGlobalRows();
547 int cols = (*Matrix_).NumGlobalCols();
548 int num_edges = ((*Matrix_).NumMyNonzeros() - (*Matrix_).NumMyDiagonals())/2;
551 IFPACK_CHK_ERR((rows == cols));
554 int num_verts = rows;
557 int t = fill*pow(num_verts,.5);
560 E *edge_array =
new E[num_edges];
561 double *weights =
new double[num_edges];
562 double *shiftedweights =
new double[num_edges];
565 int max_num_entries = (*Matrix_).MaxNumEntries();
566 double *values =
new double[max_num_entries];
567 int *indices =
new int[max_num_entries];
569 double * diagonal =
new double[num_verts];
572 for(
int i = 0; i < max_num_entries; i++)
580 for(
int i = 0; i < num_verts; i++)
582 (*Matrix_).ExtractMyRowCopy(i,max_num_entries,num_entries,values,indices);
584 for(
int j = 0; j < num_entries; j++)
589 diagonal[i] = values[j];
594 edge_array[k] = E(i,indices[j]);
595 if(values[j] < shift)
598 weights[k] = values[j];
607 for(
int i = 0; i < num_edges; i++)
609 shiftedweights[i] = weights[i] - shift;
613 std::vector<int> TreeNz(num_verts,1);
615 int *TotalNz =
new int[num_verts];
616 for(
int i = 0; i < num_verts; i++)
622 Graph gtemp(edge_array, edge_array + num_edges, shiftedweights, num_verts);
625 property_map<Graph, edge_weight_t>::type weightmap =
get(edge_weight, gtemp);
629 std::vector < Vertex > p(num_vertices(gtemp));
630 prim_minimum_spanning_tree(gtemp, &p[0]);
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)
645 for (std::size_t i = 0; i != p.size(); ++i)
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;
668 for (std::size_t i = 0; i != p.size(); ++i)
671 k = k + numchildren[i];
675 for (std::size_t i = 0; i != p.size(); ++i)
680 children[table[p[i]] + numchildren[p[i]] - 1] = i;
681 numchildren[p[i]] = numchildren[p[i]] - 1;
686 for(std::size_t i = 0; i != p.size(); ++i)
688 treecount(p,subtreesize, i);
692 for(
int i = 0; i < roots.size(); i++)
694 treepartition(table, children, subtreesize, roots, roots[i], num_verts, t);
710 std::vector<int> ExtraIndices[num_verts];
711 std::vector<double> ExtraValues[num_verts];
713 for(
int i = 0; i < num_verts; i++)
715 std::vector<int> temp;
716 std::vector<double> temp2;
717 ExtraIndices[i] = temp;
718 ExtraValues[i] = temp2;
723 for(
int i = 0; i < roots.size(); i++)
725 for(
int j = i+1; j < roots.size(); j++)
727 double largest = -shift;
728 int extrasource = -1;
729 int extratarget = -1;
732 largestbetween(table, children, gtemp, weightmap, roots[i],roots[j],&largest,&extrasource,&extratarget,num_verts);
737 if((p[extrasource] != extratarget) && (p[extratarget] != extrasource))
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;
757 Support_ = rcp(
new Epetra_CrsMatrix(Copy,
Matrix().RowMatrixRowMap(),TotalNz,
true));
760 for(
int i = 0; i < num_verts; i++)
763 std::vector<int> Indices(TotalNz[i],0);
764 std::vector<double> Values(TotalNz[i],0);
770 upper = TreeNz[i] - 1;
776 std::cout << TreeNz[i] << std::endl;
777 upper = TreeNz[i] - 2;
778 Indices[TreeNz[i]-1] = p[i];
780 if(!edge(i,p[i],gtemp).second)
782 std::cout <<
"WTFFFFFF" << std::endl;
784 Values[TreeNz[i]-1] =
get(weightmap, edge(i, p[i], gtemp).first) + shift;
787 for(
int j = 0; j < upper; j++)
790 other = children[table[i]+j];
794 Indices[j+1] = other;
795 Values[j+1] =
get(weightmap, edge(i, other, gtemp).first) + shift;
799 for(
int j = TreeNz[i] + 1; j < TotalNz[i]; j++)
801 std::cout <<
"extra" << std::endl;
802 Indices[j] = ExtraIndices[i][s];
803 Values[j] = ExtraValues[i][s];
811 Values[0] = diagonal[i];
813 (*Support_).InsertGlobalValues(i,TotalNz[i],&Values[0],&Indices[0]);
817 (*Support_).FillComplete();
832 int rows = (*Matrix_).NumGlobalRows();
833 int cols = (*Matrix_).NumGlobalCols();
834 int num_edges = ((*Matrix_).NumMyNonzeros() - (*Matrix_).NumMyDiagonals())/2;
837 IFPACK_CHK_ERR((rows == cols));
840 int num_verts = rows;
843 E *edge_array =
new E[num_edges];
844 double *weights =
new double[num_edges];
847 int max_num_entries = (*Matrix_).MaxNumEntries();
848 double *values =
new double[max_num_entries];
849 int *indices =
new int[max_num_entries];
851 double * diagonal =
new double[num_verts];
854 for(
int i = 0; i < max_num_entries; i++)
862 for(
int i = 0; i < num_verts; i++)
864 (*Matrix_).ExtractMyRowCopy(i,max_num_entries,num_entries,values,indices);
866 for(
int j = 0; j < num_entries; j++)
871 diagonal[i] = values[j];
881 edge_array[k] = E(i,indices[j]);
882 weights[k] = values[j];
886 weights[k] *= (1.0 + 1e-8 * drand48());
895 Graph g(edge_array, edge_array + num_edges, weights, num_verts);
898 property_map < Graph, edge_weight_t >::type weight =
get(edge_weight, g);
900 std::vector < Edge > spanning_tree;
903 kruskal_minimum_spanning_tree(g, std::back_inserter(spanning_tree));
906 std::vector<int> NumNz(num_verts,1);
909 for (std::vector < Edge >::iterator ei = spanning_tree.begin();
910 ei != spanning_tree.end(); ++ei)
912 NumNz[source(*ei,g)] = NumNz[source(*ei,g)] + 1;
913 NumNz[target(*ei,g)] = NumNz[target(*ei,g)] + 1;
918 std::vector< std::vector< int > > Indices(num_verts);
923 std::vector< std::vector< double > > Values(num_verts);
925 for(
int i = 0; i < num_verts; i++)
927 std::vector<int> temp(NumNz[i],0);
928 std::vector<double> temp2(NumNz[i],0);
933 int *l =
new int[num_verts];
934 for(
int i = 0; i < num_verts; i++)
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)
950 NumNz[source(*ei,g)] = NumNz[source(*ei,g)] + 1;
951 NumNz[target(*ei,g)] = NumNz[target(*ei,g)] + 1;
953 for(
int i = 0; i < num_verts; i++)
955 Indices[i].resize(NumNz[i]);
956 Values[i].resize(NumNz[i]);
960 for (std::vector < Edge >::iterator ei = spanning_tree.begin();
961 ei != spanning_tree.end(); ++ei)
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];
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;
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;
989 Epetra_Vector ones(
Matrix_->OperatorDomainMap());
990 Epetra_Vector surplus(
Matrix_->OperatorRangeMap());
993 Matrix_->Multiply(
false, ones, surplus);
995 for(
int i = 0; i < num_verts; i++)
997 Values[i][0] += surplus[i];
1003 Support_ = rcp(
new Epetra_CrsMatrix(Copy,
Matrix().RowMatrixRowMap(),l,
true));
1007 for(
int i = 0; i < num_verts; i++)
1009 (*Support_).InsertGlobalValues(i,l[i],&Values[i][0],&Indices[i][0]);
1012 (*Support_).FillComplete();
1027 template<
typename T>
1041 template<
typename T>
1048 if (
Time_ == Teuchos::null)
1050 Time_ = Teuchos::rcp(
new Epetra_Time(
Comm()) );
1054 Time_->ResetStartTime();
1060 IFPACK_CHK_ERR(
Inverse_->Initialize());
1070 template<
typename T>
1076 Time_->ResetStartTime();
1080 IFPACK_CHK_ERR(
Inverse_->Compute());
1090 template<
typename T>
1099 IFPACK_CHK_ERR(
Inverse_->SetUseTranspose(UseTranspose_in));
1104 template<
typename T>
1106 Apply(
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 1108 IFPACK_CHK_ERR(
Matrix_->Apply(X,Y));
1112 template<
typename T>
1118 template<
typename T>
1125 Time_->ResetStartTime();
1135 template<
typename T>
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 = " 1147 os <<
"Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
1148 os <<
"----- ------- -------------- ------------ --------" << endl;
1151 <<
" 0.0 0.0" << endl;
1152 os <<
"Compute() " << std::setw(10) <<
NumCompute_ 1158 os <<
" " << std::setw(15) << 0.0 << endl;
1165 os <<
" " << std::setw(15) << 0.0 << endl;
1167 os << std::endl << std::endl;
1168 os <<
"Now calling the underlying preconditioner's print()" << std::endl;
1173 template<
typename T>
1175 Condest(
const Ifpack_CondestType CT,
const int MaxIters,
1176 const double Tol, Epetra_RowMatrix* Matrix_in)
1183 Condest_ = Ifpack_Condest(*
this, CT, MaxIters, Tol, Matrix_in);
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.