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

adc_cpp: include/pops/mesh/storage/fab2d.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
fab2d.hpp
Go to the documentation of this file.
1
13
14#pragma once
15
19
20#include <cassert>
21#include <cstdint>
22#include <vector>
23
24namespace pops {
25
29struct Array4 {
30 Real* p{nullptr};
31 int nx_tot{0};
32 std::int64_t comp_stride{0};
33 int ig0{0}, jg0{0}; // global indices of the lower corner of the grown box
34
37 POPS_HD Real& operator()(int i, int j, int c = 0) const {
38 return p[c * comp_stride + static_cast<std::int64_t>(j - jg0) * nx_tot + (i - ig0)];
39 }
40};
41
45 const Real* p{nullptr};
46 int nx_tot{0};
47 std::int64_t comp_stride{0};
48 int ig0{0}, jg0{0};
49
51 POPS_HD Real operator()(int i, int j, int c = 0) const {
52 return p[c * comp_stride + static_cast<std::int64_t>(j - jg0) * nx_tot + (i - ig0)];
53 }
54};
55
59class Fab2D {
60 public:
61 Fab2D() = default;
62
64 Fab2D(const Box2D& valid, int ncomp, int ng)
65 : valid_(valid),
66 ng_(ng),
67 ncomp_(ncomp),
68 gbox_(valid.grow(ng)),
69 nx_tot_(gbox_.nx()),
70 ny_tot_(gbox_.ny()),
71 data_(static_cast<std::int64_t>(nx_tot_) * ny_tot_ * ncomp, Real{0}) {}
72
74 const Box2D& box() const { return valid_; }
76 const Box2D& grown_box() const { return gbox_; }
78 int ncomp() const { return ncomp_; }
80 int n_ghost() const { return ng_; }
82 std::int64_t size() const { return static_cast<std::int64_t>(data_.size()); }
83
86 Real& operator()(int i, int j, int c = 0) { return data_[idx(i, j, c)]; }
88 Real operator()(int i, int j, int c = 0) const { return data_[idx(i, j, c)]; }
89
92 return Array4{data_.data(), nx_tot_, static_cast<std::int64_t>(nx_tot_) * ny_tot_, gbox_.lo[0],
93 gbox_.lo[1]};
94 }
97 return ConstArray4{data_.data(), nx_tot_, static_cast<std::int64_t>(nx_tot_) * ny_tot_,
98 gbox_.lo[0], gbox_.lo[1]};
99 }
100
102 Real* data() { return data_.data(); }
103 const Real* data() const { return data_.data(); }
105 void set_val(Real v) { std::fill(data_.begin(), data_.end(), v); }
106
107 private:
108 // linear index (i, j, c) in the component-slow layout; bounds assert in debug.
109 std::int64_t idx(int i, int j, int c) const {
110 assert(gbox_.contains(i, j) && c >= 0 && c < ncomp_);
111 return c * static_cast<std::int64_t>(nx_tot_) * ny_tot_ +
112 static_cast<std::int64_t>(j - gbox_.lo[1]) * nx_tot_ + (i - gbox_.lo[0]);
113 }
114
115 Box2D valid_{};
116 int ng_{0};
117 int ncomp_{1};
118 Box2D gbox_{};
119 int nx_tot_{0}, ny_tot_{0};
120 // storage: host (std::allocator) or CUDA unified memory (cf. allocator.hpp).
121 std::vector<Real, fab_allocator<Real>> data_{};
122};
123
124} // namespace pops
Fab2D storage allocator, selectable at compile time.
Box2D: the integer index space of a 2D cell-centered Cartesian grid.
Single-grid data on a Box2D: VALID box + ng ghost layers, ncomp components, component-slow layout.
Definition fab2d.hpp:59
void set_val(Real v)
Fills the whole buffer (valid + ghosts) with value v.
Definition fab2d.hpp:105
const Box2D & grown_box() const
Grown box (valid + ng ghosts) = actual memory footprint.
Definition fab2d.hpp:76
const Real * data() const
Definition fab2d.hpp:103
const Box2D & box() const
VALID box (without ghosts).
Definition fab2d.hpp:74
Fab2D(const Box2D &valid, int ncomp, int ng)
Allocates the valid box grown by ng ghosts, ncomp components, initialized to 0.
Definition fab2d.hpp:64
int n_ghost() const
Number of ghost layers.
Definition fab2d.hpp:80
Real & operator()(int i, int j, int c=0)
HOST write access (i, j, c) (bounds assert in debug).
Definition fab2d.hpp:86
Fab2D()=default
ConstArray4 const_array() const
READ handle (POD device-copyable) over this Fab. Valid as long as the Fab lives.
Definition fab2d.hpp:96
int ncomp() const
Number of components.
Definition fab2d.hpp:78
Array4 array()
WRITE handle (POD device-copyable) over this Fab. Valid as long as the Fab lives.
Definition fab2d.hpp:91
std::int64_t size() const
Buffer size (nx_tot * ny_tot * ncomp).
Definition fab2d.hpp:82
Real * data()
Raw pointer to the buffer (passed directly to MPI in unified memory, for instance).
Definition fab2d.hpp:102
Real operator()(int i, int j, int c=0) const
HOST read access (i, j, c) (bounds assert in debug).
Definition fab2d.hpp:88
Definition amr_hierarchy.hpp:29
double Real
Definition types.hpp:30
WRITE POD handle (raw pointer + strides) over a Fab2D buffer, indexed by (i, j, c) IN GLOBAL INDICES ...
Definition fab2d.hpp:29
std::int64_t comp_stride
Definition fab2d.hpp:32
int nx_tot
Definition fab2d.hpp:31
Real * p
Definition fab2d.hpp:30
POPS_HD Real & operator()(int i, int j, int c=0) const
Reference to cell (i, j) of component c (global indices).
Definition fab2d.hpp:37
int ig0
Definition fab2d.hpp:33
int jg0
Definition fab2d.hpp:33
2D integer index space, cell-centered.
Definition box2d.hpp:37
bool contains(int i, int j) const
true if cell (i, j) is inside the box (lo/hi bounds inclusive).
Definition box2d.hpp:61
int lo[2]
Definition box2d.hpp:38
READ-only handle (const counterpart of Array4): same layout and same contract (POD device-copyable,...
Definition fab2d.hpp:44
std::int64_t comp_stride
Definition fab2d.hpp:47
int ig0
Definition fab2d.hpp:48
int nx_tot
Definition fab2d.hpp:46
int jg0
Definition fab2d.hpp:48
const Real * p
Definition fab2d.hpp:45
POPS_HD Real operator()(int i, int j, int c=0) const
Value of cell (i, j) of component c (global indices). POPS_HD, no bounds checking.
Definition fab2d.hpp:51
Base scalar types and the POPS_HD macro (host+device portability).
#define POPS_HD
Definition types.hpp:25