Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_EpetraUtils.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Xpetra: A linear algebra interface package
4 //
5 // Copyright 2012 NTESS and the Xpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #include "Xpetra_ConfigDefs.hpp"
11 
12 #ifdef HAVE_XPETRA_EPETRA
13 
14 #include "Xpetra_EpetraUtils.hpp"
15 
16 // header files for comm objects conversion
17 #ifdef HAVE_MPI
18 #include <mpi.h>
19 #include <Teuchos_DefaultMpiComm.hpp>
20 #include <Teuchos_OpaqueWrapper.hpp>
21 #endif
22 #include <Teuchos_DefaultSerialComm.hpp>
23 #ifdef HAVE_MPI
24 #include <Epetra_MpiComm.h>
25 #endif
26 #include <Epetra_SerialComm.h>
27 
28 #include "Xpetra_Exceptions.hpp"
29 
30 namespace Xpetra {
31 
32 using Teuchos::RCP;
33 
34 const RCP<const Epetra_Comm> toEpetra(const RCP<const Teuchos::Comm<int> >& comm) {
35 #ifdef HAVE_MPI
36  const RCP<const Teuchos::MpiComm<int> > mpiComm = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(comm);
37  if (mpiComm != Teuchos::null) {
38  return Teuchos::rcp(new Epetra_MpiComm(*mpiComm->getRawMpiComm()));
39  } else
40 #endif
41  if ((Teuchos::rcp_dynamic_cast<const Teuchos::SerialComm<int> >(comm) != Teuchos::null))
42  return Teuchos::rcp(new Epetra_SerialComm());
43  else
44  TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::BadCast, "Cannot convert a Teuchos::Comm to an Epetra_Comm: The exact type of the Teuchos::Comm object is unknown");
45 }
46 
47 const RCP<const Teuchos::Comm<int> > toXpetra(const Epetra_Comm& comm) {
48 #ifdef HAVE_MPI
49  try {
50  const Epetra_MpiComm& mpiComm = dynamic_cast<const Epetra_MpiComm&>(comm);
51  // We need to pass some tag to the Teuchos::MpiComm constructor. We
52  // cannot use Epetra's GetMpiTag() as that increases the tag counter.
53  const int MAGIC_TAG = 26077;
54  return Teuchos::rcp(new Teuchos::MpiComm<int>(Teuchos::opaqueWrapper(mpiComm.Comm()), MAGIC_TAG));
55  } catch (std::bad_cast& /*b*/) {
56  }
57 #endif
58  try {
59  const Epetra_SerialComm& serialComm = dynamic_cast<const Epetra_SerialComm&>(comm);
60  serialComm.NumProc(); // avoid compilation warning
61  return Teuchos::rcp(new Teuchos::SerialComm<int>());
62  } catch (std::bad_cast& /*b*/) {
63  TEUCHOS_TEST_FOR_EXCEPTION(1, Xpetra::Exceptions::BadCast, "Cannot convert an Epetra_Comm to a Teuchos::Comm: The exact type of the Epetra_Comm object is unknown");
64  }
65 }
66 
67 bool toEpetra(Teuchos::ETransp trans) {
68  if (trans == Teuchos::NO_TRANS)
69  return false;
70  else if (trans == Teuchos::TRANS)
71  return true;
72  else {
73  TEUCHOS_TEST_FOR_EXCEPTION((trans != Teuchos::NO_TRANS) && (trans == Teuchos::TRANS), Xpetra::Exceptions::NotImplemented, "Cannot convert Teuchos::ETransp to a boolean.");
74  }
75 
76  return false; // to skip a compilation warning msg.
77 }
78 
79 } // namespace Xpetra
80 
81 #endif
Exception indicating invalid cast attempted.
Exception throws when you call an unimplemented method of Xpetra.
const Epetra_CrsGraph & toEpetra(const RCP< const CrsGraph< int, GlobalOrdinal, Node > > &graph)
RCP< const CrsGraph< int, GlobalOrdinal, Node > > toXpetra(const Epetra_CrsGraph &g)