IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_SparsityFilter.cpp
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 #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"
51 
52 //==============================================================================
53 Ifpack_SparsityFilter::Ifpack_SparsityFilter(const Teuchos::RefCountPtr<Epetra_RowMatrix>& Matrix,
54  int AllowedEntries,
55  int AllowedBandwidth) :
56  A_(Matrix),
57  MaxNumEntries_(0),
58  MaxNumEntriesA_(0),
59  AllowedBandwidth_(AllowedBandwidth),
60  AllowedEntries_(AllowedEntries),
61  NumNonzeros_(0),
62  NumRows_(0)
63 {
64  // use this filter only on serial matrices
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;
69  exit(EXIT_FAILURE);
70  }
71 
72  // only square serial matrices
73  if ((A_->NumMyRows() != A_->NumMyCols()) ||
74  (A_->NumMyRows() != A_->NumGlobalRows()))
75  IFPACK_CHK_ERRV(-1);
76 
77  NumRows_ = A_->NumMyRows();
78  MaxNumEntriesA_ = A_->MaxNumEntries();
79  Indices_.resize(MaxNumEntriesA_);
80  Values_.resize(MaxNumEntriesA_);
81 
82  // default value is to not consider bandwidth
83  if (AllowedBandwidth_ == -1)
84  AllowedBandwidth_ = NumRows_;
85 
86  // computes the number of nonzero elements per row in the
87  // dropped matrix. Stores this number in NumEntries_.
88  // Also, computes the global number of nonzeros.
89  vector<int> Ind(MaxNumEntriesA_);
90  vector<double> Val(MaxNumEntriesA_);
91 
92  NumEntries_.resize(NumRows_);
93  for (int i = 0 ; i < NumRows_ ; ++i)
94  NumEntries_[i] = MaxNumEntriesA_;
95 
96  for (int i = 0 ; i < A_->NumMyRows() ; ++i) {
97  int Nnz;
98  IFPACK_CHK_ERRV(ExtractMyRowCopy(i,MaxNumEntriesA_,Nnz,
99  &Val[0], &Ind[0]));
100 
101  NumEntries_[i] = Nnz;
102  NumNonzeros_ += Nnz;
103  if (Nnz > MaxNumEntries_)
104  MaxNumEntries_ = Nnz;
105 
106  }
107 }
108 
109 //==============================================================================
110 int Ifpack_SparsityFilter::
111 ExtractMyRowCopy(int MyRow, int Length, int & NumEntries,
112  double *Values, int * Indices) const
113 {
114  if (Length < NumEntries_[MyRow])
115  IFPACK_CHK_ERR(-1);
116 
117  int Nnz;
118  IFPACK_CHK_ERR(A_->ExtractMyRowCopy(MyRow,MaxNumEntriesA_,Nnz,
119  &Values_[0],&Indices_[0]));
120 
121  double Threshold = 0.0;
122 
123  // this `if' is to define the cut-off value
124  if (Nnz > AllowedEntries_) {
125 
126  vector<double> Values2(Nnz);
127  int count = 0;
128  for (int i = 0 ; i < Nnz ; ++i) {
129  // skip diagonal entry (which is always inserted)
130  if (Indices_[i] == MyRow)
131  continue;
132  // put absolute value
133  Values2[count] = IFPACK_ABS(Values_[i]);
134  count++;
135  }
136 
137  for (int i = count ; i < Nnz ; ++i)
138  Values2[i] = 0.0;
139 
140  // sort in descending order
141  sort(Values2.rbegin(),Values2.rend());
142  // get the cut-off value
143  Threshold = Values2[AllowedEntries_ - 1];
144 
145  }
146 
147  // loop over all nonzero elements of row MyRow,
148  // and drop elements below specified threshold.
149  // Also, add value to zero diagonal
150  NumEntries = 0;
151 
152  for (int i = 0 ; i < Nnz ; ++i) {
153 
154  if (IFPACK_ABS(Indices_[i] - MyRow) > AllowedBandwidth_)
155  continue;
156 
157  if ((Indices_[i] != MyRow) && (IFPACK_ABS(Values_[i]) < Threshold))
158  continue;
159 
160  Values[NumEntries] = Values_[i];
161  Indices[NumEntries] = Indices_[i];
162 
163  NumEntries++;
164  if (NumEntries > AllowedEntries_)
165  break;
166  }
167 
168  return(0);
169 }
170 
171 //==============================================================================
172 int Ifpack_SparsityFilter::
173 ExtractDiagonalCopy(Epetra_Vector & Diagonal) const
174 {
175  IFPACK_RETURN(A_->ExtractDiagonalCopy(Diagonal));
176 }
177 
178 //==============================================================================
179 int Ifpack_SparsityFilter::
180 Multiply(bool TransA, const Epetra_MultiVector& X,
181  Epetra_MultiVector& Y) const
182 {
183 
184  int NumVectors = X.NumVectors();
185  if (NumVectors != Y.NumVectors())
186  IFPACK_CHK_ERR(-1);
187 
188  Y.PutScalar(0.0);
189 
190  vector<int> Indices(MaxNumEntries_);
191  vector<double> Values(MaxNumEntries_);
192 
193  for (int i = 0 ; i < A_->NumMyRows() ; ++i) {
194 
195  int Nnz;
196  ExtractMyRowCopy(i,MaxNumEntries_,Nnz,
197  &Values[0], &Indices[0]);
198  if (!TransA) {
199  // no transpose first
200  for (int j = 0 ; j < NumVectors ; ++j) {
201  for (int k = 0 ; k < Nnz ; ++k) {
202  Y[j][i] += Values[k] * X[j][Indices[k]];
203  }
204  }
205  }
206  else {
207  // transpose here
208  for (int j = 0 ; j < NumVectors ; ++j) {
209  for (int k = 0 ; k < Nnz ; ++k) {
210  Y[j][Indices[k]] += Values[k] * X[j][i];
211  }
212  }
213  }
214  }
215 
216  return(0);
217 }
218 
219 //==============================================================================
220 int Ifpack_SparsityFilter::
221 Solve(bool Upper, bool Trans, bool UnitDiagonal,
222  const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
223 {
224  IFPACK_CHK_ERR(-98);
225 }
226 
227 //==============================================================================
228 int Ifpack_SparsityFilter::
229 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
230 {
231  IFPACK_RETURN(Multiply(UseTranspose(),X,Y));
232 }
233 
234 //==============================================================================
235 int Ifpack_SparsityFilter::
236 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
237 {
238  IFPACK_CHK_ERR(-98);
239 }