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
22#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
78 shamalgs::ImplVariantGlobal<
80#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
81 ,
82 GroupReduction
83#endif
84 >
85 reduction_impl{[](const sham::DeviceScheduler_ptr &, auto &self) {
86#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
87 self.set(GroupReduction{});
88#else
89 self.set(Fallback{});
90#endif
91 }};
92
94 std::vector<std::string> get_default_impl_list_reduction() {
95 return reduction_impl.get_default_config_list();
96 }
97
99 std::string get_current_impl_reduction() { return reduction_impl.get_current_config(); }
100
102 bool is_impl_set_reduction() { return reduction_impl.is_set(); }
103
105 void set_impl_reduction(const std::string &impl) {
106 shamlog_info_ln("algs", "setting reduction implementation to impl :", impl);
107 reduction_impl.set(impl);
108 }
109
111 void autoselect_impl_reduction(const sham::DeviceScheduler_ptr &dev_sched) {
112 reduction_impl.autoselect(dev_sched);
113 shamlog_info_ln(
114 "algs",
115 "defaulting reduction implementation to impl :",
117 }
118
119 } // namespace impl
120
121 template<class T>
123 const sham::DeviceScheduler_ptr &sched,
124 const sham::DeviceBuffer<T> &buf1,
125 u32 start_id,
126 u32 end_id) {
127
128 using namespace shamalgs::reduction::details;
129
130 if (!impl::reduction_impl.is_set()) {
132 }
133
134 return std::visit(
136 [&](impl::Fallback) {
137 return sum_usm_fallback(sched, buf1, start_id, end_id);
138 },
139#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
140 [&](impl::GroupReduction cfg) {
141 return sum_usm_group(sched, buf1, start_id, end_id, cfg.group_size);
142 },
143#endif
144 },
145 impl::reduction_impl.get());
146 }
147
148 template<class T>
150 const sham::DeviceScheduler_ptr &sched,
151 const sham::DeviceBuffer<T> &buf1,
152 u32 start_id,
153 u32 end_id) {
154
155 using namespace shamalgs::reduction::details;
156
157 if (!impl::reduction_impl.is_set()) {
159 }
160
161 return std::visit(
163 [&](impl::Fallback) {
164 return min_usm_fallback(sched, buf1, start_id, end_id);
165 },
166#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
167 [&](impl::GroupReduction cfg) {
168 return min_usm_group(sched, buf1, start_id, end_id, cfg.group_size);
169 },
170#endif
171 },
172 impl::reduction_impl.get());
173 }
174
175 template<class T>
177 const sham::DeviceScheduler_ptr &sched,
178 const sham::DeviceBuffer<T> &buf1,
179 u32 start_id,
180 u32 end_id) {
181
182 using namespace shamalgs::reduction::details;
183
184 if (!impl::reduction_impl.is_set()) {
186 }
187
188 return std::visit(
190 [&](impl::Fallback) {
191 return max_usm_fallback(sched, buf1, start_id, end_id);
192 },
193#ifdef SYCL2020_FEATURE_GROUP_REDUCTION
194 [&](impl::GroupReduction cfg) {
195 return max_usm_group(sched, buf1, start_id, end_id, cfg.group_size);
196 },
197#endif
198 },
199 impl::reduction_impl.get());
200 }
201
202#ifndef DOXYGEN
203 #define XMAC_TYPES \
204 X(f32) \
205 X(f32_2) \
206 X(f32_3) \
207 X(f32_4) \
208 X(f32_8) \
209 X(f32_16) \
210 X(f64) \
211 X(f64_2) \
212 X(f64_3) \
213 X(f64_4) \
214 X(f64_8) \
215 X(f64_16) \
216 X(u32) \
217 X(u64) \
218 X(i32) \
219 X(i64) \
220 X(u32_3) \
221 X(u64_3) \
222 X(i64_3) \
223 X(i32_3)
224
225 #define X(_arg_) \
226 template _arg_ sum<_arg_>( \
227 const sham::DeviceScheduler_ptr &sched, \
228 const sham::DeviceBuffer<_arg_> &buf1, \
229 u32 start_id, \
230 u32 end_id); \
231 template _arg_ min<_arg_>( \
232 const sham::DeviceScheduler_ptr &sched, \
233 const sham::DeviceBuffer<_arg_> &buf1, \
234 u32 start_id, \
235 u32 end_id); \
236 template _arg_ max<_arg_>( \
237 const sham::DeviceScheduler_ptr &sched, \
238 const sham::DeviceBuffer<_arg_> &buf1, \
239 u32 start_id, \
240 u32 end_id);
241
242 XMAC_TYPES
243 #undef X
244#endif
245
246} // 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).
This header file contains utility functions related to exception handling in the code.
namespace to control implementation behavior
bool is_impl_set_reduction()
Check if an implementation has been selected for reduction.
void autoselect_impl_reduction(const sham::DeviceScheduler_ptr &dev_sched)
Select the default implementation for reduction.
std::string get_current_impl_reduction()
Get the current implementation for reduction, as a config json string.
Definition reduction.cpp:99
std::vector< std::string > get_default_impl_list_reduction()
Get list of available reduction implementations, as config json strings.
Definition reduction.cpp:94
void set_impl_reduction(const std::string &impl)
Set the implementation for reduction, from a config json string.
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.