MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MueLu_Utilities_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
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 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef MUELU_UTILITIES_DEF_HPP
47 #define MUELU_UTILITIES_DEF_HPP
48 
49 #include <Teuchos_DefaultComm.hpp>
51 #include <iostream>
52 
53 #include "MueLu_ConfigDefs.hpp"
55 
56 #ifdef HAVE_MUELU_EPETRA
57 #ifdef HAVE_MPI
58 #include "Epetra_MpiComm.h"
59 #endif
60 #endif
61 
62 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
63 #include <EpetraExt_MatrixMatrix.h>
64 #include <EpetraExt_RowMatrixOut.h>
66 #include <EpetraExt_CrsMatrixIn.h>
68 #include <EpetraExt_BlockMapIn.h>
69 #include <Xpetra_EpetraUtils.hpp>
71 #include <EpetraExt_BlockMapOut.h>
72 #endif
73 
74 #include <MatrixMarket_Tpetra.hpp>
75 #include <Tpetra_RowMatrixTransposer.hpp>
76 #include <TpetraExt_MatrixMatrix.hpp>
77 #include <Xpetra_TpetraMultiVector.hpp>
79 #include <Xpetra_TpetraCrsMatrix.hpp>
80 #include <Xpetra_TpetraBlockCrsMatrix.hpp>
81 
82 #ifdef HAVE_MUELU_EPETRA
83 #include <Xpetra_EpetraMap.hpp>
84 #endif
85 
87 //#include <Xpetra_DefaultPlatform.hpp>
88 #include <Xpetra_IO.hpp>
89 #include <Xpetra_Map.hpp>
90 #include <Xpetra_MapFactory.hpp>
91 #include <Xpetra_Matrix.hpp>
92 #include <Xpetra_MatrixFactory.hpp>
93 #include <Xpetra_MultiVector.hpp>
94 #include <Xpetra_MultiVectorFactory.hpp>
95 #include <Xpetra_Operator.hpp>
96 #include <Xpetra_Vector.hpp>
97 #include <Xpetra_VectorFactory.hpp>
98 
99 #include <Xpetra_MatrixMatrix.hpp>
100 
101 #include <MueLu_Utilities_decl.hpp>
102 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_ML)
103 #include <ml_operator.h>
104 #include <ml_epetra_utils.h>
105 #endif
106 
107 namespace MueLu {
108 
109 #ifdef HAVE_MUELU_EPETRA
110 // using Xpetra::EpetraCrsMatrix; // TODO: mv in Xpetra_UseShortNamesScalar
111 // using Xpetra::EpetraMultiVector;
112 #endif
113 
114 #ifdef HAVE_MUELU_EPETRA
115 template <typename SC, typename LO, typename GO, typename NO>
117  return Xpetra::Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap<SC, LO, GO, NO>(epAB);
118 }
119 #endif
120 
121 #ifdef HAVE_MUELU_EPETRA
122 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
125  if (tmpVec == Teuchos::null)
126  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::EpetraMultiVector failed");
127  return tmpVec->getEpetra_MultiVector();
128 }
129 
130 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
133  if (tmpVec == Teuchos::null)
134  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::EpetraMultiVector failed");
135  return tmpVec->getEpetra_MultiVector();
136 }
137 
138 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
141  return *(tmpVec.getEpetra_MultiVector());
142 }
143 
144 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
147  return *(tmpVec.getEpetra_MultiVector());
148 }
149 
150 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
153  if (crsOp == Teuchos::null)
154  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
155  const RCP<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal, Node>>& tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal, Node>>(crsOp->getCrsMatrix());
156  if (tmp_ECrsMtx == Teuchos::null)
157  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
158  return tmp_ECrsMtx->getEpetra_CrsMatrix();
159 }
160 
161 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
164  if (crsOp == Teuchos::null)
165  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
166  const RCP<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal, Node>>& tmp_ECrsMtx = rcp_dynamic_cast<const Xpetra::EpetraCrsMatrixT<GlobalOrdinal, Node>>(crsOp->getCrsMatrix());
167  if (tmp_ECrsMtx == Teuchos::null)
168  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
169  return tmp_ECrsMtx->getEpetra_CrsMatrixNonConst();
170 }
171 
172 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
174  try {
176  try {
178  return *tmp_ECrsMtx.getEpetra_CrsMatrix();
179  } catch (std::bad_cast&) {
180  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
181  }
182  } catch (std::bad_cast&) {
183  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
184  }
185 }
186 
187 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
189  try {
191  try {
193  return *tmp_ECrsMtx.getEpetra_CrsMatrixNonConst();
194  } catch (std::bad_cast&) {
195  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::EpetraCrsMatrix failed");
196  }
197  } catch (std::bad_cast&) {
198  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
199  }
200 }
201 
202 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
204  RCP<const Xpetra::EpetraMapT<GlobalOrdinal, Node>> xeMap = rcp_dynamic_cast<const Xpetra::EpetraMapT<GlobalOrdinal, Node>>(rcpFromRef(map));
205  if (xeMap == Teuchos::null)
206  throw Exceptions::BadCast("Utilities::Map2EpetraMap : Cast from Xpetra::Map to Xpetra::EpetraMap failed");
207  return xeMap->getEpetra_Map();
208 }
209 #endif
210 
211 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
215  if (tmpVec == Teuchos::null)
216  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::TpetraMultiVector failed");
217  return tmpVec->getTpetra_MultiVector();
218 }
219 
220 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
223  if (tmpVec == Teuchos::null)
224  throw Exceptions::BadCast("Cast from Xpetra::MultiVector to Xpetra::TpetraMultiVector failed");
225  return tmpVec->getTpetra_MultiVector();
226 }
227 
228 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
231  return *(tmpVec.getTpetra_MultiVector());
232 }
233 
234 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
237  return tmpVec.getTpetra_MultiVector();
238 }
239 
240 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
244  return *(tmpVec.getTpetra_MultiVector());
245 }
246 
247 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
249  // Get the underlying Tpetra Mtx
251  if (crsOp == Teuchos::null)
252  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
254  if (tmp_ECrsMtx == Teuchos::null)
255  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
256  return tmp_ECrsMtx->getTpetra_CrsMatrix();
257 }
258 
259 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
262  if (crsOp == Teuchos::null)
263  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
265  if (tmp_ECrsMtx == Teuchos::null)
266  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
267  return tmp_ECrsMtx->getTpetra_CrsMatrixNonConst();
268 }
269 
270 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
272  try {
274  try {
276  return *tmp_ECrsMtx.getTpetra_CrsMatrix();
277  } catch (std::bad_cast&) {
278  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
279  }
280  } catch (std::bad_cast&) {
281  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
282  }
283 }
284 
285 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
287  try {
289  try {
291  return *tmp_ECrsMtx.getTpetra_CrsMatrixNonConst();
292  } catch (std::bad_cast&) {
293  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix failed");
294  }
295  } catch (std::bad_cast&) {
296  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
297  }
298 }
299 
300 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
303  // Get the underlying Tpetra Mtx
304  RCP<const XCrsMatrixWrap> crsOp = rcp_dynamic_cast<const XCrsMatrixWrap>(Op);
305  if (crsOp == Teuchos::null)
306  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
308  if (tmp_ECrsMtx == Teuchos::null)
309  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
310  return tmp_ECrsMtx->getTpetra_BlockCrsMatrix();
311 }
312 
313 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
316  RCP<const XCrsMatrixWrap> crsOp = rcp_dynamic_cast<const XCrsMatrixWrap>(Op);
317  if (crsOp == Teuchos::null)
318  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
320  if (tmp_ECrsMtx == Teuchos::null)
321  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
322  return tmp_ECrsMtx->getTpetra_BlockCrsMatrixNonConst();
323 }
324 
325 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
327  try {
329  const XCrsMatrixWrap& crsOp = dynamic_cast<const XCrsMatrixWrap&>(Op);
330  try {
332  return *tmp_ECrsMtx.getTpetra_BlockCrsMatrix();
333  } catch (std::bad_cast&) {
334  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
335  }
336  } catch (std::bad_cast&) {
337  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
338  }
339 }
340 
341 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
343  try {
345  XCrsMatrixWrap& crsOp = dynamic_cast<XCrsMatrixWrap&>(Op);
346  try {
348  return *tmp_ECrsMtx.getTpetra_BlockCrsMatrixNonConst();
349  } catch (std::bad_cast&) {
350  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraBlockCrsMatrix failed");
351  }
352  } catch (std::bad_cast&) {
353  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
354  }
355 }
356 
357 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
361  if (!mat.is_null()) {
363  if (crsOp == Teuchos::null)
364  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
365 
369  if (!tmp_Crs.is_null()) {
370  return tmp_Crs->getTpetra_CrsMatrixNonConst();
371  } else {
372  tmp_BlockCrs = rcp_dynamic_cast<const Xpetra::TpetraBlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>(crsMat);
373  if (tmp_BlockCrs.is_null())
374  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix and Xpetra::TpetraBlockCrsMatrix failed");
375  return tmp_BlockCrs->getTpetra_BlockCrsMatrixNonConst();
376  }
377  } else if (!rmat.is_null()) {
378  return rmat->getTpetra_RowMatrix();
379  } else {
383  return tRow;
384  }
385 }
386 
387 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
391  if (!mat.is_null()) {
393  if (crsOp == Teuchos::null)
394  throw Exceptions::BadCast("Cast from Xpetra::Matrix to Xpetra::CrsMatrixWrap failed");
395 
399  if (!tmp_Crs.is_null()) {
400  return tmp_Crs->getTpetra_CrsMatrixNonConst();
401  } else {
402  tmp_BlockCrs = rcp_dynamic_cast<const Xpetra::TpetraBlockCrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>>(crsMat);
403  if (tmp_BlockCrs.is_null())
404  throw Exceptions::BadCast("Cast from Xpetra::CrsMatrix to Xpetra::TpetraCrsMatrix and Xpetra::TpetraBlockCrsMatrix failed");
405  return tmp_BlockCrs->getTpetra_BlockCrsMatrixNonConst();
406  }
407  } else if (!rmat.is_null()) {
408  return rmat->getTpetra_RowMatrixNonConst();
409  } else {
413  return tRow;
414  }
415 }
416 
417 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
420  if (tmp_TMap == Teuchos::null)
421  throw Exceptions::BadCast("Utilities::Map2TpetraMap : Cast from Xpetra::Map to Xpetra::TpetraMap failed");
422  return tmp_TMap->getTpetra_Map();
423 }
424 
425 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
427  bool doFillComplete,
428  bool doOptimizeStorage) {
430  Teuchos::ArrayRCP<Scalar> sv(scalingVector.size());
431  if (doInverse) {
432  for (int i = 0; i < scalingVector.size(); ++i)
433  sv[i] = one / scalingVector[i];
434  } else {
435  for (int i = 0; i < scalingVector.size(); ++i)
436  sv[i] = scalingVector[i];
437  }
438 
439  switch (Op.getRowMap()->lib()) {
440  case Xpetra::UseTpetra:
441  MyOldScaleMatrix_Tpetra(Op, sv, doFillComplete, doOptimizeStorage);
442  break;
443 
444  case Xpetra::UseEpetra:
445  MyOldScaleMatrix_Epetra(Op, sv, doFillComplete, doOptimizeStorage);
446  break;
447 
448  default:
449  throw Exceptions::RuntimeError("Only Epetra and Tpetra matrices can be scaled.");
450  }
451 }
452 
453 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
455  throw Exceptions::RuntimeError("MyOldScaleMatrix_Epetra: Epetra needs SC=double and LO=GO=int.");
456 }
457 
458 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
460  bool doFillComplete,
461  bool doOptimizeStorage) {
462  try {
463  Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& tpOp = Op2NonConstTpetraCrs(Op);
464 
468 
469  size_t maxRowSize = tpOp.getLocalMaxNumRowEntries();
470  if (maxRowSize == Teuchos::as<size_t>(-1)) // hasn't been determined yet
471  maxRowSize = 20;
472 
473  if (tpOp.isFillComplete())
474  tpOp.resumeFill();
475 
476  if (Op.isLocallyIndexed() == true) {
479 
480  for (size_t i = 0; i < rowMap->getLocalNumElements(); ++i) {
481  tpOp.getLocalRowView(i, cols, vals);
482 
483  size_t nnz = tpOp.getNumEntriesInLocalRow(i);
485 
486  for (size_t j = 0; j < nnz; ++j) {
487  scaledVals[j] = scalingVector[i] * vals[j];
488  }
489 
490  if (nnz > 0) {
491  tpOp.replaceLocalValues(i, cols, scaledVals);
492  }
493  } // for (size_t i=0; ...
494 
495  } else {
498 
499  for (size_t i = 0; i < rowMap->getLocalNumElements(); ++i) {
500  GlobalOrdinal gid = rowMap->getGlobalElement(i);
501  tpOp.getGlobalRowView(gid, cols, vals);
502  size_t nnz = tpOp.getNumEntriesInGlobalRow(gid);
504 
505  // FIXME FIXME FIXME FIXME FIXME FIXME
506  for (size_t j = 0; j < nnz; ++j)
507  scaledVals[j] = scalingVector[i] * vals[j]; // FIXME i or gid?
508 
509  if (nnz > 0) {
510  tpOp.replaceGlobalValues(gid, cols, scaledVals);
511  }
512  } // for (size_t i=0; ...
513  }
514 
515  if (doFillComplete) {
516  if (domainMap == Teuchos::null || rangeMap == Teuchos::null)
517  throw Exceptions::RuntimeError("In Utilities::Scaling: cannot fillComplete because the domain and/or range map hasn't been defined");
518 
520  params->set("Optimize Storage", doOptimizeStorage);
521  params->set("No Nonlocal Changes", true);
522  Op.fillComplete(Op.getDomainMap(), Op.getRangeMap(), params);
523  }
524  } catch (...) {
525  throw Exceptions::RuntimeError("Only Tpetra::CrsMatrix types can be scaled (Err.1)");
526  }
527 } // MyOldScaleMatrix_Tpetra()
528 
529 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
532  Transpose(Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>& Op, bool /* optimizeTranspose */, const std::string& label, const Teuchos::RCP<Teuchos::ParameterList>& params) {
533 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
534  std::string TorE = "epetra";
535 #else
536  std::string TorE = "tpetra";
537 #endif
538 
539 #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_EPETRAEXT)
540  try {
542  (void)epetraOp; // silence "unused variable" compiler warning
543  } catch (...) {
544  TorE = "tpetra";
545  }
546 #endif
547 
548  if (TorE == "tpetra") {
550  /***************************************************************/
551  if (Helpers::isTpetraCrs(Op)) {
553 
555  Tpetra::RowMatrixTransposer<Scalar, LocalOrdinal, GlobalOrdinal, Node> transposer(rcpFromRef(tpetraOp), label); // more than meets the eye
556 
557  {
559  using Teuchos::rcp;
560  RCP<ParameterList> transposeParams = params.is_null() ? rcp(new ParameterList) : rcp(new ParameterList(*params));
561  transposeParams->set("sort", false);
562  A = transposer.createTranspose(transposeParams);
563  }
564 
568  if (!AAAA->isFillComplete())
569  AAAA->fillComplete(Op.getRangeMap(), Op.getDomainMap());
570 
571  if (Op.IsView("stridedMaps"))
572  AAAA->CreateView("stridedMaps", Teuchos::rcpFromRef(Op), true /*doTranspose*/);
573 
574  return AAAA;
575  } else if (Helpers::isTpetraBlockCrs(Op)) {
580  // using CRS = Tpetra::CrsMatrix<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
582 
583  RCP<BCRS> At;
584  {
586 
588  using Teuchos::rcp;
589  RCP<ParameterList> transposeParams = params.is_null() ? rcp(new ParameterList) : rcp(new ParameterList(*params));
590  transposeParams->set("sort", false);
591  At = transposer.createTranspose(transposeParams);
592  }
593 
595  RCP<XCrsMatrix> AAA = rcp_implicit_cast<XCrsMatrix>(AA);
596  RCP<XMatrix> AAAA = rcp(new XCrsMatrixWrap(AAA));
597 
598  if (Op.IsView("stridedMaps"))
599  AAAA->CreateView("stridedMaps", Teuchos::rcpFromRef(Op), true /*doTranspose*/);
600 
601  return AAAA;
602  } else {
603  throw Exceptions::RuntimeError("Utilities::Transpose failed, perhaps because matrix is not a Crs matrix");
604  }
605  } // if
606 
607  // Epetra case
608  std::cout << "Utilities::Transpose() not implemented for Epetra" << std::endl;
609  return Teuchos::null;
610 
611 } // Transpose
612 
613 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
618 #if defined(HAVE_XPETRA_TPETRA) && (defined(HAVE_TPETRA_INST_COMPLEX_DOUBLE) || defined(HAVE_TPETRA_INST_COMPLEX_FLOAT))
619  using range_type = Kokkos::RangePolicy<LocalOrdinal, typename Node::execution_space>;
620 
621  // Need to cast the real-valued multivector to Scalar=complex
622  if ((typeid(Scalar).name() == typeid(std::complex<double>).name()) ||
623  (typeid(Scalar).name() == typeid(std::complex<float>).name())) {
624  size_t numVecs = X->getNumVectors();
626  auto XVec = X->getDeviceLocalView(Xpetra::Access::ReadOnly);
627  auto XVecScalar = Xscalar->getDeviceLocalView(Xpetra::Access::ReadWrite);
628 
629  Kokkos::parallel_for(
630  "MueLu:Utils::RealValuedToScalarMultiVector", range_type(0, X->getLocalLength()),
631  KOKKOS_LAMBDA(const size_t i) {
632  for (size_t j = 0; j < numVecs; j++)
633  XVecScalar(i, j) = XVec(i, j);
634  });
635  } else
636 #endif
638  return Xscalar;
639 }
640 
641 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
646 
647  // check whether coordinates are contained in parameter list
648  if (paramList.isParameter("Coordinates") == false)
649  return coordinates;
650 
651  // define Tpetra::MultiVector type with Scalar=float only if
652  // * ETI is turned off, since then the compiler will instantiate it automatically OR
653  // * Tpetra is instantiated on Scalar=float
654 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
656  RCP<tfMV> floatCoords = Teuchos::null;
657 #endif
658 
659  // define Tpetra::MultiVector type with Scalar=double only if
660  // * ETI is turned off, since then the compiler will instantiate it automatically OR
661  // * Tpetra is instantiated on Scalar=double
662 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_DOUBLE)
664  RCP<tdMV> doubleCoords = Teuchos::null;
665  if (paramList.isType<RCP<tdMV>>("Coordinates")) {
666  // Coordinates are stored as a double vector
667  doubleCoords = paramList.get<RCP<tdMV>>("Coordinates");
668  paramList.remove("Coordinates");
669  }
670 #if !defined(HAVE_TPETRA_EXPLICIT_INSTANTIATION) || defined(HAVE_TPETRA_INST_FLOAT)
671  else if (paramList.isType<RCP<tfMV>>("Coordinates")) {
672  // check if coordinates are stored as a float vector
673  floatCoords = paramList.get<RCP<tfMV>>("Coordinates");
674  paramList.remove("Coordinates");
675  doubleCoords = rcp(new tdMV(floatCoords->getMap(), floatCoords->getNumVectors()));
676  deep_copy(*doubleCoords, *floatCoords);
677  }
678 #endif
679  // We have the coordinates in a Tpetra double vector
680  if (doubleCoords != Teuchos::null) {
681  // rcp(new Xpetra::TpetraMultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>(Vtpetra));
682  coordinates = Teuchos::rcp_dynamic_cast<Xpetra::MultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LocalOrdinal, GlobalOrdinal, Node>>(MueLu::TpetraMultiVector_To_XpetraMultiVector<typename Teuchos::ScalarTraits<Scalar>::magnitudeType, LocalOrdinal, GlobalOrdinal, Node>(doubleCoords));
684  TEUCHOS_TEST_FOR_EXCEPT(doubleCoords->getNumVectors() != coordinates->getNumVectors());
685  }
686 #else
687  // coordinates usually are stored as double vector
688  // Tpetra is not instantiated on scalar=double
689  throw Exceptions::RuntimeError("ExtractCoordinatesFromParameterList: The coordinates vector in parameter list is expected to be a Tpetra multivector with SC=double or float.");
690 #endif
691 
692  // check for Xpetra coordinates vector
693  if (paramList.isType<decltype(coordinates)>("Coordinates")) {
694  coordinates = paramList.get<decltype(coordinates)>("Coordinates");
695  }
696 
697  return coordinates;
698 } // ExtractCoordinatesFromParameterList
699 
700 } // namespace MueLu
701 
702 #define MUELU_UTILITIES_SHORT
703 #endif // MUELU_UTILITIES_DEF_HPP
704 
705 // LocalWords: LocalOrdinal
RCP< CrsMatrix > getCrsMatrix() const
Exception indicating invalid cast attempted.
static const RCP< const Tpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > Map2TpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
MueLu::DefaultLocalOrdinal LocalOrdinal
static RCP< const Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2TpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >> const vec)
Helper utility to pull out the underlying Tpetra objects from an Xpetra object.
static void MyOldScaleMatrix(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< const Scalar > &scalingVector, bool doInverse=true, bool doFillComplete=true, bool doOptimizeStorage=true)
static Teuchos::RCP< MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Build(const Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node >> &map, size_t NumVectors, bool zeroOut=true)
static RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > RealValuedToScalarMultiVector(RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::coordinateType, LocalOrdinal, GlobalOrdinal, Node >> X)
static const Epetra_Map & Map2EpetraMap(const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > &map)
Teuchos::RCP< const map_type > getRangeMap() const override
RCP< Tpetra::BlockCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_BlockCrsMatrixNonConst() const
T & get(const std::string &name, T def_value)
RCP< Epetra_CrsMatrix > getEpetra_CrsMatrixNonConst() const
static RCP< Xpetra::MultiVector< typename Teuchos::ScalarTraits< Scalar >::magnitudeType, LocalOrdinal, GlobalOrdinal, Node > > ExtractCoordinatesFromParameterList(ParameterList &paramList)
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
static RCP< Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraRow(RCP< Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
bool is_null(const std::shared_ptr< T > &p)
static RCP< Epetra_CrsMatrix > Op2NonConstEpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
void resumeFill(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
static RCP< const Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2TpetraCrs(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
size_type size() const
MueLu::DefaultNode Node
static RCP< Tpetra::BlockCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraBlockCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
bool isParameter(const std::string &name) const
void deep_copy(MultiVector< DS, DL, DG, DN > &dst, const MultiVector< SS, SL, SG, SN > &src)
bool remove(std::string const &name, bool throwIfNotExists=true)
virtual void fillComplete(const RCP< const Map > &domainMap, const RCP< const Map > &rangeMap, const RCP< ParameterList > &params=null)=0
size_t getLocalMaxNumRowEntries() const override
bool isFillComplete() const override
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
static RCP< Epetra_MultiVector > MV2NonConstEpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >> vec)
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
RCP< const Tpetra::BlockCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_BlockCrsMatrix() const
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV2(Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &vec)
static RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Transpose(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, bool optimizeTranspose=false, const std::string &label=std::string(), const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
RCP< const Epetra_CrsMatrix > getEpetra_CrsMatrix() const
local_ordinal_type replaceGlobalValues(const global_ordinal_type globalRow, const Kokkos::View< const global_ordinal_type *, Kokkos::AnonymousSpace > &inputInds, const Kokkos::View< const impl_scalar_type *, Kokkos::AnonymousSpace > &inputVals)
RCP< Xpetra::CrsMatrixWrap< SC, LO, GO, NO > > Convert_Epetra_CrsMatrix_ToXpetra_CrsMatrixWrap(RCP< Epetra_CrsMatrix > &epAB)
size_t getNumEntriesInLocalRow(local_ordinal_type localRow) const override
Teuchos::RCP< bcrs_matrix_type > createTranspose(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
Teuchos::RCP< crs_matrix_type > createTranspose(const Teuchos::RCP< Teuchos::ParameterList > &params=Teuchos::null)
static void MyOldScaleMatrix_Epetra(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< Scalar > &scalingVector, bool doFillComplete, bool doOptimizeStorage)
Teuchos::RCP< const map_type > getDomainMap() const override
virtual bool isLocallyIndexed() const =0
RCP< const Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_CrsMatrix() const
RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_CrsMatrixNonConst() const
bool isType(const std::string &name) const
static RCP< const Tpetra::RowMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2TpetraRow(RCP< const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
virtual void getGlobalRowView(GlobalOrdinal GlobalRow, global_inds_host_view_type &indices, values_host_view_type &values) const =0
static RCP< const Epetra_MultiVector > MV2EpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >> const vec)
Helper utility to pull out the underlying Epetra objects from an Xpetra object.
RCP< Epetra_MultiVector > getEpetra_MultiVector() const
virtual Teuchos::RCP< const Map > getRangeMap() const =0
RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getTpetra_MultiVector() const
size_t getNumEntriesInGlobalRow(GlobalOrdinal globalRow) const override
Exception throws to report errors in the internal logical of the program.
virtual void getLocalRowView(LocalOrdinal LocalRow, local_inds_host_view_type &indices, values_host_view_type &values) const =0
local_ordinal_type replaceLocalValues(const local_ordinal_type localRow, const Kokkos::View< const local_ordinal_type *, Kokkos::AnonymousSpace > &inputInds, const Kokkos::View< const impl_scalar_type *, Kokkos::AnonymousSpace > &inputVals)
static RCP< const Tpetra::BlockCrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2TpetraBlockCrs(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
static void MyOldScaleMatrix_Tpetra(Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op, const Teuchos::ArrayRCP< Scalar > &scalingVector, bool doFillComplete, bool doOptimizeStorage)
static RCP< const Epetra_CrsMatrix > Op2EpetraCrs(RCP< const Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
static RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > Op2NonConstTpetraCrs(RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node >> Op)
virtual Teuchos::RCP< const Map > getDomainMap() const =0
static RCP< Tpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > MV2NonConstTpetraMV(RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node >> vec)
Teuchos::RCP< const map_type > getRowMap() const override
bool is_null() const