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,
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,
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,
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,
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: ";
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>
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) {
246 this->returnValues_kokkos_view(nzval);
247 this->returnRowPtr_kokkos_view(rowptr);
248 this->returnColInd_kokkos_view(colind);
255 using Teuchos::ArrayView;
256 using Teuchos::OrdinalTraits;
260 RCP<const type> get_mat;
261 if( *rowmap == *this->row_map_ && distribution != CONTIGUOUS_AND_ROOTED ){
263 get_mat = rcp(
this,
false);
265 get_mat =
get(rowmap, distribution);
276 RCP<const map_t> rmap = get_mat->getRowMap();
277 ArrayView<const global_ordinal_t> node_elements = rmap->getLocalElementList();
279 typename ArrayView<const global_ordinal_t>::iterator row_it, row_end;
280 row_end = node_elements.end();
282 size_t rowptr_ind = OrdinalTraits<size_t>::zero();
283 global_ordinal_t rowInd = OrdinalTraits<global_ordinal_t>::zero();
286 typename KV_GS::HostMirror host_rowptr = Kokkos::create_mirror_view(rowptr);
288 #if !defined(TESTING_AMESOS2_WITH_TPETRA_REMOVE_UVM)
292 Kokkos::View<scalar_t*, Kokkos::HostSpace>
293 mat_nzval(Kokkos::ViewAllocateWithoutInitializing(
"mat_nzval"), nzval.size());
295 Kokkos::View<global_ordinal_t*, Kokkos::HostSpace>
296 mat_colind(Kokkos::ViewAllocateWithoutInitializing(
"mat_colind"), colind.size());
298 ArrayView<scalar_t> nzval_arrayview(mat_nzval.data(), nzval.size());
299 ArrayView<global_ordinal_t> colind_arrayview(mat_colind.data(), colind.size());
301 for( row_it = node_elements.begin(); row_it != row_end; ++row_it ){
302 host_rowptr(rowptr_ind++) = rowInd;
303 size_t rowNNZ = get_mat->getGlobalRowNNZ(*row_it);
304 size_t nnzRet = OrdinalTraits<size_t>::zero();
305 ArrayView<global_ordinal_t> colind_view = colind_arrayview.view(rowInd,rowNNZ);
306 ArrayView<scalar_t> nzval_view = nzval_arrayview.view(rowInd,rowNNZ);
308 get_mat->getGlobalRowCopy(*row_it, colind_view, nzval_view, nnzRet);
310 for (
size_t rr = 0; rr < nnzRet ; rr++) {
311 colind_view[rr] -= rmap->getIndexBase();
317 if( ordering == SORTED_INDICES ) {
318 Tpetra::sort2(colind_view.begin(), colind_view.end(), nzval_view.begin());
321 TEUCHOS_TEST_FOR_EXCEPTION( rowNNZ != nnzRet,
323 "Number of values returned different from "
324 "number of values reported");
327 host_rowptr(rowptr_ind) = nnz = rowInd;
329 deep_copy_or_assign_view(nzval, mat_nzval);
330 deep_copy_or_assign_view(colind, mat_colind);
331 deep_copy_or_assign_view(rowptr, host_rowptr);
334 global_host_idx_t mat_colind(Kokkos::ViewAllocateWithoutInitializing(
"mat_colind"), nzval.size());
335 global_host_val_t mat_nzvals(Kokkos::ViewAllocateWithoutInitializing(
"mat_nzvals"), colind.size());
337 auto host_colind = Kokkos::create_mirror_view(colind);
338 auto host_nzval = Kokkos::create_mirror_view(nzval);
341 for( row_it = node_elements.begin(); row_it != row_end; ++row_it ){
342 size_t rowNNZ = get_mat->getGlobalRowNNZ(*row_it);
343 size_t nnzRet = OrdinalTraits<size_t>::zero();
347 global_host_idx_t colind_view (&(mat_colind(rowInd)), rowNNZ);
348 global_host_val_t nzvals_view (&(mat_nzvals(rowInd)), rowNNZ);
350 global_ordinal_t row_id = *row_it;
351 get_mat->getGlobalRowCopy_kokkos_view(row_id, colind_view, nzvals_view, nnzRet);
353 TEUCHOS_TEST_FOR_EXCEPTION( rowNNZ != nnzRet,
355 "Number of values returned different from "
356 "number of values reported");
357 host_rowptr(rowptr_ind++) = rowInd;
360 host_rowptr(rowptr_ind) = nnz = rowInd;
363 if (rmap->getIndexBase() != 0) {
364 for (
size_t k = 0; k < mat_colind.extent(0); k++) {
365 mat_colind(k) -= rmap->getIndexBase();
370 deep_copy_or_assign_view(nzval, mat_nzvals);
371 deep_copy_or_assign_view(colind, mat_colind);
372 deep_copy_or_assign_view(rowptr, host_rowptr);
375 if( ordering == SORTED_INDICES ) {
376 using execution_space =
typename KV_GS::execution_space;
377 KokkosSparse::sort_crs_matrix <execution_space, KV_GS, KV_GO, KV_S>
378 (rowptr, colind, nzval);
383 template <
class Matrix >
384 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
386 MatrixAdapter<Matrix>::help_getCcs_kokkos_view(KV_S & nzval,
389 typename MatrixAdapter<Matrix>::global_size_t& nnz,
390 const Teuchos::Ptr<const map_t> colmap,
393 no_special_impl nsi)
const
398 do_getCcs_kokkos_view(nzval, rowind, colptr,
399 nnz, colmap, distribution, ordering,
400 typename adapter_t::major_access());
403 template <
class Matrix >
404 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
406 MatrixAdapter<Matrix>::do_getCcs_kokkos_view(KV_S & nzval,
409 typename MatrixAdapter<Matrix>::global_size_t& nnz,
410 const Teuchos::Ptr<const map_t> colmap,
415 using Teuchos::ArrayView;
420 KV_S nzval_tmp(Kokkos::ViewAllocateWithoutInitializing(
"nzval_tmp"), nzval.size());
421 KV_GO colind(Kokkos::ViewAllocateWithoutInitializing(
"colind"), rowind.size());
422 KV_GS rowptr(Kokkos::ViewAllocateWithoutInitializing(
"rowptr"), this->getGlobalNumRows() + 1);
424 this->getCrs_kokkos_view(nzval_tmp, colind, rowptr, nnz, colmap, ordering, distribution);
430 ArrayView<typename KV_S::value_type> av_nzval_tmp(nzval_tmp.data(), nzval_tmp.size());
431 ArrayView<typename KV_GO::value_type> av_colind(colind.data(), colind.size());
432 ArrayView<typename KV_GS::value_type> av_rowptr(rowptr.data(), rowptr.size());
433 ArrayView<typename KV_S::value_type> av_nzval(nzval.data(), nzval.size());
434 ArrayView<typename KV_GO::value_type> av_rowind(rowind.data(), rowind.size());
435 ArrayView<typename KV_GS::value_type> av_colptr(colptr.data(), colptr.size());
436 Util::transpose(av_nzval_tmp, av_colind, av_rowptr, av_nzval, av_rowind, av_colptr);
441 template <
class Matrix >
442 template<
typename KV_GO,
typename KV_S>
449 static_cast<const adapter_t*
>(
this)->getGlobalRowCopy_kokkos_view_impl(row, indices, vals, nnz);
452 template <
class Matrix >
456 return static_cast<const adapter_t*
>(
this)->getMaxRowNNZ_impl();
459 template <
class Matrix >
461 MatrixAdapter<Matrix>::getMaxColNNZ()
const
463 return static_cast<const adapter_t*
>(
this)->getMaxColNNZ_impl();
466 template <
class Matrix >
468 MatrixAdapter<Matrix>::getGlobalRowNNZ(global_ordinal_t row)
const
470 return static_cast<const adapter_t*
>(
this)->getGlobalRowNNZ_impl(row);
473 template <
class Matrix >
475 MatrixAdapter<Matrix>::getLocalRowNNZ(local_ordinal_t row)
const
477 return static_cast<const adapter_t*
>(
this)->getLocalRowNNZ_impl(row);
480 template <
class Matrix >
482 MatrixAdapter<Matrix>::getGlobalColNNZ(global_ordinal_t col)
const
484 return static_cast<const adapter_t*
>(
this)->getGlobalColNNZ_impl(col);
487 template <
class Matrix >
489 MatrixAdapter<Matrix>::getLocalColNNZ(local_ordinal_t col)
const
491 return static_cast<const adapter_t*
>(
this)->getLocalColNNZ_impl(col);
494 template <
class Matrix >
496 MatrixAdapter<Matrix>::isLocallyIndexed()
const
498 return static_cast<const adapter_t*
>(
this)->isLocallyIndexed_impl();
501 template <
class Matrix >
503 MatrixAdapter<Matrix>::isGloballyIndexed()
const
505 return static_cast<const adapter_t*
>(
this)->isGloballyIndexed_impl();
509 template <
class Matrix >
510 Teuchos::RCP<const MatrixAdapter<Matrix> >
511 MatrixAdapter<Matrix>::get(
const Teuchos::Ptr<const map_t> map,
EDistribution distribution)
const
513 return static_cast<const adapter_t*
>(
this)->get_impl(map, distribution);
517 template <
class Matrix >
518 Teuchos::RCP<const MatrixAdapter<Matrix> >
521 return static_cast<const adapter_t*
>(
this)->reindex_impl(contigRowMap, contigColMap, current_phase);
524 template <
class Matrix >
525 template<
typename KV_S,
typename KV_GO,
typename KV_GS,
typename host_ordinal_type_array,
typename host_scalar_type_array>
526 typename MatrixAdapter<Matrix>::local_ordinal_t
528 host_ordinal_type_array &recvCounts, host_ordinal_type_array &recvDispls,
529 host_ordinal_type_array &transpose_map, host_scalar_type_array &nzvals_t,
530 bool column_major,
EPhase current_phase)
const
532 return static_cast<const adapter_t*
>(
this)->gather_impl(nzvals, indices, pointers, recvCounts, recvDispls, transpose_map, nzvals_t,
533 column_major, current_phase);
536 template <
class Matrix>
537 Teuchos::RCP<MatrixAdapter<Matrix> >
538 createMatrixAdapter(Teuchos::RCP<Matrix> m){
540 using Teuchos::rcp_const_cast;
542 if(m.is_null())
return Teuchos::null;
543 return( rcp(
new ConcreteMatrixAdapter<Matrix>(m)) );
546 template <
class Matrix>
547 Teuchos::RCP<const MatrixAdapter<Matrix> >
548 createConstMatrixAdapter(Teuchos::RCP<const Matrix> m){
550 using Teuchos::rcp_const_cast;
552 if(m.is_null())
return Teuchos::null;
553 return( rcp(
new ConcreteMatrixAdapter<Matrix>(rcp_const_cast<Matrix,const Matrix>(m))).getConst() );
558 #endif // AMESOS2_MATRIXADAPTER_DEF_HPP
EPhase
Used to indicate a phase in the direct solution.
Definition: Amesos2_TypeDecl.hpp:31
EStorage_Ordering
Definition: Amesos2_TypeDecl.hpp:107
void transpose(ArrayView< Scalar > vals, ArrayView< GlobalOrdinal > indices, ArrayView< GlobalSizeT > ptr, ArrayView< Scalar > trans_vals, ArrayView< GlobalOrdinal > trans_indices, ArrayView< GlobalSizeT > trans_ptr)
std::string description() const
Returns a short description of this Solver.
Definition: Amesos2_MatrixAdapter_def.hpp:167
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:42
Indicates that the concrete class can use the generic getC{c|r}s methods implemented in MatrixAdapter...
Definition: Amesos2_TypeDecl.hpp:57
EDistribution
Definition: Amesos2_TypeDecl.hpp:89