Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends 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"
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 
211  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
213  description () const
214  {
215  return this->descriptionImpl ("Tpetra::Vector");
216  }
217 
218  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
220  describe (Teuchos::FancyOStream& out,
221  const Teuchos::EVerbosityLevel verbLevel) const
222  {
223  this->describeImpl (out, "Tpetra::Vector", verbLevel);
224  }
225 
226  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
227  Teuchos::RCP<const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
229  offsetView (const Teuchos::RCP<const map_type>& subMap,
230  const size_t offset) const
231  {
232  using Kokkos::ALL;
233  using Kokkos::subview;
234  using Teuchos::rcp;
236 
237  const size_t newNumRows = subMap->getNodeNumElements ();
238  const bool tooManyElts = newNumRows + offset > this->getOrigNumLocalRows ();
239  if (tooManyElts) {
240  const int myRank = this->getMap ()->getComm ()->getRank ();
241  TEUCHOS_TEST_FOR_EXCEPTION(
242  newNumRows + offset > this->getLocalLength (), std::runtime_error,
243  "Tpetra::Vector::offsetView(NonConst): Invalid input Map. The input "
244  "Map owns " << newNumRows << " entries on process " << myRank << ". "
245  "offset = " << offset << ". Yet, the Vector contains only "
246  << this->getOrigNumLocalRows () << " rows on this process.");
247  }
248 
249  const std::pair<size_t, size_t> offsetPair (offset, offset + newNumRows);
250  // Need 'this->' to get view_ and origView_ from parent class.
251  return rcp (new V (subMap,
252  subview (this->view_, offsetPair, ALL ()),
253  this->origView_));
254  }
255 
256  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
257  Teuchos::RCP<Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
258  Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
259  offsetViewNonConst (const Teuchos::RCP<const map_type>& subMap,
260  const size_t offset)
261  {
262  typedef Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> V;
263  return Teuchos::rcp_const_cast<V> (this->offsetView (subMap, offset));
264  }
265 
266  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
267  Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>
269  {
271  // The 2-argument copy constructor with second argument =
272  // Teuchos::Copy does a deep copy of its input.
273  vec_type dst (src, Teuchos::Copy);
274 
275  // The Kokkos refactor version of Vector has view semantics, so
276  // returning the Vector directly, rather than through RCP, only
277  // does a shallow copy.
278  return dst;
279  }
280 
281 } // namespace Tpetra
282 
291 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
292  template class Vector< SCALAR , LO , GO , NODE >; \
293  template Vector< SCALAR , LO , GO , NODE > createCopy (const Vector< SCALAR , LO , GO , NODE >& src);
294 
295 #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.
Declaration of a function that prints strings from each 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 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.
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.