Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
PatchDataField.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
18
20#include "shambase/memory.hpp"
24#include "shamalgs/memory.hpp"
25#include "shamalgs/numeric.hpp"
34#include <array>
35#include <memory>
36#include <random>
37#include <set>
38#include <string>
39#include <utility>
40#include <vector>
41
42template<class T>
43class PatchDataField {
44
46 // constexpr utilities (using & constexpr vals)
48
49 static constexpr bool isprimitive = std::is_same<T, f32>::value || std::is_same<T, f64>::value
50 || std::is_same<T, u32>::value
51 || std::is_same<T, u64>::value;
52
53 // clang-format off
54 static constexpr bool is_in_type_list =
55 #define X(args) std::is_same<T, args>::value ||
56 XMAC_LIST_ENABLED_FIELD false
57 #undef X
58 ;
59
60 static_assert(
61 is_in_type_list,
62 "PatchDataField must be one of those types : "
63
64 #define X(args) #args " "
65 XMAC_LIST_ENABLED_FIELD
66 #undef X
67 );
68 // clang-format on
69
70 template<bool B, class Tb = void>
71 using enable_if_t = typename std::enable_if<B, Tb>;
72
73 using EnableIfPrimitive = enable_if_t<isprimitive>;
74
75 using EnableIfVec = enable_if_t<is_in_type_list && (!isprimitive)>;
76
78 // member fields
80
82
83 std::string field_name;
84
85 u32 nvar; // number of variable per object
86
87 inline void check_nvar() const {
88 if (nvar == 0) {
89 throw shambase::make_except_with_loc<std::runtime_error>("nvar is 0 is not allowed");
90 }
91 }
92
94 // Constructors
96
97 public:
98 inline PatchDataField(PatchDataField &&other) noexcept
99 : buf(std::move(other.buf)), field_name(std::move(other.field_name)),
100 nvar(std::move(other.nvar)) {} // move constructor
101
102 inline PatchDataField &operator=(PatchDataField &&other) noexcept {
103 buf = std::move(other.buf);
104 field_name = std::move(other.field_name);
105 nvar = std::move(other.nvar);
106
107 return *this;
108 } // move assignment
109
110 // TODO find a way to add particles easily cf setup require public vector
111
112 using Field_type = T;
113
114 inline PatchDataField(std::string name, u32 nvar)
115 : field_name(std::move(name)), nvar(nvar),
116 buf(0, shamsys::instance::get_compute_scheduler_ptr()) {
117 check_nvar();
118 };
119
120 inline PatchDataField(std::string name, u32 nvar, u32 obj_cnt)
121 : field_name(std::move(name)), nvar(nvar),
122 buf(obj_cnt * nvar, shamsys::instance::get_compute_scheduler_ptr()) {
123 check_nvar();
124 };
125
126 inline PatchDataField(const PatchDataField &other)
127 : field_name(other.field_name), nvar(other.nvar), buf(other.buf.copy()) {
128 check_nvar();
129 }
130
131 inline PatchDataField(sham::DeviceBuffer<T> &&moved_buf, std::string name, u32 nvar)
132 : field_name(name), nvar(nvar), buf(std::forward<sham::DeviceBuffer<T>>(moved_buf)) {
133 check_nvar();
134 }
135
136 PatchDataField &operator=(const PatchDataField &other) = delete;
137
139 // member functions
141
142 inline PatchDataField duplicate() const {
143 const PatchDataField &current = *this;
144 return PatchDataField(current);
145 }
146
147 inline PatchDataField duplicate(std::string new_name) const {
148 const PatchDataField &current = *this;
149 PatchDataField ret = PatchDataField(current);
150 ret.field_name = new_name;
151 return ret;
152 }
153
154 inline std::unique_ptr<PatchDataField> duplicate_to_ptr() const {
155 const PatchDataField &current = *this;
156 return std::make_unique<PatchDataField>(current);
157 }
158
159 inline sham::DeviceBuffer<T> &get_buf() { return buf; }
160 inline const sham::DeviceBuffer<T> &get_buf() const { return buf; }
161
162 [[nodiscard]] inline bool is_empty() const { return get_obj_cnt() == 0; }
163
164 [[nodiscard]] inline u64 memsize() const { return buf.get_mem_usage(); }
165
166 [[nodiscard]] inline const u32 &get_nvar() const { return nvar; }
167
168 [[nodiscard]] inline u32 get_obj_cnt() const {
169 size_t sz = buf.get_size();
170 if (sz % nvar != 0) {
172 "the size of the buffer ({}) is not a multiple of the number of variables ({})",
173 sz,
174 nvar));
175 }
176 return shambase::narrow_or_throw<u32>(sz / nvar);
177 }
178
188 [[nodiscard]] inline u32 get_val_cnt() const {
189 return shambase::narrow_or_throw<u32>(buf.get_size());
190 }
191
192 [[nodiscard]] inline const std::string &get_name() const { return field_name; }
193
194 // TODO add overflow check
195 void resize(u32 new_obj_cnt);
196
197 void reserve(u32 new_obj_cnt);
198
199 void expand(u32 obj_to_add);
200
201 void shrink(u32 obj_to_rem);
202
203 void insert_element(T v);
204
205 void apply_offset(T off);
206
207 void insert(const PatchDataField<T> &f2);
208
209 void overwrite(const PatchDataField<T> &f2, u32 obj_cnt);
210
211 void overwrite(const sham::DeviceBuffer<T> &f2, u32 len);
212
213 void override(sycl::buffer<T> &data, u32 cnt);
214
215 void override(std::vector<T> &data, u32 cnt);
216
217 void override(const T val);
218
219 inline void synchronize_buf() { buf.synchronize(); }
220
221 inline std::vector<T> copy_to_stdvec() {
222 auto tmp = buf.copy_to_stdvec();
223 tmp.resize(get_val_cnt());
224 return tmp;
225 }
226
228 // Span utilities
230
237 template<u32 nvar, bool is_pointer_access = shamrock::access_t_span>
242
253
255 get_pointer_span() {
256 StackEntry stack_loc{};
258 }
259
261 // get_subsets utilities
263
273 template<class Lambdacd, class... Args>
274 inline std::set<u32> get_ids_set_where(Lambdacd &&cd_true, Args... args) {
275 StackEntry stack_loc{};
276 std::set<u32> idx_cd{};
277 if (get_obj_cnt() > 0) {
278 auto acc = get_buf().copy_to_stdvec();
279
280 for (u32 i = 0; i < get_obj_cnt(); i++) {
281 if (cd_true(acc, i * nvar, args...)) {
282 idx_cd.insert(i);
283 }
284 }
285 }
286 return idx_cd;
287 }
288
299 template<class Lambdacd, class... Args>
300 inline std::vector<u32> get_ids_vec_where(Lambdacd &&cd_true, Args &&...args) {
301 StackEntry stack_loc{};
302 std::vector<u32> idx_cd{};
303 if (get_obj_cnt() > 0) {
304 auto acc = buf.copy_to_stdvec();
305
306 for (u32 i = 0; i < get_obj_cnt(); i++) {
307 if (std::forward<Lambdacd>(cd_true)(acc, i * nvar, std::forward<Args>(args)...)) {
308 idx_cd.push_back(i);
309 }
310 }
311 }
312 return idx_cd;
313 }
314
325 template<class Lambdacd, class... Args>
326 inline std::tuple<std::optional<sycl::buffer<u32>>, u32> get_ids_buf_where(
327 Lambdacd &&cd_true, Args... args) {
328 StackEntry stack_loc{};
329
330 if (get_obj_cnt() > 0) {
331
332 // buffer of booleans to store result of the condition
333 sycl::buffer<u32> mask(get_obj_cnt());
334
335 sham::EventList depends_list;
336 const T *acc = buf.get_read_access(depends_list);
337
338 sham::DeviceQueue &q = shamsys::instance::get_compute_scheduler().get_queue();
339
340 auto e = q.submit(depends_list, [&, args...](sycl::handler &cgh) {
341 sycl::accessor acc_mask{mask, cgh, sycl::write_only, sycl::no_init};
342 u32 nvar_field = nvar;
343
344 shambase::parallel_for(
345 cgh, get_obj_cnt(), "PatchdataField::get_ids_buf_where", [=](u32 id) {
346 acc_mask[id] = cd_true(acc, id * nvar_field, args...);
347 });
348 });
349
350 buf.complete_event_state(e);
351
353 shamsys::instance::get_compute_queue(), mask, get_obj_cnt());
354 } else {
355 return {std::nullopt, 0};
356 }
357 }
358
359 template<class Lambdacd, class... Args>
360 inline sham::DeviceBuffer<u32> get_ids_where_recycle_buffer(
361 sham::DeviceBuffer<u32> &mask, Lambdacd &&cd_true, Args... args) const {
362 StackEntry stack_loc{};
363
364 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
365 sham::DeviceQueue &q = shambase::get_check_ref(dev_sched).get_queue();
366
367 auto obj_cnt = get_obj_cnt();
368 if (obj_cnt > 0) {
369 // buffer of booleans to store result of the condition
370 mask.resize(obj_cnt);
371
373 q,
374 sham::MultiRef{buf},
375 sham::MultiRef{mask},
376 obj_cnt,
377 [=, nvar_field = nvar](u32 id, const T *__restrict acc, u32 *__restrict acc_mask) {
378 acc_mask[id] = cd_true(acc, id * nvar_field, args...);
379 });
380
381 return shamalgs::stream_compact(dev_sched, mask, obj_cnt);
382 } else {
383 return sham::DeviceBuffer<u32>(0, dev_sched);
384 }
385 }
386
397 template<class Lambdacd, class... Args>
398 inline sham::DeviceBuffer<u32> get_ids_where(Lambdacd &&cd_true, Args... args) const {
399 StackEntry stack_loc{};
400
401 auto dev_sched = shamsys::instance::get_compute_scheduler_ptr();
402 sham::DeviceQueue &q = shambase::get_check_ref(dev_sched).get_queue();
403
404 auto obj_cnt = get_obj_cnt();
405 if (obj_cnt > 0) {
406 // buffer of booleans to store result of the condition
407 sham::DeviceBuffer<u32> mask(obj_cnt, dev_sched);
408
410 q,
411 sham::MultiRef{buf},
412 sham::MultiRef{mask},
413 obj_cnt,
414 [=, nvar_field = nvar](u32 id, const T *__restrict acc, u32 *__restrict acc_mask) {
415 acc_mask[id] = cd_true(acc, id * nvar_field, args...);
416 });
417
418 return shamalgs::stream_compact(dev_sched, mask, obj_cnt);
419 } else {
420 return sham::DeviceBuffer<u32>(0, dev_sched);
421 }
422 }
423
425 //
427
428 template<class Lambdacd>
429 void check_err_range(Lambdacd &&cd_true, T vmin, T vmax, std::string add_log = "");
430
431 void extract_element(u32 pidx, PatchDataField<T> &to);
432 void extract_elements(const sham::DeviceBuffer<u32> &idxs, PatchDataField<T> &to);
433
434 bool check_field_match(PatchDataField<T> &f2);
435
436 inline void field_raz() {
437 shamlog_debug_ln("PatchDataField", "raz : ", field_name);
438 override(shambase::VectorProperties<T>::get_zero());
439 }
440
447 void append_subset_to(const std::vector<u32> &idxs, PatchDataField &pfield);
448 void append_subset_to(sycl::buffer<u32> &idxs_buf, u32 sz, PatchDataField &pfield);
449 void append_subset_to(
450 const sham::DeviceBuffer<u32> &idxs_buf, u32 sz, PatchDataField &pfield) const;
451
452 inline PatchDataField make_new_from_subset(sham::DeviceBuffer<u32> &idxs_buf, u32 sz) {
453 PatchDataField pfield(field_name, nvar);
454 append_subset_to(idxs_buf, sz, pfield);
455 return pfield;
456 }
457
458 void gen_mock_data(u32 obj_cnt, std::mt19937 &eng);
459
471
473 void permut_vars(const std::vector<u32> &permut);
474
487
494 void remove_ids(const sham::DeviceBuffer<u32> &indexes, u32 len);
495
503
512 static PatchDataField deserialize_buf(
513 shamalgs::SerializeHelper &serializer, std::string field_name, u32 nvar);
514
521
528
535 static PatchDataField deserialize_full(shamalgs::SerializeHelper &serializer);
536
543
544 T compute_max() const;
545 T compute_min() const;
546 T compute_sum() const;
547
548 shambase::VecComponent<T> compute_dot_sum();
549
550 bool has_nan();
551 bool has_inf();
552 bool has_nan_or_inf();
553
555 // static member functions
557
558 static PatchDataField<T> mock_field(u64 seed, u32 obj_cnt, std::string name, u32 nvar);
559 static PatchDataField<T> mock_field(
560 u64 seed, u32 obj_cnt, std::string name, u32 nvar, T vmin, T vmax);
561};
562
563// TODO add overflow check
564template<class T>
565inline void PatchDataField<T>::resize(u32 new_obj_cnt) {
566 buf.resize(shambase::narrow_or_throw<u32>(u64(new_obj_cnt) * u64(nvar)));
567}
568
569template<class T>
570inline void PatchDataField<T>::reserve(u32 new_obj_cnt) {
571
572 u32 add_cnt = shambase::narrow_or_throw<u32>(u64(new_obj_cnt) * u64(nvar));
573 buf.reserve(add_cnt);
574}
575
576template<class T>
577inline void PatchDataField<T>::expand(u32 obj_to_add) {
578 resize(get_obj_cnt() + obj_to_add);
579}
580
581template<class T>
582inline void PatchDataField<T>::shrink(u32 obj_to_rem) {
583
584 if (obj_to_rem > get_obj_cnt()) {
585
587 "impossible to remove more object than there is in the patchdata field");
588 }
589
590 resize(get_obj_cnt() - obj_to_rem);
591}
592
593template<class T>
594inline void PatchDataField<T>::overwrite(const PatchDataField<T> &f2, u32 obj_cnt) {
595 StackEntry stack_loc{};
596 buf.copy_from(f2.buf, obj_cnt * f2.nvar);
597}
598
599template<class T>
600inline void PatchDataField<T>::overwrite(const sham::DeviceBuffer<T> &f2, u32 len) {
601 StackEntry stack_loc{};
602 buf.copy_from(f2, len);
603}
604
605template<class T>
606inline void PatchDataField<T>::override(sycl::buffer<T> &data, u32 cnt) {
607 StackEntry stack_loc{};
608 buf.copy_from_sycl_buffer(data, cnt);
609}
610
611template<class T>
612inline void PatchDataField<T>::override(std::vector<T> &data, u32 cnt) {
613 StackEntry stack_loc{};
614 buf.copy_from_stdvec(data, cnt);
615}
616
617template<class T>
618inline void PatchDataField<T>::override(const T val) {
619 StackEntry stack_loc{};
620 buf.fill(val);
621}
622
623class PatchDataRangeCheckError : public std::exception {
624 public:
625 explicit PatchDataRangeCheckError(const char *message) : msg_(message) {}
626
627 explicit PatchDataRangeCheckError(const std::string &message) : msg_(message) {}
628
629 ~PatchDataRangeCheckError() noexcept override = default;
630
631 [[nodiscard]] const char *what() const noexcept override { return msg_.c_str(); }
632
633 protected:
634 std::string msg_;
635};
636
637template<class T>
638template<class Lambdacd>
639inline void PatchDataField<T>::check_err_range(
640 Lambdacd &&cd_true, T vmin, T vmax, std::string add_log) {
641 StackEntry stack_loc{};
642
643 if (is_empty()) {
644 return;
645 }
646
647 if (nvar != 1) {
649 }
650
651 bool error = false;
652 {
653 auto acc = buf.copy_to_stdvec();
654 u32 err_cnt = 0;
655
656 for (u32 i = 0; i < get_val_cnt(); i++) {
657 if (!cd_true(acc[i], vmin, vmax)) {
659 "PatchDataField",
660 "obj =",
661 i,
662 "->",
663 acc[i],
664 "not in range [",
665 vmin,
666 ",",
667 vmax,
668 "]");
669 error = true;
670 err_cnt++;
671 if (err_cnt > 50) {
672 logger::err_ln("PatchDataField", "...");
673 break;
674 }
675 }
676 }
677 }
678
679 if (error) {
680 logger::err_ln("PatchDataField", "additional infos :", add_log);
682 }
683}
Header file describing a Node Instance.
sycl::queue & get_compute_queue(u32 id=0)
std::uint32_t u32
32 bit unsigned integer
std::uint64_t u64
64 bit unsigned integer
shamalgs::SerializeSize serialize_full_byte_size()
give the size usage of serialize_full
void permut_vars(const std::vector< u32 > &permut)
permut the variables of the field according to the permut
shamrock::PatchDataFieldSpan< T, shamrock::dynamic_nvar > get_span_nvar_dynamic()
Returns a shamrock::PatchDataFieldSpan pointing to the current PatchDataField.
shamrock::PatchDataFieldSpan< T, nvar, is_pointer_access > get_span()
Returns a shamrock::PatchDataFieldSpan pointing to the current PatchDataField.
void index_remap(sham::DeviceBuffer< u32 > &index_map, u32 len)
this function remaps the patchdatafield like so val[id] = val[index_map[id]] index map describe : at ...
void remove_ids(const sham::DeviceBuffer< u32 > &indexes, u32 len)
remove the ids from the field
static PatchDataField deserialize_buf(shamalgs::SerializeHelper &serializer, std::string field_name, u32 nvar)
deserialize a field inverse of serialize_buf
void append_subset_to(const std::vector< u32 > &idxs, PatchDataField &pfield)
Copy all objects in idxs to pfield.
shamalgs::SerializeSize serialize_buf_byte_size()
record the size usage of the serialization using serialize_buf
u32 get_val_cnt() const
Get the number of values stored in the field.
static PatchDataField deserialize_full(shamalgs::SerializeHelper &serializer)
deserialize a field inverse of serialize_full
sham::DeviceBuffer< u32 > get_ids_where(Lambdacd &&cd_true, Args... args) const
Same function as.
std::tuple< std::optional< sycl::buffer< u32 > >, u32 > get_ids_buf_where(Lambdacd &&cd_true, Args... args)
Same function as.
std::set< u32 > get_ids_set_where(Lambdacd &&cd_true, Args... args)
Get the ids set where object.
void index_remap_resize(sham::DeviceBuffer< u32 > &index_map, u32 len)
this function remaps the patchdatafield like so val[id] = val[index_map[id]] index map describe : at ...
void serialize_buf(shamalgs::SerializeHelper &serializer)
minimal serialization assuming the user know the layout of the field
std::vector< u32 > get_ids_vec_where(Lambdacd &&cd_true, Args &&...args)
Same function as.
void serialize_full(shamalgs::SerializeHelper &serializer)
serialize everything in the class
A buffer allocated in USM (Unified Shared Memory).
void resize(size_t new_size, bool keep_data=true)
Resizes the buffer to a given size.
A SYCL queue associated with a device and a context.
sycl::event submit(Fct &&fct)
Submits a kernel to the SYCL queue.
Class to manage a list of SYCL events.
Definition EventList.hpp:32
Represents a span of data within a PatchDataField.
This header file contains utility functions related to exception handling in the code.
void kernel_call(sham::DeviceQueue &q, RefIn in, RefOut in_out, u32 n, Functor &&func, SourceLocation &&callsite=SourceLocation{})
Submit a kernel to a SYCL queue.
std::tuple< std::optional< sycl::buffer< u32 > >, u32 > stream_compact(sycl::queue &q, sycl::buffer< u32 > &buf_flags, u32 len)
Stream compaction algorithm.
Definition numeric.cpp:65
T & get_check_ref(const std::unique_ptr< T > &ptr, SourceLocation loc=SourceLocation())
Takes a std::unique_ptr and returns a reference to the object it holds. It throws a std::runtime_erro...
Definition memory.hpp:112
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
void throw_unimplemented(SourceLocation loc=SourceLocation{})
Throw a std::runtime_error saying that the function is unimplemented.
std::vector< std::string_view > args
Executable argument list (mapped from argv).
Definition cmdopt.cpp:63
Utilities for safe type narrowing conversions.
main include file for memory algorithms
void err_ln(std::string module_name, Types... var2)
Prints a log message with multiple arguments followed by a newline.
Definition logs.hpp:132
This file contains the definition for the stacktrace related functionality.
shambase::details::BasicStackEntry StackEntry
Alias for shambase::details::BasicStackEntry.
A class that references multiple buffers or similar objects.
Definition MultiRef.hpp:33