Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PatchDataFieldSpan.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
20#include "shambase/format.hpp"
22#include "shambase/string.hpp"
24
25// forward declare PatchDataField
26template<class T>
27class PatchDataField;
28
29namespace shamrock {
30
31 namespace details {
32
34 template<class T>
36 T *ptr;
38
40 T &operator()(u32 idx, u32 offset) const { return ptr[idx * nvar + offset]; }
41
43 T &operator[](u32 idx) const { return ptr[idx]; }
44 };
45
47 template<class T>
49 const T *ptr;
51
53 const T &operator()(u32 idx, u32 offset) const { return ptr[idx * nvar + offset]; }
54
56 const T &operator[](u32 idx) const { return ptr[idx]; }
57 };
58
60 template<class T, u32 nvar>
62 T *ptr;
63
65 T &operator()(u32 idx, u32 offset) const { return ptr[idx * nvar + offset]; }
66
68 template<typename Dummy = void, typename = std::enable_if_t<nvar == 1, Dummy>>
69 T &operator()(u32 idx) const {
70 return ptr[idx];
71 }
72 };
73
75 template<class T, u32 nvar>
77 const T *ptr;
78
80 const T &operator()(u32 idx, u32 offset) const { return ptr[idx * nvar + offset]; }
81
83 template<typename Dummy = void, typename = std::enable_if_t<nvar == 1, Dummy>>
84 const T &operator()(u32 idx) const {
85 return ptr[idx];
86 }
87 };
88 } // namespace details
89
91 template<class T>
93
95 template<class T>
97
99 template<class T, u32 nvar>
101
103 template<class T, u32 nvar>
105
107 inline constexpr u32 dynamic_nvar = u32_max;
108
109 inline constexpr bool access_t_pointer = true;
110 inline constexpr bool access_t_span = !access_t_pointer;
111
142 template<class T, u32 nvar = dynamic_nvar, bool pointer_access = access_t_span>
144 public:
150 inline static constexpr bool is_nvar_dynamic() { return nvar == dynamic_nvar; }
151
157 inline static constexpr bool is_nvar_static() { return nvar != dynamic_nvar; }
158
159 inline static constexpr bool is_pointer_access() {
160 return pointer_access == access_t_pointer;
161 }
162 inline static constexpr bool is_span_access() { return pointer_access == access_t_span; }
163
180 u32 start,
181 u32 count,
184
185 StackEntry stack_loc{};
186
187 // ensure that the underlying USM pointer can be accessed
188 if (field_ref.get_buf().is_empty()) {
190 "PatchDataFieldSpan can not be binded to empty buffer", loc);
191 }
192
193 if (is_nvar_static()) {
194 if (field_ref.get_nvar() != nvar) {
196 shambase::format(
197 "You are trying to bind a PatchDataFieldSpan with static nvar={} to a "
198 "PatchDataField with nvar={}",
199 nvar,
200 field_ref.get_nvar()),
201 loc);
202 }
203 }
204
205 if (start + count > field_ref.get_obj_cnt()) {
207 shambase::format(
208 "PatchDataFieldSpan out of bounds: {} + {} > {}",
209 start,
210 count,
211 field_ref.get_obj_cnt()),
212 loc);
213 }
214 }
215
226 template<
227 typename Dummy = void,
228 typename = std::enable_if_t<is_nvar_dynamic() && is_span_access(), Dummy>>
229 inline auto get_read_access(
230 sham::EventList &depends_list, SourceLocation src_loc = SourceLocation{}) const
232 StackEntry stack_loc{};
233 return details::PatchDataFieldSpan_access_ro_dyn_nvar<T>{
234 get_buf().get_read_access(depends_list, std::move(src_loc))
235 + start * field_ref.get_nvar(),
236 field_ref.get_nvar()};
237 }
238
249 template<
250 typename Dummy = void,
251 typename = std::enable_if_t<is_nvar_dynamic() && is_span_access(), Dummy>>
252 inline auto get_write_access(
253 sham::EventList &depends_list, SourceLocation src_loc = SourceLocation{})
255 StackEntry stack_loc{};
256 return details::PatchDataFieldSpan_access_rw_dyn_nvar<T>{
257 get_buf().get_write_access(depends_list, std::move(src_loc))
258 + start * field_ref.get_nvar(),
259 field_ref.get_nvar()};
260 }
261
272 template<
273 typename Dummy = void,
274 typename = std::enable_if_t<is_nvar_static() && is_span_access(), Dummy>>
275 inline auto get_read_access(
276 sham::EventList &depends_list, SourceLocation src_loc = SourceLocation{}) const
278 StackEntry stack_loc{};
279 return details::PatchDataFieldSpan_access_ro_static_nvar<T, nvar>{
280 get_buf().get_read_access(depends_list, std::move(src_loc))
281 + start * field_ref.get_nvar()};
282 }
283
294 template<
295 typename Dummy = void,
296 typename = std::enable_if_t<is_nvar_static() && is_span_access(), Dummy>>
297 inline auto get_write_access(
298 sham::EventList &depends_list, SourceLocation src_loc = SourceLocation{})
300 StackEntry stack_loc{};
301 return details::PatchDataFieldSpan_access_rw_static_nvar<T, nvar>{
302 get_buf().get_write_access(depends_list, std::move(src_loc))
303 + start * field_ref.get_nvar()};
304 }
305
306 template<typename Dummy = void, typename = std::enable_if_t<is_pointer_access(), Dummy>>
307 inline auto get_read_access(
308 sham::EventList &depends_list, SourceLocation src_loc = SourceLocation{}) const
309 -> const T * {
310 StackEntry stack_loc{};
311 return {
312 get_buf().get_read_access(depends_list, std::move(src_loc))
313 + start * field_ref.get_nvar()};
314 }
315
316 template<typename Dummy = void, typename = std::enable_if_t<is_pointer_access(), Dummy>>
317 inline auto get_write_access(
318 sham::EventList &depends_list, SourceLocation src_loc = SourceLocation{}) -> T * {
319 StackEntry stack_loc{};
320 return {
321 get_buf().get_write_access(depends_list, std::move(src_loc))
322 + start * field_ref.get_nvar()};
323 }
324
330 inline void complete_event_state(sycl::event e) const {
331 StackEntry stack_loc{};
332 get_buf().complete_event_state(e);
333 }
334
337
340
343
344 private:
346 inline sham::DeviceBuffer<T> &get_buf() { return field_ref.get_buf(); }
347
349 inline const sham::DeviceBuffer<T> &get_buf() const { return field_ref.get_buf(); }
350 };
351
352 template<class T>
353 using PatchDataFieldSpanPointer = PatchDataFieldSpan<T, dynamic_nvar, access_t_pointer>;
354
355} // namespace shamrock
Source location utility.
std::uint32_t u32
32 bit unsigned integer
A buffer allocated in USM (Unified Shared Memory)
Class to manage a list of SYCL events.
Definition EventList.hpp:31
Represents a span of data within a PatchDataField.
static constexpr bool is_nvar_dynamic()
Returns true if the number of variables is dynamic.
auto get_read_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) const -> details::PatchDataFieldSpan_access_ro_dyn_nvar< T >
Returns a read-only accessor to the data in the span.
void complete_event_state(sycl::event e) const
Completes the event state of the underlying buffer.
auto get_read_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) const -> details::PatchDataFieldSpan_access_ro_static_nvar< T, nvar >
Returns a read-only accessor to the data in the span.
auto get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) -> details::PatchDataFieldSpan_access_rw_dyn_nvar< T >
Returns a read-write accessor to the data in the span.
PatchDataFieldSpan(PatchDataField< T > &field_ref, u32 start, u32 count, SourceLocation loc=SourceLocation{})
Constructor.
u32 start
Starting element index of the span.
PatchDataField< T > & field_ref
Reference to the PatchDataField.
static constexpr bool is_nvar_static()
Returns true if the number of variables is static.
auto get_write_access(sham::EventList &depends_list, SourceLocation src_loc=SourceLocation{}) -> details::PatchDataFieldSpan_access_rw_static_nvar< T, nvar >
Returns a read-write accessor to the data in the span.
Namespace for internal details of the logs module.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
namespace for the main framework
Definition __init__.py:1
constexpr u32 dynamic_nvar
Constant for dynamic number of variables.
constexpr u32 u32_max
u32 max value
This file contains the definition for the stacktrace related functionality.
provide information about the source location
Accessor for read-only access to dynamic nvar buffer data.
const T & operator()(u32 idx, u32 offset) const
Access to element at index idx and offset var.
const T & operator[](u32 idx) const
Access the underlying pointer.
Accessor for read-only access to static nvar buffer data.
const T & operator()(u32 idx, u32 offset) const
Access to element at index idx and offset var.
const T & operator()(u32 idx) const
Access without offset if nvar is 1.
Accessor for read-write access to dynamic nvar buffer data.
T & operator()(u32 idx, u32 offset) const
Access to element at index idx and offset var.
T & operator[](u32 idx) const
Access the underlying pointer.
Accessor for read-write access to static nvar buffer data.
T & operator()(u32 idx, u32 offset) const
Access to element at index idx and offset var.
T & operator()(u32 idx) const
Access without offset if nvar is 1.