Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Basker_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 
53 #ifndef AMESOS2_BASKER_DEF_HPP
54 #define AMESOS2_BASKER_DEF_HPP
55 
56 #include <Teuchos_Tuple.hpp>
57 #include <Teuchos_ParameterList.hpp>
58 #include <Teuchos_StandardParameterEntryValidators.hpp>
59 
61 #include "Amesos2_Basker_decl.hpp"
62 
63 namespace Amesos2 {
64 
65 
66 template <class Matrix, class Vector>
67 Basker<Matrix,Vector>::Basker(
68  Teuchos::RCP<const Matrix> A,
69  Teuchos::RCP<Vector> X,
70  Teuchos::RCP<const Vector> B )
71  : SolverCore<Amesos2::Basker,Matrix,Vector>(A, X, B)
72  , nzvals_() // initialize to empty arrays
73  , rowind_()
74  , colptr_()
75 {
76 
77  //Nothing
78 
79  // Override some default options
80  // TODO: use data_ here to init
81 }
82 
83 
84 template <class Matrix, class Vector>
85 Basker<Matrix,Vector>::~Basker( )
86 {
87  /* Basker will cleanup its own internal memory*/
88 }
89 
90 template<class Matrix, class Vector>
91 int
93 {
94  /* TODO: Define what it means for Basker
95  */
96 #ifdef HAVE_AMESOS2_TIMERS
97  Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_);
98 #endif
99 
100  return(0);
101 }
102 
103 
104 template <class Matrix, class Vector>
105 int
107 {
108 
109  /*No symbolic factoriztion*/
110  return(0);
111 }
112 
113 
114 template <class Matrix, class Vector>
115 int
117 {
118  using Teuchos::as;
119 
120  int info = 0;
121  if ( this->root_ ){
122  { // Do factorization
123 #ifdef HAVE_AMESOS2_TIMERS
124  Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
125 #endif
126 
127 #ifdef HAVE_AMESOS2_VERBOSE_DEBUG
128  std::cout << "Basker:: Before numeric factorization" << std::endl;
129  std::cout << "nzvals_ : " << nzvals_.toString() << std::endl;
130  std::cout << "rowind_ : " << rowind_.toString() << std::endl;
131  std::cout << "colptr_ : " << colptr_.toString() << std::endl;
132 #endif
133 
134  info =basker.factor(this->globalNumRows_, this->globalNumCols_, this->globalNumNonZeros_, colptr_.getRawPtr(), rowind_.getRawPtr(), nzvals_.getRawPtr());
135 
136  }
137 
138  }
139 
140  /* All processes should have the same error code */
141  Teuchos::broadcast(*(this->matrixA_->getComm()), 0, &info);
142 
143  //global_size_type info_st = as<global_size_type>(info);
144  /* TODO : Proper error messages*/
145  TEUCHOS_TEST_FOR_EXCEPTION( (info == -1) ,
146  std::runtime_error,
147  "Basker: Could not alloc space for L and U");
148  TEUCHOS_TEST_FOR_EXCEPTION( (info == -2),
149  std::runtime_error,
150  "Basker: Could not alloc needed work space");
151  TEUCHOS_TEST_FOR_EXCEPTION( (info == -3) ,
152  std::runtime_error,
153  "Basker: Could not alloc additional memory needed for L and U");
154  TEUCHOS_TEST_FOR_EXCEPTION( (info > 0) ,
155  std::runtime_error,
156  "Basker: Zero pivot found at: " << info );
157 
158  return(info);
159 }
160 
161 
162 template <class Matrix, class Vector>
163 int
165  const Teuchos::Ptr<MultiVecAdapter<Vector> > X,
166  const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
167 {
168  using Teuchos::as;
169 
170  const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
171  const size_t nrhs = X->getGlobalNumVectors();
172 
173  const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
174 
175  xvals_.resize(val_store_size);
176  bvals_.resize(val_store_size);
177 
178  { // Get values from RHS B
179 #ifdef HAVE_AMESOS2_TIMERS
180  Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
181  Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
182 #endif
184  slu_type>::do_get(B, bvals_(),as<size_t>(ld_rhs),
185  ROOTED);
186  }
187 
188  int ierr = 0; // returned error code
189 
190  if ( this->root_ ) {
191  { // Do solve!
192 #ifdef HAVE_AMESOS2_TIMERS
193  Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
194 #endif
195 
196  ierr = basker.solveMultiple(nrhs, bvals_.getRawPtr(),xvals_.getRawPtr());
197  }
198 
199  }
200 
201  /* All processes should have the same error code */
202  Teuchos::broadcast(*(this->getComm()), 0, &ierr);
203 
204  TEUCHOS_TEST_FOR_EXCEPTION( ierr > 0,
205  std::runtime_error,
206  "Encountered zero diag element at: " << ierr);
207  TEUCHOS_TEST_FOR_EXCEPTION( ierr == -1,
208  std::runtime_error,
209  "Could not alloc needed working memory for solve" );
210 
211  {
212 #ifdef HAVE_AMESOS2_TIMERS
213  Teuchos::TimeMonitor redistTimer(this->timers_.vecRedistTime_);
214 #endif
215 
217  MultiVecAdapter<Vector>,slu_type>::do_put(X, xvals_(),
218  as<size_t>(ld_rhs),
219  ROOTED);
220  }
221 
222  return(ierr);
223 }
224 
225 
226 template <class Matrix, class Vector>
227 bool
229 {
230  // The Basker can only handle square for right now
231  return( this->globalNumRows_ == this->globalNumCols_ );
232 }
233 
234 
235 template <class Matrix, class Vector>
236 void
237 Basker<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
238 {
239  using Teuchos::RCP;
240  using Teuchos::getIntegralValue;
241  using Teuchos::ParameterEntryValidator;
242 
243  //RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
244  /*To Do --- add support for parameters */
245  if(parameterList->isParameter("Trans"))
246  {}
247 }
248 
249 template <class Matrix, class Vector>
250 Teuchos::RCP<const Teuchos::ParameterList>
252 {
253  using Teuchos::ParameterList;
254 
255  static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
256 
257  if( is_null(valid_params) ){
258  Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
259 
260  pl->set("alnnz", 2, "Approx number of nonzeros in L, default is 2*nnz(A)");
261  pl->set("aunnx", 2, "Approx number of nonzeros in I, default is 2*nnz(U)");
262  valid_params = pl;
263  }
264 
265  return valid_params;
266 }
267 
268 
269 template <class Matrix, class Vector>
270 bool
272 {
273  using Teuchos::as;
274 
275  if(current_phase == SOLVE) return (false);
276 
277 #ifdef HAVE_AMESOS2_TIMERS
278  Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
279 #endif
280 
281 
282 
283  // Only the root image needs storage allocated
284  if( this->root_ ){
285  nzvals_.resize(this->globalNumNonZeros_);
286  rowind_.resize(this->globalNumNonZeros_);
287  colptr_.resize(this->globalNumCols_ + 1);
288  }
289 
290  local_ordinal_type nnz_ret = 0;
291  {
292 #ifdef HAVE_AMESOS2_TIMERS
293  Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
294 #endif
295 
297  MatrixAdapter<Matrix>,slu_type,local_ordinal_type,local_ordinal_type>
298  ::do_get(this->matrixA_.ptr(), nzvals_(), rowind_(), colptr_(),
299  nnz_ret, ROOTED, ARBITRARY);
300  }
301 
302 
303  if( this->root_ ){
304  TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != as<local_ordinal_type>(this->globalNumNonZeros_),
305  std::runtime_error,
306  "Did not get the expected number of non-zero vals");
307  }
308 
309  return true;
310 }
311 
312 
313 template<class Matrix, class Vector>
314 const char* Basker<Matrix,Vector>::name = "Basker";
315 
316 
317 } // end namespace Amesos2
318 
319 #endif // AMESOS2_Basker_DEF_HPP
Definition: basker.cpp:35
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
global_size_type globalNumCols_
Number of global columns in matrixA_.
Definition: Amesos2_SolverCore_decl.hpp:479
bool root_
If true, then this is the root processor.
Definition: Amesos2_SolverCore_decl.hpp:507
global_size_type globalNumRows_
Number of global rows in matrixA_.
Definition: Amesos2_SolverCore_decl.hpp:476
Teuchos::Array< slu_type > bvals_
Persisting 1D store for B.
Definition: Amesos2_Basker_decl.hpp:187
bool loadA_impl(EPhase current_phase)
Reads matrix data into internal structures.
Definition: Amesos2_Basker_def.hpp:271
Helper class for getting 1-D copies of multivectors.
Definition: Amesos2_MultiVecAdapter_decl.hpp:243
Definition: Amesos2_TypeDecl.hpp:142
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters_impl() const
Definition: Amesos2_Basker_def.hpp:251
Teuchos::Array< local_ordinal_type > colptr_
Stores the row indices of the nonzero entries.
Definition: Amesos2_Basker_decl.hpp:182
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:575
Teuchos::RCP< const Teuchos::Comm< int > > getComm() const
Returns a pointer to the Teuchos::Comm communicator with this operator.
Definition: Amesos2_SolverCore_decl.hpp:363
Amesos2 interface to the Baker package.
Definition: Amesos2_Basker_decl.hpp:72
Amesos2 Basker declarations.
Teuchos::Array< local_ordinal_type > rowind_
Stores the location in Ai_ and Aval_ that starts row j.
Definition: Amesos2_Basker_decl.hpp:180
Teuchos::Array< slu_type > xvals_
Persisting 1D store for X.
Definition: Amesos2_Basker_decl.hpp:185
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
Teuchos::Array< slu_type > nzvals_
Stores the values of the nonzero entries for Basker.
Definition: Amesos2_Basker_decl.hpp:178
bool matrixShapeOK_impl() const
Determines whether the shape of the matrix is OK for this solver.
Definition: Amesos2_Basker_def.hpp:228
global_size_type globalNumNonZeros_
Number of global non-zero values in matrixA_.
Definition: Amesos2_SolverCore_decl.hpp:482
int preOrdering_impl()
Performs pre-ordering on the matrix to increase efficiency.
Definition: Amesos2_Basker_def.hpp:92
Definition: Amesos2_TypeDecl.hpp:127
Timers timers_
Various timing statistics.
Definition: Amesos2_SolverCore_decl.hpp:498
int numericFactorization_impl()
Basker specific numeric factorization.
Definition: Amesos2_Basker_def.hpp:116
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
Teuchos::RCP< const MatrixAdapter< Matrix > > matrixA_
The LHS operator.
Definition: Amesos2_SolverCore_decl.hpp:455
int solve_impl(const Teuchos::Ptr< MultiVecAdapter< Vector > > X, const Teuchos::Ptr< const MultiVecAdapter< Vector > > B) const
Basker specific solve.
Definition: Amesos2_Basker_def.hpp:164