Line data Source code
1 : //* This file is part of the MOOSE framework
2 : //* https://www.mooseframework.org
3 : //*
4 : //* All rights reserved, see COPYRIGHT for full restrictions
5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 : //*
7 : //* Licensed under LGPL 2.1, please see LICENSE for details
8 : //* https://www.gnu.org/licenses/lgpl-2.1.html
9 :
10 : #pragma once
11 :
12 : #include "KokkosTypes.h"
13 : #include "KokkosMaterialPropertyValue.h"
14 : #include "KokkosDispatcher.h"
15 :
16 : #include "MaterialBase.h"
17 :
18 : namespace Moose::Kokkos
19 : {
20 :
21 : /**
22 : * The base class for Kokkos materials
23 : */
24 : class MaterialBase : public ::MaterialBase,
25 : public MeshHolder,
26 : public AssemblyHolder,
27 : public SystemHolder
28 : {
29 : public:
30 : static InputParameters validParams();
31 :
32 : /**
33 : * Constructor
34 : */
35 : MaterialBase(const InputParameters & parameters);
36 : /**
37 : * Copy constructor for parallel dispatch
38 : */
39 : MaterialBase(const MaterialBase & object);
40 :
41 : // Unused for Kokkos materials because all subdomains are computed in parallel
42 1509997 : virtual void subdomainSetup() override final {}
43 :
44 : /**
45 : * Kokkos function tags
46 : */
47 : ///@{
48 : struct ElementInit
49 : {
50 : };
51 : struct SideInit
52 : {
53 : };
54 : struct NeighborInit
55 : {
56 : };
57 : struct ElementCompute
58 : {
59 : };
60 : struct SideCompute
61 : {
62 : };
63 : struct NeighborCompute
64 : {
65 : };
66 : ///@}
67 :
68 : protected:
69 : /**
70 : * Declare a material property
71 : * @tparam T The property data type
72 : * @tparam dimension The property dimension
73 : * @param name The property name or the parameter name containing the property name
74 : * @param dims The vector containing the size of each dimension
75 : * @returns The material property
76 : */
77 : template <typename T, unsigned int dimension = 0>
78 : MaterialProperty<T, dimension> declareKokkosProperty(const std::string & name,
79 : const std::vector<unsigned int> & dims = {});
80 : /**
81 : * Declare an on-demand material property
82 : * @tparam T The property data type
83 : * @tparam dimension The property dimension
84 : * @param name The property name or the parameter name containing the property name
85 : * @param dims The vector containing the size of each dimension
86 : * @returns The material property
87 : */
88 : template <typename T, unsigned int dimension = 0>
89 : MaterialProperty<T, dimension>
90 : declareKokkosOnDemandProperty(const std::string & name,
91 : const std::vector<unsigned int> & dims = {});
92 : /**
93 : * Declare a material property by property name
94 : * @tparam T The property data type
95 : * @tparam dimension The property dimension
96 : * @param prop_name The property name
97 : * @param dims The vector containing the size of each dimension
98 : * @returns The material property
99 : */
100 : template <typename T, unsigned int dimension = 0>
101 : MaterialProperty<T, dimension>
102 2792 : declareKokkosPropertyByName(const std::string & prop_name,
103 : const std::vector<unsigned int> & dims = {})
104 : {
105 2792 : return declareKokkosPropertyInternal<T, dimension>(prop_name, dims, false);
106 : }
107 : /**
108 : * Declare an on-demand material property by property name
109 : * The on-demand property is only allocated when any object requests it
110 : * @tparam T The property data type
111 : * @tparam dimension The property dimension
112 : * @param prop_name The property name
113 : * @param dims The vector containing the size of each dimension
114 : * @returns The material property
115 : */
116 : template <typename T, unsigned int dimension = 0>
117 : MaterialProperty<T, dimension>
118 204 : declareKokkosOnDemandPropertyByName(const std::string & prop_name,
119 : const std::vector<unsigned int> & dims = {})
120 : {
121 204 : return declareKokkosPropertyInternal<T, dimension>(prop_name, dims, true);
122 : }
123 :
124 : /**
125 : * Get the number of elements this material operates on for element material property evaluation
126 : * @returns The number of elements
127 : */
128 28893 : KOKKOS_FUNCTION dof_id_type numKokkosElements() const { return _element_ids.size(); }
129 : /**
130 : * Get the number of sides this material is operating on for face material property evaluation
131 : * @returns The number of sides
132 : */
133 56468 : KOKKOS_FUNCTION dof_id_type numKokkosElementSides() const { return _element_side_ids.size(); }
134 : /**
135 : * Get the contiguous element ID for a thread
136 : * @param tid The thread ID
137 : * @returns The contiguous element ID
138 : */
139 1265315 : KOKKOS_FUNCTION ContiguousElementID kokkosElementID(ThreadID tid) const
140 : {
141 1265315 : return _element_ids[tid];
142 : }
143 : /**
144 : * Get the contiguous element ID - side index pair for a thread
145 : * @param tid The thread ID
146 : * @returns The contiguous element ID - side index pair
147 : */
148 98320 : KOKKOS_FUNCTION auto kokkosElementSideID(ThreadID tid) const { return _element_side_ids[tid]; }
149 :
150 : /**
151 : * Kokkos functor dispatchers
152 : */
153 : ///@{
154 : std::unique_ptr<DispatcherBase> _init_dispatcher;
155 : std::unique_ptr<DispatcherBase> _compute_dispatcher;
156 : ///@}
157 :
158 : /**
159 : * Whether the properties declared by this material are constant over element or subdomain
160 : */
161 : const PropertyConstantOption _constant_option;
162 :
163 : /**
164 : * TODO: Move to TransientInterface
165 : */
166 : ///@{
167 : /**
168 : * Time
169 : */
170 : Scalar<Real> _t;
171 : /**
172 : * Old time
173 : */
174 : Scalar<const Real> _t_old;
175 : /**
176 : * The number of the time step
177 : */
178 : Scalar<int> _t_step;
179 : /**
180 : * Time step size
181 : */
182 : Scalar<Real> _dt;
183 : /**
184 : * Size of the old time step
185 : */
186 : Scalar<Real> _dt_old;
187 : ///@}
188 :
189 : private:
190 : // Unused for Kokkos materials because they are hidden by Kokkos functions
191 0 : virtual void initQpStatefulProperties() override final {}
192 0 : virtual void computeQpProperties() override final {}
193 :
194 : /**
195 : * Setup block and boundary restrictions for material
196 : */
197 : void initializeMaterialRestrictable();
198 :
199 : /**
200 : * Internal method for declaring a material property
201 : * @tparam T The property data type
202 : * @tparam dimension The property dimension
203 : * @param prop_name The property name
204 : * @param dims The vector containing the size of each dimension
205 : * @param on_demand Whether the property is an on-demand property
206 : */
207 : template <typename T, unsigned int dimension>
208 : MaterialProperty<T, dimension> declareKokkosPropertyInternal(
209 : const std::string & prop_name, const std::vector<unsigned int> & dims, const bool on_demand);
210 :
211 : /**
212 : * Contiguous element IDs this material operates on for element material property evaluation
213 : */
214 : Array<ContiguousElementID> _element_ids;
215 : /**
216 : * Contiguous element ID - side index pairs this material operates on for face material property
217 : * evaluation
218 : */
219 : Array<Pair<ContiguousElementID, unsigned int>> _element_side_ids;
220 : };
221 :
222 : template <typename T, unsigned int dimension>
223 : MaterialProperty<T, dimension>
224 2792 : MaterialBase::declareKokkosProperty(const std::string & name,
225 : const std::vector<unsigned int> & dims)
226 : {
227 2792 : std::string prop_name = name;
228 2792 : if (_pars.have_parameter<MaterialPropertyName>(name))
229 561 : prop_name = _pars.get<MaterialPropertyName>(name);
230 :
231 5576 : return declareKokkosPropertyByName<T, dimension>(prop_name, dims);
232 2784 : }
233 :
234 : template <typename T, unsigned int dimension>
235 : MaterialProperty<T, dimension>
236 204 : MaterialBase::declareKokkosOnDemandProperty(const std::string & name,
237 : const std::vector<unsigned int> & dims)
238 : {
239 204 : std::string prop_name = name;
240 204 : if (_pars.have_parameter<MaterialPropertyName>(name))
241 0 : prop_name = _pars.get<MaterialPropertyName>(name);
242 :
243 408 : return declareKokkosOnDemandPropertyByName<T, dimension>(prop_name, dims);
244 204 : }
245 :
246 : template <typename T, unsigned int dimension>
247 : MaterialProperty<T, dimension>
248 2996 : MaterialBase::declareKokkosPropertyInternal(const std::string & prop_name,
249 : const std::vector<unsigned int> & dims,
250 : const bool on_demand)
251 : {
252 2996 : if (dims.size() != dimension)
253 0 : mooseError("The declared Kokkos material property '",
254 : prop_name,
255 : "'\nhas a different dimension (",
256 : dimension,
257 : ") with the provided dimension (",
258 0 : dims.size(),
259 : ").");
260 :
261 4694 : const auto prop_name_modified =
262 1698 : _declare_suffix.empty()
263 1698 : ? prop_name
264 1698 : : MooseUtils::join(std::vector<std::string>({prop_name, _declare_suffix}), "_");
265 :
266 2996 : auto prop = materialData().declareKokkosProperty<T, dimension>(
267 1698 : prop_name_modified, dims, this, isBoundaryMaterial(), on_demand, _constant_option);
268 :
269 2988 : registerPropName(prop_name_modified, false, 0);
270 :
271 5976 : return prop;
272 2988 : }
273 :
274 : } // namespace Moose::Kokkos
|