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 "KokkosCompat_View.hpp"
55 
56 namespace Tpetra {
57 
58  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
60  Vector (const Teuchos::RCP<const map_type>& map,
61  const bool zeroOut)
62  : base_type (map, 1, zeroOut)
63  {}
64 
65  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
68  : base_type (source)
69  {}
70 
71  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
74  const Teuchos::DataAccess copyOrView)
75  : base_type (source, copyOrView)
76  {}
77 
78  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
80  Vector (const Teuchos::RCP<const map_type>& map,
81  const Teuchos::ArrayView<const Scalar>& values)
82  : base_type (map, values, values.size (), 1)
83  {}
84 
85  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
87  Vector (const Teuchos::RCP<const map_type>& map,
88  const dual_view_type& view)
89  : base_type (map, view)
90  {}
91 
92  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
94  Vector (const Teuchos::RCP<const map_type>& map,
95  const dual_view_type& view,
96  const dual_view_type& origView)
97  : base_type (map, view, origView)
98  {}
99 
100  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
103  {}
104 
105  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
106  void
108  replaceGlobalValue (GlobalOrdinal globalRow, const Scalar& value) {
109  this->base_type::replaceGlobalValue (globalRow, 0, value);
110  }
111 
112  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
113  void
115  sumIntoGlobalValue (GlobalOrdinal globalRow, const Scalar& value) {
116  this->base_type::sumIntoGlobalValue (globalRow, 0, value);
117  }
118 
119  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
120  void
122  replaceLocalValue (LocalOrdinal myRow, const Scalar& value) {
123  this->base_type::replaceLocalValue (myRow, 0, value);
124  }
125 
126  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
127  void
129  sumIntoLocalValue (LocalOrdinal myRow, const Scalar& value) {
130  this->base_type::sumIntoLocalValue (myRow, 0, value);
131  }
132 
133  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
134  void
136  get1dCopy (const Teuchos::ArrayView<Scalar>& A) const {
137  const size_t lda = this->getLocalLength ();
138  this->get1dCopy (A, lda);
139  }
140 
141  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
145  {
146  dot_type result;
147  this->dot (y, Teuchos::arrayView (&result, 1));
148  return result;
149  }
150 
151  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
152  Scalar
154  meanValue () const
155  {
156  Scalar mean;
157  this->meanValue (Teuchos::arrayView (&mean, 1));
158  return mean;
159  }
160 
161  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
164  norm1 () const
165  {
166  mag_type norm;
167  this->norm1 (Teuchos::arrayView (&norm, 1));
168  return norm;
169  }
170 
171  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
174  norm2 () const
175  {
176  mag_type norm;
177  this->norm2 (Teuchos::arrayView (&norm, 1));
178  return norm;
179  }
180 
181  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
184  normInf () const
185  {
186  mag_type norm;
187  this->normInf (Teuchos::arrayView (&norm, 1));
188  return norm;
189  }
190 
191  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
195  {
196  mag_type norm;
197  this->normWeighted (weights, Teuchos::arrayView (&norm, 1));
198  return norm;
199  }
200 
201  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
203  description () const
204  {
205  using Teuchos::TypeNameTraits;
206 
207  std::ostringstream out;
208  out << "\"Tpetra::Vector\": {";
209  out << "Template parameters: {Scalar: " << TypeNameTraits<Scalar>::name ()
210  << ", LocalOrdinal: " << TypeNameTraits<LocalOrdinal>::name ()
211  << ", GlobalOrdinal: " << TypeNameTraits<GlobalOrdinal>::name ()
212  << ", Node" << Node::name ()
213  << "}, ";
214  if (this->getObjectLabel () != "") {
215  out << "Label: \"" << this->getObjectLabel () << "\", ";
216  }
217  out << "Global length: " << this->getGlobalLength ();
218  out << "}";
219 
220  return out.str ();
221  }
222 
223  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
225  describe (Teuchos::FancyOStream& out,
226  const Teuchos::EVerbosityLevel verbLevel) const
227  {
228  using std::endl;
229  using std::setw;
230  using Teuchos::VERB_DEFAULT;
231  using Teuchos::VERB_NONE;
232  using Teuchos::VERB_LOW;
233  using Teuchos::VERB_MEDIUM;
234  using Teuchos::VERB_HIGH;
235  using Teuchos::VERB_EXTREME;
236 
237  const Teuchos::EVerbosityLevel vl =
238  (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
239  const Teuchos::Comm<int>& comm = * (this->getMap ()->getComm ());
240  const int myImageID = comm.getRank ();
241  const int numImages = comm.getSize ();
242 
243  size_t width = 1;
244  for (size_t dec=10; dec<this->getGlobalLength(); dec *= 10) {
245  ++width;
246  }
247  Teuchos::OSTab tab(out);
248  if (vl != VERB_NONE) {
249  // VERB_LOW and higher prints description()
250  if (myImageID == 0) out << this->description() << std::endl;
251  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
252  if (myImageID == imageCtr) {
253  if (vl != VERB_LOW) {
254  // VERB_MEDIUM and higher prints getLocalLength()
255  out << "Process " << setw(width) << myImageID << ":" << endl;
256  Teuchos::OSTab tab1 (out);
257  const size_t lclNumRows = this->getLocalLength ();
258 
259  out << "Local length: " << lclNumRows << endl;
260  if (vl != VERB_MEDIUM) {
261  // VERB_HIGH and higher prints isConstantStride() and stride()
262  if (vl == VERB_EXTREME && lclNumRows > 0) {
263  // VERB_EXTREME prints values
264  dual_view_type X_lcl = this->getDualView ();
265 
266  // We have to be able to access the data on host in
267  // order to print it.
268  //
269  // FIXME (mfh 06 Mar 2015) For now, just sync to host.
270  // At some point, we might like to check whether the
271  // host execution space can access device memory, so
272  // that we can avoid the sync.
273  typedef typename dual_view_type::t_host::execution_space HES;
274  X_lcl.template sync<HES> ();
275  typename dual_view_type::t_host X_host = X_lcl.h_view;
276  for (size_t i = 0; i < lclNumRows; ++i) {
277  out << setw(width) << this->getMap ()->getGlobalElement (i)
278  << ": " << X_host(i,0) << endl;
279  }
280  }
281  }
282  else {
283  out << endl;
284  }
285  }
286  }
287  comm.barrier ();
288  }
289  }
290  }
291 
292  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
295  {
296  // The 2-argument copy constructor with second argument =
297  // Teuchos::Copy does a deep copy of its input.
299 
300  // The Kokkos refactor version of Vector has view semantics, so
301  // returning the Vector directly, rather than through RCP, only
302  // does a shallow copy.
303  return dst;
304  }
305 
306  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
307  Teuchos::RCP<const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node, classic> >
309  offsetView (const Teuchos::RCP<const map_type>& subMap,
310  const size_t offset) const
311  {
312  using Kokkos::ALL;
313  using Kokkos::subview;
314  using Teuchos::rcp;
316 
317  const size_t newNumRows = subMap->getNodeNumElements ();
318  const bool tooManyElts = newNumRows + offset > this->getOrigNumLocalRows ();
319  if (tooManyElts) {
320  const int myRank = this->getMap ()->getComm ()->getRank ();
321  TEUCHOS_TEST_FOR_EXCEPTION(
322  newNumRows + offset > this->getLocalLength (), std::runtime_error,
323  "Tpetra::Vector::offsetView(NonConst): Invalid input Map. The input "
324  "Map owns " << newNumRows << " entries on process " << myRank << ". "
325  "offset = " << offset << ". Yet, the Vector contains only "
326  << this->getOrigNumLocalRows () << " rows on this process.");
327  }
328 
329  const std::pair<size_t, size_t> offsetPair (offset, offset + newNumRows);
330  // Need 'this->' to get view_ and origView_ from parent class.
331  return rcp (new V (subMap,
332  subview (this->view_, offsetPair, ALL ()),
333  this->origView_));
334  }
335 
336  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
337  Teuchos::RCP<Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node, classic> >
339  offsetViewNonConst (const Teuchos::RCP<const map_type>& subMap,
340  const size_t offset)
341  {
343  return Teuchos::rcp_const_cast<V> (this->offsetView (subMap, offset));
344  }
345 
346 } // namespace Tpetra
347 
356 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
357  template class Vector< SCALAR , LO , GO , NODE >; \
358  template Vector< SCALAR , LO , GO , NODE > createCopy (const Vector< SCALAR , LO , GO , NODE >& src);
359 
360 #endif // TPETRA_VECTOR_DEF_HPP
size_t getLocalLength() const
Local number of rows on the calling process.
void sumIntoLocalValue(LocalOrdinal localRow, size_t col, const impl_scalar_type &value)
Add value to existing value, using local (row) index.
Vector(const Teuchos::RCP< const map_type > &map, const bool zeroOut=true)
Basic constructor.
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Scalar meanValue() const
Compute mean (average) value of this Vector.
void sumIntoGlobalValue(GlobalOrdinal globalRow, size_t col, const impl_scalar_type &value)
Add value to existing value, using global (row) index.
void replaceLocalValue(LocalOrdinal localRow, size_t col, const impl_scalar_type &value)
Replace value, using local (row) index.
mag_type normWeighted(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node, classic > &weights) const
Compute Weighted 2-norm (RMS Norm) of this Vector.
base_type::dot_type dot_type
Type of an inner ("dot") product result.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to a FancyOStream.
mag_type norm2() const
Compute 2-norm of this Vector.
dual_view_type view_
The Kokkos::DualView containing the MultiVector&#39;s data.
Kokkos::Details::InnerProductSpaceTraits< impl_scalar_type >::dot_type dot_type
Type of an inner ("dot") product result.
void replaceGlobalValue(GlobalOrdinal globalRow, const Scalar &value)
Replace current value at the specified location with specified value.
base_type::mag_type mag_type
Type of a norm result.
void replaceGlobalValue(GlobalOrdinal globalRow, size_t col, const impl_scalar_type &value)
Replace value, using global (row) index.
dual_view_type getDualView() const
Get the Kokkos::DualView which implements local storage.
dot_type dot(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node, classic > &y) const
Computes dot product of this Vector against input Vector x.
virtual ~Vector()
Destructor.
void get1dCopy(const Teuchos::ArrayView< Scalar > &A) const
Return multi-vector values in user-provided two-dimensional array (using Teuchos memory management cl...
void sumIntoGlobalValue(GlobalOrdinal globalRow, const Scalar &value)
Adds specified value to existing value at the specified location.
global_size_t getGlobalLength() const
Global number of rows in the multivector.
size_t getOrigNumLocalRows() const
"Original" number of rows in the (local) data.
void replaceLocalValue(LocalOrdinal myRow, const Scalar &value)
Replace current value at the specified location with specified values.
Kokkos::Details::ArithTraits< impl_scalar_type >::mag_type mag_type
Type of a norm result.
mag_type normInf() const
Compute Inf-norm of this Vector.
Kokkos::DualView< impl_scalar_type **, Kokkos::LayoutLeft, typename execution_space::execution_space > dual_view_type
Kokkos::DualView specialization used by this class.
void sumIntoLocalValue(LocalOrdinal myRow, const Scalar &value)
Adds specified value to existing value at the specified location.
Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node, classic > createCopy(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node, classic > &src)
Return a deep copy of the given Vector.
A distributed dense vector.
virtual Teuchos::RCP< const map_type > getMap() const
The Map describing the parallel distribution of this object.
virtual std::string description() const
A simple one-line description of this object.
mag_type norm1() const
Return 1-norm of this Vector.
dual_view_type origView_
The "original view" of the MultiVector&#39;s data.