18#ifndef AMESOS2_UTIL_HPP
19#define AMESOS2_UTIL_HPP
25#include "Amesos2_config.h"
27#include "Teuchos_RCP.hpp"
28#include "Teuchos_BLAS_types.hpp"
29#include "Teuchos_Array.hpp"
30#include "Teuchos_ArrayView.hpp"
31#include "Teuchos_FancyOStream.hpp"
33#include <Tpetra_Map.hpp>
34#include <Tpetra_DistObject_decl.hpp>
35#include <Tpetra_ComputeGatherMap.hpp>
41#ifdef HAVE_AMESOS2_METIS
56 using Teuchos::ArrayView;
74 template <
typename LO,
typename GO,
typename GS,
typename Node>
75 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
76 getGatherMap(
const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> > &map );
79 template <
typename LO,
typename GO,
typename GS,
typename Node>
80 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
82 GS num_global_elements,
83 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
85 const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> >& map = Teuchos::null);
92 template <
typename Scalar,
93 typename GlobalOrdinal,
96 ArrayView<GlobalOrdinal> indices,
97 ArrayView<GlobalSizeT> ptr,
98 ArrayView<Scalar> trans_vals,
99 ArrayView<GlobalOrdinal> trans_indices,
100 ArrayView<GlobalSizeT> trans_ptr);
115 template <
typename Scalar1,
typename Scalar2>
116 void scale(ArrayView<Scalar1> vals,
size_t l,
117 size_t ld, ArrayView<Scalar2> s);
137 template <
typename Scalar1,
typename Scalar2,
class BinaryOp>
138 void scale(ArrayView<Scalar1> vals,
size_t l,
139 size_t ld, ArrayView<Scalar2> s, BinaryOp binary_op);
143 void printLine( Teuchos::FancyOStream &out );
148 template <
class T0,
class T1 >
149 struct getStdCplxType
151 using common_type =
typename std::common_type<T0,T1>::type;
152 using type = common_type;
155 template <
class T0,
class T1 >
156 struct getStdCplxType< T0, T1* >
158 using common_type =
typename std::common_type<T0,T1>::type;
159 using type = common_type;
162#if defined(HAVE_TEUCHOS_COMPLEX) && defined(HAVE_AMESOS2_KOKKOS)
163 template <
class T0 >
164 struct getStdCplxType< T0, Kokkos::complex<T0>* >
166 using type = std::complex<T0>;
169 template <
class T0 ,
class T1 >
170 struct getStdCplxType< T0, Kokkos::complex<T1>* >
172 using common_type =
typename std::common_type<T0,T1>::type;
173 using type = std::complex<common_type>;
196 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
199 static void do_get(
const Teuchos::Ptr<const M> mat,
203 typename KV_GS::value_type& nnz,
205 const Tpetra::Map<
typename M::local_ordinal_t,
206 typename M::global_ordinal_t,
207 typename M::node_t> > map,
211 Op::template apply_kokkos_view<KV_S, KV_GO, KV_GS>(mat, nzvals,
212 indices, pointers, nnz, map, distribution, ordering);
216 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
217 struct diff_gs_helper_kokkos_view
219 static void do_get(
const Teuchos::Ptr<const M> mat,
223 typename KV_GS::value_type& nnz,
225 const Tpetra::Map<
typename M::local_ordinal_t,
226 typename M::global_ordinal_t,
227 typename M::node_t> > map,
231 typedef typename M::global_size_t mat_gs_t;
232 typedef typename Kokkos::View<mat_gs_t*, Kokkos::HostSpace> KV_TMP;
233 size_t i,
size = (pointers.extent(0) > 0 ? pointers.extent(0) : 1);
234 KV_TMP pointers_tmp(Kokkos::ViewAllocateWithoutInitializing(
"pointers_tmp"),
size);
236 mat_gs_t nnz_tmp = 0;
237 Op::template apply_kokkos_view<KV_S, KV_GO, KV_TMP>(mat, nzvals,
238 indices, pointers_tmp, nnz_tmp, Teuchos::ptrInArg(*map), distribution, ordering);
239 nnz = Teuchos::as<typename KV_GS::value_type>(nnz_tmp);
241 typedef typename KV_GS::value_type view_gs_t;
242 if (pointers.extent(0) == 1) {
243 Kokkos::deep_copy(pointers, 0);
245 auto host_pointers = Kokkos::create_mirror_view(pointers);
246 for (i = 0; i < pointers.extent(0); ++i){
247 host_pointers(i) = Teuchos::as<view_gs_t>(pointers_tmp(i));
249 Kokkos::deep_copy(pointers, host_pointers);
251 nnz = Teuchos::as<view_gs_t>(nnz_tmp);
255 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
256 struct same_go_helper_kokkos_view
258 static void do_get(
const Teuchos::Ptr<const M> mat,
262 typename KV_GS::value_type& nnz,
264 const Tpetra::Map<
typename M::local_ordinal_t,
265 typename M::global_ordinal_t,
266 typename M::node_t> > map,
270 typedef typename M::global_size_t mat_gs_t;
271 typedef typename KV_GS::value_type view_gs_t;
272 std::conditional_t<std::is_same_v<view_gs_t,mat_gs_t>,
273 same_gs_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op>,
274 diff_gs_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op> >::do_get(mat, nzvals, indices,
276 distribution, ordering);
280 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
281 struct diff_go_helper_kokkos_view
283 static void do_get(
const Teuchos::Ptr<const M> mat,
287 typename KV_GS::value_type& nnz,
289 const Tpetra::Map<
typename M::local_ordinal_t,
290 typename M::global_ordinal_t,
291 typename M::node_t> > map,
295 typedef typename M::global_ordinal_t mat_go_t;
296 typedef typename M::global_size_t mat_gs_t;
297 typedef typename Kokkos::View<mat_go_t*, Kokkos::HostSpace> KV_TMP;
298 size_t i,
size = indices.extent(0);
299 KV_TMP indices_tmp(Kokkos::ViewAllocateWithoutInitializing(
"indices_tmp"),
size);
301 typedef typename KV_GO::value_type view_go_t;
302 typedef typename KV_GS::value_type view_gs_t;
303 std::conditional_t<std::is_same_v<view_gs_t,mat_gs_t>,
304 same_gs_helper_kokkos_view<M, KV_S, KV_TMP, KV_GS, Op>,
305 diff_gs_helper_kokkos_view<M, KV_S, KV_TMP, KV_GS, Op> >::do_get(mat, nzvals, indices_tmp,
307 distribution, ordering);
308 auto host_indices = Kokkos::create_mirror_view(indices);
309 for (i = 0; i <
size; ++i){
310 host_indices(i) = Teuchos::as<view_go_t>(indices_tmp(i));
312 Kokkos::deep_copy(indices, host_indices);
316 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
317 struct same_scalar_helper_kokkos_view
319 static void do_get(
const Teuchos::Ptr<const M> mat,
323 typename KV_GS::value_type& nnz,
325 const Tpetra::Map<
typename M::local_ordinal_t,
326 typename M::global_ordinal_t,
327 typename M::node_t> > map,
331 typedef typename M::global_ordinal_t mat_go_t;
332 typedef typename KV_GO::value_type view_go_t;
333 std::conditional_t<std::is_same_v<view_go_t, mat_go_t>,
334 same_go_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op>,
335 diff_go_helper_kokkos_view<M, KV_S, KV_GO, KV_GS, Op> >::do_get(mat, nzvals, indices,
337 distribution, ordering);
341 template<
class M,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
342 struct diff_scalar_helper_kokkos_view
344 static void do_get(
const Teuchos::Ptr<const M> mat,
348 typename KV_GS::value_type& nnz,
350 const Tpetra::Map<
typename M::local_ordinal_t,
351 typename M::global_ordinal_t,
352 typename M::node_t> > map,
356 typedef typename M::global_ordinal_t mat_go_t;
357#if KOKKOS_VERSION >= 40799
358 typedef typename KokkosKernels::ArithTraits<typename M::scalar_t>::val_type mat_scalar_t;
360 typedef typename Kokkos::ArithTraits<typename M::scalar_t>::val_type mat_scalar_t;
362 typedef typename Kokkos::View<mat_scalar_t*, Kokkos::HostSpace> KV_TMP;
363 size_t i,
size = nzvals.extent(0);
364 KV_TMP nzvals_tmp(Kokkos::ViewAllocateWithoutInitializing(
"nzvals_tmp"),
size);
366 typedef typename KV_S::value_type view_scalar_t;
367 typedef typename KV_GO::value_type view_go_t;
368 std::conditional_t<std::is_same_v<view_go_t, mat_go_t>,
369 same_go_helper_kokkos_view<M, KV_TMP, KV_GO, KV_GS, Op>,
370 diff_go_helper_kokkos_view<M, KV_TMP, KV_GO, KV_GS, Op> >::do_get(mat, nzvals_tmp, indices,
372 distribution, ordering);
374 auto host_nzvals = Kokkos::create_mirror_view(nzvals);
375 for (i = 0; i <
size; ++i){
376 host_nzvals(i) = Teuchos::as<view_scalar_t>(nzvals_tmp(i));
378 Kokkos::deep_copy(nzvals, host_nzvals);
383 template<
class Matrix,
typename KV_S,
typename KV_GO,
typename KV_GS,
class Op>
384 struct get_cxs_helper_kokkos_view
386 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
390 typename KV_GS::value_type& nnz,
393 typename KV_GO::value_type indexBase = 0)
395 typedef typename Matrix::local_ordinal_t lo_t;
396 typedef typename Matrix::global_ordinal_t go_t;
397 typedef typename Matrix::global_size_t gs_t;
398 typedef typename Matrix::node_t node_t;
400 const Teuchos::RCP<const Tpetra::Map<lo_t,go_t,node_t> > map
401 = getDistributionMap<lo_t,go_t,gs_t,node_t>(distribution,
402 Op::get_dimension(mat),
405 Op::getMapFromMatrix(mat)
407 do_get(mat, nzvals, indices, pointers, nnz, Teuchos::ptrInArg(*map), distribution, ordering);
414 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
418 typename KV_GS::value_type& nnz,
422 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
423 typename Matrix::global_ordinal_t,
424 typename Matrix::node_t> > map
426 do_get(mat, nzvals, indices, pointers, nnz, Teuchos::ptrInArg(*map), distribution, ordering);
433 static void do_get(
const Teuchos::Ptr<const Matrix> mat,
437 typename KV_GS::value_type& nnz,
439 const Tpetra::Map<
typename Matrix::local_ordinal_t,
440 typename Matrix::global_ordinal_t,
441 typename Matrix::node_t> > map,
445 typedef typename Matrix::scalar_t mat_scalar;
446 typedef typename KV_S::value_type view_scalar_t;
448 std::conditional_t<std::is_same_v<mat_scalar,view_scalar_t>,
449 same_scalar_helper_kokkos_view<Matrix,KV_S,KV_GO,KV_GS,Op>,
450 diff_scalar_helper_kokkos_view<Matrix,KV_S,KV_GO,KV_GS,Op> >::do_get(mat,
454 distribution, ordering);
458#ifndef DOXYGEN_SHOULD_SKIP_THIS
463 template<
class Matrix>
466 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
467 static void apply_kokkos_view(
const Teuchos::Ptr<const Matrix> mat,
471 typename Matrix::global_size_t& nnz,
473 const Tpetra::Map<
typename Matrix::local_ordinal_t,
474 typename Matrix::global_ordinal_t,
475 typename Matrix::node_t> > map,
476 EDistribution distribution,
477 EStorage_Ordering ordering)
479 mat->getCcs_kokkos_view(nzvals, rowind, colptr, nnz, map, ordering, distribution);
483 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
484 typename Matrix::global_ordinal_t,
485 typename Matrix::node_t> >
486 getMapFromMatrix(
const Teuchos::Ptr<const Matrix> mat)
488 return mat->getMap();
492 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
493 typename Matrix::global_ordinal_t,
494 typename Matrix::node_t> >
495 getMap(
const Teuchos::Ptr<const Matrix> mat)
497 return mat->getColMap();
501 typename Matrix::global_size_t
502 get_dimension(
const Teuchos::Ptr<const Matrix> mat)
504 return mat->getGlobalNumCols();
508 template<
class Matrix>
511 template<
typename KV_S,
typename KV_GO,
typename KV_GS>
512 static void apply_kokkos_view(
const Teuchos::Ptr<const Matrix> mat,
516 typename Matrix::global_size_t& nnz,
518 const Tpetra::Map<
typename Matrix::local_ordinal_t,
519 typename Matrix::global_ordinal_t,
520 typename Matrix::node_t> > map,
521 EDistribution distribution,
522 EStorage_Ordering ordering)
524 mat->getCrs_kokkos_view(nzvals, colind, rowptr, nnz, map, ordering, distribution);
528 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
529 typename Matrix::global_ordinal_t,
530 typename Matrix::node_t> >
531 getMapFromMatrix(
const Teuchos::Ptr<const Matrix> mat)
533 return mat->getMap();
537 const Teuchos::RCP<
const Tpetra::Map<
typename Matrix::local_ordinal_t,
538 typename Matrix::global_ordinal_t,
539 typename Matrix::node_t> >
540 getMap(
const Teuchos::Ptr<const Matrix> mat)
542 return mat->getRowMap();
546 typename Matrix::global_size_t
547 get_dimension(
const Teuchos::Ptr<const Matrix> mat)
549 return mat->getGlobalNumRows();
591 template<
class Matrix,
typename KV_S,
typename KV_GO,
typename KV_GS>
602 template<
class Matrix,
typename KV_S,
typename KV_GO,
typename KV_GS>
613 template <
typename LO,
typename GO,
typename GS,
typename Node>
614 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
615 getGatherMap(
const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> > &map )
618 Teuchos::RCP< const Tpetra::Map<LO,GO,Node> > gather_map = Tpetra::Details::computeGatherMap(map, Teuchos::null);
623 template <
typename LO,
typename GO,
typename GS,
typename Node>
624 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> >
626 GS num_global_elements,
627 const Teuchos::RCP<
const Teuchos::Comm<int> >& comm,
629 const Teuchos::RCP<
const Tpetra::Map<LO,GO,Node> >& map)
633 switch( distribution ){
636 return Tpetra::createUniformContigMapWithNode<LO,GO, Node>(num_global_elements, comm);
638 return Tpetra::createLocalMapWithNode<LO,GO, Node>(num_global_elements, comm);
641 int rank = Teuchos::rank(*comm);
642 size_t my_num_elems = Teuchos::OrdinalTraits<size_t>::zero();
643 if( rank == 0 ) { my_num_elems = num_global_elements; }
645 return rcp(
new Tpetra::Map<LO,GO, Node>(num_global_elements,
646 my_num_elems, indexBase, comm));
650 const Teuchos::RCP<const Tpetra::Map<LO,GO,Node> > gathermap
655 TEUCHOS_TEST_FOR_EXCEPTION(
true,
657 "Control should never reach this point. "
658 "Please contact the Amesos2 developers." );
662 template <
typename Scalar,
663 typename GlobalOrdinal,
664 typename GlobalSizeT>
665 void transpose(Teuchos::ArrayView<Scalar> vals,
666 Teuchos::ArrayView<GlobalOrdinal> indices,
667 Teuchos::ArrayView<GlobalSizeT> ptr,
668 Teuchos::ArrayView<Scalar> trans_vals,
669 Teuchos::ArrayView<GlobalOrdinal> trans_indices,
670 Teuchos::ArrayView<GlobalSizeT> trans_ptr)
678#ifdef HAVE_AMESOS2_DEBUG
679 typename Teuchos::ArrayView<GlobalOrdinal>::iterator ind_it, ind_begin, ind_end;
680 ind_begin = indices.begin();
681 ind_end = indices.end();
682 size_t min_trans_ptr_size = *std::max_element(ind_begin, ind_end) + 1;
683 TEUCHOS_TEST_FOR_EXCEPTION( Teuchos::as<size_t>(trans_ptr.size()) < min_trans_ptr_size,
684 std::invalid_argument,
685 "Transpose pointer size not large enough." );
686 TEUCHOS_TEST_FOR_EXCEPTION( trans_vals.size() < vals.size(),
687 std::invalid_argument,
688 "Transpose values array not large enough." );
689 TEUCHOS_TEST_FOR_EXCEPTION( trans_indices.size() < indices.size(),
690 std::invalid_argument,
691 "Transpose indices array not large enough." );
693 typename Teuchos::ArrayView<GlobalOrdinal>::iterator ind_it, ind_end;
696 Teuchos::Array<GlobalSizeT> count(trans_ptr.size(), 0);
697 ind_end = indices.end();
698 for( ind_it = indices.begin(); ind_it != ind_end; ++ind_it ){
699 ++(count[(*ind_it) + 1]);
702 typename Teuchos::Array<GlobalSizeT>::iterator cnt_it, cnt_end;
703 cnt_end = count.end();
704 for( cnt_it = count.begin() + 1; cnt_it != cnt_end; ++cnt_it ){
705 *cnt_it = *cnt_it + *(cnt_it - 1);
708 trans_ptr.assign(count);
722 GlobalSizeT
size = ptr.size();
723 for( GlobalSizeT i = 0; i <
size - 1; ++i ){
724 GlobalOrdinal u = ptr[i];
725 GlobalOrdinal v = ptr[i + 1];
726 for( GlobalOrdinal j = u; j < v; ++j ){
727 GlobalOrdinal k = count[indices[j]];
728 trans_vals[k] = vals[j];
729 trans_indices[k] = i;
730 ++(count[indices[j]]);
736 template <
typename Scalar1,
typename Scalar2>
738 scale(Teuchos::ArrayView<Scalar1> vals,
size_t l,
739 size_t ld, Teuchos::ArrayView<Scalar2> s)
741 size_t vals_size = vals.size();
742#ifdef HAVE_AMESOS2_DEBUG
743 size_t s_size = s.size();
744 TEUCHOS_TEST_FOR_EXCEPTION( s_size < l,
745 std::invalid_argument,
746 "Scale vector must have length at least that of the vector" );
749 for( i = 0, s_i = 0; i < vals_size; ++i, ++s_i ){
759 template <
typename Scalar1,
typename Scalar2,
class BinaryOp>
761 scale(Teuchos::ArrayView<Scalar1> vals,
size_t l,
762 size_t ld, Teuchos::ArrayView<Scalar2> s,
765 size_t vals_size = vals.size();
766#ifdef HAVE_AMESOS2_DEBUG
767 size_t s_size = s.size();
768 TEUCHOS_TEST_FOR_EXCEPTION( s_size < l,
769 std::invalid_argument,
770 "Scale vector must have length at least that of the vector" );
773 for( i = 0, s_i = 0; i < vals_size; ++i, ++s_i ){
779 vals[i] = binary_op(vals[i], s[s_i]);
783 template<
class row_ptr_view_t,
class cols_view_t,
class per_view_t>
785 reorder(row_ptr_view_t & row_ptr, cols_view_t & cols,
786 per_view_t & perm, per_view_t & peri,
size_t & nnz,
789 #ifndef HAVE_AMESOS2_METIS
790 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
791 "Cannot reorder for cuSolver because no METIS is available.");
793 typedef typename cols_view_t::value_type ordinal_type;
794 typedef typename row_ptr_view_t::value_type size_type;
797 auto host_row_ptr = Kokkos::create_mirror_view(row_ptr);
798 auto host_cols = Kokkos::create_mirror_view(cols);
799 Kokkos::deep_copy(host_row_ptr, row_ptr);
800 Kokkos::deep_copy(host_cols, cols);
804 typedef Kokkos::View<idx_t*, Kokkos::HostSpace> host_metis_array;
805 const ordinal_type
size = row_ptr.size() - 1;
806 size_type max_nnz = host_row_ptr(
size);
807 host_metis_array host_strip_diag_row_ptr(
808 Kokkos::ViewAllocateWithoutInitializing(
"host_strip_diag_row_ptr"),
810 host_metis_array host_strip_diag_cols(
811 Kokkos::ViewAllocateWithoutInitializing(
"host_strip_diag_cols"),
814 size_type new_nnz = 0;
815 for(ordinal_type i = 0; i <
size; ++i) {
816 host_strip_diag_row_ptr(i) = new_nnz;
817 for(size_type j = host_row_ptr(i); j < host_row_ptr(i+1); ++j) {
818 if (i != host_cols(j)) {
819 host_strip_diag_cols(new_nnz++) = host_cols(j);
823 host_strip_diag_row_ptr(
size) = new_nnz;
826 host_metis_array host_perm(
827 Kokkos::ViewAllocateWithoutInitializing(
"host_perm"),
size);
828 host_metis_array host_peri(
829 Kokkos::ViewAllocateWithoutInitializing(
"host_peri"),
size);
833 idx_t metis_size =
size;
834 int err = METIS_NodeND(&metis_size, host_strip_diag_row_ptr.data(), host_strip_diag_cols.data(),
835 NULL, NULL, host_perm.data(), host_peri.data());
837 TEUCHOS_TEST_FOR_EXCEPTION(err != METIS_OK, std::runtime_error,
838 "METIS_NodeND failed to sort matrix.");
842 typedef typename cols_view_t::execution_space exec_space_t;
843 auto device_perm = Kokkos::create_mirror_view(exec_space_t(), host_perm);
844 auto device_peri = Kokkos::create_mirror_view(exec_space_t(), host_peri);
845 deep_copy(device_perm, host_perm);
846 deep_copy(device_peri, host_peri);
850 deep_copy_or_assign_view(perm, device_perm);
851 deep_copy_or_assign_view(peri, device_peri);
853 if (permute_matrix) {
855 row_ptr_view_t new_row_ptr(
856 Kokkos::ViewAllocateWithoutInitializing(
"new_row_ptr"), row_ptr.size());
857 cols_view_t new_cols(
858 Kokkos::ViewAllocateWithoutInitializing(
"new_cols"), cols.size() - new_nnz/2);
861 Kokkos::RangePolicy<exec_space_t> policy_row(0, row_ptr.size());
862 Kokkos::parallel_scan(policy_row, KOKKOS_LAMBDA(
863 ordinal_type i, size_type & update,
const bool &
final) {
865 new_row_ptr(i) = update;
868 ordinal_type count = 0;
869 const ordinal_type row = device_perm(i);
870 for(ordinal_type k = row_ptr(row); k < row_ptr(row + 1); ++k) {
871 const ordinal_type j = device_peri(cols(k));
879 Kokkos::RangePolicy<exec_space_t> policy_col(0,
size);
880 Kokkos::parallel_for(policy_col, KOKKOS_LAMBDA(ordinal_type i) {
881 const ordinal_type kbeg = new_row_ptr(i);
882 const ordinal_type row = device_perm(i);
883 const ordinal_type col_beg = row_ptr(row);
884 const ordinal_type col_end = row_ptr(row + 1);
885 const ordinal_type nk = col_end - col_beg;
886 for(ordinal_type k = 0, t = 0; k < nk; ++k) {
887 const ordinal_type tk = kbeg + t;
888 const ordinal_type sk = col_beg + k;
889 const ordinal_type j = device_peri(cols(sk));
898 row_ptr = new_row_ptr;
906 template<
class values_view_t,
class row_ptr_view_t,
907 class cols_view_t,
class per_view_t>
909 reorder_values(values_view_t & values,
const row_ptr_view_t & orig_row_ptr,
910 const row_ptr_view_t & new_row_ptr,
911 const cols_view_t & orig_cols,
const per_view_t & perm,
const per_view_t & peri,
914 typedef typename cols_view_t::value_type ordinal_type;
915 typedef typename cols_view_t::execution_space exec_space_t;
917 auto device_perm = Kokkos::create_mirror_view(exec_space_t(), perm);
918 auto device_peri = Kokkos::create_mirror_view(exec_space_t(), peri);
919 deep_copy(device_perm, perm);
920 deep_copy(device_peri, peri);
922 const ordinal_type
size = orig_row_ptr.size() - 1;
924 auto host_orig_row_ptr = Kokkos::create_mirror_view(orig_row_ptr);
925 auto new_nnz = host_orig_row_ptr(
size);
927 values_view_t new_values(
928 Kokkos::ViewAllocateWithoutInitializing(
"new_values"), values.size() - new_nnz/2);
931 Kokkos::RangePolicy<exec_space_t> policy_col(0,
size);
932 Kokkos::parallel_for(policy_col, KOKKOS_LAMBDA(ordinal_type i) {
933 const ordinal_type kbeg = new_row_ptr(i);
934 const ordinal_type row = device_perm(i);
935 const ordinal_type col_beg = orig_row_ptr(row);
936 const ordinal_type col_end = orig_row_ptr(row + 1);
937 const ordinal_type nk = col_end - col_beg;
938 for(ordinal_type k = 0, t = 0; k < nk; ++k) {
939 const ordinal_type tk = kbeg + t;
940 const ordinal_type sk = col_beg + k;
941 const ordinal_type j = device_peri(orig_cols(sk));
943 new_values(tk) = values(sk);
952 template<
class array_view_t,
class per_view_t>
954 apply_reorder_permutation(
const array_view_t & array,
955 array_view_t & permuted_array,
const per_view_t & permutation) {
956 if(permuted_array.extent(0) != array.extent(0) || permuted_array.extent(1) != array.extent(1)) {
957 permuted_array = array_view_t(
958 Kokkos::ViewAllocateWithoutInitializing(
"permuted_array"),
959 array.extent(0), array.extent(1));
961 typedef typename array_view_t::execution_space exec_space_t;
962 Kokkos::RangePolicy<exec_space_t> policy(0, array.extent(0));
963 Kokkos::parallel_for(policy, KOKKOS_LAMBDA(
size_t i) {
964 for(
size_t j = 0; j < array.extent(1); ++j) {
965 permuted_array(i, j) = array(permutation(i), j);
972 template <
typename GO,
typename Scalar>
974 readEntryFromFile (GO& gblRowInd, GO& gblColInd, Scalar& val,
const std::string& s)
976 if (s.size () == 0 || s.find (
"%") != std::string::npos) {
979 std::istringstream in (s);
996 template<
class map_type,
class MAT>
998 readCrsMatrixFromFile (
const std::string& matrixFilename,
999 Teuchos::RCP<Teuchos::FancyOStream> & fos,
1000 const Teuchos::RCP<const map_type>& rowMap,
1001 const Teuchos::RCP<const map_type>& domainMap,
1002 const Teuchos::RCP<const map_type>& rangeMap,
1003 const bool convert_to_zero_base,
1004 const int header_size)
1006 using Scalar =
typename MAT::scalar_type;
1007 using GO =
typename MAT::global_ordinal_type;
1009 using counter_type = std::map<GO, size_t>;
1010 using pair_type = std::pair<const GO, size_t>;
1013 auto comm = rowMap->getComm ();
1014 const int myRank = comm->getRank ();
1016 std::ifstream inFile;
1021 inFile.open (matrixFilename);
1030 Teuchos::broadcast<int, int> (*comm, 0, Teuchos::outArg (opened));
1031 TEUCHOS_TEST_FOR_EXCEPTION
1032 (opened == 0, std::runtime_error,
"readCrsMatrixFromFile: "
1033 "Failed to open file \"" << matrixFilename <<
"\" on Process 0.");
1042 for (
int i = 0; i < header_size; ++i ) {
1043 std::getline (inFile, line);
1046 counter_type counts;
1047 Teuchos::Array<Scalar> vals;
1048 Teuchos::Array<GO> gblRowInds;
1049 Teuchos::Array<GO> gblColInds;
1051 std::getline (inFile, line);
1055 const bool gotLine = readEntryFromFile (gblRowInd, gblColInd, val, line);
1058 if ( convert_to_zero_base ) {
1062 counts[gblRowInd]++;
1063 vals.push_back(val);
1064 gblRowInds.push_back(gblRowInd);
1065 gblColInds.push_back(gblColInd);
1070 auto pr = std::max_element(
1073 [] (pair_type
const& p1, pair_type
const& p2){
return p1.second < p2.second; }
1075 size_t maxCount = (counts.empty()) ? size_t(0) : pr->second;
1076 A = Teuchos::rcp(
new MAT(rowMap, maxCount));
1077 for (
typename Teuchos::Array<GO>::size_type i=0; i<gblRowInds.size(); i++) {
1078 A->insertGlobalValues (gblRowInds[i], gblColInds(i,1), vals(i,1));
1081 A = Teuchos::rcp(
new MAT(rowMap, 0));
1084 A->fillComplete (domainMap, rangeMap);
Copy or assign views based on memory spaces.
Enum and other types declarations for Amesos2.
@ DISTRIBUTED
Definition Amesos2_TypeDecl.hpp:90
@ GLOBALLY_REPLICATED
Definition Amesos2_TypeDecl.hpp:92
@ DISTRIBUTED_NO_OVERLAP
Definition Amesos2_TypeDecl.hpp:91
@ ROOTED
Definition Amesos2_TypeDecl.hpp:93
@ CONTIGUOUS_AND_ROOTED
Definition Amesos2_TypeDecl.hpp:94
EDistribution
Definition Amesos2_TypeDecl.hpp:89
EStorage_Ordering
Definition Amesos2_TypeDecl.hpp:107
void scale(ArrayView< Scalar1 > vals, size_t l, size_t ld, ArrayView< Scalar2 > s)
Scales a 1-D representation of a multivector.
void transpose(ArrayView< Scalar > vals, ArrayView< GlobalOrdinal > indices, ArrayView< GlobalSizeT > ptr, ArrayView< Scalar > trans_vals, ArrayView< GlobalOrdinal > trans_indices, ArrayView< GlobalSizeT > trans_ptr)
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:615
const int size
Definition klu2_simple.cpp:50
A generic helper class for getting a CCS representation of a Matrix.
Definition Amesos2_Util.hpp:593
Similar to get_ccs_helper , but used to get a CRS representation of the given matrix.
Definition Amesos2_Util.hpp:604
Scales a 1-D representation of a multivector.
Definition Amesos2_Util.hpp:198