Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
numericFallback.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
19#include "shamalgs/memory.hpp"
21#include <numeric>
22
23namespace shamalgs::numeric::details {
24
25 template<class T>
26 sycl::buffer<T> exclusive_sum_fallback(sycl::queue &q, sycl::buffer<T> &buf1, u32 len) {
27
28 sycl::buffer<T> ret_buf(len);
29
30 T accum{0};
31
32 {
33 sycl::host_accessor acc_src{buf1, sycl::read_only};
34 sycl::host_accessor acc_res{ret_buf, sycl::write_only, sycl::no_init};
35
36 for (u32 idx = 0; idx < len; idx++) {
37
38 acc_res[idx] = accum;
39 accum += acc_src[idx];
40 }
41 }
42
43 return std::move(ret_buf);
44 }
45
46 template<class T>
48 const sham::DeviceScheduler_ptr &sched, sham::DeviceBuffer<T> &buf1, u32 len) {
49
50 sham::DeviceBuffer<T> ret_buf(len, sched);
51
52 auto acc_src = buf1.copy_to_stdvec();
53
54 std::exclusive_scan(acc_src.begin(), acc_src.end(), acc_src.begin(), 0);
55
56 ret_buf.copy_from_stdvec(acc_src, len);
57
58 return ret_buf;
59 }
60
61 template<class T>
62 sycl::buffer<T> inclusive_sum_fallback(sycl::queue &q, sycl::buffer<T> &buf1, u32 len) {
63
64 sycl::buffer<T> ret_buf(len);
65
66 T accum{0};
67
68 {
69 sycl::host_accessor acc_src{buf1, sycl::read_only};
70 sycl::host_accessor acc_res{ret_buf, sycl::write_only, sycl::no_init};
71
72 for (u32 idx = 0; idx < len; idx++) {
73
74 accum += acc_src[idx];
75 acc_res[idx] = accum;
76 }
77 }
78
79 return std::move(ret_buf);
80 }
81
82 template<class T>
83 void exclusive_sum_in_place_fallback(sycl::queue &q, sycl::buffer<T> &buf1, u32 len) {
84
85 T accum{0};
86
87 {
88 sycl::host_accessor acc_src{buf1, sycl::read_write};
89
90 for (u32 idx = 0; idx < len; idx++) {
91
92 T val = accum;
93
94 accum += acc_src[idx];
95
96 acc_src[idx] = val;
97 }
98 }
99 }
100
101 template<class T>
102 void inclusive_sum_in_place_fallback(sycl::queue &q, sycl::buffer<T> &buf1, u32 len) {
103
104 T accum{0};
105
106 {
107 sycl::host_accessor acc_src{buf1, sycl::read_write};
108
109 for (u32 idx = 0; idx < len; idx++) {
110
111 accum += acc_src[idx];
112 acc_src[idx] = accum;
113 }
114 }
115 }
116
117 template sycl::buffer<u32> inclusive_sum_fallback(
118 sycl::queue &q, sycl::buffer<u32> &buf1, u32 len);
119
121 const sham::DeviceScheduler_ptr &sched, sham::DeviceBuffer<u32> &buf1, u32 len);
122
123 template sycl::buffer<u32> exclusive_sum_fallback(
124 sycl::queue &q, sycl::buffer<u32> &buf1, u32 len);
125
126 template void exclusive_sum_in_place_fallback(sycl::queue &q, sycl::buffer<u32> &buf1, u32 len);
127
128 template void inclusive_sum_in_place_fallback(sycl::queue &q, sycl::buffer<u32> &buf1, u32 len);
129
130 std::tuple<std::optional<sycl::buffer<u32>>, u32> stream_compact_fallback(
131 sycl::queue &q, sycl::buffer<u32> &buf_flags, u32 len) {
132
133 std::vector<u32> idxs;
134
135 {
136 sycl::host_accessor acc_src{buf_flags, sycl::read_only};
137
138 for (u32 idx = 0; idx < len; idx++) {
139
140 if (acc_src[idx]) {
141 idxs.push_back(idx);
142 }
143 }
144 }
145
146 if (idxs.empty()) {
147 return {{}, 0};
148 }
149
150 return {memory::vec_to_buf(idxs), idxs.size()};
151 }
152
154 const sham::DeviceScheduler_ptr &sched, sham::DeviceBuffer<u32> &buf_flags, u32 len) {
155
156 std::vector<u32> idxs;
157
158 {
159 auto acc_src = buf_flags.copy_to_stdvec();
160
161 for (u32 idx = 0; idx < len; idx++) {
162
163 if (acc_src[idx]) {
164 idxs.push_back(idx);
165 }
166 }
167 }
168
169 sham::DeviceBuffer<u32> ret(idxs.size(), sched);
170 ret.copy_from_stdvec(idxs);
171 return ret;
172 }
173} // namespace shamalgs::numeric::details
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
void copy_from_stdvec(const std::vector< T > &vec)
Copy the content of a std::vector into the buffer.
std::vector< T > copy_to_stdvec() const
Copy the content of the buffer to a std::vector.
sycl::buffer< T > vec_to_buf(const std::vector< T > &buf)
Convert a std::vector to a sycl::buffer
Definition memory.cpp:29
sham::DeviceBuffer< T > exclusive_sum_fallback_usm(const sham::DeviceScheduler_ptr &sched, sham::DeviceBuffer< T > &buf1, u32 len)
Exclusive sum fallback on USM.
std::tuple< std::optional< sycl::buffer< u32 > >, u32 > stream_compact_fallback(sycl::queue &q, sycl::buffer< u32 > &buf_flags, u32 len)
Stream compaction algorithm on fallback.
sycl::buffer< T > exclusive_sum_fallback(sycl::queue &q, sycl::buffer< T > &buf1, u32 len)
Exclusive sum fallback on SYCL buffer.
main include file for memory algorithms