Xpetra_EpetraCrsMatrix.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 <Teuchos_Array.hpp>
48 
49 namespace Xpetra {
50 
51  EpetraCrsMatrix::EpetraCrsMatrix(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype, const Teuchos::RCP< Teuchos::ParameterList > &plist)
52  : mtx_(Teuchos::rcp(new Epetra_CrsMatrix(Copy, toEpetra(rowMap), maxNumEntriesPerRow, toEpetra(pftype)))), isFillResumed_(false) { }
53 
54  EpetraCrsMatrix::EpetraCrsMatrix(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype, const Teuchos::RCP< Teuchos::ParameterList > &plist)
55  : isFillResumed_(false)
56  {
57  Teuchos::Array<int> numEntriesPerRowToAlloc(NumEntriesPerRowToAlloc.begin(), NumEntriesPerRowToAlloc.end()); // convert array of "size_t" to array of "int"
58  mtx_ = Teuchos::rcp(new Epetra_CrsMatrix(Copy, toEpetra(rowMap), numEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)));
59  }
60 
61  EpetraCrsMatrix::EpetraCrsMatrix(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &colMap, size_t maxNumEntriesPerRow, ProfileType pftype, const Teuchos::RCP< Teuchos::ParameterList > &plist)
62  : mtx_(Teuchos::rcp(new Epetra_CrsMatrix(Copy, toEpetra(rowMap), toEpetra(colMap), maxNumEntriesPerRow, toEpetra(pftype)))), isFillResumed_(false) { }
63 
64  EpetraCrsMatrix::EpetraCrsMatrix(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &colMap, const ArrayRCP< const size_t > &NumEntriesPerRowToAlloc, ProfileType pftype, const Teuchos::RCP< Teuchos::ParameterList > &plist)
65  : isFillResumed_(false)
66  {
67  Teuchos::Array<int> numEntriesPerRowToAlloc(NumEntriesPerRowToAlloc.begin(), NumEntriesPerRowToAlloc.end()); // convert array of "size_t" to array of "int"
68  mtx_ = Teuchos::rcp(new Epetra_CrsMatrix(Copy, toEpetra(rowMap), toEpetra(colMap), numEntriesPerRowToAlloc.getRawPtr(), toEpetra(pftype)));
69  }
70 
71  EpetraCrsMatrix::EpetraCrsMatrix(const Teuchos::RCP< const CrsGraph< LocalOrdinal, GlobalOrdinal, Node, LocalMatOps > > &graph, const Teuchos::RCP< Teuchos::ParameterList > &plist)
72  : mtx_(Teuchos::rcp(new Epetra_CrsMatrix(Copy, toEpetra(graph)))), isFillResumed_(false) { }
73 
74 #ifdef HAVE_XPETRA_EXPERIMENTAL
76  : mtx_(Teuchos::rcp(new Epetra_CrsMatrix(*(matrix.mtx_)))), isFillResumed_(false) { }
77 #endif
78 
79 
82  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& domainMap,
83  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& rangeMap,
84  const Teuchos::RCP<Teuchos::ParameterList>& params):
85  isFillResumed_(false)
86  {
87  XPETRA_DYNAMIC_CAST(const EpetraCrsMatrix, *sourceMatrix, tSourceMatrix, "Xpetra::EpetraCrsMatrix constructor only accepts Xpetra::EpetraCrsMatrix as an input argument.");
88  XPETRA_DYNAMIC_CAST(const EpetraImport, importer, tImporter, "Xpetra::EpetraCrsMatrix constructor only accepts Xpetra::EpetraImport as an input argument.");
89 
90  const Epetra_Map* myDomainMap = (domainMap!=Teuchos::null)? &toEpetra(domainMap): 0;
91  const Epetra_Map* myRangeMap = (rangeMap !=Teuchos::null)? &toEpetra(rangeMap) : 0;
92 
93  // Follows the Tpetra parameters
94  bool restrictComm=false;
95  if(!params.is_null()) restrictComm = params->get("Restrict Communicator",restrictComm);
96  mtx_ = Teuchos::rcp(new Epetra_CrsMatrix(*tSourceMatrix.getEpetra_CrsMatrix(),*tImporter.getEpetra_Import(),myDomainMap,myRangeMap,restrictComm));
97  if(restrictComm && mtx_->NumMyRows()==0)
98  mtx_=Teuchos::null;
99  }
100 
103  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& domainMap,
104  const Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> >& rangeMap,
105  const Teuchos::RCP<Teuchos::ParameterList>& params):
106  isFillResumed_(false)
107  {
108  XPETRA_DYNAMIC_CAST(const EpetraCrsMatrix, *sourceMatrix, tSourceMatrix, "Xpetra::EpetraCrsMatrix constructor only accepts Xpetra::EpetraCrsMatrix as an input argument.");
109  XPETRA_DYNAMIC_CAST(const EpetraExport, exporter, tExporter, "Xpetra::EpetraCrsMatrix constructor only accepts Xpetra::EpetraExport as an input argument.");
110 
111  const Epetra_Map* myDomainMap = (domainMap!=Teuchos::null)? &toEpetra(domainMap): 0;
112  const Epetra_Map* myRangeMap = (rangeMap !=Teuchos::null)? &toEpetra(rangeMap) : 0;
113 
114  // Follows the Tpetra parameters
115  bool restrictComm=false;
116  if(!params.is_null()) restrictComm = params->get("Restrict Communicator",restrictComm);
117 
118  mtx_ = Teuchos::rcp(new Epetra_CrsMatrix(*tSourceMatrix.getEpetra_CrsMatrix(),*tExporter.getEpetra_Export(),myDomainMap,myRangeMap,restrictComm));
119  }
120 
121 
122 
123  void EpetraCrsMatrix::insertGlobalValues(int globalRow, const ArrayView<const int> &cols, const ArrayView<const double> &vals) {
124  XPETRA_MONITOR("EpetraCrsMatrix::insertGlobalValues");
125  XPETRA_ERR_CHECK(mtx_->InsertGlobalValues(globalRow, vals.size(), vals.getRawPtr(), cols.getRawPtr()));
126  }
127 
128  void EpetraCrsMatrix::insertLocalValues(int localRow, const ArrayView<const int> &cols, const ArrayView<const double> &vals) {
129  XPETRA_MONITOR("EpetraCrsMatrix::insertLocalValues");
130  XPETRA_ERR_CHECK(mtx_->InsertMyValues(localRow, vals.size(), vals.getRawPtr(), cols.getRawPtr()));
131  }
132 
133  void EpetraCrsMatrix::replaceGlobalValues(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &indices, const ArrayView< const Scalar > &values) {
134  XPETRA_MONITOR("EpetraCrsMatrix::replaceGlobalValues");
135 
136  {
137  const std::string tfecfFuncName("replaceGlobalValues");
138  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(! isFillActive(), std::runtime_error,
139  ": Fill must be active in order to call this method. If you have already "
140  "called fillComplete(), you need to call resumeFill() before you can "
141  "replace values.");
142 
143  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(values.size() != indices.size(),
144  std::runtime_error, ": values.size() must equal indices.size().");
145  }
146 
147  XPETRA_ERR_CHECK(mtx_->ReplaceGlobalValues(globalRow, indices.size(), values.getRawPtr(), indices.getRawPtr()));
148 
149  }
150 
151  void EpetraCrsMatrix::replaceLocalValues(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &indices, const ArrayView< const Scalar > &values) {
152  XPETRA_MONITOR("EpetraCrsMatrix::replaceLocalValues");
153 
154  {
155  const std::string tfecfFuncName("replaceGlobalValues");
156  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(! isFillActive(), std::runtime_error,
157  ": Fill must be active in order to call this method. If you have already "
158  "called fillComplete(), you need to call resumeFill() before you can "
159  "replace values.");
160 
161  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(values.size() != indices.size(),
162  std::runtime_error, ": values.size() must equal indices.size().");
163  }
164 
165  XPETRA_ERR_CHECK(mtx_->ReplaceMyValues(localRow, indices.size(), values.getRawPtr(), indices.getRawPtr()));
166 
167  }
168 
169  void EpetraCrsMatrix::allocateAllValues(size_t numNonZeros,ArrayRCP<size_t> & rowptr, ArrayRCP<int> & colind, ArrayRCP<double> & values)
170  {
171  // NOTE: Tpetra's insistence on using size_t's necessitate not directly accessing the rowptr here... Thus is is the ONLY thing we copy in setAllValues.
172  XPETRA_MONITOR("EpetraCrsMatrix::allocateAllValues");
173 
174  // Grab pointers
175  Epetra_IntSerialDenseVector& myColind = mtx_->ExpertExtractIndices();
176  double *& myValues = mtx_->ExpertExtractValues();
177 
178  // Resize
179  myColind.Resize(numNonZeros);
180  delete [] myValues; myValues = new double[numNonZeros];
181 
182  // Wrap in array RCPs w/o the memory control.
183  rowptr.resize(getNodeNumRows()+1);
184  colind = Teuchos::arcp(myColind.Values(),0,numNonZeros,false);
185  values = Teuchos::arcp(myValues,0,numNonZeros,false);
186  }
187 
188  void EpetraCrsMatrix::setAllValues(const ArrayRCP<size_t> & rowptr, const ArrayRCP<int> & colind, const ArrayRCP<double> & values)
189  {
190  XPETRA_MONITOR("EpetraCrsMatrix::setAllValues");
191 
192  TEUCHOS_TEST_FOR_EXCEPTION((size_t)rowptr.size()!=getNodeNumRows()+1, Xpetra::Exceptions::RuntimeError, "An exception is thrown to let you know that the size of your rowptr array is incorrect.");
193  TEUCHOS_TEST_FOR_EXCEPTION(colind.getRawPtr()!=mtx_->ExpertExtractIndices().Values(), Xpetra::Exceptions::RuntimeError, "An exception is thrown to let you know that you mismatched your pointers.");
194  TEUCHOS_TEST_FOR_EXCEPTION(values.getRawPtr()!=mtx_->ExpertExtractValues(), Xpetra::Exceptions::RuntimeError, "An exception is thrown to let you know that you mismatched your pointers.");
195  TEUCHOS_TEST_FOR_EXCEPTION(values.size()!=colind.size(), Xpetra::Exceptions::RuntimeError, "An exception is thrown to let you know that you mismatched your pointers.");
196 
197  // NOTE: Tpetra's insistence on using size_t's necessitate not directly accessing the rowptr here... Thus is is the ONLY thing we copy in setAllValues.
198  Epetra_IntSerialDenseVector& myRowptr = mtx_->ExpertExtractIndexOffset();
199  size_t N = getNodeNumRows();
200 
201  myRowptr.Resize(N+1);
202  for(size_t i=0; i<N+1; i++)
203  myRowptr[i] = Teuchos::as<int>(rowptr[i]);
204  }
205 
206 
207  void EpetraCrsMatrix::resumeFill(const RCP< ParameterList > &params) {
208  XPETRA_MONITOR("EpetraCrsMatrix::resumeFill");
209 
210  // According to Tpetra documentation, resumeFill() may be called repeatedly.
211  isFillResumed_ = true;
212  }
213 
214  bool EpetraCrsMatrix::isFillComplete() const { XPETRA_MONITOR("EpetraCrsMatrix::isFillComplete"); if (isFillResumed_) return false; else return mtx_->Filled(); }
215 
216  bool EpetraCrsMatrix::isFillActive() const { XPETRA_MONITOR("EpetraCrsMatrix::isFillActive"); return !isFillComplete(); }
217 
218  bool EpetraCrsMatrix::supportsRowViews() const { XPETRA_MONITOR("EpetraCrsMatrix::supportsRowViews"); return true; }
219 
220  //TODO: throw same exception as Tpetra
221  void EpetraCrsMatrix::getLocalRowCopy(int LocalRow, const ArrayView<int> &Indices, const ArrayView<double> &Values, size_t &NumEntries) const {
222  XPETRA_MONITOR("EpetraCrsMatrix::getLocalRowCopy");
223 
224  int numEntries=-1;
225  XPETRA_ERR_CHECK(mtx_->ExtractMyRowCopy(LocalRow, Indices.size(), numEntries, Values.getRawPtr(), Indices.getRawPtr()));
226  NumEntries = numEntries;
227  }
228 
229  void EpetraCrsMatrix::getGlobalRowView(int GlobalRow, ArrayView<const int> &indices, ArrayView<const double> &values) const {
230  XPETRA_MONITOR("EpetraCrsMatrix::getGlobalRowView");
231 
232  int numEntries;
233  double * eValues;
234  int * eIndices;
235 
236  XPETRA_ERR_CHECK(mtx_->ExtractGlobalRowView(GlobalRow, numEntries, eValues, eIndices));
237  if (numEntries == 0) { eValues = NULL; eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
238 
239  indices = ArrayView<const int>(eIndices, numEntries);
240  values = ArrayView<const double>(eValues, numEntries);
241  }
242 
243  void EpetraCrsMatrix::getLocalRowView(int LocalRow, ArrayView<const int> &indices, ArrayView<const double> &values) const {
244  XPETRA_MONITOR("EpetraCrsMatrix::getLocalRowView");
245 
246  int numEntries;
247  double * eValues;
248  int * eIndices;
249 
250  XPETRA_ERR_CHECK(mtx_->ExtractMyRowView(LocalRow, numEntries, eValues, eIndices));
251  if (numEntries == 0) { eValues = NULL; eIndices = NULL; } // Cf. TEUCHOS_TEST_FOR_EXCEPT( p == 0 && size_in != 0 ) in Teuchos ArrayView constructor.
252 
253  indices = ArrayView<const int>(eIndices, numEntries);
254  values = ArrayView<const double>(eValues, numEntries);
255  }
256 
257  void EpetraCrsMatrix::apply(const MultiVector<double,int,int> & X, MultiVector<double,int,int> &Y, Teuchos::ETransp mode, double alpha, double beta) const {
258  XPETRA_MONITOR("EpetraCrsMatrix::apply");
259 
260  //TEUCHOS_TEST_FOR_EXCEPTION((alpha != 1) || (beta != 0), Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraCrsMatrix.multiply() only accept alpha==1 and beta==0");
261 
262  XPETRA_DYNAMIC_CAST(const EpetraMultiVector, X, eX, "Xpetra::EpetraCrsMatrix->apply() only accept Xpetra::EpetraMultiVector as input arguments.");
263  XPETRA_DYNAMIC_CAST( EpetraMultiVector, Y, eY, "Xpetra::EpetraCrsMatrix->apply() only accept Xpetra::EpetraMultiVector as input arguments.");
264 
265  TEUCHOS_TEST_FOR_EXCEPTION((mode != Teuchos::NO_TRANS) && (mode != Teuchos::TRANS), Xpetra::Exceptions::NotImplemented, "Xpetra::EpetraCrsMatrix->apply() only accept mode == NO_TRANS or mode == TRANS");
266  bool eTrans = toEpetra(mode);
267 
268  // /!\ UseTranspose value
269  TEUCHOS_TEST_FOR_EXCEPTION(mtx_->UseTranspose(), Xpetra::Exceptions::NotImplemented, "An exception is throw to let you know that Xpetra::EpetraCrsMatrix->apply() do not take into account the UseTranspose() parameter of Epetra_CrsMatrix.");
270 
271  RCP<Epetra_MultiVector> epY = eY.getEpetra_MultiVector();
272 
273  // helper vector: tmp = A*x
274  RCP<Epetra_MultiVector> tmp = Teuchos::rcp(new Epetra_MultiVector(*epY));
275  tmp->PutScalar(0.0);
276  XPETRA_ERR_CHECK(mtx_->Multiply(eTrans, *eX.getEpetra_MultiVector(), *tmp));
277 
278  // calculate alpha * A * x + beta * y
279  XPETRA_ERR_CHECK(eY.getEpetra_MultiVector()->Update(alpha,*tmp,beta));
280  }
281 
282  std::string EpetraCrsMatrix::description() const {
283  XPETRA_MONITOR("EpetraCrsMatrix::description");
284 
285  // This implementation come from Tpetra_CrsMatrix_def.hpp (without modification)
286  std::ostringstream oss;
287  //TODO: oss << DistObject<char, LocalOrdinal,GlobalOrdinal>::description();
288  if (isFillComplete()) {
289  oss << "{status = fill complete"
290  << ", global rows = " << getGlobalNumRows()
291  << ", global cols = " << getGlobalNumCols()
292  << ", global num entries = " << getGlobalNumEntries()
293  << "}";
294  }
295  else {
296  oss << "{status = fill not complete"
297  << ", global rows = " << getGlobalNumRows()
298  << "}";
299  }
300  return oss.str();
301 
302  }
303 
304  void EpetraCrsMatrix::describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const {
305  XPETRA_MONITOR("EpetraCrsMatrix::describe");
306 
307  // This implementation come from Tpetra_CrsMatrix_def.hpp (without modification)
308  using std::endl;
309  using std::setw;
310  using Teuchos::VERB_DEFAULT;
311  using Teuchos::VERB_NONE;
312  using Teuchos::VERB_LOW;
313  using Teuchos::VERB_MEDIUM;
314  using Teuchos::VERB_HIGH;
315  using Teuchos::VERB_EXTREME;
316  Teuchos::EVerbosityLevel vl = verbLevel;
317  if (vl == VERB_DEFAULT) vl = VERB_LOW;
318  RCP<const Comm<int> > comm = this->getComm();
319  const int myImageID = comm->getRank(),
320  numImages = comm->getSize();
321  size_t width = 1;
322  for (size_t dec=10; dec<getGlobalNumRows(); dec *= 10) {
323  ++width;
324  }
325  width = std::max<size_t>(width,11) + 2;
326  Teuchos::OSTab tab(out);
327  // none: print nothing
328  // low: print O(1) info from node 0
329  // medium: print O(P) info, num entries per node
330  // high: print O(N) info, num entries per row
331  // extreme: print O(NNZ) info: print indices and values
332  //
333  // for medium and higher, print constituent objects at specified verbLevel
334  if (vl != VERB_NONE) {
335  if (myImageID == 0) out << this->description() << std::endl;
336  // O(1) globals, minus what was already printed by description()
337  if (isFillComplete() && myImageID == 0) {
338  out << "Global number of diagonals = " << getGlobalNumDiags() << std::endl;
339  out << "Global max number of entries = " << getGlobalMaxNumRowEntries() << std::endl;
340  }
341  // constituent objects
342  if (vl == VERB_MEDIUM || vl == VERB_HIGH || vl == VERB_EXTREME) {
343  if (myImageID == 0) out << "\nRow map: " << std::endl;
344  getRowMap()->describe(out,vl);
345  //
346  if (getColMap() != null) {
347  if (getColMap() == getRowMap()) {
348  if (myImageID == 0) out << "\nColumn map is row map.";
349  }
350  else {
351  if (myImageID == 0) out << "\nColumn map: " << std::endl;
352  getColMap()->describe(out,vl);
353  }
354  }
355  if (getDomainMap() != null) {
356  if (getDomainMap() == getRowMap()) {
357  if (myImageID == 0) out << "\nDomain map is row map.";
358  }
359  else if (getDomainMap() == getColMap()) {
360  if (myImageID == 0) out << "\nDomain map is row map.";
361  }
362  else {
363  if (myImageID == 0) out << "\nDomain map: " << std::endl;
364  getDomainMap()->describe(out,vl);
365  }
366  }
367  if (getRangeMap() != null) {
368  if (getRangeMap() == getDomainMap()) {
369  if (myImageID == 0) out << "\nRange map is domain map." << std::endl;
370  }
371  else if (getRangeMap() == getRowMap()) {
372  if (myImageID == 0) out << "\nRange map is row map." << std::endl;
373  }
374  else {
375  if (myImageID == 0) out << "\nRange map: " << std::endl;
376  getRangeMap()->describe(out,vl);
377  }
378  }
379  if (myImageID == 0) out << std::endl;
380  }
381  // O(P) data
382  if (vl == VERB_MEDIUM || vl == VERB_HIGH || vl == VERB_EXTREME) {
383  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
384  if (myImageID == imageCtr) {
385  out << "Node ID = " << imageCtr << std::endl;
386  // TODO: need a graph
387  // if (staticGraph_->indicesAreAllocated() == false) {
388  // out << "Node not allocated" << std::endl;
389  // }
390  // else {
391  // out << "Node number of allocated entries = " << staticGraph_->getNodeAllocationSize() << std::endl;
392  // }
393 
394  // TMP:
395  // const Epetra_CrsGraph & staticGraph_ = mtx_->Graph();
396  // End of TMP
397 
398  out << "Node number of entries = " << getNodeNumEntries() << std::endl;
399  if (isFillComplete()) {
400  out << "Node number of diagonals = " << getNodeNumDiags() << std::endl;
401  }
402  out << "Node max number of entries = " << getNodeMaxNumRowEntries() << std::endl;
403  }
404  comm->barrier();
405  comm->barrier();
406  comm->barrier();
407  }
408  }
409  // O(N) and O(NNZ) data
410  if (vl == VERB_HIGH || vl == VERB_EXTREME) {
411  for (int imageCtr = 0; imageCtr < numImages; ++imageCtr) {
412  if (myImageID == imageCtr) {
413  out << std::setw(width) << "Node ID"
414  << std::setw(width) << "Global Row"
415  << std::setw(width) << "Num Entries";
416  if (vl == VERB_EXTREME) {
417  out << std::setw(width) << "(Index,Value)";
418  }
419  out << std::endl;
420  for (size_t r=0; r < getNodeNumRows(); ++r) {
421  const size_t nE = getNumEntriesInLocalRow(r);
422  GlobalOrdinal gid = getRowMap()->getGlobalElement(r);
423  out << std::setw(width) << myImageID
424  << std::setw(width) << gid
425  << std::setw(width) << nE;
426  if (vl == VERB_EXTREME) {
427  if (isGloballyIndexed()) {
428  ArrayView<const GlobalOrdinal> rowinds;
429  ArrayView<const Scalar> rowvals;
430  getGlobalRowView(gid,rowinds,rowvals);
431  for (size_t j=0; j < nE; ++j) {
432  out << " (" << rowinds[j]
433  << ", " << rowvals[j]
434  << ") ";
435  }
436  }
437  else if (isLocallyIndexed()) {
438  ArrayView<const LocalOrdinal> rowinds;
439  ArrayView<const Scalar> rowvals;
440  getLocalRowView(r,rowinds,rowvals);
441  for (size_t j=0; j < nE; ++j) {
442  out << " (" << getColMap()->getGlobalElement(rowinds[j])
443  << ", " << rowvals[j]
444  << ") ";
445  }
446  }
447  }
448  out << std::endl;
449  }
450  }
451  comm->barrier();
452  comm->barrier();
453  comm->barrier();
454  }
455  }
456  }
457 
458  }
459 
460  // TODO: use toEpetra()
462  const Import<int, int> &importer, CombineMode CM) {
463  XPETRA_MONITOR("EpetraCrsMatrix::doImport");
464 
465  XPETRA_DYNAMIC_CAST(const EpetraCrsMatrix, source, tSource, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraCrsMatrix as input arguments.");
466  XPETRA_DYNAMIC_CAST(const EpetraImport, importer, tImporter, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraImport as input arguments.");
467 
468  RCP<const Epetra_CrsMatrix> v = tSource.getEpetra_CrsMatrix();
469  int err = mtx_->Import(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
470  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
471  }
472 
474  const Import<int, int>& importer, CombineMode CM) {
475  XPETRA_MONITOR("EpetraCrsMatrix::doExport");
476 
477  XPETRA_DYNAMIC_CAST(const EpetraCrsMatrix, dest, tDest, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraCrsMatrix as input arguments.");
478  XPETRA_DYNAMIC_CAST(const EpetraImport, importer, tImporter, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraImport as input arguments.");
479 
480  RCP<const Epetra_CrsMatrix> v = tDest.getEpetra_CrsMatrix();
481  int err = mtx_->Export(*v, *tImporter.getEpetra_Import(), toEpetra(CM));
482  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
483  }
484 
486  const Export<int, int>& exporter, CombineMode CM) {
487  XPETRA_MONITOR("EpetraCrsMatrix::doImport");
488 
489  XPETRA_DYNAMIC_CAST(const EpetraCrsMatrix, source, tSource, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraCrsMatrix as input arguments.");
490  XPETRA_DYNAMIC_CAST(const EpetraExport, exporter, tExporter, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraImport as input arguments.");
491 
492  RCP<const Epetra_CrsMatrix> v = tSource.getEpetra_CrsMatrix();
493  int err = mtx_->Import(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
494  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
495 
496  }
497 
499  const Export<int, int>& exporter, CombineMode CM) {
500  XPETRA_MONITOR("EpetraCrsMatrix::doExport");
501 
502  XPETRA_DYNAMIC_CAST(const EpetraCrsMatrix, dest, tDest, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraCrsMatrix as input arguments.");
503  XPETRA_DYNAMIC_CAST(const EpetraExport, exporter, tExporter, "Xpetra::EpetraCrsMatrix::doImport only accept Xpetra::EpetraImport as input arguments.");
504 
505  RCP<const Epetra_CrsMatrix> v = tDest.getEpetra_CrsMatrix();
506  int err = mtx_->Export(*v, *tExporter.getEpetra_Export(), toEpetra(CM));
507  TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, "Catch error code returned by Epetra.");
508  }
509 
511  const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap,
512  const RCP< ParameterList > &params) {
513  XPETRA_MONITOR("EpetraCrsMatrix::fillComplete");
514 
515  // For Epetra matrices, resumeFill() is a fictive operation. There is no need for a fillComplete after some resumeFill() operations.
516  if (isFillResumed_ == true) { isFillResumed_ = false; return; }
517 
518  bool doOptimizeStorage = true;
519  if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
520  mtx_->FillComplete(toEpetra(domainMap), toEpetra(rangeMap), doOptimizeStorage);
521  }
522 
523  void EpetraCrsMatrix::fillComplete(const RCP< ParameterList > &params) {
524  XPETRA_MONITOR("EpetraCrsMatrix::fillComplete");
525 
526  // For Epetra matrices, resumeFill() is a fictive operation. There is no need for a fillComplete after some resumeFill() operations.
527  if (isFillResumed_ == true) { isFillResumed_ = false; return; }
528 
529  bool doOptimizeStorage = true;
530  if (params != null && params->get("Optimize Storage",true) == false) doOptimizeStorage = false;
531  mtx_->FillComplete(doOptimizeStorage);
532  }
533 
534 
535  void EpetraCrsMatrix::replaceDomainMapAndImporter(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > >& newDomainMap, Teuchos::RCP<const Import<LocalOrdinal,GlobalOrdinal,Node> > & newImporter) {
536  XPETRA_MONITOR("EpetraCrsMatrix::replaceDomainMapAndImporter");
537  XPETRA_DYNAMIC_CAST(const EpetraImport, *newImporter, eImporter, "Xpetra::EpetraCrsMatrix::replaceDomainMapAndImporter only accepts Xpetra::EpetraImport.");
538 
539  const RCP<const Epetra_Import> & myImport = eImporter.getEpetra_Import();
540  int rv=0;
541  if(myImport==Teuchos::null)
542  rv=mtx_->ReplaceDomainMapAndImporter( toEpetra(newDomainMap),0);
543  else
544  rv=mtx_->ReplaceDomainMapAndImporter( toEpetra(newDomainMap),&*myImport);
545  TEUCHOS_TEST_FOR_EXCEPTION(rv != 0, std::runtime_error, "Xpetra::EpetraCrsMatrix::replaceDomainMapAndImporter FAILED!");
546  }
547 
548 
550  const RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > & rangeMap,
551  const RCP<const Import<LocalOrdinal,GlobalOrdinal,Node> > &importer,
552  const RCP<const Export<LocalOrdinal,GlobalOrdinal,Node> > &exporter,
553  const RCP<ParameterList> & params) {
554  XPETRA_MONITOR("EpetraCrsMatrix::expertStaticFillComplete");
555  int rv=0;
556  const Epetra_Import * myimport =0;
557  const Epetra_Export * myexport =0;
558 
559  if(!importer.is_null()) {
560  XPETRA_DYNAMIC_CAST(const EpetraImport, *importer, eImporter, "Xpetra::EpetraCrsMatrix::expertStaticFillComplete only accepts Xpetra::EpetraImport.");
561  myimport = eImporter.getEpetra_Import().getRawPtr();
562  }
563  if(!exporter.is_null()) {
564  XPETRA_DYNAMIC_CAST(const EpetraExport, *exporter, eExporter, "Xpetra::EpetraCrsMatrix::expertStaticFillComplete only accepts Xpetra::EpetraImport.");
565  myexport = eExporter.getEpetra_Export().getRawPtr();
566  }
567 
568  rv=mtx_->ExpertStaticFillComplete(toEpetra(domainMap), toEpetra(rangeMap), myimport, myexport);
569 
570  TEUCHOS_TEST_FOR_EXCEPTION(rv != 0, std::runtime_error, "Xpetra::EpetraCrsMatrix::expertStaticFillComplete FAILED!");
571  }
572 }
size_t getNumEntriesInLocalRow(LocalOrdinal localRow) const
Returns the current number of entries on this node in the specified local row.
void replaceGlobalValues(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Replace matrix entries, using global IDs.
void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=ScalarTraits< Scalar >::one(), Scalar beta=ScalarTraits< Scalar >::zero()) const
Computes the sparse matrix-multivector multiplication.
void insertGlobalValues(GlobalOrdinal globalRow, const ArrayView< const GlobalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Insert matrix entries, using global IDs.
global_size_t getGlobalNumEntries() const
Returns the global number of entries in this matrix.
size_t getNodeNumEntries() const
Returns the local number of entries in this matrix.
bool isFillComplete() const
Returns true if the matrix is in compute mode, i.e. if fillComplete() has been called.
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getColMap() const
Returns the Map that describes the column distribution in this matrix.
void getGlobalRowView(GlobalOrdinal GlobalRow, ArrayView< const GlobalOrdinal > &indices, ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of global indices in a specified row of the matrix.
const RCP< const Comm< int > > getComm() const
Returns the communicator.
size_t getNodeNumDiags() const
Returns the number of local diagonal entries, based on global row/column index comparisons.
void resumeFill(const RCP< ParameterList > &params=null)
global_size_t getGlobalNumDiags() const
Returns the number of global diagonal entries, based on global row/column index comparisons.
EpetraCrsMatrix(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rowMap, size_t maxNumEntriesPerRow, ProfileType pftype=DynamicProfile, const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Constructor specifying fixed number of entries for each row.
Xpetra namespace
void replaceLocalValues(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Replace matrix entries, using local IDs.
Exception throws to report errors in the internal logical of the program.
bool isGloballyIndexed() const
If matrix indices are in the global range, this function returns true. Otherwise, this function retur...
void setAllValues(const ArrayRCP< size_t > &rowptr, const ArrayRCP< LocalOrdinal > &colind, const ArrayRCP< Scalar > &values)
Sets the matrix&#39;s structure from the Crs arrays.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, int > > &graph)
bool isLocallyIndexed() const
If matrix indices are in the local range, this function returns true. Otherwise, this function return...
size_t getNodeMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on this node.
#define XPETRA_ERR_CHECK(arg)
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRowMap() const
Returns the Map that describes the row distribution in this matrix.
void doExport(const DistObject< char, LocalOrdinal, GlobalOrdinal, Node > &dest, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Export.
void doImport(const DistObject< char, LocalOrdinal, GlobalOrdinal, Node > &source, const Import< LocalOrdinal, GlobalOrdinal, Node > &importer, CombineMode CM)
Import.
void getLocalRowView(LocalOrdinal LocalRow, ArrayView< const LocalOrdinal > &indices, ArrayView< const Scalar > &values) const
Extract a const, non-persisting view of local indices in a specified row of the matrix.
bool supportsRowViews() const
Returns true if getLocalRowView() and getGlobalRowView() are valid for this class.
void getLocalRowCopy(LocalOrdinal LocalRow, const ArrayView< LocalOrdinal > &Indices, const ArrayView< Scalar > &Values, size_t &NumEntries) const
Extract a list of entries in a specified local row of the matrix. Put into storage allocated by calli...
Exception throws when you call an unimplemented method of Xpetra.
bool isFillActive() const
Returns true if the matrix is in edit mode.
void replaceDomainMapAndImporter(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &newDomainMap, Teuchos::RCP< const Import< LocalOrdinal, GlobalOrdinal, Node > > &newImporter)
Replaces the current domainMap and importer with the user-specified objects.
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
Returns the Map associated with the range of this operator, which must be compatible with Y...
const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
Returns the Map associated with the domain of this operator. This will be null until fillComplete() i...
size_t getGlobalMaxNumRowEntries() const
Returns the maximum number of entries across all rows/columns on all nodes.
global_size_t getGlobalNumCols() const
Number of global columns in the matrix.
void expertStaticFillComplete(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &domainMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap, const RCP< const Import< LocalOrdinal, GlobalOrdinal, Node > > &importer=Teuchos::null, const RCP< const Export< LocalOrdinal, GlobalOrdinal, Node > > &exporter=Teuchos::null, const RCP< ParameterList > &params=Teuchos::null)
Expert static fill complete.
#define XPETRA_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
void allocateAllValues(size_t numNonZeros, ArrayRCP< size_t > &rowptr, ArrayRCP< LocalOrdinal > &colind, ArrayRCP< Scalar > &values)
Allocates and returns ArrayRCPs of the Crs arrays — This is an Xpetra-only routine.
CombineMode
Xpetra::Combine Mode enumerable type.
#define XPETRA_MONITOR(funcName)
void fillComplete(const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &domainMap, const RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > &rangeMap, const RCP< ParameterList > &params=null)
Signal that data entry is complete, specifying domain and range maps.
void insertLocalValues(LocalOrdinal localRow, const ArrayView< const LocalOrdinal > &cols, const ArrayView< const Scalar > &vals)
Insert matrix entries, using local IDs.
size_t getNodeNumRows() const
Returns the number of matrix rows owned on the calling node.
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.
RCP< Epetra_CrsMatrix > mtx_
global_size_t getGlobalNumRows() const
Number of global elements in the row map of this matrix.
std::string description() const
A simple one-line description of this object.