52 #ifndef AMESOS2_KLU2_DEF_HPP
53 #define AMESOS2_KLU2_DEF_HPP
55 #include <Teuchos_Tuple.hpp>
56 #include <Teuchos_ParameterList.hpp>
57 #include <Teuchos_StandardParameterEntryValidators.hpp>
65 template <
class Matrix,
class Vector>
67 Teuchos::RCP<const Matrix> A,
68 Teuchos::RCP<Vector> X,
69 Teuchos::RCP<const Vector> B )
75 , is_contiguous_(true)
77 ::KLU2::klu_defaults<slu_type, local_ordinal_type> (&(data_.common_)) ;
78 data_.symbolic_ = NULL;
79 data_.numeric_ = NULL;
86 template <
class Matrix,
class Vector>
94 if (data_.symbolic_ != NULL)
95 ::KLU2::klu_free_symbolic<slu_type, local_ordinal_type>
96 (&(data_.symbolic_), &(data_.common_)) ;
97 if (data_.numeric_ != NULL)
98 ::KLU2::klu_free_numeric<slu_type, local_ordinal_type>
99 (&(data_.numeric_), &(data_.common_)) ;
112 template <
class Matrix,
class Vector>
115 return (this->root_ && (this->matrixA_->getComm()->getSize() == 1) && is_contiguous_);
118 template<
class Matrix,
class Vector>
124 #ifdef HAVE_AMESOS2_TIMERS
125 Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_);
132 template <
class Matrix,
class Vector>
136 if (data_.symbolic_ != NULL) {
137 ::KLU2::klu_free_symbolic<slu_type, local_ordinal_type>
138 (&(data_.symbolic_), &(data_.common_)) ;
141 if ( single_proc_optimization() ) {
143 auto sp_rowptr = this->matrixA_->returnRowPtr();
144 TEUCHOS_TEST_FOR_EXCEPTION(sp_rowptr ==
nullptr,
145 std::runtime_error,
"Amesos2 Runtime Error: sp_rowptr returned null ");
146 auto sp_colind = this->matrixA_->returnColInd();
147 TEUCHOS_TEST_FOR_EXCEPTION(sp_colind ==
nullptr,
148 std::runtime_error,
"Amesos2 Runtime Error: sp_colind returned null ");
149 #ifndef HAVE_TEUCHOS_COMPLEX
150 auto sp_values = this->matrixA_->returnValues();
154 using complex_type =
typename Util::getStdCplxType< magnitude_type, typename matrix_adapter_type::spmtx_vals_t >::type;
155 complex_type * sp_values =
nullptr;
156 sp_values =
reinterpret_cast< complex_type *
> ( this->matrixA_->returnValues() );
158 TEUCHOS_TEST_FOR_EXCEPTION(sp_values ==
nullptr,
159 std::runtime_error,
"Amesos2 Runtime Error: sp_values returned null ");
164 Teuchos::Array< local_ordinal_type > sp_rowptr_with_common_type( this->globalNumRows_ + 1 );
165 for ( global_size_type i = 0; i < this->globalNumRows_+1; ++i ) {
167 sp_rowptr_with_common_type[i] = sp_rowptr[i];
170 data_.symbolic_ = ::KLU2::klu_analyze<slu_type, local_ordinal_type>
171 ((local_ordinal_type)this->globalNumCols_, sp_rowptr_with_common_type.getRawPtr(),
172 sp_colind, &(data_.common_)) ;
176 data_.symbolic_ = ::KLU2::klu_analyze<slu_type, local_ordinal_type>
177 ((local_ordinal_type)this->globalNumCols_, colptr_.getRawPtr(),
178 rowind_.getRawPtr(), &(data_.common_)) ;
186 template <
class Matrix,
class Vector>
200 #ifdef HAVE_AMESOS2_TIMERS
201 Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
204 #ifdef HAVE_AMESOS2_VERBOSE_DEBUG
205 std::cout <<
"KLU2:: Before numeric factorization" << std::endl;
206 std::cout <<
"nzvals_ : " << nzvals_.toString() << std::endl;
207 std::cout <<
"rowind_ : " << rowind_.toString() << std::endl;
208 std::cout <<
"colptr_ : " << colptr_.toString() << std::endl;
211 if (data_.numeric_ != NULL) {
212 ::KLU2::klu_free_numeric<slu_type, local_ordinal_type>
213 (&(data_.numeric_), &(data_.common_)) ;
216 if ( single_proc_optimization() ) {
218 auto sp_rowptr = this->matrixA_->returnRowPtr();
219 TEUCHOS_TEST_FOR_EXCEPTION(sp_rowptr ==
nullptr,
220 std::runtime_error,
"Amesos2 Runtime Error: sp_rowptr returned null ");
221 auto sp_colind = this->matrixA_->returnColInd();
222 TEUCHOS_TEST_FOR_EXCEPTION(sp_colind ==
nullptr,
223 std::runtime_error,
"Amesos2 Runtime Error: sp_colind returned null ");
224 #ifndef HAVE_TEUCHOS_COMPLEX
225 auto sp_values = this->matrixA_->returnValues();
229 using complex_type =
typename Util::getStdCplxType< magnitude_type, typename matrix_adapter_type::spmtx_vals_t >::type;
230 complex_type * sp_values =
nullptr;
231 sp_values =
reinterpret_cast< complex_type *
> ( this->matrixA_->returnValues() );
233 TEUCHOS_TEST_FOR_EXCEPTION(sp_values ==
nullptr,
234 std::runtime_error,
"Amesos2 Runtime Error: sp_values returned null ");
239 Teuchos::Array< local_ordinal_type > sp_rowptr_with_common_type( this->globalNumRows_ + 1 );
240 for ( global_size_type i = 0; i < this->globalNumRows_+1; ++i ) {
242 sp_rowptr_with_common_type[i] = sp_rowptr[i];
245 data_.numeric_ = ::KLU2::klu_factor<slu_type, local_ordinal_type>
246 (sp_rowptr_with_common_type.getRawPtr(), sp_colind, sp_values,
247 data_.symbolic_, &(data_.common_)) ;
250 data_.numeric_ = ::KLU2::klu_factor<slu_type, local_ordinal_type>
251 (colptr_.getRawPtr(), rowind_.getRawPtr(), nzvals_.getRawPtr(),
252 data_.symbolic_, &(data_.common_)) ;
257 this->setNnzLU( as<size_t>((data_.numeric_)->lnz) + as<size_t>((data_.numeric_)->unz) );
263 Teuchos::broadcast(*(this->matrixA_->getComm()), 0, &info);
281 template <
class Matrix,
class Vector>
290 const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
291 const size_t nrhs = X->getGlobalNumVectors();
293 if ( single_proc_optimization() && nrhs == 1 ) {
294 #ifdef HAVE_AMESOS2_TIMERS
295 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
298 #ifndef HAVE_TEUCHOS_COMPLEX
304 using complex_type =
typename Util::getStdCplxType< magnitude_type, typename matrix_adapter_type::spmtx_vals_t >::type;
308 TEUCHOS_TEST_FOR_EXCEPTION(b_vector ==
nullptr,
309 std::runtime_error,
"Amesos2 Runtime Error: b_vector returned null ");
311 TEUCHOS_TEST_FOR_EXCEPTION(x_vector ==
nullptr,
312 std::runtime_error,
"Amesos2 Runtime Error: x_vector returned null ");
319 ::KLU2::klu_tsolve2<slu_type, local_ordinal_type>
320 (data_.symbolic_, data_.numeric_,
321 (local_ordinal_type)this->globalNumCols_,
322 (local_ordinal_type)nrhs,
323 b_vector, x_vector, &(data_.common_)) ;
326 ::KLU2::klu_solve2<slu_type, local_ordinal_type>
327 (data_.symbolic_, data_.numeric_,
328 (local_ordinal_type)this->globalNumCols_,
329 (local_ordinal_type)nrhs,
330 b_vector, x_vector, &(data_.common_)) ;
334 Teuchos::broadcast(*(this->getComm()), 0, &ierr);
340 const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
341 Teuchos::Array<slu_type> bValues(val_store_size);
344 #ifdef HAVE_AMESOS2_TIMERS
345 Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
346 Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
348 if ( is_contiguous_ ==
true ) {
350 slu_type>::do_get(B, bValues(),
352 ROOTED, this->rowIndexBase_);
356 slu_type>::do_get(B, bValues(),
358 CONTIGUOUS_AND_ROOTED, this->rowIndexBase_);
365 #ifdef HAVE_AMESOS2_TIMERS
366 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
373 if ( single_proc_optimization() ) {
374 ::KLU2::klu_tsolve<slu_type, local_ordinal_type>
375 (data_.symbolic_, data_.numeric_,
376 (local_ordinal_type)this->globalNumCols_,
377 (local_ordinal_type)nrhs,
378 bValues.getRawPtr(), &(data_.common_)) ;
381 ::KLU2::klu_solve<slu_type, local_ordinal_type>
382 (data_.symbolic_, data_.numeric_,
383 (local_ordinal_type)this->globalNumCols_,
384 (local_ordinal_type)nrhs,
385 bValues.getRawPtr(), &(data_.common_)) ;
393 if ( single_proc_optimization() ) {
394 ::KLU2::klu_solve<slu_type, local_ordinal_type>
395 (data_.symbolic_, data_.numeric_,
396 (local_ordinal_type)this->globalNumCols_,
397 (local_ordinal_type)nrhs,
398 bValues.getRawPtr(), &(data_.common_)) ;
401 ::KLU2::klu_tsolve<slu_type, local_ordinal_type>
402 (data_.symbolic_, data_.numeric_,
403 (local_ordinal_type)this->globalNumCols_,
404 (local_ordinal_type)nrhs,
405 bValues.getRawPtr(), &(data_.common_)) ;
412 Teuchos::broadcast(*(this->getComm()), 0, &ierr);
429 #ifdef HAVE_AMESOS2_TIMERS
430 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
433 if ( is_contiguous_ ==
true ) {
437 ROOTED, this->rowIndexBase_);
443 CONTIGUOUS_AND_ROOTED, this->rowIndexBase_);
452 template <
class Matrix,
class Vector>
459 return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
463 template <
class Matrix,
class Vector>
468 using Teuchos::getIntegralValue;
469 using Teuchos::ParameterEntryValidator;
471 RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
473 transFlag_ = this->control_.useTranspose_ ? 1: 0;
475 if( parameterList->isParameter(
"Trans") ){
476 RCP<const ParameterEntryValidator> trans_validator = valid_params->getEntry(
"Trans").validator();
477 parameterList->getEntry(
"Trans").setValidator(trans_validator);
479 transFlag_ = getIntegralValue<int>(*parameterList,
"Trans");
482 if( parameterList->isParameter(
"IsContiguous") ){
483 is_contiguous_ = parameterList->get<
bool>(
"IsContiguous");
488 template <
class Matrix,
class Vector>
489 Teuchos::RCP<const Teuchos::ParameterList>
493 using Teuchos::tuple;
494 using Teuchos::ParameterList;
495 using Teuchos::setStringToIntegralParameter;
497 static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
499 if( is_null(valid_params) )
501 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
503 pl->set(
"Equil",
true,
"Whether to equilibrate the system before solve, does nothing now");
504 pl->set(
"IsContiguous",
true,
"Whether GIDs contiguous");
506 setStringToIntegralParameter<int>(
"Trans",
"NOTRANS",
507 "Solve for the transpose system or not",
508 tuple<string>(
"NOTRANS",
"TRANS",
"CONJ"),
509 tuple<string>(
"Solve with transpose",
510 "Do not solve with transpose",
511 "Solve with the conjugate transpose"),
521 template <
class Matrix,
class Vector>
527 if(current_phase == SOLVE)
return(
false);
529 if ( single_proc_optimization() ) {
535 #ifdef HAVE_AMESOS2_TIMERS
536 Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
541 nzvals_.resize(this->globalNumNonZeros_);
542 rowind_.resize(this->globalNumNonZeros_);
543 colptr_.resize(this->globalNumCols_ + 1);
546 local_ordinal_type nnz_ret = 0;
548 #ifdef HAVE_AMESOS2_TIMERS
549 Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
552 if ( is_contiguous_ ==
true ) {
555 ::do_get(this->matrixA_.ptr(), nzvals_(), rowind_(), colptr_(),
556 nnz_ret, ROOTED, ARBITRARY, this->rowIndexBase_);
561 ::do_get(this->matrixA_.ptr(), nzvals_(), rowind_(), colptr_(),
562 nnz_ret, CONTIGUOUS_AND_ROOTED, ARBITRARY, this->rowIndexBase_);
568 TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != as<local_ordinal_type>(this->globalNumNonZeros_),
570 "Did not get the expected number of non-zero vals");
579 template<
class Matrix,
class Vector>
585 #endif // AMESOS2_KLU2_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
KLU2(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_KLU2_def.hpp:66
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
KLU2 specific solve.
Definition: Amesos2_KLU2_def.hpp:283
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
Amesos2 KLU2 declarations.
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_KLU2_def.hpp:523
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:266
~KLU2()
Destructor.
Definition: Amesos2_KLU2_def.hpp:87
Helper struct for getting pointers to the MV data - only used when number of vectors = 1 and single M...
Definition: Amesos2_MultiVecAdapter_decl.hpp:218
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > ¶meterList)
Definition: Amesos2_KLU2_def.hpp:465
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using KLU2.
Definition: Amesos2_KLU2_def.hpp:134
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:654
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_KLU2_def.hpp:120
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_KLU2_def.hpp:454
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
int numericFactorization_impl()
KLU2 specific numeric factorization.
Definition: Amesos2_KLU2_def.hpp:188
Amesos2 interface to the KLU2 package.
Definition: Amesos2_KLU2_decl.hpp:72
bool single_proc_optimization() const
can we optimize size_type and ordinal_type for straight pass through, also check that is_contiguous_ ...
Definition: Amesos2_KLU2_def.hpp:114
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_KLU2_def.hpp:490
Helper class for putting 1-D data arrays into multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:322
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:176