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