Debian Codegraph @

packages/trilinoscouplings/examples/fenl/SampleGrouping.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 //
11 // Strategies for grouping samples for ensemble propagation
12 //
13 //
14
15 #include "Teuchos_Array.hpp"
16 #include "Teuchos_Comm.hpp"
17 #include "BoxElemFixture.hpp"
18 #include "HexElement.hpp"
19 #include "Kokkos_ArithTraits.hpp"
20
21 namespace Kokkos {
22 namespace Example {
23 namespace FENL {
24
25 template <typename Scalar>
26 class SampleGrouping {
27 public:
28
29 SampleGrouping() {}
30
31 virtual ~SampleGrouping() {}
32
33 // Given a set of sample values and a group size, group the samples into
34 // groups of that size
35 virtual void group(
36 const Teuchos::Ordinal group_size,
37 const Teuchos::Array< Teuchos::Array<Scalar> >& samples,
38 Teuchos::Array< Teuchos::Array<Teuchos::Ordinal> >& groups,
39 Teuchos::Ordinal& num_duplicate) const = 0;
40 };
41
42 // Grouping based on the natural ordering of the samples as provided
43 template <typename Scalar>
44 class NaturalGrouping : public virtual SampleGrouping<Scalar> {
45 public:
46
47 NaturalGrouping() {}
48
49 virtual ~NaturalGrouping() {}
50
51 // Given a set of sample values and a group size, group the samples into
52 // groups of that size
53 virtual void group(
54 const Teuchos::Ordinal group_size,
55 const Teuchos::Array< Teuchos::Array<Scalar> >& samples,
56 Teuchos::Array< Teuchos::Array<Teuchos::Ordinal> >& groups,
57 Teuchos::Ordinal& num_duplicate) const
58 {
59 using Teuchos::Ordinal;
60
61 Ordinal num_samples = samples.size();
62 if (num_samples == 0) {
63 groups.resize(0);
64 num_duplicate = 0;
65 return;
66 }
67
68 Ordinal num_groups = (num_samples + group_size - 1) / group_size;
69 Ordinal remainder = num_groups * group_size - num_samples;
70 Teuchos::Array<Ordinal> group;
71 group.reserve(group_size);
72 groups.reserve(num_groups);
73 for (Ordinal sample_index=0; sample_index<num_samples; ++sample_index) {
74 if (sample_index > 0 && sample_index % group_size == 0) {
75 TEUCHOS_ASSERT( group.size() == group_size );
76 groups.push_back(group);
77 group.resize(0);
78 }
79 group.push_back(sample_index);
80 }
81 for (Ordinal i=0; i<remainder; ++i)
82 group.push_back(num_samples-1);
83 groups.push_back(group);
84
85 num_duplicate = remainder;
86
87 TEUCHOS_ASSERT( groups.size() == num_groups );
88 }
89 };
90
91 // Grouping based on diffusion coefficient anisotropy
92 template <typename Scalar, typename MeshType, typename CoeffFunctionType>
93 class MaxAnisotropyGrouping : public virtual SampleGrouping<Scalar> {
94 public:
95
96 MaxAnisotropyGrouping(const Teuchos::RCP<const Teuchos::Comm<int> > & comm,
97 const MeshType& mesh,
98 const CoeffFunctionType& coeff_function) :
99 m_comm(comm),
100 m_max_min_functor(mesh, coeff_function)
101 {}
102
103 virtual ~MaxAnisotropyGrouping() {}
104
105 // Given a set of sample values and a group size, group the samples into
106 // groups of that size
107 virtual void group(
108 const Teuchos::Ordinal group_size,
109 const Teuchos::Array< Teuchos::Array<Scalar> >& samples,
110 Teuchos::Array< Teuchos::Array<Teuchos::Ordinal> >& groups,
111 Teuchos::Ordinal& num_duplicate) const
112 {
113 using Teuchos::Ordinal;
114
115 Ordinal num_samples = samples.size();
116 if (num_samples == 0) {
117 groups.resize(0);
118 num_duplicate = 0;
119 return;
120 }
121
122 // Compute the maximum and minimum values of the diffusion coefficient
123 // over the mesh (which is our measure of anisotropy) for each sample point
124 typedef typename CoeffFunctionType::RandomVariableView RV;
125 typedef typename RV::HostMirror HRV;
126 RV rv = m_max_min_functor.m_coeff_function.getRandomVariables();
127 HRV hrv = Kokkos::create_mirror_view(rv);
128 const Ordinal dim = rv.extent(0);
129 Teuchos::Array< std::pair<Scalar,Ordinal> > coeffs(num_samples);
130 for (Ordinal sample_index=0; sample_index<num_samples; ++sample_index) {
131 for (Ordinal i=0; i<dim; ++i)
132 hrv(i) = samples[sample_index][i];
133 Kokkos::deep_copy( rv, hrv );
134
135 Ordinal num_elem = m_max_min_functor.m_elem_node_ids.extent(0);
136 Scalar local_coeff[2] = { 0.0, Kokkos::ArithTraits<Scalar>::max() };
137 parallel_reduce( num_elem, m_max_min_functor, local_coeff );
138
139 Scalar coeff[2] = { 0.0, Kokkos::ArithTraits<Scalar>::max() };
140 Teuchos::reduceAll( *m_comm, Teuchos::REDUCE_MAX, 1, &local_coeff[0], &coeff[0] );
141 Teuchos::reduceAll( *m_comm, Teuchos::REDUCE_MIN, 1, &local_coeff[1], &coeff[1] );
142
143 if (coeff[0] >= 1.0 / coeff[1])
144 coeffs[sample_index].first = coeff[0];
145 else
146 coeffs[sample_index].first = 1.0 / coeff[1];
147 coeffs[sample_index].second = sample_index;
148 }
149
150 // Sort based on increasing anisotropy (sort with an array of pair objects
151 // by default sorts on the first value, using the second to resolve ties)
152 std::sort( coeffs.begin(), coeffs.end() );
153
154 // Now group based on the sorted ordering
155 Ordinal num_groups = (num_samples + group_size - 1) / group_size;
156 Ordinal remainder = num_groups * group_size - num_samples;
157 Teuchos::Array<Ordinal> group;
158 group.reserve(group_size);
159 groups.reserve(num_groups);
160 for (Ordinal sample_index=0; sample_index<num_samples; ++sample_index) {
161 if (sample_index > 0 && sample_index % group_size == 0) {
162 TEUCHOS_ASSERT( group.size() == group_size );
163 groups.push_back(group);
164 group.resize(0);
165 }
166 group.push_back(coeffs[sample_index].second);
167 }
168 for (Ordinal i=0; i<remainder; ++i)
169 group.push_back(coeffs[num_samples-1].second);
170 groups.push_back(group);
171
172 num_duplicate = remainder;
173
174 TEUCHOS_ASSERT( groups.size() == num_groups );
175 }
176
177 public:
178
179 // Needs to be a public nested class for Cuda
180 struct MaxMinFunctor {
181
182 typedef Kokkos::Example::HexElement_Data< MeshType::ElemNode > elem_data_type;
183 typedef typename MeshType::elem_node_type elem_node_type;
184 typedef typename MeshType::node_coord_type node_coord_type;
185
186 static const unsigned ElemNodeCount = elem_data_type::element_node_count;
187 static const unsigned IntegrationCount = elem_data_type::integration_count;
188
189 typedef typename MeshType::execution_space execution_space;
190 typedef Scalar value_type[];
191
192 const elem_data_type m_elem_data;
193 const elem_node_type m_elem_node_ids;
194 const node_coord_type m_node_coords;
195 const CoeffFunctionType m_coeff_function;
196 const unsigned value_count;
197
198 MaxMinFunctor(const MeshType& mesh,
199 const CoeffFunctionType& coeff_function) :
200 m_elem_data(),
201 m_elem_node_ids( mesh.elem_node() ),
202 m_node_coords( mesh.node_coord() ),
203 m_coeff_function(coeff_function),
204 value_count(2) {}
205
206 KOKKOS_INLINE_FUNCTION
207 void operator()( const unsigned ielem, value_type coeff_max_min ) const
208 {
209 // Storage for diffusion coefficient at each node
210 Scalar coeff_k[ ElemNodeCount ];
211 for ( unsigned j = 0; j < ElemNodeCount; ++j)
212 coeff_k[j] = 0.0;
213
214 // Extract nodal coordinates
215 Scalar x[ ElemNodeCount ] ;
216 Scalar y[ ElemNodeCount ] ;
217 Scalar z[ ElemNodeCount ] ;
218 for ( unsigned i = 0 ; i < ElemNodeCount ; ++i ) {
219 const unsigned ni = m_elem_node_ids( ielem , i );
220 x[i] = m_node_coords( ni , 0 );
221 y[i] = m_node_coords( ni , 1 );
222 z[i] = m_node_coords( ni , 2 );
223 }
224
225 for ( unsigned i = 0 ; i < IntegrationCount ; ++i ) {
226
227 // Compute physical coordinates of integration point
228 Scalar pt[] = { 0 , 0 , 0 } ;
229 for ( unsigned j = 0 ; j < ElemNodeCount ; ++j ) {
230 pt[0] += x[j] * m_elem_data.values[i][j] ;
231 pt[1] += y[j] * m_elem_data.values[i][j] ;
232 pt[2] += z[j] * m_elem_data.values[i][j] ;
233 }
234
235 // Evaluate diffusion coefficient at integration point
236 Scalar coeff_k_at_pt = m_coeff_function(pt,0);
237
238 // Compute diffusion coefficient at nodes
239 for ( unsigned j = 0; j < ElemNodeCount; ++j)
240 coeff_k[j] +=
241 m_elem_data.weights[i] * coeff_k_at_pt * m_elem_data.values[i][j] ;
242
243 }
244
245 // Compute the maximum value of diffusion coefficient among nodes
246 for ( unsigned j = 0; j < ElemNodeCount; ++j) {
247 if (coeff_k[j] > coeff_max_min[0])
248 coeff_max_min[0] = coeff_k[j];
249 if (coeff_k[j] < coeff_max_min[1])
250 coeff_max_min[1] = coeff_k[j];
251 }
252 }
253
254 KOKKOS_INLINE_FUNCTION
255 void join (value_type dst, const value_type src) const
256 {
257 if (src[0] > dst[0])
258 dst[0] = src[0];
259 if (src[1] < dst[1])
260 dst[1] = src[1];
261 }
262
263 // Hack to work around Kokkos bug
264 KOKKOS_INLINE_FUNCTION
265 void init (Scalar** dst) const {}
266
267 KOKKOS_INLINE_FUNCTION
268 void init (value_type dst) const
269 {
270 //TEUCHOS_ASSERT( false );
271
272 // The diffusion coefficient must be positive, so initializing the
273 // result to zero is safe
274 dst[0] = 0.0;
275 dst[1] = Kokkos::ArithTraits<Scalar>::max();
276 }
277
278 };
279
280 private:
281
282 Teuchos::RCP<const Teuchos::Comm<int> > m_comm;
283 MaxMinFunctor m_max_min_functor;
284
285 };
286
287 // Grouping based on iteration surrogate
288 template <typename Scalar, typename Surrogate>
289 class SurrogateGrouping : public virtual SampleGrouping<Scalar> {
290 public:
291
292 SurrogateGrouping(const Teuchos::RCP<Surrogate>& s,
293 const bool print_iterations,
294 const int comm_rank) :
295 m_s(s), m_print(print_iterations), m_comm_rank(comm_rank) {}
296
297 virtual ~SurrogateGrouping() {}
298
299 Teuchos::RCP<Surrogate> getSurrogate() { return m_s; }
300
301 void setSurrogate(const Teuchos::RCP<Surrogate>& s) { m_s = s; }
302
303 // Given a set of sample values and a group size, group the samples into
304 // groups of that size
305 virtual void group(
306 const Teuchos::Ordinal group_size,
307 const Teuchos::Array< Teuchos::Array<Scalar> >& samples,
308 Teuchos::Array< Teuchos::Array<Teuchos::Ordinal> >& groups,
309 Teuchos::Ordinal& num_duplicate) const
310 {
311 using Teuchos::Ordinal;
312
313 Ordinal num_samples = samples.size();
314 if (num_samples == 0) {
315 groups.resize(0);
316 num_duplicate = 0;
317 return;
318 }
319
320 // Evaluate the surrogate for each sample point
321 Teuchos::Array< std::pair<Scalar,Ordinal> > iterations(num_samples);
322 for (Ordinal sample_index=0; sample_index<num_samples; ++sample_index) {
323 iterations[sample_index].first = m_s->evaluate(samples[sample_index]);
324 iterations[sample_index].second = sample_index;
325 }
326
327 if (m_print && (m_comm_rank == 0)) {
328 std::cout << "Predicted sample iterations = ";
329 for (Ordinal sample_index=0; sample_index<num_samples; ++sample_index) {
330 std::cout << "("
331 << iterations[sample_index].second << ","
332 << iterations[sample_index].first
333 << ") ";
334 }
335 std::cout << std::endl;
336 }
337
338 // Sort based on increasing iterations (sort with an array of pair objects
339 // by default sorts on the first value, using the second to resolve ties)
340 std::sort( iterations.begin(), iterations.end() );
341
342 // Now group based on the sorted ordering
343 Ordinal num_groups = (num_samples + group_size - 1) / group_size;
344 Ordinal remainder = num_groups * group_size - num_samples;
345 Teuchos::Array<Ordinal> group;
346 group.reserve(group_size);
347 groups.reserve(num_groups);
348
349 for (Ordinal sample_index=0; sample_index<num_samples; ++sample_index) {
350
351 if (sample_index > 0 && sample_index % group_size == 0) {
352 TEUCHOS_ASSERT( group.size() == group_size );
353 groups.push_back(group);
354 group.resize(0);
355 }
356 group.push_back(iterations[sample_index].second);
357 }
358 for (Ordinal i=0; i<remainder; ++i)
359 group.push_back(iterations[num_samples-1].second);
360 groups.push_back(group);
361
362 num_duplicate = remainder;
363
364 TEUCHOS_ASSERT( groups.size() == num_groups );
365 }
366
367 private:
368
369 Teuchos::RCP<Surrogate> m_s;
370 int m_print;
371 int m_comm_rank;
372
373 };
374
375 } /* namespace FENL */
376 } /* namespace Example */
377 } /* namespace Kokkos */