IFPACK  Development
 All Classes Files Functions Variables Enumerations Friends Pages
Ifpack_AdditiveSchwarz.h
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // Ifpack: Object-Oriented Algebraic Preconditioner Package
6 // Copyright (2002) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
44 #ifndef IFPACK_ADDITIVESCHWARZ_H
45 #define IFPACK_ADDITIVESCHWARZ_H
46 
47 #include "Ifpack_ConfigDefs.h"
48 #include "Ifpack_Preconditioner.h"
49 #include "Ifpack_ConfigDefs.h"
50 #include "Ifpack_Preconditioner.h"
51 #include "Ifpack_Reordering.h"
52 #include "Ifpack_RCMReordering.h"
53 #include "Ifpack_METISReordering.h"
54 #include "Ifpack_LocalFilter.h"
55 #include "Ifpack_NodeFilter.h"
56 #ifdef IFPACK_SUBCOMM_CODE
57 #include "Ifpack_SubdomainFilter.h"
58 #endif
59 #include "Ifpack_SingletonFilter.h"
60 #include "Ifpack_ReorderFilter.h"
61 #include "Ifpack_Utils.h"
62 #include "Ifpack_OverlappingRowMatrix.h"
63 #include "Epetra_CombineMode.h"
64 #include "Epetra_MultiVector.h"
65 #include "Epetra_Map.h"
66 #include "Epetra_Comm.h"
67 #include "Epetra_Time.h"
68 #include "Epetra_LinearProblem.h"
69 #include "Epetra_RowMatrix.h"
70 #include "Epetra_CrsMatrix.h"
71 #include "Teuchos_ParameterList.hpp"
72 #include "Teuchos_RefCountPtr.hpp"
73 
74 #ifdef IFPACK_SUBCOMM_CODE
75 #include "EpetraExt_Reindex_CrsMatrix.h"
76 #include "EpetraExt_Reindex_MultiVector.h"
77 #endif
78 #ifdef IFPACK_NODE_AWARE_CODE
79 #include "EpetraExt_OperatorOut.h"
80 #include "EpetraExt_RowMatrixOut.h"
81 #include "EpetraExt_BlockMapOut.h"
82 #endif
83 
84 #ifdef HAVE_IFPACK_AMESOS
85  #include "Ifpack_AMDReordering.h"
86 #endif
87 
88 
90 
143 template<typename T>
145 
146 public:
147 
149 
161  Ifpack_AdditiveSchwarz(Epetra_RowMatrix* Matrix_in,
162  int OverlapLevel_in = 0);
163 
167 
169 
171 
180  virtual int SetUseTranspose(bool UseTranspose_in);
182 
184 
186 
196  virtual int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
197 
199 
210  virtual int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
211 
213  virtual double NormInf() const;
215 
217 
219  virtual const char * Label() const;
220 
222  virtual bool UseTranspose() const;
223 
225  virtual bool HasNormInf() const;
226 
228  virtual const Epetra_Comm & Comm() const;
229 
231  virtual const Epetra_Map & OperatorDomainMap() const;
232 
234  virtual const Epetra_Map & OperatorRangeMap() const;
236 
238  virtual bool IsInitialized() const
239  {
240  return(IsInitialized_);
241  }
242 
244  virtual bool IsComputed() const
245  {
246  return(IsComputed_);
247  }
248 
250 
269  virtual int SetParameters(Teuchos::ParameterList& List);
270 
271  // @}
272 
273  // @{ Query methods
274 
276  virtual int Initialize();
277 
279  virtual int Compute();
280 
282  virtual double Condest(const Ifpack_CondestType CT = Ifpack_Cheap,
283  const int MaxIters = 1550,
284  const double Tol = 1e-9,
285  Epetra_RowMatrix* Matrix_in = 0);
286 
288  virtual double Condest() const
289  {
290  return(Condest_);
291  }
292 
294  virtual const Epetra_RowMatrix& Matrix() const
295  {
296  return(*Matrix_);
297  }
298 
300  virtual bool IsOverlapping() const
301  {
302  return(IsOverlapping_);
303  }
304 
306  virtual std::ostream& Print(std::ostream&) const;
307 
308  virtual const T* Inverse() const
309  {
310  return(&*Inverse_);
311  }
312 
314  virtual int NumInitialize() const
315  {
316  return(NumInitialize_);
317  }
318 
320  virtual int NumCompute() const
321  {
322  return(NumCompute_);
323  }
324 
326  virtual int NumApplyInverse() const
327  {
328  return(NumApplyInverse_);
329  }
330 
332  virtual double InitializeTime() const
333  {
334  return(InitializeTime_);
335  }
336 
338  virtual double ComputeTime() const
339  {
340  return(ComputeTime_);
341  }
342 
344  virtual double ApplyInverseTime() const
345  {
346  return(ApplyInverseTime_);
347  }
348 
350  virtual double InitializeFlops() const
351  {
352  return(InitializeFlops_);
353  }
354 
355  virtual double ComputeFlops() const
356  {
357  return(ComputeFlops_);
358  }
359 
360  virtual double ApplyInverseFlops() const
361  {
362  return(ApplyInverseFlops_);
363  }
364 
366  virtual int OverlapLevel() const
367  {
368  return(OverlapLevel_);
369  }
370 
372  virtual const Teuchos::ParameterList& List() const
373  {
374  return(List_);
375  }
376 
377 protected:
378 
379  // @}
380 
381  // @{ Internal merhods.
382 
385  { }
386 
388  int Setup();
389 
390  // @}
391 
392  // @{ Internal data.
393 
395  Teuchos::RefCountPtr<const Epetra_RowMatrix> Matrix_;
397  Teuchos::RefCountPtr<Ifpack_OverlappingRowMatrix> OverlappingMatrix_;
399 /*
400  //TODO if we choose to expose the node aware code, i.e., no ifdefs,
401  //TODO then we should switch to this definition.
402  Teuchos::RefCountPtr<Epetra_RowMatrix> LocalizedMatrix_;
403 */
404 #ifdef IFPACK_SUBCOMM_CODE
405  Teuchos::RCP<Epetra_RowMatrix> LocalizedMatrix_;
408  Teuchos::RCP<Epetra_CrsMatrix> SubdomainCrsMatrix_;
410  Teuchos::RCP<Epetra_CrsMatrix> ReindexedCrsMatrix_;
411 
412  // The following data members are needed when doing ApplyInverse
413  // with an Ifpack_SubdomainFilter local matrix
414  Teuchos::RCP<Epetra_Map> tempMap_;
415  Teuchos::RCP<Epetra_Map> tempDomainMap_;
416  Teuchos::RCP<Epetra_Map> tempRangeMap_;
417  Teuchos::RCP<EpetraExt::CrsMatrix_Reindex> SubdomainMatrixReindexer_;
418  Teuchos::RCP<EpetraExt::MultiVector_Reindex> DomainVectorReindexer_;
419  Teuchos::RCP<EpetraExt::MultiVector_Reindex> RangeVectorReindexer_;
420  mutable Teuchos::RCP<Epetra_MultiVector> tempX_;
421  mutable Teuchos::RCP<Epetra_MultiVector> tempY_;
422 #else
423 # ifdef IFPACK_NODE_AWARE_CODE
424  Teuchos::RefCountPtr<Ifpack_NodeFilter> LocalizedMatrix_;
425 # else
426  Teuchos::RefCountPtr<Ifpack_LocalFilter> LocalizedMatrix_;
427 # endif
428 #endif
429 #ifdef IFPACK_SUBCOMM_CODE
430  // Some data members used for determining the subdomain id (color)
432  int MpiRank_;
434  int NumMpiProcs_;
436  int NumMpiProcsPerSubdomain_;
438  int NumSubdomains_;
440  int SubdomainId_;
441 #endif
442  string Label_;
455  Teuchos::ParameterList List_;
457  Epetra_CombineMode CombineMode_;
459  double Condest_;
467  Teuchos::RefCountPtr<Ifpack_Reordering> Reordering_;
469  Teuchos::RefCountPtr<Ifpack_ReorderFilter> ReorderedLocalizedMatrix_;
473  Teuchos::RefCountPtr<Ifpack_SingletonFilter> SingletonFilter_;
479  mutable int NumApplyInverse_;
483  double ComputeTime_;
485  mutable double ApplyInverseTime_;
491  mutable double ApplyInverseFlops_;
493  Teuchos::RefCountPtr<Epetra_Time> Time_;
495  Teuchos::RefCountPtr<T> Inverse_;
497 #ifdef IFPACK_SUBCOMM_CODE
498  mutable Teuchos::RefCountPtr<Epetra_MultiVector> OverlappingX;
499  mutable Teuchos::RefCountPtr<Epetra_MultiVector> OverlappingY;
500 #endif
501 # ifdef IFPACK_NODE_AWARE_CODE
502  mutable Teuchos::RefCountPtr<Epetra_MultiVector> OverlappingX;
503  mutable Teuchos::RefCountPtr<Epetra_MultiVector> OverlappingY;
504 #endif
505 
506 }; // class Ifpack_AdditiveSchwarz<T>
507 
508 //==============================================================================
509 template<typename T>
511 Ifpack_AdditiveSchwarz(Epetra_RowMatrix* Matrix_in,
512  int OverlapLevel_in) :
513 #ifdef IFPACK_SUBCOMM_CODE
514  MpiRank_(0),
515  NumMpiProcs_(1),
516  NumMpiProcsPerSubdomain_(1),
517  NumSubdomains_(1),
518  SubdomainId_(0),
519 #endif
520  IsInitialized_(false),
521  IsComputed_(false),
522  UseTranspose_(false),
523  IsOverlapping_(false),
524  OverlapLevel_(OverlapLevel_in),
525  CombineMode_(Zero),
526  Condest_(-1.0),
527  ComputeCondest_(true),
528  UseReordering_(false),
529  ReorderingType_("none"),
530  FilterSingletons_(false),
531  NumInitialize_(0),
532  NumCompute_(0),
533  NumApplyInverse_(0),
534  InitializeTime_(0.0),
535  ComputeTime_(0.0),
536  ApplyInverseTime_(0.0),
537  InitializeFlops_(0.0),
538  ComputeFlops_(0.0),
539  ApplyInverseFlops_(0.0)
540 {
541  // Construct a reference-counted pointer with the input matrix, don't manage the memory.
542  Matrix_ = Teuchos::rcp( Matrix_in, false );
543 
544  if (Matrix_->Comm().NumProc() == 1)
545  OverlapLevel_ = 0;
546 
547  if ((OverlapLevel_ != 0) && (Matrix_->Comm().NumProc() > 1))
548  IsOverlapping_ = true;
549  // Sets parameters to default values
550  Teuchos::ParameterList List_in;
551  SetParameters(List_in);
552 }
553 
554 #ifdef IFPACK_NODE_AWARE_CODE
555 extern int ML_NODE_ID;
556 #endif
557 
558 //==============================================================================
559 template<typename T>
561 {
562 
563  Epetra_RowMatrix* MatrixPtr;
564 
565 #ifndef IFPACK_SUBCOMM_CODE
566 # ifdef IFPACK_NODE_AWARE_CODE
567 /*
568  sleep(3);
569  if (Comm().MyPID() == 0) cout << "Printing out ovArowmap" << endl;
570  Comm().Barrier();
571 
572  EpetraExt::BlockMapToMatrixMarketFile("ovArowmap",OverlappingMatrix_->RowMatrixRowMap());
573  if (Comm().MyPID() == 0) cout << "Printing out ovAcolmap" << endl;
574  Comm().Barrier();
575  EpetraExt::BlockMapToMatrixMarketFile("ovAcolmap",OverlappingMatrix_->RowMatrixColMap());
576  Comm().Barrier();
577 */
578 /*
579  EpetraExt::RowMatrixToMatlabFile("ovA",*OverlappingMatrix_);
580  fprintf(stderr,"p %d n %d matrix file done\n",Comm().MyPID(),ML_NODE_ID);
581  Comm().Barrier();
582 */
583  int nodeID;
584  try{ nodeID = List_.get("ML node id",0);}
585  catch(...){fprintf(stderr,"%s","Ifpack_AdditiveSchwarz<T>::Setup(): no parameter \"ML node id\"\n\n");
586  cout << List_ << endl;}
587 # endif
588 #endif
589 
590  try{
591  if (OverlappingMatrix_ != Teuchos::null)
592  {
593 #ifdef IFPACK_SUBCOMM_CODE
594  if (NumMpiProcsPerSubdomain_ > 1) {
595  LocalizedMatrix_ = Teuchos::rcp(new Ifpack_SubdomainFilter(OverlappingMatrix_, SubdomainId_));
596  } else {
597  LocalizedMatrix_ = Teuchos::rcp(new Ifpack_LocalFilter(OverlappingMatrix_));
598  }
599 #else
600 # ifdef IFPACK_NODE_AWARE_CODE
601  Ifpack_NodeFilter *tt = new Ifpack_NodeFilter(OverlappingMatrix_,nodeID); //FIXME
602  LocalizedMatrix_ = Teuchos::rcp(tt);
603  //LocalizedMatrix_ = Teuchos::rcp( new Ifpack_LocalFilter(OverlappingMatrix_) );
604 # else
605  LocalizedMatrix_ = Teuchos::rcp( new Ifpack_LocalFilter(OverlappingMatrix_) );
606 # endif
607 #endif
608  }
609  else
610  {
611 #ifdef IFPACK_SUBCOMM_CODE
612  if (NumMpiProcsPerSubdomain_ > 1) {
613  LocalizedMatrix_ = Teuchos::rcp(new Ifpack_SubdomainFilter(Matrix_, SubdomainId_));
614  } else {
615  LocalizedMatrix_ = Teuchos::rcp(new Ifpack_LocalFilter(Matrix_));
616  }
617 #else
618 # ifdef IFPACK_NODE_AWARE_CODE
619  Ifpack_NodeFilter *tt = new Ifpack_NodeFilter(Matrix_,nodeID); //FIXME
620  LocalizedMatrix_ = Teuchos::rcp(tt);
621  //LocalizedMatrix_ = Teuchos::rcp( new Ifpack_LocalFilter(Matrix_) );
622 # else
623  LocalizedMatrix_ = Teuchos::rcp( new Ifpack_LocalFilter(Matrix_) );
624 # endif
625 #endif
626  }
627  }
628  catch(...) {
629  fprintf(stderr,"%s","AdditiveSchwarz Setup: problem creating local filter matrix.\n");
630  }
631 
632  if (LocalizedMatrix_ == Teuchos::null)
633  IFPACK_CHK_ERR(-5);
634 
635  // users may want to skip singleton check
636  if (FilterSingletons_) {
637  SingletonFilter_ = Teuchos::rcp( new Ifpack_SingletonFilter(LocalizedMatrix_) );
638  MatrixPtr = &*SingletonFilter_;
639  }
640  else
641  MatrixPtr = &*LocalizedMatrix_;
642 
643  if (UseReordering_) {
644 
645  // create reordering and compute it
646  if (ReorderingType_ == "rcm")
647  Reordering_ = Teuchos::rcp( new Ifpack_RCMReordering() );
648  else if (ReorderingType_ == "metis")
649  Reordering_ = Teuchos::rcp( new Ifpack_METISReordering() );
650 #ifdef HAVE_IFPACK_AMESOS
651  else if (ReorderingType_ == "amd" )
652  Reordering_ = Teuchos::rcp( new Ifpack_AMDReordering() );
653 #endif
654  else {
655  cerr << "reordering type not correct (" << ReorderingType_ << ")" << endl;
656  exit(EXIT_FAILURE);
657  }
658  if (Reordering_ == Teuchos::null) IFPACK_CHK_ERR(-5);
659 
660  IFPACK_CHK_ERR(Reordering_->SetParameters(List_));
661  IFPACK_CHK_ERR(Reordering_->Compute(*MatrixPtr));
662 
663  // now create reordered localized matrix
664  ReorderedLocalizedMatrix_ =
665  Teuchos::rcp( new Ifpack_ReorderFilter(Teuchos::rcp( MatrixPtr, false ), Reordering_) );
666 
667  if (ReorderedLocalizedMatrix_ == Teuchos::null) IFPACK_CHK_ERR(-5);
668 
669  MatrixPtr = &*ReorderedLocalizedMatrix_;
670  }
671 
672 #ifdef IFPACK_SUBCOMM_CODE
673  // The subdomain matrix needs to be reindexed by Amesos so we need to make a CrsMatrix
674  // and then reindex it with EpetraExt.
675  // The reindexing is done here because this feature is only implemented in Amesos_Klu,
676  // and it is desired to have reindexing with other direct solvers in the Amesos package
677  SubdomainCrsMatrix_.reset(new Epetra_CrsMatrix(Copy, MatrixPtr->RowMatrixRowMap(), -1));
678  Teuchos::RCP<Epetra_Import> tempImporter = Teuchos::rcp(new Epetra_Import(SubdomainCrsMatrix_->Map(), MatrixPtr->Map()));
679  SubdomainCrsMatrix_->Import(*MatrixPtr, *tempImporter, Insert);
680  SubdomainCrsMatrix_->FillComplete();
681 
682  tempMap_.reset(new Epetra_Map(SubdomainCrsMatrix_->RowMap().NumGlobalElements(),
683  SubdomainCrsMatrix_->RowMap().NumMyElements(),
684  0, SubdomainCrsMatrix_->Comm()));
685  tempRangeMap_.reset(new Epetra_Map(SubdomainCrsMatrix_->OperatorRangeMap().NumGlobalElements(),
686  SubdomainCrsMatrix_->OperatorRangeMap().NumMyElements(),
687  0, SubdomainCrsMatrix_->Comm()));
688  tempDomainMap_.reset(new Epetra_Map(SubdomainCrsMatrix_->OperatorDomainMap().NumGlobalElements(),
689  SubdomainCrsMatrix_->OperatorDomainMap().NumMyElements(),
690  0, SubdomainCrsMatrix_->Comm()));
691 
692  SubdomainMatrixReindexer_.reset(new EpetraExt::CrsMatrix_Reindex(*tempMap_));
693  DomainVectorReindexer_.reset(new EpetraExt::MultiVector_Reindex(*tempDomainMap_));
694  RangeVectorReindexer_.reset(new EpetraExt::MultiVector_Reindex(*tempRangeMap_));
695 
696  ReindexedCrsMatrix_.reset(&((*SubdomainMatrixReindexer_)(*SubdomainCrsMatrix_)), false);
697 
698  MatrixPtr = &*ReindexedCrsMatrix_;
699 
700  Inverse_ = Teuchos::rcp( new T(&*ReindexedCrsMatrix_) );
701 #else
702  Inverse_ = Teuchos::rcp( new T(MatrixPtr) );
703 #endif
704 
705  if (Inverse_ == Teuchos::null)
706  IFPACK_CHK_ERR(-5);
707 
708  return(0);
709 }
710 
711 //==============================================================================
712 template<typename T>
713 int Ifpack_AdditiveSchwarz<T>::SetParameters(Teuchos::ParameterList& List_in)
714 {
715 #ifdef IFPACK_SUBCOMM_CODE
716  MpiRank_ = Matrix_->Comm().MyPID();
717  NumMpiProcs_ = Matrix_->Comm().NumProc();
718  NumMpiProcsPerSubdomain_ = List_in.get("subdomain: number-of-processors", 1);
719  NumSubdomains_ = NumMpiProcs_ / NumMpiProcsPerSubdomain_;
720  SubdomainId_ = MpiRank_ / NumMpiProcsPerSubdomain_;
721 
722  if (NumSubdomains_ == 1) {
723  IsOverlapping_ = false;
724  }
725 
726 #endif
727  // compute the condition number each time Compute() is invoked.
728  ComputeCondest_ = List_in.get("schwarz: compute condest", ComputeCondest_);
729  // combine mode
730  if( Teuchos::ParameterEntry *combineModeEntry = List_in.getEntryPtr("schwarz: combine mode") )
731  {
732  if( typeid(std::string) == combineModeEntry->getAny().type() )
733  {
734  std::string mode = List_in.get("schwarz: combine mode", "Add");
735  if (mode == "Add")
736  CombineMode_ = Add;
737  else if (mode == "Zero")
738  CombineMode_ = Zero;
739  else if (mode == "Insert")
740  CombineMode_ = Insert;
741  else if (mode == "InsertAdd")
742  CombineMode_ = InsertAdd;
743  else if (mode == "Average")
744  CombineMode_ = Average;
745  else if (mode == "AbsMax")
746  CombineMode_ = AbsMax;
747  else
748  {
749  TEUCHOS_TEST_FOR_EXCEPTION(
750  true,std::logic_error
751  ,"Error, The (Epetra) combine mode of \""<<mode<<"\" is not valid! Only the values"
752  " \"Add\", \"Zero\", \"Insert\", \"InsertAdd\", \"Average\", and \"AbsMax\" are accepted!"
753  );
754  }
755  }
756  else if ( typeid(Epetra_CombineMode) == combineModeEntry->getAny().type() )
757  {
758  CombineMode_ = Teuchos::any_cast<Epetra_CombineMode>(combineModeEntry->getAny());
759  }
760  else
761  {
762  // Throw exception with good error message!
763  Teuchos::getParameter<std::string>(List_in,"schwarz: combine mode");
764  }
765  }
766  else
767  {
768  // Make the default be a string to be consistent with the valid parameters!
769  List_in.get("schwarz: combine mode","Zero");
770  }
771  // type of reordering
772  ReorderingType_ = List_in.get("schwarz: reordering type", ReorderingType_);
773  if (ReorderingType_ == "none")
774  UseReordering_ = false;
775  else
776  UseReordering_ = true;
777  // if true, filter singletons. NOTE: the filtered matrix can still have
778  // singletons! A simple example: upper triangular matrix, if I remove
779  // the lower node, I still get a matrix with a singleton! However, filter
780  // singletons should help for PDE problems with Dirichlet BCs.
781  FilterSingletons_ = List_in.get("schwarz: filter singletons", FilterSingletons_);
782 
783  // This copy may be needed by Amesos or other preconditioners.
784  List_ = List_in;
785 
786  return(0);
787 }
788 
789 //==============================================================================
790 template<typename T>
792 {
793  IsInitialized_ = false;
794  IsComputed_ = false; // values required
795  Condest_ = -1.0; // zero-out condest
796 
797  if (Time_ == Teuchos::null)
798  Time_ = Teuchos::rcp( new Epetra_Time(Comm()) );
799 
800  Time_->ResetStartTime();
801 
802  // compute the overlapping matrix if necessary
803  if (IsOverlapping_) {
804 #ifdef IFPACK_SUBCOMM_CODE
805  if (NumMpiProcsPerSubdomain_ > 1) {
806  OverlappingMatrix_ = Teuchos::rcp( new Ifpack_OverlappingRowMatrix(Matrix_, OverlapLevel_, SubdomainId_) );
807  } else {
808  OverlappingMatrix_ = Teuchos::rcp( new Ifpack_OverlappingRowMatrix(Matrix_, OverlapLevel_));
809  }
810 #else
811 # ifdef IFPACK_NODE_AWARE_CODE
812  int myNodeID;
813  try{ myNodeID = List_.get("ML node id",-1);}
814  catch(...){fprintf(stderr,"pid %d: no such entry (returned %d)\n",Comm().MyPID(),myNodeID);}
815 /*
816  cout << "pid " << Comm().MyPID()
817  << ": calling Ifpack_OverlappingRowMatrix with myNodeID = "
818  << myNodeID << ", OverlapLevel_ = " << OverlapLevel_ << endl;
819 */
820  OverlappingMatrix_ = Teuchos::rcp( new Ifpack_OverlappingRowMatrix(Matrix_, OverlapLevel_, myNodeID) );
821 # else
822  OverlappingMatrix_ = Teuchos::rcp( new Ifpack_OverlappingRowMatrix(Matrix_, OverlapLevel_) );
823 # endif
824 #endif
825 
826  if (OverlappingMatrix_ == Teuchos::null) {
827  IFPACK_CHK_ERR(-5);
828  }
829  }
830 
831 # ifdef IFPACK_NODE_AWARE_CODE
832 /*
833  sleep(1);
834  Comm().Barrier();
835 */
836 # endif
837 
838  IFPACK_CHK_ERR(Setup());
839 
840 # ifdef IFPACK_NODE_AWARE_CODE
841 /*
842  sleep(1);
843  Comm().Barrier();
844 */
845 #endif
846 
847  if (Inverse_ == Teuchos::null)
848  IFPACK_CHK_ERR(-5);
849 
850  if (LocalizedMatrix_ == Teuchos::null)
851  IFPACK_CHK_ERR(-5);
852 
853  IFPACK_CHK_ERR(Inverse_->SetUseTranspose(UseTranspose()));
854  IFPACK_CHK_ERR(Inverse_->SetParameters(List_));
855  IFPACK_CHK_ERR(Inverse_->Initialize());
856 
857  // Label is for Aztec-like solvers
858  Label_ = "Ifpack_AdditiveSchwarz, ";
859  if (UseTranspose())
860  Label_ += ", transp";
861  Label_ += ", ov = " + Ifpack_toString(OverlapLevel_)
862  + ", local solver = \n\t\t***** `" + string(Inverse_->Label()) + "'";
863 
864  IsInitialized_ = true;
865  ++NumInitialize_;
866  InitializeTime_ += Time_->ElapsedTime();
867 
868 #ifdef IFPACK_FLOPCOUNTERS
869  // count flops by summing up all the InitializeFlops() in each
870  // Inverse. Each Inverse() can only give its flops -- it acts on one
871  // process only
872  double partial = Inverse_->InitializeFlops();
873  double total;
874  Comm().SumAll(&partial, &total, 1);
875  InitializeFlops_ += total;
876 #endif
877 
878  return(0);
879 }
880 
881 //==============================================================================
882 template<typename T>
884 {
885  if (IsInitialized() == false)
886  IFPACK_CHK_ERR(Initialize());
887 
888  Time_->ResetStartTime();
889  IsComputed_ = false;
890  Condest_ = -1.0;
891 
892  IFPACK_CHK_ERR(Inverse_->Compute());
893 
894  IsComputed_ = true; // need this here for Condest(Ifpack_Cheap)
895  ++NumCompute_;
896  ComputeTime_ += Time_->ElapsedTime();
897 
898 #ifdef IFPACK_FLOPCOUNTERS
899  // sum up flops
900  double partial = Inverse_->ComputeFlops();
901  double total;
902  Comm().SumAll(&partial, &total, 1);
903  ComputeFlops_ += total;
904 #endif
905 
906  // reset the Label
907  string R = "";
908  if (UseReordering_)
909  R = ReorderingType_ + " reord, ";
910 
911  if (ComputeCondest_)
912  Condest(Ifpack_Cheap);
913 
914  // add Condest() to label
915  Label_ = "Ifpack_AdditiveSchwarz, ov = " + Ifpack_toString(OverlapLevel_)
916  + ", local solver = \n\t\t***** `" + string(Inverse_->Label()) + "'"
917  + "\n\t\t***** " + R + "Condition number estimate = "
918  + Ifpack_toString(Condest_);
919 
920  return(0);
921 }
922 
923 //==============================================================================
924 template<typename T>
926 {
927  // store the flag -- it will be set in Initialize() if Inverse_ does not
928  // exist.
929  UseTranspose_ = UseTranspose_in;
930 
931  // If Inverse_ exists, pass it right now.
932  if (Inverse_!=Teuchos::null)
933  IFPACK_CHK_ERR(Inverse_->SetUseTranspose(UseTranspose_in));
934  return(0);
935 }
936 
937 //==============================================================================
938 template<typename T>
940 Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
941 {
942  IFPACK_CHK_ERR(Matrix_->Apply(X,Y));
943  return(0);
944 }
945 
946 //==============================================================================
947 template<typename T>
949 {
950  return(-1.0);
951 }
952 
953 //==============================================================================
954 template<typename T>
956 {
957  return(Label_.c_str());
958 }
959 
960 //==============================================================================
961 template<typename T>
963 {
964  return(UseTranspose_);
965 }
966 
967 //==============================================================================
968 template<typename T>
970 {
971  return(false);
972 }
973 
974 //==============================================================================
975 template<typename T>
976 const Epetra_Comm & Ifpack_AdditiveSchwarz<T>::Comm() const
977 {
978  return(Matrix_->Comm());
979 }
980 
981 //==============================================================================
982 template<typename T>
984 {
985  return(Matrix_->OperatorDomainMap());
986 }
987 
988 //==============================================================================
989 template<typename T>
991 {
992  return(Matrix_->OperatorRangeMap());
993 }
994 
995 //==============================================================================
996 template<typename T>
998 ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
999 {
1000  // compute the preconditioner is not done by the user
1001  if (!IsComputed())
1002  IFPACK_CHK_ERR(-3);
1003 
1004  int NumVectors = X.NumVectors();
1005 
1006  if (NumVectors != Y.NumVectors())
1007  IFPACK_CHK_ERR(-2); // wrong input
1008 
1009  Time_->ResetStartTime();
1010 
1011  Teuchos::RefCountPtr<Epetra_MultiVector> OverlappingX;
1012  Teuchos::RefCountPtr<Epetra_MultiVector> OverlappingY;
1013  Teuchos::RefCountPtr<Epetra_MultiVector> Xtmp;
1014 
1015  // for flop count, see bottom of this function
1016 #ifdef IFPACK_FLOPCOUNTERS
1017  double pre_partial = Inverse_->ApplyInverseFlops();
1018  double pre_total;
1019  Comm().SumAll(&pre_partial, &pre_total, 1);
1020 #endif
1021 
1022  // process overlap, may need to create vectors and import data
1023  if (IsOverlapping()) {
1024 #ifdef IFPACK_SUBCOMM_CODE
1025  if (OverlappingX == Teuchos::null) {
1026  OverlappingX = Teuchos::rcp( new Epetra_MultiVector(OverlappingMatrix_->RowMatrixRowMap(), X.NumVectors()) );
1027  if (OverlappingX == Teuchos::null) IFPACK_CHK_ERR(-5);
1028  } else assert(OverlappingX->NumVectors() == X.NumVectors());
1029  if (OverlappingY == Teuchos::null) {
1030  OverlappingY = Teuchos::rcp( new Epetra_MultiVector(OverlappingMatrix_->RowMatrixRowMap(), Y.NumVectors()) );
1031  if (OverlappingY == Teuchos::null) IFPACK_CHK_ERR(-5);
1032  } else assert(OverlappingY->NumVectors() == Y.NumVectors());
1033 #else
1034 # ifdef IFPACK_NODE_AWARE_CODE
1035  if (OverlappingX == Teuchos::null) {
1036  OverlappingX = Teuchos::rcp( new Epetra_MultiVector(OverlappingMatrix_->RowMatrixRowMap(),
1037  X.NumVectors()) );
1038  if (OverlappingX == Teuchos::null) IFPACK_CHK_ERR(-5);
1039  } else assert(OverlappingX->NumVectors() == X.NumVectors());
1040  if (OverlappingY == Teuchos::null) {
1041  OverlappingY = Teuchos::rcp( new Epetra_MultiVector(OverlappingMatrix_->RowMatrixRowMap(),
1042  Y.NumVectors()) );
1043  if (OverlappingY == Teuchos::null) IFPACK_CHK_ERR(-5);
1044  } else assert(OverlappingY->NumVectors() == Y.NumVectors());
1045 #else
1046  OverlappingX = Teuchos::rcp( new Epetra_MultiVector(OverlappingMatrix_->RowMatrixRowMap(),
1047  X.NumVectors()) );
1048  OverlappingY = Teuchos::rcp( new Epetra_MultiVector(OverlappingMatrix_->RowMatrixRowMap(),
1049  Y.NumVectors()) );
1050  if (OverlappingY == Teuchos::null) IFPACK_CHK_ERR(-5);
1051 # endif
1052 #endif
1053  OverlappingY->PutScalar(0.0);
1054  OverlappingX->PutScalar(0.0);
1055  IFPACK_CHK_ERR(OverlappingMatrix_->ImportMultiVector(X,*OverlappingX,Insert));
1056  // FIXME: this will not work with overlapping and non-zero starting
1057  // solutions. The same for other cases below.
1058  // IFPACK_CHK_ERR(OverlappingMatrix_->ImportMultiVector(Y,*OverlappingY,Insert));
1059  }
1060  else {
1061  Xtmp = Teuchos::rcp( new Epetra_MultiVector(X) );
1062  OverlappingX = Xtmp;
1063  OverlappingY = Teuchos::rcp( &Y, false );
1064  }
1065 
1066  if (FilterSingletons_) {
1067  // process singleton filter
1068  Epetra_MultiVector ReducedX(SingletonFilter_->Map(),NumVectors);
1069  Epetra_MultiVector ReducedY(SingletonFilter_->Map(),NumVectors);
1070  IFPACK_CHK_ERR(SingletonFilter_->SolveSingletons(*OverlappingX,*OverlappingY));
1071  IFPACK_CHK_ERR(SingletonFilter_->CreateReducedRHS(*OverlappingY,*OverlappingX,ReducedX));
1072 
1073  // process reordering
1074  if (!UseReordering_) {
1075  IFPACK_CHK_ERR(Inverse_->ApplyInverse(ReducedX,ReducedY));
1076  }
1077  else {
1078  Epetra_MultiVector ReorderedX(ReducedX);
1079  Epetra_MultiVector ReorderedY(ReducedY);
1080  IFPACK_CHK_ERR(Reordering_->P(ReducedX,ReorderedX));
1081  IFPACK_CHK_ERR(Inverse_->ApplyInverse(ReorderedX,ReorderedY));
1082  IFPACK_CHK_ERR(Reordering_->Pinv(ReorderedY,ReducedY));
1083  }
1084 
1085  // finish up with singletons
1086  IFPACK_CHK_ERR(SingletonFilter_->UpdateLHS(ReducedY,*OverlappingY));
1087  }
1088  else {
1089  // process reordering
1090  if (!UseReordering_) {
1091 #ifdef IFPACK_SUBCOMM_CODE
1092  tempX_.reset(&((*RangeVectorReindexer_)(*OverlappingX)), false);
1093  tempY_.reset(&((*DomainVectorReindexer_)(*OverlappingY)), false);
1094  IFPACK_CHK_ERR(Inverse_->ApplyInverse(*tempX_,*tempY_));
1095 #else
1096  IFPACK_CHK_ERR(Inverse_->ApplyInverse(*OverlappingX,*OverlappingY));
1097 #endif
1098  }
1099  else {
1100  Epetra_MultiVector ReorderedX(*OverlappingX);
1101  Epetra_MultiVector ReorderedY(*OverlappingY);
1102  IFPACK_CHK_ERR(Reordering_->P(*OverlappingX,ReorderedX));
1103  IFPACK_CHK_ERR(Inverse_->ApplyInverse(ReorderedX,ReorderedY));
1104  IFPACK_CHK_ERR(Reordering_->Pinv(ReorderedY,*OverlappingY));
1105  }
1106  }
1107 
1108  if (IsOverlapping()) {
1109  IFPACK_CHK_ERR(OverlappingMatrix_->ExportMultiVector(*OverlappingY,Y,
1110  CombineMode_));
1111  }
1112 
1113 #ifdef IFPACK_FLOPCOUNTERS
1114  // add flops. Note the we only have to add the newly counted
1115  // flops -- and each Inverse returns the cumulative sum
1116  double partial = Inverse_->ApplyInverseFlops();
1117  double total;
1118  Comm().SumAll(&partial, &total, 1);
1119  ApplyInverseFlops_ += total - pre_total;
1120 #endif
1121 
1122  // FIXME: right now I am skipping the overlap and singletons
1123  ++NumApplyInverse_;
1124  ApplyInverseTime_ += Time_->ElapsedTime();
1125 
1126  return(0);
1127 
1128 }
1129 
1130 //==============================================================================
1131 template<typename T>
1132 std::ostream& Ifpack_AdditiveSchwarz<T>::
1133 Print(std::ostream& os) const
1134 {
1135 #ifdef IFPACK_FLOPCOUNTERS
1136  double IF = InitializeFlops();
1137  double CF = ComputeFlops();
1138  double AF = ApplyInverseFlops();
1139 
1140  double IFT = 0.0, CFT = 0.0, AFT = 0.0;
1141  if (InitializeTime() != 0.0)
1142  IFT = IF / InitializeTime();
1143  if (ComputeTime() != 0.0)
1144  CFT = CF / ComputeTime();
1145  if (ApplyInverseTime() != 0.0)
1146  AFT = AF / ApplyInverseTime();
1147 #endif
1148 
1149  if (Matrix().Comm().MyPID())
1150  return(os);
1151 
1152  os << endl;
1153  os << "================================================================================" << endl;
1154  os << "Ifpack_AdditiveSchwarz, overlap level = " << OverlapLevel_ << endl;
1155  if (CombineMode_ == Insert)
1156  os << "Combine mode = Insert" << endl;
1157  else if (CombineMode_ == Add)
1158  os << "Combine mode = Add" << endl;
1159  else if (CombineMode_ == Zero)
1160  os << "Combine mode = Zero" << endl;
1161  else if (CombineMode_ == Average)
1162  os << "Combine mode = Average" << endl;
1163  else if (CombineMode_ == AbsMax)
1164  os << "Combine mode = AbsMax" << endl;
1165 
1166  os << "Condition number estimate = " << Condest_ << endl;
1167  os << "Global number of rows = " << Matrix_->NumGlobalRows() << endl;
1168 
1169 #ifdef IFPACK_SUBCOMM_CODE
1170  os << endl;
1171  os << "================================================================================" << endl;
1172  os << "Subcommunicator stats" << endl;
1173  os << "Number of MPI processes in simulation: " << NumMpiProcs_ << endl;
1174  os << "Number of subdomains: " << NumSubdomains_ << endl;
1175  os << "Number of MPI processes per subdomain: " << NumMpiProcsPerSubdomain_ << endl;
1176 #endif
1177 
1178  os << endl;
1179  os << "Phase # calls Total Time (s) Total MFlops MFlops/s" << endl;
1180  os << "----- ------- -------------- ------------ --------" << endl;
1181  os << "Initialize() " << std::setw(5) << NumInitialize()
1182  << " " << std::setw(15) << InitializeTime()
1183 #ifdef IFPACK_FLOPCOUNTERS
1184  << " " << std::setw(15) << 1.0e-6 * IF
1185  << " " << std::setw(15) << 1.0e-6 * IFT
1186 #endif
1187  << endl;
1188  os << "Compute() " << std::setw(5) << NumCompute()
1189  << " " << std::setw(15) << ComputeTime()
1190 #ifdef IFPACK_FLOPCOUNTERS
1191  << " " << std::setw(15) << 1.0e-6 * CF
1192  << " " << std::setw(15) << 1.0e-6 * CFT
1193 #endif
1194  << endl;
1195  os << "ApplyInverse() " << std::setw(5) << NumApplyInverse()
1196  << " " << std::setw(15) << ApplyInverseTime()
1197 #ifdef IFPACK_FLOPCOUNTERS
1198  << " " << std::setw(15) << 1.0e-6 * AF
1199  << " " << std::setw(15) << 1.0e-6 * AFT
1200 #endif
1201  << endl;
1202  os << "================================================================================" << endl;
1203  os << endl;
1204 
1205  return(os);
1206 }
1207 
1208 #include "Ifpack_Condest.h"
1209 //==============================================================================
1210 template<typename T>
1212 Condest(const Ifpack_CondestType CT, const int MaxIters,
1213  const double Tol, Epetra_RowMatrix* Matrix_in)
1214 {
1215  if (!IsComputed()) // cannot compute right now
1216  return(-1.0);
1217 
1218  Condest_ = Ifpack_Condest(*this, CT, MaxIters, Tol, Matrix_in);
1219 
1220  return(Condest_);
1221 }
1222 
1223 #endif // IFPACK_ADDITIVESCHWARZ_H
virtual int ApplyInverse(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the preconditioner to X, returns the result in Y.
int Setup()
Sets up the localized matrix and the singleton filter.
int OverlapLevel_
Level of overlap among the processors.
Teuchos::RefCountPtr< const Epetra_RowMatrix > Matrix_
Pointers to the matrix to be preconditioned.
string ReorderingType_
Type of reordering of the local matrix.
double ComputeFlops_
Contains the number of flops for Compute().
virtual int SetUseTranspose(bool UseTranspose_in)
If set true, transpose of this operator will be applied (not implemented).
virtual const Epetra_Map & OperatorRangeMap() const
Returns the Epetra_Map object associated with the range of this operator.
Ifpack_AMDReordering: approximate minimum degree reordering.
bool UseTranspose_
If true, solve with the transpose (not supported by all solvers).
virtual double ComputeFlops() const
Returns the number of flops in the computation phase.
bool ComputeCondest_
If true, compute the condition number estimate each time Compute() is called.
double Condest_
Contains the estimated condition number.
virtual bool IsInitialized() const
Returns true if the preconditioner has been successfully initialized.
Ifpack_METISReordering: A class to reorder a graph using METIS.
double ApplyInverseFlops_
Contain sthe number of flops for ApplyInverse().
string Ifpack_toString(const int &x)
Convertes an integer to string.
virtual bool IsComputed() const
Returns true if the preconditioner has been successfully computed.
Teuchos::RefCountPtr< Ifpack_OverlappingRowMatrix > OverlappingMatrix_
Pointers to the overlapping matrix.
virtual int OverlapLevel() const
Returns the level of overlap.
virtual double ApplyInverseTime() const
Returns the time spent in ApplyInverse().
virtual bool IsOverlapping() const
Returns true is an overlapping matrix is present.
virtual double ComputeTime() const
Returns the time spent in Compute().
Teuchos::RefCountPtr< Ifpack_Reordering > Reordering_
Pointer to a reordering object.
virtual double ApplyInverseFlops() const
Returns the number of flops in the application of the preconditioner.
Teuchos::RefCountPtr< Ifpack_ReorderFilter > ReorderedLocalizedMatrix_
Pointer to the reorderd matrix.
virtual int NumInitialize() const
Returns the number of calls to Initialize().
bool IsOverlapping_
If true, overlapping is used.
virtual std::ostream & Print(std::ostream &) const
Prints major information about this preconditioner.
virtual int Initialize()
Initialized the preconditioner.
Ifpack_AdditiveSchwarz(const Ifpack_AdditiveSchwarz &RHS)
Copy constructor (should never be used)
double ComputeTime_
Contains the time for all successful calls to Compute().
Ifpack_RCMReordering: reverse Cuthill-McKee reordering.
Ifpack_AdditiveSchwarz: a class to define Additive Schwarz preconditioners of Epetra_RowMatrix&#39;s.
virtual int NumApplyInverse() const
Returns the number of calls to ApplyInverse().
virtual const Epetra_Map & OperatorDomainMap() const
Returns the Epetra_Map object associated with the domain of this operator.
Ifpack_Preconditioner: basic class for preconditioning in Ifpack.
virtual bool UseTranspose() const
Returns the current UseTranspose setting.
Ifpack_AdditiveSchwarz(Epetra_RowMatrix *Matrix_in, int OverlapLevel_in=0)
Ifpack_AdditiveSchwarz constructor with given Epetra_RowMatrix.
double InitializeFlops_
Contains the number of flops for Initialize().
virtual bool HasNormInf() const
Returns true if the this object can provide an approximate Inf-norm, false otherwise.
Ifpack_SingletonFilter: Filter based on matrix entries.
virtual int Apply(const Epetra_MultiVector &X, Epetra_MultiVector &Y) const
Applies the matrix to X, returns the result in Y.
virtual ~Ifpack_AdditiveSchwarz()
Destructor.
Ifpack_OverlappingRowMatrix: matrix with ghost rows, based on Epetra_RowMatrix.
Ifpack_LocalFilter a class for light-weight extraction of the submatrix corresponding to local rows a...
bool IsInitialized_
If true, the preconditioner has been successfully initialized.
string Label_
Contains the label of this object.
int NumInitialize_
Contains the number of successful calls to Initialize().
virtual int SetParameters(Teuchos::ParameterList &List)
Sets the parameters.
bool UseReordering_
If true, reorder the local matrix.
virtual int Compute()
Computes the preconditioner.
int NumApplyInverse_
Contains the number of successful call to ApplyInverse().
Teuchos::RefCountPtr< Ifpack_SingletonFilter > SingletonFilter_
filtering object.
double InitializeTime_
Contains the time for all successful calls to Initialize().
virtual double InitializeFlops() const
Returns the number of flops in the initialization phase.
bool IsComputed_
If true, the preconditioner has been successfully computed.
virtual const Teuchos::ParameterList & List() const
Returns a reference to the internally stored list.
virtual const char * Label() const
Returns a character string describing the operator.
Teuchos::RefCountPtr< T > Inverse_
Pointer to the local solver.
int NumCompute_
Contains the number of successful call to Compute().
virtual double InitializeTime() const
Returns the time spent in Initialize().
virtual const Epetra_Comm & Comm() const
Returns a pointer to the Epetra_Comm communicator associated with this operator.
double ApplyInverseTime_
Contains the time for all successful calls to ApplyInverse().
Ifpack_ReorderFilter: a class for light-weight reorder of local rows and columns of an Epetra_RowMatr...
virtual double NormInf() const
Returns the infinity norm of the global matrix (not implemented)
Teuchos::ParameterList List_
Stores a copy of the list given in SetParameters()
Epetra_CombineMode CombineMode_
Combine mode for off-process elements (only if overlap is used)
virtual const Epetra_RowMatrix & Matrix() const
Returns a refernence to the internally stored matrix.
bool FilterSingletons_
Filter for singletons.
virtual double Condest() const
Returns the estimated condition number, or -1.0 if not computed.
virtual int NumCompute() const
Returns the number of calls to Compute().
Teuchos::RefCountPtr< Epetra_Time > Time_
Object used for timing purposes.
Teuchos::RefCountPtr< Ifpack_LocalFilter > LocalizedMatrix_
Localized version of Matrix_ or OverlappingMatrix_.