Tpetra parallel linear algebra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
57 namespace Tpetra {
58 namespace Details {
59 
60 
61 template<class SparseMatrixType,
62  class MultiVectorType>
63 void 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_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 
74  // Sanity checking: Map Compatibility (A's rowmap matches diagonal's map)
75  if (Tpetra::Details::Behavior::debug() == true) {
76  TEUCHOS_TEST_FOR_EXCEPTION(!A.getRowMap()->isSameAs(*diagonal.getMap()),
77  std::runtime_error, "Tpetra::Details::extractBlockDiagonal was given incompatible maps");
78  }
79 
80  LO numrows = diagonal.getLocalLength();
81  LO blocksize = diagonal.getNumVectors();
82  SC ZERO = Teuchos::ScalarTraits<typename MultiVectorType::scalar_type>::zero();
83 
84  // Get Kokkos versions of objects
85  local_map_type rowmap = A.getRowMap()->getLocalMap();
86  local_map_type colmap = A.getRowMap()->getLocalMap();
87  local_mv_type diag = diagonal.getLocalViewDevice();
88  const KCRS Amat = A.getLocalMatrix();
89  lno_view_t Arowptr = Amat.graph.row_map;
90  lno_nnz_view_t Acolind = Amat.graph.entries;
91  scalar_view_t Avals = Amat.values;
92 
93  Kokkos::parallel_for("Tpetra::extractBlockDiagonal",range_type(0,numrows),KOKKOS_LAMBDA(const LO i){
94  LO diag_col = colmap.getLocalElement(rowmap.getGlobalElement(i));
95  LO blockStart = diag_col - (diag_col % blocksize);
96  LO blockStop = blockStart + blocksize;
97  for(LO k=0; k<blocksize; k++)
98  diag(i,k)=ZERO;
99 
100  for (size_t k = Arowptr(i); k < Arowptr(i+1); k++) {
101  LO col = Acolind(k);
102  if (blockStart <= col && col < blockStop) {
103  diag(i,col-blockStart) = Avals(k);
104  }
105  }
106  });
107 }
108 
109 } // namespace Details
110 } // namespace Tpetra
111 
112 #endif // TPETRA_DETAILS_EXTRACTBLOCKDIAGONAL_HPP
static bool debug()
Whether Tpetra is in debug mode.
Replace old values with zero.
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra&#39;s behavior.