| 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 "Kokkos_View_MP_Vector_Fwd.hpp" |
| 11 |
#include "Kokkos_View_UQ_PCE_Fwd.hpp" |
| 12 |
#include "Stokhos_Sacado_Kokkos_UQ_PCE.hpp" |
| 13 |
#include "Stokhos_Sacado_Kokkos_MP_Vector.hpp" |
| 14 |
#include "Stokhos.hpp" |
| 15 |
|
| 16 |
//---------------------------------------------------------------------------- |
| 17 |
|
| 18 |
#include <Kokkos_Core.hpp> |
| 19 |
|
| 20 |
#include <fenl_pce.hpp> |
| 21 |
#include <fenl_utils.hpp> |
| 22 |
|
| 23 |
//---------------------------------------------------------------------------- |
| 24 |
|
| 25 |
#include <Tpetra_Version.hpp> |
| 26 |
#include <Teuchos_Comm.hpp> |
| 27 |
#include <Teuchos_DefaultComm.hpp> |
| 28 |
#include <Teuchos_GlobalMPISession.hpp> |
| 29 |
#include <Teuchos_oblackholestream.hpp> |
| 30 |
#include <Teuchos_StandardCatchMacros.hpp> |
| 31 |
#include <Tpetra_MultiVector.hpp> |
| 32 |
|
| 33 |
//---------------------------------------------------------------------------- |
| 34 |
|
| 35 |
// For unit-testing |
| 36 |
#include "Teuchos_TestingHelpers.hpp" |
| 37 |
|
| 38 |
template< class Device > |
| 39 |
bool run( const Teuchos::RCP<const Teuchos::Comm<int> > & comm , |
| 40 |
const CMD & cmd) |
| 41 |
{ |
| 42 |
bool success = true; |
| 43 |
try { |
| 44 |
|
| 45 |
const int comm_rank = comm->getRank(); |
| 46 |
|
| 47 |
// Set up stochastic discretization |
| 48 |
using Teuchos::Array; |
| 49 |
using Teuchos::RCP; |
| 50 |
using Teuchos::rcp; |
| 51 |
using one_d_basis = Stokhos::OneDOrthogPolyBasis<int,double>; |
| 52 |
using legendre_basis = Stokhos::LegendreBasis<int,double>; |
| 53 |
using order_type = Stokhos::LexographicLess< Stokhos::MultiIndex<int> >; |
| 54 |
using product_basis = Stokhos::TotalOrderBasis<int,double,order_type>; |
| 55 |
using Cijk = Stokhos::Sparse3Tensor<int,double>; |
| 56 |
using quadrature = Stokhos::Quadrature<int,double>; |
| 57 |
const int dim = cmd.USE_UQ_DIM; |
| 58 |
const int order = cmd.USE_UQ_ORDER ; |
| 59 |
Array< RCP<const one_d_basis> > bases(dim); |
| 60 |
for (int i=0; i<dim; i++) |
| 61 |
bases[i] = rcp(new legendre_basis(order, true)); |
| 62 |
RCP<const product_basis> basis = rcp(new product_basis(bases)); |
| 63 |
RCP<Cijk> cijk = basis->computeTripleProductTensor(); |
| 64 |
|
| 65 |
using Storage = Stokhos::DynamicStorage<int,double,Device>; |
| 66 |
using Scalar = Sacado::UQ::PCE<Storage>; |
| 67 |
typename Scalar::cijk_type kokkos_cijk = |
| 68 |
Stokhos::create_product_tensor<Device>(*basis, *cijk); |
| 69 |
Kokkos::setGlobalCijkTensor(kokkos_cijk); |
| 70 |
|
| 71 |
// Create quadrature data used by assembly |
| 72 |
RCP<const quadrature> quad; |
| 73 |
if ( cmd.USE_SPARSE ) { |
| 74 |
Stokhos::TotalOrderIndexSet<int> index_set(dim, order); |
| 75 |
quad = |
| 76 |
rcp(new Stokhos::SmolyakSparseGridQuadrature<int,double>(basis, |
| 77 |
index_set)); |
| 78 |
} |
| 79 |
else |
| 80 |
quad = rcp(new Stokhos::TensorProductQuadrature<int,double>(basis)); |
| 81 |
const int num_pce = basis->size(); |
| 82 |
const int num_quad_points = quad->size(); |
| 83 |
const Array<double>& quad_weights = quad->getQuadWeights(); |
| 84 |
const Array< Array<double> >& quad_points = quad->getQuadPoints(); |
| 85 |
const Array< Array<double> >& quad_values = quad->getBasisAtQuadPoints(); |
| 86 |
|
| 87 |
// Align number of quadrature points to ensemble size used in assembly |
| 88 |
const int align = 32; |
| 89 |
const int mask = align-1; |
| 90 |
const int num_quad_points_aligned = (num_quad_points + mask) & ~mask; |
| 91 |
|
| 92 |
// Copy quadrature data to view's for assembly kernels |
| 93 |
using QD = Kokkos::Example::FENL::QuadratureData<Device>; |
| 94 |
using quad_weights_type = typename QD::quad_weights_type; |
| 95 |
using quad_values_type = typename QD::quad_values_type; |
| 96 |
QD qd; |
| 97 |
qd.weights_view = |
| 98 |
quad_weights_type( "quad weights", num_quad_points_aligned ); |
| 99 |
qd.points_view = |
| 100 |
quad_values_type( "quad points", num_quad_points_aligned, dim ); |
| 101 |
qd.values_view = |
| 102 |
quad_values_type( "quad values", num_quad_points_aligned, num_pce ); |
| 103 |
typename quad_weights_type::HostMirror host_weights_view = |
| 104 |
Kokkos::create_mirror_view( qd.weights_view ); |
| 105 |
typename quad_values_type::HostMirror host_points_view = |
| 106 |
Kokkos::create_mirror_view( qd.points_view ); |
| 107 |
typename quad_values_type::HostMirror host_values_view = |
| 108 |
Kokkos::create_mirror_view( qd.values_view ); |
| 109 |
for (int qp=0; qp<num_quad_points; ++qp) { |
| 110 |
host_weights_view(qp) = quad_weights[qp]; |
| 111 |
for (int i=0; i<dim; ++i) |
| 112 |
host_points_view(qp,i) = quad_points[qp][i]; |
| 113 |
for (int i=0; i<num_pce; ++i) |
| 114 |
host_values_view(qp,i) = quad_values[qp][i]; |
| 115 |
} |
| 116 |
for (int qp=num_quad_points; qp<num_quad_points_aligned; ++qp) { |
| 117 |
host_weights_view(qp) = 0.0; |
| 118 |
for (int i=0; i<dim; ++i) |
| 119 |
host_points_view(qp,i) = quad_points[num_quad_points-1][i]; |
| 120 |
for (int i=0; i<num_pce; ++i) |
| 121 |
host_values_view(qp,i) = quad_values[num_quad_points-1][i]; |
| 122 |
} |
| 123 |
Kokkos::deep_copy( qd.weights_view, host_weights_view ); |
| 124 |
Kokkos::deep_copy( qd.points_view, host_points_view ); |
| 125 |
Kokkos::deep_copy( qd.values_view, host_values_view ); |
| 126 |
|
| 127 |
// Print output headers |
| 128 |
const std::vector< size_t > widths = |
| 129 |
print_headers( std::cout , cmd , comm_rank ); |
| 130 |
|
| 131 |
using Kokkos::Example::FENL::ElementComputationKLCoefficient; |
| 132 |
using Kokkos::Example::FENL::ExponentialKLCoefficient; |
| 133 |
using Kokkos::Example::BoxElemPart; |
| 134 |
using Kokkos::Example::FENL::fenl; |
| 135 |
using Kokkos::Example::FENL::Perf; |
| 136 |
|
| 137 |
const double bc_lower_value = 1 ; |
| 138 |
const double bc_upper_value = 2 ; |
| 139 |
|
| 140 |
int nelem[3] = { cmd.USE_FIXTURE_X , |
| 141 |
cmd.USE_FIXTURE_Y , |
| 142 |
cmd.USE_FIXTURE_Z }; |
| 143 |
|
| 144 |
// Create KL diffusion coefficient |
| 145 |
const double kl_mean = cmd.USE_MEAN; |
| 146 |
const double kl_variance = cmd.USE_VAR; |
| 147 |
const double kl_correlation = cmd.USE_COR; |
| 148 |
const bool kl_exp = cmd.USE_EXPONENTIAL; |
| 149 |
const double kl_exp_shift = cmd.USE_EXP_SHIFT; |
| 150 |
const double kl_exp_scale = cmd.USE_EXP_SCALE; |
| 151 |
const bool kl_disc_exp_scale = cmd.USE_DISC_EXP_SCALE; |
| 152 |
//typedef ElementComputationKLCoefficient< Scalar, double, Device > KL; |
| 153 |
typedef ExponentialKLCoefficient< Scalar, double, Device > KL; |
| 154 |
KL diffusion_coefficient( kl_mean, kl_variance, kl_correlation, dim, |
| 155 |
kl_exp, kl_exp_shift, kl_exp_scale, |
| 156 |
kl_disc_exp_scale ); |
| 157 |
typedef typename KL::RandomVariableView RV; |
| 158 |
typedef typename RV::HostMirror HRV; |
| 159 |
RV rv("KL Random Variables", dim); |
| 160 |
HRV hrv = Kokkos::create_mirror_view(rv); |
| 161 |
|
| 162 |
// Set random variables |
| 163 |
// ith random variable \xi_i = \psi_I(\xi) / \psi_I(1.0) |
| 164 |
// where I is determined by the basis ordering (since the component basis |
| 165 |
// functions have unit two-norm, \psi_I(1.0) might not be 1.0). We compute |
| 166 |
// this by finding the index of the multivariate term that is first order in |
| 167 |
// the ith slot, all other orders 0 |
| 168 |
Teuchos::Array<double> point(dim, 1.0); |
| 169 |
Teuchos::Array<double> basis_vals(num_pce); |
| 170 |
basis->evaluateBases(point, basis_vals); |
| 171 |
for (int i=0; i<dim; ++i) { |
| 172 |
Stokhos::MultiIndex<int> term(dim, 0); |
| 173 |
term[i] = 1; |
| 174 |
int index = basis->index(term); |
| 175 |
hrv(i).fastAccessCoeff(index) = 1.0 / basis_vals[index]; |
| 176 |
} |
| 177 |
Kokkos::deep_copy( rv, hrv ); |
| 178 |
diffusion_coefficient.setRandomVariables(rv); |
| 179 |
|
| 180 |
// Compute stochastic response using stochastic Galerkin method |
| 181 |
Scalar response = 0; |
| 182 |
Teuchos::Array<Scalar> response_gradient; |
| 183 |
Perf perf; |
| 184 |
if ( cmd.USE_FIXTURE_QUADRATIC ) |
| 185 |
perf = fenl< Scalar , Device , BoxElemPart::ElemQuadratic > |
| 186 |
( comm , cmd.USE_FENL_XML_FILE , |
| 187 |
cmd.PRINT , cmd.USE_TRIALS , |
| 188 |
cmd.USE_ATOMIC , cmd.USE_BELOS , cmd.USE_MUELU , |
| 189 |
cmd.USE_MEANBASED , |
| 190 |
nelem , diffusion_coefficient , cmd.USE_ISOTROPIC , cmd.USE_COEFF_SRC , |
| 191 |
cmd.USE_COEFF_ADV , bc_lower_value , bc_upper_value , |
| 192 |
response, response_gradient, qd ); |
| 193 |
else |
| 194 |
perf = fenl< Scalar , Device , BoxElemPart::ElemLinear > |
| 195 |
( comm , cmd.USE_FENL_XML_FILE , |
| 196 |
cmd.PRINT , cmd.USE_TRIALS , |
| 197 |
cmd.USE_ATOMIC , cmd.USE_BELOS , cmd.USE_MUELU , |
| 198 |
cmd.USE_MEANBASED , |
| 199 |
nelem , diffusion_coefficient , cmd.USE_ISOTROPIC , cmd.USE_COEFF_SRC , |
| 200 |
cmd.USE_COEFF_ADV , bc_lower_value , bc_upper_value , |
| 201 |
response , response_gradient, qd ); |
| 202 |
|
| 203 |
// std::cout << "newton count = " << perf.newton_iter_count |
| 204 |
// << " cg count = " << perf.cg_iter_count << std::endl; |
| 205 |
perf.uq_count = num_quad_points; |
| 206 |
perf.newton_iter_count *= num_quad_points; |
| 207 |
perf.cg_iter_count *= num_pce; |
| 208 |
perf.map_ratio *= num_pce; |
| 209 |
perf.fill_node_set *= num_pce; |
| 210 |
perf.scan_node_count *= num_pce; |
| 211 |
perf.fill_graph_entries *= num_pce; |
| 212 |
perf.sort_graph_entries *= num_pce; |
| 213 |
perf.fill_element_graph *= num_pce; |
| 214 |
|
| 215 |
// Compute response mean, variance |
| 216 |
perf.response_mean = response.mean(); |
| 217 |
perf.response_std_dev = response.standard_deviation(); |
| 218 |
|
| 219 |
//std::cout << std::endl << response << std::endl; |
| 220 |
|
| 221 |
Kokkos::setGlobalCijkTensor(typename Scalar::cijk_type()); |
| 222 |
|
| 223 |
if ( 0 == comm_rank ) { |
| 224 |
print_perf_value( std::cout , cmd , widths , perf ); |
| 225 |
} |
| 226 |
|
| 227 |
if ( cmd.SUMMARIZE ) { |
| 228 |
Teuchos::TimeMonitor::report (comm.ptr (), std::cout); |
| 229 |
print_memory_usage(std::cout, *comm); |
| 230 |
} |
| 231 |
|
| 232 |
// If we are running as a unit-test, check mean and variance |
| 233 |
if (cmd.UNIT_TEST) { |
| 234 |
TEUCHOS_TEST_FLOATING_EQUALITY( perf.response_mean, cmd.TEST_MEAN, cmd.TEST_TOL, std::cout, success ); |
| 235 |
TEUCHOS_TEST_FLOATING_EQUALITY( perf.response_std_dev, cmd.TEST_STD_DEV, cmd.TEST_TOL, std::cout, success ); |
| 236 |
if (success) |
| 237 |
std::cout << "Test Passed!" << std::endl; |
| 238 |
else |
| 239 |
std::cout << "Test Failed!" << std::endl; |
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
TEUCHOS_STANDARD_CATCH_STATEMENTS(true, std::cerr, success); |
| 244 |
|
| 245 |
return success; |
| 246 |
} |
| 247 |
|
| 248 |
//---------------------------------------------------------------------------- |
| 249 |
//---------------------------------------------------------------------------- |
| 250 |
|
| 251 |
int main( int argc , char ** argv ) |
| 252 |
{ |
| 253 |
Teuchos::oblackholestream blackHole; |
| 254 |
Teuchos::GlobalMPISession mpiSession (&argc, &argv, &blackHole); |
| 255 |
|
| 256 |
Teuchos::RCP<const Teuchos::Comm<int> > comm = |
| 257 |
Teuchos::DefaultComm<int>::getComm(); |
| 258 |
|
| 259 |
//-------------------------------------------------------------------------- |
| 260 |
CMD cmdline; |
| 261 |
clp_return_type rv = parse_cmdline( argc, argv, cmdline, *comm, true ); |
| 262 |
|
| 263 |
{ |
| 264 |
Kokkos::initialize(argc, argv); |
| 265 |
|
| 266 |
// Print a warning if we are using the non-mean-based preconditioner |
| 267 |
if (cmdline.USE_MUELU && !cmdline.USE_MEANBASED && |
| 268 |
comm->getRank() == 0) { |
| 269 |
std::cout << "Warning: The non-mean-based preconditioner for PCE is " |
| 270 |
<< "not correctly applying the inverse diagonal in the smoother." |
| 271 |
<< std::endl; |
| 272 |
} |
| 273 |
#ifndef HAVE_TPETRA_EXPLICIT_INSTANTIATION |
| 274 |
if (cmdline.USE_MUELU && cmdline.USE_MEANBASED && |
| 275 |
comm->getRank() == 0) { |
| 276 |
std::cout << "Warning: The mean-based preconditioner for PCE requires " |
| 277 |
<< "specializations introduced through ETI, however it is not " |
| 278 |
<< "enabled. Memory errors are likely!" |
| 279 |
<< std::endl; |
| 280 |
} |
| 281 |
#endif |
| 282 |
if (rv==CLP_HELP) |
| 283 |
return(EXIT_SUCCESS); |
| 284 |
else if (rv==CLP_ERROR) |
| 285 |
return(EXIT_FAILURE); |
| 286 |
|
| 287 |
if ( cmdline.VTUNE ) { |
| 288 |
connect_vtune(comm->getRank()); |
| 289 |
} |
| 290 |
|
| 291 |
if ( ! cmdline.ERROR && ! cmdline.ECHO ) { |
| 292 |
|
| 293 |
// If execution space not specified, use the default |
| 294 |
if (!cmdline.USE_SERIAL && !cmdline.USE_THREADS && !cmdline.USE_OPENMP && |
| 295 |
!cmdline.USE_CUDA) |
| 296 |
run< Kokkos::DefaultExecutionSpace >( comm , cmdline ); |
| 297 |
|
| 298 |
#if defined( HAVE_TPETRA_SERIAL ) |
| 299 |
if ( cmdline.USE_SERIAL ) { |
| 300 |
run< Kokkos::Serial >( comm , cmdline ); |
| 301 |
} |
| 302 |
#endif |
| 303 |
|
| 304 |
#if defined( HAVE_TPETRA_PTHREAD ) |
| 305 |
if ( cmdline.USE_THREADS ) { |
| 306 |
run< Kokkos::Threads >( comm , cmdline ); |
| 307 |
} |
| 308 |
#endif |
| 309 |
|
| 310 |
#if defined( HAVE_TPETRA_OPENMP ) |
| 311 |
if ( cmdline.USE_OPENMP ) { |
| 312 |
run< Kokkos::OpenMP >( comm , cmdline ); |
| 313 |
} |
| 314 |
#endif |
| 315 |
|
| 316 |
#if defined( HAVE_TPETRA_CUDA ) |
| 317 |
if ( cmdline.USE_CUDA ) { |
| 318 |
run< Kokkos::Cuda >( comm , cmdline ); |
| 319 |
} |
| 320 |
#endif |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
} |
| 325 |
Kokkos::finalize(); |
| 326 |
|
| 327 |
//-------------------------------------------------------------------------- |
| 328 |
|
| 329 |
return cmdline.ERROR ? -1 : 0 ; |
| 330 |
} |