Debian Codegraph @

packages/trilinoscouplings/examples/fenl/fenl_ensemble.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16
17 #ifndef KOKKOS_EXAMPLE_FENL_ENSEMBLE_HPP
18 #define KOKKOS_EXAMPLE_FENL_ENSEMBLE_HPP
19
20 //----------------------------------------------------------------------------
21 // Specializations for ensemble scalar type
22 //----------------------------------------------------------------------------
23
24 #include "Stokhos_Tpetra_MP_Vector.hpp"
25
26 #if defined( HAVE_STOKHOS_BELOS )
27 #include "Belos_TpetraAdapter_MP_Vector.hpp"
28 #include "BelosPseudoBlockCGSolMgr.hpp"
29 #endif
30
31 #if defined( HAVE_STOKHOS_MUELU )
32 #include "Stokhos_MueLu_MP_Vector.hpp"
33 #endif
34
35 namespace Kokkos {
36 namespace Example {
37 namespace FENL {
38
39 template <typename S>
40 typename S::value_type
41 scalar_norm(const Sacado::MP::Vector<S>& x) {
42 typename S::value_type z = 0.0;
43 for (typename S::ordinal_type i=0; i<x.size(); ++i)
44 z += x.fastAccessCoeff(i)*x.fastAccessCoeff(i);
45 z = std::sqrt(z);
46 return z;
47 }
48
49 } /* namespace FENL */
50 } /* namespace Example */
51 } /* namespace Kokkos */
52
53 #include <fenl.hpp>
54 #include <fenl_impl.hpp>
55
56 namespace Kokkos {
57 namespace Example {
58 namespace FENL {
59
60 template <typename S>
61 struct EnsembleTraits< Sacado::MP::Vector<S> > {
62 static const int size = S::static_size;
63 typedef typename S::value_type value_type;
64 KOKKOS_INLINE_FUNCTION
65 static const value_type& coeff(const Sacado::MP::Vector<S>& x, int i) {
66 return x.fastAccessCoeff(i);
67 }
68 KOKKOS_INLINE_FUNCTION
69 static value_type& coeff(Sacado::MP::Vector<S>& x, int i) {
70 return x.fastAccessCoeff(i);
71 }
72 };
73
74 #if defined( KOKKOS_ENABLE_CUDA )
75
76 template <typename ViewType>
77 struct LocalViewTraits<
78 ViewType,
79 typename std::enable_if< std::is_same<typename ViewType::execution_space,
80 Kokkos::Cuda>::value &&
81 Kokkos::is_view_mp_vector<ViewType>::value
82 >::type > {
83 typedef ViewType view_type;
84 typedef typename Kokkos::LocalMPVectorView<view_type,1>::type local_view_type;
85 typedef typename local_view_type::value_type local_value_type;
86 static const bool use_team = true;
87
88 KOKKOS_INLINE_FUNCTION
89 static local_view_type create_local_view(const view_type& v,
90 const unsigned local_rank)
91 {
92 return Kokkos::partition<1>(v, local_rank);
93 }
94 };
95
96 // Compute DeviceConfig struct's based on scalar type
97 template <typename StorageType>
98 struct CreateDeviceConfigs< Sacado::MP::Vector<StorageType> > {
99 typedef typename StorageType::execution_space execution_space;
100 static void eval( KokkosSparse::DeviceConfig& dev_config_elem,
101 KokkosSparse::DeviceConfig& dev_config_gath,
102 KokkosSparse::DeviceConfig& dev_config_bc ) {
103 static const unsigned VectorSize = StorageType::static_size;
104 if ( std::is_same< execution_space, Kokkos::Cuda >::value ) {
105 dev_config_elem = KokkosSparse::DeviceConfig( 0 , VectorSize , 64/VectorSize );
106 dev_config_gath = KokkosSparse::DeviceConfig( 0 , VectorSize , 128/VectorSize );
107 dev_config_bc = KokkosSparse::DeviceConfig( 0 , VectorSize , 256/VectorSize );
108 }
109 else {
110 dev_config_elem = KokkosSparse::DeviceConfig( 0 , 1 , 1 );
111 dev_config_gath = KokkosSparse::DeviceConfig( 0 , 1 , 1 );
112 dev_config_bc = KokkosSparse::DeviceConfig( 0 , 1 , 1 );
113 }
114 }
115 };
116 #endif
117
118 } /* namespace FENL */
119
120 #if defined(HAVE_TRILINOSCOUPLINGS_BELOS)
121 template <typename S, typename V, typename O>
122 struct ExtractEnsembleIts;
123
124 template <typename S, typename V, typename O>
125 struct ExtractEnsembleIts<Sacado::MP::Vector<S>,V,O> {
126 typedef Sacado::MP::Vector<S> Sc;
127
128 static std::vector<int>
129 apply(const Belos::SolverManager<Sc,V,O>& solver) {
130 const Belos::PseudoBlockCGSolMgr<Sc, V, O>* cg_solver =
131 dynamic_cast<const Belos::PseudoBlockCGSolMgr<Sc, V, O>*>(&solver);
132 if (cg_solver != 0)
133 return cg_solver->getResidualStatusTest()->getEnsembleIterations();
134 return std::vector<int>();
135 }
136 };
137 #endif
138
139 } /* namespace Example */
140 } /* namespace Kokkos */
141
142 #endif /* #ifndef KOKKOS_EXAMPLE_FENL_ENSEMBLE_HPP */