44 #ifndef TPETRA_EPETRAROWMATRIX_HPP
45 #define TPETRA_EPETRAROWMATRIX_HPP
47 #include "TpetraCore_config.h"
49 #if defined(HAVE_TPETRA_EPETRA)
51 #include <Epetra_Comm.h>
52 #include <Epetra_BasicRowMatrix.h>
53 #include <Tpetra_CrsMatrix.hpp>
54 #include <Teuchos_TestForException.hpp>
57 #include <type_traits>
67 std::shared_ptr<Epetra_Comm>
68 makeEpetraCommFromTeuchosComm (
const Teuchos::Comm<int>& teuchosComm);
75 template<
class EpetraGlobalOrdinalType,
class TpetraMapType>
77 tpetraToEpetraMapTmpl (
const TpetraMapType& tpetraMap)
79 using Teuchos::ArrayView;
80 typedef typename TpetraMapType::global_ordinal_type TGO;
81 typedef typename TpetraMapType::local_ordinal_type LO;
82 typedef EpetraGlobalOrdinalType EGO;
84 const TGO gblNumInds =
static_cast<TGO
> (tpetraMap.getGlobalNumElements ());
85 const LO lclNumInds =
static_cast<LO
> (tpetraMap.getNodeNumElements ());
86 ArrayView<const TGO> global_index_list = tpetraMap.getNodeElementList ();
88 std::vector<EGO> global_index_list_epetra;
89 const EGO* global_index_list_epetra_ptr = NULL;
90 if (std::is_same<TGO, EGO>::value) {
91 global_index_list_epetra_ptr =
92 reinterpret_cast<const EGO*
> (global_index_list.getRawPtr ());
95 global_index_list_epetra.resize (lclNumInds);
96 for (LO k = 0; k < lclNumInds; ++k) {
98 global_index_list_epetra[k] =
static_cast<EGO
> (global_index_list[k]);
100 global_index_list_epetra_ptr = global_index_list_epetra.data ();
102 const EGO indexBase = tpetraMap.getIndexBase ();
103 std::shared_ptr<Epetra_Comm> epetraComm =
104 Tpetra::Details::makeEpetraCommFromTeuchosComm (* (tpetraMap.getComm ()));
112 return Epetra_Map (static_cast<EGO> (gblNumInds),
113 static_cast<int> (lclNumInds),
114 global_index_list_epetra_ptr, indexBase, *epetraComm);
122 template<
class TpetraMatrixType>
123 class EpetraRowMatrix :
public Epetra_BasicRowMatrix {
125 EpetraRowMatrix(
const Teuchos::RCP<TpetraMatrixType> &mat,
const Epetra_Comm &comm);
126 virtual ~EpetraRowMatrix() {};
128 int ExtractMyRowCopy(
int MyRow,
int Length,
int & NumEntries,
double *Values,
int * Indices)
const;
131 int ExtractMyEntryView(
int CurEntry,
double * & Value,
int & RowIndex,
int & ColIndex);
134 int ExtractMyEntryView(
int CurEntry,
double const * & Value,
int & RowIndex,
int & ColIndex)
const;
136 int NumMyRowEntries(
int MyRow,
int & NumEntries)
const;
139 Teuchos::RCP<TpetraMatrixType> tpetra_matrix_;
142 template<
class TpetraMatrixType>
143 EpetraRowMatrix<TpetraMatrixType>::EpetraRowMatrix(
144 const Teuchos::RCP<TpetraMatrixType> &mat,
const Epetra_Comm &comm
146 : Epetra_BasicRowMatrix(comm),
150 typedef typename TpetraMatrixType::map_type tpetra_map_type;
151 #if ! defined(EPETRA_NO_64BIT_GLOBAL_INDICES)
153 typedef long long EGO;
154 #elif ! defined(EPETRA_NO_32BIT_GLOBAL_INDICES)
158 # error "Epetra was not configured correctly. Neither 64-bit nor 32-bit indices are enabled."
160 const char tfecfFuncName[] =
"EpetraRowMatrix: ";
162 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
163 (mat.is_null (), std::invalid_argument,
164 "The input Tpetra matrix is null.");
165 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
166 (mat->getRowMap ().is_null (), std::invalid_argument,
167 "The input Tpetra matrix's row Map is null.");
168 TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
169 (mat->getColMap ().is_null (), std::invalid_argument,
170 "The input Tpetra matrix's column Map is null.");
172 RCP<const tpetra_map_type> tpetraRowMap = mat->getRowMap ();
173 Epetra_Map epetraRowMap =
174 tpetraToEpetraMapTmpl<EGO, tpetra_map_type> (*tpetraRowMap);
175 RCP<const tpetra_map_type> tpetraColMap = mat->getColMap ();
176 Epetra_Map epetraColMap =
177 tpetraToEpetraMapTmpl<EGO, tpetra_map_type> (*tpetraColMap);
178 this->SetMaps (epetraRowMap, epetraColMap);
181 template<
class TpetraMatrixType>
182 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyRowCopy(
int MyRow,
int Length,
int & NumEntries,
double *Values,
int * Indices)
const
184 static_assert (std::is_same<typename TpetraMatrixType::scalar_type, double>::value,
185 "This code assumes that Tpetra::CrsMatrix's scalar_type is int.");
186 static_assert (std::is_same<typename TpetraMatrixType::local_ordinal_type, int>::value,
187 "This code assumes that Tpetra::CrsMatrix's local_ordinal_type is int.");
188 Teuchos::ArrayView<int> inds(Indices, Length);
189 Teuchos::ArrayView<double> vals(Values, Length);
190 size_t num_entries = NumEntries;
191 tpetra_matrix_->getLocalRowCopy(MyRow, inds, vals, num_entries);
192 NumEntries = num_entries;
196 template<
class TpetraMatrixType>
197 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyEntryView(
int CurEntry,
double * & Value,
int & RowIndex,
int & ColIndex)
203 template<
class TpetraMatrixType>
204 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyEntryView(
int CurEntry,
double const * & Value,
int & RowIndex,
int & ColIndex)
const
210 template<
class TpetraMatrixType>
211 int EpetraRowMatrix<TpetraMatrixType>::NumMyRowEntries(
int MyRow,
int & NumEntries)
const
213 NumEntries = tpetra_matrix_->getNumEntriesInLocalRow(MyRow);
219 #endif // defined(HAVE_TPETRA_EPETRA)