Amesos2 - Direct Sparse Solver Interfaces Version of the Day
Amesos2_Umfpack_def.hpp
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#ifndef AMESOS2_UMFPACK_DEF_HPP
11#define AMESOS2_UMFPACK_DEF_HPP
12
13#include <Teuchos_Tuple.hpp>
14#include <Teuchos_ParameterList.hpp>
15#include <Teuchos_StandardParameterEntryValidators.hpp>
16
18#include "Amesos2_Umfpack_decl.hpp"
19#include "Amesos2_Util.hpp"
20
21namespace Amesos2 {
22
23template <class Matrix, class Vector>
25 Teuchos::RCP<const Matrix> A,
26 Teuchos::RCP<Vector> X,
27 Teuchos::RCP<const Vector> B )
28 : SolverCore<Amesos2::Umfpack,Matrix,Vector>(A, X, B)
29 , is_contiguous_(true)
30{
31 data_.Symbolic = NULL;
32 data_.Numeric = NULL;
33}
34
35
36template <class Matrix, class Vector>
38{
39 if (data_.Symbolic) function_map::umfpack_free_symbolic (&data_.Symbolic);
40 if (data_.Numeric) function_map::umfpack_free_numeric (&data_.Numeric);
41}
42
43template <class Matrix, class Vector>
44std::string
46{
47 std::ostringstream oss;
48 oss << "Umfpack solver interface";
49 return oss.str();
50}
51
52template<class Matrix, class Vector>
53int
58
59
60template <class Matrix, class Vector>
61int
63{
64 int status = 0;
65 if ( this->root_ ) {
66 if (data_.Symbolic) {
67 function_map::umfpack_free_symbolic(&(data_.Symbolic));
68 }
69
70 function_map::umfpack_defaults(data_.Control);
71
72 status = function_map::umfpack_symbolic(
73 this->globalNumRows_,this->globalNumCols_,
74 &(this->colptr_view_[0]), &(this->rowind_view_[0]),
75 &(this->nzvals_view_[0]), &(data_.Symbolic), data_.Control, data_.Info);
76 }
77
78 return status;
79}
80
81
82template <class Matrix, class Vector>
83int
85{
86 int status = 0;
87 if ( this->root_ ) {
88 if(!data_.Symbolic) {
89 symbolicFactorization_impl();
90 }
91
92 function_map::umfpack_defaults(data_.Control);
93
94 if (data_.Numeric) {
95 function_map::umfpack_free_numeric(&(data_.Numeric));
96 }
97
98 status = function_map::umfpack_numeric(
99 &(this->colptr_view_[0]),
100 &(this->rowind_view_[0]), &(this->nzvals_view_[0]), data_.Symbolic,
101 &(data_.Numeric), data_.Control, data_.Info);
102 }
103 return status;
104}
105
106template <class Matrix, class Vector>
107int
109 const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
110{
111 using Teuchos::as;
112
113 const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
114 const size_t nrhs = X->getGlobalNumVectors();
115
116 const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
117 Teuchos::Array<umfpack_type> xValues(val_store_size);
118 Teuchos::Array<umfpack_type> bValues(val_store_size);
119
120 { // Get values from RHS B
121#ifdef HAVE_AMESOS2_TIMERS
122 Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
123 Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
124#endif
125 Util::get_1d_copy_helper<MultiVecAdapter<Vector>, umfpack_type>::do_get(B, bValues(),
126 as<size_t>(ld_rhs),
127 (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
128 this->rowIndexBase_);
129 }
130
131 int UmfpackRequest = this->control_.useTranspose_ ? UMFPACK_At : UMFPACK_A;
132
133 int ierr = 0; // returned error code
134
135 if ( this->root_ ) {
136 { // Do solve!
137#ifdef HAVE_AMESOS2_TIMER
138 Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
139#endif
140 if (data_.Symbolic) {
141 function_map::umfpack_free_symbolic(&(data_.Symbolic));
142 }
143
144 // validate
145 int i_ld_rhs = as<int>(ld_rhs);
146
147 for(size_t j = 0 ; j < nrhs; j++) {
148 int status = function_map::umfpack_solve(
149 UmfpackRequest,
150 &(this->colptr_view_[0]), &(this->rowind_view_[0]), &(this->nzvals_view_[0]),
151 &xValues.getRawPtr()[j*i_ld_rhs],
152 &bValues.getRawPtr()[j*i_ld_rhs],
153 data_.Numeric, data_.Control, data_.Info);
154
155 if(status != 0) {
156 ierr = status;
157 break; // need to verify best ways to handle error in this loop
158 }
159 }
160 }
161 }
162
163 /* All processes should have the same error code */
164 Teuchos::broadcast(*(this->getComm()), 0, &ierr);
165
166 TEUCHOS_TEST_FOR_EXCEPTION( ierr != 0, std::runtime_error,
167 "umfpack_solve has error code: " << ierr );
168
169 /* Update X's global values */
170 {
171#ifdef HAVE_AMESOS2_TIMERS
172 Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
173#endif
174
175 Util::put_1d_data_helper<MultiVecAdapter<Vector>,umfpack_type>::do_put(X, xValues(),
176 as<size_t>(ld_rhs),
177 (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
178 this->rowIndexBase_);
179 }
180
181 return(ierr);
182}
183
184
185template <class Matrix, class Vector>
186bool
188{
189 // The Umfpack factorization routines can handle square as well as
190 // rectangular matrices, but Umfpack can only apply the solve routines to
191 // square matrices, so we check the matrix for squareness.
192 return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
193}
194
195
196template <class Matrix, class Vector>
197void
198Umfpack<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
199{
200 using Teuchos::RCP;
201 using Teuchos::getIntegralValue;
202 using Teuchos::ParameterEntryValidator;
203
204 RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
205
206 if( parameterList->isParameter("IsContiguous") ){
207 is_contiguous_ = parameterList->get<bool>("IsContiguous");
208 }
209}
210
211
212template <class Matrix, class Vector>
213Teuchos::RCP<const Teuchos::ParameterList>
215{
216 static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
217
218 if( is_null(valid_params) ){
219 Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
220
221 pl->set("IsContiguous", true, "Whether GIDs contiguous");
222
223 valid_params = pl;
224 }
225
226 return valid_params;
227}
228
229
230template <class Matrix, class Vector>
231bool
233{
234 if(current_phase == SOLVE) {
235 return(false);
236 }
237
238#ifdef HAVE_AMESOS2_TIMERS
239 Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
240#endif
241
242 // Only the root image needs storage allocated
243 if( this->root_ ){
244 Kokkos::resize(nzvals_view_,this->globalNumNonZeros_);
245 Kokkos::resize(rowind_view_,this->globalNumNonZeros_);
246 Kokkos::resize(colptr_view_,this->globalNumCols_ + 1);
247 }
248
249 int nnz_ret = 0;
250 {
251#ifdef HAVE_AMESOS2_TIMERS
252 Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
253#endif
254
255 TEUCHOS_TEST_FOR_EXCEPTION( this->rowIndexBase_ != this->columnIndexBase_,
256 std::runtime_error,
257 "Row and column maps have different indexbase ");
258 Util::get_ccs_helper_kokkos_view<MatrixAdapter<Matrix>, host_value_type_array,host_ordinal_type_array,
259 host_size_type_array>::do_get(this->matrixA_.ptr(),
260 nzvals_view_, rowind_view_, colptr_view_, nnz_ret,
261 (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
262 ARBITRARY,
263 this->rowIndexBase_);
264 }
265
266 return true;
267}
268
269
270template<class Matrix, class Vector>
271const char* Umfpack<Matrix,Vector>::name = "Umfpack";
272
273
274} // end namespace Amesos2
275
276#endif // AMESOS2_UMFPACK_DEF_HPP
@ ROOTED
Definition Amesos2_TypeDecl.hpp:93
@ CONTIGUOUS_AND_ROOTED
Definition Amesos2_TypeDecl.hpp:94
@ ARBITRARY
Definition Amesos2_TypeDecl.hpp:109
Utility functions for Amesos2.
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers.
Definition Amesos2_SolverCore_decl.hpp:72
Amesos2 interface to the Umfpack package.
Definition Amesos2_Umfpack_decl.hpp:30
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition Amesos2_Umfpack_def.hpp:232
int numericFactorization_impl()
Umfpack specific numeric factorization.
Definition Amesos2_Umfpack_def.hpp:84
~Umfpack()
Destructor.
Definition Amesos2_Umfpack_def.hpp:37
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
Umfpack specific solve.
Definition Amesos2_Umfpack_def.hpp:108
std::string description() const
Returns a short description of this Solver.
Definition Amesos2_Umfpack_def.hpp:45
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition Amesos2_Umfpack_def.hpp:54
Umfpack(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition Amesos2_Umfpack_def.hpp:24
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using Umfpack.
Definition Amesos2_Umfpack_def.hpp:62
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition Amesos2_Umfpack_def.hpp:187
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition Amesos2_Umfpack_def.hpp:214
EPhase
Used to indicate a phase in the direct solution.
Definition Amesos2_TypeDecl.hpp:31
A templated MultiVector class adapter for Amesos2.
Definition Amesos2_MultiVecAdapter_decl.hpp:142
Helper class for getting 1-D copies of multivectors.
Definition Amesos2_MultiVecAdapter_decl.hpp:233
A generic helper class for getting a CCS representation of a Matrix.
Definition Amesos2_Util.hpp:633
Helper class for putting 1-D data arrays into multivectors.
Definition Amesos2_MultiVecAdapter_decl.hpp:339