EpetraExt Package Browser (Single Doxygen Collection) Development
EpetraExt_HypreIJMatrix.h
Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2011) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 
00042 #ifndef EPETRAEXT_HYPREIJMATRIX_H_
00043 #define EPETRAEXT_HYPREIJMATRIX_H_
00044 
00045 // Trilinos source files
00046 #include "Epetra_Object.h"
00047 #include "Epetra_CompObject.h"
00048 #include "Epetra_BasicRowMatrix.h"
00049 #include "Epetra_Map.h"
00050 #include "Epetra_Import.h"
00051 #include "Epetra_MpiComm.h"
00052 
00053 //Hypre source files
00054 #include "krylov.h"
00055 #include "HYPRE_parcsr_ls.h"
00056 #include "_hypre_parcsr_mv.h"
00057 #include "HYPRE_parcsr_mv.h"
00058 #include "HYPRE_IJ_mv.h"
00059 #include "_hypre_IJ_mv.h"
00060 #include "HYPRE.h"
00061 
00062 class Epetra_Vector;
00063 class Epetra_MultiVector;
00064 class Epetra_Import;
00065 
00067 
00074 #ifndef HYPRE_ENUMS
00075 #define HYPRE_ENUMS
00076 
00077 
00079   enum Hypre_Solver{ 
00080     BoomerAMG, 
00081     ParaSails, 
00082     Euclid,    
00083     AMS,       
00084     Hybrid,    
00085     PCG,       
00086     GMRES,     
00087     FlexGMRES, 
00088     LGMRES,    
00089     BiCGSTAB   
00090   };
00091 
00093   enum Hypre_Chooser{
00094     Solver,        
00095     Preconditioner 
00096   };
00097 #endif //HYPRE_ENUMS
00098 
00099 class EpetraExt_HypreIJMatrix: public Epetra_BasicRowMatrix  {
00100       
00101 public:
00102 
00104 
00105 
00106 
00110   EpetraExt_HypreIJMatrix(HYPRE_IJMatrix matrix);
00111 
00113   virtual ~EpetraExt_HypreIJMatrix();
00115   
00117 
00118 
00120 
00129   int ExtractMyRowCopy(int MyRow, int Length, int & NumEntries, double *Values, int * Indices) const;
00130     
00132 
00141   int ExtractMyEntryView(int CurEntry, double *&Value, int &RowIndex, int &ColIndex);
00142     
00144 
00153   int ExtractMyEntryView(int CurEntry, const double *&Value, int &RowIndex, int &ColIndex) const;
00155 
00157 
00158 
00160 
00168   int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int), int parameter);
00169 
00171 
00179   int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double), double parameter);
00180 
00182 
00191   int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double, int), double parameter1, int parameter2);
00192 
00194 
00203   int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int, int), int parameter1, int parameter2);
00204 
00206 
00214   int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, double*), double* parameter);
00215 
00217 
00225   int SetParameter(Hypre_Chooser chooser, int (*pt2Func)(HYPRE_Solver, int*), int* parameter);
00226 
00228 
00238   int SetParameter(Hypre_Chooser chooser, Hypre_Solver Solver, bool transpose=false);
00239 
00241 
00249   int SetParameter(bool UsePreconditioner);
00250 
00252 
00258   int SetParameter(Hypre_Chooser chooser);
00260 
00261 
00262 
00264 
00271   int Multiply(bool TransA, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
00272 
00274 
00284   int Solve(bool Upper, bool Trans, bool UnitDiagonal, const Epetra_MultiVector& X, Epetra_MultiVector& Y) const;
00285 
00287 
00293   int LeftScale(const Epetra_Vector& X);
00294 
00295 
00297 
00303   int RightScale(const Epetra_Vector& X);
00305 
00307 
00308 
00310 
00316   int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const {
00317     return(EpetraExt_HypreIJMatrix::Multiply(EpetraExt_HypreIJMatrix::UseTranspose(), X, Y));};
00318 
00320 
00331   int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const {
00332     return(EpetraExt_HypreIJMatrix::Solve(EpetraExt_HypreIJMatrix::UpperTriangular(), EpetraExt_HypreIJMatrix::UseTranspose(), false, X, Y));};
00333 
00335   virtual bool UseTranspose() const {return(false);}
00336 
00338 
00339 
00340 
00342 
00349   int NumMyRowEntries(int MyRow, int & NumEntries) const;
00350 
00352   HYPRE_IJMatrix& GetMatrix(){ return Matrix_;}
00353 
00355 protected:
00356 
00358   int InitializeDefaults();
00359    
00360   // These methods are needed only because the create methods in Hypre sometimes take an MPI_Comm but not always. 
00361   // They simply call the create solver in the correct way.
00363   int Hypre_BoomerAMGCreate(MPI_Comm comm, HYPRE_Solver *solver)
00364     { return HYPRE_BoomerAMGCreate(solver);}
00365 
00367   int Hypre_ParaSailsCreate(MPI_Comm comm, HYPRE_Solver *solver)
00368     { return HYPRE_ParaSailsCreate(comm, solver);}
00369 
00371   int Hypre_EuclidCreate(MPI_Comm comm, HYPRE_Solver *solver)
00372     { return HYPRE_EuclidCreate(comm, solver);}
00373 
00375   int Hypre_AMSCreate(MPI_Comm comm, HYPRE_Solver *solver)
00376     { return HYPRE_AMSCreate(solver);}
00377 
00379   int Hypre_ParCSRHybridCreate(MPI_Comm comm, HYPRE_Solver *solver)
00380     { return HYPRE_ParCSRHybridCreate(solver);}
00381 
00383   int Hypre_ParCSRPCGCreate(MPI_Comm comm, HYPRE_Solver *solver)
00384     { return HYPRE_ParCSRPCGCreate(comm, solver);}
00385 
00387   int Hypre_ParCSRGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver)
00388     { return HYPRE_ParCSRGMRESCreate(comm, solver);}
00389 
00391   int Hypre_ParCSRFlexGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver)
00392     { return HYPRE_ParCSRFlexGMRESCreate(comm, solver);}
00393 
00395   int Hypre_ParCSRLGMRESCreate(MPI_Comm comm, HYPRE_Solver *solver)
00396     { return HYPRE_ParCSRLGMRESCreate(comm, solver);}
00397 
00399   int Hypre_ParCSRBiCGSTABCreate(MPI_Comm comm, HYPRE_Solver *solver)
00400     { return HYPRE_ParCSRBiCGSTABCreate(comm, solver);}
00401 
00403   int CreateSolver();
00405   int CreatePrecond();
00406   // These two methods setup the solver or preconditioner by calling the pointer. 
00407   // They are const because they are called from the Solve() routine.
00408   // They really aren't const because they change the value of IsSolverSetup_. This is because it should only be called if it isn't setup.
00409   int SetupSolver() const;
00410   int SetupPrecond() const;
00411 
00412   // Hypre variables
00413   mutable HYPRE_IJMatrix Matrix_;
00414   mutable HYPRE_ParCSRMatrix ParMatrix_;
00415   mutable HYPRE_IJVector X_hypre;
00416   mutable HYPRE_IJVector Y_hypre;
00417   mutable HYPRE_ParVector par_x;
00418   mutable HYPRE_ParVector par_y;
00419   mutable hypre_ParVector *x_vec;
00420   mutable hypre_ParVector *y_vec;
00421   mutable hypre_Vector *x_local;
00422   mutable hypre_Vector *y_local;
00423   mutable HYPRE_Solver Solver_;
00424   mutable HYPRE_Solver Preconditioner_;
00425   // The following are pointers to functions to use the solver and preconditioner.
00426   int (EpetraExt_HypreIJMatrix::*SolverCreatePtr_)(MPI_Comm, HYPRE_Solver*);
00427   int (*SolverDestroyPtr_)(HYPRE_Solver);
00428   int (*SolverSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
00429   int (*SolverSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
00430   int (*SolverPrecondPtr_)(HYPRE_Solver, HYPRE_PtrToParSolverFcn, HYPRE_PtrToParSolverFcn, HYPRE_Solver);
00431   int (EpetraExt_HypreIJMatrix::*PrecondCreatePtr_)(MPI_Comm, HYPRE_Solver*);
00432   int (*PrecondDestroyPtr_)(HYPRE_Solver);
00433   int (*PrecondSetupPtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
00434   int (*PrecondSolvePtr_)(HYPRE_Solver, HYPRE_ParCSRMatrix, HYPRE_ParVector, HYPRE_ParVector);
00435     
00437   int NumMyRows_;
00439   int NumGlobalRows_;
00441   int NumGlobalCols_;
00443   int MyRowStart_;
00445   int MyRowEnd_;
00447   mutable int MatType_;
00449   bool TransposeSolve_;
00451   Hypre_Chooser SolveOrPrec_; 
00453   bool *IsSolverSetup_; 
00455   bool *IsPrecondSetup_;
00456 };
00457 #endif /* EPETRAEXT_HYPREIJMATRIX_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines