Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
Stokhos_Tpetra_Utilities.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
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 Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef STOKHOS_TPETRA_UTILITIES_HPP
43 #define STOKHOS_TPETRA_UTILITIES_HPP
44 
47 #include "Tpetra_CrsMatrix.hpp"
48 
49 namespace Stokhos {
50 
52 
55  template <class ViewType>
57  public:
58  typedef ViewType MeanViewType;
60  typedef typename ViewType::size_type size_type;
61 
62  GetMeanValsFunc(const ViewType& vals) {
63  mean_vals = ViewType("mean-values", vals.dimension_0());
65  }
66 
67  MeanViewType getMeanValues() const { return mean_vals; }
68 
69  private:
71  };
72 
74 
76  template <class Storage, class ... P>
77  class GetMeanValsFunc< Kokkos::View< Sacado::UQ::PCE<Storage>*,
78  P... > > {
79  public:
81  typedef Kokkos::View< Scalar*, P... > ViewType;
84  typedef typename ViewType::size_type size_type;
85 
86  GetMeanValsFunc(const ViewType& vals_) : vals(vals_) {
87  const size_type nnz = vals.dimension_0();
88  typename Scalar::cijk_type mean_cijk =
89  Stokhos::create_mean_based_product_tensor<execution_space, typename Storage::ordinal_type, typename Storage::value_type>();
90  mean_vals = Kokkos::make_view<ViewType>("mean-values", mean_cijk, nnz, 1);
91  Kokkos::parallel_for( nnz, *this );
92  }
93 
94  KOKKOS_INLINE_FUNCTION
95  void operator() (const size_type i) const {
96  mean_vals(i) = vals(i).fastAccessCoeff(0);
97  }
98 
99  MeanViewType getMeanValues() const { return mean_vals; }
100 
101  private:
104  };
105 
107 
109  template <class Storage, class ... P>
110  class GetMeanValsFunc< Kokkos::View< Sacado::MP::Vector<Storage>*,
111  P... > > {
112  public:
114  typedef Kokkos::View< Scalar*, P... > ViewType;
117  typedef typename ViewType::size_type size_type;
118 
119  GetMeanValsFunc(const ViewType& vals_) :
120  vals(vals_), vec_size(Kokkos::dimension_scalar(vals))
121  {
122  const size_type nnz = vals.dimension_0();
123  mean_vals = ViewType("mean-values", nnz, 1);
124  Kokkos::parallel_for( nnz, *this );
125  }
126 
127  KOKKOS_INLINE_FUNCTION
128  void operator() (const size_type i) const
129  {
130  typename Scalar::value_type s = 0.0;
131  for (size_type j=0; j<vec_size; ++j)
132  s += vals(i).fastAccessCoeff(j);
133  mean_vals(i) = s;
134  }
135 
137 
138  private:
142  };
143 
144  template <typename Scalar, typename LO, typename GO, typename N>
145  Teuchos::RCP< Tpetra::CrsMatrix<Scalar,LO,GO,N> >
146  build_mean_matrix(const Tpetra::CrsMatrix<Scalar,LO,GO,N>& A)
147  {
148  using Teuchos::RCP;
149  using Teuchos::rcp;
150  typedef Tpetra::CrsMatrix<Scalar,LO,GO,N> MatrixType;
151  typedef Tpetra::Map<LO,GO,N> Map;
152 
153  typedef typename MatrixType::local_matrix_type KokkosMatrixType;
154 
155  typedef typename KokkosMatrixType::StaticCrsGraphType KokkosGraphType;
156  typedef typename KokkosMatrixType::values_type KokkosMatrixValuesType;
157 
158  RCP< const Map > rmap = A.getRowMap();
159  RCP< const Map > cmap = A.getColMap();
160 
161  KokkosMatrixType kokkos_matrix = A.getLocalMatrix();
162  KokkosGraphType kokkos_graph = kokkos_matrix.graph;
163  KokkosMatrixValuesType matrix_values = kokkos_matrix.values;
164  const size_t ncols = kokkos_matrix.numCols();
166  typedef typename MeanFunc::MeanViewType KokkosMeanMatrixValuesType;
167  MeanFunc meanfunc(matrix_values);
168  KokkosMeanMatrixValuesType mean_matrix_values = meanfunc.getMeanValues();
169 
170  // From here on we are assuming that
171  // KokkosMeanMatrixValuesType == KokkosMatrixValuestype
172 
173  KokkosMatrixType mean_kokkos_matrix(
174  "mean-matrix", ncols, mean_matrix_values, kokkos_graph);
175  RCP < MatrixType > mean_matrix =
176  rcp( new MatrixType(rmap, cmap, mean_kokkos_matrix) );
177  return mean_matrix;
178  }
179 
180 }
181 
182 #endif // STOKHOS_TPETRA_UTILITIES_HPP
Teuchos::RCP< Tpetra::CrsMatrix< Scalar, LO, GO, N > > build_mean_matrix(const Tpetra::CrsMatrix< Scalar, LO, GO, N > &A)
Stokhos::StandardStorage< int, double > Storage
Kokkos::DefaultExecutionSpace execution_space
GetMeanValsFunc(const ViewType &vals)
Top-level namespace for Stokhos classes and functions.
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< View< T, P... > >::value, unsigned >::type dimension_scalar(const View< T, P... > &view)
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
expr expr expr expr j
Get mean values matrix for mean-based preconditioning.
ViewType::execution_space execution_space