ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_ShocGpuUtils.H
Go to the documentation of this file.
1 #ifndef ERF_SHOC_GPU_UTILS_H_
2 #define ERF_SHOC_GPU_UTILS_H_
3 
4 #include <AMReX_FArrayBox.H>
5 #include <AMReX_Gpu.H>
6 #include <AMReX_REAL.H>
7 
8 namespace shoc {
9 
10 enum class InitRunOn {
11  Host,
12  Device
13 };
14 
15 // SHOC GPU portability rules:
16 // 1. ShocColumnData, FArrayBox, and MultiFab are host-side ownership objects.
17 // 2. Device-callable functions must take Array4 views, POD structs, scalars, and options only.
18 // 3. Device-callable functions must not call .array(), .const_array(), .box(), .nComp(), resize(), or setVal().
19 // 4. Do not capture this in AMReX GPU lambdas.
20 // 5. Do not reference anonymous-namespace constexpr Real constants inside GPU lambdas.
21 // 6. Use SHOC constants through AMREX_GPU_HOST_DEVICE inline accessors.
22 // 7. Use amrex::min, amrex::max, amrex::Math::abs, and explicit arithmetic in device paths.
23 // 8. Use a central SHOC FArrayBox initialization helper, not direct FArrayBox::setVal calls.
24 // 9. Unit tests must allocate column data in backend-safe memory and synchronize before host assertions.
25 
26 inline InitRunOn
28 {
29 #ifdef AMREX_USE_GPU
30  return InitRunOn::Device;
31 #else
32  return InitRunOn::Host;
33 #endif
34 }
35 
36 inline void
37 set_fab_val (amrex::FArrayBox& fab,
39  InitRunOn /*run_on*/)
40 {
41  fab.template setVal<amrex::RunOn::Device>(value);
42 }
43 
44 AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
46 height_agl (amrex::Real z, amrex::Real z_sfc) noexcept
47 {
48  return amrex::max(amrex::Real(0.0), z - z_sfc);
49 }
50 
51 inline void
53 {
54  // Test-owned helper for host-side assertions. Production SHOC code should
55  // keep synchronization out of the runtime path and use this only through
56  // shoc_test::run_and_sync or other explicit test utilities.
57 #ifdef AMREX_USE_GPU
58  amrex::Gpu::streamSynchronize();
59 #endif
60 }
61 
62 } // namespace shoc
63 
64 #endif
amrex::Real value
Definition: ERF_HurricaneDiagnostics.H:20
amrex::Real Real
Definition: ERF_ShocInterface.H:19
Definition: ERF_ShocConstants.H:7
InitRunOn
Definition: ERF_ShocGpuUtils.H:10
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE amrex::Real height_agl(amrex::Real z, amrex::Real z_sfc) noexcept
Definition: ERF_ShocGpuUtils.H:46
void sync_if_needed()
Definition: ERF_ShocGpuUtils.H:52
void set_fab_val(amrex::FArrayBox &fab, amrex::Real value, InitRunOn)
Definition: ERF_ShocGpuUtils.H:37
InitRunOn default_init_run_on()
Definition: ERF_ShocGpuUtils.H:27