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
29namespace 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 const Epetra_BlockMap map = this->mat_->Map();
278 return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(map) );
279 }
280
281 template <class DerivedMat>
282 const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
283 MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
284 MatrixTraits<Epetra_RowMatrix>::node_t> >
285 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getRowMap_impl() const
286 {
287 // Must transform to a Tpetra::Map
288 const Epetra_Map rowmap = this->mat_->RowMatrixRowMap();
289 return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(rowmap) );
290 }
291
292 template <class DerivedMat>
293 const RCP<const Tpetra::Map<MatrixTraits<Epetra_RowMatrix>::local_ordinal_t,
294 MatrixTraits<Epetra_RowMatrix>::global_ordinal_t,
295 MatrixTraits<Epetra_RowMatrix>::node_t> >
296 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getColMap_impl() const
297 {
298 // Must transform this matrix' Epetra_Map to a Tpetra::Map
299 const Epetra_Map colmap = this->mat_->RowMatrixColMap();
300 return( Util::epetra_map_to_tpetra_map<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(colmap) );
301 }
302
303 template <class DerivedMat>
304 const RCP<const Teuchos::Comm<int> >
305 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getComm_impl() const
306 {
307 return Util::to_teuchos_comm(Teuchos::rcpFromRef(this->mat_->Comm()));
308 }
309
310 template <class DerivedMat>
311 bool
312 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isLocallyIndexed_impl() const
313 {
314 return this->mat_->IndicesAreLocal();
315 }
316
317 template <class DerivedMat>
318 bool
319 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::isGloballyIndexed_impl() const
320 {
321 return this->mat_->IndicesAreGlobal();
322 }
323
324
325 template <class DerivedMat>
326 RCP<const MatrixAdapter<DerivedMat> >
327 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::get_impl(const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > map, EDistribution distribution) const
328 {
329 // Delegate implementation to subclass
330#ifdef __CUDACC__
331 // NVCC doesn't seem to like the static_cast, even though it is valid
332 return dynamic_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->get_impl(map, distribution);
333#else
334 return static_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->get_impl(map, distribution);
335#endif
336 }
337
338 template <class DerivedMat>
339 RCP<const MatrixAdapter<DerivedMat> >
340 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::reindex_impl(Teuchos::RCP<const map_t> &contigRowMap,
341 Teuchos::RCP<const map_t> &contigColMap,
342 const EPhase current_phase) const
343 {
344 // Delegate implementation to subclass
345#ifdef __CUDACC__
346 // NVCC doesn't seem to like the static_cast, even though it is valid
347 return dynamic_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->reindex_impl(contigRowMap, contigColMap, current_phase);
348#else
349 return static_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->reindex_impl(contigRowMap, contigColMap, current_phase);
350#endif
351 }
352
353 template <class DerivedMat>
354 template<typename KV_S, typename KV_GO, typename KV_GS, typename host_ordinal_type_array, typename host_scalar_type_array>
355 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::local_ordinal_t
356 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::gather_impl(KV_S& nzvals, KV_GO& indices, KV_GS& pointers,
357 host_ordinal_type_array &perm_g2l,
358 host_ordinal_type_array &recvCountRows, host_ordinal_type_array &recvDisplRows,
359 host_ordinal_type_array &recvCounts, host_ordinal_type_array &recvDispls,
360 host_ordinal_type_array &transpose_map, host_scalar_type_array &nzvals_t,
361 bool column_major, EPhase current_phase) const
362 {
363#ifdef __CUDACC__
364 // NVCC doesn't seem to like the static_cast, even though it is valid
365 return dynamic_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->gather_impl(nzvals, indices, pointers, perm_g2l, recvCounts, recvDispls, recvCountRows, recvDisplRows,
366 transpose_map, nzvals_t, column_major, current_phase);
367#else
368 return static_cast<ConcreteMatrixAdapter<DerivedMat>*>(this)->gather_impl(nzvals, indices, pointers, perm_g2l, recvCounts, recvDispls, recvCountRows, recvDisplRows,
369 transpose_map, nzvals_t, column_major, current_phase);
370#endif
371 }
372
373 template <class DerivedMat>
374 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>
375 ::spmtx_ptr_t
376 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getSparseRowPtr() const
377 {
378 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_ptr_t sp_rowptr = nullptr;
379 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_idx_t sp_colind = nullptr;
380 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_val_t sp_values = nullptr;
381
382 this->mat_->ExtractCrsDataPointers(sp_rowptr, sp_colind, sp_values);
383
384 return sp_rowptr;
385 }
386
387 template <class DerivedMat>
388 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>
389 ::spmtx_idx_t
390 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getSparseColInd() const
391 {
392 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_ptr_t sp_rowptr = nullptr;
393 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_idx_t sp_colind = nullptr;
394 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_val_t sp_values = nullptr;
395
396 this->mat_->ExtractCrsDataPointers(sp_rowptr, sp_colind, sp_values);
397
398 return sp_colind;
399 }
400
401 template <class DerivedMat>
402 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>
403 ::spmtx_val_t
404 AbstractConcreteMatrixAdapter<Epetra_RowMatrix, DerivedMat>::getSparseValues() const
405 {
406 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_ptr_t sp_rowptr = nullptr;
407 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_idx_t sp_colind = nullptr;
408 typename AbstractConcreteMatrixAdapter<Epetra_RowMatrix,DerivedMat>::spmtx_val_t sp_values = nullptr;
409
410 this->mat_->ExtractCrsDataPointers(sp_rowptr, sp_colind, sp_values);
411
412 return sp_values;
413 }
414
415} // end namespace Amesos2
416
417#endif // AMESOS2_EPETRAROWMATRIX_ABSTRACTMATRIXADAPTER_DEF_HPP
Provides the Epetra_RowMatrix abstraction for the concrete Epetra row matric adapters.