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
17
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 void exclusive_sum_in_place_fallback(sycl::queue &q, sycl::buffer<T> &buf1, u32 len) {
63
64 T accum{0};
65
66 {
67 sycl::host_accessor acc_src{buf1, sycl::read_write};
68
69 for (u32 idx = 0; idx < len; idx++) {
70
71 T val = accum;
72
73 accum += acc_src[idx];
74
75 acc_src[idx] = val;
76 }
77 }
78 }
79
80 template sham::DeviceBuffer<u32> exclusive_sum_fallback_usm(
81 const sham::DeviceScheduler_ptr &sched, sham::DeviceBuffer<u32> &buf1, u32 len);
82
83 template sycl::buffer<u32> exclusive_sum_fallback(
84 sycl::queue &q, sycl::buffer<u32> &buf1, u32 len);
85
86 template void exclusive_sum_in_place_fallback(sycl::queue &q, sycl::buffer<u32> &buf1, u32 len);
87
88 std::tuple<std::optional<sycl::buffer<u32>>, u32> stream_compact_fallback(
89 sycl::queue &q, sycl::buffer<u32> &buf_flags, u32 len) {
90
91 std::vector<u32> idxs;
92
93 {
94 sycl::host_accessor acc_src{buf_flags, sycl::read_only};
95
96 for (u32 idx = 0; idx < len; idx++) {
97
98 if (acc_src[idx]) {
99 idxs.push_back(idx);
100 }
101 }
102 }
103
104 if (idxs.empty()) {
105 return {{}, 0};
106 }
107
108 return {memory::vec_to_buf(idxs), idxs.size()};
109 }
110
112 const sham::DeviceScheduler_ptr &sched, sham::DeviceBuffer<u32> &buf_flags, u32 len) {
113
114 std::vector<u32> idxs;
115
116 {
117 auto acc_src = buf_flags.copy_to_stdvec();
118
119 for (u32 idx = 0; idx < len; idx++) {
120
121 if (acc_src[idx]) {
122 idxs.push_back(idx);
123 }
124 }
125 }
126
127 sham::DeviceBuffer<u32> ret(idxs.size(), sched);
128 ret.copy_from_stdvec(idxs);
129 return ret;
130 }
131} // 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