Skip to content

hydro_umdrv_eb: divc/redistwgt/flux_tmp freed while StateRedist kernels still read them #95

Closed

Description

@WeiqunZhang

Severity: high/medium · Category: gpu-parallel · Fix order: 1 of 25 — fix this first.

Filenames are numbered in reverse fix order: 001 = fix last, 025 = fix first. This file is 025.

Locations: Source/Hydro/Hydro_umdrv_eb.cpp:83, Source/Hydro/Hydro_umdrv_eb.cpp:107

Based on commit 8f87fa8 (line numbers refer to that tree).

The defect

What is wrong — divc, redistwgt (line 83) and flux_tmp (lines 105-109) are plain The_Arena FArrayBoxes destroyed at function exit while ApplyMLRedistribution/hydro_consup_eb device kernels reading them are still in flight; the StateRedist and NoRedist branches of ApplyMLRedistribution contain no streamSynchronize, unlike the FluxRedist branch. Reported independently at this same line by F008, F015, F019, F040; each reviewer's own wording and evidence is under Verification evidence below.

Why it matters

GPU build, USE_EB=TRUE, redistribution_type=StateRedist (or NoRedist), multiple boxes per rank: MLStateRedistribute kernels on stream A still read divc/redistwgt when the FABs' memory is freed at return; the next MFIter iteration (AMReX default 4 streams) reallocates and setVals the same blocks on stream B, producing nondeterministic garbage/NaNs in the hydro source.

How to reach it

  • Exec/SodPlusSphere: make USE_CUDA=TRUE (DIM=2, USE_EB=TRUE); shipped inputs' duplicated CAMR.redistribution_type ends on StateRedist (last occurrence wins), n_cell=32^2; append amr.max_grid_size=16 -- the deck's LAST max_grid_size is 64 (inputs:70, overriding inputs:42), which puts all of level 0 in one box and removes the cross-iteration race entirely. With 16 there are 4 cut-cell boxes per rank across 4 GPU streams.
  • CI-supported build: Exec/SodPlusSphere, make DIM=2 USE_CUDA=TRUE (USE_EB=TRUE in GNUmakefile); shipped inputs' last redistribution_type line is StateRedist (ParmParse last-wins); >=2 EB-cut boxes per rank via amr.max_grid_size.
  • Exec/SodPlusSphere: make DIM=2 USE_CUDA=TRUE (USE_EB=TRUE; exact CI build in .github/workflows/cuda.yml) with shipped inputs — effective CAMR.redistribution_type=StateRedist (last line wins), 32x32 cells, max_grid_size=16 gives multiple EB-cut boxes per rank.
  • Exec/SodPlusSphere built with USE_EB=TRUE, USE_CUDA=TRUE; shipped inputs resolve to StateRedist (duplicate key, ParmParse last-wins); sphere EB cuts multiple level-1 grids on one rank; amrex.max_gpu_streams defaults to 4.

Suggested fix

Give divc, redistwgt and flux_tmp The_Async_Arena() — the convention the rest of this code already follows: divu/qec a few lines above, flux/q/qaux/src_q in the caller, every temporary in the non-EB twin Hydro_umdrv.cpp, and AMReX's own StateRedist scratch (itracker, nrs_fab, alpha_fab, nbhd_vol_fab, cent_hat_fab in AMReX_EB_RedistributionApply.cpp). The async arena defers the free to a stream callback, so it closes the race without serializing MFIter's multi-stream pipeline the way a trailing Gpu::streamSynchronize() would. The asymmetry dates to the initial EB import, where qec was already async — an oversight, not a decision.

Judgement call: arena vs. blunt sync. If you take the arena route, two companions in CAMR_construct_hydro_source.cpp must land with it, since only streamSynchronize would have covered them: bcs_d (line 183), whose pointer reaches MLStateRedistribute — hoist it out of the MFIter loop, it is loop-invariant and the per-box H2D copy is wasted — and dm_as_fine, resized per iteration from the default arena while the previous iteration's redistribution may still be writing it.

