Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
TreeCellRanges.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
21
22namespace shamrock::tree {
23
24 template<class u_morton, class pos_t>
26
29 static constexpr u32 dim = 3;
30
31 public:
32 // this one is not used, it should be removed
33 std::unique_ptr<sycl::buffer<ipos_t>>
34 buf_pos_min_cell; // size = total count //rename to ipos
35 std::unique_ptr<sycl::buffer<ipos_t>> buf_pos_max_cell; // size = total count
36
37 // optional
38 std::unique_ptr<sycl::buffer<pos_t>>
39 buf_pos_min_cell_flt; // size = total count //drop the flt part
40 std::unique_ptr<sycl::buffer<pos_t>> buf_pos_max_cell_flt; // size = total count
41
42 void build1(
43 sycl::queue &queue,
44 TreeReducedMortonCodes<u_morton> &tree_reduced_morton_codes,
45 TreeStructure<u_morton> &tree_struct);
46
47 void build2(sycl::queue &queue, u32 total_count, std::tuple<pos_t, pos_t> bounding_box);
48
49 inline bool are_range_int_built() {
50 return bool(buf_pos_min_cell) && bool(buf_pos_max_cell);
51 }
52
53 inline bool are_range_float_built() {
54 return bool(buf_pos_min_cell_flt) && bool(buf_pos_max_cell_flt);
55 }
56
57 inline TreeCellRanges() = default;
58
59 TreeCellRanges(const TreeCellRanges &other);
60
61 inline TreeCellRanges &operator=(TreeCellRanges &&other) noexcept {
62 buf_pos_min_cell = std::move(other.buf_pos_min_cell);
63 buf_pos_max_cell = std::move(other.buf_pos_max_cell);
64 buf_pos_min_cell_flt = std::move(other.buf_pos_min_cell_flt);
65 buf_pos_max_cell_flt = std::move(other.buf_pos_max_cell_flt);
66
67 return *this;
68 } // move assignment
69
70 [[nodiscard]] inline u64 memsize() const {
71 u64 sum = 0;
72
73 auto add_ptr = [&](auto &a) {
74 if (a) {
75 sum += a->byte_size();
76 }
77 };
78
79 add_ptr(buf_pos_min_cell);
80 add_ptr(buf_pos_max_cell);
81 add_ptr(buf_pos_min_cell_flt);
82 add_ptr(buf_pos_max_cell_flt);
83
84 return sum;
85 }
86
87 bool operator==(const TreeCellRanges &rhs) const;
88
89 inline u32 get_total_tree_cell_count() {
90 if (buf_pos_min_cell) {
91 return buf_pos_min_cell->size();
92 } else if (buf_pos_min_cell_flt) {
93 return buf_pos_min_cell_flt->size();
94 } else {
96 "no buffers are allocated");
97 }
98 }
99
100 inline void serialize(shamalgs::SerializeHelper &serializer) {
101 StackEntry stack_loc{};
102 u32 state = (bool(buf_pos_min_cell) ? 1 : 0) + (bool(buf_pos_min_cell_flt) ? 1 : 0) * 2;
103
104 serializer.write(state);
105
106 if (state == 1) {
107 u32 sz = buf_pos_min_cell->size();
108 serializer.write(sz);
109 serializer.write_buf(*buf_pos_min_cell, sz);
110 serializer.write_buf(*buf_pos_max_cell, sz);
111 } else if (state == 2) {
112 u32 sz = buf_pos_min_cell_flt->size();
113 serializer.write(sz);
114 serializer.write_buf(*buf_pos_min_cell_flt, sz);
115 serializer.write_buf(*buf_pos_max_cell_flt, sz);
116 } else if (state == 3) {
117 u32 sz = buf_pos_min_cell->size();
118 serializer.write(sz);
119 serializer.write_buf(*buf_pos_min_cell, sz);
120 serializer.write_buf(*buf_pos_max_cell, sz);
121 serializer.write_buf(*buf_pos_min_cell_flt, sz);
122 serializer.write_buf(*buf_pos_max_cell_flt, sz);
123 }
124 }
125
126 inline shamalgs::SerializeSize serialize_byte_size() {
127
129
130 shamalgs::SerializeSize sum = H::serialize_byte_size<u32>();
131
132 u32 state = (bool(buf_pos_min_cell) ? 1 : 0) + (bool(buf_pos_min_cell_flt) ? 1 : 0) * 2;
133
134 if (state == 1) {
135 u32 sz = buf_pos_min_cell->size();
136 sum += H::serialize_byte_size<u32>();
137 sum += H::serialize_byte_size<ipos_t>(sz);
138 sum += H::serialize_byte_size<ipos_t>(sz);
139 } else if (state == 2) {
140 u32 sz = buf_pos_min_cell_flt->size();
141 sum += H::serialize_byte_size<u32>();
142 sum += H::serialize_byte_size<pos_t>(sz);
143 sum += H::serialize_byte_size<pos_t>(sz);
144 } else if (state == 3) {
145 u32 sz = buf_pos_min_cell->size();
146 sum += H::serialize_byte_size<u32>();
147 sum += H::serialize_byte_size<ipos_t>(sz);
148 sum += H::serialize_byte_size<ipos_t>(sz);
149 sum += H::serialize_byte_size<pos_t>(sz);
150 sum += H::serialize_byte_size<pos_t>(sz);
151 }
152
153 return sum;
154 }
155
156 inline static TreeCellRanges deserialize(shamalgs::SerializeHelper &serializer) {
157 StackEntry stack_loc{};
158
159 TreeCellRanges ret;
160
161 u32 state;
162 serializer.load(state);
163
164 if (state == 1) {
165 u32 sz;
166 serializer.load(sz);
167 ret.buf_pos_min_cell = std::make_unique<sycl::buffer<ipos_t>>(sz);
168 ret.buf_pos_max_cell = std::make_unique<sycl::buffer<ipos_t>>(sz);
169 serializer.load_buf(*ret.buf_pos_min_cell, sz);
170 serializer.load_buf(*ret.buf_pos_max_cell, sz);
171 } else if (state == 2) {
172 u32 sz;
173 serializer.load(sz);
174 ret.buf_pos_min_cell_flt = std::make_unique<sycl::buffer<pos_t>>(sz);
175 ret.buf_pos_max_cell_flt = std::make_unique<sycl::buffer<pos_t>>(sz);
176 serializer.load_buf(*ret.buf_pos_min_cell_flt, sz);
177 serializer.load_buf(*ret.buf_pos_max_cell_flt, sz);
178 } else if (state == 3) {
179 u32 sz;
180 serializer.load(sz);
181 ret.buf_pos_min_cell = std::make_unique<sycl::buffer<ipos_t>>(sz);
182 ret.buf_pos_max_cell = std::make_unique<sycl::buffer<ipos_t>>(sz);
183 ret.buf_pos_min_cell_flt = std::make_unique<sycl::buffer<pos_t>>(sz);
184 ret.buf_pos_max_cell_flt = std::make_unique<sycl::buffer<pos_t>>(sz);
185 serializer.load_buf(*ret.buf_pos_min_cell, sz);
186 serializer.load_buf(*ret.buf_pos_max_cell, sz);
187 serializer.load_buf(*ret.buf_pos_min_cell_flt, sz);
188 serializer.load_buf(*ret.buf_pos_max_cell_flt, sz);
189 }
190
191 return ret;
192 }
193 };
194
195} // namespace shamrock::tree
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
Morton curve implementation.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.