Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Details_extractBlockDiagonal.hpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Tpetra: Templated Linear Algebra Services Package
5// Copyright (2008) Sandia Corporation
6//
7// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8// the U.S. Government retains certain rights in this software.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ************************************************************************
40// @HEADER
41
42#ifndef TPETRA_DETAILS_EXTRACTBLOCKDIAGONAL_HPP
43#define TPETRA_DETAILS_EXTRACTBLOCKDIAGONAL_HPP
44
45#include "TpetraCore_config.h"
46#include "Tpetra_CrsMatrix.hpp"
47#include "Teuchos_RCP.hpp"
49
56
57namespace Tpetra {
58namespace Details {
59
60
61template<class SparseMatrixType,
62 class MultiVectorType>
63void extractBlockDiagonal(const SparseMatrixType& A, MultiVectorType & diagonal) {
64 using local_map_type = typename SparseMatrixType::map_type::local_map_type;
65 using SC = typename MultiVectorType::scalar_type;
66 using LO = typename SparseMatrixType::local_ordinal_type;
67 using KCRS = typename SparseMatrixType::local_matrix_device_type;
68 using lno_view_t = typename KCRS::StaticCrsGraphType::row_map_type::const_type;
69 using lno_nnz_view_t = typename KCRS::StaticCrsGraphType::entries_type::const_type;
70 using scalar_view_t = typename KCRS::values_type::const_type;
71 using local_mv_type = typename MultiVectorType::dual_view_type::t_dev;
72 using range_type = Kokkos::RangePolicy<typename SparseMatrixType::node_type::execution_space, LO>;
73 using ATS = Kokkos::ArithTraits<SC>;
74 using impl_ATS = Kokkos::ArithTraits<typename ATS::val_type>;
75
76 // Sanity checking: Map Compatibility (A's rowmap matches diagonal's map)
78 TEUCHOS_TEST_FOR_EXCEPTION(!A.getRowMap()->isSameAs(*diagonal.getMap()),
79 std::runtime_error, "Tpetra::Details::extractBlockDiagonal was given incompatible maps");
80 }
81
82 LO numrows = diagonal.getLocalLength();
83 LO blocksize = diagonal.getNumVectors();
84
85 // Get Kokkos versions of objects
86 local_map_type rowmap = A.getRowMap()->getLocalMap();
87 local_map_type colmap = A.getRowMap()->getLocalMap();
88 local_mv_type diag = diagonal.getLocalViewDevice(Access::OverwriteAll);
89 const KCRS Amat = A.getLocalMatrixDevice();
90 lno_view_t Arowptr = Amat.graph.row_map;
91 lno_nnz_view_t Acolind = Amat.graph.entries;
92 scalar_view_t Avals = Amat.values;
93
94 Kokkos::parallel_for("Tpetra::extractBlockDiagonal",range_type(0,numrows),KOKKOS_LAMBDA(const LO i){
95 LO diag_col = colmap.getLocalElement(rowmap.getGlobalElement(i));
96 LO blockStart = diag_col - (diag_col % blocksize);
97 LO blockStop = blockStart + blocksize;
98 for(LO k=0; k<blocksize; k++)
99 diag(i,k)=impl_ATS::zero();
100
101 for (size_t k = Arowptr(i); k < Arowptr(i+1); k++) {
102 LO col = Acolind(k);
103 if (blockStart <= col && col < blockStop) {
104 diag(i,col-blockStart) = Avals(k);
105 }
106 }
107 });
108}
109
110} // namespace Details
111} // namespace Tpetra
112
113#endif // TPETRA_DETAILS_EXTRACTBLOCKDIAGONAL_HPP
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.
static bool debug()
Whether Tpetra is in debug mode.
Implementation details of Tpetra.
Namespace Tpetra contains the class and methods constituting the Tpetra library.