52 #ifndef BELOS_THYRA_ADAPTER_HPP
53 #define BELOS_THYRA_ADAPTER_HPP
59 #include <Thyra_DetachedMultiVectorView.hpp>
60 #include <Thyra_MultiVectorBase.hpp>
61 #include <Thyra_MultiVectorStdOps.hpp>
62 #ifdef HAVE_BELOS_TSQR
63 # include <Thyra_TsqrAdaptor.hpp>
66 #include <Teuchos_TimeMonitor.hpp>
82 template<
class ScalarType>
86 typedef Thyra::MultiVectorBase<ScalarType> TMVB;
87 typedef Teuchos::ScalarTraits<ScalarType> ST;
88 typedef typename ST::magnitudeType magType;
99 static Teuchos::RCP<TMVB>
Clone(
const TMVB& mv,
const int numvecs )
101 Teuchos::RCP<TMVB> c = Thyra::createMembers( mv.range(), numvecs );
111 int numvecs = mv.domain()->dim();
113 Teuchos::RCP< TMVB > cc = Thyra::createMembers( mv.range(), numvecs );
115 Thyra::assign(cc.ptr(), mv);
124 static Teuchos::RCP<TMVB>
CloneCopy(
const TMVB& mv,
const std::vector<int>& index )
126 int numvecs = index.size();
128 Teuchos::RCP<TMVB> cc = Thyra::createMembers( mv.range(), numvecs );
130 Teuchos::RCP<const TMVB> view = mv.subView(index);
132 Thyra::assign(cc.ptr(), *view);
136 static Teuchos::RCP<TMVB>
137 CloneCopy (
const TMVB& mv,
const Teuchos::Range1D& index)
139 const int numVecs = index.size();
141 Teuchos::RCP<TMVB> cc = Thyra::createMembers (mv.range(), numVecs);
143 Teuchos::RCP<const TMVB> view = mv.subView (index);
145 Thyra::assign (cc.ptr(), *view);
156 int numvecs = index.size();
172 for (
int i=0; i<numvecs; i++) {
173 if (lb+i != index[i]) contig =
false;
176 Teuchos::RCP< TMVB > cc;
178 const Thyra::Range1D rng(lb,lb+numvecs-1);
180 cc = mv.subView(rng);
184 cc = mv.subView(index);
189 static Teuchos::RCP<TMVB>
197 return mv.subView (index);
206 static Teuchos::RCP<const TMVB>
CloneView(
const TMVB& mv,
const std::vector<int>& index )
208 int numvecs = index.size();
224 for (
int i=0; i<numvecs; i++) {
225 if (lb+i != index[i]) contig =
false;
228 Teuchos::RCP< const TMVB > cc;
230 const Thyra::Range1D rng(lb,lb+numvecs-1);
232 cc = mv.subView(rng);
236 cc = mv.subView(index);
241 static Teuchos::RCP<const TMVB>
242 CloneView (
const TMVB& mv,
const Teuchos::Range1D& index)
249 return mv.subView (index);
259 return Teuchos::as<ptrdiff_t>(mv.range()->dim());
264 {
return mv.domain()->dim(); }
274 const Teuchos::SerialDenseMatrix<int,ScalarType>& B,
275 const ScalarType beta, TMVB& mv )
277 using Teuchos::arrayView;
using Teuchos::arcpFromArrayView;
279 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::MvTimesMatAddMv")));
281 const int m = B.numRows();
282 const int n = B.numCols();
283 auto vs = A.domain();
285 Teuchos::RCP< const TMVB >
286 B_thyra = vs->createCachedMembersView(
289 arcpFromArrayView(arrayView(&B(0,0), B.stride()*B.numCols())), B.stride()
293 Thyra::apply<ScalarType>(A, Thyra::NOTRANS, *B_thyra, Teuchos::outArg(mv), alpha, beta);
298 static void MvAddMv(
const ScalarType alpha,
const TMVB& A,
299 const ScalarType beta,
const TMVB& B, TMVB& mv )
301 typedef Teuchos::ScalarTraits<ScalarType> ST;
302 using Teuchos::tuple;
using Teuchos::ptrInArg;
using Teuchos::inoutArg;
304 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::MvAddMv")));
306 Thyra::linear_combination<ScalarType>(
307 tuple(alpha, beta)(), tuple(ptrInArg(A), ptrInArg(B))(), ST::zero(), inoutArg(mv));
312 static void MvScale ( TMVB& mv,
const ScalarType alpha )
314 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::MvScale")));
316 Thyra::scale(alpha, Teuchos::inoutArg(mv));
321 static void MvScale (TMVB& mv,
const std::vector<ScalarType>& alpha)
323 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::MvScale")));
325 for (
unsigned int i=0; i<alpha.size(); i++) {
326 Thyra::scale<ScalarType> (alpha[i], mv.col(i).ptr());
332 static void MvTransMv(
const ScalarType alpha,
const TMVB& A,
const TMVB& mv,
333 Teuchos::SerialDenseMatrix<int,ScalarType>& B )
335 using Teuchos::arrayView;
using Teuchos::arcpFromArrayView;
337 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::MvTransMv")));
340 int m = A.domain()->dim();
341 int n = mv.domain()->dim();
342 auto vs = A.domain();
345 B_thyra = vs->createCachedMembersView(
348 arcpFromArrayView(arrayView(&B(0,0), B.stride()*B.numCols())), B.stride()
351 Thyra::apply<ScalarType>(A, Thyra::CONJTRANS, mv, B_thyra.ptr(), alpha);
357 static void MvDot(
const TMVB& mv,
const TMVB& A, std::vector<ScalarType>& b )
359 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::MvDot")));
361 Thyra::dots(mv, A, Teuchos::arrayViewFromVector(b));
372 static void MvNorm(
const TMVB& mv, std::vector<magType>& normvec,
374 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::MvNorm")));
377 Thyra::norms_2(mv, Teuchos::arrayViewFromVector(normvec));
379 Thyra::norms_1(mv, Teuchos::arrayViewFromVector(normvec));
381 Thyra::norms_inf(mv, Teuchos::arrayViewFromVector(normvec));
383 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
384 "Belos::MultiVecTraits::MvNorm (Thyra specialization): "
385 "invalid norm type. Must be either TwoNorm, OneNorm or InfNorm");
395 static void SetBlock(
const TMVB& A,
const std::vector<int>& index, TMVB& mv )
398 int numvecs = index.size();
399 std::vector<int> indexA(numvecs);
400 int numAcols = A.domain()->dim();
401 for (
int i=0; i<numvecs; i++) {
406 if ( numAcols < numvecs ) {
411 else if ( numAcols > numvecs ) {
413 indexA.resize( numAcols );
416 Teuchos::RCP< const TMVB > relsource = A.subView(indexA);
418 Teuchos::RCP< TMVB > reldest = mv.subView(index);
420 Thyra::assign(reldest.ptr(), *relsource);
424 SetBlock (
const TMVB& A,
const Teuchos::Range1D& index, TMVB& mv)
426 const int numColsA = A.domain()->dim();
427 const int numColsMv = mv.domain()->dim();
429 const bool validIndex = index.lbound() >= 0 && index.ubound() < numColsMv;
431 const bool validSource = index.size() <= numColsA;
433 if (! validIndex || ! validSource)
435 std::ostringstream os;
436 os <<
"Belos::MultiVecTraits<Scalar, Thyra::MultiVectorBase<Scalar> "
437 ">::SetBlock(A, [" << index.lbound() <<
", " << index.ubound()
439 TEUCHOS_TEST_FOR_EXCEPTION(index.lbound() < 0, std::invalid_argument,
440 os.str() <<
"Range lower bound must be nonnegative.");
441 TEUCHOS_TEST_FOR_EXCEPTION(index.ubound() >= numColsMv, std::invalid_argument,
442 os.str() <<
"Range upper bound must be less than "
443 "the number of columns " << numColsA <<
" in the "
444 "'mv' output argument.");
445 TEUCHOS_TEST_FOR_EXCEPTION(index.size() > numColsA, std::invalid_argument,
446 os.str() <<
"Range must have no more elements than"
447 " the number of columns " << numColsA <<
" in the "
448 "'A' input argument.");
449 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Should never get here!");
455 Teuchos::RCP<TMVB> mv_view;
456 if (index.lbound() == 0 && index.ubound()+1 == numColsMv)
457 mv_view = Teuchos::rcpFromRef (mv);
459 mv_view = mv.subView (index);
464 Teuchos::RCP<const TMVB> A_view;
465 if (index.size() == numColsA)
466 A_view = Teuchos::rcpFromRef (A);
468 A_view = A.subView (Teuchos::Range1D(0, index.size()-1));
471 Thyra::assign(mv_view.ptr(), *A_view);
475 Assign (
const TMVB& A, TMVB& mv)
477 Teuchos::TimeMonitor tM(*Teuchos::TimeMonitor::getNewTimer(std::string(
"Belos::MVT::Assign")));
479 const int numColsA = A.domain()->dim();
480 const int numColsMv = mv.domain()->dim();
481 if (numColsA > numColsMv)
483 std::ostringstream os;
484 os <<
"Belos::MultiVecTraits<Scalar, Thyra::MultiVectorBase<Scalar>"
485 " >::Assign(A, mv): ";
486 TEUCHOS_TEST_FOR_EXCEPTION(numColsA > numColsMv, std::invalid_argument,
487 os.str() <<
"Input multivector 'A' has "
488 << numColsA <<
" columns, but output multivector "
489 "'mv' has only " << numColsMv <<
" columns.");
490 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Should never get here!");
493 if (numColsA == numColsMv) {
494 Thyra::assign (Teuchos::outArg (mv), A);
496 Teuchos::RCP<TMVB> mv_view =
498 Thyra::assign (mv_view.ptr(), A);
508 Thyra::randomize<ScalarType>(
509 -Teuchos::ScalarTraits<ScalarType>::one(),
510 Teuchos::ScalarTraits<ScalarType>::one(),
511 Teuchos::outArg(mv));
516 MvInit (TMVB& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero())
518 Thyra::assign (Teuchos::outArg (mv), alpha);
528 static void MvPrint(
const TMVB& mv, std::ostream& os )
529 { os << describe(mv,Teuchos::VERB_EXTREME); }
533 #ifdef HAVE_BELOS_TSQR
556 template<
class ScalarType>
558 Thyra::MultiVectorBase<ScalarType>,
559 Thyra::LinearOpBase<ScalarType> >
562 typedef Thyra::MultiVectorBase<ScalarType> TMVB;
563 typedef Thyra::LinearOpBase<ScalarType> TLOB;
587 Thyra::EOpTransp whichOp;
595 whichOp = Thyra::NOTRANS;
596 else if (trans ==
TRANS)
597 whichOp = Thyra::TRANS;
599 whichOp = Thyra::CONJTRANS;
601 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
602 "Belos::OperatorTraits::Apply (Thyra specialization): "
603 "'trans' argument must be neither NOTRANS=" <<
NOTRANS
605 <<
", but instead has an invalid value of " << trans <<
".");
606 Thyra::apply<ScalarType>(Op, whichOp, x, Teuchos::outArg(y));
612 typedef Teuchos::ScalarTraits<ScalarType> STS;
621 return Op.opSupported (Thyra::TRANS) &&
622 (! STS::isComplex || Op.opSupported (Thyra::CONJTRANS));
static void MvPrint(const TMVB &mv, std::ostream &os)
Print the mv multi-std::vector to the os output stream.
static Teuchos::RCP< TMVB > Clone(const TMVB &mv, const int numvecs)
Creates a new empty MultiVectorBase containing numvecs columns.
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 MvAddMv(const ScalarType alpha, const TMVB &A, const ScalarType beta, const TMVB &B, TMVB &mv)
Replace mv with .
static int GetNumberVecs(const TMVB &mv)
Obtain the number of vectors in mv.
static ptrdiff_t GetGlobalLength(const TMVB &mv)
Obtain the std::vector length of mv.
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...
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 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 void MvScale(TMVB &mv, const ScalarType alpha)
Scale each element of the vectors in *this with alpha.
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 MvTimesMatAddMv(const ScalarType alpha, const TMVB &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, TMVB &mv)
Update mv with .
static void MvRandom(TMVB &mv)
Replace the vectors in mv with random vectors.
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 std::vector< int > &index)
Creates a new MultiVectorBase and copies the selected contents of mv into the new std::vector (deep c...
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 void MvInit(TMVB &mv, ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
static void Assign(const MV &A, MV &mv)
static bool HasApplyTranspose(const TLOB &Op)
Whether the operator implements applying the transpose.
static void Apply(const TLOB &Op, const TMVB &x, TMVB &y, ETrans trans=NOTRANS)
Apply Op to x, storing the result in y.
Stub adaptor from Thyra::MultiVectorBase to TSQR.