computation/solver/SparseSolver.hxx

00001 // Copyright (C) 2011 Marc Duruflé
00002 // Copyright (C) 2010 Vivien Mallet
00003 //
00004 // This file is part of the linear-algebra library Seldon,
00005 // http://seldon.sourceforge.net/.
00006 //
00007 // Seldon is free software; you can redistribute it and/or modify it under the
00008 // terms of the GNU Lesser General Public License as published by the Free
00009 // Software Foundation; either version 2.1 of the License, or (at your option)
00010 // any later version.
00011 //
00012 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
00013 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00014 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
00015 // more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public License
00018 // along with Seldon. If not, see http://www.gnu.org/licenses/.
00019 
00020 
00021 #ifndef SELDON_FILE_COMPUTATION_SPARSESOLVER_HXX
00022 #define SELDON_FILE_COMPUTATION_SPARSESOLVER_HXX
00023 
00024 
00025 #include "Ordering.hxx"
00026 
00027 
00028 namespace Seldon
00029 {
00030 
00032   template<class T, class Allocator = SELDON_DEFAULT_ALLOCATOR<T> >
00033   class SparseSeldonSolver
00034   {
00035   protected :
00037     int print_level;
00039     double permtol;
00041     Matrix<T, Symmetric, ArrayRowSymSparse, Allocator> mat_sym;
00043     Matrix<T, General, ArrayRowSparse, Allocator> mat_unsym;
00045     IVect permutation_row, permutation_col;
00047     Vector<T, VectFull, Allocator> xtmp;
00049     bool symmetric_matrix;
00050 
00051   public :
00052 
00053     SparseSeldonSolver();
00054 
00055     void Clear();
00056 
00057     void HideMessages();
00058     void ShowMessages();
00059 
00060     double GetPivotThreshold() const;
00061     void SetPivotThreshold(const double&);
00062 
00063     template<class T0, class Storage0, class Allocator0>
00064     void FactorizeMatrix(const IVect& perm,
00065                          Matrix<T0, General, Storage0, Allocator0>& mat,
00066                          bool keep_matrix = false);
00067 
00068     template<class T0, class Storage0, class Allocator0>
00069     void FactorizeMatrix(const IVect& perm,
00070                          Matrix<T0, Symmetric, Storage0, Allocator0>& mat,
00071                          bool keep_matrix = false);
00072 
00073     template<class Vector1>
00074     void Solve(Vector1& z);
00075 
00076     template<class TransStatus, class Vector1>
00077     void Solve(const TransStatus& TransA, Vector1& z);
00078 
00079   };
00080 
00081 
00083   template<class T>
00084   class SparseDirectSolver
00085   {
00086   protected :
00088     int type_ordering;
00090     int type_solver;
00092     int number_threads_per_node;
00094     IVect permut;
00096     int n;
00097 
00098 #ifdef SELDON_WITH_UMFPACK
00099 
00100     MatrixUmfPack<T> mat_umf;
00101 #endif
00102 #ifdef SELDON_WITH_SUPERLU
00103 
00104     MatrixSuperLU<T> mat_superlu;
00105 #endif
00106 #ifdef SELDON_WITH_MUMPS
00107 
00108     MatrixMumps<T> mat_mumps;
00109 #endif
00110 #ifdef SELDON_WITH_PASTIX
00111 
00112     MatrixPastix<T> mat_pastix;
00113 #endif
00114 
00115 #ifdef SELDON_WITH_PRECONDITIONING
00116 
00117     IlutPreconditioning<double, T, NewAlloc<T> > mat_ilut;
00118 #endif
00119 
00121     double threshold_matrix;
00123     bool enforce_unsym_ilut;
00124 
00126     SparseSeldonSolver<T> mat_seldon;
00127 
00128   public :
00129     // Available solvers.
00130     enum {SELDON_SOLVER, UMFPACK, SUPERLU, MUMPS, PASTIX, ILUT};
00131     // Error codes.
00132     enum {FACTO_OK, STRUCTURALLY_SINGULAR_MATRIX,
00133           NUMERICALLY_SINGULAR_MATRIX, OUT_OF_MEMORY, INVALID_ARGUMENT,
00134           INCORRECT_NUMBER_OF_ROWS, MATRIX_INDICES_INCORRECT,
00135           INVALID_PERMUTATION, ORDERING_FAILED, INTERNAL_ERROR};
00136 
00137     SparseDirectSolver();
00138 
00139     void HideMessages();
00140     void ShowMessages();
00141     void ShowFullHistory();
00142 
00143     void Clear();
00144 
00145     int GetM() const;
00146     int GetN() const;
00147 
00148     int GetTypeOrdering() const;
00149     void SetPermutation(const IVect&);
00150     void SelectOrdering(int);
00151 
00152     void SetNumberThreadPerNode(int m);
00153 
00154     template<class MatrixSparse>
00155     void ComputeOrdering(MatrixSparse& A);
00156 
00157     void SelectDirectSolver(int);
00158     void SetNonSymmetricIlut();
00159 
00160     int GetDirectSolver();
00161 
00162     double GetThresholdMatrix() const;
00163 
00164     template<class MatrixSparse>
00165     void Factorize(MatrixSparse& A, bool keep_matrix = false);
00166 
00167     int GetInfoFactorization(int& ierr) const;
00168 
00169     template<class Vector1>
00170     void Solve(Vector1& x);
00171 
00172     template<class TransStatus, class Vector1>
00173     void Solve(const TransStatus& TransA, Vector1& x);
00174 
00175 #ifdef SELDON_WITH_MPI
00176     template<class Tint>
00177     void FactorizeDistributed(MPI::Comm& comm_facto,
00178                               Vector<Tint>& Ptr, Vector<Tint>& IndRow,
00179                               Vector<T>& Val, const IVect& glob_num,
00180                               bool sym, bool keep_matrix = false);
00181 
00182     template<class Vector1>
00183     void SolveDistributed(MPI::Comm& comm_facto, Vector1& x_solution,
00184                           const IVect& glob_number);
00185 
00186     template<class TransStatus, class Vector1>
00187     void SolveDistributed(MPI::Comm& comm_facto,
00188                           const TransStatus& TransA, Vector1& x_solution,
00189                           const IVect& glob_number);
00190 #endif
00191 
00192 
00193   };
00194 
00195 
00196   template <class T0, class Prop0, class Storage0, class Allocator0,
00197             class T1, class Storage1, class Allocator1>
00198   void SparseSolve(Matrix<T0, Prop0, Storage0, Allocator0>& M,
00199                    Vector<T1, Storage1, Allocator1>& Y);
00200 
00201 
00202   template <class T, class Prop0, class Allocator0, class Allocator1>
00203   void Solve(Matrix<T, Prop0, ColSparse, Allocator0>& M,
00204              Vector<T, VectFull, Allocator1>& Y);
00205 
00206 
00207   template <class T, class Prop0, class Allocator0, class Allocator1>
00208   void Solve(Matrix<T, Prop0, RowSparse, Allocator0>& M,
00209              Vector<T, VectFull, Allocator1>& Y);
00210 
00211 
00212 }  // namespace Seldon.
00213 
00214 
00215 #endif