Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
BufferEventHandler.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
16
18#include "shamcomm/logs.hpp"
19#include <random>
20
21u32 shamalgs::gen_buf_hash() {
22 constexpr u32 u32_max = 0xFFFFFFFF;
23 static std::mt19937 gengine{0};
24 return std::uniform_int_distribution<u32>(0, u32_max)(gengine);
25}
26
27void shamalgs::BufferEventHandler::add_read_dependancies(std::vector<sycl::event> &depends_list) {
28
29 if (!up_to_date_events) {
30 std::string err
31 = get_hash_log()
32 + "cannot create a read dependency: the event state was not updated after the "
33 "last event usage";
34
36 }
37
38 up_to_date_events = false;
39 last_event_create = READ;
40
41 depends_list.push_back(event_last_write);
42
43 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "add read dependency");
44}
45
46void shamalgs::BufferEventHandler::add_read_write_dependancies(
47 std::vector<sycl::event> &depends_list) {
48
49 if (!up_to_date_events) {
50 std::string err
51 = get_hash_log()
52 + "cannot create a read-write dependency: the event state was not updated after "
53 "the last event usage";
54
56 }
57
58 up_to_date_events = false;
59 last_event_create = READ_WRITE;
60
61 depends_list.push_back(event_last_write);
62 for (const sycl::event &e : event_last_read) {
63 depends_list.push_back(e);
64 }
65 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "add read write dependency");
66
67 event_last_read = {};
68 event_last_write = {};
69
70 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "reset event list");
71}
72
73void shamalgs::BufferEventHandler::register_read_event(const sycl::event &e) {
74
75 if (up_to_date_events) {
76 std::string err
77 = (get_hash_log()
78 + "cannot register a read event: no dependency was fetched before this "
79 "registration");
80
82 }
83
84 if (last_event_create != READ) {
85 std::string err
86 = (get_hash_log()
87 + "cannot register a read event: the last dependency created was not a read "
88 "dependency");
89
91 }
92
93 up_to_date_events = true;
94 event_last_read.push_back(e);
95
96 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "append last read");
97}
98
99void shamalgs::BufferEventHandler::register_read_write_event(const sycl::event &e) {
100 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "set last write");
101 if (up_to_date_events) {
102 std::string err
103 = (get_hash_log()
104 + "cannot register a read-write event: no dependency was fetched before this "
105 "registration");
106
108 }
109
110 if (last_event_create != READ_WRITE) {
111 std::string err
112 = (get_hash_log()
113 + "cannot register a read-write event: the last dependency created was not a "
114 "read-write dependency");
115
117 }
118
119 up_to_date_events = true;
120 event_last_write = e;
121}
122
123void shamalgs::BufferEventHandler::synchronize() {
124
125 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "synchronize");
126
127 if (!up_to_date_events) {
128 std::string err
129 = (get_hash_log() + "cannot synchronize: the event state is not up to date");
130
132 }
133
134 event_last_write.wait_and_throw();
135 for (sycl::event e : event_last_read) {
136 e.wait_and_throw();
137 }
138
139 event_last_read = {};
140 event_last_write = {};
141}
std::uint32_t u32
32 bit unsigned integer
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.
constexpr u32 u32_max
u32 max value
void err(std::string module_name, Types... var2)
Prints a log message with multiple arguments.
Definition logs.hpp:132