IFPACK  Development
Ifpack_OverlappingRowMatrix.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_OverlappingRowMatrix.h"
45 #include "Epetra_RowMatrix.h"
46 #include "Epetra_Map.h"
47 #include "Epetra_CrsMatrix.h"
48 #include "Epetra_Comm.h"
49 #include "Epetra_MultiVector.h"
50 
51 #ifdef IFPACK_SUBCOMM_CODE
52 #include "Epetra_IntVector.h"
53 #include "Epetra_MpiComm.h"
54 #include "Teuchos_Hashtable.hpp"
55 #include "Teuchos_Array.hpp"
56 #include "EpetraExt_OperatorOut.h"
57 #include "EpetraExt_BlockMapOut.h"
58 #else
59 # ifdef IFPACK_NODE_AWARE_CODE
60 # include "Epetra_IntVector.h"
61 # include "Epetra_MpiComm.h"
62 # include "Teuchos_Hashtable.hpp"
63 # include "Teuchos_Array.hpp"
64 # include "EpetraExt_OperatorOut.h"
65 # include "EpetraExt_BlockMapOut.h"
66  extern int ML_NODE_ID;
67  int IFPACK_NODE_ID;
68 # endif
69 #endif
70 
71 using namespace Teuchos;
72 
73 // ======================================================================
74 // Constructor for the case of one core per subdomain
75 Ifpack_OverlappingRowMatrix::
76 Ifpack_OverlappingRowMatrix(const RCP<const Epetra_RowMatrix>& Matrix_in,
77  int OverlapLevel_in) :
78  Matrix_(Matrix_in),
79  OverlapLevel_(OverlapLevel_in)
80 {
81  // should not be here if no overlap
82  if (OverlapLevel_in == 0)
83  IFPACK_CHK_ERRV(-1);
84 
85  // nothing to do as well with one process
86  if (Comm().NumProc() == 1)
87  IFPACK_CHK_ERRV(-1);
88 
89  NumMyRowsA_ = A().NumMyRows();
90 
91  // construct the external matrix
92  vector<int> ExtElements;
93 
94  RCP<Epetra_Map> TmpMap;
95  RCP<Epetra_CrsMatrix> TmpMatrix;
96  RCP<Epetra_Import> TmpImporter;
97 
98  // importing rows corresponding to elements that are
99  // in ColMap, but not in RowMap
100  const Epetra_Map *RowMap;
101  const Epetra_Map *ColMap;
102 
103  for (int overlap = 0 ; overlap < OverlapLevel_in ; ++overlap) {
104  if (TmpMatrix != Teuchos::null) {
105  RowMap = &(TmpMatrix->RowMatrixRowMap());
106  ColMap = &(TmpMatrix->RowMatrixColMap());
107  }
108  else {
109  RowMap = &(A().RowMatrixRowMap());
110  ColMap = &(A().RowMatrixColMap());
111  }
112 
113  int size = ColMap->NumMyElements() - RowMap->NumMyElements();
114  vector<int> list(size);
115 
116  int count = 0;
117 
118  // define the set of rows that are in ColMap but not in RowMap
119  for (int i = 0 ; i < ColMap->NumMyElements() ; ++i) {
120  int GID = ColMap->GID(i);
121  if (A().RowMatrixRowMap().LID(GID) == -1) {
122  vector<int>::iterator pos
123  = find(ExtElements.begin(),ExtElements.end(),GID);
124  if (pos == ExtElements.end()) {
125  ExtElements.push_back(GID);
126  list[count] = GID;
127  ++count;
128  }
129  }
130  }
131 
132  const int *listptr = NULL;
133  if ( ! list.empty() ) listptr = &list[0];
134  TmpMap = rcp( new Epetra_Map(-1,count, listptr,0,Comm()) );
135 
136  TmpMatrix = rcp( new Epetra_CrsMatrix(Copy,*TmpMap,0) );
137 
138  TmpImporter = rcp( new Epetra_Import(*TmpMap,A().RowMatrixRowMap()) );
139 
140  TmpMatrix->Import(A(),*TmpImporter,Insert);
141  TmpMatrix->FillComplete(A().OperatorDomainMap(),*TmpMap);
142 
143  }
144 
145  // build the map containing all the nodes (original
146  // matrix + extended matrix)
147  vector<int> list(NumMyRowsA_ + ExtElements.size());
148  for (int i = 0 ; i < NumMyRowsA_ ; ++i)
149  list[i] = A().RowMatrixRowMap().GID(i);
150  for (int i = 0 ; i < (int)ExtElements.size() ; ++i)
151  list[i + NumMyRowsA_] = ExtElements[i];
152 
153  const int *listptr = NULL;
154  if ( ! list.empty() ) listptr = &list[0];
155  {
156  Map_ = rcp( new Epetra_Map(-1, NumMyRowsA_ + ExtElements.size(),
157  listptr, 0, Comm()) );
158  }
159 #ifdef IFPACK_SUBCOMM_CODE
160  colMap_ = &*Map_;
161 #else
162 # ifdef IFPACK_NODE_AWARE_CODE
163  colMap_ = &*Map_;
164 # endif
165 #endif
166  // now build the map corresponding to all the external nodes
167  // (with respect to A().RowMatrixRowMap().
168  {
169  const int * extelsptr = NULL;
170  if ( ! ExtElements.empty() ) extelsptr = &ExtElements[0];
171  ExtMap_ = rcp( new Epetra_Map(-1,ExtElements.size(),
172  extelsptr,0,A().Comm()) );
173  }
174  ExtMatrix_ = rcp( new Epetra_CrsMatrix(Copy,*ExtMap_,*Map_,0) );
175 
176  ExtImporter_ = rcp( new Epetra_Import(*ExtMap_,A().RowMatrixRowMap()) );
177  ExtMatrix_->Import(A(),*ExtImporter_,Insert);
178  ExtMatrix_->FillComplete(A().OperatorDomainMap(),*Map_);
179 
180  Importer_ = rcp( new Epetra_Import(*Map_,A().RowMatrixRowMap()) );
181 
182  // fix indices for overlapping matrix
183  NumMyRowsB_ = B().NumMyRows();
184  NumMyRows_ = NumMyRowsA_ + NumMyRowsB_;
185  NumMyCols_ = NumMyRows_;
186 
187  NumMyDiagonals_ = A().NumMyDiagonals() + B().NumMyDiagonals();
188 
189  NumMyNonzeros_ = A().NumMyNonzeros() + B().NumMyNonzeros();
190  long long NumMyNonzeros_tmp = NumMyNonzeros_;
191  Comm().SumAll(&NumMyNonzeros_tmp,&NumGlobalNonzeros_,1);
192  MaxNumEntries_ = A().MaxNumEntries();
193 
194  if (MaxNumEntries_ < B().MaxNumEntries())
195  MaxNumEntries_ = B().MaxNumEntries();
196 
197 }
198 
199 #ifdef IFPACK_SUBCOMM_CODE
200 
201 // ======================================================================
202 // Constructor for the case of two or more cores per subdomain
203 Ifpack_OverlappingRowMatrix::
204 Ifpack_OverlappingRowMatrix(const RCP<const Epetra_RowMatrix>& Matrix_in,
205  int OverlapLevel_in, int subdomainID) :
206  Matrix_(Matrix_in),
207  OverlapLevel_(OverlapLevel_in)
208 {
209 
210  //FIXME timing
211 #ifdef IFPACK_OVA_TIME_BUILD
212  double t0 = MPI_Wtime();
213  double t1, true_t0=t0;
214 #endif
215  //FIXME timing
216 
217  const int votesMax = INT_MAX;
218 
219  // should not be here if no overlap
220  if (OverlapLevel_in == 0)
221  IFPACK_CHK_ERRV(-1);
222 
223  // nothing to do as well with one process
224  if (Comm().NumProc() == 1)
225  IFPACK_CHK_ERRV(-1);
226 
227  // subdomainID is the node (aka socket) number, and so is system dependent
228  // nodeComm is the communicator for all the processes on a particular node
229  // these processes will have the same subdomainID.
230 # ifdef HAVE_MPI
231  const Epetra_MpiComm *pComm = dynamic_cast<const Epetra_MpiComm*>( &Comm() );
232  assert(pComm != NULL);
233  MPI_Comm nodeMPIComm;
234  MPI_Comm_split(pComm->Comm(),subdomainID,pComm->MyPID(),&nodeMPIComm);
235  Epetra_MpiComm *nodeComm = new Epetra_MpiComm(nodeMPIComm);
236 # else
237  Epetra_SerialComm *nodeComm = dynamic_cast<Epetra_MpiComm*>( &(Matrix_in->RowMatrixRowMap().Comm()) );
238 # endif
239 
240  NumMyRowsA_ = A().NumMyRows();
241 
242  RCP<Epetra_Map> TmpMap;
243  RCP<Epetra_CrsMatrix> TmpMatrix;
244  RCP<Epetra_Import> TmpImporter;
245 
246  // importing rows corresponding to elements that are
247  // in ColMap, but not in RowMap
248  const Epetra_Map *RowMap;
249  const Epetra_Map *ColMap;
250  const Epetra_Map *DomainMap;
251 
252  // TODO Count #connections from nodes I own to each ghost node
253 
254 
255  //FIXME timing
256 #ifdef IFPACK_OVA_TIME_BUILD
257  t1 = MPI_Wtime();
258  fprintf(stderr,"[node %d]: time for initialization %2.3e\n", subdomainID, t1-t0);
259  t0=t1;
260 #endif
261  //FIXME timing
262 
263 #ifdef IFPACK_OVA_TIME_BUILD
264  t1 = MPI_Wtime();
265  fprintf(stderr,"[node %d]: overlap hash table allocs %2.3e\n", subdomainID, t1-t0);
266  t0=t1;
267 #endif
268 
269  Teuchos::Hashtable<int,int> colMapTable(3 * A().RowMatrixColMap().NumMyElements() );
270 
271  // ghostTable holds off-node GIDs that are connected to on-node rows and can potentially be this PID's overlap
272  // TODO hopefully 3 times the # column entries is a reasonable table size
273  Teuchos::Hashtable<int,int> ghostTable(3 * A().RowMatrixColMap().NumMyElements() );
274 
275  /* ** ************************************************************************** ** */
276  /* ** ********************** start of main overlap loop ************************ ** */
277  /* ** ************************************************************************** ** */
278  for (int overlap = 0 ; overlap < OverlapLevel_in ; ++overlap)
279  {
280  // nbTable holds node buddy GIDs that are connected to current PID's rows, i.e., GIDs that should be in the overlapped
281  // matrix's column map
282 
283  if (TmpMatrix != Teuchos::null) {
284  RowMap = &(TmpMatrix->RowMatrixRowMap());
285  ColMap = &(TmpMatrix->RowMatrixColMap());
286  DomainMap = &(TmpMatrix->OperatorDomainMap());
287  }
288  else {
289  RowMap = &(A().RowMatrixRowMap());
290  ColMap = &(A().RowMatrixColMap());
291  DomainMap = &(A().OperatorDomainMap());
292  }
293 
294 #ifdef IFPACK_OVA_TIME_BUILD
295  t1 = MPI_Wtime();
296  fprintf(stderr,"[node %d]: overlap pointer pulls %2.3e\n", subdomainID, t1-t0);
297  t0=t1;
298 #endif
299 
300  // For each column ID, determine the owning node (as opposed to core)
301  // ID of the corresponding row.
302  Epetra_IntVector colIdList( *ColMap );
303  Epetra_IntVector rowIdList(*DomainMap);
304  rowIdList.PutValue(subdomainID);
305  Teuchos::RCP<Epetra_Import> nodeIdImporter = rcp(new Epetra_Import( *ColMap, *DomainMap ));
306 
307 #ifdef IFPACK_OVA_TIME_BUILD
308  t1 = MPI_Wtime();
309  fprintf(stderr,"[node %d]: overlap intvector/importer allocs %2.3e\n", subdomainID, t1-t0);
310  t0=t1;
311 #endif
312 
313  colIdList.Import(rowIdList,*nodeIdImporter,Insert);
314 
315  int size = ColMap->NumMyElements() - RowMap->NumMyElements();
316  int count = 0;
317 
318 #ifdef IFPACK_OVA_TIME_BUILD
319  t1 = MPI_Wtime();
320  fprintf(stderr,"[node %d]: overlap col/row ID imports %2.3e\n", subdomainID, t1-t0);
321  t0=t1;
322 #endif
323 
324 
325  // define the set of off-node rows that are in ColMap but not in RowMap
326  // This naturally does not include off-core rows that are on the same node as me, i.e., node buddy rows.
327  for (int i = 0 ; i < ColMap->NumMyElements() ; ++i) {
328  int GID = ColMap->GID(i);
329  int myvotes=0;
330  if (ghostTable.containsKey(GID)) myvotes = ghostTable.get(GID);
331  if ( colIdList[i] != subdomainID && myvotes < votesMax) // don't include anybody found in a previous round
332  {
333  int votes;
334  if (ghostTable.containsKey(GID)) {
335  votes = ghostTable.get(GID);
336  votes++;
337  ghostTable.put(GID,votes);
338  } else {
339  ghostTable.put(GID,1);
340  }
341  }
342  } //for (int i = 0 ; i < ColMap->NumMyElements() ; ++i)
343 
344  Teuchos::Array<int> gidsarray,votesarray;
345  ghostTable.arrayify(gidsarray,votesarray);
346  int *gids = gidsarray.getRawPtr();
347  int *votes = votesarray.getRawPtr();
348 
349  /*
350  This next bit of code decides which node buddy (NB) gets which ghost points. Everyone sends their
351  list of ghost points to pid 0 of the local subcommunicator. Pid 0 decides who gets what:
352 
353  - if a ghost point is touched by only one NB, that NB gets the ghost point
354  - if two or more NBs share a ghost point, whichever NB has the most connections to the ghost
355  point gets it.
356  */
357 
358 # ifdef HAVE_MPI //FIXME What if we build in serial?! This file will likely not compile.
359  int *cullList;
360  int ncull;
361  //int mypid = nodeComm->MyPID();
362 
363  //FIXME timing
364 #ifdef IFPACK_OVA_TIME_BUILD
365  t1 = MPI_Wtime();
366  fprintf(stderr,"[node %d]: overlap before culling %2.3e\n", subdomainID, t1-t0);
367  t0=t1;
368 #endif
369  //FIXME timing
370 
371 
372  if (nodeComm->MyPID() == 0)
373  {
374  // Figure out how much pid 0 is to receive
375  MPI_Status status;
376  int *lengths = new int[nodeComm->NumProc()+1];
377  lengths[0] = 0;
378  lengths[1] = ghostTable.size();
379  for (int i=1; i<nodeComm->NumProc(); i++) {
380  int leng;
381  MPI_Recv( &leng, 1, MPI_INT, i, MPI_ANY_TAG, nodeComm->Comm(), &status);
382  lengths[i+1] = lengths[i] + leng;
383  }
384  int total = lengths[nodeComm->NumProc()];
385 
386  int* ghosts = new int[total];
387  for (int i=0; i<total; i++) ghosts[i] = -9;
388  int *round = new int[total];
389  int *owningpid = new int[total];
390 
391  for (int i=1; i<nodeComm->NumProc(); i++) {
392  int count = lengths[i+1] - lengths[i];
393  MPI_Recv( ghosts+lengths[i], count, MPI_INT, i, MPI_ANY_TAG, nodeComm->Comm(), &status);
394  MPI_Recv( round+lengths[i], count, MPI_INT, i, MPI_ANY_TAG, nodeComm->Comm(), &status);
395  }
396 
397  // put in pid 0's info
398  for (int i=0; i<lengths[1]; i++) {
399  ghosts[i] = gids[i];
400  round[i] = votes[i];
401  owningpid[i] = 0;
402  }
403 
404  // put in the pid associated with each ghost
405  for (int j=1; j<nodeComm->NumProc(); j++) {
406  for (int i=lengths[j]; i<lengths[j+1]; i++) {
407  owningpid[i] = j;
408  }
409  }
410 
411  // sort everything based on the ghost gids
412  int* roundpid[2];
413  roundpid[0] = round; roundpid[1] = owningpid;
414  Epetra_Util epetraUtil;
415  epetraUtil.Sort(true,total,ghosts,0,0,2,roundpid);
416 
417  //set up arrays that get sent back to node buddies and that tell them what ghosts to cull
418  int *nlosers = new int[nodeComm->NumProc()];
419  int** losers = new int*[nodeComm->NumProc()];
420  for (int i=0; i<nodeComm->NumProc(); i++) {
421  nlosers[i] = 0;
422  losers[i] = new int[ lengths[i+1]-lengths[i] ];
423  }
424 
425  // Walk through ghosts array and and for each sequence of duplicate ghost GIDs, choose just one NB to keep it.
426  // The logic is pretty simple. The ghost list is sorted, so all duplicate PIDs are together.
427  // The list is traversed. As duplicates are found, node pid 0 keeps track of the current "winning"
428  // pid. When a pid is determined to have "lost" (less votes/connections to the current GID), the
429  // GID is added to that pid's list of GIDs to be culled. At the end of the repeated sequence, we have
430  // a winner, and other NBs know whether they need to delete it from their import list.
431  int max=0; //for duplicated ghosts, index of pid with most votes and who hence keeps the ghost.
432  // TODO to break ties randomly
433 
434  for (int i=1; i<total; i++) {
435  if (ghosts[i] == ghosts[i-1]) {
436  int idx = i; // pid associated with idx is current "loser"
437  if (round[i] > round[max]) {
438  idx = max;
439  max=i;
440  }
441  int j = owningpid[idx];
442  losers[j][nlosers[j]++] = ghosts[idx];
443  } else {
444  max=i;
445  }
446  } //for (int i=1; i<total; i++)
447 
448  delete [] round;
449  delete [] ghosts;
450  delete [] owningpid;
451 
452  // send the arrays of ghost GIDs to be culled back to the respective node buddies
453  for (int i=1; i<nodeComm->NumProc(); i++) {
454  MPI_Send( nlosers+i, 1, MPI_INT, i, 8675, nodeComm->Comm());
455  MPI_Send( losers[i], nlosers[i], MPI_INT, i, 8675, nodeComm->Comm());
456  }
457 
458  //FIXME Unnecessary memory allocation and copying, but makes culling code cleaner
459  //Could we stick this info into gids and votes, since neither is being used anymore?
460  //TODO Instead of using "losers" arrays, just use "cullList" as in the else clause
461  ncull = nlosers[0];
462  cullList = new int[ncull+1];
463  for (int i=0; i<nlosers[0]; i++)
464  cullList[i] = losers[0][i];
465 
466  for (int i=0; i<nodeComm->NumProc(); i++)
467  delete [] losers[i];
468 
469  delete [] lengths;
470  delete [] losers;
471  delete [] nlosers;
472 
473  } else { //everyone but pid 0
474 
475  // send to node pid 0 all ghosts that this pid could potentially import
476  int hashsize = ghostTable.size();
477  MPI_Send( &hashsize, 1, MPI_INT, 0, 8675, nodeComm->Comm());
478  MPI_Send( gids, hashsize, MPI_INT, 0, 8675, nodeComm->Comm());
479  MPI_Send( votes, hashsize, MPI_INT, 0, 8675, nodeComm->Comm());
480 
481  // receive the ghost GIDs that should not be imported (subset of the list sent off just above)
482  MPI_Status status;
483  MPI_Recv( &ncull, 1, MPI_INT, 0, 8675, nodeComm->Comm(), &status);
484  cullList = new int[ncull+1];
485  MPI_Recv( cullList, ncull, MPI_INT, 0, 8675, nodeComm->Comm(), &status);
486  }
487 
488 
489  //TODO clean out hash table after each time through overlap loop 4/1/07 JJH done moved both hash tables to local scope
490 
491  // Remove from my row map all off-node ghosts that will be imported by a node buddy.
492  for (int i=0; i<ncull; i++) {
493  try{ghostTable.remove(cullList[i]);}
494 
495  catch(...) {
496  printf("pid %d: In OverlappingRowMatrix ctr, problem removing ghost elt %d from ghostTable\n",
497  Comm().MyPID(),cullList[i]);
498  fflush(stdout);
499  }
500  }//for
501 
502  delete [] cullList;
503 
504  // Save off the remaining ghost GIDs from the current overlap round.
505  // These are off-node GIDs (rows) that I will import.
506  gidsarray.clear(); votesarray.clear();
507  ghostTable.arrayify(gidsarray,votesarray);
508 
509  vector<int> list(size);
510  count=0;
511  for (int i=0; i<ghostTable.size(); i++) {
512  // if votesarray[i] == votesmax, then this GID was found during a previous overlap round
513  if (votesarray[i] < votesMax) {
514  list[count] = gidsarray[i];
515  ghostTable.put(gidsarray[i],votesMax); //set this GID's vote to the maximum so that this PID alway wins in any subsequent overlap tiebreaking.
516  count++;
517  }
518  }
519 
520  //FIXME timing
521 #ifdef IFPACK_OVA_TIME_BUILD
522  t1 = MPI_Wtime();
523  fprintf(stderr,"[node %d]: overlap duplicate removal %2.3e\n", subdomainID, t1-t0);
524  t0=t1;
525 #endif
526  //FIXME timing
527 
528 # endif //ifdef HAVE_MPI
529 
530  TmpMap = rcp( new Epetra_Map(-1,count, &list[0],0,Comm()) );
531 
532  TmpMatrix = rcp( new Epetra_CrsMatrix(Copy,*TmpMap,0) );
533 
534  TmpImporter = rcp( new Epetra_Import(*TmpMap,A().RowMatrixRowMap()) );
535 
536  TmpMatrix->Import(A(),*TmpImporter,Insert);
537  TmpMatrix->FillComplete(A().OperatorDomainMap(),*TmpMap);
538 
539  //FIXME timing
540 #ifdef IFPACK_OVA_TIME_BUILD
541  t1 = MPI_Wtime();
542  fprintf(stderr,"[node %d]: overlap TmpMatrix fillcomplete %2.3e\n", subdomainID, t1-t0);
543  t0=t1;
544 #endif
545  //FIXME timing
546 
547 
548  // These next two imports get the GIDs that need to go into the column map of the overlapped matrix.
549 
550  // For each column ID in the overlap, determine the owning node (as opposed to core)
551  // ID of the corresponding row. Save those column IDs whose owning node is the current one.
552  // This should get all the imported ghost GIDs.
553  Epetra_IntVector ov_colIdList( TmpMatrix->ColMap() );
554  ov_colIdList.PutValue(-1);
555  Epetra_IntVector ov_rowIdList( TmpMatrix->RowMap() );
556  ov_rowIdList.PutValue(subdomainID);
557  Teuchos::RCP<Epetra_Import> ov_nodeIdImporter = rcp(new Epetra_Import( TmpMatrix->ColMap(), TmpMatrix->RowMap()));
558  ov_colIdList.Import(ov_rowIdList,*ov_nodeIdImporter,Insert);
559 
560  for (int i=0 ; i<ov_colIdList.MyLength(); i++) {
561  if (ov_colIdList[i] == subdomainID) {
562  int GID = (ov_colIdList.Map()).GID(i);
563  colMapTable.put(GID,1);
564  }
565  }
566 
567  // Do a second import of the owning node ID from A's rowmap to TmpMat's column map. This ensures that
568  // all GIDs that belong to a node buddy and are in a ghost row's sparsity pattern will be in the final
569  // overlapped matrix's column map.
570  ov_colIdList.PutValue(-1);
571  Epetra_IntVector ArowIdList( A().RowMatrixRowMap() );
572  ArowIdList.PutValue(subdomainID);
573  nodeIdImporter = rcp(new Epetra_Import( TmpMatrix->ColMap(), A().RowMatrixRowMap() ));
574  ov_colIdList.Import(ArowIdList,*nodeIdImporter,Insert);
575 
576  for (int i=0 ; i<ov_colIdList.MyLength(); i++) {
577  if (ov_colIdList[i] == subdomainID) {
578  int GID = (ov_colIdList.Map()).GID(i);
579  colMapTable.put(GID,1);
580  }
581  }
582 
583  //FIXME timing
584 #ifdef IFPACK_OVA_TIME_BUILD
585  t1 = MPI_Wtime();
586  fprintf(stderr,"[node %d]: overlap 2 imports to fix up colmap %2.3e\n", subdomainID, t1-t0);
587  t0=t1;
588 #endif
589  //FIXME timing
590 
591  } //for (int overlap = 0 ; overlap < OverlapLevel_in ; ++overlap)
592 
593  /* ** ************************************************************************ ** */
594  /* ** ********************** end of main overlap loop ************************ ** */
595  /* ** ************************************************************************ ** */
596 
597  // off-node GIDs that will be in the overlap
598  vector<int> ghostElements;
599 
600  Teuchos::Array<int> gidsarray,votesarray;
601  ghostTable.arrayify(gidsarray,votesarray);
602  for (int i=0; i<ghostTable.size(); i++) {
603  ghostElements.push_back(gidsarray[i]);
604  }
605 
606  for (int i = 0 ; i < A().RowMatrixColMap().NumMyElements() ; ++i) {
607  int GID = A().RowMatrixColMap().GID(i);
608  // Remove any entries that are in A's original column map
609  if (colMapTable.containsKey(GID)) {
610  try{colMapTable.remove(GID);}
611  catch(...) {
612  printf("pid %d: In OverlappingRowMatrix ctr, problem removing entry %d from colMapTable\n", Comm().MyPID(),GID);
613  fflush(stdout);
614  }
615  }
616  }
617 
618  // GIDs that will be in the overlapped matrix's column map
619  vector<int> colMapElements;
620 
621  gidsarray.clear(); votesarray.clear();
622  colMapTable.arrayify(gidsarray,votesarray);
623  for (int i=0; i<colMapTable.size(); i++)
624  colMapElements.push_back(gidsarray[i]);
625 
626 /*
627  We need two maps here. The first is the row map, which we've got by using my original rows
628  plus whatever I've picked up in ghostElements.
629 
630  The second is a column map. This map should include my rows, plus any columns that could come from node buddies.
631  These GIDs come from the std:array colMapElements, which in turn comes from colMapTable.
632  This map should *not* omit ghosts that have been imported by my node buddies, i.e., for any row that I own,
633  the stencil should include all column GIDs (including imported ghosts) that are on the node.
634 */
635 
636  // build the row map containing all the nodes (original matrix + off-node matrix)
637  vector<int> rowList(NumMyRowsA_ + ghostElements.size());
638  for (int i = 0 ; i < NumMyRowsA_ ; ++i)
639  rowList[i] = A().RowMatrixRowMap().GID(i);
640  for (int i = 0 ; i < (int)ghostElements.size() ; ++i)
641  rowList[i + NumMyRowsA_] = ghostElements[i];
642 
643  // row map for the overlapped matrix (local + overlap)
644  Map_ = rcp( new Epetra_Map(-1, NumMyRowsA_ + ghostElements.size(), &rowList[0], 0, Comm()) );
645 
646  // build the column map for the overlapping matrix
647  //vector<int> colList(colMapElements.size());
648  // column map for the overlapped matrix (local + overlap)
649  //colMap_ = rcp( new Epetra_Map(-1, colMapElements.size(), &colList[0], 0, Comm()) );
650  //for (int i = 0 ; i < (int)colMapElements.size() ; i++)
651  // colList[i] = colMapElements[i];
652  vector<int> colList(A().RowMatrixColMap().NumMyElements() + colMapElements.size());
653  int nc = A().RowMatrixColMap().NumMyElements();
654  for (int i = 0 ; i < nc; i++)
655  colList[i] = A().RowMatrixColMap().GID(i);
656  for (int i = 0 ; i < (int)colMapElements.size() ; i++)
657  colList[nc+i] = colMapElements[i];
658 
659  // column map for the overlapped matrix (local + overlap)
660  //colMap_ = rcp( new Epetra_Map(-1, A().RowMatrixColMap().NumMyElements() + colMapElements.size(), &colList[0], 0, Comm()) );
661  colMap_ = new Epetra_Map(-1, A().RowMatrixColMap().NumMyElements() + colMapElements.size(), &colList[0], 0, Comm());
662 
663 
664  //FIXME timing
665 #ifdef IFPACK_OVA_TIME_BUILD
666  t1 = MPI_Wtime();
667  fprintf(stderr,"[node %d]: time to init B() col/row maps %2.3e\n", subdomainID, t1-t0);
668  t0=t1;
669 #endif
670  //FIXME timing
671 
672  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
673  //++++ start of sort
674  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
675  // build the column map, but don't use a copy constructor, b/c local communicator SubComm_ is
676  // different from that of Matrix.
677  try {
678  // build row map based on the local communicator. We need this temporarily to build the column map.
679  Epetra_Map* nodeMap = new Epetra_Map(-1,NumMyRowsA_ + ghostElements.size(),&rowList[0],0,*nodeComm);
680  int numMyElts = colMap_->NumMyElements();
681  assert(numMyElts!=0);
682 
683  // The column map *must* be sorted: first locals, then ghosts. The ghosts
684  // must be further sorted so that they are contiguous by owning processor.
685 
686  // For each GID in column map, determine owning PID in local communicator.
687  int* myGlobalElts = new int[numMyElts];
688  colMap_->MyGlobalElements(myGlobalElts);
689  int* pidList = new int[numMyElts];
690  nodeMap->RemoteIDList(numMyElts, myGlobalElts, pidList, 0);
691 
692  // First sort column map GIDs based on the owning PID in local communicator.
693  Epetra_Util Util;
694  int *tt[1];
695  tt[0] = myGlobalElts;
696  Util.Sort(true, numMyElts, pidList, 0, (double**)0, 1, tt);
697 
698  // For each remote PID, sort the column map GIDs in ascending order.
699  // Don't sort the local PID's entries.
700  int localStart=0;
701  while (localStart<numMyElts) {
702  int currPID = (pidList)[localStart];
703  int i=localStart;
704  while (i<numMyElts && (pidList)[i] == currPID) i++;
705  if (currPID != nodeComm->MyPID())
706  Util.Sort(true, i-localStart, (myGlobalElts)+localStart, 0, 0, 0, 0);
707  localStart = i;
708  }
709 
710  // now move the local entries to the front of the list
711  localStart=0;
712  while (localStart<numMyElts && (pidList)[localStart] != nodeComm->MyPID()) localStart++;
713  assert(localStart != numMyElts);
714  int localEnd=localStart;
715  while (localEnd<numMyElts && (pidList)[localEnd] == nodeComm->MyPID()) localEnd++;
716  int* mySortedGlobalElts = new int[numMyElts];
717  int leng = localEnd - localStart;
718  /* This is a little gotcha. It appears that the ordering of the column map's local entries
719  must be the same as that of the domain map's local entries. See the comment in method
720  MakeColMap() in Epetra_CrsGraph.cpp, line 1072. */
721  int *rowGlobalElts = nodeMap->MyGlobalElements();
722 
723  /*TODO TODO TODO NTS rows 68 and 83 show up as local GIDs in rowGlobalElts for both pids 0 & 1 on node 0.
724  This seems to imply that there is something wrong with rowList!!
725  It is ok for the overlapped matrix's row map to have repeated entries (it's overlapped, after all).
726  But ... on a node, there should be no repeats.
727  Ok, here's the underlying cause. On node 0, gpid 1 finds 83 on overlap round 0. gpid 0 finds 83
728  on overlap round 1. This shouldn't happen .. once a node buddy "owns" a row, no other node buddy
729  should ever import ("own") that row.
730 
731  Here's a possible fix. Extend tie breaking across overlap rounds. This means sending to lpid 0
732  a list of *all* rows you've imported (this round plus previous rounds) for tie-breaking
733  If a nb won a ghost row in a previous round, his votes for that ghost row should be really high, i.e.,
734  he should probably win that ghost row forever.
735  7/17/09
736 
737  7/28/09 JJH I believe I've fixed this, but I thought it might be helpful to have this comment in here, in case I missed something.
738  */
739 
740  //move locals to front of list
741  for (int i=0; i<leng; i++) {
742  /* //original code */
743  (mySortedGlobalElts)[i] = rowGlobalElts[i];
744  //(&*mySortedGlobalElts)[i] = (&*myGlobalElts)[localStart+i];
745  //(&*mySortedPidList)[i] = (&*pidList)[localStart+i];
746  }
747  for (int i=leng; i< localEnd; i++) {
748  (mySortedGlobalElts)[i] = (myGlobalElts)[i-leng];
749  }
750  for (int i=localEnd; i<numMyElts; i++) {
751  (mySortedGlobalElts)[i] = (myGlobalElts)[i];
752  }
753 
754  //FIXME timing
755 #ifdef IFPACK_OVA_TIME_BUILD
756  t1 = MPI_Wtime();
757  fprintf(stderr,"[node %d]: time to sort col map arrays (cp=1) %2.3e\n", subdomainID, t1-t0);
758  t0=t1;
759 #endif
760  //FIXME timing
761 
762  int indexBase = colMap_->IndexBase();
763  if (colMap_) delete colMap_;
764  colMap_ = new Epetra_Map(-1,numMyElts,mySortedGlobalElts,indexBase,Comm());
765 
766  delete nodeMap;
767  delete [] myGlobalElts;
768  delete [] pidList;
769  delete [] mySortedGlobalElts;
770 
771  } //try
772  catch(...) {
773  printf("** * Ifpack_OverlappingRowmatrix ctor: problem creating column map * **\n\n");
774  }
775 
776  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
777  //++++ end of sort
778  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
779 
780  //FIXME timing
781 #ifdef IFPACK_OVA_TIME_BUILD
782  t1 = MPI_Wtime();
783  fprintf(stderr,"[node %d]: time to sort col map arrays (cp=2) %2.3e\n", subdomainID, t1-t0);
784  t0=t1;
785 #endif
786  //FIXME timing
787 
788 
789 /*
790 
791  FIXME
792  Does the column map need to be sorted for the overlapping matrix?
793 
794  The column map *must* be sorted:
795 
796  first locals
797  then ghosts
798 
799  The ghosts must be further sorted so that they are contiguous by owning processor
800 
801  int* RemoteSizeList = 0
802  int* RemoteColIndices = ColIndices.Values() + NumLocalColGIDs; // Points to back end of ColIndices
803 
804  EPETRA_CHK_ERR(DomainMap.RemoteIDList(NumRemoteColGIDs, RemoteColIndices, PIDList.Values(), 0, RemoteSizeList));
805  Epetra_Util epetraUtil;
806  SortLists[0] = RemoteColIndices;
807  SortLists[1] = RemoteSizeList;
808  epetraUtil.Sort(true, NumRemoteColGIDs, PIDList.Values(), 0, 0, NLists, SortLists);
809 */
810 
811  // now build the map corresponding to all the external nodes
812  // (with respect to A().RowMatrixRowMap().
813  ExtMap_ = rcp( new Epetra_Map(-1,ghostElements.size(), &ghostElements[0],0,Comm()) );
814  ExtMatrix_ = rcp( new Epetra_CrsMatrix(Copy,*ExtMap_,*colMap_,0) );
815 
816  ExtImporter_ = rcp( new Epetra_Import(*ExtMap_,A().RowMatrixRowMap()) );
817  ExtMatrix_->Import(A(),*ExtImporter_,Insert);
818 
819  //FIXME timing
820 #ifdef IFPACK_OVA_TIME_BUILD
821  t1 = MPI_Wtime();
822  fprintf(stderr,"[node %d]: time to import and setup ExtMap_ %2.3e\n", subdomainID, t1-t0);
823  t0=t1;
824 #endif
825  //FIXME timing
826 
827 
828 /*
829  Notes to self: In FillComplete, the range map does not have to be 1-1 as long as
830  (row map == range map). Ditto for the domain map not being 1-1
831  if (col map == domain map).
832 
833 */
834 
835  ExtMatrix_->FillComplete( *colMap_ , *Map_ ); //FIXME wrong
836 
837  //FIXME timing
838 #ifdef IFPACK_OVA_TIME_BUILD
839  t1 = MPI_Wtime();
840  fprintf(stderr,"[node %d]: time to FillComplete B() %2.3e\n", subdomainID, t1-t0);
841  t0=t1;
842 #endif
843  //FIXME timing
844 
845 
846  // Note: B() = *ExtMatrix_ .
847 
848  Importer_ = rcp( new Epetra_Import(*Map_,A().RowMatrixRowMap()) ); //FIXME is this right?!
849 
850  // fix indices for overlapping matrix
851  NumMyRowsB_ = B().NumMyRows();
852  NumMyRows_ = NumMyRowsA_ + NumMyRowsB_; //TODO is this wrong for a subdomain on >1 processor? // should be ok
853  //NumMyCols_ = NumMyRows_; //TODO is this wrong for a subdomain on >1 processor? // YES!!!
854  //NumMyCols_ = A().NumMyCols() + B().NumMyCols();
855  NumMyCols_ = B().NumMyCols();
856 
857  /*FIXME*/ //somehow B's NumMyCols is the entire subdomain (local + overlap)
858 
859  NumMyDiagonals_ = A().NumMyDiagonals() + B().NumMyDiagonals();
860 
861  NumMyNonzeros_ = A().NumMyNonzeros() + B().NumMyNonzeros();
862  long long NumMyNonZerosTemp = NumMyNonzeros_;
863  Comm().SumAll(&NumMyNonZerosTemp,&NumGlobalNonzeros_,1);
864  MaxNumEntries_ = A().MaxNumEntries();
865 
866  if (MaxNumEntries_ < B().MaxNumEntries())
867  MaxNumEntries_ = B().MaxNumEntries();
868 
869 # ifdef HAVE_MPI
870  delete nodeComm;
871 # endif
872 
873  //FIXME timing
874 #ifdef IFPACK_OVA_TIME_BUILD
875  t1 = MPI_Wtime();
876  fprintf(stderr,"[node %d]: time for final calcs %2.3e\n", subdomainID, t1-t0);
877  fprintf(stderr,"[node %d]: total IORM ctor time %2.3e\n", subdomainID, t1-true_t0);
878 #endif
879  //FIXME timing
880 
881 
882 } //Ifpack_OverlappingRowMatrix() ctor for more than one core
883 
884 #else
885 # ifdef IFPACK_NODE_AWARE_CODE
886 
887 // ======================================================================
888 // Constructor for the case of two or more cores per subdomain
889 Ifpack_OverlappingRowMatrix::
890 Ifpack_OverlappingRowMatrix(const RCP<const Epetra_RowMatrix>& Matrix_in,
891  int OverlapLevel_in, int nodeID) :
892  Matrix_(Matrix_in),
893  OverlapLevel_(OverlapLevel_in)
894 {
895 
896  //FIXME timing
897 #ifdef IFPACK_OVA_TIME_BUILD
898  double t0 = MPI_Wtime();
899  double t1, true_t0=t0;
900 #endif
901  //FIXME timing
902 
903  const int votesMax = INT_MAX;
904 
905  // should not be here if no overlap
906  if (OverlapLevel_in == 0)
907  IFPACK_CHK_ERRV(-1);
908 
909  // nothing to do as well with one process
910  if (Comm().NumProc() == 1)
911  IFPACK_CHK_ERRV(-1);
912 
913  // nodeID is the node (aka socket) number, and so is system dependent
914  // nodeComm is the communicator for all the processes on a particular node
915  // these processes will have the same nodeID.
916 # ifdef HAVE_MPI
917  const Epetra_MpiComm *pComm = dynamic_cast<const Epetra_MpiComm*>( &Comm() );
918  assert(pComm != NULL);
919  MPI_Comm nodeMPIComm;
920  MPI_Comm_split(pComm->Comm(),nodeID,pComm->MyPID(),&nodeMPIComm);
921  Epetra_MpiComm *nodeComm = new Epetra_MpiComm(nodeMPIComm);
922 # else
923  Epetra_SerialComm *nodeComm = dynamic_cast<Epetra_MpiComm*>( &(Matrix_in->RowMatrixRowMap().Comm()) );
924 # endif
925 
926  NumMyRowsA_ = A().NumMyRows();
927 
928  RCP<Epetra_Map> TmpMap;
929  RCP<Epetra_CrsMatrix> TmpMatrix;
930  RCP<Epetra_Import> TmpImporter;
931 
932  // importing rows corresponding to elements that are
933  // in ColMap, but not in RowMap
934  const Epetra_Map *RowMap;
935  const Epetra_Map *ColMap;
936  const Epetra_Map *DomainMap;
937 
938  // TODO Count #connections from nodes I own to each ghost node
939 
940 
941  //FIXME timing
942 #ifdef IFPACK_OVA_TIME_BUILD
943  t1 = MPI_Wtime();
944  fprintf(stderr,"[node %d]: time for initialization %2.3e\n", nodeID, t1-t0);
945  t0=t1;
946 #endif
947  //FIXME timing
948 
949 #ifdef IFPACK_OVA_TIME_BUILD
950  t1 = MPI_Wtime();
951  fprintf(stderr,"[node %d]: overlap hash table allocs %2.3e\n", nodeID, t1-t0);
952  t0=t1;
953 #endif
954 
955  Teuchos::Hashtable<int,int> colMapTable(3 * A().RowMatrixColMap().NumMyElements() );
956 
957  // ghostTable holds off-node GIDs that are connected to on-node rows and can potentially be this PID's overlap
958  // TODO hopefully 3 times the # column entries is a reasonable table size
959  Teuchos::Hashtable<int,int> ghostTable(3 * A().RowMatrixColMap().NumMyElements() );
960 
961  /* ** ************************************************************************** ** */
962  /* ** ********************** start of main overlap loop ************************ ** */
963  /* ** ************************************************************************** ** */
964  for (int overlap = 0 ; overlap < OverlapLevel_in ; ++overlap)
965  {
966  // nbTable holds node buddy GIDs that are connected to current PID's rows, i.e., GIDs that should be in the overlapped
967  // matrix's column map
968 
969  if (TmpMatrix != Teuchos::null) {
970  RowMap = &(TmpMatrix->RowMatrixRowMap());
971  ColMap = &(TmpMatrix->RowMatrixColMap());
972  DomainMap = &(TmpMatrix->OperatorDomainMap());
973  }
974  else {
975  RowMap = &(A().RowMatrixRowMap());
976  ColMap = &(A().RowMatrixColMap());
977  DomainMap = &(A().OperatorDomainMap());
978  }
979 
980 #ifdef IFPACK_OVA_TIME_BUILD
981  t1 = MPI_Wtime();
982  fprintf(stderr,"[node %d]: overlap pointer pulls %2.3e\n", nodeID, t1-t0);
983  t0=t1;
984 #endif
985 
986  // For each column ID, determine the owning node (as opposed to core)
987  // ID of the corresponding row.
988  Epetra_IntVector colIdList( *ColMap );
989  Epetra_IntVector rowIdList(*DomainMap);
990  rowIdList.PutValue(nodeID);
991  Teuchos::RCP<Epetra_Import> nodeIdImporter = rcp(new Epetra_Import( *ColMap, *DomainMap ));
992 
993 #ifdef IFPACK_OVA_TIME_BUILD
994  t1 = MPI_Wtime();
995  fprintf(stderr,"[node %d]: overlap intvector/importer allocs %2.3e\n", nodeID, t1-t0);
996  t0=t1;
997 #endif
998 
999  colIdList.Import(rowIdList,*nodeIdImporter,Insert);
1000 
1001  int size = ColMap->NumMyElements() - RowMap->NumMyElements();
1002  int count = 0;
1003 
1004 #ifdef IFPACK_OVA_TIME_BUILD
1005  t1 = MPI_Wtime();
1006  fprintf(stderr,"[node %d]: overlap col/row ID imports %2.3e\n", nodeID, t1-t0);
1007  t0=t1;
1008 #endif
1009 
1010 
1011  // define the set of off-node rows that are in ColMap but not in RowMap
1012  // This naturally does not include off-core rows that are on the same node as me, i.e., node buddy rows.
1013  for (int i = 0 ; i < ColMap->NumMyElements() ; ++i) {
1014  int GID = ColMap->GID(i);
1015  int myvotes=0;
1016  if (ghostTable.containsKey(GID)) myvotes = ghostTable.get(GID);
1017  if ( colIdList[i] != nodeID && myvotes < votesMax) // don't include anybody found in a previous round
1018  {
1019  int votes;
1020  if (ghostTable.containsKey(GID)) {
1021  votes = ghostTable.get(GID);
1022  votes++;
1023  ghostTable.put(GID,votes);
1024  } else {
1025  ghostTable.put(GID,1);
1026  }
1027  }
1028  } //for (int i = 0 ; i < ColMap->NumMyElements() ; ++i)
1029 
1030  Teuchos::Array<int> gidsarray,votesarray;
1031  ghostTable.arrayify(gidsarray,votesarray);
1032  int *gids = gidsarray.getRawPtr();
1033  int *votes = votesarray.getRawPtr();
1034 
1035  /*
1036  This next bit of code decides which node buddy (NB) gets which ghost points. Everyone sends their
1037  list of ghost points to pid 0 of the local subcommunicator. Pid 0 decides who gets what:
1038 
1039  - if a ghost point is touched by only one NB, that NB gets the ghost point
1040  - if two or more NBs share a ghost point, whichever NB has the most connections to the ghost
1041  point gets it.
1042  */
1043 
1044 # ifdef HAVE_MPI //FIXME What if we build in serial?! This file will likely not compile.
1045  int *cullList;
1046  int ncull;
1047  //int mypid = nodeComm->MyPID();
1048 
1049  //FIXME timing
1050 #ifdef IFPACK_OVA_TIME_BUILD
1051  t1 = MPI_Wtime();
1052  fprintf(stderr,"[node %d]: overlap before culling %2.3e\n", nodeID, t1-t0);
1053  t0=t1;
1054 #endif
1055  //FIXME timing
1056 
1057 
1058  if (nodeComm->MyPID() == 0)
1059  {
1060  // Figure out how much pid 0 is to receive
1061  MPI_Status status;
1062  int *lengths = new int[nodeComm->NumProc()+1];
1063  lengths[0] = 0;
1064  lengths[1] = ghostTable.size();
1065  for (int i=1; i<nodeComm->NumProc(); i++) {
1066  int leng;
1067  MPI_Recv( &leng, 1, MPI_INT, i, MPI_ANY_TAG, nodeComm->Comm(), &status);
1068  lengths[i+1] = lengths[i] + leng;
1069  }
1070  int total = lengths[nodeComm->NumProc()];
1071 
1072  int* ghosts = new int[total];
1073  for (int i=0; i<total; i++) ghosts[i] = -9;
1074  int *round = new int[total];
1075  int *owningpid = new int[total];
1076 
1077  for (int i=1; i<nodeComm->NumProc(); i++) {
1078  int count = lengths[i+1] - lengths[i];
1079  MPI_Recv( ghosts+lengths[i], count, MPI_INT, i, MPI_ANY_TAG, nodeComm->Comm(), &status);
1080  MPI_Recv( round+lengths[i], count, MPI_INT, i, MPI_ANY_TAG, nodeComm->Comm(), &status);
1081  }
1082 
1083  // put in pid 0's info
1084  for (int i=0; i<lengths[1]; i++) {
1085  ghosts[i] = gids[i];
1086  round[i] = votes[i];
1087  owningpid[i] = 0;
1088  }
1089 
1090  // put in the pid associated with each ghost
1091  for (int j=1; j<nodeComm->NumProc(); j++) {
1092  for (int i=lengths[j]; i<lengths[j+1]; i++) {
1093  owningpid[i] = j;
1094  }
1095  }
1096 
1097  // sort everything based on the ghost gids
1098  int* roundpid[2];
1099  roundpid[0] = round; roundpid[1] = owningpid;
1100  Epetra_Util epetraUtil;
1101  epetraUtil.Sort(true,total,ghosts,0,0,2,roundpid);
1102 
1103  //set up arrays that get sent back to node buddies and that tell them what ghosts to cull
1104  int *nlosers = new int[nodeComm->NumProc()];
1105  int** losers = new int*[nodeComm->NumProc()];
1106  for (int i=0; i<nodeComm->NumProc(); i++) {
1107  nlosers[i] = 0;
1108  losers[i] = new int[ lengths[i+1]-lengths[i] ];
1109  }
1110 
1111  // Walk through ghosts array and and for each sequence of duplicate ghost GIDs, choose just one NB to keep it.
1112  // The logic is pretty simple. The ghost list is sorted, so all duplicate PIDs are together.
1113  // The list is traversed. As duplicates are found, node pid 0 keeps track of the current "winning"
1114  // pid. When a pid is determined to have "lost" (less votes/connections to the current GID), the
1115  // GID is added to that pid's list of GIDs to be culled. At the end of the repeated sequence, we have
1116  // a winner, and other NBs know whether they need to delete it from their import list.
1117  int max=0; //for duplicated ghosts, index of pid with most votes and who hence keeps the ghost.
1118  // TODO to break ties randomly
1119 
1120  for (int i=1; i<total; i++) {
1121  if (ghosts[i] == ghosts[i-1]) {
1122  int idx = i; // pid associated with idx is current "loser"
1123  if (round[i] > round[max]) {
1124  idx = max;
1125  max=i;
1126  }
1127  int j = owningpid[idx];
1128  losers[j][nlosers[j]++] = ghosts[idx];
1129  } else {
1130  max=i;
1131  }
1132  } //for (int i=1; i<total; i++)
1133 
1134  delete [] round;
1135  delete [] ghosts;
1136  delete [] owningpid;
1137 
1138  // send the arrays of ghost GIDs to be culled back to the respective node buddies
1139  for (int i=1; i<nodeComm->NumProc(); i++) {
1140  MPI_Send( nlosers+i, 1, MPI_INT, i, 8675, nodeComm->Comm());
1141  MPI_Send( losers[i], nlosers[i], MPI_INT, i, 8675, nodeComm->Comm());
1142  }
1143 
1144  //FIXME Unnecessary memory allocation and copying, but makes culling code cleaner
1145  //Could we stick this info into gids and votes, since neither is being used anymore?
1146  //TODO Instead of using "losers" arrays, just use "cullList" as in the else clause
1147  ncull = nlosers[0];
1148  cullList = new int[ncull+1];
1149  for (int i=0; i<nlosers[0]; i++)
1150  cullList[i] = losers[0][i];
1151 
1152  for (int i=0; i<nodeComm->NumProc(); i++)
1153  delete [] losers[i];
1154 
1155  delete [] lengths;
1156  delete [] losers;
1157  delete [] nlosers;
1158 
1159  } else { //everyone but pid 0
1160 
1161  // send to node pid 0 all ghosts that this pid could potentially import
1162  int hashsize = ghostTable.size();
1163  MPI_Send( &hashsize, 1, MPI_INT, 0, 8675, nodeComm->Comm());
1164  MPI_Send( gids, hashsize, MPI_INT, 0, 8675, nodeComm->Comm());
1165  MPI_Send( votes, hashsize, MPI_INT, 0, 8675, nodeComm->Comm());
1166 
1167  // receive the ghost GIDs that should not be imported (subset of the list sent off just above)
1168  MPI_Status status;
1169  MPI_Recv( &ncull, 1, MPI_INT, 0, 8675, nodeComm->Comm(), &status);
1170  cullList = new int[ncull+1];
1171  MPI_Recv( cullList, ncull, MPI_INT, 0, 8675, nodeComm->Comm(), &status);
1172  }
1173 
1174 
1175  //TODO clean out hash table after each time through overlap loop 4/1/07 JJH done moved both hash tables to local scope
1176 
1177  // Remove from my row map all off-node ghosts that will be imported by a node buddy.
1178  for (int i=0; i<ncull; i++) {
1179  try{ghostTable.remove(cullList[i]);}
1180 
1181  catch(...) {
1182  printf("pid %d: In OverlappingRowMatrix ctr, problem removing ghost elt %d from ghostTable\n",
1183  Comm().MyPID(),cullList[i]);
1184  fflush(stdout);
1185  }
1186  }//for
1187 
1188  delete [] cullList;
1189 
1190  // Save off the remaining ghost GIDs from the current overlap round.
1191  // These are off-node GIDs (rows) that I will import.
1192  gidsarray.clear(); votesarray.clear();
1193  ghostTable.arrayify(gidsarray,votesarray);
1194 
1195  vector<int> list(size);
1196  count=0;
1197  for (int i=0; i<ghostTable.size(); i++) {
1198  // if votesarray[i] == votesmax, then this GID was found during a previous overlap round
1199  if (votesarray[i] < votesMax) {
1200  list[count] = gidsarray[i];
1201  ghostTable.put(gidsarray[i],votesMax); //set this GID's vote to the maximum so that this PID alway wins in any subsequent overlap tiebreaking.
1202  count++;
1203  }
1204  }
1205 
1206  //FIXME timing
1207 #ifdef IFPACK_OVA_TIME_BUILD
1208  t1 = MPI_Wtime();
1209  fprintf(stderr,"[node %d]: overlap duplicate removal %2.3e\n", nodeID, t1-t0);
1210  t0=t1;
1211 #endif
1212  //FIXME timing
1213 
1214 # endif //ifdef HAVE_MPI
1215 
1216  TmpMap = rcp( new Epetra_Map(-1,count, &list[0],0,Comm()) );
1217 
1218  TmpMatrix = rcp( new Epetra_CrsMatrix(Copy,*TmpMap,0) );
1219 
1220  TmpImporter = rcp( new Epetra_Import(*TmpMap,A().RowMatrixRowMap()) );
1221 
1222  TmpMatrix->Import(A(),*TmpImporter,Insert);
1223  TmpMatrix->FillComplete(A().OperatorDomainMap(),*TmpMap);
1224 
1225  //FIXME timing
1226 #ifdef IFPACK_OVA_TIME_BUILD
1227  t1 = MPI_Wtime();
1228  fprintf(stderr,"[node %d]: overlap TmpMatrix fillcomplete %2.3e\n", nodeID, t1-t0);
1229  t0=t1;
1230 #endif
1231  //FIXME timing
1232 
1233 
1234  // These next two imports get the GIDs that need to go into the column map of the overlapped matrix.
1235 
1236  // For each column ID in the overlap, determine the owning node (as opposed to core)
1237  // ID of the corresponding row. Save those column IDs whose owning node is the current one.
1238  // This should get all the imported ghost GIDs.
1239  Epetra_IntVector ov_colIdList( TmpMatrix->ColMap() );
1240  ov_colIdList.PutValue(-1);
1241  Epetra_IntVector ov_rowIdList( TmpMatrix->RowMap() );
1242  ov_rowIdList.PutValue(nodeID);
1243  Teuchos::RCP<Epetra_Import> ov_nodeIdImporter = rcp(new Epetra_Import( TmpMatrix->ColMap(), TmpMatrix->RowMap()));
1244  ov_colIdList.Import(ov_rowIdList,*ov_nodeIdImporter,Insert);
1245 
1246  for (int i=0 ; i<ov_colIdList.MyLength(); i++) {
1247  if (ov_colIdList[i] == nodeID) {
1248  int GID = (ov_colIdList.Map()).GID(i);
1249  colMapTable.put(GID,1);
1250  }
1251  }
1252 
1253  // Do a second import of the owning node ID from A's rowmap to TmpMat's column map. This ensures that
1254  // all GIDs that belong to a node buddy and are in a ghost row's sparsity pattern will be in the final
1255  // overlapped matrix's column map.
1256  ov_colIdList.PutValue(-1);
1257  Epetra_IntVector ArowIdList( A().RowMatrixRowMap() );
1258  ArowIdList.PutValue(nodeID);
1259  nodeIdImporter = rcp(new Epetra_Import( TmpMatrix->ColMap(), A().RowMatrixRowMap() ));
1260  ov_colIdList.Import(ArowIdList,*nodeIdImporter,Insert);
1261 
1262  for (int i=0 ; i<ov_colIdList.MyLength(); i++) {
1263  if (ov_colIdList[i] == nodeID) {
1264  int GID = (ov_colIdList.Map()).GID(i);
1265  colMapTable.put(GID,1);
1266  }
1267  }
1268 
1269  //FIXME timing
1270 #ifdef IFPACK_OVA_TIME_BUILD
1271  t1 = MPI_Wtime();
1272  fprintf(stderr,"[node %d]: overlap 2 imports to fix up colmap %2.3e\n", nodeID, t1-t0);
1273  t0=t1;
1274 #endif
1275  //FIXME timing
1276 
1277  } //for (int overlap = 0 ; overlap < OverlapLevel_in ; ++overlap)
1278 
1279  /* ** ************************************************************************ ** */
1280  /* ** ********************** end of main overlap loop ************************ ** */
1281  /* ** ************************************************************************ ** */
1282 
1283  // off-node GIDs that will be in the overlap
1284  vector<int> ghostElements;
1285 
1286  Teuchos::Array<int> gidsarray,votesarray;
1287  ghostTable.arrayify(gidsarray,votesarray);
1288  for (int i=0; i<ghostTable.size(); i++) {
1289  ghostElements.push_back(gidsarray[i]);
1290  }
1291 
1292  for (int i = 0 ; i < A().RowMatrixColMap().NumMyElements() ; ++i) {
1293  int GID = A().RowMatrixColMap().GID(i);
1294  // Remove any entries that are in A's original column map
1295  if (colMapTable.containsKey(GID)) {
1296  try{colMapTable.remove(GID);}
1297  catch(...) {
1298  printf("pid %d: In OverlappingRowMatrix ctr, problem removing entry %d from colMapTable\n", Comm().MyPID(),GID);
1299  fflush(stdout);
1300  }
1301  }
1302  }
1303 
1304  // GIDs that will be in the overlapped matrix's column map
1305  vector<int> colMapElements;
1306 
1307  gidsarray.clear(); votesarray.clear();
1308  colMapTable.arrayify(gidsarray,votesarray);
1309  for (int i=0; i<colMapTable.size(); i++)
1310  colMapElements.push_back(gidsarray[i]);
1311 
1312 /*
1313  We need two maps here. The first is the row map, which we've got by using my original rows
1314  plus whatever I've picked up in ghostElements.
1315 
1316  The second is a column map. This map should include my rows, plus any columns that could come from node buddies.
1317  These GIDs come from the std:array colMapElements, which in turn comes from colMapTable.
1318  This map should *not* omit ghosts that have been imported by my node buddies, i.e., for any row that I own,
1319  the stencil should include all column GIDs (including imported ghosts) that are on the node.
1320 */
1321 
1322  // build the row map containing all the nodes (original matrix + off-node matrix)
1323  vector<int> rowList(NumMyRowsA_ + ghostElements.size());
1324  for (int i = 0 ; i < NumMyRowsA_ ; ++i)
1325  rowList[i] = A().RowMatrixRowMap().GID(i);
1326  for (int i = 0 ; i < (int)ghostElements.size() ; ++i)
1327  rowList[i + NumMyRowsA_] = ghostElements[i];
1328 
1329  // row map for the overlapped matrix (local + overlap)
1330  Map_ = rcp( new Epetra_Map(-1, NumMyRowsA_ + ghostElements.size(), &rowList[0], 0, Comm()) );
1331 
1332  // build the column map for the overlapping matrix
1333  //vector<int> colList(colMapElements.size());
1334  // column map for the overlapped matrix (local + overlap)
1335  //colMap_ = rcp( new Epetra_Map(-1, colMapElements.size(), &colList[0], 0, Comm()) );
1336  //for (int i = 0 ; i < (int)colMapElements.size() ; i++)
1337  // colList[i] = colMapElements[i];
1338  vector<int> colList(A().RowMatrixColMap().NumMyElements() + colMapElements.size());
1339  int nc = A().RowMatrixColMap().NumMyElements();
1340  for (int i = 0 ; i < nc; i++)
1341  colList[i] = A().RowMatrixColMap().GID(i);
1342  for (int i = 0 ; i < (int)colMapElements.size() ; i++)
1343  colList[nc+i] = colMapElements[i];
1344 
1345  // column map for the overlapped matrix (local + overlap)
1346  //colMap_ = rcp( new Epetra_Map(-1, A().RowMatrixColMap().NumMyElements() + colMapElements.size(), &colList[0], 0, Comm()) );
1347  colMap_ = new Epetra_Map(-1, A().RowMatrixColMap().NumMyElements() + colMapElements.size(), &colList[0], 0, Comm());
1348 
1349 
1350  //FIXME timing
1351 #ifdef IFPACK_OVA_TIME_BUILD
1352  t1 = MPI_Wtime();
1353  fprintf(stderr,"[node %d]: time to init B() col/row maps %2.3e\n", nodeID, t1-t0);
1354  t0=t1;
1355 #endif
1356  //FIXME timing
1357 
1358  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1359  //++++ start of sort
1360  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1361  // build the column map, but don't use a copy constructor, b/c local communicator SubComm_ is
1362  // different from that of Matrix.
1363  try {
1364  // build row map based on the local communicator. We need this temporarily to build the column map.
1365  Epetra_Map* nodeMap = new Epetra_Map(-1,NumMyRowsA_ + ghostElements.size(),&rowList[0],0,*nodeComm);
1366  int numMyElts = colMap_->NumMyElements();
1367  assert(numMyElts!=0);
1368 
1369  // The column map *must* be sorted: first locals, then ghosts. The ghosts
1370  // must be further sorted so that they are contiguous by owning processor.
1371 
1372  // For each GID in column map, determine owning PID in local communicator.
1373  int* myGlobalElts = new int[numMyElts];
1374  colMap_->MyGlobalElements(myGlobalElts);
1375  int* pidList = new int[numMyElts];
1376  nodeMap->RemoteIDList(numMyElts, myGlobalElts, pidList, 0);
1377 
1378  // First sort column map GIDs based on the owning PID in local communicator.
1379  Epetra_Util Util;
1380  int *tt[1];
1381  tt[0] = myGlobalElts;
1382  Util.Sort(true, numMyElts, pidList, 0, (double**)0, 1, tt);
1383 
1384  // For each remote PID, sort the column map GIDs in ascending order.
1385  // Don't sort the local PID's entries.
1386  int localStart=0;
1387  while (localStart<numMyElts) {
1388  int currPID = (pidList)[localStart];
1389  int i=localStart;
1390  while (i<numMyElts && (pidList)[i] == currPID) i++;
1391  if (currPID != nodeComm->MyPID())
1392  Util.Sort(true, i-localStart, (myGlobalElts)+localStart, 0, 0, 0, 0);
1393  localStart = i;
1394  }
1395 
1396  // now move the local entries to the front of the list
1397  localStart=0;
1398  while (localStart<numMyElts && (pidList)[localStart] != nodeComm->MyPID()) localStart++;
1399  assert(localStart != numMyElts);
1400  int localEnd=localStart;
1401  while (localEnd<numMyElts && (pidList)[localEnd] == nodeComm->MyPID()) localEnd++;
1402  int* mySortedGlobalElts = new int[numMyElts];
1403  int leng = localEnd - localStart;
1404  /* This is a little gotcha. It appears that the ordering of the column map's local entries
1405  must be the same as that of the domain map's local entries. See the comment in method
1406  MakeColMap() in Epetra_CrsGraph.cpp, line 1072. */
1407  int *rowGlobalElts = nodeMap->MyGlobalElements();
1408 
1409  /*TODO TODO TODO NTS rows 68 and 83 show up as local GIDs in rowGlobalElts for both pids 0 & 1 on node 0.
1410  This seems to imply that there is something wrong with rowList!!
1411  It is ok for the overlapped matrix's row map to have repeated entries (it's overlapped, after all).
1412  But ... on a node, there should be no repeats.
1413  Ok, here's the underlying cause. On node 0, gpid 1 finds 83 on overlap round 0. gpid 0 finds 83
1414  on overlap round 1. This shouldn't happen .. once a node buddy "owns" a row, no other node buddy
1415  should ever import ("own") that row.
1416 
1417  Here's a possible fix. Extend tie breaking across overlap rounds. This means sending to lpid 0
1418  a list of *all* rows you've imported (this round plus previous rounds) for tie-breaking
1419  If a nb won a ghost row in a previous round, his votes for that ghost row should be really high, i.e.,
1420  he should probably win that ghost row forever.
1421  7/17/09
1422 
1423  7/28/09 JJH I believe I've fixed this, but I thought it might be helpful to have this comment in here, in case I missed something.
1424  */
1425 
1426  //move locals to front of list
1427  for (int i=0; i<leng; i++) {
1428  /* //original code */
1429  (mySortedGlobalElts)[i] = rowGlobalElts[i];
1430  //(&*mySortedGlobalElts)[i] = (&*myGlobalElts)[localStart+i];
1431  //(&*mySortedPidList)[i] = (&*pidList)[localStart+i];
1432  }
1433  for (int i=leng; i< localEnd; i++) {
1434  (mySortedGlobalElts)[i] = (myGlobalElts)[i-leng];
1435  }
1436  for (int i=localEnd; i<numMyElts; i++) {
1437  (mySortedGlobalElts)[i] = (myGlobalElts)[i];
1438  }
1439 
1440  //FIXME timing
1441 #ifdef IFPACK_OVA_TIME_BUILD
1442  t1 = MPI_Wtime();
1443  fprintf(stderr,"[node %d]: time to sort col map arrays (cp=1) %2.3e\n", nodeID, t1-t0);
1444  t0=t1;
1445 #endif
1446  //FIXME timing
1447 
1448  int indexBase = colMap_->IndexBase();
1449  if (colMap_) delete colMap_;
1450  colMap_ = new Epetra_Map(-1,numMyElts,mySortedGlobalElts,indexBase,Comm());
1451 
1452  delete nodeMap;
1453  delete [] myGlobalElts;
1454  delete [] pidList;
1455  delete [] mySortedGlobalElts;
1456 
1457  } //try
1458  catch(...) {
1459  printf("** * Ifpack_OverlappingRowmatrix ctor: problem creating column map * **\n\n");
1460  }
1461 
1462  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1463  //++++ end of sort
1464  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1465 
1466  //FIXME timing
1467 #ifdef IFPACK_OVA_TIME_BUILD
1468  t1 = MPI_Wtime();
1469  fprintf(stderr,"[node %d]: time to sort col map arrays (cp=2) %2.3e\n", nodeID, t1-t0);
1470  t0=t1;
1471 #endif
1472  //FIXME timing
1473 
1474 
1475 /*
1476 
1477  FIXME
1478  Does the column map need to be sorted for the overlapping matrix?
1479 
1480  The column map *must* be sorted:
1481 
1482  first locals
1483  then ghosts
1484 
1485  The ghosts must be further sorted so that they are contiguous by owning processor
1486 
1487  int* RemoteSizeList = 0
1488  int* RemoteColIndices = ColIndices.Values() + NumLocalColGIDs; // Points to back end of ColIndices
1489 
1490  EPETRA_CHK_ERR(DomainMap.RemoteIDList(NumRemoteColGIDs, RemoteColIndices, PIDList.Values(), 0, RemoteSizeList));
1491  Epetra_Util epetraUtil;
1492  SortLists[0] = RemoteColIndices;
1493  SortLists[1] = RemoteSizeList;
1494  epetraUtil.Sort(true, NumRemoteColGIDs, PIDList.Values(), 0, 0, NLists, SortLists);
1495 */
1496 
1497  // now build the map corresponding to all the external nodes
1498  // (with respect to A().RowMatrixRowMap().
1499  ExtMap_ = rcp( new Epetra_Map(-1,ghostElements.size(), &ghostElements[0],0,Comm()) );
1500  ExtMatrix_ = rcp( new Epetra_CrsMatrix(Copy,*ExtMap_,*colMap_,0) );
1501 
1502  ExtImporter_ = rcp( new Epetra_Import(*ExtMap_,A().RowMatrixRowMap()) );
1503  ExtMatrix_->Import(A(),*ExtImporter_,Insert);
1504 
1505  //FIXME timing
1506 #ifdef IFPACK_OVA_TIME_BUILD
1507  t1 = MPI_Wtime();
1508  fprintf(stderr,"[node %d]: time to import and setup ExtMap_ %2.3e\n", nodeID, t1-t0);
1509  t0=t1;
1510 #endif
1511  //FIXME timing
1512 
1513 
1514 /*
1515  Notes to self: In FillComplete, the range map does not have to be 1-1 as long as
1516  (row map == range map). Ditto for the domain map not being 1-1
1517  if (col map == domain map).
1518 
1519 */
1520 
1521  ExtMatrix_->FillComplete( *colMap_ , *Map_ ); //FIXME wrong
1522 
1523  //FIXME timing
1524 #ifdef IFPACK_OVA_TIME_BUILD
1525  t1 = MPI_Wtime();
1526  fprintf(stderr,"[node %d]: time to FillComplete B() %2.3e\n", nodeID, t1-t0);
1527  t0=t1;
1528 #endif
1529  //FIXME timing
1530 
1531 
1532  // Note: B() = *ExtMatrix_ .
1533 
1534  Importer_ = rcp( new Epetra_Import(*Map_,A().RowMatrixRowMap()) ); //FIXME is this right?!
1535 
1536  // fix indices for overlapping matrix
1537  NumMyRowsB_ = B().NumMyRows();
1538  NumMyRows_ = NumMyRowsA_ + NumMyRowsB_; //TODO is this wrong for a subdomain on >1 processor? // should be ok
1539  //NumMyCols_ = NumMyRows_; //TODO is this wrong for a subdomain on >1 processor? // YES!!!
1540  //NumMyCols_ = A().NumMyCols() + B().NumMyCols();
1541  NumMyCols_ = B().NumMyCols();
1542 
1543  /*FIXME*/ //somehow B's NumMyCols is the entire subdomain (local + overlap)
1544 
1545  NumMyDiagonals_ = A().NumMyDiagonals() + B().NumMyDiagonals();
1546 
1547  NumMyNonzeros_ = A().NumMyNonzeros() + B().NumMyNonzeros();
1548  Comm().SumAll(&NumMyNonzeros_,&NumGlobalNonzeros_,1);
1549  MaxNumEntries_ = A().MaxNumEntries();
1550 
1551  if (MaxNumEntries_ < B().MaxNumEntries())
1552  MaxNumEntries_ = B().MaxNumEntries();
1553 
1554 # ifdef HAVE_MPI
1555  delete nodeComm;
1556 # endif
1557 
1558  //FIXME timing
1559 #ifdef IFPACK_OVA_TIME_BUILD
1560  t1 = MPI_Wtime();
1561  fprintf(stderr,"[node %d]: time for final calcs %2.3e\n", nodeID, t1-t0);
1562  fprintf(stderr,"[node %d]: total IORM ctor time %2.3e\n", nodeID, t1-true_t0);
1563 #endif
1564  //FIXME timing
1565 
1566 
1567 } //Ifpack_OverlappingRowMatrix() ctor for more than one core
1568 
1569 // Destructor
1570 Ifpack_OverlappingRowMatrix::~Ifpack_OverlappingRowMatrix() {
1571  delete colMap_;
1572 }
1573 
1574 # endif //ifdef IFPACK_NODE_AWARE_CODE
1575 #endif // ifdef IFPACK_SUBCOMM_CODE
1576 
1577 
1578 // ======================================================================
1580 NumMyRowEntries(int MyRow, int & NumEntries) const
1581 {
1582  if (MyRow < NumMyRowsA_)
1583  return(A().NumMyRowEntries(MyRow,NumEntries));
1584  else
1585  return(B().NumMyRowEntries(MyRow - NumMyRowsA_, NumEntries));
1586 }
1587 
1588 #ifdef IFPACK_SUBCOMM_CODE
1589 // ======================================================================
1591 ExtractMyRowCopy(int LocRow, int Length, int & NumEntries, double *Values,
1592  int * Indices) const
1593 {
1594  //assert(1==0);
1595  int ierr;
1596  const Epetra_Map *Themap;
1597  if (LocRow < NumMyRowsA_) {
1598  ierr = A().ExtractMyRowCopy(LocRow,Length,NumEntries,Values,Indices);
1599  Themap=&A().RowMatrixColMap();
1600  }
1601  else {
1602  ierr = B().ExtractMyRowCopy(LocRow-NumMyRowsA_,Length,NumEntries,Values,Indices);
1603  Themap=&B().RowMatrixColMap();
1604  }
1605 
1606  IFPACK_RETURN(ierr);
1607 }
1608 
1609 // ======================================================================
1610 int Ifpack_OverlappingRowMatrix::
1611 ExtractGlobalRowCopy(int GlobRow, int Length, int & NumEntries, double *Values,
1612  int * Indices) const
1613 {
1614  int ierr;
1615  const Epetra_Map *Themap;
1616  int LocRow = A().RowMatrixRowMap().LID(GlobRow);
1617  if (LocRow < NumMyRowsA_ && LocRow != -1) { //TODO don't need to check less than nummyrows
1618  ierr = A().ExtractMyRowCopy(LocRow,Length,NumEntries,Values,Indices);
1619  Themap=&A().RowMatrixColMap();
1620  }
1621  else {
1622  LocRow = B().RowMatrixRowMap().LID(GlobRow);
1623  assert(LocRow!=-1);
1624  //ierr = B().ExtractMyRowCopy(LocRow-NumMyRowsA_,Length,NumEntries,Values,Indices);
1625  ierr = B().ExtractMyRowCopy(LocRow,Length,NumEntries,Values,Indices);
1626  Themap=&B().RowMatrixColMap();
1627  }
1628 
1629  for (int i=0; i<NumEntries; i++) {
1630  Indices[i]=Themap->GID(Indices[i]);
1631  assert(Indices[i] != -1);
1632  }
1633 
1634  IFPACK_RETURN(ierr);
1635 }
1636 #else
1637 # ifdef IFPACK_NODE_AWARE_CODE
1638 // ======================================================================
1640 ExtractMyRowCopy(int LocRow, int Length, int & NumEntries, double *Values,
1641  int * Indices) const
1642 {
1643  assert(1==0);
1644  int ierr;
1645  const Epetra_Map *Themap;
1646  if (LocRow < NumMyRowsA_) {
1647  ierr = A().ExtractMyRowCopy(LocRow,Length,NumEntries,Values,Indices);
1648  Themap=&A().RowMatrixColMap();
1649  }
1650  else {
1651  ierr = B().ExtractMyRowCopy(LocRow-NumMyRowsA_,Length,NumEntries,Values,Indices);
1652  Themap=&B().RowMatrixColMap();
1653  }
1654 
1655  IFPACK_RETURN(ierr);
1656 }
1657 
1658 // ======================================================================
1659 int Ifpack_OverlappingRowMatrix::
1660 ExtractGlobalRowCopy(int GlobRow, int Length, int & NumEntries, double *Values,
1661  int * Indices) const
1662 {
1663  int ierr;
1664  const Epetra_Map *Themap;
1665  int LocRow = A().RowMatrixRowMap().LID(GlobRow);
1666  if (LocRow < NumMyRowsA_ && LocRow != -1) { //TODO don't need to check less than nummyrows
1667  ierr = A().ExtractMyRowCopy(LocRow,Length,NumEntries,Values,Indices);
1668  Themap=&A().RowMatrixColMap();
1669  }
1670  else {
1671  LocRow = B().RowMatrixRowMap().LID(GlobRow);
1672  assert(LocRow!=-1);
1673  //ierr = B().ExtractMyRowCopy(LocRow-NumMyRowsA_,Length,NumEntries,Values,Indices);
1674  ierr = B().ExtractMyRowCopy(LocRow,Length,NumEntries,Values,Indices);
1675  Themap=&B().RowMatrixColMap();
1676  }
1677 
1678  for (int i=0; i<NumEntries; i++) {
1679  Indices[i]=Themap->GID(Indices[i]);
1680  assert(Indices[i] != -1);
1681  }
1682 
1683  IFPACK_RETURN(ierr);
1684 }
1685 # else
1686 
1687 // ======================================================================
1689 ExtractMyRowCopy(int MyRow, int Length, int & NumEntries, double *Values,
1690  int * Indices) const
1691 {
1692  int ierr;
1693  if (MyRow < NumMyRowsA_)
1694  ierr = A().ExtractMyRowCopy(MyRow,Length,NumEntries,Values,Indices);
1695  else
1696  ierr = B().ExtractMyRowCopy(MyRow - NumMyRowsA_,Length,NumEntries,
1697  Values,Indices);
1698 
1699  IFPACK_RETURN(ierr);
1700 }
1701 # endif // ifdef IFPACK_NODE_AWARE_CODE
1702 #endif // ifdef IFPACK_SUBCOMM_CODE
1703 
1704 // ======================================================================
1706 ExtractDiagonalCopy(Epetra_Vector & Diagonal) const
1707 {
1708  IFPACK_CHK_ERR(-1);
1709 }
1710 
1711 
1712 // ======================================================================
1714 Multiply(bool TransA, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
1715 {
1716  int NumVectors = X.NumVectors();
1717  vector<int> Ind(MaxNumEntries_);
1718  vector<double> Val(MaxNumEntries_);
1719 
1720  Y.PutScalar(0.0);
1721 
1722  // matvec with A (local rows)
1723  for (int i = 0 ; i < NumMyRowsA_ ; ++i) {
1724  for (int k = 0 ; k < NumVectors ; ++k) {
1725  int Nnz;
1726  IFPACK_CHK_ERR(A().ExtractMyRowCopy(i,MaxNumEntries_,Nnz,
1727  &Val[0], &Ind[0]));
1728  for (int j = 0 ; j < Nnz ; ++j) {
1729  Y[k][i] += Val[j] * X[k][Ind[j]];
1730  }
1731  }
1732  }
1733 
1734  // matvec with B (overlapping rows)
1735  for (int i = 0 ; i < NumMyRowsB_ ; ++i) {
1736  for (int k = 0 ; k < NumVectors ; ++k) {
1737  int Nnz;
1738  IFPACK_CHK_ERR(B().ExtractMyRowCopy(i,MaxNumEntries_,Nnz,
1739  &Val[0], &Ind[0]));
1740  for (int j = 0 ; j < Nnz ; ++j) {
1741  Y[k][i + NumMyRowsA_] += Val[j] * X[k][Ind[j]];
1742  }
1743  }
1744  }
1745  return(0);
1746 }
1747 
1748 // ======================================================================
1749 int Ifpack_OverlappingRowMatrix::
1750 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
1751 {
1752  IFPACK_CHK_ERR(Multiply(UseTranspose(),X,Y));
1753  return(0);
1754 }
1755 
1756 // ======================================================================
1757 int Ifpack_OverlappingRowMatrix::
1758 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
1759 {
1760  IFPACK_CHK_ERR(-1);
1761 }
1762 
1763 // ======================================================================
1764 #ifndef IFPACK_SUBCOMM_CODE
1765 # ifndef IFPACK_NODE_AWARE_CODE
1766 Epetra_RowMatrix& Ifpack_OverlappingRowMatrix::B() const
1767 {
1768  return(*ExtMatrix_);
1769 }
1770 # endif
1771 #endif
1772 // ======================================================================
1773 const Epetra_BlockMap& Ifpack_OverlappingRowMatrix::Map() const
1774 {
1775  return(*Map_);
1776 }
1777 
1778 // ======================================================================
1779 int Ifpack_OverlappingRowMatrix::
1780 ImportMultiVector(const Epetra_MultiVector& X, Epetra_MultiVector& OvX,
1781  Epetra_CombineMode CM)
1782 {
1783  OvX.Import(X,*Importer_,CM);
1784  return(0);
1785 }
1786 
1787 // ======================================================================
1788 int Ifpack_OverlappingRowMatrix::
1789 ExportMultiVector(const Epetra_MultiVector& OvX, Epetra_MultiVector& X,
1790  Epetra_CombineMode CM)
1791 {
1792  X.Export(OvX,*Importer_,CM);
1793  return(0);
1794 }
1795 
virtual int NumMyRowEntries(int MyRow, int &NumEntries) const
Returns the number of nonzero entries in MyRow.
int MaxNumEntries() const
Returns the max number of local entries in a row.
virtual int ExtractDiagonalCopy(Epetra_Vector &Diagonal) const
Returns a copy of the main diagonal in a user-provided vector.
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.
virtual int Multiply(bool TransA, const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Returns the result of a Epetra_RowMatrix multiplied by a Epetra_MultiVector X in Y.
const Epetra_Comm & Comm() const
Returns the communicator object of Graph.