Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tpetra_EpetraRowMatrix.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Tpetra: Templated Linear Algebra Services Package
4 //
5 // Copyright 2008 NTESS and the Tpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TPETRA_EPETRAROWMATRIX_HPP
11 #define TPETRA_EPETRAROWMATRIX_HPP
12 
13 #include "TpetraCore_config.h"
14 
15 #if !defined(TPETRA_ENABLE_DEPRECATED_CODE)
16 #error This file is deprecated due to Epetra removal and will be removed
17 #endif
18 
19 #if defined(TPETRA_ENABLE_DEPRECATED_CODE) && defined(HAVE_TPETRA_EPETRA)
20 
21 #include <Epetra_Comm.h>
22 #include <Epetra_BasicRowMatrix.h>
23 #include <Tpetra_CrsMatrix.hpp>
24 #include <Teuchos_TestForException.hpp>
25 #include <memory> // std::shared_ptr
26 #include <stdexcept>
27 #include <type_traits>
28 #include <vector>
29 
30 namespace Tpetra {
31 namespace Details {
32 
33 // Epetra_MpiComm actually has reference-counting value semantics,
34 // just like std::shared_ptr. We only return
35 // std::shared_ptr<Epetra_Comm> because Epetra_Comm is an abstract
36 // base class, so we must return it by pointer.
37 TPETRA_DEPRECATED_MSG("epetra removal")
38 std::shared_ptr<Epetra_Comm>
39 makeEpetraCommFromTeuchosComm(const Teuchos::Comm<int> &teuchosComm);
40 
41 } // namespace Details
42 } // namespace Tpetra
43 
44 namespace { // (anonymous)
45 
46 template <class EpetraGlobalOrdinalType, class TpetraMapType>
47 TPETRA_DEPRECATED_MSG("epetra removal")
48 Epetra_Map
49  tpetraToEpetraMapTmpl(const TpetraMapType &tpetraMap) {
50  using Teuchos::ArrayView;
51  typedef typename TpetraMapType::global_ordinal_type TGO;
52  typedef typename TpetraMapType::local_ordinal_type LO;
53  typedef EpetraGlobalOrdinalType EGO;
54 
55  const TGO gblNumInds = static_cast<TGO>(tpetraMap.getGlobalNumElements());
56  const LO lclNumInds = static_cast<LO>(tpetraMap.getLocalNumElements());
57  ArrayView<const TGO> global_index_list = tpetraMap.getLocalElementList();
58 
59  std::vector<EGO> global_index_list_epetra;
60  const EGO *global_index_list_epetra_ptr = NULL;
61  if (std::is_same<TGO, EGO>::value) {
62  global_index_list_epetra_ptr =
63  reinterpret_cast<const EGO *>(global_index_list.getRawPtr());
64  } else {
65  global_index_list_epetra.resize(lclNumInds);
66  for (LO k = 0; k < lclNumInds; ++k) {
67  // TODO (mfh 11 Oct 2017) Detect overflow from TGO to EGO.
68  global_index_list_epetra[k] = static_cast<EGO>(global_index_list[k]);
69  }
70  global_index_list_epetra_ptr = global_index_list_epetra.data();
71  }
72  const EGO indexBase = tpetraMap.getIndexBase();
73  std::shared_ptr<Epetra_Comm> epetraComm =
74  Tpetra::Details::makeEpetraCommFromTeuchosComm(*(tpetraMap.getComm()));
75  // It's OK for the shared_ptr to fall out of scope. Subclasses of
76  // Epetra_Comm have reference-counted value semantics, so passing
77  // the Epetra_Comm by const reference into Epetra_Map's constructor
78  // is safe.
79  //
80  // TODO (mfh 11 Oct 2017) Detect overflow from TGO to EGO, or from
81  // LO to int.
82  return Epetra_Map(static_cast<EGO>(gblNumInds),
83  static_cast<int>(lclNumInds),
84  global_index_list_epetra_ptr, indexBase, *epetraComm);
85 }
86 
87 } // namespace
88 
89 namespace Tpetra {
90 
92 template <class TpetraMatrixType>
93 class
94  TPETRA_DEPRECATED_MSG("epetra removal")
95  EpetraRowMatrix : public Epetra_BasicRowMatrix {
96  public:
97  EpetraRowMatrix(const Teuchos::RCP<TpetraMatrixType> &mat, const Epetra_Comm &comm);
98  virtual ~EpetraRowMatrix(){};
99 
100  int ExtractMyRowCopy(int MyRow, int Length, int &NumEntries, double *Values, int *Indices) const;
101 
102  // not implemented
103  int ExtractMyEntryView(int CurEntry, double *&Value, int &RowIndex, int &ColIndex);
104 
105  // not implemented
106  int ExtractMyEntryView(int CurEntry, double const *&Value, int &RowIndex, int &ColIndex) const;
107 
108  int NumMyRowEntries(int MyRow, int &NumEntries) const;
109 
110  private:
111  Teuchos::RCP<TpetraMatrixType> tpetra_matrix_;
112 }; // class EpetraRowMatrix
113 
114 template <class TpetraMatrixType>
115 EpetraRowMatrix<TpetraMatrixType>::EpetraRowMatrix(
116  const Teuchos::RCP<TpetraMatrixType> &mat, const Epetra_Comm &comm)
117  : Epetra_BasicRowMatrix(comm)
118  , tpetra_matrix_(mat) {
119  using Teuchos::RCP;
120  typedef typename TpetraMatrixType::map_type tpetra_map_type;
121 #if !defined(EPETRA_NO_64BIT_GLOBAL_INDICES)
122  // Prefer using Epetra64 if it is enabled.
123  typedef long long EGO;
124 #elif !defined(EPETRA_NO_32BIT_GLOBAL_INDICES)
125  // We don't have Epetra64, but we do have 32-bit indices.
126  typedef int EGO;
127 #else
128 #error "Epetra was not configured correctly. Neither 64-bit nor 32-bit indices are enabled."
129 #endif
130  const char tfecfFuncName[] = "EpetraRowMatrix: ";
131 
132  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(mat.is_null(), std::invalid_argument,
133  "The input Tpetra matrix is null.");
134  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(mat->getRowMap().is_null(), std::invalid_argument,
135  "The input Tpetra matrix's row Map is null.");
136  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(mat->getColMap().is_null(), std::invalid_argument,
137  "The input Tpetra matrix's column Map is null.");
138 
139  RCP<const tpetra_map_type> tpetraRowMap = mat->getRowMap();
140  Epetra_Map epetraRowMap =
141  tpetraToEpetraMapTmpl<EGO, tpetra_map_type>(*tpetraRowMap);
142  RCP<const tpetra_map_type> tpetraColMap = mat->getColMap();
143  Epetra_Map epetraColMap =
144  tpetraToEpetraMapTmpl<EGO, tpetra_map_type>(*tpetraColMap);
145  this->SetMaps(epetraRowMap, epetraColMap);
146 }
147 
148 template <class TpetraMatrixType>
149 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyRowCopy(int MyRow, int Length, int &NumEntries, double *Values, int *Indices) const {
150  using inds_view = typename TpetraMatrixType::nonconst_local_inds_host_view_type;
151  using vals_view = typename TpetraMatrixType::nonconst_values_host_view_type;
152  static_assert(std::is_same<typename TpetraMatrixType::scalar_type, double>::value,
153  "This code assumes that Tpetra::CrsMatrix's scalar_type is int.");
154  static_assert(std::is_same<typename TpetraMatrixType::local_ordinal_type, int>::value,
155  "This code assumes that Tpetra::CrsMatrix's local_ordinal_type is int.");
156  inds_view IndicesView(Indices, Length);
157  vals_view ValuesView(Values, Length);
158  size_t num_entries = NumEntries;
159  tpetra_matrix_->getLocalRowCopy(MyRow, IndicesView, ValuesView, num_entries);
160  NumEntries = num_entries;
161  return 0;
162 }
163 
164 template <class TpetraMatrixType>
165 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyEntryView(int CurEntry, double *&Value, int &RowIndex, int &ColIndex) {
166  // not implemented
167  return -1;
168 }
169 
170 template <class TpetraMatrixType>
171 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyEntryView(int CurEntry, double const *&Value, int &RowIndex, int &ColIndex) const {
172  // not implemented
173  return -1;
174 }
175 
176 template <class TpetraMatrixType>
177 int EpetraRowMatrix<TpetraMatrixType>::NumMyRowEntries(int MyRow, int &NumEntries) const {
178  NumEntries = tpetra_matrix_->getNumEntriesInLocalRow(MyRow);
179  return 0;
180 }
181 
182 } // namespace Tpetra
183 
184 #endif // defined(TPETRA_ENABLE_DEPRECATED_CODE) && defined(HAVE_TPETRA_EPETRA)
185 
186 // here is the include-guard #endif:
187 
188 #endif