Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
sycl2020reduction.hpp
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
10#pragma once
11
19#include "shamalgs/memory.hpp"
20#include "shambackends/sycl.hpp"
21
22namespace shamalgs::reduction::details {
23
24 template<class T>
25 struct SYCL2020 {
26 static T sum(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id);
27
28 // static T min(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id);
29
30 // static T max(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id);
31 };
32
33 template<class T, class Op>
34 inline T reduce_sycl_2020(
35 sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id, Op op) {
36
37 u32 len = end_id - start_id;
38
39 sycl::buffer<T> buf_int(len);
40 shamalgs::memory::write_with_offset_into(q, buf_int, buf1, start_id, len);
41
42 sycl::buffer<T> recov{1};
43
44 q.submit([&](sycl::handler &cgh) {
45 sycl::accessor global_mem{buf_int, cgh, sycl::read_only};
46
47#ifdef SYCL_COMP_INTEL_LLVM
48 auto reduc = sycl::reduction(recov, cgh, op);
49#else
50 sycl::accessor acc_rec{recov, cgh, sycl::write_only, sycl::no_init};
51 auto reduc = sycl::reduction(acc_rec, op);
52#endif
53
54 cgh.parallel_for(sycl::range<1>{len}, reduc, [=](sycl::id<1> idx, auto &sum) {
55 sum.combine(global_mem[idx]);
56 });
57 });
58
59 T rec;
60 {
61 sycl::host_accessor acc{recov, sycl::read_only};
62 rec = acc[0];
63 }
64
65 return rec;
66 }
67
68 template<class T>
69 inline T SYCL2020<T>::sum(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id) {
70#ifdef SYCL_COMP_INTEL_LLVM
71 return reduce_sycl_2020(q, buf1, start_id, end_id, sycl::plus<>{});
72#endif
73
74#ifdef SYCL_COMP_ACPP
75 return reduce_sycl_2020(q, buf1, start_id, end_id, sycl::plus<T>{});
76#endif
77 }
78
79} // namespace shamalgs::reduction::details
std::uint32_t u32
32 bit unsigned integer
main include file for memory algorithms