All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Xpetra_EpetraMultiVector.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
47 
48 #include "Xpetra_EpetraImport.hpp"
49 #include "Xpetra_EpetraExport.hpp"
50 #include "Xpetra_Exceptions.hpp"
51 
52 #include "Xpetra_EpetraVector.hpp"
53 
54 #include "Epetra_SerialComm.h"
55 
56 namespace Xpetra {
57 
59  //TODO: input argument 'NumVectors' is not necessary in both Xpetra and Tpetra interface. Should it be removed?
60 
61  const std::string tfecfFuncName("MultiVector(ArrayOfPtrs)");
62  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(NumVectors < 1 || NumVectors != Teuchos::as<size_t>(ArrayOfPtrs.size()), std::runtime_error,
63  ": ArrayOfPtrs.size() must be strictly positive and as large as ArrayOfPtrs.");
64 
65 #ifdef HAVE_XPETRA_DEBUG
66  // This cannot be tested by Epetra itself
67  {
68  size_t localLength = map->getNodeNumElements();
69  for(int j=0; j<ArrayOfPtrs.size(); j++) {
70  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(Teuchos::as<size_t>(ArrayOfPtrs[j].size()) != localLength, std::runtime_error,
71  ": ArrayOfPtrs[" << j << "].size() (== " << ArrayOfPtrs[j].size() <<
72  ") is not equal to getLocalLength() (== " << localLength);
73 
74  }
75  }
76 #endif
77 
78  // Convert Teuchos::ArrayView< const Teuchos::ArrayView< const Scalar > > to double**
79  Array<const double*> arrayOfRawPtrs(ArrayOfPtrs.size());
80  for(int i=0; i<ArrayOfPtrs.size(); i++) {
81  arrayOfRawPtrs[i] = ArrayOfPtrs[i].getRawPtr();
82  }
83  double** rawArrayOfRawPtrs = const_cast<double**>(arrayOfRawPtrs.getRawPtr()); // This const_cast should be fine, because Epetra_DataAccess=Copy.
84 
85  vec_ = Teuchos::rcp(new Epetra_MultiVector(Copy, toEpetra(map), rawArrayOfRawPtrs, NumVectors));
86  }
87 
88 
90  XPETRA_MONITOR("EpetraMultiVector::getVector");
91  return rcp(new EpetraVector(vec_, j)); // See constructor EpetraVector(const RCP<EpetraMultiVector> &mv, size_t j) for more info
92  }
93 
95  XPETRA_MONITOR("EpetraMultiVector::getVector");
96  return rcp(new EpetraVector(vec_, j)); // See constructor EpetraVector(const RCP<EpetraMultiVector> &mv, size_t j) for more info
97  }
98 
100  XPETRA_MONITOR("EpetraMultiVector::getData");
101 
102  double ** arrayOfPointers;
103 
104  vec_->ExtractView(&arrayOfPointers);
105 
106  double * data = arrayOfPointers[j];
107  int localLength = vec_->MyLength();
108 
109  return ArrayRCP<double>(data, 0, localLength, false); // no ownership
110  }
111 
113  XPETRA_MONITOR("EpetraMultiVector::getDataNonConst");
114 
115  double ** arrayOfPointers;
116 
117  vec_->ExtractView(&arrayOfPointers);
118 
119  double * data = arrayOfPointers[j];
120  int localLength = vec_->MyLength();
121 
122  return ArrayRCP<double>(data, 0, localLength, false); // no ownership
123  }
124 
126  XPETRA_MONITOR("EpetraMultiVector::dot");
127 
128  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, A, eA, "This Xpetra::EpetraMultiVector method only accept Xpetra::EpetraMultiVector as input arguments.");
129  vec_->Dot(*eA.getEpetra_MultiVector(), dots.getRawPtr());
130  }
131 
132  void EpetraMultiVector::norm1(const Teuchos::ArrayView< Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const { XPETRA_MONITOR("EpetraMultiVector::norm1"); vec_->Norm1(norms.getRawPtr()); }
133 
134  void EpetraMultiVector::norm2(const Teuchos::ArrayView< Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const { XPETRA_MONITOR("EpetraMultiVector::norm2"); vec_->Norm2(norms.getRawPtr()); }
135 
136  void EpetraMultiVector::normInf(const Teuchos::ArrayView< Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const { XPETRA_MONITOR("EpetraMultiVector::normInf"); vec_->NormInf(norms.getRawPtr()); }
137 
139  XPETRA_MONITOR("EpetraMultiVector::normWeighted");
140 
141  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, weights, eWeights, "This Xpetra::EpetraMultiVector method only accept Xpetra::EpetraMultiVector as input arguments.");
142  vec_->NormWeighted(*eWeights.getEpetra_MultiVector(), norms.getRawPtr());
143  }
144 
145  void EpetraMultiVector::meanValue(const Teuchos::ArrayView<double> &means) const { XPETRA_MONITOR("EpetraMultiVector::meanValue"); vec_->MeanValue(means.getRawPtr()); } //TODO: modify ArrayView size ??
146 
147  std::string EpetraMultiVector::description() const {
148  XPETRA_MONITOR("EpetraMultiVector::description");
150  return "TODO";
151  }
152 
154  XPETRA_MONITOR("EpetraMultiVector::describe");
155  vec_->Print(out);
156  }
157 
159  XPETRA_MONITOR("EpetraMultiVector::doImport");
160 
161  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, source, tSource, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraMultiVector as input arguments.");
162  XPETRA_DYNAMIC_CAST(const EpetraImport, importer, tImporter, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraImport as input arguments.");
163 
164  RCP<Epetra_MultiVector> v = tSource.getEpetra_MultiVector();
165  int err = this->getEpetra_MultiVector()->Import(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
166  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
167  }
168 
169  void EpetraMultiVector::doExport(const DistObject<double, int, int> &dest, const Import<int, int>& importer, CombineMode CM) {
170  XPETRA_MONITOR("EpetraMultiVector::doExport");
171 
172  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, dest, tDest, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraMultiVector as input arguments.");
173  XPETRA_DYNAMIC_CAST(const EpetraImport, importer, tImporter, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraImport as input arguments.");
174 
175  RCP<Epetra_MultiVector> v = tDest.getEpetra_MultiVector();
176  int err = this->getEpetra_MultiVector()->Export(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
177  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
178  }
179 
180  void EpetraMultiVector::doImport(const DistObject<double,int,int> &source, const Export<int, int>& exporter, CombineMode CM) {
181  XPETRA_MONITOR("EpetraMultiVector::doImport");
182 
183  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, source, tSource, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraMultiVector as input arguments.");
184  XPETRA_DYNAMIC_CAST(const EpetraExport, exporter, tExporter, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraImport as input arguments.");
185 
186  RCP<Epetra_MultiVector> v = tSource.getEpetra_MultiVector();
187  int err = this->getEpetra_MultiVector()->Import(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
188  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
189  }
190 
191  void EpetraMultiVector::doExport(const DistObject<double, int, int> &dest, const Export<int, int>& exporter, CombineMode CM) {
192  XPETRA_MONITOR("EpetraMultiVector::doExport");
193 
194  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, dest, tDest, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraMultiVector as input arguments.");
195  XPETRA_DYNAMIC_CAST(const EpetraExport, exporter, tExporter, "Xpetra::EpetraMultiVector::doImport only accept Xpetra::EpetraImport as input arguments.");
196 
197  RCP<Epetra_MultiVector> v = tDest.getEpetra_MultiVector();
198  int err = this->getEpetra_MultiVector()->Export(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
199  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
200  }
201 
203  int err=0;
204  if(!map.is_null()) {
205  err=this->getEpetra_MultiVector()->ReplaceMap(toEpetra(map));
206  }
207  else {
208  // Replace map with a dummy map to avoid potential hangs later
209  Epetra_SerialComm SComm;
210  Epetra_Map NewMap(vec_->MyLength(),vec_->Map().IndexBase(),SComm);
211  err=this->getEpetra_MultiVector()->ReplaceMap(NewMap);
212  }
213  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
214  }
215 
216 
217  // TODO: move that elsewhere
218  const Epetra_MultiVector & toEpetra(const MultiVector<double, int, int> & x) {
219  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, x, tX, "toEpetra");
220  return *tX.getEpetra_MultiVector();
221  }
222 
223  Epetra_MultiVector & toEpetra(MultiVector<double, int, int> & x) {
224  XPETRA_DYNAMIC_CAST( EpetraMultiVector, x, tX, "toEpetra");
225  return *tX.getEpetra_MultiVector();
226  }
227  //
228 
230  return rcp(new EpetraMultiVector(vec));
231  }
232 
233 
234 } // namespace Xpetra
void normInf(const Teuchos::ArrayView< Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
Compute Inf-norm of each vector in multi-vector.
#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< const Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVector(size_t j) const
Return a Vector which is a const view of column j.
RCP< Epetra_MultiVector > getEpetra_MultiVector() const
Get the underlying Epetra multivector.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, int > > &graph)
void meanValue(const Teuchos::ArrayView< Scalar > &means) const
Compute mean (average) value of each vector in multi-vector. The outcome of this routine is undefined...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void norm1(const Teuchos::ArrayView< Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
Compute 1-norm of each vector in multi-vector.
EpetraMultiVector(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map, size_t NumVectors, bool zeroOut=true)
Basic MultiVector constuctor.
Teuchos::RCP< Vector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getVectorNonConst(size_t j)
Return a Vector which is a nonconst view of column j.
void norm2(const Teuchos::ArrayView< Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
void doExport(const DistObject< Scalar, LocalOrdinal, GlobalOrdinal, Node > &dest, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Export.
void doImport(const DistObject< Scalar, LocalOrdinal, GlobalOrdinal, Node > &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Import.
Exception throws when you call an unimplemented method of Xpetra.
T * getRawPtr() const
#define XPETRA_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
RCP< Epetra_MultiVector > vec_
Teuchos::ArrayRCP< Scalar > getDataNonConst(size_t j)
View of the local values in a particular vector of this multivector.
CombineMode
Xpetra::Combine Mode enumerable type.
#define XPETRA_MONITOR(funcName)
Teuchos::ArrayRCP< const Scalar > getData(size_t j) const
Const view of the local values in a particular vector of this multivector.
void normWeighted(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &weights, const Teuchos::ArrayView< Teuchos::ScalarTraits< Scalar >::magnitudeType > &norms) const
std::string description() const
A simple one-line description of this object.
RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > > toXpetra(RCP< const Tpetra::CrsGraph< LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > >)
Convert an Epetra_Comm.to a Teuchos_Comm.
void dot(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Teuchos::ArrayView< Scalar > &dots) const
Compute dot product of each corresponding pair of vectors, dots[i] = this[i].dot(A[i]).
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with the given verbosity level to a FancyOStream.
void replaceMap(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &map)
Replace the map.