Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
TreeStructure.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
19
20namespace shamrock::tree {
21
22 template<class u_morton>
24
25 public:
26 u32 internal_cell_count;
27 bool one_cell_mode = false;
28
29 std::unique_ptr<sycl::buffer<u32>> buf_lchild_id; // size = internal
30 std::unique_ptr<sycl::buffer<u32>> buf_rchild_id; // size = internal
31 std::unique_ptr<sycl::buffer<u8>> buf_lchild_flag; // size = internal
32 std::unique_ptr<sycl::buffer<u8>> buf_rchild_flag; // size = internal
33 std::unique_ptr<sycl::buffer<u32>> buf_endrange; // size = internal
34
35 bool is_built() {
36 return bool(buf_lchild_id) && bool(buf_rchild_id) && bool(buf_lchild_flag)
37 && bool(buf_rchild_flag) && bool(buf_endrange);
38 }
39
40 void build(
41 sycl::queue &queue, u32 _internal_cell_count, sycl::buffer<u_morton> &morton_buf);
42
43 void build_one_cell_mode();
44
45 [[nodiscard]] inline u64 memsize() const {
46 u64 sum = 0;
47 sum += sizeof(internal_cell_count);
48 sum += sizeof(one_cell_mode);
49
50 auto add_ptr = [&](auto &a) {
51 if (a) {
52 sum += a->byte_size();
53 }
54 };
55
56 add_ptr(buf_lchild_id);
57 add_ptr(buf_rchild_id);
58 add_ptr(buf_lchild_flag);
59 add_ptr(buf_rchild_flag);
60 add_ptr(buf_endrange);
61
62 return sum;
63 }
64
65 bool operator==(const TreeStructure &rhs) const;
66
67 inline TreeStructure() = default;
68
69 TreeStructure(const TreeStructure &other);
70
71 inline TreeStructure &operator=(TreeStructure &&other) noexcept {
72 internal_cell_count = std::move(other.internal_cell_count);
73 one_cell_mode = std::move(other.one_cell_mode);
74 buf_lchild_id = std::move(other.buf_lchild_id);
75 buf_rchild_id = std::move(other.buf_rchild_id);
76 buf_lchild_flag = std::move(other.buf_lchild_flag);
77 buf_rchild_flag = std::move(other.buf_rchild_flag);
78 buf_endrange = std::move(other.buf_endrange);
79
80 return *this;
81 } // move assignment
82
83 inline TreeStructure(
84 u32 internal_cell_count,
85 bool one_cell_mode,
86 std::unique_ptr<sycl::buffer<u32>> &&buf_lchild_id,
87 std::unique_ptr<sycl::buffer<u32>> &&buf_rchild_id,
88 std::unique_ptr<sycl::buffer<u8>> &&buf_lchild_flag,
89 std::unique_ptr<sycl::buffer<u8>> &&buf_rchild_flag,
90 std::unique_ptr<sycl::buffer<u32>> &&buf_endrange)
91 : internal_cell_count(internal_cell_count), one_cell_mode(one_cell_mode),
92 buf_lchild_id(std::move(buf_lchild_id)), buf_rchild_id(std::move(buf_rchild_id)),
93 buf_lchild_flag(std::move(buf_lchild_flag)),
94 buf_rchild_flag(std::move(buf_rchild_flag)), buf_endrange(std::move(buf_endrange)) {}
95
96 inline void serialize(shamalgs::SerializeHelper &serializer) {
97 StackEntry stack_loc{};
98 serializer.write(internal_cell_count);
99 serializer.write((one_cell_mode) ? 1_u32 : 0_u32);
100 serializer.write_buf(*buf_lchild_id, internal_cell_count);
101 serializer.write_buf(*buf_rchild_id, internal_cell_count);
102 serializer.write_buf(*buf_lchild_flag, internal_cell_count);
103 serializer.write_buf(*buf_rchild_flag, internal_cell_count);
104 serializer.write_buf(*buf_endrange, internal_cell_count);
105 }
106
107 inline shamalgs::SerializeSize serialize_byte_size() {
108
110
111 return H::serialize_byte_size<u32>() + H::serialize_byte_size<u32>()
112 + H::serialize_byte_size<u32>(internal_cell_count)
113 + H::serialize_byte_size<u32>(internal_cell_count)
114 + H::serialize_byte_size<u8>(internal_cell_count)
115 + H::serialize_byte_size<u8>(internal_cell_count)
116 + H::serialize_byte_size<u32>(internal_cell_count);
117 }
118
119 inline static TreeStructure deserialize(shamalgs::SerializeHelper &serializer) {
120 StackEntry stack_loc{};
121 TreeStructure strc;
122
123 serializer.load(strc.internal_cell_count);
124 u32 one_cell;
125
126 serializer.load(one_cell);
127 strc.one_cell_mode = (one_cell == 1);
128
129 strc.buf_lchild_id = std::make_unique<sycl::buffer<u32>>(strc.internal_cell_count);
130 strc.buf_rchild_id = std::make_unique<sycl::buffer<u32>>(strc.internal_cell_count);
131 strc.buf_lchild_flag = std::make_unique<sycl::buffer<u8>>(strc.internal_cell_count);
132 strc.buf_rchild_flag = std::make_unique<sycl::buffer<u8>>(strc.internal_cell_count);
133 strc.buf_endrange = std::make_unique<sycl::buffer<u32>>(strc.internal_cell_count);
134
135 serializer.load_buf(*strc.buf_lchild_id, strc.internal_cell_count);
136 serializer.load_buf(*strc.buf_rchild_id, strc.internal_cell_count);
137 serializer.load_buf(*strc.buf_lchild_flag, strc.internal_cell_count);
138 serializer.load_buf(*strc.buf_rchild_flag, strc.internal_cell_count);
139 serializer.load_buf(*strc.buf_endrange, strc.internal_cell_count);
140
141 return strc;
142 }
143 };
144
145} // namespace shamrock::tree
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer