SparseDirectSolver.hxx
1 // Copyright (C) 2015 Marc DuruflĂ©
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 #ifndef SELDON_FILE_COMPUTATION_SPARSE_DIRECT_SOLVER_HXX
20 
21 namespace Seldon
22 {
23 
25  template<class T>
27  {
28  protected :
38  int n;
43  double pivot_threshold;
44  bool refine_solution;
45  int print_level;
48 
49  public :
50  // available solvers
51  enum {SELDON_SOLVER, UMFPACK, SUPERLU, MUMPS, PASTIX, ILUT, PARDISO, WSMP};
52 
53  // error codes
54  enum {FACTO_OK, STRUCTURALLY_SINGULAR_MATRIX,
55  NUMERICALLY_SINGULAR_MATRIX, OUT_OF_MEMORY, INVALID_ARGUMENT,
56  INCORRECT_NUMBER_OF_ROWS, MATRIX_INDICES_INCORRECT,
57  INVALID_PERMUTATION, ORDERING_FAILED, INTERNAL_ERROR,
58  OVERFLOW_32BIT};
59 
61 
62  // Inline methods
63  int GetM() const;
64  int GetN() const;
65 
66  int GetTypeOrdering() const;
67  void SetPermutation(const IVect&);
68  void SelectOrdering(int);
69 
70  void HideMessages();
71  void ShowMessages();
72  void ShowFullHistory();
73 
74  void SetPivotThreshold(const double&);
75  void SetNumberOfThreadPerNode(int m);
76  int GetNumberOfThreadPerNode() const;
77 
78  void SelectDirectSolver(int);
79  void SetNonSymmetricIlut();
80 
81  int GetDirectSolver();
82 
83  void RefineSolution();
84  void DoNotRefineSolution();
85 
86  void SetCoefficientEstimationNeededMemory(double);
87  void SetMaximumCoefficientEstimationNeededMemory(double);
88  void SetIncreaseCoefficientEstimationNeededMemory(double);
89 
90  double GetThresholdMatrix() const;
91 
92  // other methods
94 
95  void Clear();
96 
97  static bool IsAvailableSolver(int type);
98 
99  protected:
100  // internal methods
101  bool AffectOrdering();
102 
103  template<class T0, class Prop, class Storage, class Alloc>
104  void ComputeOrdering(Matrix<T0, Prop, Storage, Alloc>& A);
105 
106  void InitSolver();
107 
108  public:
109  void SetThresholdMatrix(const double&);
110 
111  template<class Prop, class Storage, class Allocator>
112  void Factorize(Matrix<T, Prop, Storage, Allocator>& A,
113  bool keep_matrix = false);
114 
115  template<class Prop, class Storage, class Allocator>
116  void PerformAnalysis(Matrix<T, Prop, Storage, Allocator>& A);
117 
118  template<class Prop, class Storage, class Allocator>
119  void PerformFactorization(Matrix<T, Prop, Storage, Allocator>& A);
120 
121  int GetInfoFactorization(int& ierr) const;
122 
123  void Solve(Vector<T>& x);
124  void Solve(const SeldonTranspose& TransA, Vector<T>& x, bool assemble = true);
125 
126  void Solve(Matrix<T, General, ColMajor>& x);
127  void Solve(const SeldonTranspose&, Matrix<T, General, ColMajor>& x);
128 
129 #ifdef SELDON_WITH_MPI
130  template<class Tint0, class Tint1>
131  void FactorizeDistributed(MPI_Comm& comm_facto,
132  Vector<Tint0>& Ptr, Vector<Tint1>& IndRow,
133  Vector<T>& Val, const IVect& glob_num,
134  bool sym, bool keep_matrix = false);
135 
136  template<class Tint0, class Tint1>
137  void PerformAnalysisDistributed(MPI_Comm& comm_facto,
138  Vector<Tint0>& Ptr, Vector<Tint1>& IndRow,
139  Vector<T>& Val, const IVect& glob_num,
140  bool sym, bool keep_matrix = false);
141 
142  template<class Tint0, class Tint1>
143  void PerformFactorizationDistributed(MPI_Comm& comm_facto,
144  Vector<Tint0>& Ptr, Vector<Tint1>& IndRow,
145  Vector<T>& Val, const IVect& glob_num,
146  bool sym, bool keep_matrix = false);
147 
148  void SolveDistributed(MPI_Comm& comm_facto, Vector<T>& x_solution,
149  const IVect& glob_number);
150 
151  void SolveDistributed(MPI_Comm& comm_facto,
152  const SeldonTranspose& TransA, Vector<T>& x_solution,
153  const IVect& glob_number);
154 
155  void SolveDistributed(MPI_Comm& comm_facto,
156  Matrix<T, General, ColMajor>& x_solution,
157  const IVect& glob_number);
158 
159  void SolveDistributed(MPI_Comm& comm_facto,
160  const SeldonTranspose& TransA,
161  Matrix<T, General, ColMajor>& x_solution,
162  const IVect& glob_number);
163 #endif
164 
165  };
166 
167 #ifdef SELDON_WITH_MPI
168  template<class T>
169  void SolveLU_Distributed(MPI_Comm& comm, const SeldonTranspose& transA,
170  SparseDirectSolver<T>& mat_lu,
171  Vector<T>& x, Vector<int>& global_col);
172 
173  template<class T>
174  void SolveLU_Distributed(MPI_Comm& comm, const SeldonTranspose& transA,
175  SparseDirectSolver<complex<T> >& mat_lu,
176  Vector<T>& x, Vector<int>& global_col);
177 
178  template<class T>
179  void SolveLU_Distributed(MPI_Comm& comm, const SeldonTranspose& transA,
180  SparseDirectSolver<T>& mat_lu,
181  Vector<complex<T> >& x, Vector<int>& global_col);
182 
183  template<class T>
184  void SolveLU_Distributed(MPI_Comm& comm, const SeldonTranspose& transA,
185  SparseDirectSolver<T>& mat_lu,
186  Matrix<T, General, ColMajor>& x, Vector<int>& global_col);
187 
188  template<class T>
189  void SolveLU_Distributed(MPI_Comm& comm, const SeldonTranspose& transA,
190  SparseDirectSolver<complex<T> >& mat_lu,
191  Matrix<T, General, ColMajor>& x, Vector<int>& global_col);
192 
193  template<class T>
194  void SolveLU_Distributed(MPI_Comm& comm, const SeldonTranspose& transA,
195  SparseDirectSolver<T>& mat_lu,
196  Matrix<complex<T>, General, ColMajor>& x,
197  Vector<int>& global_col);
198 #endif
199 
200  template<class T0, class Prop, class Storage, class Allocator, class T>
201  void GetLU(Matrix<T0, Prop, Storage, Allocator>& A, SparseDirectSolver<T>& mat_lu,
202  bool keep_matrix = false);
203 
204  template<class T>
205  void SolveLU(const SeldonTranspose& transA,
206  SparseDirectSolver<T>& mat_lu, Vector<T>& x);
207 
208  template<class T>
209  void SolveLU(const SeldonTranspose& transA,
210  SparseDirectSolver<complex<T> >& mat_lu, Vector<T>& x);
211 
212  template<class T>
213  void SolveLU(const SeldonTranspose& transA,
214  SparseDirectSolver<T>& mat_lu, Vector<complex<T> >& x);
215 
216  template<class T>
217  void SolveLU(const SeldonTranspose& transA,
218  SparseDirectSolver<T>& mat_lu,
219  Matrix<T, General, ColMajor>& x);
220 
221  template<class T>
222  void SolveLU(const SeldonTranspose& transA,
223  SparseDirectSolver<complex<T> >& mat_lu,
224  Matrix<T, General, ColMajor>& x);
225 
226  template<class T>
227  void SolveLU(const SeldonTranspose& transA,
228  SparseDirectSolver<T>& mat_lu,
229  Matrix<complex<T>, General, ColMajor>& x);
230 
231  // SparseSolve and GetAndSolveLU
232  template <class T0, class Prop0, class Storage0, class Allocator0,
233  class T1, class Storage1, class Allocator1>
234  void SparseSolve(Matrix<T0, Prop0, Storage0, Allocator0>& M,
235  Vector<T1, Storage1, Allocator1>& Y);
236 
237 
238  template <class T, class Prop0, class Allocator0, class Allocator1>
239  void GetAndSolveLU(Matrix<T, Prop0, ColSparse, Allocator0>& M,
240  Vector<T, VectFull, Allocator1>& Y);
241 
242  template <class T, class Prop0, class Allocator0, class Allocator1>
243  void GetAndSolveLU(Matrix<T, Prop0, RowSparse, Allocator0>& M,
244  Vector<T, VectFull, Allocator1>& Y);
245 
246  template <class T, class Prop0, class Allocator0, class Allocator1>
247  void GetAndSolveLU(Matrix<T, Prop0, ArrayRowSparse, Allocator0>& M,
248  Vector<T, VectFull, Allocator1>& Y);
249 
250  template <class T, class Prop0, class Allocator0, class Allocator1>
251  void GetAndSolveLU(Matrix<T, Prop0, ColSymSparse, Allocator0>& M,
252  Vector<T, VectFull, Allocator1>& Y);
253 
254  template <class T, class Prop0, class Allocator0, class Allocator1>
255  void GetAndSolveLU(Matrix<T, Prop0, RowSymSparse, Allocator0>& M,
256  Vector<T, VectFull, Allocator1>& Y);
257 
258  template <class T, class Prop0, class Allocator0, class Allocator1>
259  void GetAndSolveLU(Matrix<T, Prop0, ArrayRowSymSparse, Allocator0>& M,
260  Vector<T, VectFull, Allocator1>& Y);
261 
262 }
263 
264 #define SELDON_FILE_COMPUTATION_SPARSE_DIRECT_SOLVER_HXX
265 #endif
Seldon::SparseDirectSolver::InitSolver
void InitSolver()
initializes the solver (internal method)
Definition: SparseDirectSolver.cxx:398
Seldon::SparseDirectSolver::Factorize
void Factorize(Matrix< T, Prop, Storage, Allocator > &A, bool keep_matrix=false)
factorisation of matrix A
Definition: SparseDirectSolver.cxx:503
Seldon::SparseDirectSolver::solver
VirtualSparseDirectSolver< T > * solver
pointer to the used solver
Definition: SparseDirectSolver.hxx:40
Seldon::SparseDirectSolver::enforce_unsym_ilut
bool enforce_unsym_ilut
use of non-symmetric ilut ?
Definition: SparseDirectSolver.hxx:47
Seldon::SparseDirectSolver::SparseDirectSolver
SparseDirectSolver()
Default constructor.
Definition: SparseDirectSolver.cxx:28
Seldon::SparseDirectSolver::SetPermutation
void SetPermutation(const IVect &)
sets directly the new ordering (by giving a permutation vector)
Definition: SparseDirectSolverInline.cxx:50
Seldon::SparseDirectSolver::ComputeOrdering
void ComputeOrdering(Matrix< T0, Prop, Storage, Alloc > &A)
computation of the permutation vector in order to reduce fill-in
Definition: SparseDirectSolver.cxx:367
Seldon::SparseDirectSolver::Solve
void Solve(Vector< T > &x)
x_solution is overwritten by solution of A x = b
Definition: SparseDirectSolver.cxx:783
Seldon::SparseDirectSolver::GetM
int GetM() const
returns the number of rows of the factorised matrix
Definition: SparseDirectSolverInline.cxx:26
Seldon::SparseDirectSolver::SetThresholdMatrix
void SetThresholdMatrix(const double &)
modifies threshold used for ilut
Definition: SparseDirectSolver.cxx:385
Seldon::Vector< int, VectFull >
Seldon::SparseDirectSolver::SelectOrdering
void SelectOrdering(int)
modifies the ordering algorithm to use
Definition: SparseDirectSolverInline.cxx:59
Seldon::SparseDirectSolver::SelectDirectSolver
void SelectDirectSolver(int)
modifies the direct solver to use
Definition: SparseDirectSolverInline.cxx:120
Seldon::GetAndSolveLU
void GetAndSolveLU(Matrix< T0, Prop0, Storage0, Allocator0 > &M, Vector< T1, Storage1, Allocator1 > &Y)
Solves a linear system using LU factorization.
Definition: Functions_MatVect.cxx:1841
Seldon::SparseDirectSolver
Class grouping different direct solvers.
Definition: SparseDirectSolver.hxx:26
Seldon::SparseDirectSolver::HideMessages
void HideMessages()
hiding all messages
Definition: SparseDirectSolverInline.cxx:67
Seldon::SparseDirectSolver::GetInfoFactorization
int GetInfoFactorization(int &ierr) const
Returns error code of the direct solver.
Definition: SparseDirectSolver.cxx:661
Seldon::SparseSolve
void SparseSolve(Matrix< T0, Prop0, Storage0, Allocator0 > &M, Vector< T1, Storage1, Allocator1 > &Y)
Solves a sparse linear system using LU factorization.
Definition: SparseDirectSolverInline.cxx:217
Seldon::SparseDirectSolver::IsAvailableSolver
static bool IsAvailableSolver(int type)
returns true if the solver type is available
Definition: SparseDirectSolver.cxx:96
Seldon::SparseDirectSolver::type_ordering
int type_ordering
ordering to use
Definition: SparseDirectSolver.hxx:30
Seldon::SparseDirectSolver::GetNumberOfThreadPerNode
int GetNumberOfThreadPerNode() const
modifies the number of threads per node (for Pastix only)
Definition: SparseDirectSolverInline.cxx:112
Seldon::SparseDirectSolver::SetNonSymmetricIlut
void SetNonSymmetricIlut()
enforces the use of unsymmetric algorithm for ilut solver
Definition: SparseDirectSolverInline.cxx:130
Seldon::SparseDirectSolver::Clear
void Clear()
clearing factorisation
Definition: SparseDirectSolver.cxx:84
Seldon::SparseDirectSolver::SetNumberOfThreadPerNode
void SetNumberOfThreadPerNode(int m)
modifies the number of threads per node (for Pastix only)
Definition: SparseDirectSolverInline.cxx:103
Seldon::GetLU
void GetLU(Matrix< T0, Prop0, Storage0, Allocator0 > &A)
Returns the LU factorization of a matrix.
Definition: Functions_Matrix.cxx:2073
Seldon::VirtualSparseDirectSolver
Base class for an interface with a direct solver.
Definition: SparseSolver.hxx:30
Seldon::SparseDirectSolver::ShowMessages
void ShowMessages()
displaying basic messages
Definition: SparseDirectSolverInline.cxx:76
Seldon::SparseDirectSolver::PerformAnalysis
void PerformAnalysis(Matrix< T, Prop, Storage, Allocator > &A)
Performs the analysis of the matrix before numerical factorization.
Definition: SparseDirectSolver.cxx:611
Seldon::SparseDirectSolver::PerformFactorization
void PerformFactorization(Matrix< T, Prop, Storage, Allocator > &A)
Performs the numerical factorization.
Definition: SparseDirectSolver.cxx:637
Seldon::SparseDirectSolver::GetThresholdMatrix
double GetThresholdMatrix() const
returns threshold used for ilut (if this solver is selected)
Definition: SparseDirectSolverInline.cxx:186
Seldon::SparseDirectSolver::SetPivotThreshold
void SetPivotThreshold(const double &)
sets the threshold used for pivoting
Definition: SparseDirectSolverInline.cxx:94
Seldon::SparseDirectSolver::GetDirectSolver
int GetDirectSolver()
returns the direct solver to use
Definition: SparseDirectSolverInline.cxx:138
Seldon::SparseDirectSolver::ShowFullHistory
void ShowFullHistory()
displaying all the messages
Definition: SparseDirectSolverInline.cxx:85
Seldon::SparseDirectSolver::permut
IVect permut
ordering (if supplied by the user)
Definition: SparseDirectSolver.hxx:36
Seldon::SparseDirectSolver::type_solver
int type_solver
solver to use
Definition: SparseDirectSolver.hxx:32
Seldon
Seldon namespace.
Definition: Array.cxx:24
Seldon::SparseDirectSolver::n
int n
size of factorized linear system
Definition: SparseDirectSolver.hxx:38
Seldon::SparseDirectSolver::GetTypeOrdering
int GetTypeOrdering() const
returns the ordering algorithm to use
Definition: SparseDirectSolverInline.cxx:42
Seldon::SparseDirectSolver::nb_threads_per_node
int nb_threads_per_node
number of threads (for Pastix)
Definition: SparseDirectSolver.hxx:34
Seldon::SparseDirectSolver::GetN
int GetN() const
returns the number of rows of the factorised matrix
Definition: SparseDirectSolverInline.cxx:34
Seldon::SparseDirectSolver::threshold_matrix
double threshold_matrix
threshold for ilut solver
Definition: SparseDirectSolver.hxx:42
Seldon::SparseDirectSolver::~SparseDirectSolver
~SparseDirectSolver()
Destructor.
Definition: SparseDirectSolver.cxx:75