42 #ifndef TPETRA_VECTOR_DEF_HPP
43 #define TPETRA_VECTOR_DEF_HPP
53 #include "Tpetra_MultiVector.hpp"
54 #include "Tpetra_Details_gathervPrint.hpp"
55 #include "KokkosCompat_View.hpp"
56 #include "KokkosBlas1_nrm2w_squared.hpp"
57 #include "Teuchos_CommHelpers.hpp"
61 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
67 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
69 Vector (
const Teuchos::RCP<const map_type>& map,
74 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
77 const Teuchos::DataAccess copyOrView)
81 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
84 const Teuchos::RCP<const map_type>& map,
89 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
91 Vector (
const Teuchos::RCP<const map_type>& map,
92 const Teuchos::ArrayView<const Scalar>& values)
93 :
base_type (map, values, values.size (), 1)
96 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
98 Vector (
const Teuchos::RCP<const map_type>& map,
103 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
105 Vector (
const Teuchos::RCP<const map_type>& map,
111 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
118 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
122 this->base_type::replaceGlobalValue (globalRow, 0, value);
125 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
130 const bool atomic)
const
132 this->base_type::sumIntoGlobalValue (globalRow, 0, value, atomic);
135 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
139 this->base_type::replaceLocalValue (myRow, 0, value);
142 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
147 const bool atomic)
const
149 this->base_type::sumIntoLocalValue (globalRow, 0, value, atomic);
152 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
156 const size_t lda = this->getLocalLength ();
157 this->get1dCopy (A, lda);
160 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
166 this->dot (y, Teuchos::arrayView (&result, 1));
170 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
176 this->meanValue (Teuchos::arrayView (&mean, 1));
180 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
186 this->norm1 (Teuchos::arrayView (&norm, 1));
190 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
196 this->norm2 (Teuchos::arrayView (&norm, 1));
200 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
206 this->normInf (Teuchos::arrayView (&norm, 1));
210 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
211 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
218 using Kokkos::subview;
221 using Teuchos::reduceAll;
222 using Teuchos::REDUCE_SUM;
223 typedef Kokkos::Details::ArithTraits<impl_scalar_type> ATS;
224 typedef Kokkos::Details::ArithTraits<mag_type> ATM;
225 typedef Kokkos::View<mag_type, device_type> norm_view_type;
226 const char tfecfFuncName[] =
"normWeighted: ";
228 #ifdef HAVE_TPETRA_DEBUG
229 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
230 ! this->getMap ()->isCompatible (*weights.
getMap ()), std::runtime_error,
231 "Vectors do not have compatible Maps:" << std::endl
232 <<
"this->getMap(): " << std::endl << *this->getMap()
233 <<
"weights.getMap(): " << std::endl << *weights.
getMap() << std::endl);
235 const size_t lclNumRows = this->getLocalLength ();
236 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
238 "Vectors do not have the same local length.");
239 #endif // HAVE_TPETRA_DEBUG
241 norm_view_type lclNrm (
"lclNrm");
242 mag_type gblNrm = ATM::zero ();
244 auto X_lcl = this->
template getLocalView<device_type> ();
245 auto W_lcl = this->
template getLocalView<device_type> ();
246 KokkosBlas::nrm2w_squared (lclNrm,
247 subview (X_lcl, ALL (), 0),
248 subview (W_lcl, ALL (), 0));
249 const mag_type OneOverN =
250 ATM::one () /
static_cast<mag_type
> (this->getGlobalLength ());
251 RCP<const Comm<int> > comm = this->getMap ().is_null () ?
252 Teuchos::null : this->getMap ()->getComm ();
254 if (! comm.is_null () && this->isDistributed ()) {
256 reduceAll<int, mag_type> (*comm, REDUCE_SUM, 1, lclNrm.data (),
258 gblNrm = ATM::sqrt (gblNrm * OneOverN);
261 auto lclNrm_h = Kokkos::create_mirror_view (lclNrm);
263 gblNrm = ATM::sqrt (ATS::magnitude (lclNrm_h()) * OneOverN);
268 #endif // TPETRA_ENABLE_DEPRECATED_CODE
270 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
274 return this->descriptionImpl (
"Tpetra::Vector");
277 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
280 const Teuchos::EVerbosityLevel verbLevel)
const
282 this->describeImpl (out,
"Tpetra::Vector", verbLevel);
285 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
286 Teuchos::RCP<const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
288 offsetView (
const Teuchos::RCP<const map_type>& subMap,
289 const size_t offset)
const
292 using Kokkos::subview;
296 const size_t newNumRows = subMap->getNodeNumElements ();
297 const bool tooManyElts = newNumRows + offset > this->getOrigNumLocalRows ();
299 const int myRank = this->getMap ()->getComm ()->getRank ();
300 TEUCHOS_TEST_FOR_EXCEPTION(
301 newNumRows + offset > this->getLocalLength (), std::runtime_error,
302 "Tpetra::Vector::offsetView(NonConst): Invalid input Map. The input "
303 "Map owns " << newNumRows <<
" entries on process " << myRank <<
". "
304 "offset = " << offset <<
". Yet, the Vector contains only "
305 << this->getOrigNumLocalRows () <<
" rows on this process.");
308 const std::pair<size_t, size_t> offsetPair (offset, offset + newNumRows);
310 return rcp (
new V (subMap,
311 subview (this->view_, offsetPair, ALL ()),
315 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
316 Teuchos::RCP<Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
317 Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
318 offsetViewNonConst (
const Teuchos::RCP<const map_type>& subMap,
321 typedef Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> V;
322 return Teuchos::rcp_const_cast<V> (this->offsetView (subMap, offset));
325 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node>
326 Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>
332 vec_type dst (src, Teuchos::Copy);
350 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
351 template class Vector< SCALAR , LO , GO , NODE >; \
352 template Vector< SCALAR , LO , GO , NODE > createCopy (const Vector< SCALAR , LO , GO , NODE >& src);
354 #endif // TPETRA_VECTOR_DEF_HPP
void sumIntoGlobalValue(const GlobalOrdinal globalRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault) const
Add value to existing value, using global (row) index.
Vector()
Default constructor: makes a Vector with no rows or columns.
mag_type norm2() const
Return the two-norm of this Vector.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describe this object in a human-readable way to the given output stream.
base_type::mag_type mag_type
Type of a norm result.
void replaceGlobalValue(const GlobalOrdinal globalRow, const Scalar &value) const
Replace current value at the specified location with specified value.
Kokkos::DualView< impl_scalar_type **, Kokkos::LayoutLeft, execution_space > dual_view_type
Kokkos::DualView specialization used by this class.
size_t getLocalLength() const
Local number of rows on the calling process.
void get1dCopy(const Teuchos::ArrayView< Scalar > &A) const
Return multi-vector values in user-provided two-dimensional array (using Teuchos memory management cl...
One or more distributed dense vectors.
virtual std::string description() const
Return a one-line description of this object.
MultiVector< ST, LO, GO, NT > createCopy(const MultiVector< ST, LO, GO, NT > &src)
Return a deep copy of the given MultiVector.
dot_type dot(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y) const
Return the dot product of this Vector and the input Vector x.
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
Copy the contents of the MultiVector src into dst.
void sumIntoLocalValue(const LocalOrdinal myRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault) const
Add value to existing value, using local (row) index.
typename Kokkos::Details::InnerProductSpaceTraits< impl_scalar_type >::dot_type dot_type
Type of an inner ("dot") product result.
typename Kokkos::ArithTraits< impl_scalar_type >::mag_type mag_type
Type of a norm result.
A distributed dense vector.
typename map_type::local_ordinal_type local_ordinal_type
The type of local indices that this class uses.
Scalar meanValue() const
Compute mean (average) value of this Vector.
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
mag_type normInf() const
Return the infinity-norm of this Vector.
base_type::dot_type dot_type
Type of an inner ("dot") product result.
Base class for distributed Tpetra objects that support data redistribution.
mag_type norm1() const
Return the one-norm of this Vector.
void replaceLocalValue(const LocalOrdinal myRow, const Scalar &value) const
Replace current value at the specified location with specified values.