For Source/Hydro/Hydro_umdrv_eb.cpp:83 (F006):

--- a/Source/Hydro/Hydro_umdrv_eb.cpp
+++ b/Source/Hydro/Hydro_umdrv_eb.cpp
@@ -80,7 +80,7 @@
     // ****************************************************************
     // Quantities for redistribution
     // ****************************************************************
-    FArrayBox divc,redistwgt;
+    FArrayBox divc(amrex::The_Async_Arena()), redistwgt(amrex::The_Async_Arena());
 
     if (l_redistribution_type == "StateRedist") {
              divc.resize(bxg_i,NVAR); // This will hold "dUdt" before redistribution
@@ -104,6 +104,6 @@
     // ****************************************************************
     FArrayBox flux_tmp[AMREX_SPACEDIM];
     for (int idim=0; idim < AMREX_SPACEDIM; ++idim) {
-        flux_tmp[idim].resize(amrex::surroundingNodes(bxg_ii,idim),NVAR);
+        flux_tmp[idim].resize(amrex::surroundingNodes(bxg_ii,idim),NVAR,amrex::The_Async_Arena());
         flux_tmp[idim].setVal<RunOn::Device>(0.);
     }

Puts divc/redistwgt/flux_tmp on The_Async_Arena, matching divu/qec above. FArrayBox(Arena*) sets m_arena, and BaseFab::resize with a null arena reuses m_arena, so the four later resize calls need no change. PArena::free is stream-ordered (cudaFreeAsync, else Elixir), so in-flight kernels stay safe. Maintainer: bcs_d in CAMR_construct_hydro_source.cpp keeps the same hazard.

Diff(s) are against 8f87fa8, written from the current source and verified only with git apply --check — never compiled, never run, never applied to the tree. Treat them as precise intent, not tested patches.

Verification evidence

F006 — confirmed (two independent verifier lenses)

Lens 1 (refutation attempt): Line 83 'FArrayBox divc,redistwgt;' resized without arena -> The_Arena (AMReX_DataAllocator.H defaults null arena to The_Arena()); no Elixir anywhere in CAMR. ApplyMLRedistribution's StateRedist branch (AMReX_EB_RedistributionApply.cpp:108-216) and NoRedist branch (218-224) have no streamSynchronize; only FluxRedist does via amrex_flux_redistribute. MFIter::operator++ (AMReX_MFIter.cpp:541) only rotates streams, no sync. May relate to open issue #70 for GPU-run comparisons.

Lens 2 (reachability/intent): BaseFab::clear→Arena::streamOrderedFree falls back to plain free() for The_Arena (AMReX_Arena.cpp:130-140); MFIter::operator++ rotates 4 streams with no sync (AMReX_MFIter.cpp:526); StateRedist/NoRedist branches of ApplyMLRedistribution launch kernels reading divc/redistwgt with no sync, only FluxRedist ends in Gpu::streamSynchronize (AMReX_EB_FluxRedistribute.cpp:292). CAMR's syncs (Hydro_adjust_fluxes.cpp:169) precede hydro_consup_eb/ApplyMLRedistribution. No Elixir/Async_Arena on divc/redistwgt/flux_tmp; commit 90857ca converted only divu/qec.

F008 — confirmed (two independent verifier lenses)

Reported as: divc, redistwgt (line 83) and flux_tmp (line 105) are The_Arena FArrayBoxes with no Elixir and no stream sync before hydro_umdrv_eb returns; with StateRedist or NoRedist the kernels launched by hydro_consup_eb and ApplyMLRedistribution that read/write them are still in flight when they are freed.

Failure scenario: GPU EB run with CAMR.redistribution_type=StateRedist (shipped decks: Exec/SodPlusSphere/inputs, Exec/ReReTest) and >=2 EB boxes per rank: amrex MFIter::operator++ rotates streams without syncing, so the next iteration's identical-size The_Arena allocations reuse the just-freed divc/redistwgt/flux_tmp buffers concurrently with in-flight MLStateRedistribute kernels, corrupting dsdt nondeterministically. FluxRedist alone is safe because amrex_flux_redistribute ends with streamSynchronize.

Lens 1 (refutation attempt): divc/redistwgt (line 83) and flux_tmp (105-108) are The_Arena FABs freed at return while hydro_consup_eb (line 158) and ApplyMLRedistribution (line 176) kernels are in flight; only sync in the chain is at end of adjust_fluxes_eb (Hydro_adjust_fluxes.cpp:169), which runs BEFORE those launches. Shipped decks confirmed: SodPlusSphere/inputs:31 and ReReTest inputs_eb_rgt_fine_lft:29 end with 'CAMR.redistribution_type = StateRedist' (last ParmParse line wins). FluxRedist alone syncs (AMReX_EB_FluxRedistribute.cpp:292).

Lens 2 (reachability/intent): Hydro_umdrv_eb.cpp:83,105: divc/redistwgt/flux_tmp resize() with no arena -> The_Arena (AMReX_DataAllocator.H fallback); no elixir/sync before return. AMReX_MFIter.cpp:526 operator++ rotates streams (default 4) without sync. ApplyMLRedistribution StateRedist/NoRedist branches (AMReX_EB_RedistributionApply.cpp:115-220) are fully async; only FluxRedist syncs (AMReX_EB_FluxRedistribute.cpp:292). Commit 90857ca async-arena'd divu/qec in the same diff but not these three; adjust_fluxes_eb's sync (line 169) precedes consup/ApplyML, protecting nothing.

F015 — confirmed (two independent verifier lenses)

Reported as: divc, redistwgt (line 83) and flux_tmp (line 105) are default-Arena FArrayBoxes freed at function exit while ApplyMLRedistribution's kernels may still read them; only the FluxRedist path is saved by a streamSynchronize inside amrex_flux_redistribute — StateRedist and NoRedist have none.

Failure scenario: GPU build, USE_EB, redistribution_type=StateRedist (used by Exec/MovingEBCases inputs) or NoRedist, >=2 boxes per rank: MLStateRedistribute kernels on stream N still read divc/redistwgt (and bcs_d) after the host frees them; the next MFIter iteration on a different stream reallocates and overwrites the same memory, giving nondeterministic corruption of hyd_src/dsdt.

Lens 1 (refutation attempt): Code matches: default-arena divc/redistwgt/flux_tmp destroyed at function exit with StateRedist/NoRedist branches of AMReX_EB_RedistributionApply.cpp containing no sync; grep over amrex EB redistribution files finds streamSynchronize only in AMReX_EB_FluxRedistribute.cpp:292. Exec/MovingEBCases/MovingPlane_Shock/inputs:31 ends with StateRedist as claimed. bcs_d (Gpu::DeviceVector, CAMR_construct_hydro_source.cpp:183) shares the per-iteration non-async lifetime while MLStateRedistribute reads d_bcrec_ptr.

Lens 2 (reachability/intent): Hydro_umdrv_eb.cpp:83,105: divc/redistwgt/flux_tmp default to The_Arena (DataAllocator default); same commit 90857ca gave divu/qec The_Async_Arena but skipped these. MFIter::operator++ (AMReX_MFIter.cpp:526) rotates 4 streams, no per-iteration sync; CArena::free is host-immediate. Only FluxRedist syncs (AMReX_EB_FluxRedistribute.cpp:292 "because of FArrayBoxes defined in this function"); StateRedist/NoRedist kernels in AMReX_EB_RedistributionApply.cpp read dUdt_in=divc, scratch=redistwgt asynchronously into dsdt_arr=hyd_src, which feeds the state update unmasked.

F019 — confirmed (two independent verifier lenses)

Reported as: divc, redistwgt (line 83) and flux_tmp (line 105) are default-arena FArrayBoxes destroyed at function exit while hydro_consup_eb and ApplyMLRedistribution kernels reading them are still in flight; with redistribution_type=StateRedist or NoRedist AMReX's redistribution has no trailing stream sync (only the FluxRedist branch syncs, AMReX_EB_FluxRedistribute.cpp:292), so the freed The_Arena blocks are reused by the next MFIter iteration on a different stream.

Failure scenario: GPU EB build with redistribution_type=StateRedist (or NoRedist), >=2 cut-cell boxes per rank: iteration i+1's divc.resize(bxg_i,NVAR) gets iteration i's just-freed identically-sized block from The_Arena and setVal(0.0) on stream B overwrites it while iteration i's MLStateRedistribute on stream A still reads dUdt_in/scratch, producing corrupted dsdt nondeterministically.

Lens 1 (refutation attempt): The citation AMReX_EB_FluxRedistribute.cpp:292 'Gpu::streamSynchronize(); // because of FArrayBoxes defined in this function' is exact; StateRedist/NoRedist branches have none. MFIter::operator++ (AMReX_MFIter.cpp:526-548) does setStreamIndex(currentIndex%streams) with the debug sync commented out; default max_gpu_streams=4 (AMReX_GpuDevice.cpp:91). Iteration i+1's divc.resize(bxg_i,NVAR) + setVal on stream B can reuse iteration i's just-freed identically-sized The_Arena block while stream A's MLStateRedistribute still reads dUdt_in/scratch.

Lens 2 (reachability/intent): Hydro_umdrv_eb.cpp:83/105: divc, redistwgt, flux_tmp default to The_Arena (DataAllocator fallback) while divu/qec use The_Async_Arena. Last sync is adjust_fluxes_eb (Hydro_adjust_fluxes.cpp:169), before hydro_consup_eb/ApplyMLRedistribution. StateRedist/NoRedist branches (AMReX_EB_RedistributionApply.cpp:184-225) read divc/redistwgt with no trailing sync; only amrex_flux_redistribute syncs (AMReX_EB_FluxRedistribute.cpp:292). MFIter::operator++ only switches streams; CArena::free is not stream-ordered. Code unchanged since initial commit; no intent evidence.

F040 — confirmed (one verifier lens)

Reported as: flux_tmp (line 107) and divc/redistwgt (lines 86-95) are allocated from the default arena with no Elixir and no stream sync before destruction at function return, while sibling temporaries divu/qec in the same function correctly use The_Async_Arena.

Failure scenario: GPU build, EB, redistribution_type=StateRedist or NoRedist (only AMReX's FluxRedist path ends with Gpu::streamSynchronize): hydro_umdrv_eb returns while hydro_consup_eb/ApplyMLRedistribution kernels are in flight; the freed The_Arena chunks are re-handed to the next MFIter iteration on a different GPU stream (MFIter::operator++ does not sync), whose setVal/kernels race with the in-flight readers -> corrupted dUdt. bcs_d in CAMR_construct_hydro_source.cpp:183 has the same lifetime hazard.

Lens 1 (refutation attempt): flux_tmp resize at line 107 and divc/redistwgt resizes at 86-90 pass no arena (The_Arena, no Elixir); sibling temporaries divu (line 69) and qec (line 75) explicitly use amrex::The_Async_Arena(), confirming the asymmetry. bcs_d at CAMR_construct_hydro_source.cpp:183 is a per-iteration Gpu::DeviceVector passed as bcs_d_ptr into MLStateRedistribute with the same lifetime hazard. Same defect as F006/F008/F015/F019; raised to high to match.


Based on commit 8f87fa8, which is also the tree the audit verified against. From an automated audit of Source/, Tutorials/ and Util/. Audit finding ids: F006, F008, F015, F019, F040. Reviewer unit(s): Hydro-drivers, Hydro-kernels, theme:box-bounds-args, theme:eb-smallcell, theme:gpu-capture. Nothing here was compiled or run — the failure scenarios are code reasoning, so the reaching configuration above is the cheapest way to confirm or refute it.

Activity

  1. added a commit that references this issue on Nov 17, 2026
    c3a3c56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions