42 #ifndef ANASAZI_RANDOMIZED_SOLMGR_HPP
43 #define ANASAZI_RANDOMIZED_SOLMGR_HPP
70 #include "Teuchos_FancyOStream.hpp"
90 namespace Experimental {
92 template<
class ScalarType,
class MV,
class OP>
93 class RandomizedSolMgr :
public SolverManager<ScalarType,MV,OP> {
97 typedef MultiVecTraits<ScalarType,MV> MVT;
98 typedef OperatorTraits<ScalarType,MV,OP> OPT;
124 RandomizedSolMgr(
const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
128 virtual ~RandomizedSolMgr() {};
134 const Eigenproblem<ScalarType,MV,OP>& getProblem()
const {
138 int getNumIters()
const {
142 int getNumFailed()
const {
184 template<
class ScalarType,
class MV,
class OP>
185 RandomizedSolMgr<ScalarType,MV,OP>::RandomizedSolMgr(
186 const Teuchos::RCP<Eigenproblem<ScalarType,MV,OP> > &problem,
193 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
194 timerOrtho_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: Randomized::Orthogonalization")),
195 timerSolve_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: Randomized::solve()")),
196 timerOp_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi: Randomized::Operation Op*x")),
208 TEUCHOS_TEST_FOR_EXCEPTION(problem_->getInitVec() == Teuchos::null,std::invalid_argument,
"Problem does not contain initial vectors to clone from.");
210 whch_ = pl.
get(
"Which",whch_);
213 "RandomizedSolMgr: \"Which\" parameter must be LM or LR. Other options not available.");
215 tol_ = pl.
get(
"Convergence Tolerance",tol_);
218 "RandomizedSolMgr: \"Tolerance\" parameter must be strictly positive.");
222 osProc_ = pl.
get(
"Output Processor", osProc_);
226 osp_ = Teuchos::getParameter<Teuchos::RCP<Teuchos::FancyOStream> >(pl,
"Output Stream");
229 osp_ = OutputStreamTraits<OP>::getOutputStream (*problem_->getOperator(), osProc_);
234 if (Teuchos::isParameterType<int>(pl,
"Verbosity")) {
235 verb_ = pl.
get(
"Verbosity", verb_);
237 verb_ = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,
"Verbosity");
242 ortho_ = pl.
get(
"Orthogonalization",
"SVQB");
244 blockSize_= pl.
get(
"Block Size",problem_->getNEV());
247 "RandomizedSolMgr: \"Block Size\" parameter must be strictly positive.");
249 maxIters_ = pl.
get(
"Maximum Iterations",maxIters_);
250 trackResNorms_ = pl.
get(
"Track Residuals",
true);
253 if (pl.
isParameter(
"Orthogonalization Frequency")) {
254 if (Teuchos::isParameterType<int>(pl,
"Orthogonalization Frequency")) {
255 orthoFreq_ = pl.
get(
"Orthogonalization Frequency", orthoFreq_);
257 orthoFreq_ = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,
"Orthogonalization Frequency");
263 if (Teuchos::isParameterType<int>(pl,
"Residual Frequency")) {
264 resFreq_ = pl.
get(
"Residual Frequency", resFreq_);
266 resFreq_ = (int)Teuchos::getParameter<Anasazi::MsgType>(pl,
"Residual Frequency");
272 template<
class ScalarType,
class MV,
class OP>
274 RandomizedSolMgr<ScalarType,MV,OP>::solve() {
278 sorter->setSortType(whch_);
279 std::vector<int> order(blockSize_);
280 SolverUtils<ScalarType,MV,OP> msutils;
286 Eigensolution<ScalarType,MV> sol;
288 problem_->setSolution(sol);
293 if (ortho_==
"SVQB") {
295 }
else if (ortho_==
"DGKS") {
297 }
else if (ortho_==
"ICGS") {
300 TEUCHOS_TEST_FOR_EXCEPTION(ortho_!=
"SVQB"&&ortho_!=
"DGKS"&&ortho_!=
"ICGS",std::logic_error,
"Anasazi::RandomSolver Invalid orthogonalization type.");
303 if(blockSize_ < problem_->getNEV()){
304 printer->stream(
Warnings) <<
"Warning! Block size smaller than number evals. Increasing Block Size to num evals." << std::endl;
305 blockSize_ = problem_->getNEV();
315 "Anasazi::Randomized: eigenproblem did not specify initial vectors to clone from.");
316 if(MVT::GetNumberVecs(*(problem_->getInitVec()))==blockSize_){
317 randVecs = MVT::CloneCopy(*(problem_->getInitVec()));
319 randVecs = MVT::Clone(*(problem_->getInitVec()),blockSize_);
320 MVT::MvRandom(*randVecs);
324 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
327 rank = orthoMgr->normalize(*randVecs);
328 if( rank < blockSize_ ) printer->stream(
Warnings) <<
"Warning! Anasazi::RandomSolver Random vectors did not have full rank!" << std::endl;
341 std::vector<MT> evals_real(blockSize_);
342 std::vector<MT> evals_imag(blockSize_);
349 std::vector<ScalarType> work(1);
350 std::vector<MT> rwork(2*blockSize_);
355 std::vector<Value<ScalarType>> EigenVals(blockSize_);
358 std::vector<MT> normV( blockSize_ );
359 bool converged =
false;
362 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
365 sol.numVecs = blockSize_;
366 sol.Evals.resize(sol.numVecs);
369 for( i = 0; i < maxIters_; i++ ){
370 if (converged ==
true) {
377 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
380 OPT::Apply( *(problem_->getOperator()), *randVecs, *randVecs );
383 if ((orthoFreq_ > 0 && i % orthoFreq_ == 0) || (resFreq_ > 0 && i % resFreq_ == 0)) {
385 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
388 rank = orthoMgr->normalize(*randVecs);
389 if( rank < blockSize_ ) printer->stream(
Warnings) <<
"Warning! Anasazi::RandomSolver Random vectors did not have full rank!" << std::endl;
393 if (resFreq_ > 0 && i % resFreq_ == 0) {
396 OPT::Apply( *(problem_->getOperator()), *randVecs, *TmpVecs );
397 MVT::MvTransMv(ONE, *randVecs, *TmpVecs, H);
400 lapack.
GEEV(
'N',
'V',blockSize_,H.values(),H.stride(),evals_real.data(),evals_imag.data(),vlr, ldv, evects.values(), evects.stride(), &work[0], lwork, &rwork[0], &info);
402 work.resize( lwork );
405 lapack.
GEEV(
'N',
'V',blockSize_,H.values(),H.stride(),evals_real.data(),evals_imag.data(),vlr, ldv, evects.values(), evects.stride(), &work[0], lwork, &rwork[0], &info);
406 if(info != 0) printer->stream(
Warnings) <<
"Warning! Anasazi::RandomSolver GEEV solve possible failure: info = " << info << std::endl;
410 sorter->sort(evals_real,evals_imag,Teuchos::rcpFromRef(order),blockSize_);
411 msutils.permuteVectors(order, evects);
413 for( j = 0; j < blockSize_; j++){
414 EigenVals[j].realpart = evals_real[j];
415 EigenVals[j].imagpart = evals_imag[j];
419 MVT::MvTimesMatAddMv(ONE, *randVecs, evects, 0.0, *TmpVecs);
423 sol.numVecs = blockSize_;
424 sol.Evals = EigenVals;
432 for ( j = 0; j < numev; ++j ) T(j, j) = sol.Evals[j].realpart;
434 Avecs = MVT::Clone(*evecs, numev);
435 OPT::Apply(*(problem_->getOperator()), *evecs, *Avecs);
437 MVT::MvTimesMatAddMv(-ONE, *evecs, T, ONE, *Avecs);
438 MVT::MvNorm(*Avecs, normV);
442 for ( j = 0; j < numev; ++j )
444 if ( SCT::magnitude(sol.Evals[j].realpart) != SCT::zero() ) normV[j] = SCT::magnitude(normV[j]/sol.Evals[j].realpart);
445 if (normV[j] > tol_) {
455 if(converged ==
false)
458 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
461 rank = orthoMgr->normalize(*randVecs);
462 if( rank < blockSize_ ) printer->stream(
Warnings) <<
"Warning! Anasazi::RandomSolver Random vectors did not have full rank!" << std::endl;
465 OPT::Apply( *(problem_->getOperator()), *randVecs, *TmpVecs );
466 MVT::MvTransMv(ONE, *randVecs, *TmpVecs, H);
486 lapack.
GEEV(
'N',
'V',blockSize_,H.values(),H.stride(),evals_real.data(),evals_imag.data(),vlr, ldv, evects.values(), evects.stride(), &work[0], lwork, &rwork[0], &info);
487 if(info != 0) printer->stream(
IterationDetails) <<
"Warning!! Anasazi::RandomSolver GEEV solve possible failure: info = " << info << std::endl;
490 work.resize( lwork );
493 lapack.
GEEV(
'N',
'V',blockSize_,H.values(),H.stride(),evals_real.data(),evals_imag.data(),vlr, ldv, evects.values(), evects.stride(), &work[0], lwork, &rwork[0], &info);
494 if(info != 0) printer->stream(
IterationDetails) <<
"Warning!! Anasazi::RandomSolver GEEV solve possible failure: info = " << info << std::endl;
497 sorter->sort(evals_real,evals_imag,Teuchos::rcpFromRef(order),blockSize_);
498 msutils.permuteVectors(order, evects);
500 for( j = 0; j < blockSize_; j++){
501 EigenVals[j].realpart = evals_real[j];
502 EigenVals[j].imagpart = evals_imag[j];
504 sol.Evals = EigenVals;
507 MVT::MvTimesMatAddMv(ONE,*randVecs,evects, 0.0,*TmpVecs);
511 sol.numVecs = blockSize_;
513 numIters_ = maxIters_;
519 problem_->setSolution(sol);
520 printer->stream(
Debug) <<
"Returning " << sol.numVecs <<
" eigenpairs to eigenproblem." << std::endl;
523 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
Pure virtual base class which describes the basic interface for a solver manager. ...
T & get(const std::string &name, T def_value)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
An implementation of the Anasazi::GenOrthoManager that performs orthogonalization using iterated clas...
Basic implementation of the Anasazi::SortManager class.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using the SVQB iter...
bool isParameter(const std::string &name) const
Abstract class definition for Anasazi Output Managers.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Abstract base class which defines the interface required by an eigensolver and status test class to c...
static void summarize(Ptr< const Comm< int > > comm, std::ostream &out=std::cout, const bool alwaysWriteLocal=false, const bool writeGlobalStats=true, const bool writeZeroTimers=true, const ECounterSetOp setOp=Intersection, const std::string &filter="", const bool ignoreZeroTimers=false)
ReturnType
Enumerated type used to pass back information from a solver manager.
A status test for testing the norm of the eigenvectors residuals.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
Orthogonalization manager based on the SVQB technique described in "A Block Orthogonalization Procedu...
Status test for testing the number of iterations.
Special StatusTest for printing status tests.
void GEEV(const char &JOBVL, const char &JOBVR, const OrdinalType &n, ScalarType *A, const OrdinalType &lda, MagnitudeType *WR, MagnitudeType *WI, ScalarType *VL, const OrdinalType &ldvl, ScalarType *VR, const OrdinalType &ldvr, ScalarType *WORK, const OrdinalType &lwork, MagnitudeType *RWORK, OrdinalType *info) const
Status test for forming logical combinations of other status tests.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using (potentially)...
Types and exceptions used within Anasazi solvers and interfaces.
Abstract class definition for Anasazi output stream.
Basic implementation of the Anasazi::OrthoManager class.
Basic implementation of the Anasazi::OrthoManager class.
Class which provides internal utilities for the Anasazi solvers.