1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
|
/* -----------------------------------------------------------------------------
* Programmer(s): David J. Gardner and Cody J. Balos @ LLNL
* -----------------------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2022, Lawrence Livermore National Security
* and Southern Methodist University.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
* -----------------------------------------------------------------------------
* The following is a simple example problem based off of ark_brusselator.c.
*
* We simulate a scenario where a set of independent ODEs are batched together
* to form a larger system. Each independent ODE system has 3 components,
* Y = [u, v, w], satisfying the equations,
*
* du/dt = a - (w + 1) * u + v * u^2
* dv/dt = w * u - v * u^2
* dw/dt = (b - w) / ep - w * u
*
* for t in the interval [0, 10], with initial conditions Y0 = [u0, v0, w0].
* The problem is stiff and there are 3 testing scenarios:
*
* Reactor 0: u0 = 3.9, v0 = 1.1, w0 = 2.8, a = 1.2, b = 2.5, ep = 1.0e-5
* Here, all three components exhibit a rapid transient change during the
* first 0.2 time units, followed by a slow and smooth evolution.
*
* Reactor 1: u0 = 3, v0 = 3, w0 = 3.5, a = 0.5, b = 3, ep = 5.0e-4
* Here, all components undergo very rapid initial transients during the first
* 0.3 time units, and all then proceed very smoothly for the remainder of the
* simulation.
*
* Reactor 2: u0 = 1.2, v0 = 3.1, w0 = 3, a = 1, b = 3.5, ep = 5.0e-6
* Here, w experiences a fast initial transient, jumping 0.5 within a few
* steps. All values proceed smoothly until around t=6.5, when both u and v
* undergo a sharp transition, with u increasing from around 0.5 to 5 and v
* decreasing from around 6 to 1 in less than 0.5 time units. After this
* transition, both u and v continue to evolve somewhat rapidly for another
* 1.4 time units, and finish off smoothly.
*
* This program solves the problem with the BDF method, Newton iteration, a
* user-supplied Jacobian routine, and, since the grouping of the independent
* systems results in a block diagonal linear system, the dense KOKKOS
* SUNLinearSolver which supports batched systems. 100 outputs are printed at
* equal intervals, and run statistics are printed at the end.
*
* The program takes three optional arguments, the number of independent ODE
* systems (i.e., number of batches), the linear solver type (KOKKOS batched LU
* or non-batched GMRES with the Jacobian computed by difference quotients)
* the test type (uniform_0, uniform_1, or uniform_2).
*
* ./cv_bruss_batched_kokkos [num_batches] [solver_type] [test_type]
*
* Options:
* num_batches <int>
* solver_type:
* 0 - KOKKOS batched LU (default)
* 1 - SUNDIALS non-batched GMRES with difference quotients Jacobian
* test_type:
* 0 - uniform_0, all batches are Reactor 0
* 1 - uniform 1, all batches are Reactor 1
* 2 - uniform 2, all batches are Reactor 2 (default)
* ---------------------------------------------------------------------------*/
#include <cstdio>
#include <cvode/cvode.h>
#include <memory>
#include <nvector/nvector_kokkos.hpp>
#include <sunlinsol/sunlinsol_kokkosdense.hpp>
#include <sunlinsol/sunlinsol_spgmr.h>
#include <sunmatrix/sunmatrix_kokkosdense.hpp>
#include <vector>
// Common utility functions
#include <example_utilities.hpp>
// Execution space
#if defined(USE_CUDA)
using ExecSpace = Kokkos::Cuda;
#elif defined(USE_HIP)
#if KOKKOS_VERSION / 10000 > 3
using ExecSpace = Kokkos::HIP;
#else
using ExecSpace = Kokkos::Experimental::HIP;
#endif
#elif defined(USE_OPENMP)
using ExecSpace = Kokkos::OpenMP;
#else
using ExecSpace = Kokkos::Serial;
#endif
using VecType = sundials::kokkos::Vector<ExecSpace>;
using MatType = sundials::kokkos::DenseMatrix<ExecSpace>;
using LSType = sundials::kokkos::DenseLinearSolver<ExecSpace>;
using SizeType = VecType::size_type;
// Constants
#define ZERO SUN_RCONST(0.0)
#define ONE SUN_RCONST(1.0)
#define TWO SUN_RCONST(2.0)
// User-supplied functions called by CVODE
static int f(sunrealtype t, N_Vector y, N_Vector ydot, void* user_data);
static int Jac(sunrealtype t, N_Vector y, N_Vector fy, SUNMatrix J,
void* user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
// User data structure available in user-supplied callback functions
struct UserData
{
int nbatches = 100; // number of chemical networks
int batchSize = 3; // size of each network
sunrealtype a, b; // chemical concentrations that are constant
sunrealtype ep; // stiffness parameter
};
/* -----------------------------------------------------------------------------
* Main Program
* ---------------------------------------------------------------------------*/
int main(int argc, char* argv[])
{
// Create the SUNDIALS context
sundials::Context sunctx;
Kokkos::initialize(argc, argv);
{
// Create UserData
UserData udata;
// Parse command line options
int argi = 0;
// Total number of batch systems
if (argc > 1) udata.nbatches = atoi(argv[++argi]);
// Linear solver type
int solver_type = 0;
if (argc > 2) solver_type = atoi(argv[++argi]);
// Problem setup
int test_type = 2;
if (argc > 3) test_type = atoi(argv[++argi]);
// Shortcuts
int nbatches = udata.nbatches;
int batchSize = udata.batchSize;
std::cout << "\nBatch of independent 3-species kinetics problems\n"
<< " number of batches = " << nbatches << "\n"
<< " linear solver = "
<< (solver_type ? "GMRES" : "KokkosKernels") << "\n"
<< " test type = " << test_type << "\n"
<< " execution space = " << ExecSpace().name() << "\n\n";
sunrealtype u0, v0, w0;
if (test_type == 0)
{
u0 = SUN_RCONST(3.9);
v0 = SUN_RCONST(1.1);
w0 = SUN_RCONST(2.8);
udata.a = SUN_RCONST(1.2);
udata.b = SUN_RCONST(2.5);
udata.ep = SUN_RCONST(1.0e-5);
}
else if (test_type == 1)
{
u0 = SUN_RCONST(3.0);
v0 = SUN_RCONST(3.0);
w0 = SUN_RCONST(3.5);
udata.a = SUN_RCONST(0.5);
udata.b = SUN_RCONST(3.0);
udata.ep = SUN_RCONST(5.0e-4);
}
else if (test_type == 2)
{
u0 = SUN_RCONST(1.2);
v0 = SUN_RCONST(3.1);
w0 = SUN_RCONST(3.0);
udata.a = SUN_RCONST(1.0);
udata.b = SUN_RCONST(3.5);
udata.ep = SUN_RCONST(5.0e-6);
}
else
{
std::cerr << "ERROR: Invalid test type option\n";
return -1;
}
// Create vector with the initial condition
const sunrealtype T0 = SUN_RCONST(0.0);
SizeType length{static_cast<SizeType>(batchSize * nbatches)};
VecType y{length, sunctx};
auto y_data_d = y.View();
Kokkos::parallel_for(
"fill_y", Kokkos::RangePolicy<ExecSpace>(0, nbatches),
KOKKOS_LAMBDA(const SizeType i) {
auto idx = batchSize * i;
y_data_d(idx) = u0;
y_data_d(idx + 1) = v0;
y_data_d(idx + 2) = w0;
});
// Create vector of absolute tolerances
VecType abstol{length, sunctx};
N_VConst(SUN_RCONST(1.0e-10), abstol);
// Create CVODE using Backward Differentiation Formula methods
void* cvode_mem = CVodeCreate(CV_BDF, sunctx);
if (check_ptr(cvode_mem, "CVodeCreate")) { return 1; }
// Initialize the integrator and set the ODE right-hand side function
int retval = CVodeInit(cvode_mem, f, T0, y);
if (check_flag(retval, "CVodeInit")) { return 1; }
// Attach the user data structure
retval = CVodeSetUserData(cvode_mem, &udata);
if (check_flag(retval, "CVodeSetUserData")) { return 1; }
// Specify the scalar relative tolerance and vector absolute tolerances
retval = CVodeSVtolerances(cvode_mem, SUN_RCONST(1.0e-6), abstol);
if (check_flag(retval, "CVodeSVtolerances")) { return 1; }
// Create the matrix and linear solver objects
std::unique_ptr<sundials::ConvertibleTo<SUNMatrix>> A;
std::unique_ptr<sundials::ConvertibleTo<SUNLinearSolver>> LS;
if (solver_type == 0)
{
// Create Kokkos dense block diagonal matrix
A = std::make_unique<MatType>(nbatches, batchSize, batchSize, sunctx);
// Create Kokkos batched dense linear solver
LS = std::make_unique<LSType>(sunctx);
// Attach the matrix and linear solver to CVODE
retval = CVodeSetLinearSolver(cvode_mem, LS->Convert(), A->Convert());
if (check_flag(retval, "CVodeSetLinearSolver")) return 1;
// Set the user-supplied Jacobian function
retval = CVodeSetJacFn(cvode_mem, Jac);
if (check_flag(retval, "CVodeSetJacFn")) return 1;
}
else
{
// Create matrix-free GMRES linear solver
LS = std::make_unique<sundials::experimental::SUNLinearSolverView>(
SUNLinSol_SPGMR(y, SUN_PREC_NONE, 0, sunctx));
// Attach the linear solver to CVODE
retval = CVodeSetLinearSolver(cvode_mem, LS->Convert(), nullptr);
if (check_flag(retval, "CVodeSetLinearSolver")) return 1;
}
// Final time and time between outputs
const sunrealtype Tf = SUN_RCONST(10.0);
const sunrealtype dTout = SUN_RCONST(1.0);
// Number of output times
const int Nt = static_cast<int>(ceil(Tf / dTout));
// Current time and first output time
sunrealtype t = T0;
sunrealtype tout = T0 + dTout;
// Initial output
auto y_data_h = y.HostView();
sundials::kokkos::CopyFromDevice(y);
Kokkos::fence();
std::cout << "At t = " << t << std::endl;
for (int batchj = 0; batchj < nbatches; batchj += 10)
{
auto idx = batchj * batchSize;
std::cout << " batch " << batchj << ": y = " << y_data_h(idx) << " "
<< y_data_h(idx + 1) << " " << y_data_h(idx + 2) << std::endl;
}
// Loop over output times
for (int iout = 0; iout < Nt; iout++)
{
// Advance in time
retval = CVode(cvode_mem, tout, y, &t, CV_NORMAL);
if (check_flag(retval, "CVode")) break;
// Output solution from some batches
sundials::kokkos::CopyFromDevice(y);
Kokkos::fence();
std::cout << "At t = " << t << std::endl;
for (int batchj = 0; batchj < nbatches; batchj += 10)
{
auto idx = batchj * batchSize;
std::cout << " batch " << batchj << ": y = " << y_data_h(idx) << " "
<< y_data_h(idx + 1) << " " << y_data_h(idx + 2) << std::endl;
}
tout += dTout;
tout = (tout > Tf) ? Tf : tout;
}
// Print some final statistics
long int nst, nfe, nsetups, nje, nni, ncfn, netf;
retval = CVodeGetNumSteps(cvode_mem, &nst);
check_flag(retval, "CVodeGetNumSteps");
retval = CVodeGetNumRhsEvals(cvode_mem, &nfe);
check_flag(retval, "CVodeGetNumRhsEvals");
retval = CVodeGetNumLinSolvSetups(cvode_mem, &nsetups);
check_flag(retval, "CVodeGetNumLinSolvSetups");
retval = CVodeGetNumErrTestFails(cvode_mem, &netf);
check_flag(retval, "CVodeGetNumErrTestFails");
retval = CVodeGetNumNonlinSolvIters(cvode_mem, &nni);
check_flag(retval, "CVodeGetNumNonlinSolvIters");
retval = CVodeGetNumNonlinSolvConvFails(cvode_mem, &ncfn);
check_flag(retval, "CVodeGetNumNonlinSolvConvFails");
retval = CVodeGetNumJacEvals(cvode_mem, &nje);
check_flag(retval, "CVodeGetNumJacEvals");
std::cout << "\nFinal Statistics:\n"
<< " Steps = " << nst << "\n"
<< " RHS evals = " << nfe << "\n"
<< " LS setups = " << nsetups << "\n"
<< " Jac evals = " << nje << "\n"
<< " NLS iters = " << nni << "\n"
<< " NLS fails = " << ncfn << "\n"
<< " Error test fails = " << netf << "\n";
// Free objects
CVodeFree(&cvode_mem);
}
Kokkos::finalize();
return 0;
}
/* -----------------------------------------------------------------------------
* User-supplied functions called by CVODE
* ---------------------------------------------------------------------------*/
// Right hand side function dy/dt = f(t,y)
int f(sunrealtype t, N_Vector y, N_Vector ydot, void* user_data)
{
auto udata = static_cast<UserData*>(user_data);
auto y_data = sundials::kokkos::GetVec<VecType>(y)->View();
auto ydot_data = sundials::kokkos::GetVec<VecType>(ydot)->View();
const auto nbatches = udata->nbatches;
const auto batchSize = udata->batchSize;
const auto a = udata->a;
const auto b = udata->b;
const auto ep = udata->ep;
Kokkos::parallel_for(
"RHS", Kokkos::RangePolicy<ExecSpace>(0, nbatches),
KOKKOS_LAMBDA(const SizeType i) {
auto idx = batchSize * i;
auto u = y_data(idx);
auto v = y_data(idx + 1);
auto w = y_data(idx + 2);
ydot_data(idx) = a - (w + ONE) * u + v * u * u;
ydot_data(idx + 1) = w * u - v * u * u;
ydot_data(idx + 2) = (b - w) / ep - w * u;
});
return 0;
}
// Jacobian of f(t,y)
int Jac(sunrealtype t, N_Vector y, N_Vector fy, SUNMatrix J, void* user_data,
N_Vector tmp1, N_Vector tmp2, N_Vector tmp3)
{
auto udata = static_cast<UserData*>(user_data);
auto y_data = sundials::kokkos::GetVec<VecType>(y)->View();
auto J_data = sundials::kokkos::GetDenseMat<MatType>(J)->View();
const auto nbatches = udata->nbatches;
const auto batchSize = udata->batchSize;
const auto ep = udata->ep;
Kokkos::parallel_for(
"Jac", Kokkos::RangePolicy<ExecSpace>(0, nbatches),
KOKKOS_LAMBDA(const SizeType i) {
// get y values
auto idx = batchSize * i;
auto u = y_data(idx);
auto v = y_data(idx + 1);
auto w = y_data(idx + 2);
// first col of block
J_data(i, 0, 0) = -(w + ONE) + TWO * u * v;
J_data(i, 1, 0) = u * u;
J_data(i, 2, 0) = -u;
// second col of block
J_data(i, 0, 1) = u * u;
J_data(i, 1, 1) = -u * u;
J_data(i, 2, 1) = u;
// third col of block
J_data(i, 0, 2) = -w;
J_data(i, 1, 2) = ZERO;
J_data(i, 2, 2) = -ONE / ep - u;
});
return 0;
}
|