Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
compute_neigh_graph.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
18
23#include "shammath/AABB.hpp"
27
28namespace shammodels::basegodunov::modules::details {
29
41 template<class NeighFindKernel, class... Args>
43 const sham::DeviceScheduler_ptr &dev_sched, u32 graph_nodes, Args &&...args) {
44
45 auto &q = dev_sched->get_queue();
46
47 NeighFindKernel kergen(std::forward<Args>(args)...);
48
49 // [i] is the number of link for block i in mpdat (last value is 0)
50 sham::DeviceBuffer<u32> link_counts(graph_nodes + 1, dev_sched);
51
52 // sham::kernel_call throws on an empty range, whereas the former
53 // shambase::parallel_for was a no-op, hence the guards
54 if (graph_nodes > 0) {
55 // fill buffer with number of link in the block graph
57 q,
58 sham::MultiRef{kergen},
59 sham::MultiRef{link_counts},
60 graph_nodes,
61 [](u32 id_a, const auto ker, u32 *__restrict ptr_link_cnt) {
62 u32 block_found_count = 0;
63
64 ker.for_each_other_index(id_a, [&](u32 id_b) {
65 block_found_count++;
66 });
67
68 ptr_link_cnt[id_a] = block_found_count;
69 });
70 }
71
72 // set the last val to 0 so that the last slot after exclusive scan is the sum
73 link_counts.set_val_at_idx(graph_nodes, 0);
74
75 sham::DeviceBuffer<u32> link_cnt_offsets
76 = shamalgs::numeric::scan_exclusive(dev_sched, link_counts, graph_nodes + 1);
77
78 u32 link_cnt = link_cnt_offsets.get_val_at_idx(graph_nodes);
79
80 sham::DeviceBuffer<u32> ids_links(link_cnt, dev_sched);
81
82 if (graph_nodes > 0) {
83 // find the neigh ids
85 q,
86 sham::MultiRef{link_cnt_offsets, kergen},
87 sham::MultiRef{ids_links},
88 graph_nodes,
89 [](u32 id_a,
90 const u32 *__restrict cnt_offsets,
91 const auto ker2,
92 u32 *__restrict links) {
93 u32 next_link_idx = cnt_offsets[id_a];
94
95 ker2.for_each_other_index(id_a, [&](u32 id_b) {
96 links[next_link_idx] = id_b;
97 next_link_idx++;
98 });
99 });
100 }
101
103 return Graph(
104 Graph{
105 .node_link_offset = std::move(link_cnt_offsets),
106 .node_links = std::move(ids_links),
107 .link_count = link_cnt,
108 .obj_cnt = graph_nodes});
109 };
110
124 template<class NeighFindKernel, class... Args>
126 const sham::DeviceScheduler_ptr &dev_sched, u32 graph_nodes, Args &&...args) {
127
128 auto &q = dev_sched->get_queue();
129
130 // [i] is the number of link for block i in mpdat (last value is 0)
131 sham::DeviceBuffer<u32> link_counts(graph_nodes + 1, dev_sched);
132
133 sham::EventList deps;
134 auto ptr_link_cnt = link_counts.get_write_access(deps);
135
136 // fill buffer with number of link in the block graph
137 auto e = q.submit(deps, [&](sycl::handler &cgh) {
138 NeighFindKernel ker(cgh, std::forward<Args>(args)...);
139 shambase::parallel_for(cgh, graph_nodes, "count block graph link", [=](u64 gid) {
140 u32 id_a = (u32) gid;
141 u32 block_found_count = 0;
142
143 ker.for_each_other_index(id_a, [&](u32 id_b) {
144 block_found_count++;
145 });
146
147 ptr_link_cnt[id_a] = block_found_count;
148 });
149 });
150
151 link_counts.complete_event_state(e);
152
153 // set the last val to 0 so that the last slot after exclusive scan is the sum
154 link_counts.set_val_at_idx(graph_nodes, 0);
155
156 sham::DeviceBuffer<u32> link_cnt_offsets
157 = shamalgs::numeric::scan_exclusive(dev_sched, link_counts, graph_nodes + 1);
158
159 u32 link_cnt = link_cnt_offsets.get_val_at_idx(graph_nodes);
160
161 sham::DeviceBuffer<u32> ids_links(link_cnt, dev_sched);
162
163 sham::EventList deps2;
164 auto cnt_offsets = link_cnt_offsets.get_read_access(deps2);
165 auto links = ids_links.get_write_access(deps2);
166
167 // find the neigh ids
168 auto e2 = q.submit(deps2, [&](sycl::handler &cgh) {
169 NeighFindKernel ker(cgh, std::forward<Args>(args)...);
170 shambase::parallel_for(cgh, graph_nodes, "get ids block graph link", [=](u64 gid) {
171 u32 id_a = (u32) gid;
172
173 u32 next_link_idx = cnt_offsets[id_a];
174
175 ker.for_each_other_index(id_a, [&](u32 id_b) {
176 links[next_link_idx] = id_b;
177 next_link_idx++;
178 });
179 });
180 });
181
182 link_cnt_offsets.complete_event_state(e2);
183 ids_links.complete_event_state(e2);
184
186 return Graph(
187 Graph{
188 .node_link_offset = std::move(link_cnt_offsets),
189 .node_links = std::move(ids_links),
190 .link_count = link_cnt,
191 .obj_cnt = graph_nodes});
192 };
193
194} // namespace shammodels::basegodunov::modules::details
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
void complete_event_state(sycl::event e) const
Complete the event state of the buffer.
T * get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{})
Get a read-write pointer to the buffer's data.
T get_val_at_idx(size_t idx) const
Get the value at a given index in the buffer.
const T * get_read_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) const
Get a read-only pointer to the buffer's data.
Class to manage a list of SYCL events.
Definition EventList.hpp:32
shammodels::basegodunov::modules::NeighGraph compute_neigh_graph_deprecated(const sham::DeviceScheduler_ptr &dev_sched, u32 graph_nodes, Args &&...args)
Create a neighbour graph using a class that will list the ids of the found neighbourgh NeighFindKerne...
shammodels::basegodunov::modules::NeighGraph compute_neigh_graph(const sham::DeviceScheduler_ptr &dev_sched, u32 graph_nodes, Args &&...args)
Create a neighbour graph using a class that will list the ids of the found neighbourgh NeighFindKerne...
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
sycl::buffer< T > scan_exclusive(sycl::queue &q, sycl::buffer< T > &buf1, u32 len)
Computes the exclusive sum of elements in a SYCL buffer.
Definition numeric.cpp:35
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33