Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Lapack_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 
44 
53 #ifndef AMESOS2_LAPACK_DEF_HPP
54 #define AMESOS2_LAPACK_DEF_HPP
55 
56 #include <Teuchos_RCP.hpp>
57 
59 #include "Amesos2_Lapack_decl.hpp"
60 
61 namespace Amesos2 {
62 
63 
64  template <class Matrix, class Vector>
65  Lapack<Matrix,Vector>::Lapack(Teuchos::RCP<const Matrix> A,
66  Teuchos::RCP<Vector> X,
67  Teuchos::RCP<const Vector> B)
68  : SolverCore<Amesos2::Lapack,Matrix,Vector>(A, X, B) // instantiate superclass
69  , is_contiguous_(true)
70  {
71  // Set default parameters
72  Teuchos::RCP<Teuchos::ParameterList> default_params
73  = Teuchos::parameterList( *(this->getValidParameters()) );
74  this->setParameters(default_params);
75  }
76 
77 
78  template <class Matrix, class Vector>
80  {
81  /*
82  * Free any memory allocated by the Lapack library functions (i.e. none)
83  */
84  }
85 
86 
87  template<class Matrix, class Vector>
88  int
90  {
91  return(0);
92  }
93 
94 
95  template <class Matrix, class Vector>
96  int
98  {
99  return(0);
100  }
101 
102 
103  template <class Matrix, class Vector>
104  int
106  {
107  int factor_ierr = 0;
108 
109  if( this->root_ ){
110  // Set here so that solver_ can refresh it's internal state
111  solver_.setMatrix( Teuchos::rcpFromRef(lu_) );
112 
113  {
114 #ifdef HAVE_AMESOS2_TIMERS
115  Teuchos::TimeMonitor numFactTimer( this->timers_.numFactTime_ );
116 #endif
117  factor_ierr = solver_.factor();
118  }
119  }
120 
121  Teuchos::broadcast(*(this->getComm()), 0, &factor_ierr);
122  TEUCHOS_TEST_FOR_EXCEPTION( factor_ierr != 0,
123  std::runtime_error,
124  "Lapack factor routine returned error code "
125  << factor_ierr );
126  return( 0 );
127  }
128 
129 
130  template <class Matrix, class Vector>
131  int
133  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
134  {
135  using Teuchos::as;
136 
137  // Convert X and B to SerialDenseMatrix's
138  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
139  const size_t nrhs = X->getGlobalNumVectors();
140 
141  const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
142  if( this->root_ ){
143  rhsvals_.resize(val_store_size);
144  }
145 
146  { // Get values from RHS B
147 #ifdef HAVE_AMESOS2_TIMERS
148  Teuchos::TimeMonitor mvConvTimer( this->timers_.vecConvTime_ );
149  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
150 #endif
152  scalar_type> copy_helper;
153  copy_helper::do_get(B, rhsvals_(),
154  as<size_t>(ld_rhs),
155  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
156  0);
157  }
158 
159  int solve_ierr = 0;
160  // int unequilibrate_ierr = 0; // unused
161 
162  if( this->root_ ){
163 #ifdef HAVE_AMESOS2_TIMERS
164  Teuchos::TimeMonitor solveTimer( this->timers_.solveTime_ );
165 #endif
166 
167  using Teuchos::rcpFromRef;
168  typedef Teuchos::SerialDenseMatrix<int,scalar_type> DenseMat;
169 
170  DenseMat rhs_dense_mat(Teuchos::View, rhsvals_.getRawPtr(),
171  as<int>(ld_rhs), as<int>(ld_rhs), as<int>(nrhs));
172 
173  solver_.setVectors( rcpFromRef(rhs_dense_mat),
174  rcpFromRef(rhs_dense_mat) );
175 
176  solve_ierr = solver_.solve();
177 
178  // Solution is found in rhsvals_
179  }
180 
181  // Consolidate and check error codes
182  Teuchos::broadcast(*(this->getComm()), 0, &solve_ierr);
183  TEUCHOS_TEST_FOR_EXCEPTION( solve_ierr != 0,
184  std::runtime_error,
185  "Lapack solver solve method returned with error code "
186  << solve_ierr );
187 
188  /* Update X's global values */
189  {
190 #ifdef HAVE_AMESOS2_TIMERS
191  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
192 #endif
193 
195  MultiVecAdapter<Vector>,scalar_type>::do_put(X, rhsvals_(),
196  as<size_t>(ld_rhs),
197  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED);
198  }
199 
200  return( 0 );
201  }
202 
203 
204  template <class Matrix, class Vector>
205  bool
207  {
208  // Factorization of rectangular matrices is supported, but not
209  // their solution. For solution we can have square matrices.
210 
211  return( this->globalNumCols_ == this->globalNumRows_ );
212  }
213 
214 
215  template <class Matrix, class Vector>
216  void
217  Lapack<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
218  {
219  solver_.solveWithTranspose( parameterList->get<bool>("Transpose",
220  this->control_.useTranspose_) );
221 
222  solver_.factorWithEquilibration( parameterList->get<bool>("Equilibrate", true) );
223 
224  if( parameterList->isParameter("IsContiguous") ){
225  is_contiguous_ = parameterList->get<bool>("IsContiguous");
226  }
227 
228  // solver_.solveToRefinedSolution( parameterList->get<bool>("Refine", false) );
229  }
230 
231  template <class Matrix, class Vector>
232  Teuchos::RCP<const Teuchos::ParameterList>
234  {
235  using Teuchos::ParameterList;
236 
237  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
238 
239  if( is_null(valid_params) ){
240  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
241 
242  pl->set("Equilibrate", true, "Whether to equilibrate the input matrix");
243 
244  pl->set("IsContiguous", true, "Whether GIDs contiguous");
245 
246  // TODO: Refinement does not seem to be working with the SerialDenseSolver. Not sure why.
247  // pl->set("Refine", false, "Whether to apply iterative refinement");
248 
249  valid_params = pl;
250  }
251 
252  return valid_params;
253  }
254 
255  template <class Matrix, class Vector>
256  bool
258  {
259 #ifdef HAVE_AMESOS2_TIMERS
260  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
261 #endif
262 
263  // We only load the matrix when numericFactorization is called
264  if( current_phase < NUMFACT ) return( false );
265 
266  if( this->root_ ){
267  Kokkos::resize(nzvals_view_,this->globalNumNonZeros_);
268  Kokkos::resize(rowind_view_,this->globalNumNonZeros_);
269  Kokkos::resize(colptr_view_,this->globalNumCols_ + 1);
270  }
271 
272  // global_size_type nnz_ret = 0;
273  int nnz_ret = 0;
274  {
275 #ifdef HAVE_AMESOS2_TIMERS
276  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
277 #endif
278 
279  // typedef Util::get_ccs_helper<MatrixAdapter<Matrix>,
280  // scalar_type, global_ordinal_type, global_size_type> ccs_helper;
282  host_value_type_array, host_ordinal_type_array, host_ordinal_type_array> ccs_helper;
283 
284  ccs_helper::do_get(this->matrixA_.ptr(),
285  nzvals_view_, rowind_view_, colptr_view_, nnz_ret,
286  (is_contiguous_ == true) ? ROOTED : CONTIGUOUS_AND_ROOTED,
287  ARBITRARY,
288  this->rowIndexBase_);
289  }
290 
291  if( this->root_ ){
292  // entries are initialized to zero in here:
293  lu_.shape(this->globalNumRows_, this->globalNumCols_);
294 
295  // Put entries of ccs representation into the dense matrix
296  global_size_type end_col = this->globalNumCols_;
297  for( global_size_type col = 0; col < end_col; ++col ){
298  global_ordinal_type ptr = colptr_view_[col];
299  global_ordinal_type end_ptr = colptr_view_[col+1];
300  for( ; ptr < end_ptr; ++ptr ){
301  lu_(rowind_view_[ptr], col) = nzvals_view_[ptr];
302  }
303  }
304 
305  // lu_.print(std::cout);
306  }
307 
308  return( true );
309 }
310 
311 
312  template<class Matrix, class Vector>
313  const char* Lapack<Matrix,Vector>::name = "LAPACK";
314 
315 
316 } // end namespace Amesos2
317 
318 #endif // AMESOS2_LAPACK_DEF_HPP
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const override
Return a const parameter list of all of the valid parameters that this-&gt;setParameterList(...) will accept.
Definition: Amesos2_SolverCore_def.hpp:558
Amesos2::SolverCore: A templated interface for interaction with third-party direct sparse solvers...
Definition: Amesos2_SolverCore_decl.hpp:105
Amesos2 interface to the LAPACK.
Definition: Amesos2_Lapack_decl.hpp:80
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:648
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
int symbolicFactorization_impl()
No-op.
Definition: Amesos2_Lapack_def.hpp:97
super_type & setParameters(const Teuchos::RCP< Teuchos::ParameterList > &parameterList) override
Set/update internal variables and solver options.
Definition: Amesos2_SolverCore_def.hpp:526
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:267
void setParameters_impl(const Teuchos::RCP< Teuchos::ParameterList > &parameterList)
Definition: Amesos2_Lapack_def.hpp:217
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Lapack_def.hpp:206
Lapack(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize from Teuchos::RCP.
Definition: Amesos2_Lapack_def.hpp:65
int preOrdering_impl()
No-op.
Definition: Amesos2_Lapack_def.hpp:89
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Lapack_def.hpp:233
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
Lapack solve.
Definition: Amesos2_Lapack_def.hpp:132
Declarations for the Amesos2 interface to LAPACK.
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_Lapack_def.hpp:257
~Lapack()
Destructor.
Definition: Amesos2_Lapack_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
int numericFactorization_impl()
Perform numeric factorization using LAPACK.
Definition: Amesos2_Lapack_def.hpp:105