core
Shared MPI block decomposition + asynchronous ghost-layer exchange (header-only C++20)
Loading...
Searching...
No Matches
block_octree_view.hpp
Go to the documentation of this file.
1// core — device-resident view of a BlockOctree (portable Kokkos).
2//
3// BlockOctree (block_octree.hpp) owns the topology on the host: refine / coarsen /
4// 2:1 balance rebuild its sorted leaf arrays. BlockOctreeView mirrors those
5// arrays into Kokkos Views and exposes the *queries* (point location, and the
6// face-neighbour step) as KOKKOS_INLINE_FUNCTIONs over the same code, so field
7// kernels and the (later) octree multigrid can locate / walk leaves on device on
8// any backend (CUDA / HIP / OpenMP). This mirrors the host-vs-device split the
9// halo already uses (grid_halo.hpp vs grid_halo_kokkos.hpp): topology on host,
10// the per-leaf hot path on device, bit-for-bit identical results.
11//
12// Including this header requires Kokkos (build with -DPECLET_CORE_ENABLE_KOKKOS=ON) and
13// the morton sibling checkout (PECLET_CORE_HAVE_MORTON, with MORTON_ENABLE_KOKKOS so
14// MORTON_HD == KOKKOS_FUNCTION).
15#ifndef PECLET_CORE_AMR_BLOCK_OCTREE_VIEW_HPP
16#define PECLET_CORE_AMR_BLOCK_OCTREE_VIEW_HPP
17
18#ifdef PECLET_CORE_HAVE_MORTON
19
20#include <cstdint>
21
24
25namespace peclet::core::amr {
26
28template <int Dim, unsigned Bits = (Dim == 2 ? 32u : (Dim == 3 ? 21u : 16u))>
31 using M = typename Host::M;
32 using Code = typename Host::Code;
33 using Coord = typename Host::Coord;
34
35 View<Code> codes; // leaf origin codes, ascending (Z-order)
36 View<std::uint8_t> levels; // parallel to codes
37 Index n = 0;
38
40 void upload(const Host& t) {
41 codes = toDevice(t.codes(), "amr_codes");
42 levels = toDevice(t.levels(), "amr_levels");
43 n = t.numLeaves();
44 }
45
46 Index numLeaves() const { return n; }
47
50 return amrLocate<M>(codes.data(), levels.data(), n, p);
51 }
52
55 KOKKOS_INLINE_FUNCTION Index faceNeighbor(Index i, int axis, int dir) const {
56 M probe = M::from_code(codes(i));
57 const Coord step = Coord(Coord(1) << levels(i));
58 if (dir >= 0) {
59 if (!probe.try_add(static_cast<unsigned>(axis), step))
60 return -1;
61 } else {
62 if (!probe.try_sub(static_cast<unsigned>(axis), 1))
63 return -1;
64 }
65 return locate(probe.code());
66 }
67};
68
69} // namespace peclet::core::amr
70
71#endif // PECLET_CORE_HAVE_MORTON
72#endif // PECLET_CORE_AMR_BLOCK_OCTREE_VIEW_HPP
View< T > toDevice(const std::vector< T > &h, const std::string &label)
Upload a host std::vector into a freshly-sized device View (empty vector => empty view).
Definition view.hpp:44
Kokkos::View< T *, MemSpace > View
1D device array.
Definition view.hpp:26
std::int64_t Index
Signed index type for grids and particles (supersedes block_decomposer's long int IndxT).
Definition types.hpp:15
Device mirror of a BlockOctree's leaf arrays + device-callable queries.
void upload(const Host &t)
(Re)upload the host octree's current leaf set to the device.
Index faceNeighbor(Index i, int axis, int dir) const
Leaf across leaf i's face on axis in direction dir (±1), or -1 if it lies outside the block.
Index locate(Code p) const
Leaf containing Morton code p, or -1. Callable in a device kernel.