Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_KLU2_def.hpp
Go to the documentation of this file.
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 Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //
42 // @HEADER
43 
52 #ifndef AMESOS2_KLU2_DEF_HPP
53 #define AMESOS2_KLU2_DEF_HPP
54 
55 #include <Teuchos_Tuple.hpp>
56 #include <Teuchos_ParameterList.hpp>
57 #include <Teuchos_StandardParameterEntryValidators.hpp>
58 
60 #include "Amesos2_KLU2_decl.hpp"
61 
62 namespace Amesos2 {
63 
64 
65 template <class Matrix, class Vector>
67  Teuchos::RCP<const Matrix> A,
68  Teuchos::RCP<Vector> X,
69  Teuchos::RCP<const Vector> B )
70  : SolverCore<Amesos2::KLU2,Matrix,Vector>(A, X, B)
71  , nzvals_() // initialize to empty arrays
72  , rowind_()
73  , colptr_()
74  , transFlag_(0)
75 {
76  ::KLU2::klu_defaults<slu_type, local_ordinal_type> (&(data_.common_)) ;
77  data_.symbolic_ = NULL;
78  data_.numeric_ = NULL;
79 
80  // Override some default options
81  // TODO: use data_ here to init
82 }
83 
84 
85 template <class Matrix, class Vector>
87 {
88  /* Free KLU2 data_types
89  * - Matrices
90  * - Vectors
91  * - Other data
92  */
93  if (data_.symbolic_ != NULL)
94  ::KLU2::klu_free_symbolic<slu_type, local_ordinal_type>
95  (&(data_.symbolic_), &(data_.common_)) ;
96  if (data_.numeric_ != NULL)
97  ::KLU2::klu_free_numeric<slu_type, local_ordinal_type>
98  (&(data_.numeric_), &(data_.common_)) ;
99 
100  // Storage is initialized in numericFactorization_impl()
101  //if ( data_.A.Store != NULL ){
102  // destoy
103  //}
104 
105  // only root allocated these SuperMatrices.
106  //if ( data_.L.Store != NULL ){ // will only be true for this->root_
107  // destroy ..
108  //}
109 }
110 
111 template<class Matrix, class Vector>
112 int
114 {
115  /* TODO: Define what it means for KLU2
116  */
117 #ifdef HAVE_AMESOS2_TIMERS
118  Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_);
119 #endif
120 
121  return(0);
122 }
123 
124 
125 template <class Matrix, class Vector>
126 int
128 {
129  if (data_.symbolic_ != NULL)
130  ::KLU2::klu_free_symbolic<slu_type, local_ordinal_type>
131  (&(data_.symbolic_), &(data_.common_)) ;
132  data_.symbolic_ = ::KLU2::klu_analyze<slu_type, local_ordinal_type>
133  ((local_ordinal_type)this->globalNumCols_, colptr_.getRawPtr(),
134  rowind_.getRawPtr(), &(data_.common_)) ;
135 
136  return(0);
137 }
138 
139 
140 template <class Matrix, class Vector>
141 int
143 {
144  using Teuchos::as;
145 
146  // Cleanup old L and U matrices if we are not reusing a symbolic
147  // factorization. Stores and other data will be allocated in gstrf.
148  // Only rank 0 has valid pointers, TODO: for KLU2
149 
150 
151  int info = 0;
152  if ( this->root_ ){
153 
154  { // Do factorization
155 #ifdef HAVE_AMESOS2_TIMERS
156  Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
157 #endif
158 
159 #ifdef HAVE_AMESOS2_VERBOSE_DEBUG
160  std::cout << "KLU2:: Before numeric factorization" << std::endl;
161  std::cout << "nzvals_ : " << nzvals_.toString() << std::endl;
162  std::cout << "rowind_ : " << rowind_.toString() << std::endl;
163  std::cout << "colptr_ : " << colptr_.toString() << std::endl;
164 #endif
165 
166  if (data_.numeric_ != NULL)
167  ::KLU2::klu_free_numeric<slu_type, local_ordinal_type>
168  (&(data_.numeric_), &(data_.common_)) ;
169  data_.numeric_ = ::KLU2::klu_factor<slu_type, local_ordinal_type>
170  (colptr_.getRawPtr(), rowind_.getRawPtr(), nzvals_.getRawPtr(),
171  data_.symbolic_, &(data_.common_)) ;
172 
173  }
174 
175  }
176 
177  /* All processes should have the same error code */
178  Teuchos::broadcast(*(this->matrixA_->getComm()), 0, &info);
179 
180  //global_size_type info_st = as<global_size_type>(info); // unused
181  /* TODO : Proper error messages
182  TEUCHOS_TEST_FOR_EXCEPTION( (info_st > 0) && (info_st <= this->globalNumCols_),
183  std::runtime_error,
184  "Factorization complete, but matrix is singular. Division by zero eminent");
185  TEUCHOS_TEST_FOR_EXCEPTION( (info_st > 0) && (info_st > this->globalNumCols_),
186  std::runtime_error,
187  "Memory allocation failure in KLU2 factorization");*/
188 
189  //data_.options.Fact = SLU::FACTORED;
190  //same_symbolic_ = true;
191 
192  return(info);
193 }
194 
195 
196 template <class Matrix, class Vector>
197 int
199  const Teuchos::Ptr<MultiVecAdapter<Vector> > X,
200  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
201 {
202  using Teuchos::as;
203 
204  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
205  const size_t nrhs = X->getGlobalNumVectors();
206 
207  const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
208  Teuchos::Array<slu_type> bValues(val_store_size);
209 
210  { // Get values from RHS B
211 #ifdef HAVE_AMESOS2_TIMERS
212  Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
213  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
214 #endif
216  slu_type>::do_get(B, bValues(),
217  as<size_t>(ld_rhs),
218  ROOTED);
219  }
220 
221 
222  int ierr = 0; // returned error code
223 
224  if ( this->root_ ) {
225 
226  //local_ordinal_type i_ld_rhs = as<local_ordinal_type>(ld_rhs);
227 
228  { // Do solve!
229 #ifdef HAVE_AMESOS2_TIMERS
230  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
231 #endif
232  if (transFlag_ == 0)
233  {
234  ::KLU2::klu_solve<slu_type, local_ordinal_type>
235  (data_.symbolic_, data_.numeric_,
236  (local_ordinal_type)this->globalNumCols_,
237  (local_ordinal_type)nrhs,
238  bValues.getRawPtr(), &(data_.common_)) ;
239  }
240  else
241  {
242  ::KLU2::klu_tsolve<slu_type, local_ordinal_type>
243  (data_.symbolic_, data_.numeric_,
244  (local_ordinal_type)this->globalNumCols_,
245  (local_ordinal_type)nrhs,
246  bValues.getRawPtr(), &(data_.common_)) ;
247  }
248 
249  }
250 
251  }
252 
253  /* All processes should have the same error code */
254  Teuchos::broadcast(*(this->getComm()), 0, &ierr);
255 
256  // global_size_type ierr_st = as<global_size_type>(ierr); // unused
257  // TODO
258  //TEUCHOS_TEST_FOR_EXCEPTION( ierr < 0,
259  //std::invalid_argument,
260  //"Argument " << -ierr << " to KLU2 xgssvx had illegal value" );
261  //TEUCHOS_TEST_FOR_EXCEPTION( ierr > 0 && ierr_st <= this->globalNumCols_,
262  //std::runtime_error,
263  //"Factorization complete, but U is exactly singular" );
264  //TEUCHOS_TEST_FOR_EXCEPTION( ierr > 0 && ierr_st > this->globalNumCols_ + 1,
265  //std::runtime_error,
266  //"KLU2 allocated " << ierr - this->globalNumCols_ << " bytes of "
267  //"memory before allocation failure occured." );
268 
269  /* Update X's global values */
270  {
271 #ifdef HAVE_AMESOS2_TIMERS
272  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
273 #endif
274 
276  MultiVecAdapter<Vector>,slu_type>::do_put(X, bValues(),
277  as<size_t>(ld_rhs),
278  ROOTED);
279  }
280 
281 
282  return(ierr);
283 }
284 
285 
286 template <class Matrix, class Vector>
287 bool
289 {
290  // The KLU2 factorization routines can handle square as well as
291  // rectangular matrices, but KLU2 can only apply the solve routines to
292  // square matrices, so we check the matrix for squareness.
293  return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
294 }
295 
296 
297 template <class Matrix, class Vector>
298 void
299 KLU2<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
300 {
301  using Teuchos::RCP;
302  using Teuchos::getIntegralValue;
303  using Teuchos::ParameterEntryValidator;
304 
305  RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
306 
307  transFlag_ = this->control_.useTranspose_ ? 1: 0;
308  // The KLU2 transpose option can override the Amesos2 option
309  if( parameterList->isParameter("Trans") ){
310  RCP<const ParameterEntryValidator> trans_validator = valid_params->getEntry("Trans").validator();
311  parameterList->getEntry("Trans").setValidator(trans_validator);
312 
313  transFlag_ = getIntegralValue<int>(*parameterList, "Trans");
314  }
315 }
316 
317 
318 template <class Matrix, class Vector>
319 Teuchos::RCP<const Teuchos::ParameterList>
321 {
322  using std::string;
323  using Teuchos::tuple;
324  using Teuchos::ParameterList;
325  using Teuchos::setStringToIntegralParameter;
326 
327  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
328 
329  if( is_null(valid_params) )
330  {
331  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
332 
333  pl->set("Equil", true, "Whether to equilibrate the system before solve, does nothing now");
334 
335  setStringToIntegralParameter<int>("Trans", "NOTRANS",
336  "Solve for the transpose system or not",
337  tuple<string>("NOTRANS","TRANS","CONJ"),
338  tuple<string>("Solve with transpose",
339  "Do not solve with transpose",
340  "Solve with the conjugate transpose"),
341  tuple<int>(0, 1, 2),
342  pl.getRawPtr());
343  valid_params = pl;
344  }
345 
346  return valid_params;
347 }
348 
349 
350 template <class Matrix, class Vector>
351 bool
353 {
354  using Teuchos::as;
355 
356  if(current_phase == SOLVE)return(false);
357 
358 #ifdef HAVE_AMESOS2_TIMERS
359  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
360 #endif
361 
362  // Only the root image needs storage allocated
363  if( this->root_ ){
364  nzvals_.resize(this->globalNumNonZeros_);
365  rowind_.resize(this->globalNumNonZeros_);
366  colptr_.resize(this->globalNumCols_ + 1);
367  }
368 
369  local_ordinal_type nnz_ret = 0;
370  {
371 #ifdef HAVE_AMESOS2_TIMERS
372  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
373 #endif
374 
376  MatrixAdapter<Matrix>,slu_type,local_ordinal_type,local_ordinal_type>
377  ::do_get(this->matrixA_.ptr(), nzvals_(), rowind_(), colptr_(),
378  nnz_ret, ROOTED, ARBITRARY, this->rowIndexBase_);
379  }
380 
381 
382  if( this->root_ ){
383  TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != as<local_ordinal_type>(this->globalNumNonZeros_),
384  std::runtime_error,
385  "Did not get the expected number of non-zero vals");
386  }
387 
388  return true;
389 }
390 
391 
392 template<class Matrix, class Vector>
393 const char* KLU2<Matrix,Vector>::name = "KLU2";
394 
395 
396 } // end namespace Amesos2
397 
398 #endif // AMESOS2_KLU2_DEF_HPP
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
KLU2(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_KLU2_def.hpp:66
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
Amesos2 KLU2 declarations.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_KLU2_def.hpp:320
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_KLU2_def.hpp:352
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:243
~KLU2()
Destructor.
Definition: Amesos2_KLU2_def.hpp:86
Definition: Amesos2_TypeDecl.hpp:142
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > &parameterList)
Definition: Amesos2_KLU2_def.hpp:299
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using KLU2.
Definition: Amesos2_KLU2_def.hpp:127
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:580
Definition: Amesos2_KLU2_FunctionMap.hpp:67
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_KLU2_def.hpp:113
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:198
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_KLU2_def.hpp:288
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
int numericFactorization_impl()
KLU2 specific numeric factorization.
Definition: Amesos2_KLU2_def.hpp:142
Amesos2 interface to the KLU2 package.
Definition: Amesos2_KLU2_decl.hpp:72
Definition: Amesos2_TypeDecl.hpp:127
Helper class for putting 1-D data arrays into multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:296
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:175