Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 
61  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
64  : base_type ()
65  {}
66 
67  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
69  Vector (const Teuchos::RCP<const map_type>& map,
70  const bool zeroOut)
71  : base_type (map, 1, zeroOut)
72  {}
73 
74  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
77  const Teuchos::DataAccess copyOrView)
78  : base_type (source, copyOrView)
79  {}
80 
81  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
84  const Teuchos::RCP<const map_type>& map,
85  const local_ordinal_type rowOffset) :
86  base_type (source, map, rowOffset)
87  {}
88 
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)
94  {}
95 
96  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
98  Vector (const Teuchos::RCP<const map_type>& map,
99  const dual_view_type& view)
100  : base_type (map, view)
101  {}
102 
103  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
105  Vector (const Teuchos::RCP<const map_type>& map,
106  const dual_view_type& view,
107  const dual_view_type& origView)
108  : base_type (map, view, origView)
109  {}
110 
111  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
114  const size_t j)
115  : base_type (X, j)
116  {}
117 
118  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
119  void
121  replaceGlobalValue (const GlobalOrdinal globalRow, const Scalar& value) const {
122  this->base_type::replaceGlobalValue (globalRow, 0, value);
123  }
124 
125  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
126  void
128  sumIntoGlobalValue (const GlobalOrdinal globalRow,
129  const Scalar& value,
130  const bool atomic) const
131  {
132  this->base_type::sumIntoGlobalValue (globalRow, 0, value, atomic);
133  }
134 
135  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
136  void
138  replaceLocalValue (const LocalOrdinal myRow, const Scalar& value) const {
139  this->base_type::replaceLocalValue (myRow, 0, value);
140  }
141 
142  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
143  void
145  sumIntoLocalValue (const LocalOrdinal globalRow,
146  const Scalar& value,
147  const bool atomic) const
148  {
149  this->base_type::sumIntoLocalValue (globalRow, 0, value, atomic);
150  }
151 
152  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
153  void
155  get1dCopy (const Teuchos::ArrayView<Scalar>& A) const {
156  const size_t lda = this->getLocalLength ();
157  this->get1dCopy (A, lda);
158  }
159 
160  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
164  {
165  dot_type result;
166  this->dot (y, Teuchos::arrayView (&result, 1));
167  return result;
168  }
169 
170  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
171  Scalar
173  meanValue () const
174  {
175  Scalar mean;
176  this->meanValue (Teuchos::arrayView (&mean, 1));
177  return mean;
178  }
179 
180  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
183  norm1 () const
184  {
185  mag_type norm;
186  this->norm1 (Teuchos::arrayView (&norm, 1));
187  return norm;
188  }
189 
190  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
193  norm2 () const
194  {
195  mag_type norm;
196  this->norm2 (Teuchos::arrayView (&norm, 1));
197  return norm;
198  }
199 
200  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
203  normInf () const
204  {
205  mag_type norm;
206  this->normInf (Teuchos::arrayView (&norm, 1));
207  return norm;
208  }
209 
210 #ifdef TPETRA_ENABLE_DEPRECATED_CODE
211  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
213  TPETRA_DEPRECATED
216  {
217  using Kokkos::ALL;
218  using Kokkos::subview;
219  using Teuchos::Comm;
220  using Teuchos::RCP;
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; // just one
226  const char tfecfFuncName[] = "normWeighted: ";
227 
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);
234 #else
235  const size_t lclNumRows = this->getLocalLength ();
236  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(
237  lclNumRows != weights.getLocalLength (), std::runtime_error,
238  "Vectors do not have the same local length.");
239 #endif // HAVE_TPETRA_DEBUG
240 
241  norm_view_type lclNrm ("lclNrm"); // local norm
242  mag_type gblNrm = ATM::zero (); // return value
243 
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 ();
253 
254  if (! comm.is_null () && this->isDistributed ()) {
255  // Assume that MPI can access device memory.
256  reduceAll<int, mag_type> (*comm, REDUCE_SUM, 1, lclNrm.data (),
257  &gblNrm);
258  gblNrm = ATM::sqrt (gblNrm * OneOverN);
259  }
260  else {
261  auto lclNrm_h = Kokkos::create_mirror_view (lclNrm);
262  Kokkos::deep_copy (lclNrm_h, lclNrm);
263  gblNrm = ATM::sqrt (ATS::magnitude (lclNrm_h()) * OneOverN);
264  }
265 
266  return gblNrm;
267  }
268 #endif // TPETRA_ENABLE_DEPRECATED_CODE
269 
270  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
272  description () const
273  {
274  return this->descriptionImpl ("Tpetra::Vector");
275  }
276 
277  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
279  describe (Teuchos::FancyOStream& out,
280  const Teuchos::EVerbosityLevel verbLevel) const
281  {
282  this->describeImpl (out, "Tpetra::Vector", verbLevel);
283  }
284 
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
290  {
291  using Kokkos::ALL;
292  using Kokkos::subview;
293  using Teuchos::rcp;
295 
296  const size_t newNumRows = subMap->getNodeNumElements ();
297  const bool tooManyElts = newNumRows + offset > this->getOrigNumLocalRows ();
298  if (tooManyElts) {
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.");
306  }
307 
308  const std::pair<size_t, size_t> offsetPair (offset, offset + newNumRows);
309  // Need 'this->' to get view_ and origView_ from parent class.
310  return rcp (new V (subMap,
311  subview (this->view_, offsetPair, ALL ()),
312  this->origView_));
313  }
314 
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,
319  const size_t offset)
320  {
321  typedef Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> V;
322  return Teuchos::rcp_const_cast<V> (this->offsetView (subMap, offset));
323  }
324 
325  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
326  Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>
328  {
330  // The 2-argument copy constructor with second argument =
331  // Teuchos::Copy does a deep copy of its input.
332  vec_type dst (src, Teuchos::Copy);
333 
334  // The Kokkos refactor version of Vector has view semantics, so
335  // returning the Vector directly, rather than through RCP, only
336  // does a shallow copy.
337  return dst;
338  }
339 
340 } // namespace Tpetra
341 
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);
353 
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 (&quot;dot&quot;) 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 (&quot;dot&quot;) 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.