52 #ifndef AMESOS2_UTIL_HPP 53 #define AMESOS2_UTIL_HPP 55 #include "Amesos2_config.h" 57 #include "Teuchos_RCP.hpp" 58 #include "Teuchos_BLAS_types.hpp" 59 #include "Teuchos_Array.hpp" 60 #include "Teuchos_ArrayView.hpp" 61 #include "Teuchos_FancyOStream.hpp" 63 #include <Tpetra_Map.hpp> 64 #include <Tpetra_DistObject_decl.hpp> 65 #include <Tpetra_ComputeGatherMap.hpp> 71 #ifdef HAVE_AMESOS2_EPETRA 72 #include <Epetra_Map.h> 75 #ifdef HAVE_AMESOS2_METIS 90 using Teuchos::ArrayView;
93 using Meta::if_then_else;
111 template <
typename LO,
typename GO,
typename GS,
typename Node>
112 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
113 getGatherMap(
const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> > &map );
116 template <
typename LO,
typename GO,
typename GS,
typename Node>
117 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
119 GS num_global_elements,
120 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
122 const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> >& map = Teuchos::null);
125 #ifdef HAVE_AMESOS2_EPETRA 132 template <
typename LO,
typename GO,
typename GS,
typename Node>
133 RCP<Tpetra::Map<LO,GO,Node> >
141 template <
typename LO,
typename GO,
typename GS,
typename Node>
150 const RCP<const Teuchos::Comm<int> >
to_teuchos_comm(RCP<const Epetra_Comm> c);
157 const RCP<const Epetra_Comm>
to_epetra_comm(RCP<
const Teuchos::Comm<int> > c);
158 #endif // HAVE_AMESOS2_EPETRA 165 template <
typename Scalar,
166 typename GlobalOrdinal,
167 typename GlobalSizeT>
169 ArrayView<GlobalOrdinal> indices,
170 ArrayView<GlobalSizeT> ptr,
171 ArrayView<Scalar> trans_vals,
172 ArrayView<GlobalOrdinal> trans_indices,
173 ArrayView<GlobalSizeT> trans_ptr);
188 template <
typename Scalar1,
typename Scalar2>
189 void scale(ArrayView<Scalar1> vals,
size_t l,
190 size_t ld, ArrayView<Scalar2> s);
210 template <
typename Scalar1,
typename Scalar2,
class BinaryOp>
211 void scale(ArrayView<Scalar1> vals,
size_t l,
212 size_t ld, ArrayView<Scalar2> s, BinaryOp binary_op);
216 void printLine( Teuchos::FancyOStream &out );
221 template <
class T0,
class T1 >
222 struct getStdCplxType
224 using common_type =
typename std::common_type<T0,T1>::type;
225 using type = common_type;
228 template <
class T0,
class T1 >
229 struct getStdCplxType< T0, T1* >
231 using common_type =
typename std::common_type<T0,T1>::type;
232 using type = common_type;
235 #if defined(HAVE_TEUCHOS_COMPLEX) && defined(HAVE_AMESOS2_KOKKOS) 236 template <
class T0 >
237 struct getStdCplxType< T0, Kokkos::complex<T0>* >
239 using type = std::complex<T0>;
242 template <
class T0 ,
class T1 >
243 struct getStdCplxType< T0, Kokkos::complex<T1>* >
245 using common_type =
typename std::common_type<T0,T1>::type;
246 using type = std::complex<common_type>;
254 #ifndef DOXYGEN_SHOULD_SKIP_THIS 277 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 278 template <
class M,
typename S,
typename GO,
typename GS,
class Op>
279 struct same_gs_helper
281 static void do_get(
const Teuchos::Ptr<const M> mat,
282 const ArrayView<typename M::scalar_t> nzvals,
283 const ArrayView<typename M::global_ordinal_t> indices,
284 const ArrayView<GS> pointers,
287 const Tpetra::Map<
typename M::local_ordinal_t,
288 typename M::global_ordinal_t,
289 typename M::node_t> > map,
293 Op::apply(mat, nzvals, indices, pointers, nnz, map, distribution, ordering);
297 template <
class M,
typename S,
typename GO,
typename GS,
class Op>
298 struct diff_gs_helper
300 static void do_get(
const Teuchos::Ptr<const M> mat,
301 const ArrayView<typename M::scalar_t> nzvals,
302 const ArrayView<typename M::global_ordinal_t> indices,
303 const ArrayView<GS> pointers,
306 const Tpetra::Map<
typename M::local_ordinal_t,
307 typename M::global_ordinal_t,
308 typename M::node_t> > map,
312 typedef typename M::global_size_t mat_gs_t;
313 typename ArrayView<GS>::size_type i, size = pointers.size();
314 Teuchos::Array<mat_gs_t> pointers_tmp(size);
315 mat_gs_t nnz_tmp = 0;
317 Op::apply(mat, nzvals, indices, pointers_tmp, nnz_tmp, map, distribution, ordering);
319 for (i = 0; i < size; ++i){
320 pointers[i] = Teuchos::as<GS>(pointers_tmp[i]);
322 nnz = Teuchos::as<GS>(nnz_tmp);
326 template <
class M,
typename S,
typename GO,
typename GS,
class Op>
327 struct same_go_helper
329 static void do_get(
const Teuchos::Ptr<const M> mat,
330 const ArrayView<typename M::scalar_t> nzvals,
331 const ArrayView<GO> indices,
332 const ArrayView<GS> pointers,
335 const Tpetra::Map<
typename M::local_ordinal_t,
336 typename M::global_ordinal_t,
337 typename M::node_t> > map,
341 typedef typename M::global_size_t mat_gs_t;
342 if_then_else<is_same<GS,mat_gs_t>::value,
343 same_gs_helper<M,S,GO,GS,Op>,
344 diff_gs_helper<M,S,GO,GS,Op> >::type::do_get(mat, nzvals, indices,
346 distribution, ordering);
350 template <
class M,
typename S,
typename GO,
typename GS,
class Op>
351 struct diff_go_helper
353 static void do_get(
const Teuchos::Ptr<const M> mat,
354 const ArrayView<typename M::scalar_t> nzvals,
355 const ArrayView<GO> indices,
356 const ArrayView<GS> pointers,
359 const Tpetra::Map<
typename M::local_ordinal_t,
360 typename M::global_ordinal_t,
361 typename M::node_t> > map,
365 typedef typename M::global_ordinal_t mat_go_t;
366 typedef typename M::global_size_t mat_gs_t;
367 typename ArrayView<GO>::size_type i, size = indices.size();
368 Teuchos::Array<mat_go_t> indices_tmp(size);
370 if_then_else<is_same<GS,mat_gs_t>::value,
371 same_gs_helper<M,S,GO,GS,Op>,
372 diff_gs_helper<M,S,GO,GS,Op> >::type::do_get(mat, nzvals, indices_tmp,
374 distribution, ordering);
376 for (i = 0; i < size; ++i){
377 indices[i] = Teuchos::as<GO>(indices_tmp[i]);
382 template <
class M,
typename S,
typename GO,
typename GS,
class Op>
383 struct same_scalar_helper
385 static void do_get(
const Teuchos::Ptr<const M> mat,
386 const ArrayView<S> nzvals,
387 const ArrayView<GO> indices,
388 const ArrayView<GS> pointers,
391 const Tpetra::Map<
typename M::local_ordinal_t,
392 typename M::global_ordinal_t,
393 typename M::node_t> > map,
397 typedef typename M::global_ordinal_t mat_go_t;
398 if_then_else<is_same<GO,mat_go_t>::value,
399 same_go_helper<M,S,GO,GS,Op>,
400 diff_go_helper<M,S,GO,GS,Op> >::type::do_get(mat, nzvals, indices,
402 distribution, ordering);
406 template <
class M,
typename S,
typename GO,
typename GS,
class Op>
407 struct diff_scalar_helper
409 static void do_get(
const Teuchos::Ptr<const M> mat,
410 const ArrayView<S> nzvals,
411 const ArrayView<GO> indices,
412 const ArrayView<GS> pointers,
415 const Tpetra::Map<
typename M::local_ordinal_t,
416 typename M::global_ordinal_t,
417 typename M::node_t> > map,
421 typedef typename M::scalar_t mat_scalar_t;
422 typedef typename M::global_ordinal_t mat_go_t;
423 typename ArrayView<S>::size_type i, size = nzvals.size();
424 Teuchos::Array<mat_scalar_t> nzvals_tmp(size);
426 if_then_else<is_same<GO,mat_go_t>::value,
427 same_go_helper<M,S,GO,GS,Op>,
428 diff_go_helper<M,S,GO,GS,Op> >::type::do_get(mat, nzvals_tmp, indices,
430 distribution, ordering);
432 for (i = 0; i < size; ++i){
433 nzvals[i] = Teuchos::as<S>(nzvals_tmp[i]);
439 #endif // DOXYGEN_SHOULD_SKIP_THIS 453 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 454 template<
class Matrix,
typename S,
typename GO,
typename GS,
class Op>
455 struct get_cxs_helper
457 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
458 const ArrayView<S> nzvals,
459 const ArrayView<GO> indices,
460 const ArrayView<GS> pointers,
466 typedef typename Matrix::local_ordinal_t lo_t;
467 typedef typename Matrix::global_ordinal_t go_t;
468 typedef typename Matrix::global_size_t gs_t;
469 typedef typename Matrix::node_t node_t;
471 const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
472 = getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
473 Op::get_dimension(mat),
476 Op::getMapFromMatrix(mat)
479 do_get(mat, nzvals, indices, pointers, nnz, Teuchos::ptrInArg(*map), distribution, ordering);
486 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
487 const ArrayView<S> nzvals,
488 const ArrayView<GO> indices,
489 const ArrayView<GS> pointers,
494 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
495 typename Matrix::global_ordinal_t,
496 typename Matrix::node_t> > map
498 do_get(mat, nzvals, indices, pointers, nnz, Teuchos::ptrInArg(*map), distribution, ordering);
505 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
506 const ArrayView<S> nzvals,
507 const ArrayView<GO> indices,
508 const ArrayView<GS> pointers,
511 const Tpetra::Map<
typename Matrix::local_ordinal_t,
512 typename Matrix::global_ordinal_t,
513 typename Matrix::node_t> > map,
517 typedef typename Matrix::scalar_t mat_scalar;
518 if_then_else<is_same<mat_scalar,S>::value,
519 same_scalar_helper<Matrix,S,GO,GS,Op>,
520 diff_scalar_helper<Matrix,S,GO,GS,Op> >::type::do_get(mat,
524 distribution, ordering);
530 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
533 static void do_get(
const Teuchos::Ptr<const M> mat,
537 typename KV_GS::value_type& nnz,
539 const Tpetra::Map<
typename M::local_ordinal_t,
540 typename M::global_ordinal_t,
541 typename M::node_t> > map,
545 Op::template apply_kokkos_view<KV_S, KV_GO, KV_GS>(mat, nzvals,
546 indices, pointers, nnz, map, distribution, ordering);
550 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
551 struct diff_gs_helper_kokkos_view
553 static void do_get(
const Teuchos::Ptr<const M> mat,
557 typename KV_GS::value_type& nnz,
559 const Tpetra::Map<
typename M::local_ordinal_t,
560 typename M::global_ordinal_t,
561 typename M::node_t> > map,
565 typedef typename M::global_size_t mat_gs_t;
566 typedef typename Kokkos::View<mat_gs_t*, Kokkos::HostSpace> KV_TMP;
567 size_t i, size = pointers.extent(0);
568 KV_TMP pointers_tmp(Kokkos::ViewAllocateWithoutInitializing(
"pointers_tmp"), size);
570 mat_gs_t nnz_tmp = 0;
571 Op::template apply_kokkos_view<KV_S, KV_GO, KV_TMP>(mat, nzvals,
572 indices, pointers_tmp, nnz_tmp, Teuchos::ptrInArg(*map), distribution, ordering);
573 nnz = Teuchos::as<typename KV_GS::value_type>(nnz_tmp);
575 typedef typename KV_GS::value_type view_gs_t;
576 for (i = 0; i < size; ++i){
577 pointers(i) = Teuchos::as<view_gs_t>(pointers_tmp(i));
579 nnz = Teuchos::as<view_gs_t>(nnz_tmp);
583 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
584 struct same_go_helper_kokkos_view
586 static void do_get(
const Teuchos::Ptr<const M> mat,
590 typename KV_GS::value_type& nnz,
592 const Tpetra::Map<
typename M::local_ordinal_t,
593 typename M::global_ordinal_t,
594 typename M::node_t> > map,
598 typedef typename M::global_size_t mat_gs_t;
599 typedef typename KV_GS::value_type view_gs_t;
600 if_then_else<is_same<view_gs_t,mat_gs_t>::value,
601 same_gs_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op>,
602 diff_gs_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op> >::type::do_get(mat, nzvals, indices,
604 distribution, ordering);
608 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
609 struct diff_go_helper_kokkos_view
611 static void do_get(
const Teuchos::Ptr<const M> mat,
615 typename KV_GS::value_type& nnz,
617 const Tpetra::Map<
typename M::local_ordinal_t,
618 typename M::global_ordinal_t,
619 typename M::node_t> > map,
623 typedef typename M::global_ordinal_t mat_go_t;
624 typedef typename M::global_size_t mat_gs_t;
625 typedef typename Kokkos::View<mat_go_t*, Kokkos::HostSpace> KV_TMP;
626 size_t i, size = indices.extent(0);
627 KV_TMP indices_tmp(Kokkos::ViewAllocateWithoutInitializing(
"indices_tmp"), size);
629 typedef typename KV_GO::value_type view_go_t;
630 typedef typename KV_GS::value_type view_gs_t;
631 if_then_else<is_same<view_gs_t,mat_gs_t>::value,
632 same_gs_helper_kokkos_view<M, KV_S, KV_TMP, KV_GS, Op>,
633 diff_gs_helper_kokkos_view<M, KV_S, KV_TMP, KV_GS, Op> >::type::do_get(mat, nzvals, indices_tmp,
635 distribution, ordering);
636 for (i = 0; i < size; ++i){
637 indices(i) = Teuchos::as<view_go_t>(indices_tmp(i));
642 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
643 struct same_scalar_helper_kokkos_view
645 static void do_get(
const Teuchos::Ptr<const M> mat,
649 typename KV_GS::value_type& nnz,
651 const Tpetra::Map<
typename M::local_ordinal_t,
652 typename M::global_ordinal_t,
653 typename M::node_t> > map,
657 typedef typename M::global_ordinal_t mat_go_t;
658 typedef typename KV_GO::value_type view_go_t;
659 if_then_else<is_same<view_go_t,mat_go_t>::value,
660 same_go_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op>,
661 diff_go_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op> >::type::do_get(mat, nzvals, indices,
663 distribution, ordering);
667 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
668 struct diff_scalar_helper_kokkos_view
670 static void do_get(
const Teuchos::Ptr<const M> mat,
674 typename KV_GS::value_type& nnz,
676 const Tpetra::Map<
typename M::local_ordinal_t,
677 typename M::global_ordinal_t,
678 typename M::node_t> > map,
682 typedef typename M::global_ordinal_t mat_go_t;
683 typedef typename Kokkos::ArithTraits<typename M::scalar_t>::val_type mat_scalar_t;
684 typedef typename Kokkos::View<mat_scalar_t*, Kokkos::HostSpace> KV_TMP;
685 size_t i, size = nzvals.extent(0);
686 KV_TMP nzvals_tmp(Kokkos::ViewAllocateWithoutInitializing(
"nzvals_tmp"), size);
688 typedef typename KV_S::value_type view_scalar_t;
689 typedef typename KV_GO::value_type view_go_t;
690 if_then_else<is_same<view_go_t,mat_go_t>::value,
691 same_go_helper_kokkos_view<M, KV_TMP, KV_GO, KV_GS, Op>,
692 diff_go_helper_kokkos_view<M, KV_TMP, KV_GO, KV_GS, Op> >::type::do_get(mat, nzvals_tmp, indices,
694 distribution, ordering);
696 for (i = 0; i < size; ++i){
697 nzvals(i) = Teuchos::as<view_scalar_t>(nzvals_tmp(i));
703 template<
class Matrix,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
704 struct get_cxs_helper_kokkos_view
706 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
710 typename KV_GS::value_type& nnz,
713 typename KV_GO::value_type indexBase = 0)
715 typedef typename Matrix::local_ordinal_t lo_t;
716 typedef typename Matrix::global_ordinal_t go_t;
717 typedef typename Matrix::global_size_t gs_t;
718 typedef typename Matrix::node_t node_t;
720 const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
721 = getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
722 Op::get_dimension(mat),
725 Op::getMapFromMatrix(mat)
727 do_get(mat, nzvals, indices, pointers, nnz, Teuchos::ptrInArg(*map), distribution, ordering);
734 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
738 typename KV_GS::value_type& nnz,
742 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
743 typename Matrix::global_ordinal_t,
744 typename Matrix::node_t> > map
746 do_get(mat, nzvals, indices, pointers, nnz, Teuchos::ptrInArg(*map), distribution, ordering);
753 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
757 typename KV_GS::value_type& nnz,
759 const Tpetra::Map<
typename Matrix::local_ordinal_t,
760 typename Matrix::global_ordinal_t,
761 typename Matrix::node_t> > map,
765 typedef typename Matrix::scalar_t mat_scalar;
766 typedef typename KV_S::value_type view_scalar_t;
768 if_then_else<is_same<mat_scalar,view_scalar_t>::value,
769 same_scalar_helper_kokkos_view<Matrix,KV_S,KV_GO,KV_GS,Op>,
770 diff_scalar_helper_kokkos_view<Matrix,KV_S,KV_GO,KV_GS,Op> >::type::do_get(mat,
774 distribution, ordering);
778 #ifndef DOXYGEN_SHOULD_SKIP_THIS 783 template<
class Matrix>
786 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 787 static void apply(
const Teuchos::Ptr<const Matrix> mat,
788 const ArrayView<typename Matrix::scalar_t> nzvals,
789 const ArrayView<typename Matrix::global_ordinal_t> rowind,
790 const ArrayView<typename Matrix::global_size_t> colptr,
791 typename Matrix::global_size_t& nnz,
793 const Tpetra::Map<
typename Matrix::local_ordinal_t,
794 typename Matrix::global_ordinal_t,
795 typename Matrix::node_t> > map,
799 mat->getCcs(nzvals, rowind, colptr, nnz, map, ordering, distribution);
804 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
805 static void apply_kokkos_view(
const Teuchos::Ptr<const Matrix> mat,
809 typename Matrix::global_size_t& nnz,
811 const Tpetra::Map<
typename Matrix::local_ordinal_t,
812 typename Matrix::global_ordinal_t,
813 typename Matrix::node_t> > map,
817 mat->getCcs_kokkos_view(nzvals, rowind, colptr, nnz, map, ordering, distribution);
821 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
822 typename Matrix::global_ordinal_t,
823 typename Matrix::node_t> >
824 getMapFromMatrix(
const Teuchos::Ptr<const Matrix> mat)
826 return mat->getMap();
830 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
831 typename Matrix::global_ordinal_t,
832 typename Matrix::node_t> >
833 getMap(
const Teuchos::Ptr<const Matrix> mat)
835 return mat->getColMap();
839 typename Matrix::global_size_t
840 get_dimension(
const Teuchos::Ptr<const Matrix> mat)
842 return mat->getGlobalNumCols();
846 template<
class Matrix>
849 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 850 static void apply(
const Teuchos::Ptr<const Matrix> mat,
851 const ArrayView<typename Matrix::scalar_t> nzvals,
852 const ArrayView<typename Matrix::global_ordinal_t> colind,
853 const ArrayView<typename Matrix::global_size_t> rowptr,
854 typename Matrix::global_size_t& nnz,
856 const Tpetra::Map<
typename Matrix::local_ordinal_t,
857 typename Matrix::global_ordinal_t,
858 typename Matrix::node_t> > map,
862 mat->getCrs(nzvals, colind, rowptr, nnz, map, ordering, distribution);
866 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
867 static void apply_kokkos_view(
const Teuchos::Ptr<const Matrix> mat,
871 typename Matrix::global_size_t& nnz,
873 const Tpetra::Map<
typename Matrix::local_ordinal_t,
874 typename Matrix::global_ordinal_t,
875 typename Matrix::node_t> > map,
879 mat->getCrs_kokkos_view(nzvals, colind, rowptr, nnz, map, ordering, distribution);
883 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
884 typename Matrix::global_ordinal_t,
885 typename Matrix::node_t> >
886 getMapFromMatrix(
const Teuchos::Ptr<const Matrix> mat)
888 return mat->getMap();
892 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
893 typename Matrix::global_ordinal_t,
894 typename Matrix::node_t> >
895 getMap(
const Teuchos::Ptr<const Matrix> mat)
897 return mat->getRowMap();
901 typename Matrix::global_size_t
902 get_dimension(
const Teuchos::Ptr<const Matrix> mat)
904 return mat->getGlobalNumRows();
907 #endif // DOXYGEN_SHOULD_SKIP_THIS 946 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 947 template<
class Matrix,
typename S,
typename GO,
typename GS>
948 struct get_ccs_helper : get_cxs_helper<Matrix,S,GO,GS,get_ccs_func<Matrix> >
952 template<
class Matrix,
typename KV_S,
typename KV_GO,
typename KV_GS>
963 #ifdef TPETRA_ENABLE_DEPRECATED_CODE 964 template<
class Matrix,
typename S,
typename GO,
typename GS>
965 struct get_crs_helper : get_cxs_helper<Matrix,S,GO,GS,get_crs_func<Matrix> >
969 template<
class Matrix,
typename KV_S,
typename KV_GO,
typename KV_GS>
980 template <
typename LO,
typename GO,
typename GS,
typename Node>
981 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
982 getGatherMap(
const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> > &map )
985 Teuchos::RCP< const Tpetra::Map<LO,GO,Node> > gather_map = Tpetra::Details::computeGatherMap(map, Teuchos::null);
990 template <
typename LO,
typename GO,
typename GS,
typename Node>
991 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
993 GS num_global_elements,
994 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
996 const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> >& map)
1000 switch( distribution ){
1003 return Tpetra::createUniformContigMapWithNode<LO,GO, Node>(num_global_elements, comm);
1005 return Tpetra::createLocalMapWithNode<LO,GO, Node>(num_global_elements, comm);
1008 int rank = Teuchos::rank(*comm);
1009 size_t my_num_elems = Teuchos::OrdinalTraits<size_t>::zero();
1010 if( rank == 0 ) { my_num_elems = num_global_elements; }
1012 return rcp(
new Tpetra::Map<LO,GO, Node>(num_global_elements,
1013 my_num_elems, indexBase, comm));
1017 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> > gathermap
1018 = getGatherMap<LO,GO,GS,Node>( map );
1022 TEUCHOS_TEST_FOR_EXCEPTION(
true,
1024 "Control should never reach this point. " 1025 "Please contact the Amesos2 developers." );
1030 #ifdef HAVE_AMESOS2_EPETRA 1035 template <
typename LO,
typename GO,
typename GS,
typename Node>
1036 Teuchos::RCP<Tpetra::Map<LO,GO,Node> >
1042 int num_my_elements = map.NumMyElements();
1043 Teuchos::Array<int> my_global_elements(num_my_elements);
1044 map.MyGlobalElements(my_global_elements.getRawPtr());
1046 Teuchos::Array<GO> my_gbl_inds_buf;
1047 Teuchos::ArrayView<GO> my_gbl_inds;
1048 if (! std::is_same<int, GO>::value) {
1049 my_gbl_inds_buf.resize (num_my_elements);
1050 my_gbl_inds = my_gbl_inds_buf ();
1051 for (
int k = 0; k < num_my_elements; ++k) {
1052 my_gbl_inds[k] =
static_cast<GO
> (my_global_elements[k]);
1056 using Teuchos::av_reinterpret_cast;
1057 my_gbl_inds = av_reinterpret_cast<GO> (my_global_elements ());
1060 typedef Tpetra::Map<LO,GO,Node> map_t;
1061 RCP<map_t> tmap = rcp(
new map_t(Teuchos::OrdinalTraits<GS>::invalid(),
1063 as<GO>(map.IndexBase()),
1068 template <
typename LO,
typename GO,
typename GS,
typename Node>
1069 Teuchos::RCP<Epetra_Map>
1074 Teuchos::Array<GO> elements_tmp;
1075 elements_tmp = map.getNodeElementList();
1076 int num_my_elements = elements_tmp.size();
1077 Teuchos::Array<int> my_global_elements(num_my_elements);
1078 for (
int i = 0; i < num_my_elements; ++i){
1079 my_global_elements[i] = as<int>(elements_tmp[i]);
1083 RCP<Epetra_Map> emap = rcp(
new Epetra_Map(-1,
1085 my_global_elements.getRawPtr(),
1086 as<GO>(map.getIndexBase()),
1090 #endif // HAVE_AMESOS2_EPETRA 1092 template <
typename Scalar,
1093 typename GlobalOrdinal,
1094 typename GlobalSizeT>
1095 void transpose(Teuchos::ArrayView<Scalar> vals,
1096 Teuchos::ArrayView<GlobalOrdinal> indices,
1097 Teuchos::ArrayView<GlobalSizeT> ptr,
1098 Teuchos::ArrayView<Scalar> trans_vals,
1099 Teuchos::ArrayView<GlobalOrdinal> trans_indices,
1100 Teuchos::ArrayView<GlobalSizeT> trans_ptr)
1108 #ifdef HAVE_AMESOS2_DEBUG 1109 typename Teuchos::ArrayView<GlobalOrdinal>::iterator ind_it, ind_begin, ind_end;
1110 ind_begin = indices.begin();
1111 ind_end = indices.end();
1112 size_t min_trans_ptr_size = *std::max_element(ind_begin, ind_end) + 1;
1113 TEUCHOS_TEST_FOR_EXCEPTION( Teuchos::as<size_t>(trans_ptr.size()) < min_trans_ptr_size,
1114 std::invalid_argument,
1115 "Transpose pointer size not large enough." );
1116 TEUCHOS_TEST_FOR_EXCEPTION( trans_vals.size() < vals.size(),
1117 std::invalid_argument,
1118 "Transpose values array not large enough." );
1119 TEUCHOS_TEST_FOR_EXCEPTION( trans_indices.size() < indices.size(),
1120 std::invalid_argument,
1121 "Transpose indices array not large enough." );
1123 typename Teuchos::ArrayView<GlobalOrdinal>::iterator ind_it, ind_end;
1126 Teuchos::Array<GlobalSizeT> count(trans_ptr.size(), 0);
1127 ind_end = indices.end();
1128 for( ind_it = indices.begin(); ind_it != ind_end; ++ind_it ){
1129 ++(count[(*ind_it) + 1]);
1132 typename Teuchos::Array<GlobalSizeT>::iterator cnt_it, cnt_end;
1133 cnt_end = count.end();
1134 for( cnt_it = count.begin() + 1; cnt_it != cnt_end; ++cnt_it ){
1135 *cnt_it = *cnt_it + *(cnt_it - 1);
1138 trans_ptr.assign(count);
1152 GlobalSizeT size = ptr.size();
1153 for( GlobalSizeT i = 0; i < size - 1; ++i ){
1154 GlobalOrdinal u = ptr[i];
1155 GlobalOrdinal v = ptr[i + 1];
1156 for( GlobalOrdinal j = u; j < v; ++j ){
1157 GlobalOrdinal k = count[indices[j]];
1158 trans_vals[k] = vals[j];
1159 trans_indices[k] = i;
1160 ++(count[indices[j]]);
1166 template <
typename Scalar1,
typename Scalar2>
1168 scale(Teuchos::ArrayView<Scalar1> vals,
size_t l,
1169 size_t ld, Teuchos::ArrayView<Scalar2> s)
1171 size_t vals_size = vals.size();
1172 #ifdef HAVE_AMESOS2_DEBUG 1173 size_t s_size = s.size();
1174 TEUCHOS_TEST_FOR_EXCEPTION( s_size < l,
1175 std::invalid_argument,
1176 "Scale vector must have length at least that of the vector" );
1179 for( i = 0, s_i = 0; i < vals_size; ++i, ++s_i ){
1189 template <
typename Scalar1,
typename Scalar2,
class BinaryOp>
1191 scale(Teuchos::ArrayView<Scalar1> vals,
size_t l,
1192 size_t ld, Teuchos::ArrayView<Scalar2> s,
1195 size_t vals_size = vals.size();
1196 #ifdef HAVE_AMESOS2_DEBUG 1197 size_t s_size = s.size();
1198 TEUCHOS_TEST_FOR_EXCEPTION( s_size < l,
1199 std::invalid_argument,
1200 "Scale vector must have length at least that of the vector" );
1203 for( i = 0, s_i = 0; i < vals_size; ++i, ++s_i ){
1209 vals[i] = binary_op(vals[i], s[s_i]);
1213 template<
class row_ptr_view_t,
class cols_view_t,
class per_view_t>
1215 reorder(row_ptr_view_t & row_ptr, cols_view_t & cols,
1216 per_view_t & perm, per_view_t & peri,
size_t & nnz,
1217 bool permute_matrix)
1219 #ifndef HAVE_AMESOS2_METIS 1220 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
1221 "Cannot reorder for cuSolver because no METIS is available.");
1223 typedef typename cols_view_t::value_type ordinal_type;
1224 typedef typename row_ptr_view_t::value_type size_type;
1227 auto host_row_ptr = Kokkos::create_mirror_view(row_ptr);
1228 auto host_cols = Kokkos::create_mirror_view(cols);
1229 Kokkos::deep_copy(host_row_ptr, row_ptr);
1230 Kokkos::deep_copy(host_cols, cols);
1234 typedef Kokkos::View<idx_t*, Kokkos::HostSpace> host_metis_array;
1235 const ordinal_type size = row_ptr.size() - 1;
1236 size_type max_nnz = host_row_ptr(size);
1237 host_metis_array host_strip_diag_row_ptr(
1238 Kokkos::ViewAllocateWithoutInitializing(
"host_strip_diag_row_ptr"),
1240 host_metis_array host_strip_diag_cols(
1241 Kokkos::ViewAllocateWithoutInitializing(
"host_strip_diag_cols"),
1244 size_type new_nnz = 0;
1245 for(ordinal_type i = 0; i < size; ++i) {
1246 host_strip_diag_row_ptr(i) = new_nnz;
1247 for(size_type j = host_row_ptr(i); j < host_row_ptr(i+1); ++j) {
1248 if (i != host_cols(j)) {
1249 host_strip_diag_cols(new_nnz++) = host_cols(j);
1253 host_strip_diag_row_ptr(size) = new_nnz;
1256 host_metis_array host_perm(
1257 Kokkos::ViewAllocateWithoutInitializing(
"host_perm"), size);
1258 host_metis_array host_peri(
1259 Kokkos::ViewAllocateWithoutInitializing(
"host_peri"), size);
1263 idx_t metis_size = size;
1264 int err = METIS_NodeND(&metis_size, host_strip_diag_row_ptr.data(), host_strip_diag_cols.data(),
1265 NULL, NULL, host_perm.data(), host_peri.data());
1267 TEUCHOS_TEST_FOR_EXCEPTION(err != METIS_OK, std::runtime_error,
1268 "METIS_NodeND failed to sort matrix.");
1272 typedef typename cols_view_t::execution_space exec_space_t;
1273 auto device_perm = Kokkos::create_mirror_view(exec_space_t(), host_perm);
1274 auto device_peri = Kokkos::create_mirror_view(exec_space_t(), host_peri);
1275 deep_copy(device_perm, host_perm);
1276 deep_copy(device_peri, host_peri);
1280 deep_copy_or_assign_view(perm, device_perm);
1281 deep_copy_or_assign_view(peri, device_peri);
1283 if (permute_matrix) {
1285 row_ptr_view_t new_row_ptr(
1286 Kokkos::ViewAllocateWithoutInitializing(
"new_row_ptr"), row_ptr.size());
1287 cols_view_t new_cols(
1288 Kokkos::ViewAllocateWithoutInitializing(
"new_cols"), cols.size() - new_nnz/2);
1291 Kokkos::RangePolicy<exec_space_t> policy_row(0, row_ptr.size());
1292 Kokkos::parallel_scan(policy_row, KOKKOS_LAMBDA(
1293 ordinal_type i, size_type & update,
const bool &
final) {
1295 new_row_ptr(i) = update;
1298 ordinal_type count = 0;
1299 const ordinal_type row = device_perm(i);
1300 for(ordinal_type k = row_ptr(row); k < row_ptr(row + 1); ++k) {
1301 const ordinal_type j = device_peri(cols(k));
1309 Kokkos::RangePolicy<exec_space_t> policy_col(0, size);
1310 Kokkos::parallel_for(policy_col, KOKKOS_LAMBDA(ordinal_type i) {
1311 const ordinal_type kbeg = new_row_ptr(i);
1312 const ordinal_type row = device_perm(i);
1313 const ordinal_type col_beg = row_ptr(row);
1314 const ordinal_type col_end = row_ptr(row + 1);
1315 const ordinal_type nk = col_end - col_beg;
1316 for(ordinal_type k = 0, t = 0; k < nk; ++k) {
1317 const ordinal_type tk = kbeg + t;
1318 const ordinal_type sk = col_beg + k;
1319 const ordinal_type j = device_peri(cols(sk));
1328 row_ptr = new_row_ptr;
1333 #endif // HAVE_AMESOS2_METIS 1336 template<
class values_view_t,
class row_ptr_view_t,
1337 class cols_view_t,
class per_view_t>
1339 reorder_values(values_view_t & values,
const row_ptr_view_t & orig_row_ptr,
1340 const row_ptr_view_t & new_row_ptr,
1341 const cols_view_t & orig_cols,
const per_view_t & perm,
const per_view_t & peri,
1344 typedef typename cols_view_t::value_type ordinal_type;
1345 typedef typename cols_view_t::execution_space exec_space_t;
1347 auto device_perm = Kokkos::create_mirror_view(exec_space_t(), perm);
1348 auto device_peri = Kokkos::create_mirror_view(exec_space_t(), peri);
1349 deep_copy(device_perm, perm);
1350 deep_copy(device_peri, peri);
1352 const ordinal_type size = orig_row_ptr.size() - 1;
1354 auto host_orig_row_ptr = Kokkos::create_mirror_view(orig_row_ptr);
1355 auto new_nnz = host_orig_row_ptr(size);
1357 values_view_t new_values(
1358 Kokkos::ViewAllocateWithoutInitializing(
"new_values"), values.size() - new_nnz/2);
1361 Kokkos::RangePolicy<exec_space_t> policy_col(0, size);
1362 Kokkos::parallel_for(policy_col, KOKKOS_LAMBDA(ordinal_type i) {
1363 const ordinal_type kbeg = new_row_ptr(i);
1364 const ordinal_type row = device_perm(i);
1365 const ordinal_type col_beg = orig_row_ptr(row);
1366 const ordinal_type col_end = orig_row_ptr(row + 1);
1367 const ordinal_type nk = col_end - col_beg;
1368 for(ordinal_type k = 0, t = 0; k < nk; ++k) {
1369 const ordinal_type tk = kbeg + t;
1370 const ordinal_type sk = col_beg + k;
1371 const ordinal_type j = device_peri(orig_cols(sk));
1373 new_values(tk) = values(sk);
1379 values = new_values;
1382 template<
class array_view_t,
class per_view_t>
1384 apply_reorder_permutation(
const array_view_t & array,
1385 array_view_t & permuted_array,
const per_view_t & permutation) {
1386 if(permuted_array.extent(0) != array.extent(0) || permuted_array.extent(1) != array.extent(1)) {
1387 permuted_array = array_view_t(
1388 Kokkos::ViewAllocateWithoutInitializing(
"permuted_array"),
1389 array.extent(0), array.extent(1));
1391 typedef typename array_view_t::execution_space exec_space_t;
1392 Kokkos::RangePolicy<exec_space_t> policy(0, array.extent(0));
1393 Kokkos::parallel_for(policy, KOKKOS_LAMBDA(
size_t i) {
1394 for(
size_t j = 0; j < array.extent(1); ++j) {
1395 permuted_array(i, j) = array(permutation(i), j);
1406 #endif // #ifndef AMESOS2_UTIL_HPP A generic helper class for getting a CCS representation of a Matrix.
Definition: Amesos2_Util.hpp:953
void scale(ArrayView< Scalar1 > vals, size_t l, size_t ld, ArrayView< Scalar2 > s)
Scales a 1-D representation of a multivector.
A generic base class for the CRS and CCS helpers.
Definition: Amesos2_Util.hpp:531
EStorage_Ordering
Definition: Amesos2_TypeDecl.hpp:141
Definition: Amesos2_TypeDecl.hpp:125
void transpose(ArrayView< Scalar > vals, ArrayView< GlobalOrdinal > indices, ArrayView< GlobalSizeT > ptr, ArrayView< Scalar > trans_vals, ArrayView< GlobalOrdinal > trans_indices, ArrayView< GlobalSizeT > trans_ptr)
Copy or assign views based on memory spaces.
const RCP< const Teuchos::Comm< int > > to_teuchos_comm(RCP< const Epetra_Comm > c)
Transform an Epetra_Comm object into a Teuchos::Comm object.
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
void printLine(Teuchos::FancyOStream &out)
Prints a line of 70 "-"s on std::cout.
Definition: Amesos2_Util.cpp:119
Definition: Amesos2_TypeDecl.hpp:126
const RCP< const Epetra_Comm > to_epetra_comm(RCP< const Teuchos::Comm< int > > c)
Transfrom a Teuchos::Comm object into an Epetra_Comm object.
const Teuchos::RCP< const Tpetra::Map< LO, GO, Node > > getGatherMap(const Teuchos::RCP< const Tpetra::Map< LO, GO, Node > > &map)
Gets a Tpetra::Map described by the EDistribution.
Definition: Amesos2_Util.hpp:982
Similar to get_ccs_helper , but used to get a CRS representation of the given matrix.
Definition: Amesos2_Util.hpp:970
Teuchos::RCP< Epetra_Map > tpetra_map_to_epetra_map(const Tpetra::Map< LO, GO, Node > &map)
Transform a Tpetra::Map object into an Epetra_Map.
Definition: Amesos2_Util.hpp:1070
Definition: Amesos2_TypeDecl.hpp:124
Enum and other types declarations for Amesos2.
Teuchos::RCP< Tpetra::Map< LO, GO, Node > > epetra_map_to_tpetra_map(const Epetra_BlockMap &map)
Transform an Epetra_Map object into a Tpetra::Map.
Definition: Amesos2_Util.hpp:1037
Definition: Amesos2_TypeDecl.hpp:127
EDistribution
Definition: Amesos2_TypeDecl.hpp:123
Definition: Amesos2_TypeDecl.hpp:128