Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
reduction.cpp
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
16
21#include "fmt/std.h"
30
32
34 struct Fallback {
35 static constexpr std::string_view variant_type_name = "fallback";
36 };
37
38#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
40 struct GroupReduction {
41 static constexpr std::string_view variant_type_name = "group_reduction";
42 u32 group_size = 128;
43
45 static std::vector<GroupReduction> variant_custom_defaults() {
46 return {
47 GroupReduction{16},
48 GroupReduction{128},
49 GroupReduction{256},
50 };
51 }
52 };
53#endif
54
55} // namespace shamalgs::primitives::impl
56
57#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
58template<>
59struct shamalgs::ImplVariantParams<shamalgs::primitives::impl::GroupReduction> {
60 static nlohmann::json to_json(const shamalgs::primitives::impl::GroupReduction &p) {
61 return {{"group_size", p.group_size}};
62 }
63 static shamalgs::primitives::impl::GroupReduction from_json(const nlohmann::json &j) {
64 shamalgs::primitives::impl::GroupReduction p{};
65 if (j.contains("group_size")) {
66 p.group_size = j.at("group_size").get<u32>();
67 }
68 return p;
69 }
70};
71#endif
72
73namespace shamalgs::primitives {
74
76 namespace impl {
77
79 constexpr std::string_view reduction_impl_name = "reduction";
80
83#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
84 ,
85 GroupReduction
86#endif
87 >
88 reduction_impl{[](const sham::DeviceScheduler_ptr &, auto &self) {
89#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
90 self.set(GroupReduction{});
91#else
92 self.set(Fallback{});
93#endif
94 }};
95
96 // Must come after the global it registers: same TU, so it is initialized after it
98
99 } // namespace impl
100
101 template<class T>
103 const sham::DeviceScheduler_ptr &sched,
104 const sham::DeviceBuffer<T> &buf1,
105 u32 start_id,
106 u32 end_id) {
107
108 using namespace shamalgs::reduction::details;
109
110 if (!impl::reduction_impl.is_set()) {
112 }
113
114 return std::visit(
116 [&](impl::Fallback) {
117 return sum_usm_fallback(sched, buf1, start_id, end_id);
118 },
119#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
120 [&](impl::GroupReduction cfg) {
121 return sum_usm_group(sched, buf1, start_id, end_id, cfg.group_size);
122 },
123#endif
124 },
125 impl::reduction_impl.get());
126 }
127
128 template<class T>
130 const sham::DeviceScheduler_ptr &sched,
131 const sham::DeviceBuffer<T> &buf1,
132 u32 start_id,
133 u32 end_id) {
134
135 using namespace shamalgs::reduction::details;
136
137 if (!impl::reduction_impl.is_set()) {
139 }
140
141 return std::visit(
143 [&](impl::Fallback) {
144 return min_usm_fallback(sched, buf1, start_id, end_id);
145 },
146#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
147 [&](impl::GroupReduction cfg) {
148 return min_usm_group(sched, buf1, start_id, end_id, cfg.group_size);
149 },
150#endif
151 },
152 impl::reduction_impl.get());
153 }
154
155 template<class T>
157 const sham::DeviceScheduler_ptr &sched,
158 const sham::DeviceBuffer<T> &buf1,
159 u32 start_id,
160 u32 end_id) {
161
162 using namespace shamalgs::reduction::details;
163
164 if (!impl::reduction_impl.is_set()) {
166 }
167
168 return std::visit(
170 [&](impl::Fallback) {
171 return max_usm_fallback(sched, buf1, start_id, end_id);
172 },
173#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
174 [&](impl::GroupReduction cfg) {
175 return max_usm_group(sched, buf1, start_id, end_id, cfg.group_size);
176 },
177#endif
178 },
179 impl::reduction_impl.get());
180 }
181
182#ifndef DOXYGEN
183 #define XMAC_TYPES \
184 X(f32) \
185 X(f32_2) \
186 X(f32_3) \
187 X(f32_4) \
188 X(f32_8) \
189 X(f32_16) \
190 X(f64) \
191 X(f64_2) \
192 X(f64_3) \
193 X(f64_4) \
194 X(f64_8) \
195 X(f64_16) \
196 X(u32) \
197 X(u64) \
198 X(i32) \
199 X(i64) \
200 X(u32_3) \
201 X(u64_3) \
202 X(i64_3) \
203 X(i32_3)
204
205 #define X(_arg_) \
206 template _arg_ sum<_arg_>( \
207 const sham::DeviceScheduler_ptr &sched, \
208 const sham::DeviceBuffer<_arg_> &buf1, \
209 u32 start_id, \
210 u32 end_id); \
211 template _arg_ min<_arg_>( \
212 const sham::DeviceScheduler_ptr &sched, \
213 const sham::DeviceBuffer<_arg_> &buf1, \
214 u32 start_id, \
215 u32 end_id); \
216 template _arg_ max<_arg_>( \
217 const sham::DeviceScheduler_ptr &sched, \
218 const sham::DeviceBuffer<_arg_> &buf1, \
219 u32 start_id, \
220 u32 end_id);
221
222 XMAC_TYPES
223 #undef X
224#endif
225
226} // namespace shamalgs::primitives
Generic std::variant-based implementation selector.
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory).
Drop-in replacement for the hand-rolled "global variable + enum + name mapping.
This header file contains utility functions related to exception handling in the code.
Name-keyed registry of the implementation selectors (IImplVariant) of every algorithm.
#define SHAMALGS_REGISTER_IMPL(name, impl)
Register the implementation selector impl under name at static initialization.
void autoselect_impl(std::string_view alg, const sham::DeviceScheduler_ptr &sched)
Select the default implementation of alg for the device behind sched.
namespace to control implementation behavior
constexpr std::string_view reduction_impl_name
Registry name, shared by its registration and the dispatch site(s).
Definition reduction.cpp:79
namespace for primitive algorithm (e.g. sort, scan, reductions, ...)
T sum(const sham::DeviceScheduler_ptr &sched, const sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Compute the sum of elements in a device buffer within a specified range.
T min(const sham::DeviceScheduler_ptr &sched, const sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Find the minimum element in a device buffer within a specified range.
T max(const sham::DeviceScheduler_ptr &sched, const sham::DeviceBuffer< T > &buf1, u32 start_id, u32 end_id)
Find the maximum element in a device buffer within a specified range.
namespace to contain everything implemented by shamalgs
Definition algorithm.hpp:21
Customization point controlling how an alternative's fields (if any) are serialized to / parsed from ...
static Alt from_json(const nlohmann::json &)
Parse the alternative's fields back (default: no fields, ignored).
static nlohmann::json to_json(const Alt &)
Serialize the alternative's fields (default: no fields, empty object).
Fallback USM reduction (portable, no group reduction support required).
Definition reduction.cpp:34
Build an overload set out of several callables, for use with std::visit.