Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
DynamicIdGenerator.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#include "shambase/integer.hpp"
20#include "shamalgs/memory.hpp"
21#include "shambackends/sycl.hpp"
22
23namespace shamalgs::atomic {
24
60 template<class int_t, u32 group_size>
61 class DynamicIdGenerator;
62
69 template<class int_t>
70 class DynamicId {
71 public:
72 int_t is_main_thread;
73 int_t dyn_group_id;
74 int_t dyn_global_id;
75 };
76
83 template<class int_t, u32 group_size>
85 public:
86 sycl::accessor<int_t, 1, sycl::access::mode::read_write, sycl::access::target::device>
87 group_id;
88
89 sycl::local_accessor<int_t, 1> local_group_id;
90
92 sycl::handler &cgh, DynamicIdGenerator<int_t, group_size> &gen)
93 : group_id{gen.group_id, cgh, sycl::read_write}, local_group_id(1, cgh) {}
94
101 inline DynamicId<int_t> compute_id(sycl::nd_item<1> it) const {
103
104 ret.is_main_thread = it.get_local_id(0) == 0 ? 1 : 0;
105
106 if (ret.is_main_thread) {
107
108 sycl::atomic_ref<
109 int_t,
110 sycl::memory_order_relaxed,
111 sycl::memory_scope_device,
112 sycl::access::address_space::global_space>
113 atomic_group_id(group_id[0]);
114
115 ret.dyn_group_id = atomic_group_id.fetch_add(1);
116 local_group_id[0] = ret.dyn_group_id;
117 }
118 it.barrier(sycl::access::fence_space::local_space);
119 ret.dyn_group_id = local_group_id[0];
120
121 ret.dyn_global_id = ret.dyn_group_id * group_size + it.get_local_id(0);
122
123 return ret;
124 }
125 };
126
127 template<class int_t, u32 group_size>
129 public:
134 sycl::buffer<int_t> group_id;
135
141 inline explicit DynamicIdGenerator(sycl::queue &q) : group_id(1) {
143 }
144
153 return {cgh, *this};
154 }
155 };
156
157} // namespace shamalgs::atomic
Accesses version of DynamicIdGenerator see doc for example (DynamicIdGenerator)
DynamicId< int_t > compute_id(sycl::nd_item< 1 > it) const
compute the local ids and return the result DynamicId
Sycl utility to dynamically generate group ids.
DynamicIdGenerator(sycl::queue &q)
Construct DynamicIdGenerator
sycl::buffer< int_t > group_id
the buffer used for group_id synchronization
AccessedDynamicIdGenerator< int_t, group_size > get_access(sycl::handler &cgh)
Get the access to DynamicIdGenerator returning the accessed variants AccessedDynamicIdGenerator
Object returned by DynamicIdGenerator containing information about the worker affected id.
void buf_fill_discard(sycl::queue &q, sycl::buffer< T > &buf, T value)
Fill a buffer with a given value (sycl::no_init mode)
Definition memory.hpp:159
main include file for memory algorithms