IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_SingletonFilter.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_SingletonFilter.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 #include "Epetra_Import.h"
52 
53 //==============================================================================
54 Ifpack_SingletonFilter::Ifpack_SingletonFilter(const Teuchos::RefCountPtr<Epetra_RowMatrix>& Matrix) :
55  A_(Matrix),
56  NumSingletons_(0),
57  NumRows_(0),
58  NumRowsA_(0),
59  MaxNumEntries_(0),
60  MaxNumEntriesA_(0),
61  NumNonzeros_(0)
62 {
63  // use this filter only on serial matrices
64  if (A_->Comm().NumProc() != 1) {
65  cerr << "Ifpack_SingletonFilter can be used with Comm().NumProc() == 1" << endl;
66  cerr << "only. This class is a tool for Ifpack_AdditiveSchwarz," << endl;
67  cerr << "and it is not meant to be used otherwise." << endl;
68  exit(EXIT_FAILURE);
69  }
70 
71  if ((A_->NumMyRows() != A_->NumGlobalRows()) ||
72  (A_->NumMyRows() != A_->NumMyCols()))
73  IFPACK_CHK_ERRV(-1);
74 
75  NumRowsA_ = (A_->NumMyRows());
76  MaxNumEntriesA_ = A_->MaxNumEntries();
77 
78  Indices_.resize(MaxNumEntriesA_);
79  Values_.resize(MaxNumEntriesA_);
80  Reorder_.resize(A_->NumMyRows());
81 
82  for (int i = 0 ; i < NumRowsA_ ; ++i)
83  Reorder_[i] = -1;
84 
85  // first check how may singletons I do have
86  for (int i = 0 ; i < NumRowsA_ ; ++i) {
87  int Nnz;
88  IFPACK_CHK_ERRV(A_->ExtractMyRowCopy(i,MaxNumEntriesA_,Nnz,
89  &Values_[0], &Indices_[0]));
90  if (Nnz != 1) {
91  Reorder_[i] = NumRows_++;
92  }
93  else {
94  NumSingletons_++;
95  }
96  }
97 
98  InvReorder_.resize(NumRows_);
99  for (int i = 0 ; i < NumRowsA_ ; ++i) {
100  if (Reorder_[i] < 0)
101  continue;
102  InvReorder_[Reorder_[i]] = i;
103  }
104 
105  NumEntries_.resize(NumRows_);
106  SingletonIndex_.resize(NumSingletons_);
107 
108  // now compute the nonzeros per row
109  int count = 0;
110  for (int i = 0 ; i < A_->NumMyRows() ; ++i) {
111 
112  int Nnz;
113  IFPACK_CHK_ERRV(A_->ExtractMyRowCopy(i,MaxNumEntriesA_,Nnz,
114  &Values_[0], &Indices_[0]));
115  int ii = Reorder_[i];
116  if (ii >= 0) {
117  assert (Nnz != 1);
118 
119  NumEntries_[ii] = Nnz;
120  NumNonzeros_ += Nnz;
121  if (Nnz > MaxNumEntries_)
122  MaxNumEntries_ = Nnz;
123  }
124  else {
125  SingletonIndex_[count] = i;
126  count++;
127  }
128  }
129 
130  Map_ = Teuchos::rcp( new Epetra_Map(NumRows_,0,Comm()) );
131 
132  // and finish up with the diagonal entry
133  Diagonal_ = Teuchos::rcp( new Epetra_Vector(*Map_) );
134 
135  Epetra_Vector Diagonal(A_->Map());
136  A_->ExtractDiagonalCopy(Diagonal);
137  for (int i = 0 ; i < NumRows_ ; ++i) {
138  int ii = InvReorder_[i];
139  (*Diagonal_)[i] = Diagonal[ii];
140  }
141 
142 }
143 
144 //==============================================================================
145 int Ifpack_SingletonFilter::
146 ExtractMyRowCopy(int MyRow, int Length, int & NumEntries,
147  double *Values, int * Indices) const
148 {
149  int Nnz;
150 
151  if (Length < NumEntries_[MyRow])
152  IFPACK_CHK_ERR(-1);
153 
154  int Row = InvReorder_[MyRow];
155  IFPACK_CHK_ERR(A_->ExtractMyRowCopy(Row,MaxNumEntriesA_,Nnz,
156  &Values_[0],&Indices_[0]));
157  NumEntries = 0;
158  for (int i = 0 ; i < Nnz ; ++i) {
159  int ii = Reorder_[Indices_[i]];
160  if ( ii >= 0) {
161  Indices[NumEntries] = ii;
162  Values[NumEntries] = Values_[i];
163  NumEntries++;
164  }
165  }
166  return(0);
167 }
168 
169 //==============================================================================
170 int Ifpack_SingletonFilter::
171 ExtractDiagonalCopy(Epetra_Vector & Diagonal) const
172 {
173  Diagonal = *Diagonal_;
174  return(0);
175 }
176 
177 //==============================================================================
178 int Ifpack_SingletonFilter::
179 Multiply(bool TransA, const Epetra_MultiVector& X,
180  Epetra_MultiVector& Y) const
181 {
182 
183  int NumVectors = X.NumVectors();
184  if (NumVectors != Y.NumVectors())
185  IFPACK_CHK_ERR(-1);
186 
187  Y.PutScalar(0.0);
188 
189  vector<int> Indices(MaxNumEntries_);
190  vector<double> Values(MaxNumEntries_);
191 
192  // cycle over all rows of the original matrix
193  for (int i = 0 ; i < A_->NumMyRows() ; ++i) {
194 
195  if (Reorder_[i] < 0)
196  continue; // skip singleton rows
197 
198  int Nnz;
199  A_->ExtractMyRowCopy(i,MaxNumEntriesA_,Nnz,
200  &Values[0], &Indices[0]);
201  if (!TransA) {
202  // no transpose first
203  for (int j = 0 ; j < NumVectors ; ++j) {
204  for (int k = 0 ; k < Nnz ; ++k) {
205  if (Reorder_[i] >= 0)
206  Y[j][i] += Values[k] * X[j][Reorder_[Indices[k]]];
207  }
208  }
209  }
210  else {
211  // transpose here
212  for (int j = 0 ; j < NumVectors ; ++j) {
213  for (int k = 0 ; k < Nnz ; ++k) {
214  if (Reorder_[i] >= 0)
215  Y[j][Reorder_[Indices[k]]] += Values[k] * X[j][i];
216  }
217  }
218  }
219  }
220 
221  return(0);
222 }
223 
224 //==============================================================================
225 int Ifpack_SingletonFilter::
226 Solve(bool Upper, bool Trans, bool UnitDiagonal,
227  const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
228 {
229  return(-1);
230 }
231 
232 //==============================================================================
233 int Ifpack_SingletonFilter::
234 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
235 {
236  IFPACK_CHK_ERR(Multiply(false,X,Y));
237  return(0);
238 }
239 
240 //==============================================================================
241 int Ifpack_SingletonFilter::
242 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
243 {
244  return(-1); // NOT IMPLEMENTED AT THIS STAGE
245 }
246 
247 //==============================================================================
248 int Ifpack_SingletonFilter::
249 SolveSingletons(const Epetra_MultiVector& RHS,
250  Epetra_MultiVector& LHS)
251 {
252  for (int i = 0 ; i < NumSingletons_ ; ++i) {
253  int ii = SingletonIndex_[i];
254  // get the diagonal value for the singleton
255  int Nnz;
256  A_->ExtractMyRowCopy(ii,MaxNumEntriesA_,Nnz,
257  &Values_[0], &Indices_[0]);
258  for (int j = 0 ; j < Nnz ; ++j) {
259  if (Indices_[j] == ii) {
260  for (int k = 0 ; k < LHS.NumVectors() ; ++k)
261  LHS[k][ii] = RHS[k][ii] / Values_[j];
262  }
263  }
264  }
265 
266  return(0);
267 }
268 
269 //==============================================================================
270 int Ifpack_SingletonFilter::
271 CreateReducedRHS(const Epetra_MultiVector& LHS,
272  const Epetra_MultiVector& RHS,
273  Epetra_MultiVector& ReducedRHS)
274 {
275  int NumVectors = LHS.NumVectors();
276 
277  for (int i = 0 ; i < NumRows_ ; ++i)
278  for (int k = 0 ; k < NumVectors ; ++k)
279  ReducedRHS[k][i] = RHS[k][InvReorder_[i]];
280 
281  for (int i = 0 ; i < NumRows_ ; ++i) {
282  int ii = InvReorder_[i];
283  int Nnz;
284  IFPACK_CHK_ERR(A_->ExtractMyRowCopy(ii,MaxNumEntriesA_,Nnz,
285  &Values_[0], &Indices_[0]));
286 
287  for (int j = 0 ; j < Nnz ; ++j) {
288  if (Reorder_[Indices_[j]] == -1) {
289  for (int k = 0 ; k < NumVectors ; ++k)
290  ReducedRHS[k][i] -= Values_[j] * LHS[k][Indices_[j]];
291  }
292  }
293  }
294  return(0);
295 }
296 
297 //==============================================================================
298 int Ifpack_SingletonFilter::
299 UpdateLHS(const Epetra_MultiVector& ReducedLHS,
300  Epetra_MultiVector& LHS)
301 {
302  for (int i = 0 ; i < NumRows_ ; ++i)
303  for (int k = 0 ; k < LHS.NumVectors() ; ++k)
304  LHS[k][InvReorder_[i]] = ReducedLHS[k][i];
305 
306  return(0);
307 }
Ifpack_SingletonFilter(const Teuchos::RefCountPtr< Epetra_RowMatrix > &Matrix)
Constructor.