52 #ifndef BELOS_THYRA_ADAPTER_HPP
53 #define BELOS_THYRA_ADAPTER_HPP
55 #include "Stratimikos_Config.h"
56 #include "BelosConfigDefs.hpp"
57 #include "BelosMultiVecTraits.hpp"
58 #include "BelosOperatorTraits.hpp"
60 #include <Thyra_DetachedMultiVectorView.hpp>
61 #include <Thyra_MultiVectorBase.hpp>
62 #include <Thyra_MultiVectorStdOps.hpp>
63 #ifdef HAVE_BELOS_TSQR
65 #endif // HAVE_BELOS_TSQR
67 #ifdef HAVE_STRATIMIKOS_BELOS_TIMERS
70 # define STRATIMIKOS_TIME_MONITOR(NAME) \
71 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(NAME)))
75 # define STRATIMIKOS_TIME_MONITOR(NAME)
93 template<
class ScalarType>
94 class MultiVecTraits< ScalarType, Thyra::MultiVectorBase<ScalarType> >
97 typedef Thyra::MultiVectorBase<ScalarType>
TMVB;
122 int numvecs = mv.domain()->dim();
126 Thyra::assign(cc.
ptr(), mv);
137 int numvecs = index.size();
143 Thyra::assign(cc.
ptr(), *view);
150 const int numVecs = index.
size();
156 Thyra::assign (cc.
ptr(), *view);
167 int numvecs = index.size();
183 for (
int i=0; i<numvecs; i++) {
184 if (lb+i != index[i]) contig =
false;
189 const Thyra::Range1D rng(lb,lb+numvecs-1);
191 cc = mv.subView(rng);
195 cc = mv.subView(index);
208 return mv.subView (index);
219 int numvecs = index.size();
235 for (
int i=0; i<numvecs; i++) {
236 if (lb+i != index[i]) contig =
false;
241 const Thyra::Range1D rng(lb,lb+numvecs-1);
243 cc = mv.subView(rng);
247 cc = mv.subView(index);
260 return mv.subView (index);
270 return Teuchos::as<ptrdiff_t>(mv.range()->dim());
275 {
return mv.domain()->dim(); }
286 const ScalarType beta,
TMVB& mv )
288 using Teuchos::arrayView;
using Teuchos::arcpFromArrayView;
294 if ((m == 1) && (n == 1)) {
295 using Teuchos::tuple;
using Teuchos::ptrInArg;
using Teuchos::inoutArg;
296 const ScalarType alphaNew = alpha * B(0, 0);
297 Thyra::linear_combination<ScalarType>(tuple(alphaNew)(), tuple(ptrInArg(A))(), beta, inoutArg(mv));
300 auto vs = A.domain();
303 B_thyra = vs->createCachedMembersView(
309 Thyra::apply<ScalarType>(A, Thyra::NOTRANS, *B_thyra, Teuchos::outArg(mv), alpha, beta);
316 const ScalarType beta,
const TMVB&
B,
TMVB& mv )
318 using Teuchos::tuple;
using Teuchos::ptrInArg;
using Teuchos::inoutArg;
321 Thyra::linear_combination<ScalarType>(
331 Thyra::scale(alpha, Teuchos::inoutArg(mv));
336 static void MvScale (
TMVB& mv,
const std::vector<ScalarType>& alpha)
340 for (
unsigned int i=0; i<alpha.size(); i++) {
341 Thyra::scale<ScalarType> (alpha[i], mv.col(i).ptr());
350 using Teuchos::arrayView;
using Teuchos::arcpFromArrayView;
354 int m = A.domain()->dim();
355 int n = mv.domain()->dim();
356 auto vs = A.domain();
359 B_thyra = vs->createCachedMembersView(
366 Thyra::apply<ScalarType>(A, Thyra::CONJTRANS, mv, B_thyra.
ptr(), alpha);
376 Thyra::dots(mv, A, Teuchos::arrayViewFromVector(b));
387 static void MvNorm(
const TMVB& mv, std::vector<magType>& normvec,
388 NormType type = TwoNorm ) {
392 Thyra::norms_2(mv, Teuchos::arrayViewFromVector(normvec));
393 else if(type == OneNorm)
394 Thyra::norms_1(mv, Teuchos::arrayViewFromVector(normvec));
395 else if(type == InfNorm)
396 Thyra::norms_inf(mv, Teuchos::arrayViewFromVector(normvec));
399 "Belos::MultiVecTraits::MvNorm (Thyra specialization): "
400 "invalid norm type. Must be either TwoNorm, OneNorm or InfNorm");
413 int numvecs = index.size();
414 std::vector<int> indexA(numvecs);
415 int numAcols = A.domain()->dim();
416 for (
int i=0; i<numvecs; i++) {
421 if ( numAcols < numvecs ) {
426 else if ( numAcols > numvecs ) {
428 indexA.resize( numAcols );
435 Thyra::assign(reldest.
ptr(), *relsource);
441 const int numColsA = A.domain()->dim();
442 const int numColsMv = mv.domain()->dim();
444 const bool validIndex = index.
lbound() >= 0 && index.
ubound() < numColsMv;
446 const bool validSource = index.
size() <= numColsA;
448 if (! validIndex || ! validSource)
450 std::ostringstream os;
451 os <<
"Belos::MultiVecTraits<Scalar, Thyra::MultiVectorBase<Scalar> "
452 ">::SetBlock(A, [" << index.
lbound() <<
", " << index.
ubound()
455 os.str() <<
"Range lower bound must be nonnegative.");
457 os.str() <<
"Range upper bound must be less than "
458 "the number of columns " << numColsA <<
" in the "
459 "'mv' output argument.");
461 os.str() <<
"Range must have no more elements than"
462 " the number of columns " << numColsA <<
" in the "
463 "'A' input argument.");
471 if (index.
lbound() == 0 && index.
ubound()+1 == numColsMv)
472 mv_view = Teuchos::rcpFromRef (mv);
474 mv_view = mv.subView (index);
480 if (index.
size() == numColsA)
481 A_view = Teuchos::rcpFromRef (A);
486 Thyra::assign(mv_view.
ptr(), *A_view);
494 const int numColsA = A.domain()->dim();
495 const int numColsMv = mv.domain()->dim();
496 if (numColsA > numColsMv)
498 std::ostringstream os;
499 os <<
"Belos::MultiVecTraits<Scalar, Thyra::MultiVectorBase<Scalar>"
500 " >::Assign(A, mv): ";
502 os.str() <<
"Input multivector 'A' has "
503 << numColsA <<
" columns, but output multivector "
504 "'mv' has only " << numColsMv <<
" columns.");
508 if (numColsA == numColsMv) {
509 Thyra::assign (Teuchos::outArg (mv), A);
513 Thyra::assign (mv_view.
ptr(), A);
523 Thyra::randomize<ScalarType>(
526 Teuchos::outArg(mv));
533 Thyra::assign (Teuchos::outArg (mv), alpha);
548 #ifdef HAVE_BELOS_TSQR
555 #endif // HAVE_BELOS_TSQR
571 template<
class ScalarType>
572 class OperatorTraits <ScalarType,
573 Thyra::MultiVectorBase<ScalarType>,
574 Thyra::LinearOpBase<ScalarType> >
577 typedef Thyra::MultiVectorBase<ScalarType>
TMVB;
578 typedef Thyra::LinearOpBase<ScalarType>
TLOB;
600 ETrans trans = NOTRANS)
602 Thyra::EOpTransp whichOp;
610 whichOp = Thyra::NOTRANS;
611 else if (trans ==
TRANS)
612 whichOp = Thyra::TRANS;
614 whichOp = Thyra::CONJTRANS;
617 "Belos::OperatorTraits::Apply (Thyra specialization): "
618 "'trans' argument must be neither NOTRANS=" <<
NOTRANS
620 <<
", but instead has an invalid value of " << trans <<
".");
621 Thyra::apply<ScalarType>(Op, whichOp, x, Teuchos::outArg(y));
636 return Op.opSupported (Thyra::TRANS) &&
637 (! STS::isComplex || Op.opSupported (Thyra::CONJTRANS));
Thyra::LinearOpBase< ScalarType > TLOB
static int GetNumberVecs(const TMVB &mv)
Obtain the number of vectors in mv.
Thyra::MultiVectorBase< ScalarType > TMVB
static ptrdiff_t GetGlobalLength(const TMVB &mv)
Obtain the std::vector length of mv.
static void MvPrint(const TMVB &mv, std::ostream &os)
Print the mv multi-std::vector to the os output stream.
Thyra::MultiVectorBase< ScalarType > TMVB
Stub adaptor from Thyra::MultiVectorBase to TSQR.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::ScalarTraits< ScalarType > ST
static Teuchos::RCP< TMVB > CloneCopy(const TMVB &mv, const std::vector< int > &index)
Creates a new MultiVectorBase and copies the selected contents of mv into the new std::vector (deep c...
static void Assign(const TMVB &A, TMVB &mv)
static void MvTransMv(const ScalarType alpha, const TMVB &A, const TMVB &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
static void MvScale(TMVB &mv, const std::vector< ScalarType > &alpha)
Scale each element of the i-th vector in *this with alpha[i].
static void MvDot(const TMVB &mv, const TMVB &A, std::vector< ScalarType > &b)
Compute a std::vector b where the components are the individual dot-products of the i-th columns of A...
ST::magnitudeType magType
static Teuchos::RCP< TMVB > CloneCopy(const TMVB &mv)
Creates a new MultiVectorBase and copies contents of mv into the new std::vector (deep copy)...
static void MvScale(TMVB &mv, const ScalarType alpha)
Scale each element of the vectors in *this with alpha.
static void MvTimesMatAddMv(const ScalarType alpha, const TMVB &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, TMVB &mv)
Update mv with .
static Teuchos::RCP< const TMVB > CloneView(const TMVB &mv, const std::vector< int > &index)
Creates a new const MultiVectorBase that shares the selected contents of mv (shallow copy)...
static Teuchos::RCP< TMVB > CloneCopy(const TMVB &mv, const Teuchos::Range1D &index)
static void Apply(const TLOB &Op, const TMVB &x, TMVB &y, ETrans trans=NOTRANS)
Apply Op to x, storing the result in y.
static void MvRandom(TMVB &mv)
Replace the vectors in mv with random vectors.
static Teuchos::RCP< TMVB > CloneViewNonConst(TMVB &mv, const Teuchos::Range1D &index)
OrdinalType numCols() const
static void SetBlock(const TMVB &A, const std::vector< int > &index, TMVB &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
static void SetBlock(const TMVB &A, const Teuchos::Range1D &index, TMVB &mv)
static void MvNorm(const TMVB &mv, std::vector< magType > &normvec, NormType type=TwoNorm)
Compute the 2-norm of each individual std::vector of mv. Upon return, normvec[i] holds the value of ...
static bool HasApplyTranspose(const TLOB &Op)
Whether the operator implements applying the transpose.
static Teuchos::RCP< TMVB > CloneViewNonConst(TMVB &mv, const std::vector< int > &index)
Creates a new MultiVectorBase that shares the selected contents of mv (shallow copy).
static Teuchos::RCP< const TMVB > CloneView(const TMVB &mv, const Teuchos::Range1D &index)
OrdinalType stride() const
static Teuchos::RCP< TMVB > Clone(const TMVB &mv, const int numvecs)
Creates a new empty MultiVectorBase containing numvecs columns.
static void MvAddMv(const ScalarType alpha, const TMVB &A, const ScalarType beta, const TMVB &B, TMVB &mv)
Replace mv with .
OrdinalType numRows() const
#define STRATIMIKOS_TIME_MONITOR(NAME)