ParGeMSLR
solver.hpp
Go to the documentation of this file.
1 #ifndef PARGEMSLR_SOLVER_H
2 #define PARGEMSLR_SOLVER_H
3 
9 #include "../utils/utils.hpp"
10 #include "../vectors/vector.hpp"
11 #include "../vectors/sequential_vector.hpp"
12 #include "../vectors/parallel_vector.hpp"
13 #include "../matrices/matrix.hpp"
14 #include "../matrices/csr_matrix.hpp"
15 #include "../matrices/dense_matrix.hpp"
16 #include "../matrices/parallel_csr_matrix.hpp"
17 
18 namespace pargemslr
19 {
20 
26  {
27  kSolverUndefined = -1,
28  kSolverFgmres,
29  kSolverIlu,
30  kSolverBJ,
31  kSolverFloat,
32  kSolverGemslr,
33  kSolverParGemslr
34  };
35 
45  template <class MatrixType, class VectorType, typename DataType>
47  {
48  protected:
49 
55 
61 
66  bool _is_mixed;
67 
72  MatrixType *_matrix;
73 
79 
85 
91 
96  VectorType *_solution;
97 
102  VectorType *_right_hand_side;
103 
108  bool _ready;
109 
115 
121  int CheckReadySetups(const char *str) const
122  {
123  if(this->_ready)
124  {
125  char str1[1024];
126  snprintf( str1, 1024, "Change setup %s after preconditioner is built is invalid.", str );
127  PARGEMSLR_ERROR(str1);
128  return PARGEMSLR_ERROR_FUNCTION_CALL_ERR;
129  }
130  return PARGEMSLR_SUCCESS;
131  }
132 
133  public:
134 
140  {
141  this->_solver_precision = kUnknownPrecision;
142  this->_solver_type = kSolverUndefined;
143  /* if vector type not equal to data type, this is a mixed precision preconditioner */
145  this->_matrix = NULL;
146  this->_own_matrix = false;
147  this->_preconditioner = NULL;
148  this->_own_preconditioner = false;
149  this->_solution = NULL;
150  this->_right_hand_side = NULL;
151  this->_ready = false;
152  this->_print_option = 0;
153  }
154 
161  {
162  this->_solver_precision = solver._solver_precision;
163  this->_solver_type = solver._solver_type;
164  this->_is_mixed = solver._is_mixed;
165  this->_matrix = solver._matrix;
166  this->_own_matrix = false;
167  this->_preconditioner = solver._preconditioner;
168  this->_own_preconditioner = false;
169  this->_right_hand_side = solver._right_hand_side;
170  this->_ready = solver._ready;
171  this->_print_option = solver._print_option;
172  }
173 
180  {
181  this->_solver_precision = solver._solver_precision;
182  solver._solver_precision = kUnknownPrecision;
183  this->_solver_type = solver._solver_type;
184  solver._solver_type = kSolverUndefined;
185  this->_is_mixed = solver._is_mixed;
186  this->_matrix = solver._matrix;
187  solver._matrix = NULL;
188  this->_own_matrix = solver._own_matrix;
189  solver._own_matrix = false;
190  this->_preconditioner = solver._preconditioner;
191  solver._preconditioner = NULL;
192  this->_own_preconditioner = solver._own_preconditioner;
193  solver._own_preconditioner = false;
194  this->_solution = solver._solution;
195  solver._solution = NULL;
196  this->_right_hand_side = solver._right_hand_side;
197  solver._right_hand_side = NULL;
198  this->_ready = solver._ready;
199  solver._ready = false;
200  this->_print_option = solver._print_option;
201  solver._print_option = 0;
202  }
203 
211  {
212  this->Clear();
213  this->_solver_precision = solver._solver_precision;
214  this->_solver_type = solver._solver_type;
215  this->_is_mixed = solver._is_mixed;
216  this->_own_matrix = false;
217  this->_preconditioner = solver._preconditioner;
218  this->_own_preconditioner = false;
219  this->_solution = solver._solution;
220  this->_right_hand_side = solver._right_hand_side;
221  this->_ready = solver._ready;
222  this->_print_option = solver._print_option;
223  return *this;
224  }
225 
233  {
234  this->Clear();
235  this->_solver_precision = solver._solver_precision;
236  solver._solver_precision = kUnknownPrecision;
237  this->_solver_type = solver._solver_type;
238  solver._solver_type = kSolverUndefined;
239  this->_is_mixed = solver._is_mixed;
240  this->_matrix = solver._matrix;
241  solver._matrix = NULL;
242  this->_own_matrix = solver._own_matrix;
243  solver._own_matrix = false;
244  this->_preconditioner = solver._preconditioner;
245  solver._preconditioner = NULL;
246  this->_own_preconditioner = solver._own_preconditioner;
247  solver._own_preconditioner = false;
248  this->_solution = solver._solution;
249  solver._solution = NULL;
250  this->_right_hand_side = solver._right_hand_side;
251  solver._right_hand_side = NULL;
252  this->_ready = solver._ready;
253  solver._ready = false;
254  this->_print_option = solver._print_option;
255  solver._print_option = 0;
256  return *this;
257  }
258 
264  virtual int Clear()
265  {
266 
267  this->_solver_precision = kUnknownPrecision;
268  this->_solver_type = kSolverUndefined;
269 
270  if(this->_matrix && this->_own_matrix)
271  {
272  this->_matrix->Clear();
273  delete this->_matrix;
274  }
275  this->_matrix = NULL;
276 
277  if(this->_preconditioner && this->_own_preconditioner)
278  {
279  this->_preconditioner->Clear();
280  delete this->_preconditioner;
281  }
282  this->_preconditioner = NULL;
283  this->_solution = NULL;
284  this->_right_hand_side = NULL;
285 
286  this->_own_matrix = false;
287  this->_own_preconditioner = false;
288  this->_ready = false;
289  this->_print_option = 0;
290 
291  return PARGEMSLR_SUCCESS;
292  }
293 
298  virtual ~SolverClass()
299  {
300  /* call base clear function */
302  }
303 
311  virtual int Setup( VectorType &x, VectorType &rhs) = 0;
312 
320  virtual int Solve( VectorType &x, VectorType &rhs) = 0;
321 
330  {
331  PARGEMSLR_ERROR("Error: Solve with multiple right-hand-side unimplemented for this class");
332  return PARGEMSLR_ERROR_FUNCTION_CALL_ERR;
333  }
334 
340  virtual long int GetNumNonzeros()
341  {
342  PARGEMSLR_ERROR("Error: GetNumNonzeros unimplemented for this class");
343  return PARGEMSLR_ERROR_FUNCTION_CALL_ERR;
344  }
345 
351  MatrixType* GetMatrix()
352  {
353  return _matrix;
354  }
355 
362  {
363  return _preconditioner;
364  }
365 
372  virtual int SetWithParameterArray(double *params)
373  {
374  PARGEMSLR_ERROR("Error: SetWithParameterArray unimplemented for this class");
375  return PARGEMSLR_ERROR_FUNCTION_CALL_ERR;
376  }
377 
384  virtual int SetSolveLocation( const int &location) = 0;
385 
392  int SetMatrix( MatrixType &matrix)
393  {
394  return this->SetMatrixP(&matrix);
395  }
396 
403  int SetMatrixP( MatrixType *matrix)
404  {
405  if(matrix == this->_matrix)
406  {
407  /* set to the same matrix, do nothing */
408  return PARGEMSLR_SUCCESS;
409  }
410 
411  if(this->_matrix && this->_own_matrix)
412  {
413  this->_matrix->Clear();
414  delete this->_matrix;
415  }
416 
417  /* set a new matrix */
418  this->_matrix = matrix;
419 
420  if(this->_preconditioner && this->_own_preconditioner)
421  {
422  this->_preconditioner->Clear();
423  delete this->_preconditioner;
424  }
425 
426  /* also destroy the preconditioner */
427  this->_preconditioner = NULL;
428  this->_solution = NULL;
429  this->_right_hand_side = NULL;
430 
431  this->_ready = false;
432 
433  this->_own_matrix = false;
434  this->_own_preconditioner = false;
435 
436  return PARGEMSLR_SUCCESS;
437  }
438 
446  {
447  return this->SetPreconditionerP(&precond);
448  }
449 
457  {
458  if(this->_preconditioner && this->_own_preconditioner)
459  {
460  this->_preconditioner->Clear();
461  delete this->_preconditioner;
462  }
463  this->_preconditioner = precond;
464  this->_own_preconditioner = false;
465  return PARGEMSLR_SUCCESS;
466  }
467 
473  bool IsReady() const
474  {
475  return this->_ready;
476  }
477 
484  int SetOwnMatrix(bool own_matrix)
485  {
486  this->_own_matrix = own_matrix;
487  return PARGEMSLR_SUCCESS;
488  }
489 
496  int SetOwnPreconditioner(bool own_preconditioner)
497  {
498  this->_own_preconditioner = own_preconditioner;
499  return PARGEMSLR_SUCCESS;
500  }
501 
508  int SetPrintOption(int print_option)
509  {
510  this->_print_option = print_option;
511  return PARGEMSLR_SUCCESS;
512  }
513 
520  {
521  return this->_solver_precision;
522  }
523 
530  {
531  return this->_solver_type;
532  }
533 
534  };
535 
536  typedef SolverClass<CsrMatrixClass<float>, SequentialVectorClass<float>, float> solver_csr_seq_float;
537  typedef SolverClass<CsrMatrixClass<double>, SequentialVectorClass<double>, float> solver_csr_seq_mix;
538  typedef SolverClass<CsrMatrixClass<double>, SequentialVectorClass<double>, double> solver_csr_seq_double;
539  typedef SolverClass<CsrMatrixClass<complexs>, SequentialVectorClass<complexs>, complexs> solver_csr_seq_complexs;
540  typedef SolverClass<CsrMatrixClass<complexd>, SequentialVectorClass<complexd>, complexs> solver_csr_seq_complexmix;
541  typedef SolverClass<CsrMatrixClass<complexd>, SequentialVectorClass<complexd>, complexd> solver_csr_seq_complexd;
542 
543  typedef SolverClass<ParallelCsrMatrixClass<float>, ParallelVectorClass<float>, float> solver_csr_par_float;
544  typedef SolverClass<ParallelCsrMatrixClass<double>, ParallelVectorClass<double>, double> solver_csr_par_double;
545  typedef SolverClass<ParallelCsrMatrixClass<double>, ParallelVectorClass<double>, double> solver_csr_par_double;
546  typedef SolverClass<ParallelCsrMatrixClass<complexs>, ParallelVectorClass<complexs>, complexs> solver_csr_par_complexs;
547  typedef SolverClass<ParallelCsrMatrixClass<complexd>, ParallelVectorClass<complexd>, complexs> solver_csr_par_complexmix;
548  typedef SolverClass<ParallelCsrMatrixClass<complexd>, ParallelVectorClass<complexd>, complexd> solver_csr_par_complexd;
549 }
550 
551 #endif
pargemslr::SolverClass::_solver_type
SolverTypeEnum _solver_type
The type of the solver.
Definition: solver.hpp:60
pargemslr::SolverClass::_solver_precision
PrecisionEnum _solver_precision
The precision of the solver.
Definition: solver.hpp:54
pargemslr::SolverClass::SolverClass
SolverClass(const SolverClass< MatrixType, VectorType, DataType > &solver)
The copy constructor of solver class.
Definition: solver.hpp:160
pargemslr::DenseMatrixClass< DataType >
pargemslr::SolverClass::_own_matrix
bool _own_matrix
If the matrix is owned by this solver, default is false.
Definition: solver.hpp:78
pargemslr::SolverClass::GetSolverType
SolverTypeEnum GetSolverType() const
Get the solver type.
Definition: solver.hpp:529
pargemslr::SolverClass::SetPreconditionerP
int SetPreconditionerP(SolverClass< MatrixType, VectorType, DataType > *precond)
Set the preconditioner for the current solver.
Definition: solver.hpp:456
pargemslr::SolverClass::SetMatrix
int SetMatrix(MatrixType &matrix)
Set the target matrix for the current solver.
Definition: solver.hpp:392
pargemslr::SolverClass::SetPrintOption
int SetPrintOption(int print_option)
Set the print option.
Definition: solver.hpp:508
pargemslr::SolverTypeEnum
SolverTypeEnum
The solver type.
Definition: solver.hpp:26
pargemslr::SolverClass::Setup
virtual int Setup(VectorType &x, VectorType &rhs)=0
Setup the solver phase, include building the preconditioner. Call this function before Solve,...
pargemslr::SolverClass::SetWithParameterArray
virtual int SetWithParameterArray(double *params)
Setup with parameter array.
Definition: solver.hpp:372
pargemslr::SolverClass::GerPrecision
PrecisionEnum GerPrecision() const
Get the precision.
Definition: solver.hpp:519
pargemslr::SolverClass::~SolverClass
virtual ~SolverClass()
The destructor of solver class.
Definition: solver.hpp:298
pargemslr::SolverClass::Solve
virtual int Solve(VectorType &x, VectorType &rhs)=0
Solve phase. Call this function after Setup.
pargemslr::SolverClass::_own_preconditioner
bool _own_preconditioner
If the preconditioner is owned by this solver, default is false.
Definition: solver.hpp:90
pargemslr::SolverClass::_ready
bool _ready
If the solver is ready.
Definition: solver.hpp:108
pargemslr::SolverClass
The base solver class.
Definition: solver.hpp:47
pargemslr::SolverClass::_is_mixed
bool _is_mixed
Is the preconditioner a mixed precision preconditioner?
Definition: solver.hpp:66
pargemslr::SolverClass::SetOwnPreconditioner
int SetOwnPreconditioner(bool own_preconditioner)
Set if the preconditioner is owned by this solver, if so, preconditioner is freed when free the solve...
Definition: solver.hpp:496
pargemslr::PargemslrIsDoublePrecision
Tell if a value is in double precision.
Definition: utils.hpp:433
pargemslr::SolverClass::_print_option
int _print_option
The print option.
Definition: solver.hpp:114
pargemslr::SolverClass::SetMatrixP
int SetMatrixP(MatrixType *matrix)
Set the target matrix for the current solver.
Definition: solver.hpp:403
pargemslr::SolverClass::SolverClass
SolverClass()
The constructor of solver class.
Definition: solver.hpp:139
pargemslr::SolverClass::SetOwnMatrix
int SetOwnMatrix(bool own_matrix)
Set if the matrix is owned by this solver, if so, matrix is freed when free the solver.
Definition: solver.hpp:484
pargemslr::SolverClass::GetMatrix
MatrixType * GetMatrix()
Get pointer to the matrix.
Definition: solver.hpp:351
pargemslr::SolverClass::GetNumNonzeros
virtual long int GetNumNonzeros()
Get the total number of nonzeros.
Definition: solver.hpp:340
pargemslr::SolverClass::_solution
VectorType * _solution
Pointer to the solution, note that this vector is not going to be freed.
Definition: solver.hpp:96
pargemslr::SolverClass::_matrix
MatrixType * _matrix
The matrix.
Definition: solver.hpp:72
pargemslr::SolverClass::GetPreconditioner
SolverClass< MatrixType, VectorType, DataType > * GetPreconditioner()
Get pointer to the preconditioner.
Definition: solver.hpp:361
pargemslr::SolverClass::_right_hand_side
VectorType * _right_hand_side
Pointer to the right-hand-size, note that this vector is not going to be freed.
Definition: solver.hpp:102
pargemslr::SolverClass::_preconditioner
SolverClass< MatrixType, VectorType, DataType > * _preconditioner
The preconditioner.
Definition: solver.hpp:84
pargemslr::PrecisionEnum
PrecisionEnum
The precision enum.
Definition: utils.hpp:453
pargemslr::SolverClass::SetSolveLocation
virtual int SetSolveLocation(const int &location)=0
Set the data location that the solver apply to. Will also be applied to the preconditioner.
pargemslr::SolverClass::SetPreconditioner
int SetPreconditioner(SolverClass< MatrixType, VectorType, DataType > &precond)
Set the preconditioner for the current solver.
Definition: solver.hpp:445
pargemslr::SolverClass::operator=
SolverClass< MatrixType, VectorType, DataType > & operator=(const SolverClass< MatrixType, VectorType, DataType > &solver)
The = operator of solver class.
Definition: solver.hpp:210
pargemslr::SolverClass::SolverClass
SolverClass(SolverClass< MatrixType, VectorType, DataType > &&solver)
The move constructor of solver class.
Definition: solver.hpp:179
pargemslr::SolverClass::CheckReadySetups
int CheckReadySetups(const char *str) const
Return error is the preconditioner is ready.
Definition: solver.hpp:121
pargemslr::SolverClass::Solve
virtual int Solve(DenseMatrixClass< DataType > &x, DenseMatrixClass< DataType > &rhs)
Solve phase with multiple right-hand-sides. Call this function after Setup.
Definition: solver.hpp:329
pargemslr::SolverClass::Clear
virtual int Clear()
Free the current solver.
Definition: solver.hpp:264
pargemslr::SolverClass::IsReady
bool IsReady() const
Check if the solver is ready to be used.
Definition: solver.hpp:473