44 #ifndef AMESOS2_CUSOLVER_DEF_HPP
45 #define AMESOS2_CUSOLVER_DEF_HPP
47 #include <Teuchos_Tuple.hpp>
48 #include <Teuchos_ParameterList.hpp>
49 #include <Teuchos_StandardParameterEntryValidators.hpp>
52 #include "Amesos2_cuSOLVER_decl.hpp"
56 template <
class Matrix,
class Vector>
58 Teuchos::RCP<const Matrix> A,
59 Teuchos::RCP<Vector> X,
60 Teuchos::RCP<const Vector> B )
63 auto status = cusolverSpCreate(&data_.handle);
64 TEUCHOS_TEST_FOR_EXCEPTION( status != CUSOLVER_STATUS_SUCCESS,
65 std::runtime_error,
"cusolverSpCreate failed");
67 status = cusolverSpCreateCsrcholInfo(&data_.chol_info);
68 TEUCHOS_TEST_FOR_EXCEPTION( status != CUSOLVER_STATUS_SUCCESS,
69 std::runtime_error,
"cusolverSpCreateCsrcholInfo failed");
71 auto sparse_status = cusparseCreateMatDescr(&data_.desc);
72 TEUCHOS_TEST_FOR_EXCEPTION( sparse_status != CUSPARSE_STATUS_SUCCESS,
73 std::runtime_error,
"cusparseCreateMatDescr failed");
76 template <
class Matrix,
class Vector>
79 cusparseDestroyMatDescr(data_.desc);
80 cusolverSpDestroyCsrcholInfo(data_.chol_info);
81 cusolverSpDestroy(data_.handle);
84 template<
class Matrix,
class Vector>
88 #ifdef HAVE_AMESOS2_TIMERS
89 Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_);
91 if(do_optimization()) {
92 this->matrixA_->returnRowPtr_kokkos_view(device_row_ptr_view_);
93 this->matrixA_->returnColInd_kokkos_view(device_cols_view_);
97 Amesos2::Util::reorder(
98 device_row_ptr_view_, device_cols_view_,
99 device_perm_, device_peri_, sorted_nnz,
107 template <
class Matrix,
class Vector>
111 #ifdef HAVE_AMESOS2_TIMERS
112 Teuchos::TimeMonitor symFactTimer(this->timers_.symFactTime_);
117 const int size = this->globalNumRows_;
118 const int nnz = device_cols_view_.size();
119 const int * colIdx = device_cols_view_.data();
120 const int * rowPtr = device_row_ptr_view_.data();
121 auto status = cusolverSpXcsrcholAnalysis(
122 data_.handle, size, nnz, data_.desc, rowPtr, colIdx, data_.chol_info);
123 err = (status != CUSOLVER_STATUS_SUCCESS) ? 1 : 0;
126 Teuchos::broadcast(*(this->getComm()), 0, &err);
127 TEUCHOS_TEST_FOR_EXCEPTION(err != 0,
128 std::runtime_error,
"Amesos2 cuSolver symbolic failed.");
133 template <
class Matrix,
class Vector>
138 if(do_optimization()) {
139 this->matrixA_->returnValues_kokkos_view(device_nzvals_view_);
145 device_size_type_array orig_device_row_ptr_view;
146 device_ordinal_type_array orig_device_cols_view;
147 this->matrixA_->returnRowPtr_kokkos_view(orig_device_row_ptr_view);
148 this->matrixA_->returnColInd_kokkos_view(orig_device_cols_view);
149 Amesos2::Util::reorder_values(
150 device_nzvals_view_, orig_device_row_ptr_view, device_row_ptr_view_, orig_device_cols_view,
151 device_perm_, device_peri_, sorted_nnz);
154 const int size = this->globalNumRows_;
155 const int nnz = device_cols_view_.size();
156 const cusolver_type * values = device_nzvals_view_.data();
157 const int * colIdx = device_cols_view_.data();
158 const int * rowPtr = device_row_ptr_view_.data();
160 size_t internalDataInBytes, workspaceInBytes;
161 auto status = function_map::bufferInfo(data_.handle, size, nnz, data_.desc,
162 values, rowPtr, colIdx, data_.chol_info,
163 &internalDataInBytes, &workspaceInBytes);
165 if(status == CUSOLVER_STATUS_SUCCESS) {
166 const size_t buffer_size = workspaceInBytes /
sizeof(cusolver_type);
167 if(buffer_size > buffer_.extent(0)) {
168 buffer_ = device_value_type_array(
169 Kokkos::ViewAllocateWithoutInitializing(
"cusolver buf"), buffer_size);
171 status = function_map::numeric(data_.handle, size, nnz, data_.desc,
172 values, rowPtr, colIdx, data_.chol_info, buffer_.data());
174 err = (status != CUSOLVER_STATUS_SUCCESS) ? 1 : 0;
177 Teuchos::broadcast(*(this->getComm()), 0, &err);
178 TEUCHOS_TEST_FOR_EXCEPTION(err != 0,
179 std::runtime_error,
"Amesos2 cuSolver numeric failed.");
184 template <
class Matrix,
class Vector>
190 const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
191 const ordinal_type nrhs = X->getGlobalNumVectors();
195 #ifdef HAVE_AMESOS2_TIMERS
196 Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
197 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
200 const bool initialize_data =
true;
201 const bool do_not_initialize_data =
false;
202 Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
203 device_solve_array_t>::do_get(initialize_data, B, this->bValues_, Teuchos::as<size_t>(ld_rhs),
204 ROOTED, this->rowIndexBase_);
208 bAssignedX = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
209 device_solve_array_t>::do_get(do_not_initialize_data, X, this->xValues_, Teuchos::as<size_t>(ld_rhs),
210 ROOTED, this->rowIndexBase_);
216 #ifdef HAVE_AMESOS2_TIMER
217 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
220 const int size = this->globalNumRows_;
223 Amesos2::Util::apply_reorder_permutation(
224 this->bValues_, this->permute_result_, this->device_perm_);
227 this->permute_result_ = this->bValues_;
230 for(ordinal_type n = 0; n < nrhs; ++n) {
231 const cusolver_type * b = this->permute_result_.data() + n *
size;
232 cusolver_type * x = this->xValues_.data() + n *
size;
233 auto status = function_map::solve(
234 data_.handle, size, b, x, data_.chol_info, buffer_.data());
235 err = (status != CUSOLVER_STATUS_SUCCESS) ? 1 : 0;
241 if(data_.bReorder && err == 0) {
242 Amesos2::Util::apply_reorder_permutation(
243 this->xValues_, this->permute_result_, this->device_peri_);
244 Kokkos::deep_copy(this->xValues_, this->permute_result_);
253 #ifdef HAVE_AMESOS2_TIMERS
254 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
257 Util::template put_1d_data_helper_kokkos_view<
259 Teuchos::as<size_t>(ld_rhs), ROOTED, this->rowIndexBase_);
262 Teuchos::broadcast(*(this->getComm()), 0, &err);
263 TEUCHOS_TEST_FOR_EXCEPTION(err != 0,
264 std::runtime_error,
"Amesos2 cuSolver solve failed.");
269 template <
class Matrix,
class Vector>
273 return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
276 template <
class Matrix,
class Vector>
281 using Teuchos::ParameterEntryValidator;
283 RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
285 if( parameterList->isParameter(
"Reorder") ){
286 RCP<const ParameterEntryValidator> reorder_validator = valid_params->getEntry(
"Reorder").validator();
287 parameterList->getEntry(
"Reorder").setValidator(reorder_validator);
290 data_.bReorder = parameterList->get<
bool>(
"Reorder",
true);
293 template <
class Matrix,
class Vector>
294 Teuchos::RCP<const Teuchos::ParameterList>
297 static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
299 if( is_null(valid_params) ){
300 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
302 pl->set(
"Reorder",
true,
"Whether GIDs contiguous");
310 template <
class Matrix,
class Vector>
313 return (this->root_ && (this->matrixA_->getComm()->getSize() == 1));
316 template <
class Matrix,
class Vector>
320 if(current_phase == SOLVE) {
324 if(!do_optimization()) {
325 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
326 "cuSolver is only implemented for serial.");
332 template<
class Matrix,
class Vector>
337 #endif // AMESOS2_CUSOLVER_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
const int size
Definition: klu2_simple.cpp:74
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > ¶meterList)
Definition: Amesos2_cuSOLVER_def.hpp:278
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_cuSOLVER_def.hpp:271
cuSOLVER(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_cuSOLVER_def.hpp:57
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_cuSOLVER_def.hpp:295
Amesos2 interface to cuSOLVER.
Definition: Amesos2_cuSOLVER_decl.hpp:59
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using cuSOLVER.
Definition: Amesos2_cuSOLVER_def.hpp:109
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_cuSOLVER_def.hpp:318
bool do_optimization() const
can we optimize size_type and ordinal_type for straight pass through
Definition: Amesos2_cuSOLVER_def.hpp:312
int numericFactorization_impl()
cuSOLVER specific numeric factorization
Definition: Amesos2_cuSOLVER_def.hpp:135
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_cuSOLVER_def.hpp:86
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
cuSOLVER specific solve.
Definition: Amesos2_cuSOLVER_def.hpp:186
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:176
~cuSOLVER()
Destructor.
Definition: Amesos2_cuSOLVER_def.hpp:77