45 #ifndef AMESOS2_MATRIXADAPTER_DEF_HPP
46 #define AMESOS2_MATRIXADAPTER_DEF_HPP
47 #include <Tpetra_Util.hpp>
48 #include "Amesos2_MatrixAdapter_decl.hpp"
49 #include "Amesos2_ConcreteMatrixAdapter_def.hpp"
52 #define TESTING_AMESOS2_WITH_TPETRA_REMOVE_UVM
53 #if defined(TESTING_AMESOS2_WITH_TPETRA_REMOVE_UVM)
54 #include "KokkosSparse_Utils.hpp"
55 #include "KokkosSparse_SortCrs.hpp"
56 #include "KokkosKernels_Sorting.hpp"
62 template <
class Matrix >
63 MatrixAdapter<Matrix>::MatrixAdapter(
const Teuchos::RCP<Matrix> m)
66 comm_ =
static_cast<const adapter_t*
>(
this)->getComm_impl();
67 col_map_ =
static_cast<const adapter_t*
>(
this)->getColMap_impl();
68 row_map_ =
static_cast<const adapter_t*
>(
this)->getRowMap_impl();
71 template <
class Matrix >
72 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
74 MatrixAdapter<Matrix>::getCrs_kokkos_view(KV_S & nzval,
77 typename MatrixAdapter<Matrix>::global_size_t& nnz,
78 const Teuchos::Ptr<
const Tpetra::Map<local_ordinal_t, global_ordinal_t, node_t> > rowmap,
82 help_getCrs_kokkos_view(nzval, colind, rowptr,
83 nnz, rowmap, distribution, ordering,
84 typename adapter_t::get_crs_spec());
87 template <
class Matrix >
88 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
90 MatrixAdapter<Matrix>::getCrs_kokkos_view(KV_S & nzval,
93 typename MatrixAdapter<Matrix>::global_size_t& nnz,
97 const Teuchos::RCP<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap
98 = Util::getDistributionMap<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(distribution,
99 this->getGlobalNumRows(),
101 this->getCrs_kokkos_view(nzval, colind, rowptr, nnz, Teuchos::ptrInArg(*rowmap), ordering, distribution);
104 template <
class Matrix >
105 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
107 MatrixAdapter<Matrix>::getCcs_kokkos_view(KV_S & nzval,
110 typename MatrixAdapter<Matrix>::global_size_t& nnz,
111 const Teuchos::Ptr<
const Tpetra::Map<local_ordinal_t, global_ordinal_t, node_t> > colmap,
115 help_getCcs_kokkos_view(nzval, rowind, colptr,
116 nnz, colmap, distribution, ordering,
117 typename adapter_t::get_ccs_spec());
120 template <
class Matrix >
121 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
123 MatrixAdapter<Matrix>::getCcs_kokkos_view(KV_S & nzval,
126 typename MatrixAdapter<Matrix>::global_size_t& nnz,
130 const Teuchos::RCP<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap
131 = Util::getDistributionMap<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(distribution,
132 this->getGlobalNumCols(),
134 this->getCcs_kokkos_view(nzval, rowind, colptr, nnz, Teuchos::ptrInArg(*colmap), ordering, distribution);
138 template <
class Matrix >
139 typename MatrixAdapter<Matrix>::global_size_t
142 return static_cast<const adapter_t*
>(
this)->getGlobalNumRows_impl();
145 template <
class Matrix >
146 typename MatrixAdapter<Matrix>::global_size_t
149 return static_cast<const adapter_t*
>(
this)->getGlobalNumCols_impl();
152 template <
class Matrix >
153 typename MatrixAdapter<Matrix>::global_size_t
158 return (row_map_ != Teuchos::null) ? row_map_->getIndexBase() : 0;
161 template <
class Matrix >
162 typename MatrixAdapter<Matrix>::global_size_t
167 return (col_map_ != Teuchos::null) ? col_map_->getIndexBase() : 0;
170 template <
class Matrix >
171 typename MatrixAdapter<Matrix>::global_size_t
174 return static_cast<const adapter_t*
>(
this)->getGlobalNNZ_impl();
177 template <
class Matrix >
181 return row_map_->getLocalNumElements();
184 template <
class Matrix >
188 return col_map_->getLocalNumElements();
191 template <
class Matrix >
195 return static_cast<const adapter_t*
>(
this)->getLocalNNZ_impl();
199 template <
class Matrix >
203 std::ostringstream oss;
204 oss <<
"Amesos2::MatrixAdapter wrapping: ";
209 template <
class Matrix >
212 const Teuchos::EVerbosityLevel verbLevel)
const
215 template <
class Matrix >
216 template <
class KV >
219 return static_cast<const adapter_t*
>(
this)->getSparseRowPtr_kokkos_view(view);
222 template <
class Matrix >
223 template <
class KV >
226 return static_cast<const adapter_t*
>(
this)->getSparseColInd_kokkos_view(view);
229 template <
class Matrix >
230 template <
class KV >
233 return static_cast<const adapter_t*
>(
this)->getSparseValues_kokkos_view(view);
239 template <
class Matrix >
240 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
245 typename MatrixAdapter<Matrix>::global_size_t& nnz,
246 const Teuchos::Ptr<
const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap,
254 do_getCrs_kokkos_view(nzval, colind, rowptr,
255 nnz, rowmap, distribution, ordering,
256 typename adapter_t::major_access());
259 template <
class Matrix >
260 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
262 MatrixAdapter<Matrix>::do_getCrs_kokkos_view(KV_S & nzval,
265 typename MatrixAdapter<Matrix>::global_size_t& nnz,
266 const Teuchos::Ptr<
const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap,
276 if(this->row_map_ == Teuchos::null) {
277 this->returnValues_kokkos_view(nzval);
278 this->returnRowPtr_kokkos_view(rowptr);
279 this->returnColInd_kokkos_view(colind);
286 using Teuchos::ArrayView;
287 using Teuchos::OrdinalTraits;
291 RCP<const type> get_mat;
292 if( *rowmap == *this->row_map_ && distribution != CONTIGUOUS_AND_ROOTED ){
294 get_mat = rcp(
this,
false);
296 get_mat =
get(rowmap, distribution);
307 RCP<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rmap = get_mat->getRowMap();
308 ArrayView<const global_ordinal_t> node_elements = rmap->getLocalElementList();
310 typename ArrayView<const global_ordinal_t>::iterator row_it, row_end;
311 row_end = node_elements.end();
313 size_t rowptr_ind = OrdinalTraits<size_t>::zero();
314 global_ordinal_t rowInd = OrdinalTraits<global_ordinal_t>::zero();
317 typename KV_GS::HostMirror host_rowptr = Kokkos::create_mirror_view(rowptr);
319 #if !defined(TESTING_AMESOS2_WITH_TPETRA_REMOVE_UVM)
323 Kokkos::View<scalar_t*, Kokkos::HostSpace>
324 mat_nzval(Kokkos::ViewAllocateWithoutInitializing(
"mat_nzval"), nzval.size());
326 Kokkos::View<global_ordinal_t*, Kokkos::HostSpace>
327 mat_colind(Kokkos::ViewAllocateWithoutInitializing(
"mat_colind"), colind.size());
329 ArrayView<scalar_t> nzval_arrayview(mat_nzval.data(), nzval.size());
330 ArrayView<global_ordinal_t> colind_arrayview(mat_colind.data(), colind.size());
332 for( row_it = node_elements.begin(); row_it != row_end; ++row_it ){
333 host_rowptr(rowptr_ind++) = rowInd;
334 size_t rowNNZ = get_mat->getGlobalRowNNZ(*row_it);
335 size_t nnzRet = OrdinalTraits<size_t>::zero();
336 ArrayView<global_ordinal_t> colind_view = colind_arrayview.view(rowInd,rowNNZ);
337 ArrayView<scalar_t> nzval_view = nzval_arrayview.view(rowInd,rowNNZ);
339 get_mat->getGlobalRowCopy(*row_it, colind_view, nzval_view, nnzRet);
341 for (
size_t rr = 0; rr < nnzRet ; rr++) {
342 colind_view[rr] -= rmap->getIndexBase();
348 if( ordering == SORTED_INDICES ) {
349 Tpetra::sort2(colind_view.begin(), colind_view.end(), nzval_view.begin());
352 TEUCHOS_TEST_FOR_EXCEPTION( rowNNZ != nnzRet,
354 "Number of values returned different from "
355 "number of values reported");
358 host_rowptr(rowptr_ind) = nnz = rowInd;
360 deep_copy_or_assign_view(nzval, mat_nzval);
361 deep_copy_or_assign_view(colind, mat_colind);
362 deep_copy_or_assign_view(rowptr, host_rowptr);
365 global_host_idx_t mat_colind(Kokkos::ViewAllocateWithoutInitializing(
"mat_colind"), nzval.size());
366 global_host_val_t mat_nzvals(Kokkos::ViewAllocateWithoutInitializing(
"mat_nzvals"), colind.size());
368 auto host_colind = Kokkos::create_mirror_view(colind);
369 auto host_nzval = Kokkos::create_mirror_view(nzval);
372 for( row_it = node_elements.begin(); row_it != row_end; ++row_it ){
373 size_t rowNNZ = get_mat->getGlobalRowNNZ(*row_it);
374 size_t nnzRet = OrdinalTraits<size_t>::zero();
378 global_host_idx_t colind_view (&(mat_colind(rowInd)), rowNNZ);
379 global_host_val_t nzvals_view (&(mat_nzvals(rowInd)), rowNNZ);
381 global_ordinal_t row_id = *row_it;
382 get_mat->getGlobalRowCopy_kokkos_view(row_id, colind_view, nzvals_view, nnzRet);
384 TEUCHOS_TEST_FOR_EXCEPTION( rowNNZ != nnzRet,
386 "Number of values returned different from "
387 "number of values reported");
388 host_rowptr(rowptr_ind++) = rowInd;
391 host_rowptr(rowptr_ind) = nnz = rowInd;
394 if (rmap->getIndexBase() != 0) {
395 for (
size_t k = 0; k < mat_colind.extent(0); k++) {
396 mat_colind(k) -= rmap->getIndexBase();
401 deep_copy_or_assign_view(nzval, mat_nzvals);
402 deep_copy_or_assign_view(colind, mat_colind);
403 deep_copy_or_assign_view(rowptr, host_rowptr);
406 if( ordering == SORTED_INDICES ) {
407 using execution_space =
typename KV_GS::execution_space;
408 KokkosSparse::sort_crs_matrix <execution_space, KV_GS, KV_GO, KV_S>
409 (rowptr, colind, nzval);
414 template <
class Matrix >
415 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
417 MatrixAdapter<Matrix>::help_getCcs_kokkos_view(KV_S & nzval,
420 typename MatrixAdapter<Matrix>::global_size_t& nnz,
421 const Teuchos::Ptr<
const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap,
424 no_special_impl nsi)
const
429 do_getCcs_kokkos_view(nzval, rowind, colptr,
430 nnz, colmap, distribution, ordering,
431 typename adapter_t::major_access());
434 template <
class Matrix >
435 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
437 MatrixAdapter<Matrix>::do_getCcs_kokkos_view(KV_S & nzval,
440 typename MatrixAdapter<Matrix>::global_size_t& nnz,
441 const Teuchos::Ptr<
const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap,
446 using Teuchos::ArrayView;
451 KV_S nzval_tmp(Kokkos::ViewAllocateWithoutInitializing(
"nzval_tmp"), nzval.size());
452 KV_GO colind(Kokkos::ViewAllocateWithoutInitializing(
"colind"), rowind.size());
453 KV_GS rowptr(Kokkos::ViewAllocateWithoutInitializing(
"rowptr"), this->getGlobalNumRows() + 1);
455 this->getCrs_kokkos_view(nzval_tmp, colind, rowptr, nnz, colmap, ordering, distribution);
461 ArrayView<typename KV_S::value_type> av_nzval_tmp(nzval_tmp.data(), nzval_tmp.size());
462 ArrayView<typename KV_GO::value_type> av_colind(colind.data(), colind.size());
463 ArrayView<typename KV_GS::value_type> av_rowptr(rowptr.data(), rowptr.size());
464 ArrayView<typename KV_S::value_type> av_nzval(nzval.data(), nzval.size());
465 ArrayView<typename KV_GO::value_type> av_rowind(rowind.data(), rowind.size());
466 ArrayView<typename KV_GS::value_type> av_colptr(colptr.data(), colptr.size());
467 Util::transpose(av_nzval_tmp, av_colind, av_rowptr, av_nzval, av_rowind, av_colptr);
472 template <
class Matrix >
473 template<
typename KV_GO,
typename KV_S>
480 static_cast<const adapter_t*
>(
this)->getGlobalRowCopy_kokkos_view_impl(row, indices, vals, nnz);
483 template <
class Matrix >
487 return static_cast<const adapter_t*
>(
this)->getMaxRowNNZ_impl();
490 template <
class Matrix >
492 MatrixAdapter<Matrix>::getMaxColNNZ()
const
494 return static_cast<const adapter_t*
>(
this)->getMaxColNNZ_impl();
497 template <
class Matrix >
499 MatrixAdapter<Matrix>::getGlobalRowNNZ(global_ordinal_t row)
const
501 return static_cast<const adapter_t*
>(
this)->getGlobalRowNNZ_impl(row);
504 template <
class Matrix >
506 MatrixAdapter<Matrix>::getLocalRowNNZ(local_ordinal_t row)
const
508 return static_cast<const adapter_t*
>(
this)->getLocalRowNNZ_impl(row);
511 template <
class Matrix >
513 MatrixAdapter<Matrix>::getGlobalColNNZ(global_ordinal_t col)
const
515 return static_cast<const adapter_t*
>(
this)->getGlobalColNNZ_impl(col);
518 template <
class Matrix >
520 MatrixAdapter<Matrix>::getLocalColNNZ(local_ordinal_t col)
const
522 return static_cast<const adapter_t*
>(
this)->getLocalColNNZ_impl(col);
525 template <
class Matrix >
527 MatrixAdapter<Matrix>::isLocallyIndexed()
const
529 return static_cast<const adapter_t*
>(
this)->isLocallyIndexed_impl();
532 template <
class Matrix >
534 MatrixAdapter<Matrix>::isGloballyIndexed()
const
536 return static_cast<const adapter_t*
>(
this)->isGloballyIndexed_impl();
540 template <
class Matrix >
541 Teuchos::RCP<const MatrixAdapter<Matrix> >
542 MatrixAdapter<Matrix>::get(
const Teuchos::Ptr<
const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > map,
EDistribution distribution)
const
544 return static_cast<const adapter_t*
>(
this)->get_impl(map, distribution);
548 template <
class Matrix>
549 Teuchos::RCP<MatrixAdapter<Matrix> >
550 createMatrixAdapter(Teuchos::RCP<Matrix> m){
552 using Teuchos::rcp_const_cast;
554 if(m.is_null())
return Teuchos::null;
555 return( rcp(
new ConcreteMatrixAdapter<Matrix>(m)) );
558 template <
class Matrix>
559 Teuchos::RCP<const MatrixAdapter<Matrix> >
560 createConstMatrixAdapter(Teuchos::RCP<const Matrix> m){
562 using Teuchos::rcp_const_cast;
564 if(m.is_null())
return Teuchos::null;
565 return( rcp(
new ConcreteMatrixAdapter<Matrix>(rcp_const_cast<Matrix,const Matrix>(m))).getConst() );
570 #endif // AMESOS2_MATRIXADAPTER_DEF_HPP
EStorage_Ordering
Definition: Amesos2_TypeDecl.hpp:141
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:201
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
Indicates that the concrete class can use the generic getC{c|r}s methods implemented in MatrixAdapter...
Definition: Amesos2_TypeDecl.hpp:91
EDistribution
Definition: Amesos2_TypeDecl.hpp:123