Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Kokkos_Blas1_MP_Vector.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 KOKKOS_BLAS1_MP_VECTOR_HPP
43 #define KOKKOS_BLAS1_MP_VECTOR_HPP
44 
45 #include "Sacado_ConfigDefs.h"
46 
47 #include "Sacado_MP_Vector.hpp"
50 #include "KokkosBlas.hpp"
51 
52 //----------------------------------------------------------------------------
53 // Specializations of Kokkos Vector/MultiVector math functions
54 //----------------------------------------------------------------------------
55 
56 namespace KokkosBlas {
57 
58 #if defined(HAVE_STOKHOS_ENSEMBLE_REDUCT)
59 
60 template <typename XD, typename ... XP,
61  typename YD, typename ... YP>
62 typename std::enable_if<
63  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value &&
64  Kokkos::is_view_mp_vector< Kokkos::View<YD,YP...> >::value,
65  typename Kokkos::Details::InnerProductSpaceTraits<
66  typename Kokkos::View<XD,XP...>::non_const_value_type >::dot_type
67  >::type
68 dot(const Kokkos::View<XD,XP...>& x,
69  const Kokkos::View<YD,YP...>& y)
70 {
71  typedef Kokkos::View<XD,XP...> XVector;
72  typedef Kokkos::View<YD,YP...> YVector;
73 
74  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
75  typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
76 
77  return dot( x_flat, y_flat );
78 }
79 
80 template <typename RV,
81  typename XD, typename ... XP,
82  typename YD, typename ... YP>
83 typename std::enable_if<
84  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value &&
85  Kokkos::is_view_mp_vector< Kokkos::View<YD,YP...> >::value >::type
86 dot(const RV& r,
87  const Kokkos::View<XD,XP...>& x,
88  const Kokkos::View<YD,YP...>& y)
89 {
90  typedef Kokkos::View<XD,XP...> XVector;
91  typedef Kokkos::View<YD,YP...> YVector;
92 
93  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
94  typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
95 
96  dot( r, x_flat, y_flat );
97 }
98 
99 template <typename XD, typename ... XP>
100 typename std::enable_if<
101  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value >::type
102 fill(const Kokkos::View<XD,XP...>& x,
103  const typename Kokkos::View<XD,XP...>::non_const_value_type& val) {
104  typedef Kokkos::View<XD,XP...> XVector;
105 
106  // Use the existing fill() implementation if we can
107  if (Sacado::is_constant(val)) {
108  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
109  fill( x_flat, val.coeff(0) );
110  }
111  else {
112  Kokkos::deep_copy(x, val);
113  }
114 }
115 
116 template <typename RV,
117  typename XD, typename ... XP>
118 typename std::enable_if<
119  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value >::type
121  const RV& r,
122  const Kokkos::View<XD,XP...>& x)
123 {
124  typedef Kokkos::View<XD,XP...> XVector;
125 
126  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
127 
128  nrm2_squared( r, x_flat );
129 }
130 
131 template <typename RV,
132  typename XD, typename ... XP>
133 typename std::enable_if<
134  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value >::type
135 nrm1(
136  const RV& r,
137  const Kokkos::View<XD,XP...>& x)
138 {
139  typedef Kokkos::View<XD,XP...> XVector;
140 
141  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
142 
143  nrm1( r, x_flat );
144 }
145 
146 template <typename RV,
147  typename XD, typename ... XP>
148 typename std::enable_if<
149  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value >::type
150 nrmInf(
151  const RV& r,
152  const Kokkos::View<XD,XP...>& x)
153 {
154  typedef Kokkos::View<XD,XP...> XVector;
155 
156  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
157 
158  nrmInf( r, x_flat );
159 }
160 
161 template <typename AV,
162  typename XD, typename ... XP,
163  typename BV,
164  typename YD, typename ... YP>
165 typename std::enable_if<
166  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value &&
167  Kokkos::is_view_mp_vector< Kokkos::View<YD,YP...> >::value >::type
168 axpby(const AV& a,
169  const Kokkos::View<XD,XP...>& x,
170  const BV& b,
171  const Kokkos::View<YD,YP...>& y)
172 {
173  typedef Kokkos::View<XD,XP...> XVector;
174  typedef Kokkos::View<YD,YP...> YVector;
175 
176  if (!Sacado::is_constant(a) || !Sacado::is_constant(b)) {
177  Kokkos::Impl::raise_error("axpby not implemented for non-constant a or b");
178  }
179 
180  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
181  typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
182  auto aa = Sacado::Value<AV>::eval(a);
183  auto bb = Sacado::Value<BV>::eval(b);
184  axpby( aa, x_flat, bb, y_flat );
185 }
186 
187 // Currently not handling scal() when AV is a view
188 
189 template <typename RD, typename ... RP,
190  typename XD, typename ... XP>
191 typename std::enable_if<
192  Kokkos::is_view_mp_vector< Kokkos::View<RD,RP...> >::value &&
193  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value >::type
194 scal(const Kokkos::View<RD,RP...>& r,
195  const typename Kokkos::View<XD,XP...>::non_const_value_type& a,
196  const Kokkos::View<XD,XP...>& x)
197 {
198  typedef Kokkos::View<RD,RP...> RVector;
199  typedef Kokkos::View<XD,XP...> XVector;
200 
201  if (!Sacado::is_constant(a)) {
202  Kokkos::Impl::raise_error("scal not implemented for non-constant a");
203  }
204 
205  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
206  typename Kokkos::FlatArrayType<RVector>::type r_flat = r;
207  scal( r_flat, a.coeff(0), x_flat );
208 }
209 
210 // abs -- can't do this one by flattening. Hold out for refactoring of scalar
211 // types in Kokkos
212 
213 // We have a special verision of update for scalar alpha/beta/gamma since it
214 // is used in TrilinosCouplings CG solve (even though Tpetra doesn't).
215 template <typename XD, typename ... XP,
216  typename YD, typename ... YP,
217  typename ZD, typename ... ZP>
218 typename std::enable_if<
219  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value &&
220  Kokkos::is_view_mp_vector< Kokkos::View<YD,YP...> >::value &&
221  Kokkos::is_view_mp_vector< Kokkos::View<ZD,ZP...> >::value >::type
222 update(
223  const typename Kokkos::View<XD,XP...>::array_type::non_const_value_type& alpha,
224  const Kokkos::View<XD,XP...>& x,
225  const typename Kokkos::View<YD,YP...>::array_type::non_const_value_type& beta,
226  const Kokkos::View<YD,YP...>& y,
227  const typename Kokkos::View<ZD,ZP...>::array_type::non_const_value_type& gamma,
228  const Kokkos::View<ZD,ZP...>& z)
229 {
230  typedef Kokkos::View<XD,XP...> XVector;
231  typedef Kokkos::View<YD,YP...> YVector;
232  typedef Kokkos::View<ZD,ZP...> ZVector;
233 
234  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
235  typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
236  typename Kokkos::FlatArrayType<ZVector>::type z_flat = z;
237 
238  update( alpha, x_flat, beta, y_flat, gamma, z_flat);
239 
240 }
241 
242 template <typename XD, typename ... XP,
243  typename YD, typename ... YP,
244  typename ZD, typename ... ZP>
245 typename std::enable_if<
246  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value &&
247  Kokkos::is_view_mp_vector< Kokkos::View<YD,YP...> >::value &&
248  Kokkos::is_view_mp_vector< Kokkos::View<ZD,ZP...> >::value >::type
249 update(
250  const typename Kokkos::View<XD,XP...>::non_const_value_type& alpha,
251  const Kokkos::View<XD,XP...>& x,
252  const typename Kokkos::View<YD,YP...>::non_const_value_type& beta,
253  const Kokkos::View<YD,YP...>& y,
254  const typename Kokkos::View<ZD,ZP...>::non_const_value_type& gamma,
255  const Kokkos::View<ZD,ZP...>& z)
256 {
257  if (!Sacado::is_constant(alpha) || !Sacado::is_constant(beta) ||
258  !Sacado::is_constant(gamma)) {
260  "update not implemented for non-constant alpha, beta, gamma");
261  }
262 
263  update( alpha.coeff(0), x, beta.coeff(0), y, gamma.coeff(0), z );
264 }
265 
266 template <typename RD, typename ... RP,
267  typename XD, typename ... XP>
268 typename std::enable_if<
269  Kokkos::is_view_mp_vector< Kokkos::View<RD,RP...> >::value &&
270  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value >::type
271 reciprocal(
272  const Kokkos::View<RD,RP...>& r,
273  const Kokkos::View<XD,XP...>& x)
274 {
275  typedef Kokkos::View<RD,RP...> RVector;
276  typedef Kokkos::View<XD,XP...> XVector;
277 
278  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
279  typename Kokkos::FlatArrayType<RVector>::type r_flat = r;
280  reciprocal( r_flat, x_flat );
281 }
282 
283 template <typename RD, typename ... RP,
284  typename XD, typename ... XP>
285 typename std::enable_if<
286  Kokkos::is_view_mp_vector< Kokkos::View<RD,RP...> >::value &&
287  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value >::type
288 sum(
289  const Kokkos::View<RD,RP...>& r,
290  const Kokkos::View<XD,XP...>& x)
291 {
292  typedef Kokkos::View<RD,RP...> RVector;
293  typedef Kokkos::View<XD,XP...> XVector;
294 
295  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
296  typename Kokkos::FlatArrayType<RVector>::type r_flat = r;
297  sum( r_flat, x_flat );
298 }
299 
300 template <typename RD, typename ... RP,
301  typename XD, typename ... XP,
302  typename WD, typename ... WP>
303 typename std::enable_if<
304  Kokkos::is_view_mp_vector< Kokkos::View<RD,RP...> >::value &&
305  Kokkos::is_view_mp_vector< Kokkos::View<XD,XP...> >::value &&
306  Kokkos::is_view_mp_vector< Kokkos::View<WD,WP...> >::value >::type
308  const Kokkos::View<RD,RP...>& r,
309  const Kokkos::View<XD,XP...>& x,
310  const Kokkos::View<WD,WP...>& w)
311 {
312  typedef Kokkos::View<RD,RP...> RVector;
313  typedef Kokkos::View<XD,XP...> XVector;
314  typedef Kokkos::View<WD,WP...> WVector;
315 
316  typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
317  typename Kokkos::FlatArrayType<RVector>::type r_flat = r;
318  typename Kokkos::FlatArrayType<WVector>::type w_flat = w;
319  nrm2w_squared( r_flat, x_flat, w_flat );
320 }
321 
322 template <typename CD, typename ... CP,
323  typename AD, typename ... AP,
324  typename BD, typename ... BP>
325 typename std::enable_if<
326  Kokkos::is_view_mp_vector< Kokkos::View<CD,CP...> >::value &&
327  Kokkos::is_view_mp_vector< Kokkos::View<AD,AP...> >::value &&
328  Kokkos::is_view_mp_vector< Kokkos::View<BD,BP...> >::value >::type
329 mult(
330  const typename Kokkos::View<CD,CP...>::const_value_type& c,
331  const Kokkos::View<CD,CP...>& C,
332  const typename Kokkos::View<AD,AP...>::const_value_type& ab,
333  const Kokkos::View<AD,AP...>& A,
334  const Kokkos::View<BD,BP...>& B)
335 {
336  if (!Sacado::is_constant(c) || !Sacado::is_constant(ab)) {
337  Kokkos::Impl::raise_error("mult not implemented for non-constant c, ab");
338  }
339 
340  typedef Kokkos::View<CD,CP...> CVector;
341  typedef Kokkos::View<AD,AP...> AVector;
342  typedef Kokkos::View<BD,BP...> BVector;
343 
344  typename Kokkos::FlatArrayType<CVector>::type C_flat = C;
345  typename Kokkos::FlatArrayType<AVector>::type A_flat = A;
346  typename Kokkos::FlatArrayType<BVector>::type B_flat = B;
347  mult( c.coeff(0), C_flat, ab.coeff(0), A_flat, B_flat );
348 }
349 
350 #endif
351 
352 } // namespace KokkosBlas
353 
354 #endif /* #ifndef KOKKOS_MV_MP_VECTOR_HPP */
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type fill(const Kokkos::View< XD, XP...> &x, const typename Kokkos::View< XD, XP...>::non_const_value_type &val)
KOKKOS_INLINE_FUNCTION void raise_error(const char *msg)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< YD, YP...> >::value, typename Kokkos::Details::InnerProductSpaceTraits< typename Kokkos::View< XD, XP...>::non_const_value_type >::dot_type >::type dot(const Kokkos::View< XD, XP...> &x, const Kokkos::View< YD, YP...> &y)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type nrm1(const RV &r, const Kokkos::View< XD, XP...> &x)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< RD, RP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< WD, WP...> >::value >::type nrm2w_squared(const Kokkos::View< RD, RP...> &r, const Kokkos::View< XD, XP...> &x, const Kokkos::View< WD, WP...> &w)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type nrm2_squared(const RV &r, const Kokkos::View< XD, XP...> &x)
KOKKOS_INLINE_FUNCTION bool is_constant(const T &x)
void deep_copy(const Stokhos::CrsMatrix< ValueType, DstDevice, Layout > &dst, const Stokhos::CrsMatrix< ValueType, SrcDevice, Layout > &src)
expr val()
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< YD, YP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< ZD, ZP...> >::value >::type update(const typename Kokkos::View< XD, XP...>::array_type::non_const_value_type &alpha, const Kokkos::View< XD, XP...> &x, const typename Kokkos::View< YD, YP...>::array_type::non_const_value_type &beta, const Kokkos::View< YD, YP...> &y, const typename Kokkos::View< ZD, ZP...>::array_type::non_const_value_type &gamma, const Kokkos::View< ZD, ZP...> &z)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type nrmInf(const RV &r, const Kokkos::View< XD, XP...> &x)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< YD, YP...> >::value >::type axpby(const AV &a, const Kokkos::View< XD, XP...> &x, const BV &b, const Kokkos::View< YD, YP...> &y)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< RD, RP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type scal(const Kokkos::View< RD, RP...> &r, const typename Kokkos::View< XD, XP...>::non_const_value_type &a, const Kokkos::View< XD, XP...> &x)
std::enable_if< Kokkos::is_view_uq_pce< Kokkos::View< RD, RP...> >::value &&Kokkos::is_view_uq_pce< Kokkos::View< XD, XP...> >::value >::type sum(const Kokkos::View< RD, RP...> &r, const Kokkos::View< XD, XP...> &x)