IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_SubdomainFilter.cpp
1 //@HEADER
2 // ************************************************************************
3 //
4 // Ifpack_SubdomainFilter
5 // Author: Radu Popescu <radu.popescu@epfl.ch>
6 // Copyright 2011 EPFL - CMCS
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 //
12 // 1. Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
14 //
15 // 2. Redistributions in binary form must reproduce the above copyright
16 // notice, this list of conditions and the following disclaimer in the
17 // documentation and/or other materials provided with the distribution.
18 //
19 // 3. Neither the name of the copyright holder nor the names of the
20 // contributors may be used to endorse or promote products derived from
21 // this software without specific prior written permission.
22 //
23 // THIS SOFTWARE IS PROVIDED BY EPFL - CMCS "AS IS" AND ANY EXPRESS OR
24 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 // ARE DISCLAIMED. IN NO EVENT SHALL EPFL - CMCS OR THE CONTRIBUTORS
27 // BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28 // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 // BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32 // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33 // EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 //
35 // ************************************************************************
36 //@HEADER
37 
38 #ifdef IFPACK_SUBCOMM_CODE
39 
40 #include <vector>
41 #include <algorithm>
42 
43 #include "Ifpack_ConfigDefs.h"
44 
45 #include "Epetra_MultiVector.h"
46 #include "Epetra_Vector.h"
47 #include "Epetra_IntVector.h"
48 #include "Epetra_RowMatrix.h"
49 #include "Epetra_Map.h"
50 #include "Epetra_BlockMap.h"
51 #include "Ifpack_SubdomainFilter.h"
52 #include "Ifpack_OverlappingRowMatrix.h"
53 #include "Epetra_Import.h"
54 #include "Epetra_Export.h"
55 #include "Epetra_CrsMatrix.h"
56 #include "Epetra_BLAS_wrappers.h"
57 
58 using namespace Teuchos;
59 
60 //==============================================================================
61 Ifpack_SubdomainFilter::Ifpack_SubdomainFilter(const RefCountPtr<const Epetra_RowMatrix>& Matrix,int subdomainId) :
62  Matrix_(Matrix),
63  NumMyRows_(0),
64  NumMyNonzeros_(0),
65  NumGlobalRows_(0),
66  NumGlobalCols_(0),
67  MaxNumEntries_(0),
68  MaxNumEntriesA_(0)
69 {
70  sprintf(Label_,"%s","Ifpack_SubdomainFilter");
71 
72  ImportVector_ = 0;
73  ExportVector_ = 0;
74 
75  ovA_ = dynamic_cast<const Ifpack_OverlappingRowMatrix*>(&*Matrix_);
76 
77  if (ovA_) {
78  Acrs_=dynamic_cast<const Epetra_CrsMatrix*>(&ovA_->A());
79  NumMyRowsA_ = ovA_->A().NumMyRows();
80  NumMyRowsB_ = ovA_->B().NumMyRows();
81  } else {
82  NumMyRowsA_ = Matrix->NumMyRows();
83  NumMyRowsB_ = 0;
84  }
85 
86 #ifdef HAVE_MPI
87  const Epetra_MpiComm *pComm = dynamic_cast<const Epetra_MpiComm*>( &(Matrix->Comm()) );
88  assert(pComm != NULL);
89  MPI_Comm_split(pComm->Comm(), subdomainId, pComm->MyPID(), &subdomainMPIComm_);
90  SubComm_ = rcp( new Epetra_MpiComm(subdomainMPIComm_) );
91 #else
92  SubComm_ = rcp( new Epetra_SerialComm );
93 #endif
94 
95  NumMyRows_ = Matrix->NumMyRows();
96  SubComm_->SumAll(&NumMyRows_,&NumGlobalRows_,1);
97 
98  // Get the row GID corresponding to the process
99  const Epetra_Map &globRowMap = Matrix->RowMatrixRowMap();
100  std::vector<int> myRowsGID(NumMyRows_);
101  globRowMap.MyGlobalElements(&myRowsGID[0]);
102 
103  // Gather the GID of all rows in the subdomain
104  // Get the maximum number of local rows on each processor in the subdomain
105  // Allocate a vector large enough (Proc cout * max number local rows
106  // Gather all the local row indices. If a process has less than max num
107  // local rows, pad the local vector with -1;
108  // After the gather, eliminate the -1 elements from the target vector
109  //
110  // TODO: this section should be rewritten to avoid the GatherAll call.
111  // Right now it's not memory efficient. It could be moved to
112  // Epetra_Map since it a map operation
113 
114  int MaxLocalRows = 0;
115  SubComm_->MaxAll(&NumMyRows_, &MaxLocalRows, 1);
116 
117  std::vector<int> SubdomainRowGID(SubComm_->NumProc() * MaxLocalRows);
118  myRowsGID.resize(MaxLocalRows, -1);
119 
120  SubComm_->GatherAll(&myRowsGID[0],
121  &SubdomainRowGID[0],
122  MaxLocalRows);
123 
124  std::sort(SubdomainRowGID.begin(), SubdomainRowGID.end());
125 
126  int PaddingSize = SubdomainRowGID.size() - NumGlobalRows_;
127  SubdomainRowGID.erase(SubdomainRowGID.begin(),
128  SubdomainRowGID.begin() + PaddingSize);
129 
130  // Get the col GID corresponding to the process
131  const Epetra_Map &globColMap = Matrix->RowMatrixColMap();
132  NumMyCols_ = globColMap.NumMyElements();
133  std::vector<int> myGlobalCols(NumMyCols_);
134  globColMap.MyGlobalElements(&myGlobalCols[0]);
135 
136  // Eliminate column GID that are outside the subdomain
137  std::vector<int> filteredColGID;
138  for (int i = 0; i < NumMyCols_; ++i) {
139  if (std::find(SubdomainRowGID.begin(), SubdomainRowGID.end(),
140  myGlobalCols[i]) != SubdomainRowGID.end()) {
141  filteredColGID.push_back(myGlobalCols[i]);
142  }
143  }
144  NumMyCols_ = filteredColGID.size();
145 
146  // Create the maps with the reindexed GIDs
147  Map_ = rcp( new Epetra_Map(-1, NumMyRows_, &myRowsGID[0], globRowMap.IndexBase(), *SubComm_) );
148  colMap_ = rcp( new Epetra_Map(-1, NumMyCols_, &filteredColGID[0], globColMap.IndexBase(), *SubComm_) );
149  NumGlobalCols_ = NumGlobalRows_;
150 
151  NumEntries_.resize(NumMyRows_);
152  MaxNumEntriesA_ = Matrix->MaxNumEntries();
153  MaxNumEntries_ = Matrix->MaxNumEntries();
154 
155  Diagonal_ = rcp( new Epetra_Vector(*Map_) );
156  if (Diagonal_ == Teuchos::null) IFPACK_CHK_ERRV(-5);
157 
158  Indices_.resize(MaxNumEntries_);
159  Values_.resize(MaxNumEntries_);
160 
161  Ac_LIDMap_ = 0;
162  Bc_LIDMap_ = 0;
163  Ar_LIDMap_ = 0;
164  Br_LIDMap_ = 0;
165 
166  if(ovA_){
167  Ac_LIDMap_ = new int[ovA_->A().NumMyCols() + 1];
168  for(int i = 0; i < ovA_->A().NumMyCols(); i++) {
169  Ac_LIDMap_[i] = colMap_->LID(ovA_->A().RowMatrixColMap().GID(i));
170  }
171  Bc_LIDMap_ = new int[ovA_->B().NumMyCols() + 1];
172  for(int i = 0; i < ovA_->B().NumMyCols(); i++) {
173  Bc_LIDMap_[i] = colMap_->LID(ovA_->B().RowMatrixColMap().GID(i));
174  }
175  Ar_LIDMap_ = new int[ovA_->A().NumMyRows() + 1];
176  for(int i = 0; i < ovA_->A().NumMyRows(); i++) {
177  Ar_LIDMap_[i] = Map_->LID(ovA_->A().RowMatrixRowMap().GID(i));
178  }
179  Br_LIDMap_ = new int[ovA_->B().NumMyRows() + 1];
180  for(int i = 0; i < ovA_->B().NumMyRows(); i++) {
181  Br_LIDMap_[i] = Map_->LID(ovA_->B().RowMatrixRowMap().GID(i));
182  }
183  }
184 
185  int ActualMaxNumEntries = 0;
186 
187  for (int i = 0 ; i < NumMyRows_; ++i) {
188  NumEntries_[i] = 0;
189  int Nnz;
190  IFPACK_CHK_ERRV(ExtractMyRowCopy(i, MaxNumEntries_, Nnz, &Values_[0], &Indices_[0]));
191 
192  for (int j = 0 ; j < Nnz; ++j) {
193  if (Indices_[j] == i) {
194  (*Diagonal_)[i] = Values_[j];
195  }
196  }
197 
198  if (Nnz > ActualMaxNumEntries) {
199  ActualMaxNumEntries = Nnz;
200  }
201 
202  NumMyNonzeros_ += Nnz;
203  NumEntries_[i] = Nnz;
204  }
205 
206  SubComm_->SumAll(&NumMyNonzeros_, &NumGlobalNonzeros_, 1);
207  MaxNumEntries_ = ActualMaxNumEntries;
208 
209  int gpid = Matrix->Comm().MyPID();
210  Exporter_ = null;
211  Importer_ = null;
212 
213  if (!(RowMatrixRowMap().SameAs(OperatorRangeMap()))) {
214  try{
215  Exporter_ = rcp(new Epetra_Export(RowMatrixRowMap(), OperatorRangeMap()));
216  }
217  catch(...) {
218  printf("** * gpid %d: Ifpack_SubdomainFilter ctor: problem creating Exporter_ * **\n\n",gpid);
219  }
220  }
221 
222  if (!(*colMap_).SameAs(*Map_)) {
223  try{
224  Importer_ = rcp(new Epetra_Import(*colMap_, *Map_));
225  }
226  catch(...) {
227  printf("** * gpid %d: Ifpack_SubdomainFilter ctor: problem creating Importer_ * **\n\n",gpid);
228  }
229  }
230 
231 } //Ifpack_SubdomainFilter() ctor
232 
233 //==============================================================================
234 Ifpack_SubdomainFilter::~Ifpack_SubdomainFilter()
235 {
236  if(Ac_LIDMap_) delete [] Ac_LIDMap_;
237  if(Bc_LIDMap_) delete [] Bc_LIDMap_;
238  if(Ar_LIDMap_) delete [] Ar_LIDMap_;
239  if(Br_LIDMap_) delete [] Br_LIDMap_;
240 
241  if(ImportVector_) delete ImportVector_;
242 } //Ifpack_SubdomainFilter destructor
243 
244 //==============================================================================
245 int Ifpack_SubdomainFilter:: ExtractMyRowCopy(int MyRow, int Length, int & NumEntries,
246  double *Values, int * Indices) const
247 {
248  if ((MyRow < 0) || (MyRow >= NumMyRows_)) {
249  IFPACK_CHK_ERR(-1);
250  }
251 
252  assert(Length >= NumEntries_[MyRow]);
253 
254  int ierr;
255  if (ovA_) {
256  int LocRow = ovA_->A().RowMatrixRowMap().LID(Map_->GID(MyRow));
257  if (LocRow != -1) {
258  ierr = ovA_->A().ExtractMyRowCopy(LocRow, Length, NumEntries, Values, Indices);
259  for(int j = 0;j < NumEntries; j++) {
260  Indices[j] = Ac_LIDMap_[Indices[j]];
261  }
262  }
263  else {
264  LocRow = ovA_->B().RowMatrixRowMap().LID(Map_->GID(MyRow));
265  ierr = ovA_->B().ExtractMyRowCopy(LocRow, Length, NumEntries, Values, Indices);
266  for(int j = 0; j < NumEntries; j++) {
267  Indices[j] = Bc_LIDMap_[Indices[j]];
268  }
269  }
270  } else {
271  int Nnz = 0;
272  ierr = Matrix_->ExtractMyRowCopy(MyRow, MaxNumEntriesA_, Nnz, &Values_[0], &Indices_[0]);
273  IFPACK_CHK_ERR(ierr);
274 
275  NumEntries = 0;
276  for (int j = 0 ; j < Nnz ; ++j) {
277  int subdomainLocalIndex = colMap_->LID(Matrix_->RowMatrixColMap().GID(Indices_[j]));
278  if (subdomainLocalIndex != -1) {
279  Indices[NumEntries] = subdomainLocalIndex;
280  Values[NumEntries] = Values_[j];
281  NumEntries++;
282  }
283  }
284  }
285 
286  return(ierr);
287 }
288 
289 //==============================================================================
290 int Ifpack_SubdomainFilter::ExtractDiagonalCopy(Epetra_Vector & Diagonal) const
291 {
292  if (!Diagonal.Map().SameAs(*Map_)) {
293  IFPACK_CHK_ERR(-1);
294  }
295 
296  Diagonal = *Diagonal_;
297  return(0);
298 }
299 
300 //==============================================================================
301 int Ifpack_SubdomainFilter::Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
302 {
303  std::cout << "Ifpack_SubdomainFilter::Apply not implemented.\n";
304  IFPACK_CHK_ERR(-1); // not implemented
305 }
306 
307 //==============================================================================
308 void Ifpack_SubdomainFilter::UpdateImportVector(int NumVectors) const
309 {
310 }
311 
312 //==============================================================================
313 void Ifpack_SubdomainFilter::UpdateExportVector(int NumVectors) const
314 {
315 }
316 
317 //==============================================================================
318 int Ifpack_SubdomainFilter::ApplyInverse(const Epetra_MultiVector& X,
319  Epetra_MultiVector& Y) const
320 {
321  std::cout << "Ifpack_SubdomainFilter::ApplyInverse not implemented.\n";
322  IFPACK_CHK_ERR(-1); // not implemented
323 }
324 
325 //==============================================================================
326 const Epetra_BlockMap& Ifpack_SubdomainFilter::Map() const
327 {
328  return(*Map_);
329 }
330 
331 #endif //ifdef IFPACK_SUBCOMM_CODE
Ifpack_OverlappingRowMatrix: matrix with ghost rows, based on Epetra_RowMatrix.