Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Lapack_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Amesos2: Templated Direct Sparse Solver Package
4 //
5 // Copyright 2011 NTESS and the Amesos2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 
19 #ifndef AMESOS2_LAPACK_DEF_HPP
20 #define AMESOS2_LAPACK_DEF_HPP
21 
22 #include <Teuchos_RCP.hpp>
23 
25 #include "Amesos2_Lapack_decl.hpp"
26 
27 namespace Amesos2 {
28 
29 
30  template <class Matrix, class Vector>
31  Lapack<Matrix,Vector>::Lapack(Teuchos::RCP<const Matrix> A,
32  Teuchos::RCP<Vector> X,
33  Teuchos::RCP<const Vector> B)
34  : SolverCore<Amesos2::Lapack,Matrix,Vector>(A, X, B) // instantiate superclass
35  , is_contiguous_(true)
36  {
37  // Set default parameters
38  Teuchos::RCP<Teuchos::ParameterList> default_params
39  = Teuchos::parameterList( *(this->getValidParameters()) );
40  this->setParameters(default_params);
41  }
42 
43 
44  template <class Matrix, class Vector>
46  {
47  /*
48  * Free any memory allocated by the Lapack library functions (i.e. none)
49  */
50  }
51 
52 
53  template<class Matrix, class Vector>
54  int
56  {
57  return(0);
58  }
59 
60 
61  template <class Matrix, class Vector>
62  int
64  {
65  return(0);
66  }
67 
68 
69  template <class Matrix, class Vector>
70  int
72  {
73  int factor_ierr = 0;
74 
75  if( this->root_ ){
76  // Set here so that solver_ can refresh it's internal state
77  solver_.setMatrix( Teuchos::rcpFromRef(lu_) );
78 
79  {
80 #ifdef HAVE_AMESOS2_TIMERS
81  Teuchos::TimeMonitor numFactTimer( this->timers_.numFactTime_ );
82 #endif
83  factor_ierr = solver_.factor();
84  }
85  }
86 
87  Teuchos::broadcast(*(this->getComm()), 0, &factor_ierr);
88  TEUCHOS_TEST_FOR_EXCEPTION( factor_ierr != 0,
89  std::runtime_error,
90  "Lapack factor routine returned error code "
91  << factor_ierr );
92  return( 0 );
93  }
94 
95 
96  template <class Matrix, class Vector>
97  int
99  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
100  {
101  using Teuchos::as;
102 
103  // Convert X and B to SerialDenseMatrix's
104  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
105  const size_t nrhs = X->getGlobalNumVectors();
106 
107  const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
108  if( this->root_ ){
109  rhsvals_.resize(val_store_size);
110  }
111 
112  { // Get values from RHS B
113 #ifdef HAVE_AMESOS2_TIMERS
114  Teuchos::TimeMonitor mvConvTimer( this->timers_.vecConvTime_ );
115  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
116 #endif
118  scalar_type> copy_helper;
119  copy_helper::do_get(B, rhsvals_(),
120  as<size_t>(ld_rhs),
121  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
122  0);
123  }
124 
125  int solve_ierr = 0;
126  // int unequilibrate_ierr = 0; // unused
127 
128  if( this->root_ ){
129 #ifdef HAVE_AMESOS2_TIMERS
130  Teuchos::TimeMonitor solveTimer( this->timers_.solveTime_ );
131 #endif
132 
133  using Teuchos::rcpFromRef;
134  typedef Teuchos::SerialDenseMatrix<int,scalar_type> DenseMat;
135 
136  DenseMat rhs_dense_mat(Teuchos::View, rhsvals_.getRawPtr(),
137  as<int>(ld_rhs), as<int>(ld_rhs), as<int>(nrhs));
138 
139  solver_.setVectors( rcpFromRef(rhs_dense_mat),
140  rcpFromRef(rhs_dense_mat) );
141 
142  solve_ierr = solver_.solve();
143 
144  // Solution is found in rhsvals_
145  }
146 
147  // Consolidate and check error codes
148  Teuchos::broadcast(*(this->getComm()), 0, &solve_ierr);
149  TEUCHOS_TEST_FOR_EXCEPTION( solve_ierr != 0,
150  std::runtime_error,
151  "Lapack solver solve method returned with error code "
152  << solve_ierr );
153 
154  /* Update X's global values */
155  {
156 #ifdef HAVE_AMESOS2_TIMERS
157  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
158 #endif
159 
161  MultiVecAdapter<Vector>,scalar_type>::do_put(X, rhsvals_(),
162  as<size_t>(ld_rhs),
163  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED);
164  }
165 
166  return( 0 );
167  }
168 
169 
170  template <class Matrix, class Vector>
171  bool
173  {
174  // Factorization of rectangular matrices is supported, but not
175  // their solution. For solution we can have square matrices.
176 
177  return( this->globalNumCols_ == this->globalNumRows_ );
178  }
179 
180 
181  template <class Matrix, class Vector>
182  void
183  Lapack<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
184  {
185  solver_.solveWithTranspose( parameterList->get<bool>("Transpose",
186  this->control_.useTranspose_) );
187 
188  solver_.factorWithEquilibration( parameterList->get<bool>("Equilibrate", true) );
189 
190  if( parameterList->isParameter("IsContiguous") ){
191  is_contiguous_ = parameterList->get<bool>("IsContiguous");
192  }
193 
194  // solver_.solveToRefinedSolution( parameterList->get<bool>("Refine", false) );
195  }
196 
197  template <class Matrix, class Vector>
198  Teuchos::RCP<const Teuchos::ParameterList>
200  {
201  using Teuchos::ParameterList;
202 
203  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
204 
205  if( is_null(valid_params) ){
206  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
207 
208  pl->set("Equilibrate", true, "Whether to equilibrate the input matrix");
209 
210  pl->set("IsContiguous", true, "Whether GIDs contiguous");
211 
212  // TODO: Refinement does not seem to be working with the SerialDenseSolver. Not sure why.
213  // pl->set("Refine", false, "Whether to apply iterative refinement");
214 
215  valid_params = pl;
216  }
217 
218  return valid_params;
219  }
220 
221  template <class Matrix, class Vector>
222  bool
224  {
225 #ifdef HAVE_AMESOS2_TIMERS
226  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
227 #endif
228 
229  // We only load the matrix when numericFactorization is called
230  if( current_phase < NUMFACT ) return( false );
231 
232  if( this->root_ ){
233  Kokkos::resize(nzvals_view_,this->globalNumNonZeros_);
234  Kokkos::resize(rowind_view_,this->globalNumNonZeros_);
235  Kokkos::resize(colptr_view_,this->globalNumCols_ + 1);
236  }
237 
238  // global_size_type nnz_ret = 0;
239  int nnz_ret = 0;
240  {
241 #ifdef HAVE_AMESOS2_TIMERS
242  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
243 #endif
244 
245  // typedef Util::get_ccs_helper<MatrixAdapter<Matrix>,
246  // scalar_type, global_ordinal_type, global_size_type> ccs_helper;
248  host_value_type_array, host_ordinal_type_array, host_ordinal_type_array> ccs_helper;
249 
250  ccs_helper::do_get(this->matrixA_.ptr(),
251  nzvals_view_, rowind_view_, colptr_view_, nnz_ret,
252  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
253  ARBITRARY,
254  this->rowIndexBase_);
255  }
256 
257  if( this->root_ ){
258  // entries are initialized to zero in here:
259  lu_.shape(this->globalNumRows_, this->globalNumCols_);
260 
261  // Put entries of ccs representation into the dense matrix
262  global_size_type end_col = this->globalNumCols_;
263  for( global_size_type col = 0; col < end_col; ++col ){
264  global_ordinal_type ptr = colptr_view_[col];
265  global_ordinal_type end_ptr = colptr_view_[col+1];
266  for( ; ptr < end_ptr; ++ptr ){
267  lu_(rowind_view_[ptr], col) = nzvals_view_[ptr];
268  }
269  }
270 
271  // lu_.print(std::cout);
272  }
273 
274  return( true );
275 }
276 
277 
278  template<class Matrix, class Vector>
279  const char* Lapack<Matrix,Vector>::name = "LAPACK";
280 
281 
282 } // end namespace Amesos2
283 
284 #endif // AMESOS2_LAPACK_DEF_HPP
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return a const parameter list of all of the valid parameters that this-&gt;setParameterList(...) will accept.
Definition: Amesos2_SolverCore_def.hpp:524
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:71
Amesos2 interface to the LAPACK.
Definition: Amesos2_Lapack_decl.hpp:46
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:614
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:31
int symbolicFactorization_impl()
No-op.
Definition: Amesos2_Lapack_def.hpp:63
super_type & setParameters(const Teuchos::RCP< Teuchos::ParameterList > &parameterList) override
Set/update internal variables and solver options.
Definition: Amesos2_SolverCore_def.hpp:492
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:233
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > &parameterList)
Definition: Amesos2_Lapack_def.hpp:183
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Lapack_def.hpp:172
Lapack(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_Lapack_def.hpp:31
int preOrdering_impl()
No-op.
Definition: Amesos2_Lapack_def.hpp:55
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Lapack_def.hpp:199
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
Lapack solve.
Definition: Amesos2_Lapack_def.hpp:98
Declarations for the Amesos2 interface to LAPACK.
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_Lapack_def.hpp:223
~Lapack()
Destructor.
Definition: Amesos2_Lapack_def.hpp:45
Helper class for putting 1-D data arrays into multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:339
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:142
int numericFactorization_impl()
Perform numeric factorization using LAPACK.
Definition: Amesos2_Lapack_def.hpp:71