Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Tpetra_Vector_def.hpp
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 
45 #include <Kokkos_DefaultArithmetic.hpp>
46 #include <Kokkos_NodeTrace.hpp>
47 #include <Tpetra_MultiVector.hpp>
48 
49 #ifdef DOXYGEN_USE_ONLY
50  #include "Tpetra_Vector_decl.hpp"
51 #endif
52 
53 namespace Tpetra {
54 
55  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
57  : MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(map,1,zeroOut) {
58  }
59 
60  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
62  : MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(source) {
63  }
64 
65  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
67  const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &map,
68  const ArrayRCP<Scalar> &view, EPrivateHostViewConstructor /* dummy */)
69  : MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(map,view,view.size(),1,HOST_VIEW_CONSTRUCTOR) {
70  }
71 
72  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74  const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &map,
75  const ArrayRCP<Scalar> &view, EPrivateComputeViewConstructor /* dummy */)
76  : MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(map,view,view.size(),1,COMPUTE_VIEW_CONSTRUCTOR) {
77  }
78 
79  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
80  Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::Vector(const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > &map, const ArrayView<const Scalar> &values)
81  : MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(map,values,values.size(),1) {
82  }
83 
84  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
86 
87  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
88  void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::replaceGlobalValue(GlobalOrdinal globalRow, const Scalar &value) {
90  }
91 
92  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
93  void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::sumIntoGlobalValue(GlobalOrdinal globalRow, const Scalar &value) {
95  }
96 
97  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
98  void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::replaceLocalValue(LocalOrdinal myRow, const Scalar &value) {
100  }
101 
102  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
103  void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::sumIntoLocalValue(LocalOrdinal myRow, const Scalar &value) {
105  }
106 
107  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
109  size_t lda = this->getLocalLength();
110  this->get1dCopy(A,lda);
111  }
112 
113  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
115  using Teuchos::outArg;
116 #ifdef HAVE_TPETRA_DEBUG
117  TEUCHOS_TEST_FOR_EXCEPTION( !this->getMap()->isCompatible(*a.getMap()), std::runtime_error,
118  "Tpetra::Vector::dots(): Vectors do not have compatible Maps:" << std::endl
119  << "this->getMap(): " << std::endl << *this->getMap()
120  << "a.getMap(): " << std::endl << *a.getMap() << std::endl);
121 #else
122  TEUCHOS_TEST_FOR_EXCEPTION( this->getLocalLength() != a.getLocalLength(), std::runtime_error,
123  "Tpetra::Vector::dots(): Vectors do not have the same local length.");
124 #endif
125  Scalar gbldot;
126  gbldot = MVT::Dot(this->lclMV_,a.lclMV_);
127  if (this->isDistributed()) {
128  Scalar lcldot = gbldot;
129  Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_SUM,lcldot,outArg(gbldot));
130  }
131  return gbldot;
132  }
133 
134  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
136  using Teuchos::as;
137  using Teuchos::outArg;
138  typedef Teuchos::ScalarTraits<Scalar> SCT;
139 
140  Scalar sum = MVT::Sum(this->lclMV_);
141  if (this->isDistributed()) {
142  Scalar lsum = sum;
143  Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_SUM,lsum,outArg(sum));
144  }
145  // mfh 12 Apr 2012: Don't take out the cast from the ordinal type
146  // to the magnitude type, since operator/ (std::complex<T>, int)
147  // isn't necessarily defined.
148  return sum / as<typename SCT::magnitudeType> (this->getGlobalLength ());
149  }
150 
151  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
152  typename Teuchos::ScalarTraits<Scalar>::magnitudeType Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::norm1() const {
153  using Teuchos::outArg;
154  typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType Mag;
155  Mag norm = MVT::Norm1(this->lclMV_);
156  if (this->isDistributed()) {
157  Mag lnorm = norm;
158  Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_SUM,lnorm,outArg(norm));
159  }
160  return norm;
161  }
162 
163  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
164  typename Teuchos::ScalarTraits<Scalar>::magnitudeType Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::norm2() const {
165  using Teuchos::ScalarTraits;
166  using Teuchos::outArg;
167  typedef typename ScalarTraits<Scalar>::magnitudeType Mag;
168  Mag norm = MVT::Norm2Squared(this->lclMV_);
169  if (this->isDistributed()) {
170  Mag lnorm = norm;
171  Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_SUM,lnorm,outArg(norm));
172  }
173  return ScalarTraits<Mag>::squareroot(norm);
174  }
175 
176  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
177  typename Teuchos::ScalarTraits<Scalar>::magnitudeType Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::normInf() const {
178  using Teuchos::outArg;
179  typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType Mag;
180  Mag norm = MVT::NormInf(this->lclMV_);
181  if (this->isDistributed()) {
182  Mag lnorm = norm;
183  Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_MAX,lnorm,outArg(norm));
184  }
185  return norm;
186  }
187 
188  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
189  typename Teuchos::ScalarTraits<Scalar>::magnitudeType Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::normWeighted(const Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node> &weights) const {
190  using Teuchos::ScalarTraits;
191  using Teuchos::outArg;
192  typedef typename ScalarTraits<Scalar>::magnitudeType Mag;
193 #ifdef HAVE_TPETRA_DEBUG
194  TEUCHOS_TEST_FOR_EXCEPTION( !this->getMap()->isCompatible(*weights.getMap()), std::runtime_error,
195  "Tpetra::Vector::normWeighted(): Vectors do not have compatible Maps:" << std::endl
196  << "this->getMap(): " << std::endl << *this->getMap()
197  << "weights.getMap(): " << std::endl << *weights.getMap() << std::endl);
198 #else
199  TEUCHOS_TEST_FOR_EXCEPTION( this->getLocalLength() != weights.getLocalLength(), std::runtime_error,
200  "Tpetra::Vector::normWeighted(): Vectors do not have the same local length.");
201 #endif
202  Mag norm = MVT::WeightedNorm(this->lclMV_,weights.lclMV_);
203  if (this->isDistributed()) {
204  Mag lnorm = norm;
205  Teuchos::reduceAll(*this->getMap()->getComm(),Teuchos::REDUCE_SUM,lnorm,outArg(norm));
206  }
207  return ScalarTraits<Mag>::squareroot(norm / this->getGlobalLength());
208  }
209 
210  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
212  std::ostringstream oss;
213  oss << Teuchos::Describable::description();
214  oss << "{length="<<this->getGlobalLength()
215  << "}";
216  return oss.str();
217  }
218 
219  template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
220  void Vector<Scalar,LocalOrdinal,GlobalOrdinal,Node>::describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const {
221  using std::endl;
222  using std::setw;
223  using Teuchos::VERB_DEFAULT;
224  using Teuchos::VERB_NONE;
225  using Teuchos::VERB_LOW;
226  using Teuchos::VERB_MEDIUM;
227  using Teuchos::VERB_HIGH;
228  using Teuchos::VERB_EXTREME;
229  Teuchos::EVerbosityLevel vl = verbLevel;
230  if (vl == VERB_DEFAULT) vl = VERB_LOW;
231  RCP<const Teuchos::Comm<int> > comm = this->getMap()->getComm();
232  const int myImageID = comm->getRank(),
233  numImages = comm->getSize();
234  size_t width = 1;
235  for (size_t dec=10; dec<this->getGlobalLength(); dec *= 10) {
236  ++width;
237  }
238  Teuchos::OSTab tab(out);
239  if (vl != VERB_NONE) {
240  // VERB_LOW and higher prints description()
241  if (myImageID == 0) out << this->description() << std::endl;
242  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
243  if (myImageID == imageCtr) {
244  if (vl != VERB_LOW) {
245  // VERB_MEDIUM and higher prints getLocalLength()
246  out << "node " << setw(width) << myImageID << ": local length=" << this->getLocalLength() << endl;
247  if (vl != VERB_MEDIUM) {
248  // VERB_HIGH and higher prints isConstantStride() and stride()
249  if (vl == VERB_EXTREME && this->getLocalLength() > 0) {
250  RCP<Node> node = this->lclMV_.getNode();
251  KOKKOS_NODE_TRACE("Vector::describe()")
252  ArrayRCP<const Scalar> myview = node->template viewBuffer<Scalar>(
253  this->getLocalLength(),
254  MVT::getValues(this->lclMV_) );
255  // VERB_EXTREME prints values
256  for (size_t i=0; i<this->getLocalLength(); ++i) {
257  out << setw(width) << this->getMap()->getGlobalElement(i)
258  << ": "
259  << myview[i] << endl;
260  }
261  myview = Teuchos::null;
262  }
263  }
264  else {
265  out << endl;
266  }
267  }
268  }
269  comm->barrier();
270  }
271  }
272  }
273 
274 } // namespace Tpetra
275 
276 //
277 // Explicit instantiation macro
278 //
279 // Must be expanded from within the Tpetra namespace!
280 //
281 
282 #define TPETRA_VECTOR_INSTANT(SCALAR,LO,GO,NODE) \
283  \
284  template class Vector< SCALAR , LO , GO , NODE >; \
285 
286 
287 #endif // TPETRA_VECTOR_DEF_HPP
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 sumIntoGlobalValue(GlobalOrdinal globalRow, size_t vectorIndex, const Scalar &value)
Add value to existing value, using global (row) index.
Teuchos::ScalarTraits< Scalar >::magnitudeType normWeighted(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &weights) const
Compute Weighted 2-norm (RMS Norm) of this Vector.
size_t getLocalLength() const
Local number of rows on the calling process.
void replaceLocalValue(LocalOrdinal myRow, size_t vectorIndex, const Scalar &value)
Replace value, using local (row) index.
void replaceGlobalValue(GlobalOrdinal globalRow, size_t vectorIndex, const Scalar &value)
Replace value, using global (row) index.
One or more distributed dense vectors.
virtual std::string description() const
A simple one-line description of this object.
void replaceGlobalValue(GlobalOrdinal globalRow, const Scalar &value)
Replace current value at the specified location with specified value.
Teuchos::ScalarTraits< Scalar >::magnitudeType normInf() const
Compute Inf-norm of this Vector.
void sumIntoLocalValue(LocalOrdinal myRow, size_t vectorIndex, const Scalar &value)
Add value to existing value, using local (row) index.
virtual ~Vector()
Destructor.
void replaceLocalValue(LocalOrdinal myRow, const Scalar &value)
Replace current value at the specified location with specified values.
Scalar dot(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &a) const
Computes dot product of this Vector against input Vector x.
const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > & getMap() const
The Map describing the parallel distribution of this object.
void sumIntoGlobalValue(GlobalOrdinal globalRow, const Scalar &value)
Adds specified value to existing value at the specified location.
Teuchos::ScalarTraits< Scalar >::magnitudeType norm2() const
Compute 2-norm of this Vector.
void get1dCopy(ArrayView< Scalar > A) const
Return multi-vector values in user-provided two-dimensional array (using Teuchos memory management cl...
KMV lclMV_
The Kokkos::MultiVector containing the compute buffer of data.
Describes a parallel distribution of objects over processes.
Vector(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, bool zeroOut=true)
Sets all vector entries to zero.
void sumIntoLocalValue(LocalOrdinal myRow, const Scalar &value)
Adds specified value to existing value at the specified location.
A distributed dense vector.
Scalar meanValue() const
Compute mean (average) value of this Vector.
Teuchos::ScalarTraits< Scalar >::magnitudeType norm1() const
Return 1-norm of this Vector.