44 #ifndef TPETRA_DETAILS_GETDIAGCOPYWITHOUTOFFSETS_DEF_HPP
45 #define TPETRA_DETAILS_GETDIAGCOPYWITHOUTOFFSETS_DEF_HPP
56 #include "Tpetra_RowGraph.hpp"
57 #include "Tpetra_CrsGraph.hpp"
58 #include "Tpetra_RowMatrix.hpp"
59 #include "Tpetra_Vector.hpp"
72 template<
class SC,
class LO,
class GO,
class NT>
73 class GetLocalDiagCopyWithoutOffsetsNotFillCompleteFunctor {
75 typedef ::Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
76 typedef ::Tpetra::Vector<SC, LO, GO, NT> vec_type;
82 typedef typename vec_type::dual_view_type::t_host::execution_space host_execution_space;
86 graphIsSorted (
const row_matrix_type& A)
89 using Teuchos::rcp_dynamic_cast;
98 RCP<const row_graph_type> G_row = A.getGraph ();
99 if (! G_row.is_null ()) {
100 RCP<const crs_graph_type> G_crs =
101 rcp_dynamic_cast<
const crs_graph_type> (G_row);
102 if (! G_crs.is_null ()) {
103 sorted = G_crs->isSorted ();
112 GetLocalDiagCopyWithoutOffsetsNotFillCompleteFunctor (LO& lclNumErrs,
114 const row_matrix_type& A) :
116 lclRowMap_ (*A.getRowMap ()),
117 lclColMap_ (*A.getColMap ()),
118 sorted_ (graphIsSorted (A))
120 const LO lclNumRows =
static_cast<LO
> (diag.getLocalLength ());
122 const LO matLclNumRows =
123 static_cast<LO
> (lclRowMap_.getLocalNumElements ());
124 TEUCHOS_TEST_FOR_EXCEPTION
125 (lclNumRows != matLclNumRows, std::invalid_argument,
126 "diag.getLocalLength() = " << lclNumRows <<
" != "
127 "A.getRowMap()->getLocalNumElements() = " << matLclNumRows <<
".");
131 D_lcl_ = diag.getLocalViewHost(Access::OverwriteAll);
132 D_lcl_1d_ = Kokkos::subview (D_lcl_, Kokkos::ALL (), 0);
134 Kokkos::RangePolicy<host_execution_space, LO> range (0, lclNumRows);
136 Kokkos::parallel_reduce (range, *
this, lclNumErrs);
143 void operator () (
const LO& lclRowInd, LO& errCount)
const {
144 using KokkosSparse::findRelOffset;
146 D_lcl_1d_(lclRowInd) = Kokkos::ArithTraits<IST>::zero ();
147 const GO gblInd = lclRowMap_.getGlobalElement (lclRowInd);
148 const LO lclColInd = lclColMap_.getLocalElement (gblInd);
150 if (lclColInd == Tpetra::Details::OrdinalTraits<LO>::invalid ()) {
154 typename row_matrix_type::local_inds_host_view_type lclColInds;
155 typename row_matrix_type::values_host_view_type curVals;
156 A_.getLocalRowView(lclRowInd, lclColInds, curVals);
157 LO numEnt = lclColInds.extent(0);
162 findRelOffset (lclColInds, numEnt, lclColInd, hint, sorted_);
163 if (offset == numEnt) {
167 D_lcl_1d_(lclRowInd) = curVals[offset];
173 const row_matrix_type& A_;
176 typename vec_type::dual_view_type::t_host D_lcl_;
177 decltype (Kokkos::subview (D_lcl_, Kokkos::ALL (), 0)) D_lcl_1d_;
182 template<class SC, class LO, class GO, class NT>
185 const ::Tpetra::
RowMatrix<SC, LO, GO, NT>& A,
188 using ::Tpetra::Details::gathervPrint;
189 using Teuchos::outArg;
190 using Teuchos::REDUCE_MIN;
191 using Teuchos::reduceAll;
192 typedef GetLocalDiagCopyWithoutOffsetsNotFillCompleteFunctor<SC,
193 LO, GO, NT> functor_type;
203 std::ostringstream errStrm;
204 Teuchos::RCP<const Teuchos::Comm<int> > commPtr = A.getComm ();
205 if (commPtr.is_null ()) {
208 const Teuchos::Comm<int>& comm = *commPtr;
211 functor_type functor (lclNumErrs, diag, A);
213 catch (std::exception& e) {
215 errStrm <<
"Process " << A.getComm ()->getRank () <<
": "
216 << e.what () << std::endl;
218 if (lclNumErrs != 0) {
222 reduceAll<int, int> (comm, REDUCE_MIN, lclSuccess, outArg (gblSuccess));
223 if (gblSuccess == -1) {
224 if (comm.getRank () == 0) {
232 std::cerr <<
"getLocalDiagCopyWithoutOffsetsNotFillComplete threw an "
233 "exception on one or more MPI processes in the matrix's comunicator."
239 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
"");
241 else if (gblSuccess == 0) {
242 TEUCHOS_TEST_FOR_EXCEPTION
243 (gblSuccess != 1, std::runtime_error,
244 "getLocalDiagCopyWithoutOffsetsNotFillComplete failed on "
245 "one or more MPI processes in the matrix's communicator.");
249 functor_type functor (lclNumErrs, diag, A);
261 #define TPETRA_DETAILS_GETDIAGCOPYWITHOUTOFFSETS_INSTANT( SCALAR, LO, GO, NODE ) \
263 Details::getLocalDiagCopyWithoutOffsetsNotFillComplete< SCALAR, LO, GO, NODE > \
264 ( ::Tpetra::Vector< SCALAR, LO, GO, NODE >& diag, \
265 const ::Tpetra::RowMatrix< SCALAR, LO, GO, NODE >& A, \
268 #endif // TPETRA_DETAILS_GETDIAGCOPYWITHOUTOFFSETS_DEF_HPP
An abstract interface for graphs accessed by rows.
base_type::impl_scalar_type impl_scalar_type
The type used internally in place of Scalar.
Declaration of a function that prints strings from each process.
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator, in rank order.
LO getLocalDiagCopyWithoutOffsetsNotFillComplete(::Tpetra::Vector< SC, LO, GO, NT > &diag, const ::Tpetra::RowMatrix< SC, LO, GO, NT > &A, const bool debug=false)
Given a locally indexed, global sparse matrix, extract the matrix's diagonal entries into a Tpetra::V...
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
A read-only, row-oriented interface to a sparse matrix.
A distributed dense vector.
base_type::map_type map_type
The type of the Map specialization used by this class.