ERF
Energy Research and Forecasting: An Atmospheric Modeling Code
ERF_HostFabView.H File Reference

Host-readable view of one FAB of a MultiFab. More...

#include <AMReX_MultiFab.H>
#include <AMReX_FArrayBox.H>
#include <AMReX_Gpu.H>
Include dependency graph for ERF_HostFabView.H:

Go to the source code of this file.

Classes

struct  ERFHostFabView
 

Detailed Description

Host-readable view of one FAB of a MultiFab.

AMReX leaves the_arena_is_managed at false by default, so under CUDA a MultiFab's data lives in device memory and an Array4 obtained from it cannot be dereferenced on the host at all – a plain for loop or amrex::LoopOnCpu over it faults, or silently reads garbage if the address happens to be mapped. Gpu::streamSynchronize() does not help: it orders kernels, it does not migrate memory.

Most of the time the answer is to do the work in a kernel. This helper is for the cases where it genuinely cannot be: serial trajectory integration with a host RNG, scatter-accumulation into named bins, and gathering a field into a flat host vector ahead of an MPI reduce or a file write.

On CPU builds the view is a plain alias for the device FAB, so host runs pay nothing for it.

Typical use:

for (MFIter mfi(mf); mfi.isValid(); ++mfi) {
const ERFHostFabView view(mf[mfi]);
auto arr = view.array();
amrex::LoopOnCpu(mfi.validbox(), [&](int i, int j, int k) {
total += arr(i,j,k);
});
}
Definition: ERF_HostFabView.H:39