Debian Codegraph @

packages/trilinoscouplings/examples/fenl/main_ensemble.cpp

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 #include "Stokhos_Sacado_Kokkos_MP_Vector.hpp"
11 #include "Stokhos_Sacado_Kokkos_UQ_PCE.hpp"
12 #include "Stokhos.hpp"
13 #include "Stokhos_Ensemble_Sizes.hpp"
14 #include "Sacado_mpl_for_each.hpp"
15
16 //----------------------------------------------------------------------------
17
18 #include <Kokkos_Core.hpp>
19
20 #include <fenl_ensemble.hpp>
21 #include <fenl_utils.hpp>
22 #include <SampleGrouping.hpp>
23
24 //----------------------------------------------------------------------------
25
26 #include <Tpetra_Version.hpp>
27 #include <Teuchos_Comm.hpp>
28 #include <Teuchos_DefaultComm.hpp>
29 #include <Teuchos_GlobalMPISession.hpp>
30 #include <Teuchos_oblackholestream.hpp>
31 #include <Teuchos_StandardCatchMacros.hpp>
32
33 #ifdef HAVE_TRILINOSCOUPLINGS_TASMANIAN
34 #include <TasmanianSparseGrid.hpp>
35 #endif
36
37 #include "VPS_ensemble.hpp"
38
39 // For unit-testing
40 #include "Teuchos_TestingHelpers.hpp"
41
42 //----------------------------------------------------------------------------
43
44 template < class ProblemType, class CoeffFunctionType >
45 void run_samples(
46 const Teuchos::Comm<int>& comm ,
47 ProblemType& problem,
48 CoeffFunctionType& coeff_function,
49 const Teuchos::RCP<Kokkos::Example::FENL::SampleGrouping<double> >& grouper,
50 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
51 const CMD & cmd ,
52 const double bc_lower_value,
53 const double bc_upper_value,
54 const Teuchos::Array< Teuchos::Array<double> >& points,
55 Teuchos::Array<double>& responses,
56 Teuchos::Array< Teuchos::Array<double> >& response_gradients,
57 Teuchos::Array<int>& iterations,
58 Teuchos::Array<int>& ensemble_iterations,
59 Kokkos::Example::FENL::Perf& perf_total)
60 {
61 typedef typename CoeffFunctionType::RandomVariableView RV;
62 typedef typename RV::HostMirror HRV;
63 const int dim = cmd.USE_UQ_DIM;
64 RV rv("KL Random Variables", dim);
65 HRV hrv = Kokkos::create_mirror_view(rv);
66
67 const int num_samples = points.size();
68 for (int sample=0; sample<num_samples; ++sample) {
69
70 // Set random variable values to this sample
71 for (int i=0; i<dim; ++i)
72 hrv(i) = points[sample][i];
73 Kokkos::deep_copy( rv, hrv );
74 coeff_function.setRandomVariables(rv);
75
76 // Evaluate response at quadrature point
77 double response = 0;
78 Teuchos::Array<double> response_gradient;
79 if (response_gradients.size() > 0)
80 response_gradient.resize(dim);
81 Kokkos::Example::FENL::Perf perf =
82 fenl( problem , fenlParams ,
83 cmd.PRINT , cmd.USE_TRIALS , cmd.USE_ATOMIC ,
84 cmd.USE_BELOS , cmd.USE_MUELU , cmd.USE_MEANBASED ,
85 coeff_function , cmd.USE_ISOTROPIC ,
86 cmd.USE_COEFF_SRC , cmd.USE_COEFF_ADV ,
87 bc_lower_value , bc_upper_value ,
88 response, response_gradient);
89
90 responses[sample] = response;
91 if (response_gradients.size() > 0)
92 response_gradients[sample] = response_gradient;
93 iterations[sample] = perf.cg_iter_count;
94 ensemble_iterations[sample] = perf.cg_iter_count;
95
96 if (cmd.PRINT_ITS && 0 == comm.getRank()) {
97 std::cout << sample << " : " << perf.cg_iter_count << " ( ";
98 for (int i=0; i<dim; ++i)
99 std::cout << hrv(i) << " ";
100 std::cout << ")" << std::endl;
101 }
102
103 // Increment timing statistics
104 perf_total.increment(perf, !cmd.USE_BELOS);
105
106 }
107 }
108
109 template < class Storage,
110 class Device ,
111 Kokkos::Example::BoxElemPart::ElemOrder ElemOrder,
112 class CoeffFunctionType >
113 void run_samples(
114 const Teuchos::Comm<int>& comm ,
115 Kokkos::Example::FENL::Problem< Sacado::MP::Vector<Storage>, Device, ElemOrder>& problem ,
116 CoeffFunctionType & coeff_function,
117 const Teuchos::RCP<Kokkos::Example::FENL::SampleGrouping<double> >& grouper,
118 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
119 const CMD & cmd ,
120 const double bc_lower_value,
121 const double bc_upper_value,
122 const Teuchos::Array< Teuchos::Array<double> >& points,
123 Teuchos::Array<double>& responses,
124 Teuchos::Array< Teuchos::Array<double> >& response_gradients,
125 Teuchos::Array<int>& iterations,
126 Teuchos::Array<int>& ensemble_iterations,
127 Kokkos::Example::FENL::Perf& perf_total)
128 {
129 using Teuchos::Array;
130 using Teuchos::Ordinal;
131
132 typedef typename Sacado::MP::Vector<Storage> Scalar;
133 typedef typename CoeffFunctionType::RandomVariableView RV;
134 typedef typename RV::HostMirror HRV;
135 static const int VectorSize = Storage::static_size;
136
137 // Group points into ensembles
138 Array< Array<Ordinal> > groups;
139 Ordinal num_duplicate = 0;
140 grouper->group(VectorSize, points, groups, num_duplicate);
141
142 const int num_groups = groups.size();
143 const int dim = cmd.USE_UQ_DIM;
144 RV rv("KL Random Variables", dim, VectorSize);
145 HRV hrv = Kokkos::create_mirror_view(rv);
146
147 // Loop over quadrature point groups
148 for (int group=0; group<num_groups; ++group) {
149
150 // Set random variables
151 for (int qp=0; qp<VectorSize; ++qp)
152 for (int i=0; i<dim; ++i)
153 hrv(i).fastAccessCoeff(qp) = points[groups[group][qp]][i];
154 Kokkos::deep_copy( rv, hrv );
155 coeff_function.setRandomVariables(rv);
156
157 // Evaluate response at quadrature point
158 Scalar response = 0;
159 Teuchos::Array<Scalar> response_gradient;
160 if (response_gradients.size() > 0)
161 response_gradient.resize(dim);
162 Kokkos::Example::FENL::Perf perf =
163 fenl( problem , fenlParams ,
164 cmd.PRINT , cmd.USE_TRIALS , cmd.USE_ATOMIC ,
165 cmd.USE_BELOS , cmd.USE_MUELU , cmd.USE_MEANBASED ,
166 coeff_function , cmd.USE_ISOTROPIC ,
167 cmd.USE_COEFF_SRC , cmd.USE_COEFF_ADV ,
168 bc_lower_value , bc_upper_value ,
169 response, response_gradient);
170
171 // Save response and iterations
172 const int ensemble_it_size = perf.ensemble_cg_iter_count.size();
173 for (int qp=0; qp<VectorSize; ++qp) {
174 responses[groups[group][qp]] = response.coeff(qp);
175 if (response_gradients.size() > 0) {
176 for (int i=0; i<dim; ++i)
177 response_gradients[groups[group][qp]][i] =
178 response_gradient[i].coeff(qp);
179 }
180 if (ensemble_it_size == VectorSize)
181 iterations[groups[group][qp]] = perf.ensemble_cg_iter_count[qp];
182 else
183 iterations[groups[group][qp]] = perf.cg_iter_count;
184 ensemble_iterations[groups[group][qp]] = perf.cg_iter_count;
185 }
186
187 if (cmd.PRINT_ITS && 0 == comm.getRank()) {
188 std::cout << group << " : " << perf.cg_iter_count << " [ ";
189 for (int qp=0; qp<VectorSize; ++qp)
190 std::cout << "(" << groups[group][qp] << ","
191 << iterations[groups[group][qp]] << ") ";
192 std::cout << "]";
193 std::cout << " ( ";
194 for (int i=0; i<dim; ++i)
195 std::cout << hrv(i) << " ";
196 std::cout << ")" << std::endl;
197 }
198
199 // Adjust timing statistics for ensemble size
200 perf.newton_iter_count *= VectorSize;
201 perf.cg_iter_count *= VectorSize;
202 perf.map_ratio *= VectorSize;
203 perf.fill_node_set *= VectorSize;
204 perf.scan_node_count *= VectorSize;
205 perf.fill_graph_entries *= VectorSize;
206 perf.sort_graph_entries *= VectorSize;
207 perf.fill_element_graph *= VectorSize;
208
209 // Increment timing statistics
210 perf_total.increment(perf, !cmd.USE_BELOS);
211
212 }
213 }
214
215 template< class ProblemType, class CoeffFunctionType >
216 void run_stokhos(
217 const Teuchos::Comm<int>& comm ,
218 ProblemType& problem ,
219 CoeffFunctionType & coeff_function,
220 const Teuchos::RCP<Kokkos::Example::FENL::SampleGrouping<double> >& grouper,
221 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
222 const CMD & cmd ,
223 const double bc_lower_value,
224 const double bc_upper_value,
225 Kokkos::Example::FENL::Perf& perf_total)
226 {
227 // Set up stochastic discretization
228 using Teuchos::Array;
229 using Teuchos::RCP;
230 using Teuchos::rcp;
231 using Teuchos::Ordinal;
232 typedef Stokhos::OneDOrthogPolyBasis<int,double> one_d_basis;
233 typedef Stokhos::LegendreBasis<int,double> legendre_basis;
234 typedef Stokhos::LexographicLess< Stokhos::MultiIndex<int> > order_type;
235 typedef Stokhos::TotalOrderBasis<int,double,order_type> product_basis;
236 typedef Stokhos::Quadrature<int,double> quadrature;
237 const int dim = cmd.USE_UQ_DIM ;
238 const int order = cmd.USE_UQ_ORDER ;
239 Array< RCP<const one_d_basis> > bases(dim);
240 for (int i=0; i<dim; i++)
241 bases[i] = rcp(new legendre_basis(order, true));
242 RCP<const product_basis> basis = rcp(new product_basis(bases));
243 RCP<const quadrature> quad;
244 int num_quad_points;
245 Array<double> quad_weights;
246 Array< Array<double> > quad_points;
247 Array< Array<double> > quad_values;
248 if ( cmd.USE_UQ_FAKE > 0 ) {
249 // Create fake UQ problem of size cmd.USE_UQ_FAKE, initializing
250 // points, weights, values to 0
251 num_quad_points = cmd.USE_UQ_FAKE;
252 quad_weights.resize(num_quad_points);
253 quad_points.resize(num_quad_points);
254 quad_values.resize(num_quad_points);
255 for (int i=0; i<num_quad_points; ++i) {
256 quad_points[i].resize(dim);
257 quad_values[i].resize(basis->size());
258 }
259 }
260 else {
261 if ( cmd.USE_SPARSE ) {
262 Stokhos::TotalOrderIndexSet<int> index_set(dim, order);
263 quad =
264 rcp(new Stokhos::SmolyakSparseGridQuadrature<int,double>(basis,
265 index_set));
266 }
267 else
268 quad = rcp(new Stokhos::TensorProductQuadrature<int,double>(basis));
269 num_quad_points = quad->size();
270 quad_weights = quad->getQuadWeights();
271 quad_points = quad->getQuadPoints();
272 quad_values = quad->getBasisAtQuadPoints();
273 }
274
275 perf_total.uq_count = num_quad_points;
276
277 typedef typename ProblemType::DeviceType Device;
278 typedef Stokhos::DynamicStorage<int,double,Device> PCEStorage;
279 typedef Sacado::UQ::PCE<PCEStorage> PCEScalar;
280 PCEScalar response_pce( typename PCEScalar::cijk_type(), basis->size() );
281
282 // Evaluate response at each quadrature point
283 Array<double> responses(num_quad_points);
284 Array< Array<double> > response_gradients; // Can't use gradients
285 Array<int> iterations(num_quad_points), ensemble_iterations(num_quad_points);
286 run_samples(comm, problem, coeff_function, grouper,
287 fenlParams, cmd,
288 bc_lower_value, bc_upper_value,
289 quad_points, responses, response_gradients,
290 iterations, ensemble_iterations,
291 perf_total);
292
293 // Integrate responses into PCE
294 for (int qp=0; qp<num_quad_points; ++qp) {
295 double r = responses[qp];
296 double w = quad_weights[qp];
297 for (int i=0; i<basis->size(); ++i)
298 response_pce.fastAccessCoeff(i) += r*w*quad_values[qp][i];
299 }
300
301 //std::cout << std::endl << response_pce << std::endl;
302
303 perf_total.response_mean = response_pce.mean();
304 perf_total.response_std_dev = response_pce.standard_deviation();
305
306 // Compute efficiency
307 double R_num = 0.0;
308 double R_denom = 0.0;
309 for (int i=0; i<num_quad_points; ++i) {
310 R_num += ensemble_iterations[i];
311 R_denom += iterations[i];
312 }
313 double R = R_num / R_denom;
314 if (cmd.PRINT_ITS && 0 == comm.getRank()) {
315 std::cout << "R_total = " << R << std::endl;
316 std::cout << "Total samples = " << perf_total.uq_count << std::endl;
317 std::cout << "Total solve time (s) = " << perf_total.cg_total_time
318 << std::endl;
319 std::cout << "Total prec setup time (s) = " << perf_total.prec_setup_time
320 << std::endl;
321 std::cout << "Total assembly time (s) = " << perf_total.fill_time + perf_total.bc_time
322 << std::endl;
323 std::cout << "Total newton time (s) = " << perf_total.newton_total_time
324 << std::endl;
325 std::cout << std::scientific;
326 std::cout.precision(12);
327 std::cout << "Computed mean = " << perf_total.response_mean << std::endl;
328 std::cout << "Computed std dev = " << perf_total.response_std_dev << std::endl;
329 }
330 }
331
332 #ifdef HAVE_TRILINOSCOUPLINGS_TASMANIAN
333 class TasmanianSurrogate {
334 public:
335 TasmanianSurrogate(const int min_level) : m_min_level(min_level) {}
336 void setTasmanian(const Teuchos::RCP<const TasGrid::TasmanianSparseGrid>& tas,
337 const int index) {
338 m_tas = tas;
339 m_index = index;
340 const int ny = m_tas->getNumOutputs();
341 m_y.resize(ny);
342 }
343 void setLevel(const int level) { m_level = level; }
344 int evaluate(const Teuchos::Array<double>& x) const {
345 if (m_level < m_min_level)
346 return 1;
347 m_tas->evaluate(x.getRawPtr(), m_y.getRawPtr());
348 return static_cast<int>(m_y[m_index]);
349 }
350
351 private:
352 Teuchos::RCP<const TasGrid::TasmanianSparseGrid> m_tas;
353 mutable Teuchos::Array<double> m_y;
354 int m_index;
355 int m_min_level;
356 int m_level;
357 };
358 #endif
359
360 template< class ProblemType, class CoeffFunctionType >
361 void run_tasmanian(
362 const Teuchos::Comm<int>& comm ,
363 ProblemType& problem ,
364 CoeffFunctionType & coeff_function,
365 const Teuchos::RCP<Kokkos::Example::FENL::SampleGrouping<double> >& grouper,
366 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
367 const CMD & cmd ,
368 const double bc_lower_value,
369 const double bc_upper_value,
370 Kokkos::Example::FENL::Perf& perf_total)
371 {
372 #ifdef HAVE_TRILINOSCOUPLINGS_TASMANIAN
373
374 using Teuchos::Array;
375
376 // Start up Tasmanian
377 TasGrid::TasmanianSparseGrid sparseGrid;
378
379 // Algorithmic parameters
380 const int dim = cmd.USE_UQ_DIM;
381 const int qoi = 3;
382 const int initial_level = cmd.USE_UQ_INIT_LEVEL;
383 const int max_level = cmd.USE_UQ_MAX_LEVEL;
384 const int max_order = 1;
385 const double tol = cmd.USE_UQ_TOL;
386 const TasGrid::TypeOneDRule rule = TasGrid::rule_localp;
387 const TasGrid::TypeRefinement refinement = TasGrid::refine_classic;
388 const int qoi_to_refine = 0;
389
390 // For studying efficiency of ensemble propagation
391 std::vector<double> R_level;
392 double R_total_num, R_total_denom, R_total;
393
394 // Create the initial grid
395 sparseGrid.makeLocalPolynomialGrid(dim, qoi, initial_level, max_order, rule);
396 int num_new_points = sparseGrid.getNumNeeded();
397
398 // Check for Tasmanian-based surrogate grouping
399 typedef Kokkos::Example::FENL::SurrogateGrouping<double,TasmanianSurrogate> TasGrouper;
400 Teuchos::RCP<TasGrouper> tas_grouper =
401 Teuchos::rcp_dynamic_cast<TasGrouper>(grouper);
402 if (tas_grouper != Teuchos::null)
403 tas_grouper->getSurrogate()->setTasmanian(Teuchos::rcpFromRef(sparseGrid),
404 2);
405
406 perf_total.uq_count = num_new_points;
407 int level = initial_level;
408 R_total_num = 0.0;
409 R_total_denom = 0.0;
410 bool reached_max_samples = false;
411 while (num_new_points > 0 && level <= max_level) {
412
413 // Set level in grouper
414 if (tas_grouper != Teuchos::null)
415 tas_grouper->getSurrogate()->setLevel(level);
416
417 if (cmd.PRINT_ITS && 0 == comm.getRank()) {
418 std::cout << "Tasmanian grid level " << level
419 << ", " << num_new_points << " points"
420 << std::endl;
421 }
422
423 // Get the sample points
424 const double *points = sparseGrid.getNeededPoints();
425
426 // Copy points into Teuchos arrays
427 Array< Array<double> > quad_points(num_new_points);
428 for (int i=0; i<num_new_points; ++i) {
429 quad_points[i].resize(dim);
430 for (int j=0; j<dim; ++j)
431 quad_points[i][j] = points[dim*i+j];
432 }
433
434 // Evaluate response on those points
435 Array<double> responses(num_new_points);
436 Array< Array<double> > response_gradients; // Can't use gradients
437 Array<int> iterations(num_new_points), ensemble_iterations(num_new_points);
438 run_samples(comm, problem, coeff_function, grouper,
439 fenlParams, cmd,
440 bc_lower_value, bc_upper_value,
441 quad_points, responses, response_gradients,
442 iterations, ensemble_iterations,
443 perf_total);
444
445 // Compute efficiency
446 double R_num = 0.0;
447 double R_denom = 0.0;
448 for (int i=0; i<num_new_points; ++i) {
449 R_num += ensemble_iterations[i];
450 R_denom += iterations[i];
451 }
452 R_level.push_back( R_num / R_denom );
453 R_total_num += R_num;
454 R_total_denom += R_denom;
455
456 // Load responses back into Tasmanian
457 Array<double> tas_responses(qoi*num_new_points);
458 for (int i=0; i<num_new_points; ++i) {
459 tas_responses[i*qoi] = responses[i]; // for mean
460 tas_responses[i*qoi+1] = responses[i]*responses[i]; // for variance
461 tas_responses[i*qoi+2] = iterations[i]; // solver iterations
462 }
463 sparseGrid.loadNeededPoints(&tas_responses[0]);
464
465 // Refine the grid
466 sparseGrid.setSurplusRefinement(tol, refinement, qoi_to_refine);
467
468 // Get the number of new points
469 num_new_points = sparseGrid.getNumNeeded();
470
471 ++level;
472
473 if (static_cast<int>(perf_total.uq_count) + num_new_points > cmd.USE_UQ_MAX_SAMPLES) {
474 reached_max_samples = true;
475 break;
476 }
477
478 // Don't add new points to the count if this is the last iteration
479 if (level <= max_level)
480 perf_total.uq_count += num_new_points;
481 }
482 R_total = R_total_num / R_total_denom;
483
484 if (((level > max_level) || reached_max_samples) && comm.getRank() == 0)
485 std::cout << "Warning: Tasmanian did not achieve refinement tolerance "
486 << tol << std::endl;
487
488 // Compute mean and standard deviation of response
489 double s[qoi];
490 sparseGrid.integrate(s);
491 const double weight = std::pow(0.5, dim); // uniform measure in dim dimensions
492 s[0] *= weight; s[1] *= weight;
493 perf_total.response_mean = s[0];
494 perf_total.response_std_dev = std::sqrt(s[1]-s[0]*s[0]);
495
496 if (cmd.PRINT_ITS && 0 == comm.getRank()) {
497 std::cout << "R_level = ";
498 for (int l=0; l<level-1; ++l)
499 std::cout << R_level[l] << " ";
500 std::cout << std::endl << "R_total = " << R_total << std::endl;
501 std::cout << "Total samples = " << perf_total.uq_count << std::endl;
502 std::cout << "Total solve time (s) = " << perf_total.cg_total_time
503 << std::endl;
504 std::cout << "Total prec setup time (s) = " << perf_total.prec_setup_time
505 << std::endl;
506 std::cout << "Total assembly time (s) = " << perf_total.fill_time + perf_total.bc_time
507 << std::endl;
508 std::cout << "Total newton time (s) = " << perf_total.newton_total_time
509 << std::endl;
510 std::cout << std::scientific;
511 std::cout.precision(12);
512 std::cout << "Computed mean = " << perf_total.response_mean << std::endl;
513 std::cout << "Computed std dev = " << perf_total.response_std_dev << std::endl;
514 }
515
516 #else
517
518 TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "TASMANIAN not available. Please re-configure with TASMANIAN TPL enabled.");
519
520 #endif
521 }
522
523 template< class ProblemType, class CoeffFunctionType >
524 void run_file(
525 const Teuchos::Comm<int>& comm ,
526 ProblemType& problem ,
527 CoeffFunctionType & coeff_function,
528 const Teuchos::RCP<Kokkos::Example::FENL::SampleGrouping<double> >& grouper,
529 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
530 const CMD & cmd ,
531 const double bc_lower_value,
532 const double bc_upper_value,
533 Kokkos::Example::FENL::Perf& perf_total)
534 {
535 using Teuchos::Array;
536
537 const int dim = cmd.USE_UQ_DIM;
538 int num_quad_points;
539 Array< Array<double > > quad_points;
540
541 // Open and read sample points
542 std::ifstream fin("samples.txt");
543 fin >> num_quad_points;
544 quad_points.resize(num_quad_points);
545 for (int i=0; i<num_quad_points; ++i) {
546 quad_points[i].resize(dim);
547 for (int j=0; j<dim; ++j)
548 fin >> quad_points[i][j];
549 }
550 fin.close();
551
552 // Evaluate response at each quadrature point
553 Array<double> responses(num_quad_points);
554 Array< Array<double> > response_gradients; // Can't use gradients yet
555 Array<int> iterations(num_quad_points), ensemble_iterations(num_quad_points);
556 run_samples(comm, problem, coeff_function, grouper,
557 fenlParams, cmd,
558 bc_lower_value, bc_upper_value,
559 quad_points, responses, response_gradients,
560 iterations, ensemble_iterations,
561 perf_total);
562
563 // Write responses to file, including solver iterations
564 if (comm.getRank() == 0) {
565 std::ofstream fout("responses.txt");
566 fout << num_quad_points << std::endl;
567 for (int i=0; i<num_quad_points; ++i) {
568 fout << responses[i] << " " << iterations[i] << std::endl;
569 }
570 fout.close();
571 }
572
573 perf_total.response_mean = 0.0;
574 perf_total.response_std_dev = 0.0;
575 }
576
577 template< class ProblemType, class CoeffFunctionType >
578 void run_vps(
579 const Teuchos::Comm<int>& comm ,
580 ProblemType& problem ,
581 CoeffFunctionType & coeff_function,
582 const Teuchos::RCP<Kokkos::Example::FENL::SampleGrouping<double> >& grouper,
583 const Teuchos::RCP<Teuchos::ParameterList>& fenlParams,
584 const CMD & cmd ,
585 const double bc_lower_value,
586 const double bc_upper_value,
587 Kokkos::Example::FENL::Perf& perf_total)
588 {
589 const unsigned ensemble_size =
590 cmd.USE_UQ_ENSEMBLE > 0 ? cmd.USE_UQ_ENSEMBLE : 1;
591 EnsembleVPS vps(cmd.USE_UQ_DIM, cmd.USE_UQ_MAX_SAMPLES, ensemble_size,
592 comm.getRank());
593 double mean, sd;
594 vps.run(
595 [&](const size_t num_samples, const size_t dim, const double*const* x,
596 double** f)
597 {
598 using Teuchos::Array;
599 Array<double> responses(num_samples);
600 Array< Array<double> > response_gradients; // Can't use gradients
601 Array<int> iterations(num_samples), ensemble_iterations(num_samples);
602 Array< Array<double> > points(num_samples);
603 for (size_t iSample = 0; iSample < num_samples; iSample++) {
604 points[iSample].resize(dim);
605 for (size_t idim = 0; idim < dim; idim++)
606 points[iSample][idim] = x[iSample][idim];
607 }
608 run_samples(comm, problem, coeff_function, grouper,
609 fenlParams, cmd,
610 bc_lower_value, bc_upper_value,
611 points, responses, response_gradients,
612 iterations, ensemble_iterations,
613 perf_total);
614 for (size_t iSample = 0; iSample < num_samples; iSample++) {
615 f[iSample][0] = responses[iSample];
616 f[iSample][1] = iterations[iSample];
617 }
618 perf_total.uq_count += num_samples;
619 }, mean, sd);
620
621 perf_total.response_mean = mean;
622 perf_total.response_std_dev = sd;
623
624 if (cmd.PRINT_ITS && 0 == comm.getRank()) {
625 std::cout << "Total samples = " << perf_total.uq_count << std::endl;
626 std::cout << "Total solve time (s) = " << perf_total.cg_total_time
627 << std::endl;
628 std::cout << "Total prec setup time (s) = " << perf_total.prec_setup_time
629 << std::endl;
630 std::cout << "Total assembly time (s) = "
631 << perf_total.fill_time + perf_total.bc_time
632 << std::endl;
633 std::cout << "Total newton time (s) = " << perf_total.newton_total_time
634 << std::endl;
635 std::cout.precision(12);
636 std::cout << "Computed mean = " << perf_total.response_mean << std::endl;
637 std::cout << "Computed std dev = " << perf_total.response_std_dev << std::endl;
638 }
639 }
640
641 template< class Device , int VectorSize >
642 bool run( const Teuchos::RCP<const Teuchos::Comm<int> > & comm ,
643 const CMD & cmd)
644 {
645 using Teuchos::RCP;
646 using Teuchos::rcp;
647
648 bool success = true;
649 try {
650
651 const int comm_rank = comm->getRank();
652
653 // Print output headers
654 const std::vector< size_t > widths =
655 print_headers( std::cout , cmd , comm_rank );
656
657 using Kokkos::Example::FENL::ElementComputationKLCoefficient;
658 using Kokkos::Example::FENL::ExponentialKLCoefficient;
659 using Kokkos::Example::FENL::Problem;
660 using Kokkos::Example::BoxElemPart;
661 using Kokkos::Example::FENL::fenl;
662 using Kokkos::Example::FENL::Perf;
663
664 const double bc_lower_value = 1 ;
665 const double bc_upper_value = 2 ;
666 const double geom_bubble[3] = { 1.0 , 1.0 , 1.0 };
667
668 const double kl_mean = cmd.USE_MEAN;
669 const double kl_variance = cmd.USE_VAR;
670 const double kl_correlation = cmd.USE_COR;
671 const int kl_dim = cmd.USE_UQ_DIM ;
672 const bool kl_exp = cmd.USE_EXPONENTIAL;
673 const double kl_exp_shift = cmd.USE_EXP_SHIFT;
674 const double kl_exp_scale = cmd.USE_EXP_SCALE;
675 const bool kl_disc_exp_scale = cmd.USE_DISC_EXP_SCALE;
676
677 int nelem[3] = { cmd.USE_FIXTURE_X,
678 cmd.USE_FIXTURE_Y,
679 cmd.USE_FIXTURE_Z};
680 Perf perf_total;
681
682 // Read in any params from xml file
683 Teuchos::RCP<Teuchos::ParameterList> fenlParams = Teuchos::parameterList();
684 Teuchos::updateParametersFromXmlFileAndBroadcast(
685 cmd.USE_FENL_XML_FILE, fenlParams.ptr(), *comm);
686
687 // Number of sensitivities -- currently only useful for Dakota
688 unsigned num_sens = 0;
689
690 // Compute PCE of response propagating blocks of quadrature
691 // points at a time
692 if ( cmd.USE_UQ_ENSEMBLE > 0 ) {
693
694 typedef Stokhos::StaticFixedStorage<int,double,VectorSize,Device> Storage;
695 typedef Sacado::MP::Vector<Storage> Scalar;
696
697 // Set global vector size -- this is mandatory
698 Kokkos::global_sacado_mp_vector_size = VectorSize;
699
700 typedef ExponentialKLCoefficient< Scalar, double, Device > KL;
701 KL diffusion_coefficient( kl_mean, kl_variance, kl_correlation, kl_dim,
702 kl_exp, kl_exp_shift, kl_exp_scale,
703 kl_disc_exp_scale );
704
705 // Problem setup
706 typedef Problem< Scalar, Device , BoxElemPart::ElemLinear > ProblemType;
707 ProblemType problem( comm , nelem , geom_bubble , cmd.PRINT ,
708 num_sens );
709
710 // Grouping method
711 RCP< Kokkos::Example::FENL::SampleGrouping<double> > grouper;
712 if (cmd.USE_GROUPING == GROUPING_NATURAL)
713 grouper = rcp(new Kokkos::Example::FENL::NaturalGrouping<double>);
714 else if (cmd.USE_GROUPING == GROUPING_MAX_ANISOTROPY) {
715 typedef ExponentialKLCoefficient< double, double, Device > DKL;
716 DKL diff_coeff( kl_mean, kl_variance, kl_correlation, kl_dim,
717 kl_exp, kl_exp_shift, kl_exp_scale, kl_disc_exp_scale );
718 typedef typename ProblemType::FixtureType Mesh;
719 grouper =
720 rcp(new Kokkos::Example::FENL::MaxAnisotropyGrouping<double,Mesh,DKL>(
721 comm, problem.fixture, diff_coeff));
722 }
723 #ifdef HAVE_TRILINOSCOUPLINGS_TASMANIAN
724 else if (cmd.USE_GROUPING == GROUPING_TASMANIAN_SURROGATE) {
725 const int min_level = cmd.TAS_GROUPING_INITIAL_LEVEL;
726 RCP<TasmanianSurrogate> s = rcp(new TasmanianSurrogate(min_level));
727 grouper =
728 rcp(new Kokkos::Example::FENL::SurrogateGrouping<double,TasmanianSurrogate>(s, cmd.PRINT_ITS, comm->getRank()));
729 }
730 #endif
731
732 if (cmd.USE_UQ_SAMPLING == SAMPLING_STOKHOS)
733 run_stokhos(*comm, problem, diffusion_coefficient, grouper,
734 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
735 else if (cmd.USE_UQ_SAMPLING == SAMPLING_TASMANIAN)
736 run_tasmanian(*comm, problem, diffusion_coefficient, grouper,
737 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
738 else if (cmd.USE_UQ_SAMPLING == SAMPLING_FILE)
739 run_file(*comm, problem, diffusion_coefficient, grouper,
740 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
741 else if (cmd.USE_UQ_SAMPLING == SAMPLING_VPS)
742 run_vps(*comm, problem, diffusion_coefficient, grouper,
743 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
744
745 }
746
747 // Compute PCE of response propagating one quadrature point at a time
748 else {
749
750 typedef double Scalar;
751 typedef ExponentialKLCoefficient< Scalar, double, Device > KL;
752 KL diffusion_coefficient( kl_mean, kl_variance, kl_correlation, kl_dim,
753 kl_exp, kl_exp_shift, kl_exp_scale,
754 kl_disc_exp_scale);
755
756 // Problem setup
757 Problem< Scalar, Device , BoxElemPart::ElemLinear > problem(
758 comm , nelem , geom_bubble , cmd.PRINT , num_sens );
759
760 if (cmd.USE_UQ_SAMPLING == SAMPLING_STOKHOS)
761 run_stokhos(*comm, problem, diffusion_coefficient, Teuchos::null,
762 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
763 else if (cmd.USE_UQ_SAMPLING == SAMPLING_TASMANIAN)
764 run_tasmanian(*comm, problem, diffusion_coefficient, Teuchos::null,
765 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
766 else if (cmd.USE_UQ_SAMPLING == SAMPLING_FILE)
767 run_file(*comm, problem, diffusion_coefficient, Teuchos::null,
768 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
769 else if (cmd.USE_UQ_SAMPLING == SAMPLING_VPS)
770 run_vps(*comm, problem, diffusion_coefficient, Teuchos::null,
771 fenlParams, cmd, bc_lower_value, bc_upper_value, perf_total);
772
773 }
774
775 if ( 0 == comm_rank ) {
776 print_perf_value( std::cout , cmd , widths , perf_total );
777 }
778
779 if ( cmd.SUMMARIZE ) {
780 Teuchos::TimeMonitor::report (comm.ptr (), std::cout);
781 print_memory_usage(std::cout, *comm);
782 }
783
784 // If we are running as a unit-test, check mean and variance
785 if (cmd.UNIT_TEST) {
786 TEUCHOS_TEST_FLOATING_EQUALITY( perf_total.response_mean, cmd.TEST_MEAN, cmd.TEST_TOL, std::cout, success );
787 TEUCHOS_TEST_FLOATING_EQUALITY( perf_total.response_std_dev, cmd.TEST_STD_DEV, cmd.TEST_TOL, std::cout, success );
788 if (success)
789 std::cout << "Test Passed!" << std::endl;
790 else
791 std::cout << "Test Failed!" << std::endl;
792 }
793
794 }
795 TEUCHOS_STANDARD_CATCH_STATEMENTS(true, std::cerr, success);
796
797 return success;
798 }
799
800 template <typename ExecSpace>
801 struct RunOp {
802 public:
803 const CMD& cmdline;
804 Teuchos::RCP<const Teuchos::Comm<int> > comm;
805 mutable bool ran;
806
807 RunOp(const CMD& cmdline_,
808 const Teuchos::RCP<const Teuchos::Comm<int> >& comm_) :
809 cmdline(cmdline_), comm(comm_), ran(false) {}
810
811 template <typename EnsembleSizeType>
812 void operator()(const EnsembleSizeType arg) const {
813 if (EnsembleSizeType::value == cmdline.USE_UQ_ENSEMBLE) {
814 run<ExecSpace,EnsembleSizeType::value>( comm , cmdline );
815 ran = true;
816 }
817 }
818 };
819
820 template <typename ExecSpace>
821 void driver(const CMD& cmdline,
822 const Teuchos::RCP<const Teuchos::Comm<int> >& comm)
823 {
824 if ( cmdline.USE_UQ_ENSEMBLE == 0 ) {
825 // Run without ensembles (one at a time)
826 run<ExecSpace, STOKHOS_DEFAULT_ENSEMBLE_SIZE>(comm, cmdline);
827 }
828 else {
829 // Run with requested ensemble size, if possible
830 // Search over all enabled ensemble sizes and run it if it matches the
831 // requested one
832 RunOp<ExecSpace> run_op(cmdline,comm);
833 Sacado::mpl::for_each< Stokhos::ETI_Ensemble_Sizes > f(run_op);
834 if (!run_op.ran)
835 std::cout << "Invalid ensemble size!" << std::endl;
836 }
837 }
838
839 //----------------------------------------------------------------------------
840 //----------------------------------------------------------------------------
841
842 int main( int argc , char ** argv )
843 {
844 Teuchos::oblackholestream blackHole;
845 Teuchos::GlobalMPISession mpiSession (&argc, &argv, &blackHole);
846
847 Teuchos::RCP<const Teuchos::Comm<int> > comm =
848 Teuchos::DefaultComm<int>::getComm();
849
850 //--------------------------------------------------------------------------
851 CMD cmdline;
852 clp_return_type rv = parse_cmdline( argc, argv, cmdline, *comm, true );
853 if (rv==CLP_HELP)
854 return(EXIT_SUCCESS);
855 else if (rv==CLP_ERROR)
856 return(EXIT_FAILURE);
857
858 {
859 Kokkos::initialize(argc, argv);
860
861 if ( cmdline.VTUNE ) {
862 connect_vtune(comm->getRank());
863 }
864
865 if ( ! cmdline.ERROR && ! cmdline.ECHO ) {
866
867 // If execution space not specified, use the default
868 if (!cmdline.USE_SERIAL && !cmdline.USE_THREADS && !cmdline.USE_OPENMP &&
869 !cmdline.USE_CUDA)
870 driver<Kokkos::DefaultExecutionSpace>(cmdline, comm);
871
872 #if defined( HAVE_TPETRA_SERIAL )
873 if ( cmdline.USE_SERIAL )
874 driver<Kokkos::Serial>(cmdline, comm);
875 #endif
876
877 #if defined( HAVE_TPETRA_PTHREAD )
878 if ( cmdline.USE_THREADS )
879 driver<Kokkos::Threads>(cmdline, comm);
880 #endif
881
882 #if defined( HAVE_TPETRA_OPENMP )
883 if ( cmdline.USE_OPENMP )
884 driver<Kokkos::OpenMP>(cmdline, comm);
885 #endif
886
887 #if defined( HAVE_TPETRA_CUDA )
888 if ( cmdline.USE_CUDA )
889 driver<Kokkos::Cuda>(cmdline, comm);
890 #endif
891
892 }
893
894 }
895 Kokkos::finalize();
896
897 //--------------------------------------------------------------------------
898
899 return cmdline.ERROR ? -1 : 0 ;
900 }