Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_EpetraRowMatrix_AbstractMatrixAdapter_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Amesos2: Templated Direct Sparse Solver Package
4 //
5 // Copyright 2011 NTESS and the Amesos2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 
19 #ifndef AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP
20 #define AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP
21 
22 #include <Epetra_RowMatrix.h>
23 #include <Epetra_Map.h>
24 #include <Epetra_Comm.h>
25 
27 
28 
29 namespace Amesos2 {
30 
31  using Teuchos::RCP;
32  using Teuchos::ArrayView;
33 
34  template <class DerivedMat>
35  AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::AbstractConcreteMatrixAdapter(RCP<DerivedMat> m)
36  : MatrixAdapter<DerivedMat>(m)
37  {
38  // anything else? probs not
39  }
40 
41  // implementation functions
42  template <class DerivedMat>
43  void
44  AbstractConcreteMatrixAdapter<
45  Epetra_RowMatrix,
46  DerivedMat>::getGlobalRowCopy_impl(global_ordinal_t row,
47  const ArrayView<global_ordinal_t>& indices,
48  const ArrayView<scalar_t>& vals,
49  size_t& nnz) const
50  {
51  using Teuchos::as;
52  const int local_row = this->row_map_->getLocalElement(row);
53  bool threw = false;
54 
55  Teuchos::Array<local_ordinal_t> epetra_lcl_inds_buf;
56  Teuchos::ArrayView<local_ordinal_t> epetra_lcl_inds;
57  if (! std::is_same<global_ordinal_t, local_ordinal_t>::value) {
58  int num_ent = 0;
59  int err = 0;
60  try {
61  err = this->mat_->NumMyRowEntries (local_row, num_ent);
62  }
63  catch (int integer_exception) {
64  threw = true;
65  err = integer_exception;
66  }
67  TEUCHOS_TEST_FOR_EXCEPTION
68  (threw && err != 0, std::runtime_error, "Epetra_RowMatrix::"
69  "NumMyRowEntries, called on local row " << local_row << ", threw "
70  "an integer exception " << err << ".");
71  TEUCHOS_TEST_FOR_EXCEPTION
72  (! threw && err != 0, std::runtime_error, "Epetra_RowMatrix returned "
73  "error code " << err << " from NumMyRowEntries for local row "
74  << local_row << ".");
75  epetra_lcl_inds_buf.resize (num_ent);
76  epetra_lcl_inds = epetra_lcl_inds_buf ();
77  }
78  else { // local_ordinal_t == global_ordinal_t
79  using Teuchos::av_reinterpret_cast;
80  epetra_lcl_inds = av_reinterpret_cast<int> (indices);
81  }
82 
83  int nnz_ret = 0;
84  int rowmatrix_return_val = 0;
85  try {
86  rowmatrix_return_val =
87  this->mat_->ExtractMyRowCopy(local_row,
88  as<int>(std::min(epetra_lcl_inds.size(), vals.size())),
89  nnz_ret,
90  vals.getRawPtr(),
91  epetra_lcl_inds.getRawPtr());
92  }
93  catch (int integer_exception) {
94  threw = true;
95  rowmatrix_return_val = integer_exception;
96  }
97  TEUCHOS_TEST_FOR_EXCEPTION
98  (threw && rowmatrix_return_val != 0, std::runtime_error,
99  "Epetra_RowMatrix::ExtractMyRowCopy, called on local row " << local_row
100  << ", threw an integer exception " << rowmatrix_return_val << ".");
101  TEUCHOS_TEST_FOR_EXCEPTION
102  (! threw && rowmatrix_return_val != 0, std::runtime_error,
103  "Epetra_RowMatrix object returned error code "
104  << rowmatrix_return_val << " from ExtractMyRowCopy." );
105  nnz = as<size_t>(nnz_ret);
106 
107  // Epetra_CrsMatrix::ExtractMyRowCopy returns local column
108  // indices, so transform these into global indices
109  for( size_t i = 0; i < nnz; ++i ){
110  indices[i] = this->col_map_->getGlobalElement(epetra_lcl_inds[i]);
111  }
112  }
113 
114  template <class DerivedMat>
115  void
116  AbstractConcreteMatrixAdapter<
117  Epetra_RowMatrix,
118  DerivedMat>::getGlobalColCopy_impl(global_ordinal_t col,
119  const ArrayView<global_ordinal_t>& indices,
120  const ArrayView<scalar_t>& vals,
121  size_t& nnz) const
122  {
123  TEUCHOS_TEST_FOR_EXCEPTION( true,
124  std::runtime_error,
125  "Column access to row-based object not yet supported. "
126  "Please contact the Amesos2 developers." );
127  }
128 
129 
130  template <class DerivedMat>
131  template<typename KV_GO, typename KV_S>
132  void
133  AbstractConcreteMatrixAdapter<
134  Epetra_RowMatrix,
135  DerivedMat>::getGlobalRowCopy_kokkos_view_impl(global_ordinal_t row,
136  KV_GO & indices,
137  KV_S & vals,
138  size_t& nnz) const
139  {
140  using index_t = typename KV_GO::value_type;
141  using value_t = typename KV_S::value_type;
142  ArrayView<value_t> vals_array (vals.data(), vals.extent(0));
143  ArrayView<index_t> indices_array (indices.data(), indices.extent(0));
144 
145  this->getGlobalRowCopy_impl(row, indices_array, vals_array, nnz);
146  }
147 
148 
149 
150  template <class DerivedMat>
151  typename AbstractConcreteMatrixAdapter<
152  Epetra_RowMatrix,
153  DerivedMat>::global_size_t
154  AbstractConcreteMatrixAdapter<
155  Epetra_RowMatrix,
156  DerivedMat>::getGlobalNNZ_impl() const
157  {
158  return Teuchos::as<global_size_t>(this->mat_->NumGlobalNonzeros());
159  }
160 
161  template <class DerivedMat>
162  size_t
163  AbstractConcreteMatrixAdapter<
164  Epetra_RowMatrix,
165  DerivedMat>::getLocalNNZ_impl() const
166  {
167  return Teuchos::as<size_t>(this->mat_->NumMyNonzeros());
168  }
169 
170  template <class DerivedMat>
171  typename AbstractConcreteMatrixAdapter<
172  Epetra_RowMatrix,
173  DerivedMat>::global_size_t
174  AbstractConcreteMatrixAdapter<
175  Epetra_RowMatrix,
176  DerivedMat>::getGlobalNumRows_impl() const
177  {
178  return Teuchos::as<global_size_t>(this->mat_->NumGlobalRows());
179  }
180 
181  template <class DerivedMat>
182  typename AbstractConcreteMatrixAdapter<
183  Epetra_RowMatrix,
184  DerivedMat>::global_size_t
185  AbstractConcreteMatrixAdapter<
186  Epetra_RowMatrix,
187  DerivedMat>::getGlobalNumCols_impl() const
188  {
189  return Teuchos::as<global_size_t>(this->mat_->NumGlobalCols());
190  }
191 
192  template <class DerivedMat>
193  size_t
194  AbstractConcreteMatrixAdapter<
195  Epetra_RowMatrix,
196  DerivedMat>::getMaxRowNNZ_impl() const
197  {
198  return Teuchos::as<size_t>(this->mat_->MaxNumEntries());
199  }
200 
201  template <class DerivedMat>
202  size_t
203  AbstractConcreteMatrixAdapter<
204  Epetra_RowMatrix,
205  DerivedMat>::getMaxColNNZ_impl() const
206  {
207  TEUCHOS_TEST_FOR_EXCEPTION( true,
208  std::runtime_error,
209  "Column access to row-based object not yet supported. "
210  "Please contact the Amesos2 developers." );
211  }
212 
213  template <class DerivedMat>
214  size_t
215  AbstractConcreteMatrixAdapter<
216  Epetra_RowMatrix,
217  DerivedMat>::getGlobalRowNNZ_impl(global_ordinal_t row) const
218  {
219  // check whether row is local, then transform to local index
220  Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
221  int gid = Teuchos::as<int>(row);
222  TEUCHOS_TEST_FOR_EXCEPTION( !rowmap.MyGID(gid),
223  std::invalid_argument,
224  "The specified global row id does not belong to me" );
225  int lid = rowmap.LID(gid);
226  int nnz = 0;
227  this->mat_->NumMyRowEntries(lid, nnz);
228  return nnz;
229  }
230 
231  template <class DerivedMat>
232  size_t
233  AbstractConcreteMatrixAdapter<
234  Epetra_RowMatrix,
235  DerivedMat>::getLocalRowNNZ_impl(local_ordinal_t row) const
236  {
237  Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
238  int lid = Teuchos::as<int>(row);
239  TEUCHOS_TEST_FOR_EXCEPTION( !rowmap.MyLID(lid),
240  std::invalid_argument,
241  "The specified local row id does not beloing to me" );
242  int num_entries = 0;
243  this->mat_->NumMyRowEntries(row, num_entries);
244  return num_entries;
245  }
246 
247  template <class DerivedMat>
248  size_t
249  AbstractConcreteMatrixAdapter<
250  Epetra_RowMatrix,
251  DerivedMat>::getGlobalColNNZ_impl(global_ordinal_t col) const
252  {
253  TEUCHOS_TEST_FOR_EXCEPTION( true,
254  std::runtime_error,
255  "Column access to row-based object not yet supported. "
256  "Please contact the Amesos2 developers." );
257  }
258 
259  template <class DerivedMat>
260  size_t
261  AbstractConcreteMatrixAdapter<
262  Epetra_RowMatrix,
263  DerivedMat>::getLocalColNNZ_impl(local_ordinal_t col) const
264  {
265  TEUCHOS_TEST_FOR_EXCEPTION( true,
266  std::runtime_error,
267  "Column access to row-based object not yet supported. "
268  "Please contact the Amesos2 developers." );
269  }
270 
271  template <class DerivedMat>
272  const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
273  MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
274  MatrixTraits<Epetra_RowMatrix>::node_t> >
275  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getMap_impl() const
276  {
277  // Should Map() be used (returns Epetra_BlockMap)
278  /*
279  printf("Amesos2_EpetraRowMatrix_AbstractMatrixAdapter: Epetra does not support a 'getMap()' method. Returning rcp(Teuchos::null). \
280  If your map contains non-contiguous GIDs please use Tpetra instead of Epetra. \n");
281  */
282  return( Teuchos::null );
283  }
284 
285  template <class DerivedMat>
286  const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
287  MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
288  MatrixTraits<Epetra_RowMatrix>::node_t> >
289  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getRowMap_impl() const
290  {
291  // Must transform to a Tpetra::Map
292  const Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
293  return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(rowmap) );
294  }
295 
296  template <class DerivedMat>
297  const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
298  MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
299  MatrixTraits<Epetra_RowMatrix>::node_t> >
300  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getColMap_impl() const
301  {
302  // Must transform this matrix' Epetra_Map to a Tpetra::Map
303  const Epetra_Map colmap = this->mat_->RowMatrixColMap();
304  return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(colmap) );
305  }
306 
307  template <class DerivedMat>
308  const RCP<const Teuchos::Comm<int> >
309  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getComm_impl() const
310  {
311  return Util::to_teuchos_comm(Teuchos::rcpFromRef(this->mat_->Comm()));
312  }
313 
314  template <class DerivedMat>
315  bool
316  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isLocallyIndexed_impl() const
317  {
318  return this->mat_->IndicesAreLocal();
319  }
320 
321  template <class DerivedMat>
322  bool
323  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isGloballyIndexed_impl() const
324  {
325  return this->mat_->IndicesAreGlobal();
326  }
327 
328 
329  template <class DerivedMat>
330  RCP<const MatrixAdapter<DerivedMat> >
331  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::get_impl(const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > map, EDistribution distribution) const
332  {
333  // Delegate implementation to subclass
334 #ifdef __CUDACC__
335  // NVCC doesn't seem to like the static_cast, even though it is valid
336  return dynamic_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->get_impl(map, distribution);
337 #else
338  return static_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->get_impl(map, distribution);
339 #endif
340  }
341 
342  template <class DerivedMat>
343  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>
344  ::spmtx_ptr_t
345  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getSparseRowPtr() const
346  {
347  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_ptr_t sp_rowptr = nullptr;
348  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_idx_t sp_colind = nullptr;
349  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_val_t sp_values = nullptr;
350 
351  this->mat_->ExtractCrsDataPointers(sp_rowptr, sp_colind, sp_values);
352 
353  return sp_rowptr;
354  }
355 
356  template <class DerivedMat>
357  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>
358  ::spmtx_idx_t
359  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getSparseColInd() const
360  {
361  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_ptr_t sp_rowptr = nullptr;
362  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_idx_t sp_colind = nullptr;
363  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_val_t sp_values = nullptr;
364 
365  this->mat_->ExtractCrsDataPointers(sp_rowptr, sp_colind, sp_values);
366 
367  return sp_colind;
368  }
369 
370  template <class DerivedMat>
371  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>
372  ::spmtx_val_t
373  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getSparseValues() const
374  {
375  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_ptr_t sp_rowptr = nullptr;
376  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_idx_t sp_colind = nullptr;
377  typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_val_t sp_values = nullptr;
378 
379  this->mat_->ExtractCrsDataPointers(sp_rowptr, sp_colind, sp_values);
380 
381  return sp_values;
382  }
383 
384 } // end namespace Amesos2
385 
386 #endif // AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP
const RCP< const Teuchos::Comm< int > > to_teuchos_comm(RCP< const Epetra_Comm > c)
Transform an Epetra_Comm object into a Teuchos::Comm object.
Provides the Epetra_RowMatrix abstraction for the concrete Epetra row matric adapters.
EDistribution
Definition: Amesos2_TypeDecl.hpp:89