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
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 + "you want to create a event depedancy, but the event state was not updated "
33 "after 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 + "you want to create a event depedancy, but the event state was not updated "
53 "after 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 + "you are trying to register an event without having fetched one previously");
79
81 }
82
83 if (last_event_create != READ) {
84 std::string err
85 = (get_hash_log()
86 + "you want to register a read event but the last dependency was not in read mode");
87
89 }
90
91 up_to_date_events = true;
92 event_last_read.push_back(e);
93
94 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "append last read");
95}
96
97void shamalgs::BufferEventHandler::register_read_write_event(const sycl::event &e) {
98 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "set last write");
99 if (up_to_date_events) {
100 std::string err
101 = (get_hash_log()
102 + "you are trying to register an event without having fetched one previously");
103
105 }
106
107 if (last_event_create != READ_WRITE) {
108 std::string err
109 = (get_hash_log()
110 + "you want to register a read-write event but the last dependency was not in "
111 "read-write mode");
112
114 }
115
116 up_to_date_events = true;
117 event_last_write = e;
118}
119
120void shamalgs::BufferEventHandler::synchronize() {
121
122 shamlog_debug_sycl_ln("[USMBuffer]", get_hash_log(), "synchronize");
123
124 if (!up_to_date_events) {
125 std::string err = (get_hash_log() + "the events are not up to date");
126
128 }
129
130 event_last_write.wait_and_throw();
131 for (sycl::event e : event_last_read) {
132 e.wait_and_throw();
133 }
134
135 event_last_read = {};
136 event_last_write = {};
137}
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.
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:133