IFPACK  Development
Ifpack_LocalFilter.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 
45 #include "Epetra_MultiVector.h"
46 #include "Epetra_Vector.h"
47 #include "Epetra_RowMatrix.h"
48 #include "Epetra_Map.h"
49 #include "Epetra_BlockMap.h"
50 #include "Ifpack_LocalFilter.h"
51 
52 //==============================================================================
53 Ifpack_LocalFilter::Ifpack_LocalFilter(const Teuchos::RefCountPtr<const Epetra_RowMatrix>& Matrix) :
54  Matrix_(Matrix),
55  NumRows_(0),
56  NumNonzeros_(0),
57  MaxNumEntries_(0),
58  MaxNumEntriesA_(0)
59 {
60  sprintf(Label_,"%s","Ifpack_LocalFilter");
61 
62 #ifdef HAVE_MPI
63  SerialComm_ = Teuchos::rcp( new Epetra_MpiComm(MPI_COMM_SELF) );
64 #else
65  SerialComm_ = Teuchos::rcp( new Epetra_SerialComm );
66 #endif
67 
68  // localized matrix has all the local rows of Matrix
69  NumRows_ = Matrix->NumMyRows();
70 
71  // build a linear map, based on the serial communicator
72  Map_ = Teuchos::rcp( new Epetra_Map(NumRows_,0,*SerialComm_) );
73 
74  // NumEntries_ will contain the actual number of nonzeros
75  // for each localized row (that is, without external nodes,
76  // and always with the diagonal entry)
77  NumEntries_.resize(NumRows_);
78 
79  // want to store the diagonal vector. FIXME: am I really useful?
80  Diagonal_ = Teuchos::rcp( new Epetra_Vector(*Map_) );
81  if (Diagonal_ == Teuchos::null) IFPACK_CHK_ERRV(-5);
82 
83  // store this for future access to ExtractMyRowCopy().
84  // This is the # of nonzeros in the non-local matrix
85  MaxNumEntriesA_ = Matrix->MaxNumEntries();
86  // tentative value for MaxNumEntries. This is the number of
87  // nonzeros in the local matrix
88  MaxNumEntries_ = Matrix->MaxNumEntries();
89 
90  // ExtractMyRowCopy() will use these vectors
91  Indices_.resize(MaxNumEntries_);
92  Values_.resize(MaxNumEntries_);
93 
94  // now compute:
95  // - the number of nonzero per row
96  // - the total number of nonzeros
97  // - the diagonal entries
98 
99  // compute nonzeros (total and per-row), and store the
100  // diagonal entries (already modified)
101  int ActualMaxNumEntries = 0;
102 
103  for (int i = 0 ; i < NumRows_ ; ++i) {
104 
105  NumEntries_[i] = 0;
106  int Nnz, NewNnz = 0;
107  IFPACK_CHK_ERRV(ExtractMyRowCopy(i,MaxNumEntries_,Nnz,&Values_[0],&Indices_[0]));
108 
109  for (int j = 0 ; j < Nnz ; ++j) {
110  if (Indices_[j] < NumRows_ ) ++NewNnz;
111 
112  if (Indices_[j] == i)
113  (*Diagonal_)[i] = Values_[j];
114  }
115 
116  if (NewNnz > ActualMaxNumEntries)
117  ActualMaxNumEntries = NewNnz;
118 
119  NumNonzeros_ += NewNnz;
120  NumEntries_[i] = NewNnz;
121 
122  }
123 
124  MaxNumEntries_ = ActualMaxNumEntries;
125 }
126 
127 //==============================================================================
129 ExtractMyRowCopy(int MyRow, int Length, int & NumEntries,
130  double *Values, int * Indices) const
131 {
132  if ((MyRow < 0) || (MyRow >= NumRows_)) {
133  IFPACK_CHK_ERR(-1); // range not valid
134  }
135 
136  if (Length < NumEntries_[MyRow])
137  return(-1);
138 
139  // always extract using the object Values_ and Indices_.
140  // This is because I need more space than that given by
141  // the user (for the external nodes)
142  int Nnz;
143  int ierr = Matrix_->ExtractMyRowCopy(MyRow,MaxNumEntriesA_,Nnz,
144  &Values_[0],&Indices_[0]);
145 
146  IFPACK_CHK_ERR(ierr);
147 
148  // populate the user's vectors, add diagonal if not found
149  NumEntries = 0;
150 
151  for (int j = 0 ; j < Nnz ; ++j) {
152  // only local indices
153  if (Indices_[j] < NumRows_ ) {
154  Indices[NumEntries] = Indices_[j];
155  Values[NumEntries] = Values_[j];
156  ++NumEntries;
157  }
158  }
159 
160  return(0);
161 
162 }
163 
164 //==============================================================================
165 int Ifpack_LocalFilter::ExtractDiagonalCopy(Epetra_Vector & Diagonal) const
166 {
167  if (!Diagonal.Map().SameAs(*Map_))
168  IFPACK_CHK_ERR(-1);
169  Diagonal = *Diagonal_;
170  return(0);
171 }
172 
173 //==============================================================================
174 int Ifpack_LocalFilter::Apply(const Epetra_MultiVector& X,
175  Epetra_MultiVector& Y) const
176 {
177 
178  // skip expensive checks, I suppose input data are ok
179 
180  Y.PutScalar(0.0);
181  int NumVectors = Y.NumVectors();
182 
183  double** X_ptr;
184  double** Y_ptr;
185  X.ExtractView(&X_ptr);
186  Y.ExtractView(&Y_ptr);
187 
188  for (int i = 0 ; i < NumRows_ ; ++i) {
189 
190  int Nnz;
191  int ierr = Matrix_->ExtractMyRowCopy(i,MaxNumEntriesA_,Nnz,&Values_[0],
192  &Indices_[0]);
193  IFPACK_CHK_ERR(ierr);
194 
195  for (int j = 0 ; j < Nnz ; ++j) {
196  if (Indices_[j] < NumRows_ ) {
197  for (int k = 0 ; k < NumVectors ; ++k)
198  Y_ptr[k][i] += Values_[j] * X_ptr[k][Indices_[j]];
199  }
200  }
201  }
202 
203  return(0);
204 }
205 
206 //==============================================================================
207 int Ifpack_LocalFilter::ApplyInverse(const Epetra_MultiVector& X,
208  Epetra_MultiVector& Y) const
209 {
210  IFPACK_CHK_ERR(-1); // not implemented
211 }
212 
213 //==============================================================================
214 const Epetra_BlockMap& Ifpack_LocalFilter::Map() const
215 {
216  return(*Map_);
217 }
virtual int ExtractDiagonalCopy(Epetra_Vector &Diagonal) const
Returns a copy of the main diagonal in a user-provided vector.
Ifpack_LocalFilter(const Teuchos::RefCountPtr< const Epetra_RowMatrix > &Matrix)
Constructor.
virtual int ExtractMyRowCopy(int MyRow, int Length, int &NumEntries, double *Values, int *Indices) const
Returns a copy of the specified local row in user-provided arrays.