Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Umfpack_def.hpp
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Amesos2: Templated Direct Sparse Solver Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Sivasankaran Rajamanickam (srajama@sandia.gov)
39 //
40 // ***********************************************************************
41 //
42 // @HEADER
43 
44 #ifndef AMESOS2_UMFPACK_DEF_HPP
45 #define AMESOS2_UMFPACK_DEF_HPP
46 
47 #include <Teuchos_Tuple.hpp>
48 #include <Teuchos_ParameterList.hpp>
49 #include <Teuchos_StandardParameterEntryValidators.hpp>
50 
52 #include "Amesos2_Umfpack_decl.hpp"
53 #include "Amesos2_Util.hpp"
54 
55 namespace Amesos2 {
56 
57 template <class Matrix, class Vector>
59  Teuchos::RCP<const Matrix> A,
60  Teuchos::RCP<Vector> X,
61  Teuchos::RCP<const Vector> B )
62  : SolverCore<Amesos2::Umfpack,Matrix,Vector>(A, X, B)
63  , is_contiguous_(true)
64 {
65  data_.Symbolic = NULL;
66  data_.Numeric = NULL;
67 }
68 
69 
70 template <class Matrix, class Vector>
72 {
73  if (data_.Symbolic) function_map::umfpack_free_symbolic (&data_.Symbolic);
74  if (data_.Numeric) function_map::umfpack_free_numeric (&data_.Numeric);
75 }
76 
77 template <class Matrix, class Vector>
78 std::string
80 {
81  std::ostringstream oss;
82  oss << "Umfpack solver interface";
83  return oss.str();
84 }
85 
86 template<class Matrix, class Vector>
87 int
89 {
90  return(0);
91 }
92 
93 
94 template <class Matrix, class Vector>
95 int
97 {
98  int status = 0;
99  if ( this->root_ ) {
100  if (data_.Symbolic) {
101  function_map::umfpack_free_symbolic(&(data_.Symbolic));
102  }
103 
104  function_map::umfpack_defaults(data_.Control);
105 
106  status = function_map::umfpack_symbolic(
107  this->globalNumRows_,this->globalNumCols_,
108  &(this->colptr_view_[0]), &(this->rowind_view_[0]),
109  &(this->nzvals_view_[0]), &(data_.Symbolic), data_.Control, data_.Info);
110  }
111 
112  return status;
113 }
114 
115 
116 template <class Matrix, class Vector>
117 int
119 {
120  int status = 0;
121  if ( this->root_ ) {
122  if(!data_.Symbolic) {
123  symbolicFactorization_impl();
124  }
125 
126  function_map::umfpack_defaults(data_.Control);
127 
128  if (data_.Numeric) {
129  function_map::umfpack_free_numeric(&(data_.Numeric));
130  }
131 
132  status = function_map::umfpack_numeric(
133  &(this->colptr_view_[0]),
134  &(this->rowind_view_[0]), &(this->nzvals_view_[0]), data_.Symbolic,
135  &(data_.Numeric), data_.Control, data_.Info);
136  }
137  return status;
138 }
139 
140 template <class Matrix, class Vector>
141 int
143  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
144 {
145  using Teuchos::as;
146 
147  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
148  const size_t nrhs = X->getGlobalNumVectors();
149 
150  const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
151  Teuchos::Array<umfpack_type> xValues(val_store_size);
152  Teuchos::Array<umfpack_type> bValues(val_store_size);
153 
154  { // Get values from RHS B
155 #ifdef HAVE_AMESOS2_TIMERS
156  Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
157  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
158 #endif
159  Util::get_1d_copy_helper<MultiVecAdapter<Vector>, umfpack_type>::do_get(B, bValues(),
160  as<size_t>(ld_rhs),
161  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
162  this->rowIndexBase_);
163  }
164 
165  int UmfpackRequest = this->control_.useTranspose_ ? UMFPACK_At : UMFPACK_A;
166 
167  int ierr = 0; // returned error code
168 
169  if ( this->root_ ) {
170  { // Do solve!
171 #ifdef HAVE_AMESOS2_TIMER
172  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
173 #endif
174  if (data_.Symbolic) {
175  function_map::umfpack_free_symbolic(&(data_.Symbolic));
176  }
177 
178  // validate
179  int i_ld_rhs = as<int>(ld_rhs);
180 
181  for(size_t j = 0 ; j < nrhs; j++) {
182  int status = function_map::umfpack_solve(
183  UmfpackRequest,
184  &(this->colptr_view_[0]), &(this->rowind_view_[0]), &(this->nzvals_view_[0]),
185  &xValues.getRawPtr()[j*i_ld_rhs],
186  &bValues.getRawPtr()[j*i_ld_rhs],
187  data_.Numeric, data_.Control, data_.Info);
188 
189  if(status != 0) {
190  ierr = status;
191  break; // need to verify best ways to handle error in this loop
192  }
193  }
194  }
195  }
196 
197  /* All processes should have the same error code */
198  Teuchos::broadcast(*(this->getComm()), 0, &ierr);
199 
200  TEUCHOS_TEST_FOR_EXCEPTION( ierr != 0, std::runtime_error,
201  "umfpack_solve has error code: " << ierr );
202 
203  /* Update X's global values */
204  {
205 #ifdef HAVE_AMESOS2_TIMERS
206  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
207 #endif
208 
209  Util::put_1d_data_helper<MultiVecAdapter<Vector>,umfpack_type>::do_put(X, xValues(),
210  as<size_t>(ld_rhs),
211  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
212  this->rowIndexBase_);
213  }
214 
215  return(ierr);
216 }
217 
218 
219 template <class Matrix, class Vector>
220 bool
222 {
223  // The Umfpack factorization routines can handle square as well as
224  // rectangular matrices, but Umfpack can only apply the solve routines to
225  // square matrices, so we check the matrix for squareness.
226  return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
227 }
228 
229 
230 template <class Matrix, class Vector>
231 void
232 Umfpack<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
233 {
234  using Teuchos::RCP;
235  using Teuchos::getIntegralValue;
236  using Teuchos::ParameterEntryValidator;
237 
238  RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
239 
240  if( parameterList->isParameter("IsContiguous") ){
241  is_contiguous_ = parameterList->get<bool>("IsContiguous");
242  }
243 }
244 
245 
246 template <class Matrix, class Vector>
247 Teuchos::RCP<const Teuchos::ParameterList>
249 {
250  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
251 
252  if( is_null(valid_params) ){
253  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
254 
255  pl->set("IsContiguous", true, "Whether GIDs contiguous");
256 
257  valid_params = pl;
258  }
259 
260  return valid_params;
261 }
262 
263 
264 template <class Matrix, class Vector>
265 bool
267 {
268  if(current_phase == SOLVE) {
269  return(false);
270  }
271 
272 #ifdef HAVE_AMESOS2_TIMERS
273  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
274 #endif
275 
276  // Only the root image needs storage allocated
277  if( this->root_ ){
278  Kokkos::resize(nzvals_view_,this->globalNumNonZeros_);
279  Kokkos::resize(rowind_view_,this->globalNumNonZeros_);
280  Kokkos::resize(colptr_view_,this->globalNumCols_ + 1);
281  }
282 
283  int nnz_ret = 0;
284  {
285 #ifdef HAVE_AMESOS2_TIMERS
286  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
287 #endif
288 
289  TEUCHOS_TEST_FOR_EXCEPTION( this->rowIndexBase_ != this->columnIndexBase_,
290  std::runtime_error,
291  "Row and column maps have different indexbase ");
292  Util::get_ccs_helper_kokkos_view<MatrixAdapter<Matrix>, host_value_type_array,host_ordinal_type_array,
293  host_size_type_array>::do_get(this->matrixA_.ptr(),
294  nzvals_view_, rowind_view_, colptr_view_, nnz_ret,
295  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
296  ARBITRARY,
297  this->rowIndexBase_);
298  }
299 
300  return true;
301 }
302 
303 
304 template<class Matrix, class Vector>
305 const char* Umfpack<Matrix,Vector>::name = "Umfpack";
306 
307 
308 } // end namespace Amesos2
309 
310 #endif // AMESOS2_UMFPACK_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Umfpack_def.hpp:221
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:648
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Umfpack_def.hpp:248
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using Umfpack.
Definition: Amesos2_Umfpack_def.hpp:96
Umfpack(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_Umfpack_def.hpp:58
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:142
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:267
Utility functions for Amesos2.
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_Umfpack_def.hpp:266
int numericFactorization_impl()
Umfpack specific numeric factorization.
Definition: Amesos2_Umfpack_def.hpp:118
Amesos2 interface to the Umfpack package.
Definition: Amesos2_Umfpack_decl.hpp:63
~Umfpack()
Destructor.
Definition: Amesos2_Umfpack_def.hpp:71
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_Umfpack_def.hpp:88
std::string description() const
Returns a short description of this Solver.
Definition: Amesos2_Umfpack_def.hpp:79
Helper class for putting 1-D data arrays into multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:373
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:176