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 
47 template<class EpetraGlobalOrdinalType, class TpetraMapType>
48 TPETRA_DEPRECATED_MSG("epetra removal")
49 Epetra_Map
50 tpetraToEpetraMapTmpl (const TpetraMapType& tpetraMap)
51 {
52  using Teuchos::ArrayView;
53  typedef typename TpetraMapType::global_ordinal_type TGO;
54  typedef typename TpetraMapType::local_ordinal_type LO;
55  typedef EpetraGlobalOrdinalType EGO;
56 
57  const TGO gblNumInds = static_cast<TGO> (tpetraMap.getGlobalNumElements ());
58  const LO lclNumInds = static_cast<LO> (tpetraMap.getLocalNumElements ());
59  ArrayView<const TGO> global_index_list = tpetraMap.getLocalElementList ();
60 
61  std::vector<EGO> global_index_list_epetra;
62  const EGO* global_index_list_epetra_ptr = NULL;
63  if (std::is_same<TGO, EGO>::value) {
64  global_index_list_epetra_ptr =
65  reinterpret_cast<const EGO*> (global_index_list.getRawPtr ());
66  }
67  else {
68  global_index_list_epetra.resize (lclNumInds);
69  for (LO k = 0; k < lclNumInds; ++k) {
70  // TODO (mfh 11 Oct 2017) Detect overflow from TGO to EGO.
71  global_index_list_epetra[k] = static_cast<EGO> (global_index_list[k]);
72  }
73  global_index_list_epetra_ptr = global_index_list_epetra.data ();
74  }
75  const EGO indexBase = tpetraMap.getIndexBase ();
76  std::shared_ptr<Epetra_Comm> epetraComm =
77  Tpetra::Details::makeEpetraCommFromTeuchosComm (* (tpetraMap.getComm ()));
78  // It's OK for the shared_ptr to fall out of scope. Subclasses of
79  // Epetra_Comm have reference-counted value semantics, so passing
80  // the Epetra_Comm by const reference into Epetra_Map's constructor
81  // is safe.
82  //
83  // TODO (mfh 11 Oct 2017) Detect overflow from TGO to EGO, or from
84  // LO to int.
85  return Epetra_Map (static_cast<EGO> (gblNumInds),
86  static_cast<int> (lclNumInds),
87  global_index_list_epetra_ptr, indexBase, *epetraComm);
88 }
89 
90 } // namespace (anonymous)
91 
92 namespace Tpetra {
93 
95 template<class TpetraMatrixType> class
96 TPETRA_DEPRECATED_MSG("epetra removal")
97 EpetraRowMatrix : public Epetra_BasicRowMatrix {
98 public:
99 
100  EpetraRowMatrix(const Teuchos::RCP<TpetraMatrixType> &mat, const Epetra_Comm &comm);
101  virtual ~EpetraRowMatrix() {};
102 
103  int ExtractMyRowCopy(int MyRow, int Length, int & NumEntries, double *Values, int * Indices) const;
104 
105  //not implemented
106  int ExtractMyEntryView(int CurEntry, double * & Value, int & RowIndex, int & ColIndex);
107 
108  //not implemented
109  int ExtractMyEntryView(int CurEntry, double const * & Value, int & RowIndex, int & ColIndex) const;
110 
111  int NumMyRowEntries(int MyRow, int & NumEntries) const;
112 
113 private:
114  Teuchos::RCP<TpetraMatrixType> tpetra_matrix_;
115 };//class EpetraRowMatrix
116 
117 template<class TpetraMatrixType>
118 EpetraRowMatrix<TpetraMatrixType>::EpetraRowMatrix(
119  const Teuchos::RCP<TpetraMatrixType> &mat, const Epetra_Comm &comm
120  )
121  : Epetra_BasicRowMatrix(comm),
122  tpetra_matrix_(mat)
123 {
124  using Teuchos::RCP;
125  typedef typename TpetraMatrixType::map_type tpetra_map_type;
126 #if ! defined(EPETRA_NO_64BIT_GLOBAL_INDICES)
127  // Prefer using Epetra64 if it is enabled.
128  typedef long long EGO;
129 #elif ! defined(EPETRA_NO_32BIT_GLOBAL_INDICES)
130  // We don't have Epetra64, but we do have 32-bit indices.
131  typedef int EGO;
132 #else
133 # error "Epetra was not configured correctly. Neither 64-bit nor 32-bit indices are enabled."
134 #endif
135  const char tfecfFuncName[] = "EpetraRowMatrix: ";
136 
137  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
138  (mat.is_null (), std::invalid_argument,
139  "The input Tpetra matrix is null.");
140  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
141  (mat->getRowMap ().is_null (), std::invalid_argument,
142  "The input Tpetra matrix's row Map is null.");
143  TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC
144  (mat->getColMap ().is_null (), std::invalid_argument,
145  "The input Tpetra matrix's column Map is null.");
146 
147  RCP<const tpetra_map_type> tpetraRowMap = mat->getRowMap ();
148  Epetra_Map epetraRowMap =
149  tpetraToEpetraMapTmpl<EGO, tpetra_map_type> (*tpetraRowMap);
150  RCP<const tpetra_map_type> tpetraColMap = mat->getColMap ();
151  Epetra_Map epetraColMap =
152  tpetraToEpetraMapTmpl<EGO, tpetra_map_type> (*tpetraColMap);
153  this->SetMaps (epetraRowMap, epetraColMap);
154 }
155 
156 template<class TpetraMatrixType>
157 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyRowCopy(int MyRow, int Length, int & NumEntries, double *Values, int * Indices) const
158 {
159  using inds_view = typename TpetraMatrixType::nonconst_local_inds_host_view_type;
160  using vals_view = typename TpetraMatrixType::nonconst_values_host_view_type;
161  static_assert (std::is_same<typename TpetraMatrixType::scalar_type, double>::value,
162  "This code assumes that Tpetra::CrsMatrix's scalar_type is int.");
163  static_assert (std::is_same<typename TpetraMatrixType::local_ordinal_type, int>::value,
164  "This code assumes that Tpetra::CrsMatrix's local_ordinal_type is int.");
165  inds_view IndicesView(Indices, Length);
166  vals_view ValuesView(Values, Length);
167  size_t num_entries = NumEntries;
168  tpetra_matrix_->getLocalRowCopy(MyRow, IndicesView, ValuesView, num_entries);
169  NumEntries = num_entries;
170  return 0;
171 }
172 
173 template<class TpetraMatrixType>
174 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyEntryView(int CurEntry, double * & Value, int & RowIndex, int & ColIndex)
175 {
176  //not implemented
177  return -1;
178 }
179 
180 template<class TpetraMatrixType>
181 int EpetraRowMatrix<TpetraMatrixType>::ExtractMyEntryView(int CurEntry, double const * & Value, int & RowIndex, int & ColIndex) const
182 {
183  //not implemented
184  return -1;
185 }
186 
187 template<class TpetraMatrixType>
188 int EpetraRowMatrix<TpetraMatrixType>::NumMyRowEntries(int MyRow, int & NumEntries) const
189 {
190  NumEntries = tpetra_matrix_->getNumEntriesInLocalRow(MyRow);
191  return 0;
192 }
193 
194 }//namespace Tpetra
195 
196 #endif // defined(TPETRA_ENABLE_DEPRECATED_CODE) && defined(HAVE_TPETRA_EPETRA)
197 
198 //here is the include-guard #endif:
199 
200 #endif