include/pops/mesh/storage/multifab.hpp Source FileΒΆ

adc_cpp: include/pops/mesh/storage/multifab.hpp Source File
adc_cpp 0.3.0
Model-free C++23 core for coupled hyperbolic-elliptic systems on adaptive (AMR) meshes, with MPI and GPU (Kokkos) backends
multifab.hpp
Go to the documentation of this file.
1
12
13#pragma once
14
20#include <pops/mesh/execution/for_each.hpp> // device_fence, sync_host, sync_device
21#include <pops/mesh/boundary/halo_schedule.hpp> // memoized fill_boundary schedule (ADC-260)
23
24#include <memory>
25#include <utility>
26#include <vector>
27
28namespace pops {
29
33class MultiFab {
34 public:
35 MultiFab() = default;
36
40 : ba_(std::move(ba)),
41 dm_(std::move(dm)),
42 ncomp_(ncomp),
43 ngrow_(ngrow),
44 local_index_(ba_.size(), -1) {
45 const int me = my_rank();
46 for (int i = 0; i < ba_.size(); ++i) {
47 if (dm_[i] == me) {
48 local_index_[i] = static_cast<int>(fabs_.size());
49 global_of_local_.push_back(i);
50 fabs_.emplace_back(ba_[i], ncomp_, ngrow_);
51 }
52 }
53 }
54
56 const BoxArray& box_array() const { return ba_; }
58 const DistributionMapping& dmap() const { return dm_; }
60 int ncomp() const { return ncomp_; }
62 int n_grow() const { return ngrow_; }
63
65 int local_size() const { return static_cast<int>(fabs_.size()); }
67 Fab2D& fab(int li) { return fabs_[li]; }
69 const Fab2D& fab(int li) const { return fabs_[li]; }
71 const Box2D& box(int li) const { return fabs_[li].box(); }
73 int global_index(int li) const { return global_of_local_[li]; }
75 int local_index_of(int global) const { return local_index_[global]; }
76
82
85 void set_val(Real v) {
86 sync_host(); // a kernel may have written these fabs; make the host residence
87 // valid before the host fill (otherwise a host/kernel write
88 // race). Under unified memory = a device_fence().
89 for (auto& f : fabs_)
90 f.set_val(v);
91 }
92
101 if (!halo_cache_)
102 halo_cache_ = std::make_shared<HaloScheduleCache>();
103 return *halo_cache_;
104 }
105
106 private:
107 BoxArray ba_{};
108 DistributionMapping dm_{};
109 int ncomp_{1};
110 int ngrow_{0};
111 std::vector<Fab2D> fabs_{}; // locally owned fabs
112 std::vector<int> local_index_{}; // global box -> local index (-1 otherwise)
113 std::vector<int> global_of_local_{}; // local index -> global box
114 // Memoized fill_boundary schedule (ADC-260). mutable: caching is logically const; lazily built.
115 mutable std::shared_ptr<HaloScheduleCache> halo_cache_{};
116};
117
121inline Real sum(const MultiFab& mf, int comp = 0) {
122 Real s = 0;
123 for (int li = 0; li < mf.local_size(); ++li) {
124 const ConstArray4 a = mf.fab(li).const_array();
125 s += for_each_cell_reduce_sum(mf.box(li),
126 [a, comp] POPS_HD(int i, int j) { return a(i, j, comp); });
127 }
128 return static_cast<Real>(all_reduce_sum(static_cast<double>(s)));
129}
130
131} // namespace pops
Box2D: the integer index space of a 2D cell-centered Cartesian grid.
BoxArray: the set of boxes tiling a level (disjoint, covering).
Ordered list of boxes tiling a level.
Definition box_array.hpp:22
int size() const
Number of boxes in the tiling.
Definition box_array.hpp:42
Owning MPI rank of each box, indexed by GLOBAL box index (parallel to a BoxArray).
Definition distribution_mapping.hpp:19
Single-grid data on a Box2D: VALID box + ng ghost layers, ncomp components, component-slow layout.
Definition fab2d.hpp:59
ConstArray4 const_array() const
READ handle (POD device-copyable) over this Fab. Valid as long as the Fab lives.
Definition fab2d.hpp:96
Small per-MultiFab cache of halo schedules, one entry per distinct (Periodicity, domain).
Definition halo_schedule.hpp:57
Field distributed over a level: decomposition (BoxArray) + distribution (DistributionMapping) + ncomp...
Definition multifab.hpp:33
MultiFab()=default
Fab2D & fab(int li)
Local fab at index li (0 <= li < local_size()), for writing.
Definition multifab.hpp:67
int local_index_of(int global) const
LOCAL index of the global box global, or -1 if it is not owned by this rank.
Definition multifab.hpp:75
int global_index(int li) const
GLOBAL index (in box_array) of local fab li.
Definition multifab.hpp:73
int ncomp() const
Number of components.
Definition multifab.hpp:60
HaloScheduleCache & halo_cache() const
Internal (ADC-260): memoized halo-exchange schedule used by fill_boundary.
Definition multifab.hpp:100
void sync_host()
Makes the HOST residence valid (before a host access: operator(), loop, set_val).
Definition multifab.hpp:79
void sync_device()
Marks a DEVICE residence (before a kernel). No-op under unified memory.
Definition multifab.hpp:81
const Fab2D & fab(int li) const
Local fab at index li, for reading.
Definition multifab.hpp:69
const DistributionMapping & dmap() const
GLOBAL distribution (owner rank per box).
Definition multifab.hpp:58
int n_grow() const
Number of ghost layers.
Definition multifab.hpp:62
const BoxArray & box_array() const
GLOBAL decomposition of the level (all boxes, all ranks).
Definition multifab.hpp:56
const Box2D & box(int li) const
VALID box of local fab li.
Definition multifab.hpp:71
MultiFab(BoxArray ba, DistributionMapping dm, int ncomp, int ngrow)
Builds the field: allocates one Fab2D (ncomp components, ngrow ghosts) for EACH box that this rank ow...
Definition multifab.hpp:39
void set_val(Real v)
Fills all cells (valid + ghosts) of every local fab with v.
Definition multifab.hpp:85
int local_size() const
Number of fabs OWNED by this rank (bound on local indices).
Definition multifab.hpp:65
Parallel seam: minimal MPI abstraction (rank/size + collectives) with serial fallback.
DistributionMapping: maps each box (by global index) to its owning MPI rank.
Fab2D: single-grid data on a Box2D (in-house equivalent of AMReX's FArrayBox); Array4 / ConstArray4: ...
for_each_cell and reductions: the parallelism SEAM over the cells of a Box2D; sync_host / sync_device...
HaloSchedule: memoized intra-level halo-exchange plan for fill_boundary (ADC-260).
Definition amr_hierarchy.hpp:29
Real for_each_cell_reduce_sum(const Box2D &b, F f)
SUM reduction of f(i, j) over box b.
Definition for_each.hpp:199
double Real
Definition types.hpp:30
double all_reduce_sum(double x)
Definition comm.hpp:143
void sync_host()
Makes the HOST residency valid before a host access (read/write from the host).
Definition for_each.hpp:125
int my_rank()
Definition comm.hpp:136
Real sum(const MultiFab &mf, int comp=0)
Sum of the VALID cells of component comp, reduced over ALL ranks (all_reduce).
Definition multifab.hpp:121
void sync_device()
Marks a DEVICE residency (upcoming kernel).
Definition for_each.hpp:132
2D integer index space, cell-centered.
Definition box2d.hpp:37
READ-only handle (const counterpart of Array4): same layout and same contract (POD device-copyable,...
Definition fab2d.hpp:44
Base scalar types and the POPS_HD macro (host+device portability).
#define POPS_HD
Definition types.hpp:25