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  , nzvals_() // initialize to empty arrays
64  , rowind_()
65  , colptr_()
66 {
67  data_.Symbolic = 0;
68  data_.Numeric = 0;
69 }
70 
71 
72 template <class Matrix, class Vector>
74 {
75  if (data_.Symbolic) function_map::umfpack_free_symbolic (&data_.Symbolic);
76  if (data_.Numeric) function_map::umfpack_free_numeric (&data_.Numeric);
77 }
78 
79 template <class Matrix, class Vector>
80 std::string
82 {
83  std::ostringstream oss;
84  oss << "Umfpack solver interface";
85  return oss.str();
86 }
87 
88 template<class Matrix, class Vector>
89 int
91 {
92  return(0);
93 }
94 
95 
96 template <class Matrix, class Vector>
97 int
99 {
100  int status = 0;
101  if ( this->root_ ) {
102  if (data_.Symbolic) {
103  function_map::umfpack_free_symbolic(&(data_.Symbolic));
104  }
105 
106  function_map::umfpack_defaults(data_.Control);
107 
108  status = function_map::umfpack_symbolic(
109  this->globalNumRows_,this->globalNumCols_,
110  &(this->colptr_[0]), &(this->rowind_[0]),
111  &(this->nzvals_[0]), &(data_.Symbolic), data_.Control, data_.Info);
112  }
113 
114  return status;
115 }
116 
117 
118 template <class Matrix, class Vector>
119 int
121 {
122  int status = 0;
123  if ( this->root_ ) {
124  if(!data_.Symbolic) {
125  symbolicFactorization_impl();
126  }
127 
128  function_map::umfpack_defaults(data_.Control);
129 
130  if (data_.Numeric) {
131  function_map::umfpack_free_numeric(&(data_.Numeric));
132  }
133 
134  status = function_map::umfpack_numeric(
135  &(this->colptr_[0]),
136  &(this->rowind_[0]), &(this->nzvals_[0]), data_.Symbolic,
137  &(data_.Numeric), data_.Control, data_.Info);
138  }
139  return status;
140 }
141 
142 template <class Matrix, class Vector>
143 int
145  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
146 {
147  using Teuchos::as;
148 
149  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
150  const size_t nrhs = X->getGlobalNumVectors();
151 
152  const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
153  Teuchos::Array<umfpack_type> xValues(val_store_size);
154  Teuchos::Array<umfpack_type> bValues(val_store_size);
155 
156  { // Get values from RHS B
157 #ifdef HAVE_AMESOS2_TIMERS
158  Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
159  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
160 #endif
162  umfpack_type>::do_get(B, bValues(),
163  as<size_t>(ld_rhs),
164  ROOTED, this->rowIndexBase_);
165  }
166 
167  int UmfpackRequest = this->control_.useTranspose_ ? UMFPACK_At : UMFPACK_A;
168 
169  int ierr = 0; // returned error code
170 
171  if ( this->root_ ) {
172  { // Do solve!
173 #ifdef HAVE_AMESOS2_TIMER
174  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
175 #endif
176  if (data_.Symbolic) {
177  function_map::umfpack_free_symbolic(&(data_.Symbolic));
178  }
179 
180  // validate
181  int i_ld_rhs = as<int>(ld_rhs);
182 
183  for(size_t j = 0 ; j < nrhs; j++) {
184  int status = function_map::umfpack_solve(
185  UmfpackRequest,
186  &(this->colptr_[0]), &(this->rowind_[0]), &(this->nzvals_[0]),
187  &xValues.getRawPtr()[j*i_ld_rhs],
188  &bValues.getRawPtr()[j*i_ld_rhs],
189  data_.Numeric, data_.Control, data_.Info);
190 
191  if(status != 0) {
192  ierr = status;
193  break; // need to verify best ways to handle error in this loop
194  }
195  }
196  }
197  }
198 
199  /* All processes should have the same error code */
200  Teuchos::broadcast(*(this->getComm()), 0, &ierr);
201 
202  TEUCHOS_TEST_FOR_EXCEPTION( ierr != 0, std::runtime_error,
203  "umfpack_solve has error code: " << ierr );
204 
205  /* Update X's global values */
206  {
207 #ifdef HAVE_AMESOS2_TIMERS
208  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
209 #endif
210 
212  MultiVecAdapter<Vector>,umfpack_type>::do_put(X, xValues(),
213  as<size_t>(ld_rhs),
214  ROOTED, this->rowIndexBase_);
215  }
216 
217  return(ierr);
218 }
219 
220 
221 template <class Matrix, class Vector>
222 bool
224 {
225  // The Umfpack factorization routines can handle square as well as
226  // rectangular matrices, but Umfpack can only apply the solve routines to
227  // square matrices, so we check the matrix for squareness.
228  return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
229 }
230 
231 
232 template <class Matrix, class Vector>
233 void
234 Umfpack<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
235 {
236  using Teuchos::RCP;
237  using Teuchos::getIntegralValue;
238  using Teuchos::ParameterEntryValidator;
239 
240  RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
241 }
242 
243 
244 template <class Matrix, class Vector>
245 Teuchos::RCP<const Teuchos::ParameterList>
247 {
248  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
249 
250  if( is_null(valid_params) ){
251  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
252 
253  valid_params = pl;
254  }
255 
256  return valid_params;
257 }
258 
259 
260 template <class Matrix, class Vector>
261 bool
263 {
264  if(current_phase == SOLVE) {
265  return(false);
266  }
267 
268 #ifdef HAVE_AMESOS2_TIMERS
269  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
270 #endif
271 
272  // Only the root image needs storage allocated
273  if( this->root_ ){
274  nzvals_.resize(this->globalNumNonZeros_);
275  rowind_.resize(this->globalNumNonZeros_);
276  colptr_.resize(this->globalNumCols_ + 1);
277  }
278 
279  int nnz_ret = 0;
280  {
281 #ifdef HAVE_AMESOS2_TIMERS
282  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
283 #endif
284 
285  TEUCHOS_TEST_FOR_EXCEPTION( this->rowIndexBase_ != this->columnIndexBase_,
286  std::runtime_error,
287  "Row and column maps have different indexbase ");
289  MatrixAdapter<Matrix>,umfpack_type,int,int>::do_get(this->matrixA_.ptr(),
290  nzvals_(), rowind_(),
291  colptr_(), nnz_ret, ROOTED,
292  ARBITRARY,
293  this->rowIndexBase_);
294  }
295 
296  return true;
297 }
298 
299 
300 template<class Matrix, class Vector>
301 const char* Umfpack<Matrix,Vector>::name = "Umfpack";
302 
303 
304 } // end namespace Amesos2
305 
306 #endif // AMESOS2_UMFPACK_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
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:98
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
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:266
Definition: Amesos2_TypeDecl.hpp:143
Utility functions for Amesos2.
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_Umfpack_def.hpp:262
int numericFactorization_impl()
Umfpack specific numeric factorization.
Definition: Amesos2_Umfpack_def.hpp:120
Amesos2 interface to the Umfpack package.
Definition: Amesos2_Umfpack_decl.hpp:63
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Umfpack_def.hpp:246
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:657
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:144
~Umfpack()
Destructor.
Definition: Amesos2_Umfpack_def.hpp:73
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Umfpack_def.hpp:223
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_Umfpack_def.hpp:90
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
std::string description() const
Returns a short description of this Solver.
Definition: Amesos2_Umfpack_def.hpp:81
Definition: Amesos2_TypeDecl.hpp:127
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