43 #include "Ifpack_ConfigDefs.h" 44 #include "Ifpack_SparsityFilter.h" 45 #include "Epetra_ConfigDefs.h" 46 #include "Epetra_RowMatrix.h" 47 #include "Epetra_Comm.h" 48 #include "Epetra_Map.h" 49 #include "Epetra_MultiVector.h" 50 #include "Epetra_Vector.h" 53 Ifpack_SparsityFilter::Ifpack_SparsityFilter(
const Teuchos::RefCountPtr<Epetra_RowMatrix>& Matrix,
55 int AllowedBandwidth) :
59 AllowedBandwidth_(AllowedBandwidth),
60 AllowedEntries_(AllowedEntries),
65 if (A_->Comm().NumProc() != 1) {
66 cerr <<
"Ifpack_SparsityFilter can be used with Comm().NumProc() == 1" << endl;
67 cerr <<
"only. This class is a tool for Ifpack_AdditiveSchwarz," << endl;
68 cerr <<
"and it is not meant to be used otherwise." << endl;
73 if ((A_->NumMyRows() != A_->NumMyCols()) ||
74 (A_->NumMyRows() != A_->NumGlobalRows()))
77 NumRows_ = A_->NumMyRows();
78 MaxNumEntriesA_ = A_->MaxNumEntries();
79 Indices_.resize(MaxNumEntriesA_);
80 Values_.resize(MaxNumEntriesA_);
83 if (AllowedBandwidth_ == -1)
84 AllowedBandwidth_ = NumRows_;
89 vector<int> Ind(MaxNumEntriesA_);
90 vector<double> Val(MaxNumEntriesA_);
92 NumEntries_.resize(NumRows_);
93 for (
int i = 0 ; i < NumRows_ ; ++i)
94 NumEntries_[i] = MaxNumEntriesA_;
96 for (
int i = 0 ; i < A_->NumMyRows() ; ++i) {
98 IFPACK_CHK_ERRV(ExtractMyRowCopy(i,MaxNumEntriesA_,Nnz,
101 NumEntries_[i] = Nnz;
103 if (Nnz > MaxNumEntries_)
104 MaxNumEntries_ = Nnz;
110 int Ifpack_SparsityFilter::
111 ExtractMyRowCopy(
int MyRow,
int Length,
int & NumEntries,
112 double *Values,
int * Indices)
const 114 if (Length < NumEntries_[MyRow])
118 IFPACK_CHK_ERR(A_->ExtractMyRowCopy(MyRow,MaxNumEntriesA_,Nnz,
119 &Values_[0],&Indices_[0]));
121 double Threshold = 0.0;
124 if (Nnz > AllowedEntries_) {
126 vector<double> Values2(Nnz);
128 for (
int i = 0 ; i < Nnz ; ++i) {
130 if (Indices_[i] == MyRow)
133 Values2[count] = IFPACK_ABS(Values_[i]);
137 for (
int i = count ; i < Nnz ; ++i)
141 sort(Values2.rbegin(),Values2.rend());
143 Threshold = Values2[AllowedEntries_ - 1];
152 for (
int i = 0 ; i < Nnz ; ++i) {
154 if (IFPACK_ABS(Indices_[i] - MyRow) > AllowedBandwidth_)
157 if ((Indices_[i] != MyRow) && (IFPACK_ABS(Values_[i]) < Threshold))
160 Values[NumEntries] = Values_[i];
161 Indices[NumEntries] = Indices_[i];
164 if (NumEntries > AllowedEntries_)
172 int Ifpack_SparsityFilter::
173 ExtractDiagonalCopy(Epetra_Vector & Diagonal)
const 175 IFPACK_RETURN(A_->ExtractDiagonalCopy(Diagonal));
179 int Ifpack_SparsityFilter::
180 Multiply(
bool TransA,
const Epetra_MultiVector& X,
181 Epetra_MultiVector& Y)
const 185 if (NumVectors != Y.NumVectors())
190 vector<int> Indices(MaxNumEntries_);
191 vector<double> Values(MaxNumEntries_);
193 for (
int i = 0 ; i < A_->NumMyRows() ; ++i) {
196 ExtractMyRowCopy(i,MaxNumEntries_,Nnz,
197 &Values[0], &Indices[0]);
201 for (
int k = 0 ; k < Nnz ; ++k) {
202 Y[j][i] += Values[k] * X[j][Indices[k]];
209 for (
int k = 0 ; k < Nnz ; ++k) {
210 Y[j][Indices[k]] += Values[k] * X[j][i];
220 int Ifpack_SparsityFilter::
221 Solve(
bool Upper,
bool Trans,
bool UnitDiagonal,
222 const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 228 int Ifpack_SparsityFilter::
229 Apply(
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const 231 IFPACK_RETURN(Multiply(UseTranspose(),X,Y));
235 int Ifpack_SparsityFilter::
236 ApplyInverse(
const Epetra_MultiVector& X, Epetra_MultiVector& Y)
const virtual int NumVectors() const
Returns the number of vectors in LHS/RHS.