Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_SolverCore_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_SOLVERCORE_DEF_HPP
54 #define AMESOS2_SOLVERCORE_DEF_HPP
55 
56 #include "Amesos2_MatrixAdapter_def.hpp"
57 #include "Amesos2_MultiVecAdapter_def.hpp"
58 
59 #include "Amesos2_Util.hpp"
60 
61 
62 namespace Amesos2 {
63 
64 
65 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
67  Teuchos::RCP<const Matrix> A,
68  Teuchos::RCP<Vector> X,
69  Teuchos::RCP<const Vector> B )
70  : matrixA_(createConstMatrixAdapter<Matrix>(A))
71  , multiVecX_(X) // may be null
72  , multiVecB_(B) // may be null
73  , globalNumRows_(matrixA_->getGlobalNumRows())
74  , globalNumCols_(matrixA_->getGlobalNumCols())
75  , globalNumNonZeros_(matrixA_->getGlobalNNZ())
76  , rowIndexBase_(matrixA_->getRowIndexBase())
77  , columnIndexBase_(matrixA_->getColumnIndexBase())
78  , rank_(Teuchos::rank(*this->getComm()))
79  , root_(rank_ == 0)
80  , nprocs_(Teuchos::size(*this->getComm()))
81 {
82  TEUCHOS_TEST_FOR_EXCEPTION(
83  !matrixShapeOK(),
84  std::invalid_argument,
85  "Matrix shape inappropriate for this solver");
86 }
87 
88 
90 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
92 {
93  // Nothing
94 }
95 
96 
97 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
100 {
101 #ifdef HAVE_AMESOS2_TIMERS
102  Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
103 #endif
104 
105  loadA(PREORDERING);
106 
107  static_cast<solver_type*>(this)->preOrdering_impl();
108  ++status_.numPreOrder_;
109  status_.last_phase_ = PREORDERING;
110 
111  return *this;
112 }
113 
114 
115 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
118 {
119 #ifdef HAVE_AMESOS2_TIMERS
120  Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
121 #endif
122 
123  if( !status_.preOrderingDone() ){
124  preOrdering();
125  if( !matrix_loaded_ ) loadA(SYMBFACT);
126  } else {
127  loadA(SYMBFACT);
128  }
129 
130  static_cast<solver_type*>(this)->symbolicFactorization_impl();
131  ++status_.numSymbolicFact_;
132  status_.last_phase_ = SYMBFACT;
133 
134  return *this;
135 }
136 
137 
138 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
141 {
142 #ifdef HAVE_AMESOS2_TIMERS
143  Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
144 #endif
145 
146  if( !status_.symbolicFactorizationDone() ){
147  symbolicFactorization();
148  if( !matrix_loaded_ ) loadA(NUMFACT);
149  } else {
150  loadA(NUMFACT);
151  }
152 
153  static_cast<solver_type*>(this)->numericFactorization_impl();
154  ++status_.numNumericFact_;
155  status_.last_phase_ = NUMFACT;
156 
157  return *this;
158 }
159 
160 
161 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
162 void
164 {
165  solve(multiVecX_.ptr(), multiVecB_.ptr());
166 }
167 
168 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
169 void
171  const Teuchos::Ptr<const Vector> B) const
172 {
173 #ifdef HAVE_AMESOS2_TIMERS
174  Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
175 #endif
176 
177  X.assert_not_null();
178  B.assert_not_null();
179 
180  const Teuchos::RCP<MultiVecAdapter<Vector> > x =
181  createMultiVecAdapter<Vector>(Teuchos::rcpFromPtr(X));
182  const Teuchos::RCP<const MultiVecAdapter<Vector> > b =
183  createConstMultiVecAdapter<Vector>(Teuchos::rcpFromPtr(B));
184 
185 #ifdef HAVE_AMESOS2_DEBUG
186  // Check some required properties of X and B
187  TEUCHOS_TEST_FOR_EXCEPTION
188  (x->getGlobalLength() != matrixA_->getGlobalNumCols(),
189  std::invalid_argument,
190  "MultiVector X must have length equal to the number of "
191  "global columns in A. X->getGlobalLength() = "
192  << x->getGlobalLength() << " != A->getGlobalNumCols() = "
193  << matrixA_->getGlobalNumCols() << ".");
194 
195  TEUCHOS_TEST_FOR_EXCEPTION(b->getGlobalLength() != matrixA_->getGlobalNumRows(),
196  std::invalid_argument,
197  "MultiVector B must have length equal to the number of "
198  "global rows in A");
199 
200  TEUCHOS_TEST_FOR_EXCEPTION(x->getGlobalNumVectors() != b->getGlobalNumVectors(),
201  std::invalid_argument,
202  "X and B MultiVectors must have the same number of vectors");
203 #endif // HAVE_AMESOS2_DEBUG
204 
205  if( !status_.numericFactorizationDone() ){
206  // This casting-away of constness is probably OK because this
207  // function is meant to be "logically const"
208  const_cast<type*>(this)->numericFactorization();
209  }
210 
211  static_cast<const solver_type*>(this)->solve_impl(Teuchos::outArg(*x), Teuchos::ptrInArg(*b));
212  ++status_.numSolve_;
213  status_.last_phase_ = SOLVE;
214 }
215 
216 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
217 void
218 SolverCore<ConcreteSolver,Matrix,Vector>::solve(Vector* X, const Vector* B) const
219 {
220  solve(Teuchos::ptr(X), Teuchos::ptr(B));
221 }
222 
223 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
224 bool
226 {
227 #ifdef HAVE_AMESOS2_TIMERS
228  Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
229 #endif
230 
231  return( static_cast<solver_type*>(this)->matrixShapeOK_impl() );
232 }
233 
234  // The RCP should probably be to a const Matrix, but that throws a
235  // wrench in some of the traits mechanisms that aren't expecting
236  // const types
237 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
238 void
239 SolverCore<ConcreteSolver,Matrix,Vector>::setA( const Teuchos::RCP<const Matrix> a,
240  EPhase keep_phase )
241 {
242  matrixA_ = createConstMatrixAdapter(a);
243 
244 #ifdef HAVE_AMESOS2_DEBUG
245  TEUCHOS_TEST_FOR_EXCEPTION( (keep_phase != CLEAN) &&
246  (globalNumRows_ != matrixA_->getGlobalNumRows() ||
247  globalNumCols_ != matrixA_->getGlobalNumCols()),
248  std::invalid_argument,
249  "Dimensions of new matrix be the same as the old matrix if "
250  "keeping any solver phase" );
251 #endif
252 
253  status_.last_phase_ = keep_phase;
254 
255  // Reset phase counters
256  switch( status_.last_phase_ ){
257  case CLEAN:
258  status_.numPreOrder_ = 0;
259  case PREORDERING:
260  status_.numSymbolicFact_ = 0;
261  case SYMBFACT:
262  status_.numNumericFact_ = 0;
263  case NUMFACT: // probably won't ever happen by itself
264  status_.numSolve_ = 0;
265  case SOLVE: // probably won't ever happen
266  break;
267  }
268 
269  // Re-get the matrix dimensions in case they have changed
270  globalNumNonZeros_ = matrixA_->getGlobalNNZ();
271  globalNumCols_ = matrixA_->getGlobalNumCols();
272  globalNumRows_ = matrixA_->getGlobalNumRows();
273 }
274 
275 
276 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
279  const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
280 {
281 #ifdef HAVE_AMESOS2_TIMERS
282  Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
283 #endif
284 
285  if( parameterList->name() == "Amesos2" ){
286  // First, validate the parameterList
287  Teuchos::RCP<const Teuchos::ParameterList> valid_params = getValidParameters();
288  parameterList->validateParameters(*valid_params);
289 
290  // Do everything here that is for generic status and control parameters
291  control_.setControlParameters(parameterList);
292 
293  // Finally, hook to the implementation's parameter list parser
294  // First check if there is a dedicated sublist for this solver and use that if there is
295  if( parameterList->isSublist(name()) ){
296  // Have control look through the solver's parameter list to see if
297  // there is anything it recognizes (mostly the "Transpose" parameter)
298  control_.setControlParameters(Teuchos::sublist(parameterList, name()));
299 
300  static_cast<solver_type*>(this)->setParameters_impl(Teuchos::sublist(parameterList, name()));
301  }
302  }
303 
304  return *this;
305 }
306 
307 
308 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
309 Teuchos::RCP<const Teuchos::ParameterList>
311 {
312 #ifdef HAVE_AMESOS2_TIMERS
313  Teuchos::TimeMonitor LocalTimer1( timers_.totalTime_ );
314 #endif
315 
316  using Teuchos::ParameterList;
317  using Teuchos::RCP;
318  using Teuchos::rcp;
319 
320  RCP<ParameterList> control_params = rcp(new ParameterList("Amesos2 Control"));
321  control_params->set("Transpose", false, "Whether to solve with the matrix transpose");
322  // control_params->set("AddToDiag", "");
323  // control_params->set("AddZeroToDiag", false);
324  // control_params->set("MatrixProperty", "general");
325  // control_params->set("Reindex", false);
326 
327  RCP<const ParameterList>
328  solver_params = static_cast<const solver_type*>(this)->getValidParameters_impl();
329  // inject the "Transpose" parameter into the solver's valid parameters
330  Teuchos::rcp_const_cast<ParameterList>(solver_params)->set("Transpose", false,
331  "Whether to solve with the "
332  "matrix transpose");
333 
334  RCP<ParameterList> amesos2_params = rcp(new ParameterList("Amesos2"));
335  amesos2_params->setParameters(*control_params);
336  amesos2_params->set(name(), *solver_params);
337 
338  return amesos2_params;
339 }
340 
341 
342 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
343 std::string
345 {
346  std::ostringstream oss;
347  oss << name() << " solver interface";
348  return oss.str();
349 }
350 
351 
352 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
353 void
355  Teuchos::FancyOStream &out,
356  const Teuchos::EVerbosityLevel verbLevel) const
357 {
358  if( matrixA_.is_null() || (rank_ != 0) ){ return; }
359  using std::endl;
360  using std::setw;
361  using Teuchos::VERB_DEFAULT;
362  using Teuchos::VERB_NONE;
363  using Teuchos::VERB_LOW;
364  using Teuchos::VERB_MEDIUM;
365  using Teuchos::VERB_HIGH;
366  using Teuchos::VERB_EXTREME;
367  Teuchos::EVerbosityLevel vl = verbLevel;
368  if (vl == VERB_DEFAULT) vl = VERB_LOW;
369  Teuchos::RCP<const Teuchos::Comm<int> > comm = this->getComm();
370  size_t width = 1;
371  for( size_t dec = 10; dec < globalNumRows_; dec *= 10 ) {
372  ++width;
373  }
374  width = std::max<size_t>(width,size_t(11)) + 2;
375  Teuchos::OSTab tab(out);
376  // none: print nothing
377  // low: print O(1) info from node 0
378  // medium: print O(P) info, num entries per node
379  // high: print O(N) info, num entries per row
380  // extreme: print O(NNZ) info: print indices and values
381  //
382  // for medium and higher, print constituent objects at specified verbLevel
383  if( vl != VERB_NONE ) {
384  std::string p = name();
385  Util::printLine(out);
386  out << this->description() << std::endl << std::endl;
387 
388  out << p << "Matrix has " << globalNumRows_ << "rows"
389  << " and " << globalNumNonZeros_ << "nonzeros"
390  << std::endl;
391  if( vl == VERB_MEDIUM || vl == VERB_HIGH || vl == VERB_EXTREME ){
392  out << p << "Nonzero elements per row = "
393  << globalNumNonZeros_ / globalNumRows_
394  << std::endl;
395  out << p << "Percentage of nonzero elements = "
396  << 100.0 * globalNumNonZeros_ / (globalNumRows_ * globalNumCols_)
397  << std::endl;
398  }
399  if( vl == VERB_HIGH || vl == VERB_EXTREME ){
400  out << p << "Use transpose = " << control_.useTranspose_
401  << std::endl;
402  }
403  if ( vl == VERB_EXTREME ){
404  printTiming(out,vl);
405  }
406  Util::printLine(out);
407  }
408 }
409 
410 
411 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
412 void
414  Teuchos::FancyOStream &out,
415  const Teuchos::EVerbosityLevel verbLevel) const
416 {
417  if( matrixA_.is_null() || (rank_ != 0) ){ return; }
418 
419  double preTime = timers_.preOrderTime_.totalElapsedTime();
420  double symTime = timers_.symFactTime_.totalElapsedTime();
421  double numTime = timers_.numFactTime_.totalElapsedTime();
422  double solTime = timers_.solveTime_.totalElapsedTime();
423  double totTime = timers_.totalTime_.totalElapsedTime();
424  double overhead = totTime - (preTime + symTime + numTime + solTime);
425 
426  std::string p = name() + " : ";
427  Util::printLine(out);
428 
429  if(verbLevel != Teuchos::VERB_NONE)
430  {
431  out << p << "Time to convert matrix to implementation format = "
432  << timers_.mtxConvTime_.totalElapsedTime() << " (s)"
433  << std::endl;
434  out << p << "Time to redistribute matrix = "
435  << timers_.mtxRedistTime_.totalElapsedTime() << " (s)"
436  << std::endl;
437 
438  out << p << "Time to convert vectors to implementation format = "
439  << timers_.vecConvTime_.totalElapsedTime() << " (s)"
440  << std::endl;
441  out << p << "Time to redistribute vectors = "
442  << timers_.vecRedistTime_.totalElapsedTime() << " (s)"
443  << std::endl;
444 
445  out << p << "Number of pre-orderings = "
446  << status_.getNumPreOrder()
447  << std::endl;
448  out << p << "Time for pre-ordering = "
449  << preTime << " (s), avg = "
450  << preTime / status_.getNumPreOrder() << " (s)"
451  << std::endl;
452 
453  out << p << "Number of symbolic factorizations = "
454  << status_.getNumSymbolicFact()
455  << std::endl;
456  out << p << "Time for sym fact = "
457  << symTime << " (s), avg = "
458  << symTime / status_.getNumSymbolicFact() << " (s)"
459  << std::endl;
460 
461  out << p << "Number of numeric factorizations = "
462  << status_.getNumNumericFact()
463  << std::endl;
464  out << p << "Time for num fact = "
465  << numTime << " (s), avg = "
466  << numTime / status_.getNumNumericFact() << " (s)"
467  << std::endl;
468 
469  out << p << "Number of solve phases = "
470  << status_.getNumSolve()
471  << std::endl;
472  out << p << "Time for solve = "
473  << solTime << " (s), avg = "
474  << solTime / status_.getNumSolve() << " (s)"
475  << std::endl;
476 
477  out << p << "Total time spent in Amesos2 = "
478  << totTime << " (s)"
479  << std::endl;
480  out << p << "Total time spent in the Amesos2 interface = "
481  << overhead << " (s)"
482  << std::endl;
483  out << p << " (the above time does not include solver time)"
484  << std::endl;
485  out << p << "Amesos2 interface time / total time = "
486  << overhead / totTime
487  << std::endl;
488  Util::printLine(out);
489  }
490 }
491 
492 
493 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
494 void
496  Teuchos::ParameterList& timingParameterList) const
497 {
498  Teuchos::ParameterList temp;
499  timingParameterList = temp.setName("NULL");
500 }
501 
502 
503 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
504 std::string
506 {
507  std::string solverName = solver_type::name;
508  return solverName;
509 }
510 
511 template <template <class,class> class ConcreteSolver, class Matrix, class Vector >
512 void
514 {
515  matrix_loaded_ = static_cast<solver_type*>(this)->loadA_impl(current_phase);
516 }
517 
518 
519 } // end namespace Amesos2
520 
521 #endif // AMESOS2_SOLVERCORE_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
Utility functions for Amesos2.
SolverCore(Teuchos::RCP< const Matrix > A, Teuchos::RCP< Vector > X, Teuchos::RCP< const Vector > B)
Initialize a Solver instance.
Definition: Amesos2_SolverCore_def.hpp:66
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
void printLine(Teuchos::FancyOStream &out)
Prints a line of 70 "-"s on std::cout.
Definition: Amesos2_Util.cpp:123
Amesos2 interface to the PardisoMKL package.
Definition: Amesos2_PardisoMKL_decl.hpp:83
Interface to Amesos2 solver objects.
Definition: Amesos2_Solver_decl.hpp:78
Definition: Amesos2_Cholmod_TypeMap.hpp:92