1044 using ProfileClock = std::chrono::steady_clock;
1045 const bool profile_parent_fill = parent.profile_parent_fill;
1046 const auto fill_start = profile_parent_fill ? ProfileClock::now()
1047 : ProfileClock::time_point{};
1048 double parallel_copy_seconds = 0.0;
1049 int parallel_copy_calls = 0;
1050 const int current_role = std::strcmp(role,
"input") == 0 ? 0
1051 : (std::strcmp(role,
"limiter-base") == 0 ? 1 : 2);
1052 const bool probe_this_role = probe ==
nullptr ||
1053 probe->requested_role == current_role;
1054 bool inject_rank_local_store_fault =
false;
1055 #ifdef ERF_SBM_AMR_QUALIFICATION
1058 inject_rank_local_store_fault =
1059 parent.inject_parent_store_fault && level == 1 && stage == 0 &&
1060 current_role == 0 && amrex::ParallelDescriptor::MyProc() == 0;
1062 const double coarse_dt = parent.new_time - parent.old_time;
1063 const double dt_min = std::min(parent.fine_step_dt, coarse_dt);
1065 std::string diagnostic;
1067 dt_min,
theta, diagnostic)) {
1068 amrex::Abort(
"SBM coarse/fine stage interpolation: " + diagnostic);
1074 amrex::Abort(
"SBM coarse spectrum and host density endpoints do not "
1075 "share semantic times");
1077 if (parent.conserved_old ==
nullptr || parent.conserved_new ==
nullptr ||
1078 !parent.refinement_ratio.allGT(0) ||
1079 coarse_old_spectrum.nComp() != destination.nComp() ||
1080 coarse_new_spectrum.nComp() != destination.nComp() ||
1081 parent.conserved_old->nComp() <=
Rho_comp ||
1082 parent.conserved_new->nComp() <=
Rho_comp ||
1084 coarse_new_spectrum) ||
1086 *parent.conserved_old) ||
1088 *parent.conserved_new) ||
1090 missing_fine_ghosts.nComp() < 3 ||
1091 !destination.nGrowVect().allGE(amrex::IntVect(2)) ||
1092 !missing_fine_ghosts.nGrowVect().allGE(amrex::IntVect(2))) {
1093 amrex::Abort(
"SBM coarse/fine stage interpolation received an "
1094 "incompatible layout");
1097 amrex::BoxArray coarse_on_fine_ba = destination.boxArray();
1098 coarse_on_fine_ba.coarsen(parent.refinement_ratio);
1099 const amrex::IntVect coarse_ghosts(2);
1100 if (coarse_stage.nComp() != 2 * max_chunk_components + 2 ||
1101 coarse_stage.boxArray() != coarse_on_fine_ba ||
1102 coarse_stage.DistributionMap() != destination.DistributionMap() ||
1103 coarse_stage.nGrowVect() != coarse_ghosts) {
1104 coarse_stage.define(coarse_on_fine_ba, destination.DistributionMap(),
1105 2 * max_chunk_components + 2, coarse_ghosts);
1107 coarse_stage.setVal(std::numeric_limits<Real>::quiet_NaN());
1108 const bool componentwise_parent_fill = parent.componentwise_parent_fill;
1109 if (!componentwise_parent_fill) {
1110 if (coarse_pack.nComp() != 2 * max_chunk_components + 2 ||
1111 coarse_pack.boxArray() != coarse_old_spectrum.boxArray() ||
1112 coarse_pack.DistributionMap() !=
1113 coarse_old_spectrum.DistributionMap()) {
1114 coarse_pack.define(coarse_old_spectrum.boxArray(),
1115 coarse_old_spectrum.DistributionMap(),
1116 2 * max_chunk_components + 2, 0);
1117 coarse_pack.setVal(std::numeric_limits<Real>::quiet_NaN());
1121 amrex::Box coarse_domain = fine_geometry.Domain();
1122 coarse_domain.coarsen(parent.refinement_ratio);
1124 amrex::IntVect coarse_period(0);
1125 for (
int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
1126 if (fine_geometry.isPeriodic(dir)) coarse_period[dir] = coarse_domain.length(dir);
1128 const amrex::Periodicity coarse_periodicity(coarse_period);
1129 amrex::Box coarse_region = coarse_domain;
1130 for (
int dir = 0; dir < AMREX_SPACEDIM; ++dir) {
1131 if (fine_geometry.isPeriodic(dir)) coarse_region.grow(dir, coarse_ghosts[dir]);
1133 const int rho_old_local = 2 * max_chunk_components;
1134 const int rho_new_local = rho_old_local + 1;
1135 auto timed_parallel_copy = [&] (
auto&& operation) {
1136 if (profile_parent_fill) {
1137 const auto copy_start = ProfileClock::now();
1139 parallel_copy_seconds +=
1140 std::chrono::duration<double>(ProfileClock::now() - copy_start).count();
1144 ++parallel_copy_calls;
1153 missing_fine_ghosts.setVal(Real(0.0), 0, 3, 2);
1154 missing_fine_ghosts.setVal(Real(1.0), 0, 1, 2);
1155 missing_fine_ghosts.setVal(Real(0.0), 0, 1, 0);
1156 missing_fine_ghosts.FillBoundary(0, 1, fine_geometry.periodicity());
1158 amrex::IntVect fault_cell(std::numeric_limits<int>::min());
1159 #ifdef ERF_SBM_AMR_QUALIFICATION
1160 if (inject_rank_local_store_fault) {
1165 if (probe !=
nullptr && probe_this_role &&
1166 probe->seed_finite_stale_value) {
1167 const auto probe_cell = probe->fine_cell;
1168 const int probe_component = probe->global_component;
1169 const Real sentinel = probe->stale_value;
1170 for (amrex::MFIter mfi(destination,
false); mfi.isValid(); ++mfi) {
1171 const auto target = destination.array(mfi);
1172 const auto missing = missing_fine_ghosts.const_array(mfi);
1174 mfi.fabbox(), [=] AMREX_GPU_DEVICE(
int i,
int j,
int k) noexcept {
1175 if (
missing(i, j, k, 0) == Real(1.0) &&
1176 i == probe_cell[0] && j == probe_cell[1] &&
1177 k == probe_cell[2]) {
1178 target(i, j, k, probe_component) = sentinel;
1189 const Real poison = std::numeric_limits<Real>::quiet_NaN();
1190 for (amrex::MFIter mfi(destination,
false); mfi.isValid(); ++mfi) {
1191 const amrex::Box fab_box = mfi.fabbox();
1192 const auto missing = missing_fine_ghosts.const_array(mfi);
1193 const auto target = destination.array(mfi);
1194 const int ncomp = destination.nComp();
1196 fab_box, [=] AMREX_GPU_DEVICE(
int i,
int j,
int k) noexcept {
1197 if (
missing(i, j, k, 0) != Real(1.0))
return;
1198 for (
int n = 0; n < ncomp; ++n) {
1199 target(i, j, k, n) = poison;
1204 std::vector<int> flattened_components;
1205 std::vector<int> chunk_offsets;
1206 flattened_components.reserve(
static_cast<std::size_t
>(destination.nComp()));
1207 chunk_offsets.reserve(chunks.size());
1208 for (
const auto& chunk : chunks) {
1209 chunk_offsets.push_back(
static_cast<int>(flattened_components.size()));
1210 flattened_components.insert(flattened_components.end(),
1211 chunk.components.begin(),
1212 chunk.components.end());
1214 if (flattened_components.size() !=
1215 static_cast<std::size_t
>(destination.nComp())) {
1216 amrex::Abort(
"SBM coarse/fine stage chunks do not cover every "
1217 "spectrum component exactly once");
1219 amrex::Gpu::DeviceVector<int> device_components(flattened_components.size());
1220 amrex::Gpu::copy(amrex::Gpu::hostToDevice, flattened_components.begin(),
1221 flattened_components.end(), device_components.begin());
1223 const auto coarse_old_periodic = coarse_periodicity;
1224 for (std::size_t chunk_index = 0; chunk_index < chunks.size();
1226 const auto& chunk = chunks[chunk_index];
1227 if (chunk.components.empty() ||
1228 static_cast<int>(chunk.components.size()) >
1229 max_chunk_components) {
1230 amrex::Abort(
"SBM coarse/fine stage chunk exceeds bounded scratch");
1232 const int component_count =
1233 static_cast<int>(chunk.components.size());
1234 if (componentwise_parent_fill) {
1235 for (std::size_t local = 0; local < chunk.components.size(); ++local) {
1236 const int global = chunk.components[local];
1237 timed_parallel_copy([&] {
1238 coarse_stage.ParallelCopy(
1239 coarse_old_spectrum, global,
static_cast<int>(local), 1,
1240 amrex::IntVect(0), coarse_ghosts, coarse_old_periodic);
1242 timed_parallel_copy([&] {
1243 coarse_stage.ParallelCopy(
1244 coarse_new_spectrum, global,
1245 max_chunk_components +
static_cast<int>(local), 1,
1246 amrex::IntVect(0), coarse_ghosts, coarse_old_periodic);
1249 timed_parallel_copy([&] {
1250 coarse_stage.ParallelCopy(
1251 *parent.conserved_old,
Rho_comp, rho_old_local, 1,
1252 amrex::IntVect(0), coarse_ghosts, coarse_old_periodic);
1254 timed_parallel_copy([&] {
1255 coarse_stage.ParallelCopy(
1256 *parent.conserved_new,
Rho_comp, rho_new_local, 1,
1257 amrex::IntVect(0), coarse_ghosts, coarse_old_periodic);
1260 const int component_offset = chunk_offsets[chunk_index];
1261 const int* component_map_local =
1262 device_components.data() + component_offset;
1263 for (amrex::MFIter mfi(coarse_pack,
false); mfi.isValid(); ++mfi) {
1264 const amrex::Box bx = mfi.validbox();
1265 const auto old_values = coarse_old_spectrum.const_array(mfi);
1266 const auto new_values = coarse_new_spectrum.const_array(mfi);
1267 const auto rho_old = parent.conserved_old->const_array(mfi);
1268 const auto rho_new = parent.conserved_new->const_array(mfi);
1269 const auto pack = coarse_pack.array(mfi);
1271 bx, 2 * component_count + 2,
1272 [=] AMREX_GPU_DEVICE(
int i,
int j,
int k,
1273 int local) noexcept {
1274 if (local < component_count) {
1275 const int global = component_map_local[local];
1276 pack(i, j, k, local) =
1277 old_values(i, j, k, global);
1278 }
else if (local < 2 * component_count) {
1279 const int local_component =
1280 local - component_count;
1282 component_map_local[local_component];
1284 max_chunk_components + local_component) =
1285 new_values(i, j, k, global);
1286 }
else if (local == 2 * component_count) {
1287 pack(i, j, k, rho_old_local) =
1290 pack(i, j, k, rho_new_local) =
1295 timed_parallel_copy([&] {
1296 coarse_stage.ParallelCopy(
1297 coarse_pack, 0, 0, 2 * max_chunk_components + 2,
1298 amrex::IntVect(0), coarse_ghosts, coarse_old_periodic);
1303 max_chunk_components,
1304 rho_old_local, coarse_region)) {
1307 message <<
"SBM coarse/fine endpoint data are not finite with "
1308 <<
"positive density (level=" << level <<
", stage="
1309 << stage <<
", role=" << role <<
", requested_time="
1310 << requested_time <<
", coarse_old=" << parent.old_time
1311 <<
", coarse_new=" << parent.new_time <<
", rank="
1312 << amrex::ParallelDescriptor::MyProc() <<
')';
1316 const int component_offset = chunk_offsets[chunk_index];
1317 const Real theta_real =
static_cast<Real
>(
theta);
1318 const auto component_map = device_components.data();
1319 const amrex::IntVect refinement_ratio = parent.refinement_ratio;
1320 const amrex::IntVect probe_cell =
1321 probe ==
nullptr ? amrex::IntVect(-1) : probe->fine_cell;
1322 const int skipped_component =
1323 probe ==
nullptr ? -1 : probe->skipped_global_component;
1324 const bool skip_tally_only =
1325 probe !=
nullptr && probe->skip_write_tally_only;
1326 const int probe_stage = probe ==
nullptr ? -1 : probe->requested_stage;
1327 const bool probe_role_matches = probe_this_role;
1328 for (amrex::MFIter mfi(destination,
false); mfi.isValid(); ++mfi) {
1329 const amrex::Box fab_box = mfi.fabbox();
1330 const auto coarse = coarse_stage.const_array(mfi);
1331 const auto missing = missing_fine_ghosts.array(mfi);
1332 const auto target = destination.array(mfi);
1333 const auto component_map_local = component_map + component_offset;
1335 fab_box, [=] AMREX_GPU_DEVICE(
int i,
int j,
int k) noexcept {
1336 if (
missing(i, j, k, 0) != Real(1.0))
return;
1337 amrex::IntVect parent_cell(i, j, k);
1338 parent_cell.coarsen(refinement_ratio);
1339 const Real rho0 = coarse(parent_cell[0], parent_cell[1],
1340 parent_cell[2], rho_old_local);
1341 const Real rho1 = coarse(parent_cell[0], parent_cell[1],
1342 parent_cell[2], rho_new_local);
1343 const Real rho_t = (Real(1.0) - theta_real) * rho0 +
1345 int wrote_components = 0;
1346 for (
int local = 0; local < component_count; ++local) {
1347 const int global = component_map_local[local];
1348 const bool probe_fault_cell =
1349 global == skipped_component &&
1350 probe_role_matches &&
1351 (probe_stage < 0 || probe_stage == stage) &&
1352 i == probe_cell[0] && j == probe_cell[1] &&
1354 const bool skip_assignment =
1355 probe_fault_cell && !skip_tally_only;
1356 const bool skip_rank_local_assignment =
1357 inject_rank_local_store_fault && global == 0 &&
1358 i == fault_cell[0] && j == fault_cell[1] &&
1360 const Real u0 = coarse(parent_cell[0], parent_cell[1],
1361 parent_cell[2], local);
1362 const Real u1 = coarse(
1363 parent_cell[0], parent_cell[1], parent_cell[2],
1364 max_chunk_components + local);
1365 const Real u_t = (Real(1.0) - theta_real) * u0 +
1367 const Real intensive = u_t / rho_t;
1368 const bool positive_support_expected =
1369 ((theta_real < Real(1.0)) && u0 > Real(0.0)) ||
1370 ((theta_real > Real(0.0)) && u1 > Real(0.0));
1371 const bool quotient_ok =
1372 amrex::Math::isfinite(rho_t) && rho_t > Real(0.0) &&
1373 amrex::Math::isfinite(u_t) && u_t >= Real(0.0) &&
1374 amrex::Math::isfinite(intensive) &&
1375 intensive >= Real(0.0) &&
1376 (!(positive_support_expected) ||
1377 (u_t > Real(0.0) && intensive > Real(0.0)));
1378 if (!skip_assignment && !skip_rank_local_assignment) {
1380 target(i, j, k, global) = intensive;
1384 target(i, j, k, global) =
1385 std::numeric_limits<Real>::infinity();
1386 missing(i, j, k, 2) = Real(1.0);
1389 if (!(probe_fault_cell && skip_tally_only)) {
1393 missing(i, j, k, 1) +=
static_cast<Real
>(wrote_components);
1402 for (amrex::MFIter mfi(destination,
false); mfi.isValid(); ++mfi) {
1403 const amrex::Box fab_box = mfi.fabbox();
1404 const auto mask = missing_fine_ghosts.array(mfi);
1405 const int ncomp = destination.nComp();
1406 const auto target = destination.const_array(mfi);
1408 ? -1 : probe->global_component;
1409 const amrex::IntVect sample_cell = probe ==
nullptr
1410 ? amrex::IntVect(-1) : probe->fine_cell;
1411 const bool sample_role_matches = probe_this_role;
1412 const int sample_stage = probe ==
nullptr
1413 ? -1 : probe->requested_stage;
1414 auto* sampled_value = probe ==
nullptr
1415 ? nullptr : probe->sampled_intensive_device;
1416 auto* sampled_hit = probe ==
nullptr ? nullptr : probe->hit_device;
1418 fab_box, [=] AMREX_GPU_DEVICE(
int i,
int j,
int k) noexcept {
1419 if (mask(i, j, k, 0) != Real(1.0))
return;
1420 int destination_invalid = 0;
1421 const bool quotient_failed =
1422 mask(i, j, k, 2) == Real(1.0);
1423 for (
int component = 0; component < ncomp; ++component) {
1424 const Real value = target(i, j, k, component);
1425 if (!amrex::Math::isfinite(value) || value < Real(0.0)) {
1430 if (quotient_failed &&
1431 value == std::numeric_limits<Real>::infinity()) {
1434 destination_invalid = 1;
1438 if (destination_invalid != 0) {
1441 mask(i, j, k, 2) = quotient_failed
1442 ? Real(3.0) : Real(2.0);
1444 if (sampled_value !=
nullptr && sampled_hit !=
nullptr &&
1445 sample_role_matches &&
1446 (sample_stage < 0 || sample_stage == stage) &&
1447 i == sample_cell[0] && j == sample_cell[1] &&
1448 k == sample_cell[2]) {
1454 const Real destination_failure = missing_fine_ghosts.max(2, 2);
1455 if (destination_failure == Real(2.0) ||
1456 destination_failure == Real(3.0)) {
1459 message <<
"SBM coarse/fine stage left a coarse-only parent ghost "
1460 <<
"component unwritten or invalid in the destination"
1461 << (destination_failure == Real(3.0)
1462 ?
" (also found an invalid quotient)" :
"")
1463 <<
" (level=" << level <<
", stage=" << stage <<
", role="
1464 << role <<
", requested_time=" << requested_time
1465 <<
", coarse_old=" << parent.old_time <<
", coarse_new="
1466 << parent.new_time <<
", rank="
1467 << amrex::ParallelDescriptor::MyProc()
1468 << (inject_rank_local_store_fault
1469 ?
", assignment_only_fault=rank-local" :
"")
1473 if (destination_failure == Real(1.0)) {
1476 message <<
"SBM coarse/fine stage produced an unrepresentable "
1477 <<
"U(t)/rho_d(t) quotient (level=" << level <<
", stage="
1478 << stage <<
", role=" << role <<
", requested_time="
1479 << requested_time <<
", coarse_old=" << parent.old_time
1480 <<
", coarse_new=" << parent.new_time <<
", rank="
1481 << amrex::ParallelDescriptor::MyProc() <<
')';
1487 for (amrex::MFIter mfi(destination,
false); mfi.isValid(); ++mfi) {
1488 const amrex::Box fab_box = mfi.fabbox();
1489 const auto mask = missing_fine_ghosts.array(mfi);
1490 const int ncomp = destination.nComp();
1492 fab_box, [=] AMREX_GPU_DEVICE(
int i,
int j,
int k) noexcept {
1493 if (mask(i, j, k, 0) == Real(1.0)) {
1495 (mask(i, j, k, 1) ==
static_cast<Real
>(ncomp))
1496 ? Real(0.0) : Real(1.0);
1500 if (missing_fine_ghosts.max(0, 2) != Real(0.0)) {
1503 message <<
"SBM coarse/fine stage omitted a required parent ghost "
1504 <<
"component write (level=" << level <<
", stage=" << stage
1505 <<
", role=" << role <<
", requested_time=" << requested_time
1506 <<
", coarse_old=" << parent.old_time <<
", coarse_new="
1507 << parent.new_time <<
", rank="
1508 << amrex::ParallelDescriptor::MyProc() <<
')';
1511 #ifdef ERF_SBM_AMR_QUALIFICATION
1512 if (split_values_checked !=
nullptr &&
1513 parent.validate_split_ghost_values && current_role == 0) {
1516 const bool at_start = stage == 0 &&
1518 const bool interior =
theta > 0.0 &&
theta < 1.0;
1519 const std::size_t slot = at_start ? 0
U : 1U;
1520 if ((at_start || interior) && !(*split_values_checked)[slot]) {
1521 amrex::Gpu::streamSynchronize();
1523 destination, fine_source_state, fine_source_conserved,
1524 coarse_old_spectrum, coarse_new_spectrum, parent, fine_geometry,
1525 theta, stage, requested_time);
1526 (*split_values_checked)[slot] =
true;
1530 amrex::ignore_unused(fine_source_state, fine_source_conserved,
1531 split_values_checked);
1535 amrex::Gpu::streamSynchronize();
1536 if (profile_parent_fill) {
1537 std::uint64_t local_coarse_stage_bytes = 0;
1538 std::uint64_t local_coarse_pack_bytes = 0;
1539 for (amrex::MFIter mfi(coarse_stage,
false); mfi.isValid(); ++mfi) {
1540 local_coarse_stage_bytes +=
static_cast<std::uint64_t
>(
1541 coarse_stage[mfi].nBytesOwned());
1543 if (coarse_pack.isDefined()) {
1544 for (amrex::MFIter mfi(coarse_pack,
false);
1545 mfi.isValid(); ++mfi) {
1546 local_coarse_pack_bytes +=
static_cast<std::uint64_t
>(
1547 coarse_pack[mfi].nBytesOwned());
1550 std::cout << std::setprecision(9)
1551 <<
"SBM_AMR_PROFILE_PARENT rank="
1552 << amrex::ParallelDescriptor::MyProc()
1553 <<
" level=" << level <<
" stage=" << stage
1555 <<
" ncomp=" << destination.nComp()
1556 <<
" max_chunk_components=" << max_chunk_components
1557 <<
" chunks=" << chunks.size()
1558 <<
" parent_fill_mode="
1559 << (componentwise_parent_fill ?
"componentwise" :
"packed")
1560 <<
" parallel_copy_calls=" << parallel_copy_calls
1561 <<
" parallel_copy_wall_s=" << parallel_copy_seconds
1562 <<
" parent_fill_wall_s="
1563 << std::chrono::duration<double>(ProfileClock::now() -
1565 <<
" coarse_stage_fab_bytes_local="
1566 << local_coarse_stage_bytes
1567 <<
" coarse_source_pack_fab_bytes_local="
1568 << local_coarse_pack_bytes
1569 <<
" rank_live_fab_bytes="
1570 << amrex::TotalBytesAllocatedInFabs()
@ U
Definition: ERF_IndexDefines.H:178
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Real sample_component(int dir, Array4< Real const > const &f, Real x, Real y, Real z, const GpuArray< Real, AMREX_SPACEDIM > &plo, const GpuArray< Real, AMREX_SPACEDIM > &dxi, int klo, int khi, Array4< Real const > const &znd, bool has_znd, Real dz)
Definition: ERF_ActuatorSampling.cpp:61
constexpr Real missing
Definition: ERF_TopographicShading.cpp:10
bool coarse_stage_theta(const double requested_time, const double coarse_old_time, const double coarse_new_time, const double dt_min, double &theta, std::string &diagnostic)
Definition: ERF_SBMTransport.cpp:948
bool coarse_stage_endpoints_valid(const MultiFab &coarse_stage, const int component_count, const int new_offset, const int rho_old_local, const amrex::Box &coarse_region)
Definition: ERF_SBMTransport.cpp:990
void clear_outside_physical_domain(MultiFab &mask, const int component, const amrex::Geometry &geometry)
Definition: ERF_SBMTransport.cpp:200
void verify_split_ghost_values(const MultiFab &destination, const MultiFab &fine_source_state, const MultiFab &fine_source_conserved, const MultiFab &coarse_old_spectrum, const MultiFab &coarse_new_spectrum, const SBMAMRStageParent &parent, const Geometry &fine_geometry, const double theta, const int stage, const double requested_time)
Definition: ERF_SBMAMRQualification.cpp:410
IntVect first_coarse_only_ghost(const MultiFab &mask)
Definition: ERF_SBMAMRQualification.cpp:555