11#ifndef AMESOS2_MATRIXADAPTER_DEF_HPP
12#define AMESOS2_MATRIXADAPTER_DEF_HPP
13#include <Tpetra_Util.hpp>
14#include "Amesos2_MatrixAdapter_decl.hpp"
15#include "Amesos2_ConcreteMatrixAdapter_def.hpp"
18#define TESTING_AMESOS2_WITH_TPETRA_REMOVE_UVM
19#if defined(TESTING_AMESOS2_WITH_TPETRA_REMOVE_UVM)
20#include "KokkosSparse_Utils.hpp"
21#include "KokkosSparse_SortCrs.hpp"
22#include "KokkosKernels_Sorting.hpp"
28 template <
class Matrix >
29 MatrixAdapter<Matrix>::MatrixAdapter(
const Teuchos::RCP<Matrix> m)
32 comm_ =
static_cast<const adapter_t*
>(
this)->getComm_impl();
33 col_map_ =
static_cast<const adapter_t*
>(
this)->getColMap_impl();
34 row_map_ =
static_cast<const adapter_t*
>(
this)->getRowMap_impl();
37 template <
class Matrix >
38 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
40 MatrixAdapter<Matrix>::getCrs_kokkos_view(KV_S & nzval,
43 typename MatrixAdapter<Matrix>::global_size_t& nnz,
44 const Teuchos::Ptr<const map_t> rowmap,
45 EStorage_Ordering ordering,
46 EDistribution distribution)
const
48 help_getCrs_kokkos_view(nzval, colind, rowptr,
49 nnz, rowmap, distribution, ordering,
50 typename adapter_t::get_crs_spec());
53 template <
class Matrix >
54 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
56 MatrixAdapter<Matrix>::getCrs_kokkos_view(KV_S & nzval,
59 typename MatrixAdapter<Matrix>::global_size_t& nnz,
60 EDistribution distribution,
61 EStorage_Ordering ordering)
const
63 const Teuchos::RCP<const map_t> rowmap
64 = Util::getDistributionMap<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(distribution,
65 this->getGlobalNumRows(),
67 this->getCrs_kokkos_view(nzval, colind, rowptr, nnz, Teuchos::ptrInArg(*rowmap), ordering, distribution);
70 template <
class Matrix >
71 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
73 MatrixAdapter<Matrix>::getCcs_kokkos_view(KV_S & nzval,
76 typename MatrixAdapter<Matrix>::global_size_t& nnz,
77 const Teuchos::Ptr<const map_t> colmap,
78 EStorage_Ordering ordering,
79 EDistribution distribution)
const
81 help_getCcs_kokkos_view(nzval, rowind, colptr,
82 nnz, colmap, distribution, ordering,
83 typename adapter_t::get_ccs_spec());
86 template <
class Matrix >
87 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
89 MatrixAdapter<Matrix>::getCcs_kokkos_view(KV_S & nzval,
92 typename MatrixAdapter<Matrix>::global_size_t& nnz,
93 EDistribution distribution,
94 EStorage_Ordering ordering)
const
96 const Teuchos::RCP<const map_t> colmap
97 = Util::getDistributionMap<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(distribution,
98 this->getGlobalNumCols(),
100 this->getCcs_kokkos_view(nzval, rowind, colptr, nnz, Teuchos::ptrInArg(*colmap), ordering, distribution);
104 template <
class Matrix >
105 typename MatrixAdapter<Matrix>::global_size_t
108 return static_cast<const adapter_t*
>(
this)->getGlobalNumRows_impl();
111 template <
class Matrix >
112 typename MatrixAdapter<Matrix>::global_size_t
115 return static_cast<const adapter_t*
>(
this)->getGlobalNumCols_impl();
118 template <
class Matrix >
119 typename MatrixAdapter<Matrix>::global_size_t
124 return (row_map_ != Teuchos::null) ? row_map_->getIndexBase() : 0;
127 template <
class Matrix >
128 typename MatrixAdapter<Matrix>::global_size_t
133 return (col_map_ != Teuchos::null) ? col_map_->getIndexBase() : 0;
136 template <
class Matrix >
137 typename MatrixAdapter<Matrix>::global_size_t
140 return static_cast<const adapter_t*
>(
this)->getGlobalNNZ_impl();
143 template <
class Matrix >
147 return row_map_->getLocalNumElements();
150 template <
class Matrix >
154 return col_map_->getLocalNumElements();
157 template <
class Matrix >
161 return static_cast<const adapter_t*
>(
this)->getLocalNNZ_impl();
165 template <
class Matrix >
169 std::ostringstream oss;
170 oss <<
"Amesos2::MatrixAdapter wrapping: ";
171 oss << mat_->description();
175 template <
class Matrix >
178 const Teuchos::EVerbosityLevel verbLevel)
const
181 return static_cast<const adapter_t*
>(
this)->
describe(out, verbLevel);
184 template <
class Matrix >
185 template <
class KV >
188 return static_cast<const adapter_t*
>(
this)->getSparseRowPtr_kokkos_view(view);
191 template <
class Matrix >
192 template <
class KV >
195 return static_cast<const adapter_t*
>(
this)->getSparseColInd_kokkos_view(view);
198 template <
class Matrix >
199 template <
class KV >
202 return static_cast<const adapter_t*
>(
this)->getSparseValues_kokkos_view(view);
208 template <
class Matrix >
209 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
211 MatrixAdapter<Matrix>::help_getCrs_kokkos_view(KV_S & nzval,
214 typename MatrixAdapter<Matrix>::global_size_t& nnz,
215 const Teuchos::Ptr<const map_t> rowmap,
223 do_getCrs_kokkos_view(nzval, colind, rowptr,
224 nnz, rowmap, distribution, ordering,
225 typename adapter_t::major_access());
228 template <
class Matrix >
229 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
231 MatrixAdapter<Matrix>::do_getCrs_kokkos_view(KV_S & nzval,
234 typename MatrixAdapter<Matrix>::global_size_t& nnz,
235 const Teuchos::Ptr<const map_t> rowmap,
245 if(this->row_map_ == Teuchos::null) {
249 size_t m = rowptr.extent(0)-1;
256 using Teuchos::ArrayView;
257 using Teuchos::OrdinalTraits;
261 RCP<const type> get_mat;
264 get_mat = rcp(
this,
false);
266 get_mat = get(rowmap, distribution);
277 RCP<const map_t> rmap = get_mat->getRowMap();
278 ArrayView<const global_ordinal_t> node_elements = rmap->getLocalElementList();
280 typename ArrayView<const global_ordinal_t>::iterator row_it, row_end;
281 row_end = node_elements.end();
283 size_t rowptr_ind = OrdinalTraits<size_t>::zero();
284 global_ordinal_t rowInd = OrdinalTraits<global_ordinal_t>::zero();
287 typename KV_GS::host_mirror_type host_rowptr = Kokkos::create_mirror_view(rowptr);
289 #if !defined(TESTING_AMESOS2_WITH_TPETRA_REMOVE_UVM)
293 Kokkos::View<scalar_t*, Kokkos::HostSpace>
294 mat_nzval(Kokkos::ViewAllocateWithoutInitializing(
"mat_nzval"), nzval.size());
296 Kokkos::View<global_ordinal_t*, Kokkos::HostSpace>
297 mat_colind(Kokkos::ViewAllocateWithoutInitializing(
"mat_colind"), colind.size());
299 ArrayView<scalar_t> nzval_arrayview(mat_nzval.data(), nzval.size());
300 ArrayView<global_ordinal_t> colind_arrayview(mat_colind.data(), colind.size());
302 for( row_it = node_elements.begin(); row_it != row_end; ++row_it ){
303 host_rowptr(rowptr_ind++) = rowInd;
304 size_t rowNNZ = get_mat->getGlobalRowNNZ(*row_it);
305 size_t nnzRet = OrdinalTraits<size_t>::zero();
306 ArrayView<global_ordinal_t> colind_view = colind_arrayview.view(rowInd,rowNNZ);
307 ArrayView<scalar_t> nzval_view = nzval_arrayview.view(rowInd,rowNNZ);
309 get_mat->getGlobalRowCopy(*row_it, colind_view, nzval_view, nnzRet);
311 for (
size_t rr = 0; rr < nnzRet ; rr++) {
312 colind_view[rr] -= rmap->getIndexBase();
319 Tpetra::sort2(colind_view.begin(), colind_view.end(), nzval_view.begin());
322 TEUCHOS_TEST_FOR_EXCEPTION( rowNNZ != nnzRet,
324 "Number of values returned different from "
325 "number of values reported");
328 host_rowptr(rowptr_ind) = nnz = rowInd;
330 deep_copy_or_assign_view(nzval, mat_nzval);
331 deep_copy_or_assign_view(colind, mat_colind);
332 deep_copy_or_assign_view(rowptr, host_rowptr);
335 global_host_idx_t mat_colind(Kokkos::ViewAllocateWithoutInitializing(
"mat_colind"), nzval.size());
336 global_host_val_t mat_nzvals(Kokkos::ViewAllocateWithoutInitializing(
"mat_nzvals"), colind.size());
338 auto host_colind = Kokkos::create_mirror_view(colind);
339 auto host_nzval = Kokkos::create_mirror_view(nzval);
342 for( row_it = node_elements.begin(); row_it != row_end; ++row_it ){
343 size_t rowNNZ = get_mat->getGlobalRowNNZ(*row_it);
344 size_t nnzRet = OrdinalTraits<size_t>::zero();
348 global_host_idx_t colind_view (&(mat_colind(rowInd)), rowNNZ);
349 global_host_val_t nzvals_view (&(mat_nzvals(rowInd)), rowNNZ);
351 global_ordinal_t row_id = *row_it;
352 get_mat->getGlobalRowCopy_kokkos_view(row_id, colind_view, nzvals_view, nnzRet);
354 TEUCHOS_TEST_FOR_EXCEPTION( rowNNZ != nnzRet,
356 "Number of values returned different from "
357 "number of values reported");
358 host_rowptr(rowptr_ind++) = rowInd;
361 host_rowptr(rowptr_ind) = nnz = rowInd;
364 if (rmap->getIndexBase() != 0) {
365 for (
size_t k = 0; k < mat_colind.extent(0); k++) {
366 mat_colind(k) -= rmap->getIndexBase();
371 deep_copy_or_assign_view(nzval, mat_nzvals);
372 deep_copy_or_assign_view(colind, mat_colind);
373 deep_copy_or_assign_view(rowptr, host_rowptr);
376 if( ordering == SORTED_INDICES ) {
377 using execution_space =
typename KV_GS::execution_space;
378 KokkosSparse::sort_crs_matrix <execution_space, KV_GS, KV_GO, KV_S>
379 (rowptr, colind, nzval);
384 template <
class Matrix >
385 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
387 MatrixAdapter<Matrix>::help_getCcs_kokkos_view(KV_S & nzval,
390 typename MatrixAdapter<Matrix>::global_size_t& nnz,
391 const Teuchos::Ptr<const map_t> colmap,
392 EDistribution distribution,
393 EStorage_Ordering ordering,
394 no_special_impl nsi)
const
399 do_getCcs_kokkos_view(nzval, rowind, colptr,
400 nnz, colmap, distribution, ordering,
401 typename adapter_t::major_access());
404 template <
class Matrix >
405 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
407 MatrixAdapter<Matrix>::do_getCcs_kokkos_view(KV_S & nzval,
410 typename MatrixAdapter<Matrix>::global_size_t& nnz,
411 const Teuchos::Ptr<const map_t> colmap,
412 EDistribution distribution,
413 EStorage_Ordering ordering,
416 using Teuchos::ArrayView;
421 KV_S nzval_tmp(Kokkos::ViewAllocateWithoutInitializing(
"nzval_tmp"), nzval.size());
422 KV_GO colind(Kokkos::ViewAllocateWithoutInitializing(
"colind"), rowind.size());
423 KV_GS rowptr(Kokkos::ViewAllocateWithoutInitializing(
"rowptr"), this->getGlobalNumRows() + 1);
425 this->getCrs_kokkos_view(nzval_tmp, colind, rowptr, nnz, colmap, ordering, distribution);
431 ArrayView<typename KV_S::value_type> av_nzval_tmp(nzval_tmp.data(), nzval_tmp.size());
432 ArrayView<typename KV_GO::value_type> av_colind(colind.data(), colind.size());
433 ArrayView<typename KV_GS::value_type> av_rowptr(rowptr.data(), rowptr.size());
434 ArrayView<typename KV_S::value_type> av_nzval(nzval.data(), nzval.size());
435 ArrayView<typename KV_GO::value_type> av_rowind(rowind.data(), rowind.size());
436 ArrayView<typename KV_GS::value_type> av_colptr(colptr.data(), colptr.size());
437 Util::transpose(av_nzval_tmp, av_colind, av_rowptr, av_nzval, av_rowind, av_colptr);
442 template <
class Matrix >
443 template<
typename KV_GO,
typename KV_S>
450 static_cast<const adapter_t*
>(
this)->getGlobalRowCopy_kokkos_view_impl(row, indices, vals, nnz);
453 template <
class Matrix >
455 MatrixAdapter<Matrix>::getMaxRowNNZ()
const
457 return static_cast<const adapter_t*
>(
this)->getMaxRowNNZ_impl();
460 template <
class Matrix >
462 MatrixAdapter<Matrix>::getMaxColNNZ()
const
464 return static_cast<const adapter_t*
>(
this)->getMaxColNNZ_impl();
467 template <
class Matrix >
469 MatrixAdapter<Matrix>::getGlobalRowNNZ(global_ordinal_t row)
const
471 return static_cast<const adapter_t*
>(
this)->getGlobalRowNNZ_impl(row);
474 template <
class Matrix >
476 MatrixAdapter<Matrix>::getLocalRowNNZ(local_ordinal_t row)
const
478 return static_cast<const adapter_t*
>(
this)->getLocalRowNNZ_impl(row);
481 template <
class Matrix >
483 MatrixAdapter<Matrix>::getGlobalColNNZ(global_ordinal_t col)
const
485 return static_cast<const adapter_t*
>(
this)->getGlobalColNNZ_impl(col);
488 template <
class Matrix >
490 MatrixAdapter<Matrix>::getLocalColNNZ(local_ordinal_t col)
const
492 return static_cast<const adapter_t*
>(
this)->getLocalColNNZ_impl(col);
495 template <
class Matrix >
497 MatrixAdapter<Matrix>::isLocallyIndexed()
const
499 return static_cast<const adapter_t*
>(
this)->isLocallyIndexed_impl();
502 template <
class Matrix >
504 MatrixAdapter<Matrix>::isGloballyIndexed()
const
506 return static_cast<const adapter_t*
>(
this)->isGloballyIndexed_impl();
510 template <
class Matrix >
511 Teuchos::RCP<const MatrixAdapter<Matrix> >
512 MatrixAdapter<Matrix>::get(
const Teuchos::Ptr<const map_t> map, EDistribution distribution)
const
514 return static_cast<const adapter_t*
>(
this)->get_impl(map, distribution);
518 template <
class Matrix >
519 Teuchos::RCP<const MatrixAdapter<Matrix> >
522 return static_cast<const adapter_t*
>(
this)->reindex_impl(contigRowMap, contigColMap, current_phase);
525 template <
class Matrix >
526 template<
typename KV_S,
typename KV_GO,
typename KV_GS,
typename host_ordinal_type_array,
typename host_scalar_type_array>
527 typename MatrixAdapter<Matrix>::local_ordinal_t
529 host_ordinal_type_array &perm_g2l,
530 host_ordinal_type_array &recvCountRows, host_ordinal_type_array &recvDisplRows,
531 host_ordinal_type_array &recvCounts, host_ordinal_type_array &recvDispls,
532 host_ordinal_type_array &transpose_map, host_scalar_type_array &nzvals_t,
533 bool column_major,
EPhase current_phase)
const
535 return static_cast<const adapter_t*
>(
this)->gather_impl(nzvals, indices, pointers, perm_g2l, recvCountRows, recvDisplRows, recvCounts, recvDispls,
536 transpose_map, nzvals_t, column_major, current_phase);
539 template <
class Matrix>
540 Teuchos::RCP<MatrixAdapter<Matrix> >
541 createMatrixAdapter(Teuchos::RCP<Matrix> m){
543 using Teuchos::rcp_const_cast;
545 if(m.is_null())
return Teuchos::null;
546 return( rcp(
new ConcreteMatrixAdapter<Matrix>(m)) );
549 template <
class Matrix>
550 Teuchos::RCP<const MatrixAdapter<Matrix> >
551 createConstMatrixAdapter(Teuchos::RCP<const Matrix> m){
553 using Teuchos::rcp_const_cast;
555 if(m.is_null())
return Teuchos::null;
556 return( rcp(
new ConcreteMatrixAdapter<Matrix>(rcp_const_cast<Matrix,const Matrix>(m))).getConst() );
EDistribution
Definition Amesos2_TypeDecl.hpp:89
@ CONTIGUOUS_AND_ROOTED
Definition Amesos2_TypeDecl.hpp:94
EStorage_Ordering
Definition Amesos2_TypeDecl.hpp:107
@ SORTED_INDICES
Definition Amesos2_TypeDecl.hpp:108
EPhase
Used to indicate a phase in the direct solution.
Definition Amesos2_TypeDecl.hpp:31
size_t getLocalNumRows() const
Get the number of rows local to the calling process.
Definition Amesos2_MatrixAdapter_def.hpp:145
void getGlobalRowCopy_kokkos_view(global_ordinal_t row, KV_GO &indices, KV_S &vals, size_t &nnz) const
Definition Amesos2_MatrixAdapter_def.hpp:445
Teuchos::RCP< const type > reindex(Teuchos::RCP< const map_t > &contigRowMap, Teuchos::RCP< const map_t > &contigColMap, const EPhase current_phase) const
Definition Amesos2_MatrixAdapter_def.hpp:520
local_ordinal_t gather(KV_S &nzvals, KV_GO &indices, KV_GS &pointers, host_ordinal_type_array &perm_g2l, host_ordinal_type_array &recvCountRows, host_ordinal_type_array &recvDisplRows, host_ordinal_type_array &recvCounts, host_ordinal_type_array &recvDispls, host_ordinal_type_array &transpose_map, host_scalar_type_array &nzvals_t, bool column_major, EPhase current_phase) const
Gather matrix to MPI-0.
Definition Amesos2_MatrixAdapter_def.hpp:528
global_size_t getColumnIndexBase() const
Get the indexbase for the column map.
Definition Amesos2_MatrixAdapter_def.hpp:129
std::string description() const
Returns a short description of this Solver.
Definition Amesos2_MatrixAdapter_def.hpp:167
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describes of this matrix adapter with some level of verbosity.
Definition Amesos2_MatrixAdapter_def.hpp:177
size_t getLocalNumCols() const
Get the number of columns local to the calling process.
Definition Amesos2_MatrixAdapter_def.hpp:152
global_size_t getGlobalNumRows() const
Get the number of rows in this matrix.
Definition Amesos2_MatrixAdapter_def.hpp:106
global_size_t getGlobalNNZ() const
Get the global number of non-zeros in this sparse matrix.
Definition Amesos2_MatrixAdapter_def.hpp:138
void returnColInd_kokkos_view(KV &view) const
Return kokkos view of CRS column indices of matrixA_.
Definition Amesos2_MatrixAdapter_def.hpp:193
size_t getLocalNNZ() const
Get the local number of non-zeros on this processor.
Definition Amesos2_MatrixAdapter_def.hpp:159
global_size_t getGlobalNumCols() const
Get the number of columns in this matrix.
Definition Amesos2_MatrixAdapter_def.hpp:113
void returnValues_kokkos_view(KV &view) const
Return kokkos view of CRS values of matrixA_.
Definition Amesos2_MatrixAdapter_def.hpp:200
void returnRowPtr_kokkos_view(KV &view) const
Return kokkos view of CRS row pointer of matrixA_.
Definition Amesos2_MatrixAdapter_def.hpp:186
global_size_t getRowIndexBase() const
Get the indexbase for the row map.
Definition Amesos2_MatrixAdapter_def.hpp:120
Indicates that the concrete class can use the generic getC{c|r}s methods implemented in MatrixAdapter...
Definition Amesos2_TypeDecl.hpp:57
Indicates that the object of an adapter provides row access to its data.
Definition Amesos2_TypeDecl.hpp:66