Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
fallbackReduction.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
21#include "shambackends/math.hpp"
22#include "shambackends/sycl.hpp"
23
24namespace shamalgs::reduction::details {
25
26 template<class T>
28
29 static T sum(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id);
30
31 static T min(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id);
32
33 static T max(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id);
34 };
35
36 template<class T>
37 inline T _int_sum(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id) {
38 T accum = T{};
39
40 {
41 sycl::host_accessor acc{buf1, sycl::read_only};
42
43 for (u32 idx = start_id; idx < end_id; idx++) {
44 if (idx == start_id) {
45 accum = acc[idx];
46 } else {
47 accum += acc[idx];
48 }
49 }
50 }
51
52 return accum;
53 }
54
55 template<class T>
56 inline T _int_min(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id) {
57 T accum = shambase::get_max<T>();
58
59 {
60 sycl::host_accessor acc{buf1, sycl::read_only};
61
62 for (u32 idx = start_id; idx < end_id; idx++) {
63 if (idx == start_id) {
64 accum = acc[idx];
65 } else {
66 accum = sham::min(acc[idx], accum);
67 }
68 }
69 }
70
71 return accum;
72 }
73
74 template<class T>
75 inline T _int_max(sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id) {
76 T accum = shambase::get_min<T>();
77
78 {
79 sycl::host_accessor acc{buf1, sycl::read_only};
80
81 for (u32 idx = start_id; idx < end_id; idx++) {
82 if (idx == start_id) {
83 accum = acc[idx];
84 } else {
85 accum = sham::max(acc[idx], accum);
86 }
87 }
88 }
89
90 return accum;
91 }
92
93 template<class T>
94 inline T FallbackReduction<T>::sum(
95 sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id) {
96
97 return _int_sum(q, buf1, start_id, end_id);
98 }
99
100 template<class T>
101 inline T FallbackReduction<T>::min(
102 sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id) {
103
104 return _int_min(q, buf1, start_id, end_id);
105 }
106
107 template<class T>
108 inline T FallbackReduction<T>::max(
109 sycl::queue &q, sycl::buffer<T> &buf1, u32 start_id, u32 end_id) {
110
111 return _int_max(q, buf1, start_id, end_id);
112 }
113
114} // namespace shamalgs::reduction::details
std::uint32_t u32
32 bit unsigned integer
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.