10#ifndef BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_ITER_HPP
11#define BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_ITER_HPP
28#include "Teuchos_SerialDenseVector.hpp"
29#include "Teuchos_SerialDenseHelpers.hpp"
30#include "Teuchos_ScalarTraits.hpp"
31#include "Teuchos_ParameterList.hpp"
32#include "Teuchos_TimeMonitor.hpp"
51 template<
class ScalarType,
class MV,
class OP,
class DM>
62 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
76 Teuchos::ParameterList &
params );
187 "Belos::PseudoBlockStochasticCGIter::setBlockSize(): Cannot use a block size that is not one.");
198 inline Teuchos::SerialDenseVector<int,ScalarType>& normal() {
202 const double p0 = -0.322232431088;
203 const double p1 = -1.0;
204 const double p2 = -0.342242088547;
205 const double p3 = -0.204231210245e-1;
206 const double p4 = -0.453642210148e-4;
207 const double q0 = 0.993484626060e-1;
208 const double q1 = 0.588581570495;
209 const double q2 = 0.531103462366;
210 const double q3 = 0.103537752850;
211 const double q4 = 0.38560700634e-2;
215 Teuchos::randomSyncedMatrix( randvec_ );
217 for (
int i=0;
i<numRHS_;
i++)
220 r=0.5*SCT::real(randvec_[
i]) + 1.0;
223 if(r < 0.5) y=std::sqrt(-2.0 *
log(r));
224 else y=std::sqrt(-2.0 *
log(1.0 - r));
229 if(r < 0.5) z = (
p /
q) - y;
230 else z = y - (
p /
q);
232 randvec_[
i] = Teuchos::as<ScalarType,double>(z);
241 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP,DM> > lp_;
242 const Teuchos::RCP<OutputManager<ScalarType> > om_;
243 const Teuchos::RCP<StatusTest<ScalarType,MV,OP,DM> > stest_;
263 bool assertPositiveDefiniteness_;
278 Teuchos::RCP<MV> AP_;
284 Teuchos::SerialDenseVector<int,ScalarType> randvec_;
290 template<
class ScalarType,
class MV,
class OP,
class DM>
294 Teuchos::ParameterList &
params ):
301 assertPositiveDefiniteness_(
params.
get(
"Assert Positive Definiteness",
true) )
308 template<
class ScalarType,
class MV,
class OP,
class DM>
312 Teuchos::RCP<const MV>
lhsMV = lp_->getCurrLHSVec();
313 Teuchos::RCP<const MV>
rhsMV = lp_->getCurrRHSVec();
315 "Belos::PseudoBlockStochasticCGIter::initialize(): Cannot initialize state storage!");
326 if (Teuchos::is_null(R_) || MVT::GetNumberVecs(*R_)!=numRHS_) {
327 R_ = MVT::Clone( *
tmp, numRHS_ );
328 Z_ = MVT::Clone( *
tmp, numRHS_ );
329 P_ = MVT::Clone( *
tmp, numRHS_ );
330 AP_ = MVT::Clone( *
tmp, numRHS_ );
331 Y_ = MVT::Clone( *
tmp, numRHS_ );
335 randvec_.size( numRHS_ );
339 std::string
errstr(
"Belos::BlockPseudoStochasticCGIter::initialize(): Specified multivectors must have a consistent length and width.");
341 if (!Teuchos::is_null(
newstate.R)) {
344 std::invalid_argument,
errstr );
346 std::invalid_argument,
errstr );
357 if ( lp_->getLeftPrec() != Teuchos::null ) {
358 lp_->applyLeftPrec( *R_, *Z_ );
359 if ( lp_->getRightPrec() != Teuchos::null ) {
360 Teuchos::RCP<MV>
tmp2 = MVT::Clone( *Z_, numRHS_ );
361 lp_->applyRightPrec( *Z_, *
tmp2 );
365 else if ( lp_->getRightPrec() != Teuchos::null ) {
366 lp_->applyRightPrec( *R_, *Z_ );
371 MVT::Assign( *Z_, *P_ );
376 "Belos::StochasticCGIter::initialize(): CGStateIterState does not have initial residual.");
386 template<
class ScalarType,
class MV,
class OP,
class DM>
392 if (initialized_ ==
false) {
398 std::vector<int> index(1);
399 std::vector<ScalarType>
rHz( numRHS_ ),
rHz_old( numRHS_ ),
pAp( numRHS_ ), beta( numRHS_ );
400 Teuchos::RCP<DM> alpha = DMT::Create( numRHS_,numRHS_ );
401 Teuchos::RCP<DM>
zeta = DMT::Create( numRHS_,numRHS_ );
404 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
411 MVT::MvDot( *R_, *Z_,
rHz );
413 if ( assertPositiveDefiniteness_ )
414 for (
i=0;
i<numRHS_; ++
i)
417 "Belos::PseudoBlockStochasticCGIter::iterate(): negative value for r^H*M*r encountered!" );
422 while (stest_->checkStatus(
this) !=
Passed) {
428 lp_->applyOp( *P_, *AP_ );
431 MVT::MvDot( *P_, *AP_,
pAp );
433 Teuchos::SerialDenseVector<int,ScalarType>& z = normal();
435 for (
i=0;
i<numRHS_; ++
i) {
436 if ( assertPositiveDefiniteness_ )
440 "Belos::PseudoBlockStochasticCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
445 DMT::Value(*
zeta,
i,
i) = z[
i] / Teuchos::ScalarTraits<ScalarType>::squareroot(
pAp[
i]);
447 DMT::SyncDeviceToHost( *alpha );
448 DMT::SyncDeviceToHost( *
zeta );
454 lp_->updateSolution();
457 MVT::MvTimesMatAddMv(
one, *P_, *
zeta,
one, *Y_);
462 for (
i=0;
i<numRHS_; ++
i) {
468 MVT::MvTimesMatAddMv( -
one, *AP_, *alpha,
one, *R_ );
473 if ( lp_->getLeftPrec() != Teuchos::null ) {
474 lp_->applyLeftPrec( *R_, *Z_ );
475 if ( lp_->getRightPrec() != Teuchos::null ) {
476 Teuchos::RCP<MV>
tmp = MVT::Clone( *Z_, numRHS_ );
477 lp_->applyRightPrec( *Z_, *
tmp );
481 else if ( lp_->getRightPrec() != Teuchos::null ) {
482 lp_->applyRightPrec( *R_, *Z_ );
488 MVT::MvDot( *R_, *Z_,
rHz );
489 if ( assertPositiveDefiniteness_ )
490 for (
i=0;
i<numRHS_; ++
i)
493 "Belos::PseudoBlockStochasticCGIter::iterate(): negative value for r^H*M*r encountered!" );
496 for (
i=0;
i<numRHS_; ++
i) {
499 Teuchos::RCP<const MV>
Z_i = MVT::CloneView( *Z_, index );
500 Teuchos::RCP<MV>
P_i = MVT::CloneViewNonConst( *P_, index );
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class for defining the status testing capabilities of Belos.
Pure virtual base class which augments the basic interface for a stochastic conjugate gradient linear...
Collection of types and exceptions used within the Belos solvers.
CGPositiveDefiniteFailure is thrown when the the CG 'alpha = p^H*A*P' value is less than zero,...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).
This class implements the stochastic pseudo-block CG iteration, where the basic stochastic CG algorit...
Teuchos::ScalarTraits< ScalarType > SCT
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
void resetNumIters(int iter=0)
Reset the iteration count.
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
void setBlockSize(int blockSize)
Set the blocksize.
void iterate()
This method performs stochastic CG iterations on each linear system until the status test indicates t...
bool isInitialized()
States whether the solver has been initialized or not.
OperatorTraits< ScalarType, MV, OP > OPT
StochasticCGIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
Get the norms of the residuals native to the solver.
MultiVecTraits< ScalarType, MV, DM > MVT
DenseMatTraits< ScalarType, DM > DMT
virtual ~PseudoBlockStochasticCGIter()
Destructor.
Teuchos::RCP< MV > getStochasticVector() const
Get the stochastic vector
SCT::magnitudeType MagnitudeType
int getNumIters() const
Get the current iteration count.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
void initializeCG(StochasticCGIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
PseudoBlockStochasticCGIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, Teuchos::ParameterList ¶ms)
PseudoBlockStochasticCGIter constructor with linear problem, solver utilities, and parameter list of ...