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