Xpetra_EpetraVector.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Xpetra: A linear algebra interface package
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jeremie Gaidamour (jngaida@sandia.gov)
40 // Jonathan Hu (jhu@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #include "Xpetra_EpetraVector.hpp"
47 
48 //TODO: replace double -> Scalar etc.
49 
50 namespace Xpetra {
51 
52  EpetraVector::EpetraVector(const Teuchos::RCP<const Map<int,int> > &map, bool zeroOut) : EpetraMultiVector(map,1,zeroOut) { }
53 
54  void EpetraVector::replaceGlobalValue(GlobalOrdinal globalRow, const Scalar &value) { XPETRA_MONITOR("EpetraVector::replaceGlobalValue"); getEpetra_MultiVector()->ReplaceGlobalValue(globalRow, 0, value); }
55 
56  void EpetraVector::sumIntoGlobalValue(GlobalOrdinal globalRow, const Scalar &value) { XPETRA_MONITOR("EpetraVector::sumIntoGlobalValue");getEpetra_MultiVector()->SumIntoGlobalValue(globalRow, 0, value); }
57 
58  void EpetraVector::replaceLocalValue(LocalOrdinal myRow, const Scalar &value) { XPETRA_MONITOR("EpetraVector::replaceLocalValue");getEpetra_MultiVector()->ReplaceMyValue(myRow, 0, value); }
59 
60  void EpetraVector::sumIntoLocalValue(LocalOrdinal myRow, const Scalar &value) { XPETRA_MONITOR("EpetraVector::sumIntoLocalValue");getEpetra_MultiVector()->SumIntoMyValue(myRow, 0, value); }
61 
63  XPETRA_MONITOR("EpetraVector::dot");
64 
65  XPETRA_DYNAMIC_CAST(const EpetraVector, a, tA, "This Xpetra::EpetraVector method only accept Xpetra::EpetraVector as input arguments.");
66  // return getEpetra_Vector()->Dot(*tA.getEpetra_Vector());
67 
68  // other way: use the MultiVector Dot instead of VectorDot:
69  double r;
70  getEpetra_MultiVector()->Epetra_MultiVector::Dot(*tA.getEpetra_MultiVector(), &r);
71  return r;
72  }
73 
74  Teuchos::ScalarTraits<double>::magnitudeType EpetraVector::norm1() const { XPETRA_MONITOR("EpetraVector::norm1"); double r; getEpetra_MultiVector()->Norm1(&r); return r; }
75 
76  Teuchos::ScalarTraits<double>::magnitudeType EpetraVector::norm2() const { XPETRA_MONITOR("EpetraVector::norm2"); double r; getEpetra_MultiVector()->Norm2(&r); return r; }
77 
78  Teuchos::ScalarTraits<double>::magnitudeType EpetraVector::normInf() const { XPETRA_MONITOR("EpetraVector::normInf"); double r; getEpetra_MultiVector()->NormInf(&r); return r; }
79 
80  Teuchos::ScalarTraits<double>::magnitudeType EpetraVector::normWeighted(const Vector<double,int,int,Kokkos::DefaultNode::DefaultNodeType> &weights) const {
81  XPETRA_MONITOR("EpetraVector::normWeighted");
82  XPETRA_DYNAMIC_CAST(const EpetraVector, weights, tWeights, "This Xpetra::EpetraVector method only accept Xpetra::EpetraVector as input arguments.");
83  double r;
84  getEpetra_MultiVector()->NormWeighted(*tWeights.getEpetra_MultiVector(), &r); return r;
85  }
86 
87  double EpetraVector::meanValue() const { XPETRA_MONITOR("EpetraVector::meanValue"); double r; getEpetra_MultiVector()->MeanValue(&r); return r; }
88 
89  std::string EpetraVector::description() const {
90  XPETRA_MONITOR("EpetraVector::description");
91  // This implementation come from Epetra_Vector_def.hpp (without modification)
92  std::ostringstream oss;
93  oss << Teuchos::Describable::description();
94  oss << "{length="<<this->getGlobalLength()
95  << "}";
96  return oss.str();
97  }
98 
99  void EpetraVector::describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const {
100  XPETRA_MONITOR("EpetraVector::describe");
101 
102  if (verbLevel > Teuchos::VERB_NONE)
103  getEpetra_Vector()->Print(out);
104 
105 // typedef Kokkos::MultiVector<double> KMV;
106 // typedef Kokkos::DefaultArithmetic<KMV> MVT;
107 
108 // // This implementation come from Tpetra_Vector_def.hpp (without modification)
109 // using std::endl;
110 // using std::setw;
111 // using Teuchos::VERB_DEFAULT;
112 // using Teuchos::VERB_NONE;
113 // using Teuchos::VERB_LOW;
114 // using Teuchos::VERB_MEDIUM;
115 // using Teuchos::VERB_HIGH;
116 // using Teuchos::VERB_EXTREME;
117 // Teuchos::EVerbosityLevel vl = verbLevel;
118 
119 // TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::NotImplemented, "TODO");
120 
121 // if (vl == VERB_DEFAULT) vl = VERB_LOW;
122 // Teuchos::RCP<const Teuchos::Comm<int> > comm = this->getMap()->getComm();
123 // const int myImageID = comm->getRank(),
124 // numImages = comm->getSize();
125 // size_t width = 1;
126 // for (size_t dec=10; dec<this->getGlobalLength(); dec *= 10) {
127 // ++width;
128 // }
129 // Teuchos::OSTab tab(out);
130 // if (vl != VERB_NONE) {
131 // // VERB_LOW and higher prints description()
132 // if (myImageID == 0) out << this->description() << std::endl;
133 // for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
134 // if (myImageID == imageCtr) {
135 // if (vl != VERB_LOW) {
136 // // VERB_MEDIUM and higher prints getLocalLength()
137 // out << "node " << setw(width) << myImageID << ": local length=" << this->getLocalLength() << endl;
138 // if (vl != VERB_MEDIUM) {
139 // // VERB_HIGH and higher prints isConstantStride() and stride()
140 // if (vl == VERB_EXTREME && this->getLocalLength() > 0) {
141 // Teuchos::RCP<Node> node = this->lclMV_.getNode();
142 // KOKKOS_NODE_TRACE("Vector::describe()")
143 // Teuchos::ArrayRCP<const double> myview = node->template viewBuffer<double>(
144 // this->getLocalLength(),
145 // MVT::getValues(this->lclMV_) );
146 // // VERB_EXTREME prints values
147 // for (size_t i=0; i<this->getLocalLength(); ++i) {
148 // out << setw(width) << this->getMap()->getGlobalElement(i)
149 // << ": "
150 // << myview[i] << endl;
151 // }
152 // myview = Teuchos::null;
153 // }
154 // }
155 // else {
156 // out << endl;
157 // }
158 // }
159 // }
160 // }
161 // }
162  }
163 
164  EpetraVector::EpetraVector(const RCP<Epetra_MultiVector> &mv, size_t j)
165  : EpetraMultiVector(rcp((*mv)(j), false)), // view of the vector number j. false == I do not own the data.
166  internalRefToBaseMV_(mv) // keep an internal reference to the initial MultiVector to avoid desallocation of the view.
167  {
168  // The view of the internal data of 'mv' is only valid until the destruction of 'mv'.
169  // The new vector hold an internal reference to 'mv' in order to keep the view valid after disappearance of 'mv' references in user code.
170  // This implements the logic of subArray rcp (as required by the Tpetra interface).
171  }
172 
173  // TODO: move that elsewhere
174  Epetra_Vector & toEpetra(Vector<double, int, int> &x) {
175  XPETRA_DYNAMIC_CAST( EpetraVector, x, tX, "toEpetra");
176  return *tX.getEpetra_Vector();
177  }
178 
179  const Epetra_Vector & toEpetra(const Vector<double, int, int> &x) {
180  XPETRA_DYNAMIC_CAST(const EpetraVector, x, tX, "toEpetra");
181  return *tX.getEpetra_Vector();
182  }
183  //
184 
185 }
EpetraVector(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, bool zeroOut=true)
Sets all vector entries to zero.
Teuchos::ScalarTraits< Scalar >::magnitudeType normInf() const
Compute Inf-norm of this Vector.
Xpetra namespace
RCP< Epetra_MultiVector > getEpetra_MultiVector() const
Get the underlying Epetra multivector.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, int > > &graph)
void sumIntoGlobalValue(GlobalOrdinal globalRow, const Scalar &value)
Adds specified value to existing value at the specified location.
Teuchos::ScalarTraits< Scalar >::magnitudeType norm1() const
Return 1-norm of this Vector.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with some verbosity level to an FancyOStream object.
std::string description() const
Return a simple one-line description of this object.
Teuchos::ScalarTraits< Scalar >::magnitudeType norm2() const
Compute 2-norm of this Vector.
Scalar meanValue() const
Compute mean (average) value of this Vector.
Epetra_Vector * getEpetra_Vector() const
Get the underlying Epetra vector.
#define XPETRA_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
void replaceGlobalValue(GlobalOrdinal globalRow, const Scalar &value)
Replace current value at the specified location with specified value.
global_size_t getGlobalLength() const
Global number of rows in the multivector.
#define XPETRA_MONITOR(funcName)
void sumIntoLocalValue(LocalOrdinal myRow, const Scalar &value)
Adds specified value to existing value at the specified location.
void replaceLocalValue(LocalOrdinal myRow, const Scalar &value)
Replace current value at the specified location with specified values.
Teuchos::ScalarTraits< Scalar >::magnitudeType normWeighted(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &weights) const
Compute Weighted 2-norm (RMS Norm) of this Vector.
Scalar dot(const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &a) const
Computes dot product of this Vector against input Vector x.
const RCP< const Epetra_MultiVector > internalRefToBaseMV_