Debian Codegraph @

packages/stokhos/src/sacado/kokkos/pce/linalg/Kokkos_Blas1_UQ_PCE.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef KOKKOS_BLAS1_UQ_PCE_HPP
11 #define KOKKOS_BLAS1_UQ_PCE_HPP
12
13 #include "Sacado_UQ_PCE.hpp"
14 #include "Kokkos_View_UQ_PCE.hpp"
15 #include "Kokkos_InnerProductSpaceTraits_UQ_PCE.hpp"
16 #include "KokkosBlas.hpp"
17
18 //----------------------------------------------------------------------------
19 // Specializations of Kokkos Vector/MultiVector math functions
20 //----------------------------------------------------------------------------
21
22 namespace KokkosBlas {
23
24 template <typename XD, typename ... XP,
25 typename YD, typename ... YP>
26 typename std::enable_if<
27 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value &&
28 Kokkos::is_view_uq_pce< Kokkos::View<YD,YP...> >::value,
29 typename Kokkos::Details::InnerProductSpaceTraits<
30 typename Kokkos::View<XD,XP...>::non_const_value_type >::dot_type
31 >::type
32 dot(const Kokkos::View<XD,XP...>& x,
33 const Kokkos::View<YD,YP...>& y)
34 {
35 typedef Kokkos::View<XD,XP...> XVector;
36 typedef Kokkos::View<YD,YP...> YVector;
37
38 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
39 typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
40
41 return dot( x_flat, y_flat );
42 }
43
44 template <typename RV,
45 typename XD, typename ... XP,
46 typename YD, typename ... YP>
47 typename std::enable_if<
48 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value &&
49 Kokkos::is_view_uq_pce< Kokkos::View<YD,YP...> >::value >::type
50 dot(const RV& r,
51 const Kokkos::View<XD,XP...>& x,
52 const Kokkos::View<YD,YP...>& y)
53 {
54 typedef Kokkos::View<XD,XP...> XVector;
55 typedef Kokkos::View<YD,YP...> YVector;
56
57 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
58 typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
59
60 dot( r, x_flat, y_flat );
61 }
62
63 template <typename XD, typename ... XP>
64 typename std::enable_if<
65 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value >::type
66 fill(const Kokkos::View<XD,XP...>& x,
67 const typename Kokkos::View<XD,XP...>::non_const_value_type& val) {
68 typedef Kokkos::View<XD,XP...> XVector;
69
70 // Use the existing fill() implementation if we can
71 if (Sacado::is_constant(val)) {
72 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
73 fill( x_flat, val.coeff(0) );
74 }
75 else {
76 Kokkos::deep_copy(x, val);
77 }
78 }
79
80 template <typename RV,
81 typename XD, typename ... XP>
82 typename std::enable_if<
83 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value >::type
84 nrm2_squared(
85 const RV& r,
86 const Kokkos::View<XD,XP...>& x)
87 {
88 typedef Kokkos::View<XD,XP...> XVector;
89
90 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
91
92 nrm2_squared( r, x_flat );
93 }
94
95 template <typename RV,
96 typename XD, typename ... XP>
97 typename std::enable_if<
98 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value >::type
99 nrm1(
100 const RV& r,
101 const Kokkos::View<XD,XP...>& x)
102 {
103 typedef Kokkos::View<XD,XP...> XVector;
104
105 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
106
107 nrm1( r, x_flat );
108 }
109
110 template <typename RV,
111 typename XD, typename ... XP>
112 typename std::enable_if<
113 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value >::type
114 nrmInf(
115 const RV& r,
116 const Kokkos::View<XD,XP...>& x)
117 {
118 typedef Kokkos::View<XD,XP...> XVector;
119
120 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
121
122 nrmInf( r, x_flat );
123 }
124
125 template <typename AV,
126 typename XD, typename ... XP,
127 typename BV,
128 typename YD, typename ... YP>
129 typename std::enable_if<
130 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value &&
131 Kokkos::is_view_uq_pce< Kokkos::View<YD,YP...> >::value >::type
132 axpby(const AV& a,
133 const Kokkos::View<XD,XP...>& x,
134 const BV& b,
135 const Kokkos::View<YD,YP...>& y)
136 {
137 typedef Kokkos::View<XD,XP...> XVector;
138 typedef Kokkos::View<YD,YP...> YVector;
139
140 if (!Sacado::is_constant(a) || !Sacado::is_constant(b)) {
141 Kokkos::Impl::raise_error("axpby not implemented for non-constant a or b");
142 }
143
144 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
145 typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
146 auto aa = Sacado::Value<AV>::eval(a);
147 auto bb = Sacado::Value<BV>::eval(b);
148 axpby( aa, x_flat, bb, y_flat );
149 }
150
151 // Currently not handling scal() when AV is a view
152
153 template <typename RD, typename ... RP,
154 typename XD, typename ... XP>
155 typename std::enable_if<
156 Kokkos::is_view_uq_pce< Kokkos::View<RD,RP...> >::value &&
157 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value >::type
158 scal(const Kokkos::View<RD,RP...>& r,
159 const typename Kokkos::View<XD,XP...>::non_const_value_type& a,
160 const Kokkos::View<XD,XP...>& x)
161 {
162 typedef Kokkos::View<RD,RP...> RVector;
163 typedef Kokkos::View<XD,XP...> XVector;
164
165 if (!Sacado::is_constant(a)) {
166 Kokkos::Impl::raise_error("scal not implemented for non-constant a");
167 }
168
169 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
170 typename Kokkos::FlatArrayType<RVector>::type r_flat = r;
171 scal( r_flat, a.coeff(0), x_flat );
172 }
173
174 // abs -- can't do this one by flattening. Hold out for refactoring of scalar
175 // types in Kokkos
176
177 // We have a special verision of update for scalar alpha/beta/gamma since it
178 // is used in TrilinosCouplings CG solve (even though Tpetra doesn't).
179 template <typename XD, typename ... XP,
180 typename YD, typename ... YP,
181 typename ZD, typename ... ZP>
182 typename std::enable_if<
183 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value &&
184 Kokkos::is_view_uq_pce< Kokkos::View<YD,YP...> >::value &&
185 Kokkos::is_view_uq_pce< Kokkos::View<ZD,ZP...> >::value >::type
186 update(
187 const typename Kokkos::View<XD,XP...>::array_type::non_const_value_type& alpha,
188 const Kokkos::View<XD,XP...>& x,
189 const typename Kokkos::View<YD,YP...>::array_type::non_const_value_type& beta,
190 const Kokkos::View<YD,YP...>& y,
191 const typename Kokkos::View<ZD,ZP...>::array_type::non_const_value_type& gamma,
192 const Kokkos::View<ZD,ZP...>& z)
193 {
194 typedef Kokkos::View<XD,XP...> XVector;
195 typedef Kokkos::View<YD,YP...> YVector;
196 typedef Kokkos::View<ZD,ZP...> ZVector;
197
198 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
199 typename Kokkos::FlatArrayType<YVector>::type y_flat = y;
200 typename Kokkos::FlatArrayType<ZVector>::type z_flat = z;
201
202 update( alpha, x_flat, beta, y_flat, gamma, z_flat);
203
204 }
205
206 template <typename XD, typename ... XP,
207 typename YD, typename ... YP,
208 typename ZD, typename ... ZP>
209 typename std::enable_if<
210 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value &&
211 Kokkos::is_view_uq_pce< Kokkos::View<YD,YP...> >::value &&
212 Kokkos::is_view_uq_pce< Kokkos::View<ZD,ZP...> >::value >::type
213 update(
214 const typename Kokkos::View<XD,XP...>::non_const_value_type& alpha,
215 const Kokkos::View<XD,XP...>& x,
216 const typename Kokkos::View<YD,YP...>::non_const_value_type& beta,
217 const Kokkos::View<YD,YP...>& y,
218 const typename Kokkos::View<ZD,ZP...>::non_const_value_type& gamma,
219 const Kokkos::View<ZD,ZP...>& z)
220 {
221 if (!Sacado::is_constant(alpha) || !Sacado::is_constant(beta) ||
222 !Sacado::is_constant(gamma)) {
223 Kokkos::Impl::raise_error(
224 "update not implemented for non-constant alpha, beta, gamma");
225 }
226
227 update( alpha.coeff(0), x, beta.coeff(0), y, gamma.coeff(0), z );
228 }
229
230 // Mean-based implementation of reciprocal()
231 namespace Impl {
232
233 template<class RS, class ... RP,
234 class XS, class ... XP,
235 class SizeType>
236 struct MV_Reciprocal_Functor<
237 Kokkos::View<Sacado::UQ::PCE<RS>**,RP...>,
238 Kokkos::View<const Sacado::UQ::PCE<XS>**,XP...>,
239 SizeType>
240 {
241 typedef Kokkos::View<Sacado::UQ::PCE<RS>**,RP...> RMV;
242 typedef Kokkos::View<const Sacado::UQ::PCE<XS>**,XP...> XMV;
243 typedef typename RMV::execution_space execution_space;
244 typedef SizeType size_type;
245 typedef Kokkos::ArithTraits<typename Kokkos::IntrinsicScalarType<XMV>::type> ATS;
246
247 const size_type numCols;
248 RMV R_;
249 XMV X_;
250
251 MV_Reciprocal_Functor (const RMV& R, const XMV& X) :
252 numCols (X.extent(1)), R_ (R), X_ (X)
253 {
254 }
255
256 KOKKOS_INLINE_FUNCTION
257 void operator() (const size_type& i) const
258 {
259 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
260 #pragma ivdep
261 #endif
262 for (size_type j = 0; j < numCols; ++j) {
263 R_(i,j) = ATS::one () / X_(i,j).fastAccessCoeff(0);
264 }
265 }
266 };
267
268 template<class RS, class ... RP,
269 class SizeType>
270 struct MV_ReciprocalSelf_Functor<
271 Kokkos::View<Sacado::UQ::PCE<RS>**,RP...>,
272 SizeType>
273 {
274 typedef Kokkos::View<Sacado::UQ::PCE<RS>**,RP...> RMV;
275 typedef typename RMV::execution_space execution_space;
276 typedef SizeType size_type;
277 typedef Kokkos::ArithTraits<typename Kokkos::IntrinsicScalarType<RMV>::type> ATS;
278
279 const size_type numCols;
280 RMV R_;
281
282 MV_ReciprocalSelf_Functor (const RMV& R) :
283 numCols (R.extent(1)), R_ (R)
284 {
285 }
286
287 KOKKOS_INLINE_FUNCTION
288 void operator() (const size_type& i) const
289 {
290 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
291 #pragma ivdep
292 #endif
293 for (size_type j = 0; j < numCols; ++j) {
294 R_(i,j) = ATS::one () / R_(i,j).fastAccessCoeff(0);
295 }
296 }
297 };
298
299 template<class RS, class ... RP,
300 class XS, class ... XP,
301 class SizeType>
302 struct V_Reciprocal_Functor<
303 Kokkos::View<Sacado::UQ::PCE<RS>*,RP...>,
304 Kokkos::View<const Sacado::UQ::PCE<XS>*,XP...>,
305 SizeType>
306 {
307 typedef Kokkos::View<Sacado::UQ::PCE<RS>*,RP...> RV;
308 typedef Kokkos::View<const Sacado::UQ::PCE<XS>*,XP...> XV;
309 typedef typename RV::execution_space execution_space;
310 typedef SizeType size_type;
311 typedef Kokkos::ArithTraits<typename Kokkos::IntrinsicScalarType<XV>::type> ATS;
312
313 RV R_;
314 XV X_;
315
316 V_Reciprocal_Functor (const RV& R, const XV& X) : R_ (R), X_ (X)
317 {
318 }
319
320 KOKKOS_INLINE_FUNCTION
321 void operator() (const size_type& i) const
322 {
323 R_(i) = ATS::one () / X_(i).fastAccessCoeff(0);
324 }
325 };
326
327 template<class RS, class ... RP,
328 class SizeType>
329 struct V_ReciprocalSelf_Functor<
330 Kokkos::View<Sacado::UQ::PCE<RS>*,RP...>,
331 SizeType>
332 {
333 typedef Kokkos::View<Sacado::UQ::PCE<RS>*,RP...> RV;
334 typedef typename RV::execution_space execution_space;
335 typedef SizeType size_type;
336 typedef Kokkos::ArithTraits<typename Kokkos::IntrinsicScalarType<RV>::type> ATS;
337
338 RV R_;
339
340 V_ReciprocalSelf_Functor (const RV& R) : R_ (R)
341 {
342 }
343
344 KOKKOS_INLINE_FUNCTION
345 void operator() (const size_type& i) const
346 {
347 R_(i) = ATS::one () / R_(i).fastAccessCoeff(0);
348 }
349 };
350
351 } // namespace Impl
352
353 template <typename RD, typename ... RP,
354 typename XD, typename ... XP>
355 typename std::enable_if<
356 Kokkos::is_view_uq_pce< Kokkos::View<RD,RP...> >::value &&
357 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value >::type
358 sum(
359 const Kokkos::View<RD,RP...>& r,
360 const Kokkos::View<XD,XP...>& x)
361 {
362 typedef Kokkos::View<RD,RP...> RVector;
363 typedef Kokkos::View<XD,XP...> XVector;
364
365 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
366 typename Kokkos::FlatArrayType<RVector>::type r_flat = r;
367 sum( r_flat, x_flat );
368 }
369
370 template <typename RD, typename ... RP,
371 typename XD, typename ... XP,
372 typename WD, typename ... WP>
373 typename std::enable_if<
374 Kokkos::is_view_uq_pce< Kokkos::View<RD,RP...> >::value &&
375 Kokkos::is_view_uq_pce< Kokkos::View<XD,XP...> >::value &&
376 Kokkos::is_view_uq_pce< Kokkos::View<WD,WP...> >::value >::type
377 nrm2w_squared(
378 const Kokkos::View<RD,RP...>& r,
379 const Kokkos::View<XD,XP...>& x,
380 const Kokkos::View<WD,WP...>& w)
381 {
382 typedef Kokkos::View<RD,RP...> RVector;
383 typedef Kokkos::View<XD,XP...> XVector;
384 typedef Kokkos::View<WD,WP...> WVector;
385
386 typename Kokkos::FlatArrayType<XVector>::type x_flat = x;
387 typename Kokkos::FlatArrayType<RVector>::type r_flat = r;
388 typename Kokkos::FlatArrayType<WVector>::type w_flat = w;
389 nrm2w_squared( r_flat, x_flat, w_flat );
390 }
391
392 // Mean-based implementation of mult()
393 namespace Impl {
394
395 template<class CS, class ... CP,
396 class AS, class ... AP,
397 class BS, class ... BP,
398 int scalar_ab, int scalar_c, class SizeType>
399 struct MV_MultFunctor<
400 Kokkos::View<Sacado::UQ::PCE<CS>**,CP...>,
401 Kokkos::View<const Sacado::UQ::PCE<AS>*,AP...>,
402 Kokkos::View<const Sacado::UQ::PCE<BS>**,BP...>,
403 scalar_ab, scalar_c, SizeType>
404 {
405 typedef Kokkos::View<Sacado::UQ::PCE<CS>**,CP...> CMV;
406 typedef Kokkos::View<const Sacado::UQ::PCE<AS>*,AP...> AV;
407 typedef Kokkos::View<const Sacado::UQ::PCE<BS>**,BP...> BMV;
408 typedef typename CMV::execution_space execution_space;
409 typedef SizeType size_type;
410 typedef Kokkos::ArithTraits<typename Kokkos::IntrinsicScalarType<CMV>::type> ATS;
411
412 const size_type m_n;
413 const size_type m_pce;
414 const typename Kokkos::IntrinsicScalarType<CMV>::type m_c;
415 CMV m_C;
416 const typename Kokkos::IntrinsicScalarType<AV>::type m_ab;
417 AV m_A;
418 BMV m_B;
419
420 MV_MultFunctor (typename CMV::const_value_type& c,
421 const CMV& C,
422 typename AV::const_value_type& ab,
423 const AV& A,
424 const BMV& B) :
425 m_n (C.extent(1)),
426 m_pce (dimension_scalar(C)),
427 m_c (c.coeff(0)), m_C (C), m_ab (ab.coeff(0)), m_A (A), m_B (B)
428 {
429 if (!Sacado::is_constant(c) || !Sacado::is_constant(ab)) {
430 Kokkos::Impl::raise_error("mult not implemented for non-constant c, ab");
431 }
432 }
433
434 KOKKOS_INLINE_FUNCTION void
435 operator () (const size_type& i) const
436 {
437 if (scalar_c == 0) {
438 if (scalar_ab == 0) {
439 for (size_type j = 0; j < m_n; ++j) {
440 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
441 #pragma ivdep
442 #endif
443 for (size_type l=0; l<m_pce; ++l)
444 m_C(i,j).fastAccessCoeff(l) = ATS::zero ();
445 }
446 }
447 else { // ab != 0, c == 0
448 typename Kokkos::IntrinsicScalarType<AV>::type Ai = m_A(i).fastAccessCoeff(0);
449 for (size_type j = 0; j < m_n; ++j) {
450 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
451 #pragma ivdep
452 #endif
453 for (size_type l=0; l<m_pce; ++l)
454 m_C(i,j).fastAccessCoeff(l) =
455 m_ab * Ai * m_B(i,j).fastAccessCoeff(l);
456 }
457 }
458 } else { // c != 0
459 if (scalar_ab == 0) {
460 for (size_type j = 0; j < m_n; ++j) {
461 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
462 #pragma ivdep
463 #endif
464 for (size_type l=0; l<m_pce; ++l)
465 m_C(i,j).fastAccessCoeff(l) = m_c * m_C(i,j).fastAccessCoeff(l);
466 }
467 }
468 else { // m_ab != 0, and m_c != 0
469 typename Kokkos::IntrinsicScalarType<AV>::type Ai = m_A(i).fastAccessCoeff(0);
470 for (size_type j = 0; j < m_n; ++j) {
471 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
472 #pragma ivdep
473 #endif
474 for (size_type l=0; l<m_pce; ++l)
475 m_C(i,j).fastAccessCoeff(l) =
476 m_c * m_C(i,j).fastAccessCoeff(l) + m_ab * Ai * m_B(i,j).fastAccessCoeff(l);
477 }
478 }
479 }
480 }
481 };
482
483 template<class CS, class ... CP,
484 class AS, class ... AP,
485 class BS, class ... BP,
486 int scalar_ab, int scalar_c, class SizeType>
487 struct V_MultFunctor<
488 Kokkos::View<Sacado::UQ::PCE<CS>*,CP...>,
489 Kokkos::View<const Sacado::UQ::PCE<AS>*,AP...>,
490 Kokkos::View<const Sacado::UQ::PCE<BS>*,BP...>,
491 scalar_ab, scalar_c, SizeType>
492 {
493 typedef Kokkos::View<Sacado::UQ::PCE<CS>*,CP...> CV;
494 typedef Kokkos::View<const Sacado::UQ::PCE<AS>*,AP...> AV;
495 typedef Kokkos::View<const Sacado::UQ::PCE<BS>*,BP...> BV;
496 typedef typename CV::execution_space execution_space;
497 typedef SizeType size_type;
498 typedef Kokkos::ArithTraits<typename Kokkos::IntrinsicScalarType<CV>::type> ATS;
499
500 const size_type m_pce;
501 const typename Kokkos::IntrinsicScalarType<CV>::type m_c;
502 CV m_C;
503 const typename Kokkos::IntrinsicScalarType<AV>::type m_ab;
504 AV m_A;
505 BV m_B;
506
507 V_MultFunctor (typename CV::const_value_type& c,
508 const CV& C,
509 typename AV::const_value_type& ab,
510 const AV& A,
511 const BV& B) :
512 m_pce (dimension_scalar(C)),
513 m_c (c.coeff(0)), m_C (C), m_ab (ab.coeff(0)), m_A (A), m_B (B)
514 {
515 if (!Sacado::is_constant(c) || !Sacado::is_constant(ab)) {
516 Kokkos::Impl::raise_error("mult not implemented for non-constant c, ab");
517 }
518 }
519
520 KOKKOS_INLINE_FUNCTION void
521 operator () (const size_type& i) const
522 {
523 if (scalar_c == 0) {
524 if (scalar_ab == 0) {
525 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
526 #pragma ivdep
527 #endif
528 for (size_type l=0; l<m_pce; ++l)
529 m_C(i).fastAccessCoeff(l) = ATS::zero ();
530 }
531 else { // ab != 0, c == 0
532 typename Kokkos::IntrinsicScalarType<AV>::type Ai = m_A(i).fastAccessCoeff(0);
533 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
534 #pragma ivdep
535 #endif
536 for (size_type l=0; l<m_pce; ++l)
537 m_C(i).fastAccessCoeff(l) = m_ab * Ai * m_B(i).fastAccessCoeff(l);
538 }
539 } else { // c != 0
540 if (scalar_ab == 0) {
541 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
542 #pragma ivdep
543 #endif
544 for (size_type l=0; l<m_pce; ++l)
545 m_C(i).fastAccessCoeff(l) = m_c * m_C(i).fastAccessCoeff(l);
546 }
547 else { // m_ab != 0, and m_c != 0
548 typename Kokkos::IntrinsicScalarType<AV>::type Ai = m_A(i).fastAccessCoeff(0);
549 #ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
550 #pragma ivdep
551 #endif
552 for (size_type l=0; l<m_pce; ++l)
553 m_C(i).fastAccessCoeff(l) =
554 m_c * m_C(i).fastAccessCoeff(l) + m_ab * Ai * m_B(i).fastAccessCoeff(l);
555 }
556 }
557 }
558 };
559
560 } // namespace Impl
561
562 } // namespace KokkosBlas
563
564 #endif /* #ifndef KOKKOS_MV_UQ_PCE_HPP */