Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
fallbackReduction_usm.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
18#include "shamalgs/memory.hpp"
19#include "shambackends/math.hpp"
20#include "shambackends/sycl.hpp"
22#include "shambackends/vec.hpp"
23#include <stdexcept>
24
25namespace shamalgs::reduction::details {
26
27 template<class T, class BinaryOp>
28 T reduc_internal(
29 const sham::DeviceScheduler_ptr &sched,
30 const sham::DeviceBuffer<T> &buf1,
31 u32 start_id,
32 u32 end_id,
33 BinaryOp &&bop) {
34
35 if (start_id >= end_id) {
37 "Empty range not supported for min/max operations");
38 }
39
40 auto acc = buf1.copy_to_stdvec();
41 T ret = acc[start_id];
42 for (u32 i = start_id + 1; i < end_id; i++) {
43 ret = bop(ret, acc[i]);
44 }
45 return ret;
46 }
47
48 template<class T>
49 T sum_usm_fallback(
50 const sham::DeviceScheduler_ptr &sched,
51 const sham::DeviceBuffer<T> &buf1,
52 u32 start_id,
53 u32 end_id) {
54
55 // Empty range for sum should return 0
56 if (start_id >= end_id) {
57 return T{};
58 }
59
60 return reduc_internal<T>(sched, buf1, start_id, end_id, [](T lhs, T rhs) {
61 return lhs + rhs;
62 });
63 }
64
65 template<class T>
66 T max_usm_fallback(
67 const sham::DeviceScheduler_ptr &sched,
68 const sham::DeviceBuffer<T> &buf1,
69 u32 start_id,
70 u32 end_id) {
71
72 return reduc_internal<T>(sched, buf1, start_id, end_id, [](T lhs, T rhs) {
73 return sham::max(lhs, rhs);
74 });
75 }
76
77 template<class T>
78 T min_usm_fallback(
79 const sham::DeviceScheduler_ptr &sched,
80 const sham::DeviceBuffer<T> &buf1,
81 u32 start_id,
82 u32 end_id) {
83
84 return reduc_internal<T>(sched, buf1, start_id, end_id, [](T lhs, T rhs) {
85 return sham::min(lhs, rhs);
86 });
87 }
88
89} // namespace shamalgs::reduction::details
90
91#ifndef DOXYGEN
92 #define XMAC_TYPES \
93 X(f32) \
94 X(f32_2) \
95 X(f32_3) \
96 X(f32_4) \
97 X(f32_8) \
98 X(f32_16) \
99 X(f64) \
100 X(f64_2) \
101 X(f64_3) \
102 X(f64_4) \
103 X(f64_8) \
104 X(f64_16) \
105 X(u32) \
106 X(u64) \
107 X(i32) \
108 X(i64) \
109 X(u32_3) \
110 X(u64_3) \
111 X(i64_3) \
112 X(i32_3)
113
114 #define X(_arg_) \
115 template _arg_ shamalgs::reduction::details::sum_usm_fallback<_arg_>( \
116 const sham::DeviceScheduler_ptr &sched, \
117 const sham::DeviceBuffer<_arg_> &buf1, \
118 u32 start_id, \
119 u32 end_id); \
120 template _arg_ shamalgs::reduction::details::max_usm_fallback<_arg_>( \
121 const sham::DeviceScheduler_ptr &sched, \
122 const sham::DeviceBuffer<_arg_> &buf1, \
123 u32 start_id, \
124 u32 end_id); \
125 template _arg_ shamalgs::reduction::details::min_usm_fallback<_arg_>( \
126 const sham::DeviceScheduler_ptr &sched, \
127 const sham::DeviceBuffer<_arg_> &buf1, \
128 u32 start_id, \
129 u32 end_id);
130
131XMAC_TYPES
132 #undef X
133#endif
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
This header file contains utility functions related to exception handling in the code.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
main include file for memory algorithms