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  , transFlag_(0)
72  , is_contiguous_(true)
73 {
74  ::KLU2::klu_defaults<klu2_dtype, local_ordinal_type> (&(data_.common_)) ;
75  data_.symbolic_ = NULL;
76  data_.numeric_ = NULL;
77 
78  // Override some default options
79  // TODO: use data_ here to init
80 }
81 
82 
83 template <class Matrix, class Vector>
85 {
86  /* Free KLU2 data_types
87  * - Matrices
88  * - Vectors
89  * - Other data
90  */
91  if (data_.symbolic_ != NULL)
92  ::KLU2::klu_free_symbolic<klu2_dtype, local_ordinal_type>
93  (&(data_.symbolic_), &(data_.common_)) ;
94  if (data_.numeric_ != NULL)
95  ::KLU2::klu_free_numeric<klu2_dtype, local_ordinal_type>
96  (&(data_.numeric_), &(data_.common_)) ;
97 
98  // Storage is initialized in numericFactorization_impl()
99  //if ( data_.A.Store != NULL ){
100  // destoy
101  //}
102 
103  // only root allocated these SuperMatrices.
104  //if ( data_.L.Store != NULL ){ // will only be true for this->root_
105  // destroy ..
106  //}
107 }
108 
109 template <class Matrix, class Vector>
110 bool
112  return (this->root_ && (this->matrixA_->getComm()->getSize() == 1) && is_contiguous_);
113 }
114 
115 template<class Matrix, class Vector>
116 int
118 {
119  /* TODO: Define what it means for KLU2
120  */
121 #ifdef HAVE_AMESOS2_TIMERS
122  Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_);
123 #endif
124 
125  return(0);
126 }
127 
128 
129 template <class Matrix, class Vector>
130 int
132 {
133  if (data_.symbolic_ != NULL) {
134  ::KLU2::klu_free_symbolic<klu2_dtype, local_ordinal_type>
135  (&(data_.symbolic_), &(data_.common_)) ;
136  }
137 
138  if ( single_proc_optimization() ) {
139  host_ordinal_type_array host_row_ptr_view;
140  host_ordinal_type_array host_cols_view;
141  this->matrixA_->returnRowPtr_kokkos_view(host_row_ptr_view);
142  this->matrixA_->returnColInd_kokkos_view(host_cols_view);
143  data_.symbolic_ = ::KLU2::klu_analyze<klu2_dtype, local_ordinal_type>
144  ((local_ordinal_type)this->globalNumCols_, host_row_ptr_view.data(),
145  host_cols_view.data(), &(data_.common_)) ;
146  }
147  else
148  {
149  data_.symbolic_ = ::KLU2::klu_analyze<klu2_dtype, local_ordinal_type>
150  ((local_ordinal_type)this->globalNumCols_, host_col_ptr_view_.data(),
151  host_rows_view_.data(), &(data_.common_)) ;
152 
153  } //end single_process_optim_check = false
154 
155  return(0);
156 }
157 
158 
159 template <class Matrix, class Vector>
160 int
162 {
163  using Teuchos::as;
164 
165  // Cleanup old L and U matrices if we are not reusing a symbolic
166  // factorization. Stores and other data will be allocated in gstrf.
167  // Only rank 0 has valid pointers, TODO: for KLU2
168 
169  int info = 0;
170  if ( this->root_ ) {
171 
172  { // Do factorization
173 #ifdef HAVE_AMESOS2_TIMERS
174  Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
175 #endif
176 
177  if (data_.numeric_ != NULL) {
178  ::KLU2::klu_free_numeric<klu2_dtype, local_ordinal_type>
179  (&(data_.numeric_), &(data_.common_));
180  }
181 
182  if ( single_proc_optimization() ) {
183  host_ordinal_type_array host_row_ptr_view;
184  host_ordinal_type_array host_cols_view;
185  this->matrixA_->returnRowPtr_kokkos_view(host_row_ptr_view);
186  this->matrixA_->returnColInd_kokkos_view(host_cols_view);
187  this->matrixA_->returnValues_kokkos_view(host_nzvals_view_);
188  klu2_dtype * pValues = function_map::convert_scalar(host_nzvals_view_.data());
189  data_.numeric_ = ::KLU2::klu_factor<klu2_dtype, local_ordinal_type>
190  (host_row_ptr_view.data(), host_cols_view.data(), pValues,
191  data_.symbolic_, &(data_.common_));
192  }
193  else {
194  klu2_dtype * pValues = function_map::convert_scalar(host_nzvals_view_.data());
195  data_.numeric_ = ::KLU2::klu_factor<klu2_dtype, local_ordinal_type>
196  (host_col_ptr_view_.data(), host_rows_view_.data(), pValues,
197  data_.symbolic_, &(data_.common_));
198  } //end single_process_optim_check = false
199 
200  // To have a test which confirms a throw, we need MPI to throw on all the
201  // ranks. So we delay and broadcast first. Others throws in Amesos2 which
202  // happen on just the root rank would also have the same problem if we
203  // tested them but we decided to fix just this one for the present. This
204  // is the only error/throw we currently have a unit test for.
205  if(data_.numeric_ == nullptr) {
206  info = 1;
207  }
208 
209  // This is set after numeric factorization complete as pivoting can be used;
210  // In this case, a discrepancy between symbolic and numeric nnz total can occur.
211  if(info == 0) { // skip if error code so we don't segfault - will throw
212  this->setNnzLU( as<size_t>((data_.numeric_)->lnz) + as<size_t>((data_.numeric_)->unz) );
213  }
214  } // end scope
215 
216  } // end this->root_
217 
218  /* All processes should have the same error code */
219  Teuchos::broadcast(*(this->matrixA_->getComm()), 0, &info);
220 
221  TEUCHOS_TEST_FOR_EXCEPTION(info > 0, std::runtime_error,
222  "KLU2 numeric factorization failed");
223 
224  return(info);
225 }
226 
227 template <class Matrix, class Vector>
228 int
230  const Teuchos::Ptr<MultiVecAdapter<Vector> > X,
231  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
232 {
233  using Teuchos::as;
234  int ierr = 0; // returned error code
235 
236  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
237  const size_t nrhs = X->getGlobalNumVectors();
238 
239  bool bDidAssignX;
240  bool bDidAssignB;
241  {
242 #ifdef HAVE_AMESOS2_TIMERS
243  Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
244  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
245 #endif
246  const bool initialize_data = true;
247  const bool do_not_initialize_data = false;
248  if ( single_proc_optimization() && nrhs == 1 ) {
249  // no msp creation
250  bDidAssignB = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
251  host_solve_array_t>::do_get(initialize_data, B, bValues_, as<size_t>(ld_rhs));
252 
253  bDidAssignX = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
254  host_solve_array_t>::do_get(do_not_initialize_data, X, xValues_, as<size_t>(ld_rhs));
255  }
256  else {
257 
258  bDidAssignB = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
259  host_solve_array_t>::do_get(initialize_data, B, bValues_,
260  as<size_t>(ld_rhs),
261  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
262  this->rowIndexBase_);
263 
264  // see Amesos2_Tacho_def.hpp for an explanation of why we 'get' X
265  bDidAssignX = Util::get_1d_copy_helper_kokkos_view<MultiVecAdapter<Vector>,
266  host_solve_array_t>::do_get(do_not_initialize_data, X, xValues_,
267  as<size_t>(ld_rhs),
268  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
269  this->rowIndexBase_);
270 
271  // klu_tsolve is going to put the solution x into the input b.
272  // Copy b to x then solve in x.
273  // We do not want to solve in b, then copy to x, because if b was assigned
274  // then the solve will change b permanently and mess up the next test cycle.
275  // However if b was actually a copy (bDidAssignB = false) then we can avoid
276  // this deep_copy and just assign xValues_ = bValues_.
277  if(bDidAssignB) {
278  Kokkos::deep_copy(xValues_, bValues_); // need deep_copy or solve will change adapter's b memory which should never happen
279  }
280  else {
281  xValues_ = bValues_; // safe because bValues_ does not point straight to adapter's memory space
282  }
283  }
284  }
285 
286  klu2_dtype * pxValues = function_map::convert_scalar(xValues_.data());
287  klu2_dtype * pbValues = function_map::convert_scalar(bValues_.data());
288 
289  // can be null for non root
290  if( this->root_) {
291  TEUCHOS_TEST_FOR_EXCEPTION(pbValues == nullptr,
292  std::runtime_error, "Amesos2 Runtime Error: b_vector returned null ");
293 
294  TEUCHOS_TEST_FOR_EXCEPTION(pxValues == nullptr,
295  std::runtime_error, "Amesos2 Runtime Error: x_vector returned null ");
296  }
297 
298  if ( single_proc_optimization() && nrhs == 1 ) {
299 #ifdef HAVE_AMESOS2_TIMERS
300  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
301 #endif
302 
303  // For this case, Crs matrix raw pointers were used, so the non-transpose default solve
304  // is actually the transpose solve as klu_solve expects Ccs matrix pointers
305  // Thus, if the transFlag_ is true, the non-transpose solve should be used
306  if (transFlag_ == 0)
307  {
308  ::KLU2::klu_tsolve2<klu2_dtype, local_ordinal_type>
309  (data_.symbolic_, data_.numeric_,
310  (local_ordinal_type)this->globalNumCols_,
311  (local_ordinal_type)nrhs,
312  pbValues, pxValues, &(data_.common_)) ;
313  }
314  else {
315  ::KLU2::klu_solve2<klu2_dtype, local_ordinal_type>
316  (data_.symbolic_, data_.numeric_,
317  (local_ordinal_type)this->globalNumCols_,
318  (local_ordinal_type)nrhs,
319  pbValues, pxValues, &(data_.common_)) ;
320  }
321 
322  /* All processes should have the same error code */
323  // Teuchos::broadcast(*(this->getComm()), 0, &ierr);
324 
325  } // end single_process_optim_check && nrhs == 1
326  else // single proc optimizations but nrhs > 1,
327  // or distributed over processes case
328  {
329  if ( this->root_ ) {
330 #ifdef HAVE_AMESOS2_TIMERS
331  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
332 #endif
333  if (transFlag_ == 0)
334  {
335  // For this case, Crs matrix raw pointers were used, so the non-transpose default solve
336  // is actually the transpose solve as klu_solve expects Ccs matrix pointers
337  // Thus, if the transFlag_ is true, the non-transpose solve should be used
338  if ( single_proc_optimization() ) {
339  ::KLU2::klu_tsolve<klu2_dtype, local_ordinal_type>
340  (data_.symbolic_, data_.numeric_,
341  (local_ordinal_type)this->globalNumCols_,
342  (local_ordinal_type)nrhs,
343  pxValues, &(data_.common_)) ;
344  }
345  else {
346  ::KLU2::klu_solve<klu2_dtype, local_ordinal_type>
347  (data_.symbolic_, data_.numeric_,
348  (local_ordinal_type)this->globalNumCols_,
349  (local_ordinal_type)nrhs,
350  pxValues, &(data_.common_)) ;
351  }
352  }
353  else
354  {
355  // For this case, Crs matrix raw pointers were used, so the non-transpose default solve
356  // is actually the transpose solve as klu_solve expects Ccs matrix pointers
357  // Thus, if the transFlag_ is true, the non- transpose solve should be used
358  if ( single_proc_optimization() ) {
359  ::KLU2::klu_solve<klu2_dtype, local_ordinal_type>
360  (data_.symbolic_, data_.numeric_,
361  (local_ordinal_type)this->globalNumCols_,
362  (local_ordinal_type)nrhs,
363  pxValues, &(data_.common_)) ;
364  }
365  else {
366  ::KLU2::klu_tsolve<klu2_dtype, local_ordinal_type>
367  (data_.symbolic_, data_.numeric_,
368  (local_ordinal_type)this->globalNumCols_,
369  (local_ordinal_type)nrhs,
370  pxValues, &(data_.common_)) ;
371  }
372  }
373  } // end root_
374  } //end else
375 
376  // if bDidAssignX, then we solved straight to the adapter's X memory space without
377  // requiring additional memory allocation, so the x data is already in place.
378  if(!bDidAssignX) {
379 #ifdef HAVE_AMESOS2_TIMERS
380  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
381 #endif
382 
383  Util::put_1d_data_helper_kokkos_view<
384  MultiVecAdapter<Vector>,host_solve_array_t>::do_put(X, xValues_,
385  as<size_t>(ld_rhs),
386  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
387  this->rowIndexBase_);
388 
389  }
390 
391  return(ierr);
392 }
393 
394 
395 template <class Matrix, class Vector>
396 bool
398 {
399  // The KLU2 factorization routines can handle square as well as
400  // rectangular matrices, but KLU2 can only apply the solve routines to
401  // square matrices, so we check the matrix for squareness.
402  return( this->matrixA_->getGlobalNumRows() == this->matrixA_->getGlobalNumCols() );
403 }
404 
405 
406 template <class Matrix, class Vector>
407 void
408 KLU2<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
409 {
410  using Teuchos::RCP;
411  using Teuchos::getIntegralValue;
412  using Teuchos::ParameterEntryValidator;
413 
414  RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
415 
416  transFlag_ = this->control_.useTranspose_ ? 1: 0;
417  // The KLU2 transpose option can override the Amesos2 option
418  if( parameterList->isParameter("Trans") ){
419  RCP<const ParameterEntryValidator> trans_validator = valid_params->getEntry("Trans").validator();
420  parameterList->getEntry("Trans").setValidator(trans_validator);
421 
422  transFlag_ = getIntegralValue<int>(*parameterList, "Trans");
423  }
424 
425  if( parameterList->isParameter("IsContiguous") ){
426  is_contiguous_ = parameterList->get<bool>("IsContiguous");
427  }
428 }
429 
430 
431 template <class Matrix, class Vector>
432 Teuchos::RCP<const Teuchos::ParameterList>
434 {
435  using std::string;
436  using Teuchos::tuple;
437  using Teuchos::ParameterList;
438  using Teuchos::setStringToIntegralParameter;
439 
440  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
441 
442  if( is_null(valid_params) )
443  {
444  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
445 
446  pl->set("Equil", true, "Whether to equilibrate the system before solve, does nothing now");
447  pl->set("IsContiguous", true, "Whether GIDs contiguous");
448 
449  setStringToIntegralParameter<int>("Trans", "NOTRANS",
450  "Solve for the transpose system or not",
451  tuple<string>("NOTRANS","TRANS","CONJ"),
452  tuple<string>("Solve with transpose",
453  "Do not solve with transpose",
454  "Solve with the conjugate transpose"),
455  tuple<int>(0, 1, 2),
456  pl.getRawPtr());
457  valid_params = pl;
458  }
459 
460  return valid_params;
461 }
462 
463 
464 template <class Matrix, class Vector>
465 bool
467 {
468  using Teuchos::as;
469 
470  if(current_phase == SOLVE)return(false);
471 
472  if ( single_proc_optimization() ) {
473  // Do nothing in this case - Crs raw pointers will be used
474  }
475  else
476  {
477 
478 #ifdef HAVE_AMESOS2_TIMERS
479  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
480 #endif
481 
482  // Only the root image needs storage allocated
483  if( this->root_ ) {
484  host_nzvals_view_ = host_value_type_array(
485  Kokkos::ViewAllocateWithoutInitializing("host_nzvals_view_"), this->globalNumNonZeros_);
486  host_rows_view_ = host_ordinal_type_array(
487  Kokkos::ViewAllocateWithoutInitializing("host_rows_view_"), this->globalNumNonZeros_);
488  host_col_ptr_view_ = host_ordinal_type_array(
489  Kokkos::ViewAllocateWithoutInitializing("host_col_ptr_view_"), this->globalNumRows_ + 1);
490  }
491 
492  local_ordinal_type nnz_ret = 0;
493  {
494 #ifdef HAVE_AMESOS2_TIMERS
495  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
496 #endif
497 
499  MatrixAdapter<Matrix>,host_value_type_array,host_ordinal_type_array,host_ordinal_type_array>
500  ::do_get(this->matrixA_.ptr(), host_nzvals_view_, host_rows_view_, host_col_ptr_view_, nnz_ret,
501  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
502  ARBITRARY,
503  this->rowIndexBase_);
504  }
505 
506  if( this->root_ ) {
507  TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != as<local_ordinal_type>(this->globalNumNonZeros_),
508  std::runtime_error,
509  "Did not get the expected number of non-zero vals");
510  }
511 
512  } //end else single_process_optim_check = false
513 
514  return true;
515 }
516 
517 
518 template<class Matrix, class Vector>
519 const char* KLU2<Matrix,Vector>::name = "KLU2";
520 
521 
522 } // end namespace Amesos2
523 
524 #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
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:648
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:229
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
Amesos2 KLU2 declarations.
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_KLU2_def.hpp:466
~KLU2()
Destructor.
Definition: Amesos2_KLU2_def.hpp:84
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > &parameterList)
Definition: Amesos2_KLU2_def.hpp:408
int symbolicFactorization_impl()
Perform symbolic factorization of the matrix using KLU2.
Definition: Amesos2_KLU2_def.hpp:131
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_KLU2_def.hpp:117
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_KLU2_def.hpp:397
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
int numericFactorization_impl()
KLU2 specific numeric factorization.
Definition: Amesos2_KLU2_def.hpp:161
Amesos2 interface to the KLU2 package.
Definition: Amesos2_KLU2_decl.hpp:72
bool single_proc_optimization() const
can we optimize size_type and ordinal_type for straight pass through, also check that is_contiguous_ ...
Definition: Amesos2_KLU2_def.hpp:111
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_KLU2_def.hpp:433
A templated MultiVector class adapter for Amesos2.
Definition: Amesos2_MultiVecAdapter_decl.hpp:176