42 #ifndef BELOS_TPETRA_ADAPTER_MP_VECTOR_HPP
43 #define BELOS_TPETRA_ADAPTER_MP_VECTOR_HPP
45 #include "BelosTpetraAdapter.hpp"
48 #include "KokkosBlas.hpp"
50 #ifdef HAVE_BELOS_TSQR
52 #endif // HAVE_BELOS_TSQR
72 template<
class Storage,
class LO,
class GO,
class Node>
74 Tpetra::MultiVector< Sacado::MP::Vector<Storage>,
80 typedef Tpetra::MultiVector<Scalar, LO, GO, Node>
MV;
82 typedef typename Tpetra::MultiVector<Scalar,LO,GO,Node>::dot_type
dot_type;
83 typedef typename Tpetra::MultiVector<Scalar,LO,GO,Node>::mag_type
mag_type;
127 #ifdef HAVE_TPETRA_DEBUG
128 const char fnName[] =
"Belos::MultiVecTraits::CloneCopy(mv,index)";
129 const size_t inNumVecs = mv.getNumVectors ();
131 index.size () > 0 && *std::min_element (index.begin (), index.end ()) < 0,
132 std::runtime_error, fnName <<
": All indices must be nonnegative.");
135 static_cast<size_t> (*std::max_element (index.begin (), index.end ())) >= inNumVecs,
137 fnName <<
": All indices must be strictly less than the number of "
138 "columns " << inNumVecs <<
" of the input multivector mv.");
139 #endif // HAVE_TPETRA_DEBUG
143 for (std::vector<int>::size_type
j = 0;
j < index.size (); ++
j) {
144 columns[
j] = index[
j];
163 const bool validRange = index.
size() > 0 &&
165 index.
ubound() < GetNumberVecs(mv);
167 std::ostringstream os;
168 os <<
"Belos::MultiVecTraits::CloneCopy(mv,index=["
171 index.
size() == 0, std::invalid_argument,
172 os.str() <<
"Empty index range is not allowed.");
174 index.
lbound() < 0, std::invalid_argument,
175 os.str() <<
"Index range includes negative index/ices, which is not "
178 index.
ubound() >= GetNumberVecs(mv), std::invalid_argument,
179 os.str() <<
"Index range exceeds number of vectors "
180 << mv.getNumVectors() <<
" in the input multivector.");
182 os.str() <<
"Should never get here!");
193 #ifdef HAVE_TPETRA_DEBUG
194 const char fnName[] =
"Belos::MultiVecTraits::CloneViewNonConst(mv,index)";
195 const size_t numVecs = mv.getNumVectors ();
197 index.size () > 0 && *std::min_element (index.begin (), index.end ()) < 0,
198 std::invalid_argument,
199 fnName <<
": All indices must be nonnegative.");
202 static_cast<size_t> (*std::max_element (index.begin (), index.end ())) >= numVecs,
203 std::invalid_argument,
204 fnName <<
": All indices must be strictly less than the number of "
205 "columns " << numVecs <<
" in the input MultiVector mv.");
206 #endif // HAVE_TPETRA_DEBUG
210 for (std::vector<int>::size_type
j = 0;
j < index.size (); ++
j) {
211 columns[
j] = index[
j];
227 const int numCols =
static_cast<int> (mv.getNumVectors());
228 const bool validRange = index.
size() > 0 &&
231 std::ostringstream os;
232 os <<
"Belos::MultiVecTraits::CloneViewNonConst(mv,index=["
235 index.
size() == 0, std::invalid_argument,
236 os.str() <<
"Empty index range is not allowed.");
238 index.
lbound() < 0, std::invalid_argument,
239 os.str() <<
"Index range includes negative inde{x,ices}, which is "
242 index.
ubound() >= numCols, std::invalid_argument,
243 os.str() <<
"Index range exceeds number of vectors " << numCols
244 <<
" in the input multivector.");
246 true, std::logic_error,
247 os.str() <<
"Should never get here!");
257 #ifdef HAVE_TPETRA_DEBUG
258 const char fnName[] =
"Belos::MultiVecTraits<Scalar, "
259 "Tpetra::MultiVector<...> >::CloneView(mv,index)";
260 const size_t numVecs = mv.getNumVectors ();
262 *std::min_element (index.begin (), index.end ()) < 0,
263 std::invalid_argument,
264 fnName <<
": All indices must be nonnegative.");
266 static_cast<size_t> (*std::max_element (index.begin (), index.end ())) >= numVecs,
267 std::invalid_argument,
268 fnName <<
": All indices must be strictly less than the number of "
269 "columns " << numVecs <<
" in the input MultiVector mv.");
270 #endif // HAVE_TPETRA_DEBUG
274 for (std::vector<int>::size_type
j = 0;
j < index.size (); ++
j) {
275 columns[
j] = index[
j];
291 const int numCols =
static_cast<int> (mv.getNumVectors());
292 const bool validRange = index.
size() > 0 &&
295 std::ostringstream os;
296 os <<
"Belos::MultiVecTraits::CloneView(mv, index=["
299 os.str() <<
"Empty index range is not allowed.");
301 os.str() <<
"Index range includes negative index/ices, which is not "
304 index.
ubound() >= numCols, std::invalid_argument,
305 os.str() <<
"Index range exceeds number of vectors " << numCols
306 <<
" in the input multivector.");
308 os.str() <<
"Should never get here!");
316 return static_cast<ptrdiff_t
> (mv.getGlobalLength ());
320 return static_cast<int> (mv.getNumVectors ());
324 return mv.isConstantStride ();
329 const Tpetra::MultiVector<Scalar,LO,GO,Node>&
A,
332 Tpetra::MultiVector<Scalar,LO,GO,Node>&
C)
338 const int numRowsB = B.
numRows ();
339 const int numColsB = B.
numCols ();
340 const int strideB = B.
stride ();
341 if (numRowsB == 1 && numColsB == 1) {
342 C.update (alpha*B(0,0), A, beta);
347 RCP<const MV> Atmp, Ctmp;
349 else Atmp =
rcp(&A,
false);
352 else Ctmp =
rcp(&C,
false);
355 typedef Tpetra::MultiVector<dot_type,LO,GO,Node> FMV;
356 typedef typename FMV::dual_view_type::t_dev flat_view_type;
358 flat_view_type flat_A_view = Atmp->template getLocalView<execution_space>();
359 flat_view_type flat_C_view = Ctmp->template getLocalView<execution_space>();
362 typedef Kokkos::View<dot_type**, Kokkos::LayoutLeft, Kokkos::HostSpace> b_host_view_type;
363 b_host_view_type B_view_host_input( B.
values(), strideB, numColsB);
364 auto B_view_host = Kokkos::subview(B_view_host_input,
365 Kokkos::pair<int,int>(0,numRowsB),
366 Kokkos::pair<int,int>(0,numColsB));
370 typedef Kokkos::View<dot_type**, Kokkos::LayoutLeft, execution_space> b_view_type;
371 typedef Kokkos::View<dot_type*, Kokkos::LayoutLeft, execution_space> b_1d_view_type;
372 b_1d_view_type B_1d_view_dev(Kokkos::ViewAllocateWithoutInitializing(
"B"), numRowsB*numColsB);
373 b_view_type B_view_dev( B_1d_view_dev.data(), numRowsB, numColsB);
378 const char ctransA =
'N', ctransB =
'N';
381 alpha, flat_A_view, B_view_dev, beta, flat_C_view);
384 if (C.isConstantStride() ==
false)
397 const Tpetra::MultiVector<Scalar,LO,GO,Node>&
A,
399 const Tpetra::MultiVector<Scalar,LO,GO,Node>&
B,
400 Tpetra::MultiVector<Scalar,LO,GO,Node>& mv)
406 MvScale (Tpetra::MultiVector<Scalar,LO,GO,Node>& mv,
413 MvScale (Tpetra::MultiVector<Scalar,LO,GO,Node>& mv,
414 const std::vector<BaseScalar>& alphas)
416 std::vector<Scalar> alphas_mp(alphas.size());
417 const size_t sz = alphas.size();
418 for (
size_t i=0; i<sz; ++i)
419 alphas_mp[i] = alphas[i];
420 mv.scale (alphas_mp);
424 MvScale (Tpetra::MultiVector<Scalar,LO,GO,Node>& mv,
425 const std::vector<Scalar>& alphas)
432 const Tpetra::MultiVector<Scalar,LO,GO,Node>&
A,
433 const Tpetra::MultiVector<Scalar,LO,GO,Node>&
B,
440 using Teuchos::reduceAll;
443 const int numRowsC = C.
numRows ();
444 const int numColsC = C.
numCols ();
445 const int strideC = C.
stride ();
446 if (numRowsC == 1 && numColsC == 1) {
460 RCP<const MV> Atmp, Btmp;
462 else Atmp =
rcp(&A,
false);
465 else Btmp =
rcp(&B,
false);
468 typedef Tpetra::MultiVector<dot_type,LO,GO,Node> FMV;
469 typedef typename FMV::dual_view_type::t_dev flat_view_type;
471 flat_view_type flat_A_view = Atmp->template getLocalView<execution_space>();
472 flat_view_type flat_B_view = Btmp->template getLocalView<execution_space>();
475 typedef Kokkos::View<dot_type**, Kokkos::LayoutLeft, Kokkos::HostSpace> c_host_view_type;
476 c_host_view_type C_view_host_input( C.
values(), strideC, numColsC);
477 auto C_view_host = Kokkos::subview(C_view_host_input,
478 Kokkos::pair<int,int>(0,numRowsC),
479 Kokkos::pair<int,int>(0,numColsC));
483 typedef Kokkos::View<dot_type**, Kokkos::LayoutLeft, execution_space> c_view_type;
484 typedef Kokkos::View<dot_type*, Kokkos::LayoutLeft, execution_space> c_1d_view_type;
485 c_1d_view_type C_1d_view_dev(
"C", numRowsC*numColsC);
486 c_view_type C_view_dev( C_1d_view_dev.data(), numRowsC, numColsC);
490 const char ctransA =
'C', ctransB =
'N';
493 alpha, flat_A_view, flat_B_view,
494 Kokkos::Details::ArithTraits<dot_type>::zero(),
499 RCP<const Comm<int> > pcomm = A.getMap()->getComm ();
500 if (pcomm->getSize () == 1)
503 typedef Kokkos::View<dot_type*, Kokkos::LayoutLeft, Kokkos::HostSpace> c_1d_host_view_type;
504 c_1d_host_view_type C_1d_view_tmp(Kokkos::ViewAllocateWithoutInitializing(
"C_tmp"), strideC*numColsC);
505 c_host_view_type C_view_tmp( C_1d_view_tmp.data(),
508 Kokkos::pair<int,int>(0,numRowsC),
509 Kokkos::pair<int,int>(0,numColsC)) ,
511 reduceAll<int> (*pcomm, REDUCE_SUM, strideC*numColsC,
519 MvDot (
const Tpetra::MultiVector<Scalar,LO,GO,Node>&
A,
520 const Tpetra::MultiVector<Scalar,LO,GO,Node>&
B,
521 std::vector<dot_type>& dots)
523 const size_t numVecs = A.getNumVectors ();
526 numVecs != B.getNumVectors (), std::invalid_argument,
527 "Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvDot(A,B,dots): "
528 "A and B must have the same number of columns. "
529 "A has " << numVecs <<
" column(s), "
530 "but B has " << B.getNumVectors () <<
" column(s).");
531 #ifdef HAVE_TPETRA_DEBUG
533 dots.size() < numVecs, std::invalid_argument,
534 "Belos::MultiVecTraits<Scalar,Tpetra::MultiVector>::MvDot(A,B,dots): "
535 "The output array 'dots' must have room for all dot products. "
536 "A and B each have " << numVecs <<
" column(s), "
537 "but 'dots' only has " << dots.size() <<
" entry(/ies).");
538 #endif // HAVE_TPETRA_DEBUG
541 A.dot (B, av (0, numVecs));
546 MvNorm (
const Tpetra::MultiVector<Scalar,LO,GO,Node>& mv,
547 std::vector<mag_type> &normvec,
548 NormType type=TwoNorm)
551 #ifdef HAVE_TPETRA_DEBUG
552 typedef std::vector<int>::size_type size_type;
554 normvec.size () <
static_cast<size_type
> (mv.getNumVectors ()),
555 std::invalid_argument,
556 "Belos::MultiVecTraits::MvNorm(mv,normvec): The normvec output "
557 "argument must have at least as many entries as the number of vectors "
558 "(columns) in the MultiVector mv. normvec.size() = " << normvec.size ()
559 <<
" < mv.getNumVectors() = " << mv.getNumVectors () <<
".");
565 mv.norm1(av(0,mv.getNumVectors()));
568 mv.norm2(av(0,mv.getNumVectors()));
571 mv.normInf(av(0,mv.getNumVectors()));
578 true, std::logic_error,
579 "Belos::MultiVecTraits::MvNorm: Invalid NormType value " << type
580 <<
". Valid values are OneNorm=" << OneNorm <<
", TwoNorm="
581 << TwoNorm <<
", and InfNorm=" << InfNorm <<
". If you are a Belos "
582 "user and have not modified Belos in any way, and you get this "
583 "message, then this is probably a bug in the Belos solver you were "
584 "using. Please report this to the Belos developers.");
593 const size_t inNumVecs = A.getNumVectors ();
594 #ifdef HAVE_TPETRA_DEBUG
596 inNumVecs < static_cast<size_t> (index.size ()), std::invalid_argument,
597 "Belos::MultiVecTraits::SetBlock(A,index,mv): 'index' argument must "
598 "have no more entries as the number of columns in the input MultiVector"
599 " A. A.getNumVectors() = " << inNumVecs <<
" < index.size () = "
600 << index.size () <<
".");
601 #endif // HAVE_TPETRA_DEBUG
602 RCP<MV> mvsub = CloneViewNonConst (mv, index);
603 if (inNumVecs > static_cast<size_t> (index.size ())) {
604 RCP<const MV> Asub = A.subView (Range1D (0, index.size () - 1));
621 const size_t maxInt =
623 const bool overflow =
624 maxInt < A.getNumVectors () && maxInt < mv.getNumVectors ();
626 std::ostringstream os;
627 os <<
"Belos::MultiVecTraits::SetBlock(A, index=[" << index.
lbound ()
628 <<
", " << index.
ubound () <<
"], mv): ";
630 maxInt < A.getNumVectors (), std::range_error, os.str () <<
"Number "
631 "of columns (size_t) in the input MultiVector 'A' overflows int.");
633 maxInt < mv.getNumVectors (), std::range_error, os.str () <<
"Number "
634 "of columns (size_t) in the output MultiVector 'mv' overflows int.");
637 const int numColsA =
static_cast<int> (A.getNumVectors ());
638 const int numColsMv =
static_cast<int> (mv.getNumVectors ());
640 const bool validIndex =
643 const bool validSource = index.
size () <= numColsA;
645 if (! validIndex || ! validSource) {
646 std::ostringstream os;
647 os <<
"Belos::MultiVecTraits::SetBlock(A, index=[" << index.
lbound ()
648 <<
", " << index.
ubound () <<
"], mv): ";
650 index.
lbound() < 0, std::invalid_argument,
651 os.str() <<
"Range lower bound must be nonnegative.");
653 index.
ubound() >= numColsMv, std::invalid_argument,
654 os.str() <<
"Range upper bound must be less than the number of "
655 "columns " << numColsA <<
" in the 'mv' output argument.");
657 index.
size() > numColsA, std::invalid_argument,
658 os.str() <<
"Range must have no more elements than the number of "
659 "columns " << numColsA <<
" in the 'A' input argument.");
661 true, std::logic_error,
"Should never get here!");
668 if (index.
lbound () == 0 && index.
ubound () + 1 == numColsMv) {
669 mv_view = Teuchos::rcpFromRef (mv);
671 mv_view = CloneViewNonConst (mv, index);
678 if (index.
size () == numColsA) {
679 A_view = Teuchos::rcpFromRef (A);
689 const char errPrefix[] =
"Belos::MultiVecTraits::Assign(A, mv): ";
698 const size_t maxInt =
700 const bool overflow =
701 maxInt < A.getNumVectors () && maxInt < mv.getNumVectors ();
704 maxInt < A.getNumVectors(), std::range_error,
705 errPrefix <<
"Number of columns in the input multivector 'A' "
706 "(a size_t) overflows int.");
708 maxInt < mv.getNumVectors(), std::range_error,
709 errPrefix <<
"Number of columns in the output multivector 'mv' "
710 "(a size_t) overflows int.");
712 true, std::logic_error,
"Should never get here!");
715 const int numColsA =
static_cast<int> (A.getNumVectors ());
716 const int numColsMv =
static_cast<int> (mv.getNumVectors ());
717 if (numColsA > numColsMv) {
719 numColsA > numColsMv, std::invalid_argument,
720 errPrefix <<
"Input multivector 'A' has " << numColsA <<
" columns, "
721 "but output multivector 'mv' has only " << numColsMv <<
" columns.");
724 if (numColsA == numColsMv) {
741 mv.putScalar (alpha);
749 #ifdef HAVE_BELOS_TSQR
750 typedef Tpetra::TsqrAdaptor< Tpetra::MultiVector< Scalar, LO, GO, Node > > tsqr_adaptor_type;
754 #endif // HAVE_BELOS_TSQR
764 template <
class Storage,
class LO,
class GO,
class Node>
766 Tpetra::MultiVector<Sacado::MP::Vector<Storage>,
768 Tpetra::Operator<Sacado::MP::Vector<Storage>,
774 Apply (
const Tpetra::Operator<Scalar,LO,GO,Node>& Op,
775 const Tpetra::MultiVector<Scalar,LO,GO,Node>& X,
776 Tpetra::MultiVector<Scalar,LO,GO,Node>& Y,
777 ETrans trans=NOTRANS)
794 const std::string otName =
"Belos::OperatorTraits<" + scalarName
795 +
"," + loName +
"," + goName +
"," + nodeName +
">";
797 "get here; fell through a switch statement. "
798 "Please report this bug to the Belos developers.");
805 return Op.hasTransposeApply ();
ScalarType * values() const
Tpetra::MultiVector< Scalar, LO, GO, Node >::mag_type mag_type
static int GetNumberVecs(const MV &mv)
static bool HasApplyTranspose(const Tpetra::Operator< Scalar, LO, GO, Node > &Op)
static void Assign(const MV &A, MV &mv)
static Teuchos::RCP< MV > CloneCopy(const MV &mv, const std::vector< int > &index)
Create and return a deep copy of the given columns of mv.
static void MvScale(Tpetra::MultiVector< Scalar, LO, GO, Node > &mv, const std::vector< BaseScalar > &alphas)
Kokkos::DefaultExecutionSpace execution_space
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Storage::ordinal_type s_ordinal
Tpetra::MultiVector< Scalar, LO, GO, Node > MV
Sacado::MP::Vector< Storage > Scalar
static bool HasConstantStride(const MV &mv)
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const Teuchos::Range1D &index)
static void MvTimesMatAddMv(const dot_type &alpha, const Tpetra::MultiVector< Scalar, LO, GO, Node > &A, const Teuchos::SerialDenseMatrix< int, dot_type > &B, const dot_type &beta, Tpetra::MultiVector< Scalar, LO, GO, Node > &C)
static void MvScale(Tpetra::MultiVector< Scalar, LO, GO, Node > &mv, const Scalar &alpha)
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static void MvPrint(const MV &mv, std::ostream &os)
static void MvScale(Tpetra::MultiVector< Scalar, LO, GO, Node > &mv, const std::vector< Scalar > &alphas)
static void MvTransMv(dot_type alpha, const Tpetra::MultiVector< Scalar, LO, GO, Node > &A, const Tpetra::MultiVector< Scalar, LO, GO, Node > &B, Teuchos::SerialDenseMatrix< int, dot_type > &C)
static ptrdiff_t GetGlobalLength(const MV &mv)
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
static void MvNorm(const Tpetra::MultiVector< Scalar, LO, GO, Node > &mv, std::vector< mag_type > &normvec, NormType type=TwoNorm)
For all columns j of mv, set normvec[j] = norm(mv[j]).
Storage::value_type BaseScalar
Tpetra::MultiVector< Scalar, LO, GO, Node >::dot_type dot_type
static Teuchos::RCP< MV > CloneCopy(const MV &X)
Create and return a deep copy of X.
static Teuchos::RCP< MV > Clone(const MV &X, const int numVecs)
Create a new MultiVector with numVecs columns.
OrdinalType numCols() const
static void Apply(const Tpetra::Operator< Scalar, LO, GO, Node > &Op, const Tpetra::MultiVector< Scalar, LO, GO, Node > &X, Tpetra::MultiVector< Scalar, LO, GO, Node > &Y, ETrans trans=NOTRANS)
static Teuchos::RCP< MV > CloneCopy(const MV &mv, const Teuchos::Range1D &index)
Create and return a deep copy of the given columns of mv.
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
std::enable_if< Kokkos::is_view_mp_vector< Kokkos::View< DA, PA...> >::value &&Kokkos::is_view_mp_vector< Kokkos::View< DB, PB...> >::value &&Kokkos::is_view_mp_vector< Kokkos::View< DC, PC...> >::value >::type gemm(const char transA[], const char transB[], typename Kokkos::View< DA, PA...>::const_value_type &alpha, const Kokkos::View< DA, PA...> &A, const Kokkos::View< DB, PB...> &B, typename Kokkos::View< DC, PC...>::const_value_type &beta, const Kokkos::View< DC, PC...> &C)
Sacado::MP::Vector< Storage > Scalar
static void MvRandom(MV &mv)
static Teuchos::RCP< const MV > CloneView(const MV &mv, const Teuchos::Range1D &index)
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
OrdinalType stride() const
static std::string name()
OrdinalType numRows() const
static void MvDot(const Tpetra::MultiVector< Scalar, LO, GO, Node > &A, const Tpetra::MultiVector< Scalar, LO, GO, Node > &B, std::vector< dot_type > &dots)
For all columns j of A, set dots[j] := A[j]^T * B[j].
static void SetBlock(const MV &A, const Teuchos::Range1D &index, MV &mv)
static void MvAddMv(Scalar alpha, const Tpetra::MultiVector< Scalar, LO, GO, Node > &A, Scalar beta, const Tpetra::MultiVector< Scalar, LO, GO, Node > &B, Tpetra::MultiVector< Scalar, LO, GO, Node > &mv)
mv := alpha*A + beta*B