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  return 0;
186  }
187 
188  template <class DerivedMat>
189  size_t
190  AbstractConcreteMatrixAdapter<
191  Epetra_RowMatrix,
192  DerivedMat>::getGlobalRowNNZ_impl(global_ordinal_t row) const
193  {
194  // check whether row is local, then transform to local index
195  Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
196  int gid = Teuchos::as<int>(row);
197  TEUCHOS_TEST_FOR_EXCEPTION( !rowmap.MyGID(gid),
198  std::invalid_argument,
199  "The specified global row id does not belong to me" );
200  int lid = rowmap.LID(gid);
201  int nnz = 0;
202  this->mat_->NumMyRowEntries(lid, nnz);
203  return nnz;
204  }
205 
206  template <class DerivedMat>
207  size_t
208  AbstractConcreteMatrixAdapter<
209  Epetra_RowMatrix,
210  DerivedMat>::getLocalRowNNZ_impl(local_ordinal_t row) const
211  {
212  Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
213  int lid = Teuchos::as<int>(row);
214  TEUCHOS_TEST_FOR_EXCEPTION( !rowmap.MyLID(lid),
215  std::invalid_argument,
216  "The specified local row id does not beloing to me" );
217  int num_entries = 0;
218  this->mat_->NumMyRowEntries(row, num_entries);
219  return num_entries;
220  }
221 
222  template <class DerivedMat>
223  size_t
224  AbstractConcreteMatrixAdapter<
225  Epetra_RowMatrix,
226  DerivedMat>::getGlobalColNNZ_impl(global_ordinal_t col) const
227  {
228  TEUCHOS_TEST_FOR_EXCEPTION( true,
229  std::runtime_error,
230  "Column access to row-based object not yet supported. "
231  "Please contact the Amesos2 developers." );
232  return 0;
233  }
234 
235  template <class DerivedMat>
236  size_t
237  AbstractConcreteMatrixAdapter<
238  Epetra_RowMatrix,
239  DerivedMat>::getLocalColNNZ_impl(local_ordinal_t col) const
240  {
241  TEUCHOS_TEST_FOR_EXCEPTION( true,
242  std::runtime_error,
243  "Column access to row-based object not yet supported. "
244  "Please contact the Amesos2 developers." );
245  return 0;
246  }
247 
248  template <class DerivedMat>
249  const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
250  MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
251  MatrixTraits<Epetra_RowMatrix>::node_t> >
252  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getRowMap_impl() const
253  {
254  // Must transform to a Tpetra::Map
255  const Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
256  return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(rowmap) );
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>::getColMap_impl() const
264  {
265  // Must transform this matrix' Epetra_Map to a Tpetra::Map
266  const Epetra_Map colmap = this->mat_->RowMatrixColMap();
267  return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(colmap) );
268  }
269 
270  template <class DerivedMat>
271  const RCP<const Teuchos::Comm<int> >
272  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getComm_impl() const
273  {
274  return Util::to_teuchos_comm(Teuchos::rcpFromRef(this->mat_->Comm()));
275  }
276 
277  template <class DerivedMat>
278  bool
279  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isLocallyIndexed_impl() const
280  {
281  return this->mat_->IndicesAreLocal();
282  }
283 
284  template <class DerivedMat>
285  bool
286  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isGloballyIndexed_impl() const
287  {
288  return this->mat_->IndicesAreGlobal();
289  }
290 
291  template <class DerivedMat>
292  RCP<const MatrixAdapter<DerivedMat> >
293  AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::get_impl(const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > map) const
294  {
295  // Delegate implementation to subclass
296 #ifdef __CUDACC__
297  // NVCC doesn't seem to like the static_cast, even though it is valid
298  return dynamic_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->get_impl(map);
299 #else
300  return static_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->get_impl(map);
301 #endif
302  }
303 
304 } // end namespace Amesos2
305 
306 #endif // AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
Provides the Epetra_RowMatrix abstraction for the concrete Epetra row matric adapters.