18 #ifndef AMESOS2_KLU2_DEF_HPP
19 #define AMESOS2_KLU2_DEF_HPP
21 #include <Teuchos_Tuple.hpp>
22 #include <Teuchos_ParameterList.hpp>
23 #include <Teuchos_StandardParameterEntryValidators.hpp>
31 template <
class Matrix,
class Vector>
33 Teuchos::RCP<const Matrix> A,
34 Teuchos::RCP<Vector> X,
35 Teuchos::RCP<const Vector> B )
38 , is_contiguous_(true)
40 ::KLU2::klu_defaults<klu2_dtype, local_ordinal_type> (&(data_.common_)) ;
41 data_.symbolic_ = NULL;
42 data_.numeric_ = NULL;
49 template <
class Matrix,
class Vector>
57 if (data_.symbolic_ != NULL)
58 ::KLU2::klu_free_symbolic<klu2_dtype, local_ordinal_type>
59 (&(data_.symbolic_), &(data_.common_)) ;
60 if (data_.numeric_ != NULL)
61 ::KLU2::klu_free_numeric<klu2_dtype, local_ordinal_type>
62 (&(data_.numeric_), &(data_.common_)) ;
75 template <
class Matrix,
class Vector>
78 return (this->root_ && (this->matrixA_->getComm()->getSize() == 1) && is_contiguous_);
81 template<
class Matrix,
class Vector>
87 #ifdef HAVE_AMESOS2_TIMERS
88 Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_);
95 template <
class Matrix,
class Vector>
99 if (data_.symbolic_ != NULL) {
100 ::KLU2::klu_free_symbolic<klu2_dtype, local_ordinal_type>
101 (&(data_.symbolic_), &(data_.common_)) ;
104 if ( single_proc_optimization() ) {
105 host_ordinal_type_array host_row_ptr_view;
106 host_ordinal_type_array host_cols_view;
107 this->matrixA_->returnRowPtr_kokkos_view(host_row_ptr_view);
108 this->matrixA_->returnColInd_kokkos_view(host_cols_view);
109 data_.symbolic_ = ::KLU2::klu_analyze<klu2_dtype, local_ordinal_type>
110 ((local_ordinal_type)this->globalNumCols_, host_row_ptr_view.data(),
111 host_cols_view.data(), &(data_.common_)) ;
115 data_.symbolic_ = ::KLU2::klu_analyze<klu2_dtype, local_ordinal_type>
116 ((local_ordinal_type)this->globalNumCols_, host_col_ptr_view_.data(),
117 host_rows_view_.data(), &(data_.common_)) ;
125 template <
class Matrix,
class Vector>
139 #ifdef HAVE_AMESOS2_TIMERS
140 Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
143 if (data_.numeric_ != NULL) {
144 ::KLU2::klu_free_numeric<klu2_dtype, local_ordinal_type>
145 (&(data_.numeric_), &(data_.common_));
148 if ( single_proc_optimization() ) {
149 host_ordinal_type_array host_row_ptr_view;
150 host_ordinal_type_array host_cols_view;
151 this->matrixA_->returnRowPtr_kokkos_view(host_row_ptr_view);
152 this->matrixA_->returnColInd_kokkos_view(host_cols_view);
153 this->matrixA_->returnValues_kokkos_view(host_nzvals_view_);
154 klu2_dtype * pValues = function_map::convert_scalar(host_nzvals_view_.data());
155 data_.numeric_ = ::KLU2::klu_factor<klu2_dtype, local_ordinal_type>
156 (host_row_ptr_view.data(), host_cols_view.data(), pValues,
157 data_.symbolic_, &(data_.common_));
160 klu2_dtype * pValues = function_map::convert_scalar(host_nzvals_view_.data());
161 data_.numeric_ = ::KLU2::klu_factor<klu2_dtype, local_ordinal_type>
162 (host_col_ptr_view_.data(), host_rows_view_.data(), pValues,
163 data_.symbolic_, &(data_.common_));
171 if(data_.numeric_ ==
nullptr) {
178 this->setNnzLU( as<size_t>((data_.numeric_)->lnz) + as<size_t>((data_.numeric_)->unz) );
185 Teuchos::broadcast(*(this->matrixA_->getComm()), 0, &info);
187 TEUCHOS_TEST_FOR_EXCEPTION(info > 0, std::runtime_error,
188 "KLU2 numeric factorization failed");
193 template <
class Matrix,
class Vector>
202 const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
203 const size_t nrhs = X->getGlobalNumVectors();
208 #ifdef HAVE_AMESOS2_TIMERS
209 Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
210 Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
212 const bool initialize_data =
true;
213 const bool do_not_initialize_data =
false;
214 if ( single_proc_optimization() && nrhs == 1 ) {
216 bDidAssignB = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
217 host_solve_array_t>::do_get(initialize_data, B, bValues_, as<size_t>(ld_rhs));
219 bDidAssignX = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
220 host_solve_array_t>::do_get(do_not_initialize_data, X, xValues_, as<size_t>(ld_rhs));
224 bDidAssignB = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
225 host_solve_array_t>::do_get(initialize_data, B, bValues_,
227 (is_contiguous_ ==
true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
228 this->rowIndexBase_);
231 bDidAssignX = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
232 host_solve_array_t>::do_get(do_not_initialize_data, X, xValues_,
234 (is_contiguous_ ==
true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
235 this->rowIndexBase_);
244 Kokkos::deep_copy(xValues_, bValues_);
252 klu2_dtype * pxValues = function_map::convert_scalar(xValues_.data());
253 klu2_dtype * pbValues = function_map::convert_scalar(bValues_.data());
257 TEUCHOS_TEST_FOR_EXCEPTION(pbValues ==
nullptr,
258 std::runtime_error,
"Amesos2 Runtime Error: b_vector returned null ");
260 TEUCHOS_TEST_FOR_EXCEPTION(pxValues ==
nullptr,
261 std::runtime_error,
"Amesos2 Runtime Error: x_vector returned null ");
264 if ( single_proc_optimization() && nrhs == 1 ) {
265 #ifdef HAVE_AMESOS2_TIMERS
266 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
274 ::KLU2::klu_tsolve2<klu2_dtype, local_ordinal_type>
275 (data_.symbolic_, data_.numeric_,
276 (local_ordinal_type)this->globalNumCols_,
277 (local_ordinal_type)nrhs,
278 pbValues, pxValues, &(data_.common_)) ;
281 ::KLU2::klu_solve2<klu2_dtype, local_ordinal_type>
282 (data_.symbolic_, data_.numeric_,
283 (local_ordinal_type)this->globalNumCols_,
284 (local_ordinal_type)nrhs,
285 pbValues, pxValues, &(data_.common_)) ;
296 #ifdef HAVE_AMESOS2_TIMERS
297 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
304 if ( single_proc_optimization() ) {
305 ::KLU2::klu_tsolve<klu2_dtype, local_ordinal_type>
306 (data_.symbolic_, data_.numeric_,
307 (local_ordinal_type)this->globalNumCols_,
308 (local_ordinal_type)nrhs,
309 pxValues, &(data_.common_)) ;
312 ::KLU2::klu_solve<klu2_dtype, local_ordinal_type>
313 (data_.symbolic_, data_.numeric_,
314 (local_ordinal_type)this->globalNumCols_,
315 (local_ordinal_type)nrhs,
316 pxValues, &(data_.common_)) ;
324 if ( single_proc_optimization() ) {
325 ::KLU2::klu_solve<klu2_dtype, local_ordinal_type>
326 (data_.symbolic_, data_.numeric_,
327 (local_ordinal_type)this->globalNumCols_,
328 (local_ordinal_type)nrhs,
329 pxValues, &(data_.common_)) ;
332 ::KLU2::klu_tsolve<klu2_dtype, local_ordinal_type>
333 (data_.symbolic_, data_.numeric_,
334 (local_ordinal_type)this->globalNumCols_,
335 (local_ordinal_type)nrhs,
336 pxValues, &(data_.common_)) ;
345 #ifdef HAVE_AMESOS2_TIMERS
346 Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
349 Util::put_1d_data_helper_kokkos_view<
352 (is_contiguous_ ==
true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
353 this->rowIndexBase_);
361 template <
class Matrix,
class Vector>
368 return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
372 template <
class Matrix,
class Vector>
377 using Teuchos::getIntegralValue;
378 using Teuchos::ParameterEntryValidator;
380 RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
382 transFlag_ = this->control_.useTranspose_ ? 1: 0;
384 if( parameterList->isParameter(
"Trans") ){
385 RCP<const ParameterEntryValidator> trans_validator = valid_params->getEntry(
"Trans").validator();
386 parameterList->getEntry(
"Trans").setValidator(trans_validator);
388 transFlag_ = getIntegralValue<int>(*parameterList,
"Trans");
391 if( parameterList->isParameter(
"IsContiguous") ){
392 is_contiguous_ = parameterList->get<
bool>(
"IsContiguous");
397 template <
class Matrix,
class Vector>
398 Teuchos::RCP<const Teuchos::ParameterList>
402 using Teuchos::tuple;
403 using Teuchos::ParameterList;
404 using Teuchos::setStringToIntegralParameter;
406 static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
408 if( is_null(valid_params) )
410 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
412 pl->set(
"Equil",
true,
"Whether to equilibrate the system before solve, does nothing now");
413 pl->set(
"IsContiguous",
true,
"Whether GIDs contiguous");
415 setStringToIntegralParameter<int>(
"Trans",
"NOTRANS",
416 "Solve for the transpose system or not",
417 tuple<string>(
"NOTRANS",
"TRANS",
"CONJ"),
418 tuple<string>(
"Solve with transpose",
419 "Do not solve with transpose",
420 "Solve with the conjugate transpose"),
430 template <
class Matrix,
class Vector>
436 if(current_phase == SOLVE)
return(
false);
438 if ( single_proc_optimization() ) {
444 #ifdef HAVE_AMESOS2_TIMERS
445 Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
450 host_nzvals_view_ = host_value_type_array(
451 Kokkos::ViewAllocateWithoutInitializing(
"host_nzvals_view_"), this->globalNumNonZeros_);
452 host_rows_view_ = host_ordinal_type_array(
453 Kokkos::ViewAllocateWithoutInitializing(
"host_rows_view_"), this->globalNumNonZeros_);
454 host_col_ptr_view_ = host_ordinal_type_array(
455 Kokkos::ViewAllocateWithoutInitializing(
"host_col_ptr_view_"), this->globalNumRows_ + 1);
458 local_ordinal_type nnz_ret = 0;
460 #ifdef HAVE_AMESOS2_TIMERS
461 Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
466 ::do_get(this->matrixA_.ptr(), host_nzvals_view_, host_rows_view_, host_col_ptr_view_, nnz_ret,
467 (is_contiguous_ ==
true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
469 this->rowIndexBase_);
473 TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != as<local_ordinal_type>(this->globalNumNonZeros_),
475 "Did not get the expected number of non-zero vals");
484 template<
class Matrix,
class Vector>
490 #endif // AMESOS2_KLU2_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:71
KLU2(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_KLU2_def.hpp:32
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:614
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:195
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:31
Amesos2 KLU2 declarations.
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_KLU2_def.hpp:432
~KLU2()
Destructor.
Definition: Amesos2_KLU2_def.hpp:50
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > ¶meterList)
Definition: Amesos2_KLU2_def.hpp:374
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using KLU2.
Definition: Amesos2_KLU2_def.hpp:97
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_KLU2_def.hpp:83
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_KLU2_def.hpp:363
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:42
int numericFactorization_impl()
KLU2 specific numeric factorization.
Definition: Amesos2_KLU2_def.hpp:127
Amesos2 interface to the KLU2 package.
Definition: Amesos2_KLU2_decl.hpp:38
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:77
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_KLU2_def.hpp:399
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:142