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 
47 
48 #include "Tpetra_MultiVector.hpp"
50 #include "KokkosCompat_View.hpp"
51 #include "KokkosBlas1_nrm2w_squared.hpp"
52 #include "Teuchos_CommHelpers.hpp"
53 
54 namespace Tpetra {
55 
56  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
58  Vector ()
59  : base_type ()
60  {}
61 
62  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
64  Vector (const Teuchos::RCP<const map_type>& map,
65  const bool zeroOut)
66  : base_type (map, 1, zeroOut)
67  {}
68 
69  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
72  const Teuchos::DataAccess copyOrView)
73  : base_type (source, copyOrView)
74  {}
75 
76  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
79  const Teuchos::RCP<const map_type>& map,
80  const local_ordinal_type rowOffset) :
81  base_type (source, map, rowOffset)
82  {}
83 
84  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
86  Vector (const Teuchos::RCP<const map_type>& map,
87  const Teuchos::ArrayView<const Scalar>& values)
88  : base_type (map, values, values.size (), 1)
89  {}
90 
91  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
93  Vector (const Teuchos::RCP<const map_type>& map,
94  const dual_view_type& view)
95  : base_type (map, view)
96  {}
97 
98  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
100  Vector (const Teuchos::RCP<const map_type>& map,
101  const dual_view_type& view,
102  const dual_view_type& origView)
103  : base_type (map, view, origView)
104  {}
105 
106  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
109  const size_t j)
110  : base_type (X, j)
111  {}
112 
113  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
114  void
116  replaceGlobalValue (const GlobalOrdinal globalRow, const Scalar& value) {
117  this->base_type::replaceGlobalValue (globalRow, 0, value);
118  }
119 
120  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
121  void
123  sumIntoGlobalValue (const GlobalOrdinal globalRow,
124  const Scalar& value,
125  const bool atomic)
126  {
127  this->base_type::sumIntoGlobalValue (globalRow, 0, value, atomic);
128  }
129 
130  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
131  void
133  replaceLocalValue (const LocalOrdinal myRow, const Scalar& value) {
134  this->base_type::replaceLocalValue (myRow, 0, value);
135  }
136 
137  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
138  void
140  sumIntoLocalValue (const LocalOrdinal globalRow,
141  const Scalar& value,
142  const bool atomic)
143  {
144  this->base_type::sumIntoLocalValue (globalRow, 0, value, atomic);
145  }
146 
147  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
148  void
150  get1dCopy (const Teuchos::ArrayView<Scalar>& A) const {
151  const size_t lda = this->getLocalLength ();
152  this->get1dCopy (A, lda);
153  }
154 
155  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
159  {
160  dot_type result;
161  this->dot (y, Teuchos::arrayView (&result, 1));
162  return result;
163  }
164 
165  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
166  Scalar
168  meanValue () const
169  {
170  Scalar mean;
171  this->meanValue (Teuchos::arrayView (&mean, 1));
172  return mean;
173  }
174 
175  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
178  norm1 () const
179  {
180  mag_type norm;
181  this->norm1 (Teuchos::arrayView (&norm, 1));
182  return norm;
183  }
184 
185  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
188  norm2 () const
189  {
190  mag_type norm;
191  this->norm2 (Teuchos::arrayView (&norm, 1));
192  return norm;
193  }
194 
195  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
198  normInf () const
199  {
200  mag_type norm;
201  this->normInf (Teuchos::arrayView (&norm, 1));
202  return norm;
203  }
204 
205 
206  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
208  description () const
209  {
210  return this->descriptionImpl ("Tpetra::Vector");
211  }
212 
213  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
215  describe (Teuchos::FancyOStream& out,
216  const Teuchos::EVerbosityLevel verbLevel) const
217  {
218  this->describeImpl (out, "Tpetra::Vector", verbLevel);
219  }
220 
221  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
222  Teuchos::RCP<const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
224  offsetView (const Teuchos::RCP<const map_type>& subMap,
225  const size_t offset) const
226  {
227  using Kokkos::ALL;
228  using Kokkos::subview;
229  using Teuchos::rcp;
231 
232  const size_t newNumRows = subMap->getNodeNumElements ();
233  const bool tooManyElts = newNumRows + offset > this->getOrigNumLocalRows ();
234  if (tooManyElts) {
235  const int myRank = this->getMap ()->getComm ()->getRank ();
236  TEUCHOS_TEST_FOR_EXCEPTION(
237  newNumRows + offset > this->getLocalLength (), std::runtime_error,
238  "Tpetra::Vector::offsetView(NonConst): Invalid input Map. The input "
239  "Map owns " << newNumRows << " entries on process " << myRank << ". "
240  "offset = " << offset << ". Yet, the Vector contains only "
241  << this->getOrigNumLocalRows () << " rows on this process.");
242  }
243 
244  const std::pair<size_t, size_t> offsetPair (offset, offset + newNumRows);
245  // Need 'this->' to get view_ and origView_ from parent class.
246  return rcp (new V (subMap,
247  subview (this->view_, offsetPair, ALL ()),
248  this->origView_));
249  }
250 
251  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
252  Teuchos::RCP<Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
253  Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
254  offsetViewNonConst (const Teuchos::RCP<const map_type>& subMap,
255  const size_t offset)
256  {
257  typedef Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node> V;
258  return Teuchos::rcp_const_cast<V> (this->offsetView (subMap, offset));
259  }
260 
261  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
262  Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node>
264  {
266  // The 2-argument copy constructor with second argument =
267  // Teuchos::Copy does a deep copy of its input.
268  vec_type dst (src, Teuchos::Copy);
269 
270  // The Kokkos refactor version of Vector has view semantics, so
271  // returning the Vector directly, rather than through RCP, only
272  // does a shallow copy.
273  return dst;
274  }
275 
276 } // namespace Tpetra
277 
286 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
287  template class Vector< SCALAR , LO , GO , NODE >; \
288  template Vector< SCALAR , LO , GO , NODE > createCopy (const Vector< SCALAR , LO , GO , NODE >& src);
289 
290 #endif // TPETRA_VECTOR_DEF_HPP
Declaration of a function that prints strings from each process.
Base class for distributed Tpetra objects that support data redistribution.
One or more distributed dense vectors.
typename map_type::local_ordinal_type local_ordinal_type
The type of local indices that this class uses.
typename Kokkos::Details::InnerProductSpaceTraits< impl_scalar_type >::dot_type dot_type
Type of an inner ("dot") product result.
Kokkos::DualView< impl_scalar_type **, Kokkos::LayoutLeft, device_type > dual_view_type
Kokkos::DualView specialization used by this class.
typename Kokkos::ArithTraits< impl_scalar_type >::mag_type mag_type
Type of a norm result.
A distributed dense vector.
void replaceGlobalValue(const GlobalOrdinal globalRow, const Scalar &value)
Replace current value at the specified location with specified value.
void sumIntoLocalValue(const LocalOrdinal myRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault)
Add value to existing value, using local (row) index.
mag_type normInf() const
Return the infinity-norm of this Vector.
Scalar meanValue() const
Compute mean (average) value of this Vector.
base_type::mag_type mag_type
Type of a norm result.
void replaceLocalValue(const LocalOrdinal myRow, const Scalar &value)
Replace current value at the specified location with specified values.
mag_type norm2() const
Return the two-norm of this Vector.
base_type::dot_type dot_type
Type of an inner ("dot") product result.
void sumIntoGlobalValue(const GlobalOrdinal globalRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault)
Add value to existing value, using global (row) index.
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.
Vector()
Default constructor: makes a Vector with no rows or columns.
virtual std::string description() const
Return a one-line description of this object.
void get1dCopy(const Teuchos::ArrayView< Scalar > &A) const
Return multi-vector values in user-provided two-dimensional array (using Teuchos memory management cl...
dot_type dot(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &y) const
Return the dot product of this Vector and the input Vector x.
mag_type norm1() const
Return the one-norm of this Vector.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
MultiVector< ST, LO, GO, NT > createCopy(const MultiVector< ST, LO, GO, NT > &src)
Return a deep copy of the given MultiVector.