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 (const GlobalOrdinal globalRow, const Scalar& value) const {
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 (const GlobalOrdinal globalRow,
116  const Scalar& value,
117  const bool atomic) const
118  {
119  this->base_type::sumIntoGlobalValue (globalRow, 0, value, atomic);
120  }
121 
122  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
123  void
125  replaceLocalValue (const LocalOrdinal myRow, const Scalar& value) const {
126  this->base_type::replaceLocalValue (myRow, 0, value);
127  }
128 
129  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
130  void
132  sumIntoLocalValue (const LocalOrdinal globalRow,
133  const Scalar& value,
134  const bool atomic) const
135  {
136  this->base_type::sumIntoLocalValue (globalRow, 0, value, atomic);
137  }
138 
139  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
140  void
142  get1dCopy (const Teuchos::ArrayView<Scalar>& A) const {
143  const size_t lda = this->getLocalLength ();
144  this->get1dCopy (A, lda);
145  }
146 
147  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
151  {
152  dot_type result;
153  this->dot (y, Teuchos::arrayView (&result, 1));
154  return result;
155  }
156 
157  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
158  Scalar
160  meanValue () const
161  {
162  Scalar mean;
163  this->meanValue (Teuchos::arrayView (&mean, 1));
164  return mean;
165  }
166 
167  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
170  norm1 () const
171  {
172  mag_type norm;
173  this->norm1 (Teuchos::arrayView (&norm, 1));
174  return norm;
175  }
176 
177  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
180  norm2 () const
181  {
182  mag_type norm;
183  this->norm2 (Teuchos::arrayView (&norm, 1));
184  return norm;
185  }
186 
187  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
190  normInf () const
191  {
192  mag_type norm;
193  this->normInf (Teuchos::arrayView (&norm, 1));
194  return norm;
195  }
196 
197  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
199  TPETRA_DEPRECATED
202  {
203  mag_type norm;
204  this->normWeighted (weights, Teuchos::arrayView (&norm, 1));
205  return norm;
206  }
207 
208  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
210  description () const
211  {
212  using Teuchos::TypeNameTraits;
213 
214  std::ostringstream out;
215  out << "\"Tpetra::Vector\": {";
216  out << "Template parameters: {Scalar: " << TypeNameTraits<Scalar>::name ()
217  << ", LocalOrdinal: " << TypeNameTraits<LocalOrdinal>::name ()
218  << ", GlobalOrdinal: " << TypeNameTraits<GlobalOrdinal>::name ()
219  << ", Node" << Node::name ()
220  << "}, ";
221  if (this->getObjectLabel () != "") {
222  out << "Label: \"" << this->getObjectLabel () << "\", ";
223  }
224  out << "Global length: " << this->getGlobalLength ();
225  out << "}";
226 
227  return out.str ();
228  }
229 
230  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
232  describe (Teuchos::FancyOStream& out,
233  const Teuchos::EVerbosityLevel verbLevel) const
234  {
235  using std::endl;
236  using std::setw;
237  using Teuchos::VERB_DEFAULT;
238  using Teuchos::VERB_NONE;
239  using Teuchos::VERB_LOW;
240  using Teuchos::VERB_MEDIUM;
241  using Teuchos::VERB_HIGH;
242  using Teuchos::VERB_EXTREME;
243 
244  const Teuchos::EVerbosityLevel vl =
245  (verbLevel == VERB_DEFAULT) ? VERB_LOW : verbLevel;
246  const Teuchos::Comm<int>& comm = * (this->getMap ()->getComm ());
247  const int myImageID = comm.getRank ();
248  const int numImages = comm.getSize ();
249 
250  size_t width = 1;
251  for (size_t dec=10; dec<this->getGlobalLength(); dec *= 10) {
252  ++width;
253  }
254  Teuchos::OSTab tab(out);
255  if (vl != VERB_NONE) {
256  // VERB_LOW and higher prints description()
257  if (myImageID == 0) out << this->description() << std::endl;
258  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
259  if (myImageID == imageCtr) {
260  if (vl != VERB_LOW) {
261  // VERB_MEDIUM and higher prints getLocalLength()
262  out << "Process " << setw(width) << myImageID << ":" << endl;
263  Teuchos::OSTab tab1 (out);
264  const size_t lclNumRows = this->getLocalLength ();
265 
266  out << "Local length: " << lclNumRows << endl;
267  if (vl != VERB_MEDIUM) {
268  // VERB_HIGH and higher prints isConstantStride() and stride()
269  if (vl == VERB_EXTREME && lclNumRows > 0) {
270  // VERB_EXTREME prints values
271  dual_view_type X_lcl = this->getDualView ();
272 
273  // We have to be able to access the data on host in
274  // order to print it.
275  //
276  // FIXME (mfh 06 Mar 2015) For now, just sync to host.
277  // At some point, we might like to check whether the
278  // host execution space can access device memory, so
279  // that we can avoid the sync.
280  typedef typename dual_view_type::t_host::execution_space HES;
281  X_lcl.template sync<HES> ();
282  typename dual_view_type::t_host X_host = X_lcl.h_view;
283  for (size_t i = 0; i < lclNumRows; ++i) {
284  out << setw(width) << this->getMap ()->getGlobalElement (i)
285  << ": " << X_host(i,0) << endl;
286  }
287  }
288  }
289  else {
290  out << endl;
291  }
292  }
293  }
294  comm.barrier ();
295  }
296  }
297  }
298 
299  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
302  {
303  // The 2-argument copy constructor with second argument =
304  // Teuchos::Copy does a deep copy of its input.
306 
307  // The Kokkos refactor version of Vector has view semantics, so
308  // returning the Vector directly, rather than through RCP, only
309  // does a shallow copy.
310  return dst;
311  }
312 
313  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
314  Teuchos::RCP<const Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node, classic> >
316  offsetView (const Teuchos::RCP<const map_type>& subMap,
317  const size_t offset) const
318  {
319  using Kokkos::ALL;
320  using Kokkos::subview;
321  using Teuchos::rcp;
323 
324  const size_t newNumRows = subMap->getNodeNumElements ();
325  const bool tooManyElts = newNumRows + offset > this->getOrigNumLocalRows ();
326  if (tooManyElts) {
327  const int myRank = this->getMap ()->getComm ()->getRank ();
328  TEUCHOS_TEST_FOR_EXCEPTION(
329  newNumRows + offset > this->getLocalLength (), std::runtime_error,
330  "Tpetra::Vector::offsetView(NonConst): Invalid input Map. The input "
331  "Map owns " << newNumRows << " entries on process " << myRank << ". "
332  "offset = " << offset << ". Yet, the Vector contains only "
333  << this->getOrigNumLocalRows () << " rows on this process.");
334  }
335 
336  const std::pair<size_t, size_t> offsetPair (offset, offset + newNumRows);
337  // Need 'this->' to get view_ and origView_ from parent class.
338  return rcp (new V (subMap,
339  subview (this->view_, offsetPair, ALL ()),
340  this->origView_));
341  }
342 
343  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node, const bool classic>
344  Teuchos::RCP<Vector<Scalar, LocalOrdinal, GlobalOrdinal, Node, classic> >
346  offsetViewNonConst (const Teuchos::RCP<const map_type>& subMap,
347  const size_t offset)
348  {
350  return Teuchos::rcp_const_cast<V> (this->offsetView (subMap, offset));
351  }
352 
353 } // namespace Tpetra
354 
363 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
364  template class Vector< SCALAR , LO , GO , NODE >; \
365  template Vector< SCALAR , LO , GO , NODE > createCopy (const Vector< SCALAR , LO , GO , NODE >& src);
366 
367 #endif // TPETRA_VECTOR_DEF_HPP
size_t getLocalLength() const
Local number of rows on the calling process.
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.
void replaceLocalValue(const LocalOrdinal myRow, const Scalar &value) const
Replace current value at the specified location with specified values.
Scalar meanValue() const
Compute mean (average) value 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.
void replaceGlobalValue(GlobalOrdinal globalRow, size_t col, const impl_scalar_type &value) const
Replace value, using global (row) index.
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.
base_type::mag_type mag_type
Type of a norm result.
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 sumIntoGlobalValue(const GlobalOrdinal globalRow, const size_t col, const impl_scalar_type &value, const bool atomic=useAtomicUpdatesByDefault) const
Add value to existing value, using global (row) index.
mag_type TPETRA_DEPRECATED normWeighted(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node, classic > &weights) const
Compute Weighted 2-norm (RMS Norm) of this Vector.
void sumIntoLocalValue(const LocalOrdinal myRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault) const
Add value to existing value, using local (row) index.
void get1dCopy(const Teuchos::ArrayView< Scalar > &A) const
Return multi-vector values in user-provided two-dimensional array (using Teuchos memory management cl...
global_size_t getGlobalLength() const
Global number of rows in the multivector.
size_t getOrigNumLocalRows() const
"Original" number of rows in the (local) data.
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.
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.
void sumIntoGlobalValue(const GlobalOrdinal globalRow, const Scalar &value, const bool atomic=base_type::useAtomicUpdatesByDefault) const
Add value to existing value, using global (row) index.
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.
void replaceGlobalValue(const GlobalOrdinal globalRow, const Scalar &value) const
Replace current value at the specified location with specified value.
void sumIntoLocalValue(const LocalOrdinal localRow, const size_t col, const impl_scalar_type &value, const bool atomic=useAtomicUpdatesByDefault) const
Add value to existing value, using local (row) index.
void replaceLocalValue(LocalOrdinal localRow, size_t col, const impl_scalar_type &value) const
Replace value, using local (row) index.