core
Shared MPI block decomposition + asynchronous ghost-layer exchange (header-only C++20)
Loading...
Searching...
No Matches
morton_indexer.hpp
Go to the documentation of this file.
1// core — Morton / Z-order cell indexing (via the suite `morton` primitive).
2//
3// The complement to BlockIndexer's x-fastest linear order: MortonIndexer maps a
4// block's cells onto a Z-order (Morton) space-filling curve, so a cell's spatial
5// neighbourhood is near it in memory. This is the "morton/Z-order cell indexing
6// (via morton)" piece of the decomposition module described in
7// ../../../docs/ARCHITECTURE.md. The x-fastest convention in peclet/core/common/types.hpp
8// is unchanged — this is an *alternative* ordering for cache-friendly traversal /
9// spatial sorting, not a replacement.
10//
11// Header-only, guarded by PECLET_CORE_HAVE_MORTON (set by CMake when the morton sibling
12// checkout is present). Methods carry morton's MORTON_HD, so they are device-
13// callable exactly when morton is (KOKKOS_FUNCTION under a Kokkos build with
14// MORTON_ENABLE_KOKKOS, __host__ __device__ under nvcc/hipcc, nothing on host).
15#ifndef PECLET_CORE_DECOMP_MORTON_INDEXER_HPP
16#define PECLET_CORE_DECOMP_MORTON_INDEXER_HPP
17
18#ifdef PECLET_CORE_HAVE_MORTON
19
20#include "morton/morton.hpp"
22
23namespace peclet::core::decomp {
24
32template <int Dim, unsigned Bits = (Dim == 2 ? 32u : (Dim == 3 ? 21u : 16u))>
34 public:
35 using M = morton::Morton<Dim, Bits>;
36 using Code = typename M::code_type;
37 using Coord = typename M::coord_type;
38
39 MortonIndexer() = default;
41
43 origin_ = origin;
44 size_ = size;
45 }
46
47 static constexpr unsigned bits() { return Bits; }
48 const IVec<Dim>& origin() const { return origin_; }
49 const IVec<Dim>& size() const { return size_; }
50
53 MORTON_HD Code codeOf(const IVec<Dim>& g) const {
54 M m;
55 for (int i = 0; i < Dim; ++i)
56 m.set(static_cast<unsigned>(i), localCoord(g, i));
57 return m.code();
58 }
59
62 M m = M::from_code(c);
63 IVec<Dim> g{};
64 for (int i = 0; i < Dim; ++i)
65 g[i] = static_cast<Index>(m.get(static_cast<unsigned>(i))) + origin_[i];
66 return g;
67 }
68
72 MORTON_HD Code neighborCode(Code c, int axis, int dir) const {
73 return M::from_code(c).neighbor(static_cast<unsigned>(axis), dir >= 0 ? 1 : -1).code();
74 }
75
76 private:
77 MORTON_HD Coord localCoord(const IVec<Dim>& g, int i) const {
78 return static_cast<Coord>(g[i] - origin_[i]);
79 }
80
81 IVec<Dim> origin_{};
82 IVec<Dim> size_{};
83};
84
85} // namespace peclet::core::decomp
86
87#endif // PECLET_CORE_HAVE_MORTON
88#endif // PECLET_CORE_DECOMP_MORTON_INDEXER_HPP
#define MORTON_HD
Z-order (Morton) indexer over an axis-origin-relative cell block.
MORTON_HD Code codeOf(const IVec< Dim > &g) const
Global multi-index → Z-order code (origin-relative).
const IVec< Dim > & origin() const
void init(IVec< Dim > origin, IVec< Dim > size)
morton::Morton< Dim, Bits > M
const IVec< Dim > & size() const
MORTON_HD Code neighborCode(Code c, int axis, int dir) const
Neighbour code one cell along ±axis, computed directly in Morton space (the headline O(1) morton arit...
static constexpr unsigned bits()
MORTON_HD IVec< Dim > multiIndex(Code c) const
Z-order code → global multi-index (inverse of codeOf).
MortonIndexer(IVec< Dim > origin, IVec< Dim > size)
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