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(x->getGlobalLength() != matrixA_->getGlobalNumCols(),
188 std::invalid_argument,
189 "MultiVector X must have length equal to the number of "
190 "global columns in A");
192 TEUCHOS_TEST_FOR_EXCEPTION(b->getGlobalLength() != matrixA_->getGlobalNumRows(),
193 std::invalid_argument,
194 "MultiVector B must have length equal to the number of "
197 TEUCHOS_TEST_FOR_EXCEPTION(x->getGlobalNumVectors() != b->getGlobalNumVectors(),
198 std::invalid_argument,
199 "X and B MultiVectors must have the same number of vectors");
200 #endif // HAVE_AMESOS2_DEBUG
202 if( !status_.numericFactorizationDone() ){
205 const_cast<type*
>(
this)->numericFactorization();
208 static_cast<const solver_type*
>(
this)->solve_impl(Teuchos::outArg(*x), Teuchos::ptrInArg(*b));
210 status_.last_phase_ = SOLVE;
213 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
217 solve(Teuchos::ptr(X), Teuchos::ptr(B));
220 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
224 #ifdef HAVE_AMESOS2_TIMERS
225 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
228 return( static_cast<solver_type*>(
this)->matrixShapeOK_impl() );
234 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
239 matrixA_ = createConstMatrixAdapter(a);
241 #ifdef HAVE_AMESOS2_DEBUG
242 TEUCHOS_TEST_FOR_EXCEPTION( (keep_phase != CLEAN) &&
243 (globalNumRows_ != matrixA_->getGlobalNumRows() ||
244 globalNumCols_ != matrixA_->getGlobalNumCols()),
245 std::invalid_argument,
246 "Dimensions of new matrix be the same as the old matrix if "
247 "keeping any solver phase" );
250 status_.last_phase_ = keep_phase;
253 switch( status_.last_phase_ ){
255 status_.numPreOrder_ = 0;
257 status_.numSymbolicFact_ = 0;
259 status_.numNumericFact_ = 0;
261 status_.numSolve_ = 0;
267 globalNumNonZeros_ = matrixA_->getGlobalNNZ();
268 globalNumCols_ = matrixA_->getGlobalNumCols();
269 globalNumRows_ = matrixA_->getGlobalNumRows();
273 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
276 const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
278 #ifdef HAVE_AMESOS2_TIMERS
279 Teuchos::TimeMonitor LocalTimer1(timers_.totalTime_);
282 if( parameterList->name() ==
"Amesos2" ){
284 Teuchos::RCP<const Teuchos::ParameterList> valid_params = getValidParameters();
285 parameterList->validateParameters(*valid_params);
288 control_.setControlParameters(parameterList);
292 if( parameterList->isSublist(name()) ){
295 control_.setControlParameters(Teuchos::sublist(parameterList, name()));
297 static_cast<solver_type*
>(
this)->setParameters_impl(Teuchos::sublist(parameterList, name()));
305 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
306 Teuchos::RCP<const Teuchos::ParameterList>
309 #ifdef HAVE_AMESOS2_TIMERS
310 Teuchos::TimeMonitor LocalTimer1( timers_.totalTime_ );
313 using Teuchos::ParameterList;
317 RCP<ParameterList> control_params = rcp(
new ParameterList(
"Amesos2 Control"));
318 control_params->set(
"Transpose",
false,
"Whether to solve with the matrix transpose");
324 RCP<const ParameterList>
325 solver_params =
static_cast<const solver_type*
>(
this)->getValidParameters_impl();
327 Teuchos::rcp_const_cast<ParameterList>(solver_params)->set(
"Transpose",
false,
328 "Whether to solve with the "
331 RCP<ParameterList> amesos2_params = rcp(
new ParameterList(
"Amesos2"));
332 amesos2_params->setParameters(*control_params);
333 amesos2_params->set(name(), *solver_params);
335 return amesos2_params;
339 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
343 std::ostringstream oss;
344 oss << name() <<
" solver interface";
349 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
352 Teuchos::FancyOStream &out,
353 const Teuchos::EVerbosityLevel verbLevel)
const
355 if( matrixA_.is_null() || (rank_ != 0) ){
return; }
358 using Teuchos::VERB_DEFAULT;
359 using Teuchos::VERB_NONE;
360 using Teuchos::VERB_LOW;
361 using Teuchos::VERB_MEDIUM;
362 using Teuchos::VERB_HIGH;
363 using Teuchos::VERB_EXTREME;
364 Teuchos::EVerbosityLevel vl = verbLevel;
365 if (vl == VERB_DEFAULT) vl = VERB_LOW;
366 Teuchos::RCP<const Teuchos::Comm<int> > comm = this->getComm();
368 for(
size_t dec = 10; dec < globalNumRows_; dec *= 10 ) {
371 width = std::max<size_t>(width,11) + 2;
372 Teuchos::OSTab tab(out);
380 if( vl != VERB_NONE ) {
381 std::string p = name();
383 out << this->description() << std::endl << std::endl;
385 out << p <<
"Matrix has " << globalNumRows_ <<
"rows"
386 <<
" and " << globalNumNonZeros_ <<
"nonzeros"
388 if( vl == VERB_MEDIUM || vl == VERB_HIGH || vl == VERB_EXTREME ){
389 out << p <<
"Nonzero elements per row = "
390 << globalNumNonZeros_ / globalNumRows_
392 out << p <<
"Percentage of nonzero elements = "
393 << 100.0 * globalNumNonZeros_ / (globalNumRows_ * globalNumCols_)
396 if( vl == VERB_HIGH || vl == VERB_EXTREME ){
397 out << p <<
"Use transpose = " << control_.useTranspose_
400 if ( vl == VERB_EXTREME ){
408 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
411 Teuchos::FancyOStream &out,
412 const Teuchos::EVerbosityLevel verbLevel)
const
414 if( matrixA_.is_null() || (rank_ != 0) ){
return; }
416 double preTime = timers_.preOrderTime_.totalElapsedTime();
417 double symTime = timers_.symFactTime_.totalElapsedTime();
418 double numTime = timers_.numFactTime_.totalElapsedTime();
419 double solTime = timers_.solveTime_.totalElapsedTime();
420 double totTime = timers_.totalTime_.totalElapsedTime();
421 double overhead = totTime - (preTime + symTime + numTime + solTime);
423 std::string p = name() +
" : ";
426 out << p <<
"Time to convert matrix to implementation format = "
427 << timers_.mtxConvTime_.totalElapsedTime() <<
" (s)"
429 out << p <<
"Time to redistribute matrix = "
430 << timers_.mtxRedistTime_.totalElapsedTime() <<
" (s)"
433 out << p <<
"Time to convert vectors to implementation format = "
434 << timers_.vecConvTime_.totalElapsedTime() <<
" (s)"
436 out << p <<
"Time to redistribute vectors = "
437 << timers_.vecRedistTime_.totalElapsedTime() <<
" (s)"
440 out << p <<
"Number of pre-orderings = "
441 << status_.getNumPreOrder()
443 out << p <<
"Time for pre-ordering = "
444 << preTime <<
" (s), avg = "
445 << preTime / status_.getNumPreOrder() <<
" (s)"
448 out << p <<
"Number of symbolic factorizations = "
449 << status_.getNumSymbolicFact()
451 out << p <<
"Time for sym fact = "
452 << symTime <<
" (s), avg = "
453 << symTime / status_.getNumSymbolicFact() <<
" (s)"
456 out << p <<
"Number of numeric factorizations = "
457 << status_.getNumNumericFact()
459 out << p <<
"Time for num fact = "
460 << numTime <<
" (s), avg = "
461 << numTime / status_.getNumNumericFact() <<
" (s)"
464 out << p <<
"Number of solve phases = "
465 << status_.getNumSolve()
467 out << p <<
"Time for solve = "
468 << solTime <<
" (s), avg = "
469 << solTime / status_.getNumSolve() <<
" (s)"
472 out << p <<
"Total time spent in Amesos2 = "
475 out << p <<
"Total time spent in the Amesos2 interface = "
476 << overhead <<
" (s)"
478 out << p <<
" (the above time does not include solver time)"
480 out << p <<
"Amesos2 interface time / total time = "
481 << overhead / totTime
487 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
490 Teuchos::ParameterList& timingParameterList)
const
494 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
498 std::string solverName = solver_type::name;
502 template <
template <
class,
class>
class ConcreteSolver,
class Matrix,
class Vector >
506 matrix_loaded_ =
static_cast<solver_type*
>(
this)->loadA_impl(current_phase);
512 #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:236
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:410
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Definition: Amesos2_SolverCore_def.hpp:351
Utility functions for Amesos2.
std::string name() const
Return the name of this solver.
Definition: Amesos2_SolverCore_def.hpp:496
bool matrixShapeOK()
Returns true if the solver can handle this matrix shape.
Definition: Amesos2_SolverCore_def.hpp:222
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:307
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:341
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
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:504
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:489
super_type & setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶meterList)
Set/update internal variables and solver options.
Definition: Amesos2_SolverCore_def.hpp:275
super_type & symbolicFactorization()
Performs symbolic factorization on the matrix A.
Definition: Amesos2_SolverCore_def.hpp:117