Tpetra parallel linear algebra  Version of the Day
Tpetra_Vector_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_VECTOR_DEF_HPP
43 #define TPETRA_VECTOR_DEF_HPP
44 
52 
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"
58 
59 namespace Tpetra {
60 namespace Classes {
61 
62  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
65  : base_type ()
66  {}
67 
68  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
70  Vector (const Teuchos::RCP<const map_type>& map,
71  const bool zeroOut)
72  : base_type (map, 1, zeroOut)
73  {}
74 
75  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
78  : base_type (source)
79  {}
80 
81  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
84  const Teuchos::DataAccess copyOrView)
85  : base_type (source, copyOrView)
86  {}
87 
88  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
90  Vector (const Teuchos::RCP<const map_type>& map,
91  const Teuchos::ArrayView<const Scalar>& values)
92  : base_type (map, values, values.size (), 1)
93  {}
94 
95  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
97  Vector (const Teuchos::RCP<const map_type>& map,
98  const dual_view_type& view)
99  : base_type (map, view)
100  {}
101 
102  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
104  Vector (const Teuchos::RCP<const map_type>& map,
105  const dual_view_type& view,
106  const dual_view_type& origView)
107  : base_type (map, view, origView)
108  {}
109 
110  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
113  const size_t j)
114  : base_type (X, j)
115  {}
116 
117  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
120  {}
121 
122  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
123  void
125  replaceGlobalValue (const GlobalOrdinal globalRow, const Scalar& value) const {
126  this->base_type::replaceGlobalValue (globalRow, 0, value);
127  }
128 
129  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
130  void
132  sumIntoGlobalValue (const GlobalOrdinal globalRow,
133  const Scalar& value,
134  const bool atomic) const
135  {
136  this->base_type::sumIntoGlobalValue (globalRow, 0, value, atomic);
137  }
138 
139  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
140  void
142  replaceLocalValue (const LocalOrdinal myRow, const Scalar& value) const {
143  this->base_type::replaceLocalValue (myRow, 0, value);
144  }
145 
146  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
147  void
149  sumIntoLocalValue (const LocalOrdinal globalRow,
150  const Scalar& value,
151  const bool atomic) const
152  {
153  this->base_type::sumIntoLocalValue (globalRow, 0, value, atomic);
154  }
155 
156  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
157  void
159  get1dCopy (const Teuchos::ArrayView<Scalar>& A) const {
160  const size_t lda = this->getLocalLength ();
161  this->get1dCopy (A, lda);
162  }
163 
164  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
168  {
169  dot_type result;
170  this->dot (y, Teuchos::arrayView (&result, 1));
171  return result;
172  }
173 
174  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
175  Scalar
177  meanValue () const
178  {
179  Scalar mean;
180  this->meanValue (Teuchos::arrayView (&mean, 1));
181  return mean;
182  }
183 
184  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
187  norm1 () const
188  {
189  mag_type norm;
190  this->norm1 (Teuchos::arrayView (&norm, 1));
191  return norm;
192  }
193 
194  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
197  norm2 () const
198  {
199  mag_type norm;
200  this->norm2 (Teuchos::arrayView (&norm, 1));
201  return norm;
202  }
203 
204  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
207  normInf () const
208  {
209  mag_type norm;
210  this->normInf (Teuchos::arrayView (&norm, 1));
211  return norm;
212  }
213 
214  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
216  TPETRA_DEPRECATED
219  {
220  using Kokkos::ALL;
221  using Kokkos::subview;
222  using Teuchos::Comm;
223  using Teuchos::RCP;
224  using Teuchos::reduceAll;
225  using Teuchos::REDUCE_SUM;
226  typedef Kokkos::Details::ArithTraits<impl_scalar_type> ATS;
227  typedef Kokkos::Details::ArithTraits<mag_type> ATM;
228  typedef Kokkos::View<mag_type, device_type> norm_view_type; // just one
229  const char tfecfFuncName[] = "normWeighted: ";
230 
231 #ifdef HAVE_TPETRA_DEBUG
232  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
233  ! this->getMap ()->isCompatible (*weights.getMap ()), std::runtime_error,
234  "Vectors do not have compatible Maps:" << std::endl
235  << "this->getMap(): " << std::endl << *this->getMap()
236  << "weights.getMap(): " << std::endl << *weights.getMap() << std::endl);
237 #else
238  const size_t lclNumRows = this->getLocalLength ();
239  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
240  lclNumRows != weights.getLocalLength (), std::runtime_error,
241  "Vectors do not have the same local length.");
242 #endif // HAVE_TPETRA_DEBUG
243 
244  norm_view_type lclNrm ("lclNrm"); // local norm
245  mag_type gblNrm = ATM::zero (); // return value
246 
247  auto X_lcl = this->template getLocalView<device_type> ();
248  auto W_lcl = this->template getLocalView<device_type> ();
249  KokkosBlas::nrm2w_squared (lclNrm,
250  subview (X_lcl, ALL (), 0),
251  subview (W_lcl, ALL (), 0));
252  const mag_type OneOverN =
253  ATM::one () / static_cast<mag_type> (this->getGlobalLength ());
254  RCP<const Comm<int> > comm = this->getMap ().is_null () ?
255  Teuchos::null : this->getMap ()->getComm ();
256 
257  if (! comm.is_null () && this->isDistributed ()) {
258  // Assume that MPI can access device memory.
259  reduceAll<int, mag_type> (*comm, REDUCE_SUM, 1, lclNrm.data (),
260  &gblNrm);
261  gblNrm = ATM::sqrt (gblNrm * OneOverN);
262  }
263  else {
264  auto lclNrm_h = Kokkos::create_mirror_view (lclNrm);
265  Kokkos::deep_copy (lclNrm_h, lclNrm);
266  gblNrm = ATM::sqrt (ATS::magnitude (lclNrm_h()) * OneOverN);
267  }
268 
269  return gblNrm;
270  }
271 
272  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
274  description () const
275  {
276  return this->descriptionImpl ("Tpetra::Vector");
277  }
278 
279  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
281  describe (Teuchos::FancyOStream& out,
282  const Teuchos::EVerbosityLevel verbLevel) const
283  {
284  this->describeImpl (out, "Tpetra::Vector", verbLevel);
285  }
286 
287  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
288  Teuchos::RCP<const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
290  offsetView (const Teuchos::RCP<const map_type>& subMap,
291  const size_t offset) const
292  {
293  using Kokkos::ALL;
294  using Kokkos::subview;
295  using Teuchos::rcp;
297 
298  const size_t newNumRows = subMap->getNodeNumElements ();
299  const bool tooManyElts = newNumRows + offset > this->getOrigNumLocalRows ();
300  if (tooManyElts) {
301  const int myRank = this->getMap ()->getComm ()->getRank ();
302  TEUCHOS_TEST_FOR_EXCEPTION(
303  newNumRows + offset > this->getLocalLength (), std::runtime_error,
304  "Tpetra::Vector::offsetView(NonConst): Invalid input Map. The input "
305  "Map owns " << newNumRows << " entries on process " << myRank << ". "
306  "offset = " << offset << ". Yet, the Vector contains only "
307  << this->getOrigNumLocalRows () << " rows on this process.");
308  }
309 
310  const std::pair<size_t, size_t> offsetPair (offset, offset + newNumRows);
311  // Need 'this->' to get view_ and origView_ from parent class.
312  return rcp (new V (subMap,
313  subview (this->view_, offsetPair, ALL ()),
314  this->origView_));
315  }
316 
317  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
318  Teuchos::RCP<Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
319  Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
320  offsetViewNonConst (const Teuchos::RCP<const map_type>& subMap,
321  const size_t offset)
322  {
323  typedef Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> V;
324  return Teuchos::rcp_const_cast<V> (this->offsetView (subMap, offset));
325  }
326 
327 } // namespace Classes
328 
329  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
330  Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>
332  {
334  // The 2-argument copy constructor with second argument =
335  // Teuchos::Copy does a deep copy of its input.
336  vec_type dst (src, Teuchos::Copy);
337 
338  // The Kokkos refactor version of Vector has view semantics, so
339  // returning the Vector directly, rather than through RCP, only
340  // does a shallow copy.
341  return dst;
342  }
343 
344 } // namespace Tpetra
345 
354 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
355  namespace Classes { \
356  template class Vector< SCALAR , LO , GO , NODE >; \
357  } \
358  template Vector< SCALAR , LO , GO , NODE > createCopy (const Vector< SCALAR , LO , GO , NODE >& src);
359 
360 #endif // TPETRA_VECTOR_DEF_HPP
Tpetra::Classes::Vector::sumIntoGlobalValue
void sumIntoGlobalValue(const GlobalOrdinal globalRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault) const
Add value to existing value, using global (row) index.
Definition: Tpetra_Vector_def.hpp:132
Tpetra::Classes::Vector::sumIntoLocalValue
void sumIntoLocalValue(const LocalOrdinal myRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault) const
Add value to existing value, using local (row) index.
Definition: Tpetra_Vector_def.hpp:149
Tpetra::Classes::Vector::norm2
mag_type norm2() const
Return the two-norm of this Vector.
Definition: Tpetra_Vector_def.hpp:197
Tpetra::Classes::Vector::get1dCopy
void get1dCopy(const Teuchos::ArrayView< Scalar > &A) const
Return multi-vector values in user-provided two-dimensional array (using Teuchos memory management cl...
Definition: Tpetra_Vector_def.hpp:159
Tpetra::Classes::MultiVector::getLocalLength
size_t getLocalLength() const
Local number of rows on the calling process.
Definition: Tpetra_MultiVector_def.hpp:748
Tpetra::Classes::Vector::describe
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.
Definition: Tpetra_Vector_def.hpp:281
Tpetra::Classes::Vector::meanValue
Scalar meanValue() const
Compute mean (average) value of this Vector.
Definition: Tpetra_Vector_def.hpp:177
Tpetra::Classes::Vector::replaceLocalValue
void replaceLocalValue(const LocalOrdinal myRow, const Scalar &value) const
Replace current value at the specified location with specified values.
Definition: Tpetra_Vector_def.hpp:142
Tpetra::Classes::Vector::normInf
mag_type normInf() const
Return the infinity-norm of this Vector.
Definition: Tpetra_Vector_def.hpp:207
Tpetra::Classes::Vector::dot
dot_type dot(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y) const
Return the dot product of this Vector and the input Vector x.
Definition: Tpetra_Vector_def.hpp:167
Tpetra::Classes::MultiVector::dual_view_type
Kokkos::DualView< impl_scalar_type **, Kokkos::LayoutLeft, typename execution_space::execution_space > dual_view_type
Kokkos::DualView specialization used by this class.
Definition: Tpetra_MultiVector_decl.hpp:476
Tpetra::Classes::DistObject< Scalar, LocalOrdinal, GlobalOrdinal, Node >::getMap
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
Definition: Tpetra_DistObject_decl.hpp:510
Tpetra::Classes::Vector::description
virtual std::string description() const
Return a one-line description of this object.
Definition: Tpetra_Vector_def.hpp:274
Tpetra::Classes::Vector::normWeighted
mag_type TPETRA_DEPRECATED normWeighted(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &weights) const
Compute Weighted 2-norm (RMS Norm) of this Vector.
Definition: Tpetra_Vector_def.hpp:218
Tpetra::Classes::DistObject< Scalar, LocalOrdinal, GlobalOrdinal, Node >
Tpetra::Classes::MultiVector::mag_type
Kokkos::Details::ArithTraits< impl_scalar_type >::mag_type mag_type
Type of a norm result.
Definition: Tpetra_MultiVector_decl.hpp:443
Tpetra::Classes::Vector::~Vector
virtual ~Vector()
Destructor.
Definition: Tpetra_Vector_def.hpp:119
Tpetra::Classes::MultiVector
One or more distributed dense vectors.
Definition: Tpetra_MultiVector_decl.hpp:389
Tpetra::Classes::MultiVector::dot_type
Kokkos::Details::InnerProductSpaceTraits< impl_scalar_type >::dot_type dot_type
Type of an inner ("dot") product result.
Definition: Tpetra_MultiVector_decl.hpp:435
Tpetra::createCopy
MultiVector< ST, LO, GO, NT > createCopy(const MultiVector< ST, LO, GO, NT > &src)
Return a deep copy of the given MultiVector.
Definition: Tpetra_MultiVector_def.hpp:5086
Tpetra::Classes::Vector::replaceGlobalValue
void replaceGlobalValue(const GlobalOrdinal globalRow, const Scalar &value) const
Replace current value at the specified location with specified value.
Definition: Tpetra_Vector_def.hpp:125
Tpetra::Classes::Vector::dot_type
base_type::dot_type dot_type
Type of an inner ("dot") product result.
Definition: Tpetra_Vector_decl.hpp:119
Tpetra
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Tpetra::Classes::Vector::norm1
mag_type norm1() const
Return the one-norm of this Vector.
Definition: Tpetra_Vector_def.hpp:187
Tpetra::deep_copy
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.
Definition: Tpetra_MultiVector_decl.hpp:2453
Tpetra::Classes::Vector::Vector
Vector()
Default constructor: makes a Vector with no rows or columns.
Definition: Tpetra_Vector_def.hpp:64
Tpetra::Classes::Vector::mag_type
base_type::mag_type mag_type
Type of a norm result.
Definition: Tpetra_Vector_decl.hpp:127
Tpetra::Classes::Vector
A distributed dense vector.
Definition: Tpetra_Vector_decl.hpp:82