53 #ifndef AMESOS2_SOLVERCORE_DEF_HPP
54 #define AMESOS2_SOLVERCORE_DEF_HPP
56 #include "Amesos2_MatrixAdapter_def.hpp"
57 #include "Amesos2_MultiVecAdapter_def.hpp"
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))
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()))
80 , nprocs_(Teuchos::size(*this->getComm()))
82 TEUCHOS_TEST_FOR_EXCEPTION(
84 std::invalid_argument,
85 "Matrix shape inappropriate for this solver");
90 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
97 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
101 #ifdef HAVE_AMESOS2_TIMERS
102 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
107 static_cast<solver_type*
>(
this)->preOrdering_impl();
108 ++status_.numPreOrder_;
109 status_.last_phase_ = PREORDERING;
115 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
119 #ifdef HAVE_AMESOS2_TIMERS
120 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
123 if( !status_.preOrderingDone() ){
125 if( !matrix_loaded_ ) loadA(SYMBFACT);
130 static_cast<solver_type*
>(
this)->symbolicFactorization_impl();
131 ++status_.numSymbolicFact_;
132 status_.last_phase_ = SYMBFACT;
138 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
142 #ifdef HAVE_AMESOS2_TIMERS
143 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
146 if( !status_.symbolicFactorizationDone() ){
147 symbolicFactorization();
148 if( !matrix_loaded_ ) loadA(NUMFACT);
153 static_cast<solver_type*
>(
this)->numericFactorization_impl();
154 ++status_.numNumericFact_;
155 status_.last_phase_ = NUMFACT;
161 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
165 solve(multiVecX_.ptr(), multiVecB_.ptr());
168 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
171 const Teuchos::Ptr<const Vector> B)
const
173 #ifdef HAVE_AMESOS2_TIMERS
174 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
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));
185 #ifdef HAVE_AMESOS2_DEBUG
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() <<
".");
195 TEUCHOS_TEST_FOR_EXCEPTION(b->getGlobalLength() != matrixA_->getGlobalNumRows(),
196 std::invalid_argument,
197 "MultiVector B must have length equal to the number of "
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
205 if( !status_.numericFactorizationDone() ){
211 static_cast<const solver_type*
>(
this)->solve_impl(Teuchos::outArg(*x), Teuchos::ptrInArg(*b));
213 status_.last_phase_ = SOLVE;
216 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
220 solve(Teuchos::ptr(X), Teuchos::ptr(B));
223 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
227 #ifdef HAVE_AMESOS2_TIMERS
228 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
231 return( static_cast<solver_type*>(
this)->matrixShapeOK_impl() );
237 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
242 matrixA_ = createConstMatrixAdapter(a);
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" );
253 status_.last_phase_ = keep_phase;
256 switch( status_.last_phase_ ){
258 status_.numPreOrder_ = 0;
261 status_.numSymbolicFact_ = 0;
264 status_.numNumericFact_ = 0;
267 status_.numSolve_ = 0;
274 globalNumNonZeros_ = matrixA_->getGlobalNNZ();
275 globalNumCols_ = matrixA_->getGlobalNumCols();
276 globalNumRows_ = matrixA_->getGlobalNumRows();
280 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
283 const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
285 #ifdef HAVE_AMESOS2_TIMERS
286 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
289 if( parameterList->name() ==
"Amesos2" ){
291 Teuchos::RCP<const Teuchos::ParameterList> valid_params = getValidParameters();
292 parameterList->validateParameters(*valid_params);
295 control_.setControlParameters(parameterList);
299 if( parameterList->isSublist(name()) ){
302 control_.setControlParameters(Teuchos::sublist(parameterList, name()));
304 static_cast<solver_type*
>(
this)->setParameters_impl(Teuchos::sublist(parameterList, name()));
312 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
313 Teuchos::RCP<const Teuchos::ParameterList>
316 #ifdef HAVE_AMESOS2_TIMERS
317 Teuchos::TimeMonitor LocalTimer1( timers_.totalTime_ );
320 using Teuchos::ParameterList;
324 RCP<ParameterList> control_params = rcp(
new ParameterList(
"Amesos2 Control"));
325 control_params->set(
"Transpose",
false,
"Whether to solve with the matrix transpose");
331 RCP<const ParameterList>
332 solver_params =
static_cast<const solver_type*
>(
this)->getValidParameters_impl();
334 Teuchos::rcp_const_cast<ParameterList>(solver_params)->set(
"Transpose",
false,
335 "Whether to solve with the "
338 RCP<ParameterList> amesos2_params = rcp(
new ParameterList(
"Amesos2"));
339 amesos2_params->setParameters(*control_params);
340 amesos2_params->set(name(), *solver_params);
342 return amesos2_params;
346 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
350 std::ostringstream oss;
351 oss << name() <<
" solver interface";
356 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
359 Teuchos::FancyOStream &out,
360 const Teuchos::EVerbosityLevel verbLevel)
const
362 if( matrixA_.is_null() || (rank_ != 0) ){
return; }
365 using Teuchos::VERB_DEFAULT;
366 using Teuchos::VERB_NONE;
367 using Teuchos::VERB_LOW;
368 using Teuchos::VERB_MEDIUM;
369 using Teuchos::VERB_HIGH;
370 using Teuchos::VERB_EXTREME;
371 Teuchos::EVerbosityLevel vl = verbLevel;
372 if (vl == VERB_DEFAULT) vl = VERB_LOW;
373 Teuchos::RCP<const Teuchos::Comm<int> > comm = this->getComm();
375 for(
size_t dec = 10; dec < globalNumRows_; dec *= 10 ) {
378 width = std::max<size_t>(width,size_t(11)) + 2;
379 Teuchos::OSTab tab(out);
387 if( vl != VERB_NONE ) {
388 std::string p = name();
390 out << this->description() << std::endl << std::endl;
392 out << p <<
"Matrix has " << globalNumRows_ <<
"rows"
393 <<
" and " << globalNumNonZeros_ <<
"nonzeros"
395 if( vl == VERB_MEDIUM || vl == VERB_HIGH || vl == VERB_EXTREME ){
396 out << p <<
"Nonzero elements per row = "
397 << globalNumNonZeros_ / globalNumRows_
399 out << p <<
"Percentage of nonzero elements = "
400 << 100.0 * globalNumNonZeros_ / (globalNumRows_ * globalNumCols_)
403 if( vl == VERB_HIGH || vl == VERB_EXTREME ){
404 out << p <<
"Use transpose = " << control_.useTranspose_
407 if ( vl == VERB_EXTREME ){
415 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
418 Teuchos::FancyOStream &out,
419 const Teuchos::EVerbosityLevel verbLevel)
const
421 if( matrixA_.is_null() || (rank_ != 0) ){
return; }
423 double preTime = timers_.preOrderTime_.totalElapsedTime();
424 double symTime = timers_.symFactTime_.totalElapsedTime();
425 double numTime = timers_.numFactTime_.totalElapsedTime();
426 double solTime = timers_.solveTime_.totalElapsedTime();
427 double totTime = timers_.totalTime_.totalElapsedTime();
428 double overhead = totTime - (preTime + symTime + numTime + solTime);
430 std::string p = name() +
" : ";
433 if(verbLevel != Teuchos::VERB_NONE)
435 out << p <<
"Time to convert matrix to implementation format = "
436 << timers_.mtxConvTime_.totalElapsedTime() <<
" (s)"
438 out << p <<
"Time to redistribute matrix = "
439 << timers_.mtxRedistTime_.totalElapsedTime() <<
" (s)"
442 out << p <<
"Time to convert vectors to implementation format = "
443 << timers_.vecConvTime_.totalElapsedTime() <<
" (s)"
445 out << p <<
"Time to redistribute vectors = "
446 << timers_.vecRedistTime_.totalElapsedTime() <<
" (s)"
449 out << p <<
"Number of pre-orderings = "
450 << status_.getNumPreOrder()
452 out << p <<
"Time for pre-ordering = "
453 << preTime <<
" (s), avg = "
454 << preTime / status_.getNumPreOrder() <<
" (s)"
457 out << p <<
"Number of symbolic factorizations = "
458 << status_.getNumSymbolicFact()
460 out << p <<
"Time for sym fact = "
461 << symTime <<
" (s), avg = "
462 << symTime / status_.getNumSymbolicFact() <<
" (s)"
465 out << p <<
"Number of numeric factorizations = "
466 << status_.getNumNumericFact()
468 out << p <<
"Time for num fact = "
469 << numTime <<
" (s), avg = "
470 << numTime / status_.getNumNumericFact() <<
" (s)"
473 out << p <<
"Number of solve phases = "
474 << status_.getNumSolve()
476 out << p <<
"Time for solve = "
477 << solTime <<
" (s), avg = "
478 << solTime / status_.getNumSolve() <<
" (s)"
481 out << p <<
"Total time spent in Amesos2 = "
484 out << p <<
"Total time spent in the Amesos2 interface = "
485 << overhead <<
" (s)"
487 out << p <<
" (the above time does not include solver time)"
489 out << p <<
"Amesos2 interface time / total time = "
490 << overhead / totTime
497 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
500 Teuchos::ParameterList& timingParameterList)
const
502 Teuchos::ParameterList temp;
503 timingParameterList = temp.setName(
"NULL");
507 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
511 std::string solverName = solver_type::name;
515 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
519 matrix_loaded_ =
static_cast<solver_type*
>(
this)->loadA_impl(current_phase);
525 #endif // AMESOS2_SOLVERCORE_DEF_HPP
~SolverCore()
Destructor.
Definition: Amesos2_SolverCore_def.hpp:91
void setA(const Teuchos::RCP< const Matrix > a, EPhase keep_phase=CLEAN)
Sets the matrix A of this solver.
Definition: Amesos2_SolverCore_def.hpp:239
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:65
void printTiming(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Prints timing information about the current solver.
Definition: Amesos2_SolverCore_def.hpp:417
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Definition: Amesos2_SolverCore_def.hpp:358
Utility functions for Amesos2.
std::string name() const
Return the name of this solver.
Definition: Amesos2_SolverCore_def.hpp:509
bool matrixShapeOK()
Returns true if the solver can handle this matrix shape.
Definition: Amesos2_SolverCore_def.hpp:225
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Return a const parameter list of all of the valid parameters that this->setParameterList(...) will accept.
Definition: Amesos2_SolverCore_def.hpp:314
super_type & preOrdering()
Pre-orders the matrix A for minimal fill-in.
Definition: Amesos2_SolverCore_def.hpp:99
std::string description() const
Returns a short description of this Solver.
Definition: Amesos2_SolverCore_def.hpp:348
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
virtual type & numericFactorization(void)=0
Performs numeric factorization on the matrix.
void printLine(Teuchos::FancyOStream &out)
Prints a line of 70 "-"s on std::cout.
Definition: Amesos2_Util.cpp:119
void solve()
Solves (or )
Definition: Amesos2_SolverCore_def.hpp:163
super_type & numericFactorization()
Performs numeric factorization on the matrix A.
Definition: Amesos2_SolverCore_def.hpp:140
void loadA(EPhase current_phase)
Refresh this solver's internal data about A.
Definition: Amesos2_SolverCore_def.hpp:517
Interface to Amesos2 solver objects.
Definition: Amesos2_Solver_decl.hpp:78
void getTiming(Teuchos::ParameterList &timingParameterList) const
Extracts timing information from the current solver.
Definition: Amesos2_SolverCore_def.hpp:499
super_type & setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶meterList)
Set/update internal variables and solver options.
Definition: Amesos2_SolverCore_def.hpp:282
super_type & symbolicFactorization()
Performs symbolic factorization on the matrix A.
Definition: Amesos2_SolverCore_def.hpp:117