Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
dispatch_diagnostics.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <atomic>
12#include <functional>
13#include <optional>
14#include <string>
15#include <string_view>
16#include <utility>
17#include <vector>
18
20{
21
23struct event
24{
25 std::string operation;
26 std::vector<KernelBackendAttempt> backend_attempts;
27
29 [[nodiscard]] bool succeeded() const noexcept
30 {
31 for (auto const& attempt : backend_attempts)
32 {
33 if (attempt.runtime_result && kernel_attempt_succeeded(*attempt.runtime_result)) return true;
34 }
35 return false;
36 }
37
39 [[nodiscard]] std::optional<std::string_view> selected_backend() const noexcept
40 {
41 for (auto const& attempt : backend_attempts)
42 {
43 if (attempt.runtime_result && kernel_attempt_succeeded(*attempt.runtime_result)) return attempt.backend;
44 }
45 return std::nullopt;
46 }
47};
48
50using sink = std::function<void(event const&)>;
51
52namespace detail
53{
54extern std::atomic<bool> enabled_flag;
55}
56
60[[nodiscard]] inline bool enabled() noexcept { return detail::enabled_flag.load(std::memory_order_relaxed); }
61
64void set_sink(sink replacement);
65
67void reset_sink();
68
70[[nodiscard]] sink current_sink();
71
74 public:
75 explicit scoped_sink(sink replacement);
76 scoped_sink(scoped_sink const&) = delete;
78 scoped_sink(scoped_sink&& other) noexcept;
79 scoped_sink& operator=(scoped_sink&& other) noexcept;
81
82 private:
83 sink previous_;
84 bool active_ = true;
85};
86
87namespace detail
88{
89void emit(event const& diagnostic);
90} // namespace detail
91
92} // namespace uni20::linalg::dispatch_diagnostics
Temporarily replace the diagnostic sink and restore it on destruction.
Definition dispatch_diagnostics.hpp:73
scoped_sink & operator=(scoped_sink const &)=delete
~scoped_sink()
Definition dispatch_diagnostics.cpp:66
Structured kernel-dispatch failure data.
Definition dispatch_diagnostics.cpp:8
sink current_sink()
Return a copy of the active diagnostic sink.
Definition dispatch_diagnostics.cpp:43
void set_sink(sink replacement)
Replace the process-wide kernel-dispatch diagnostic sink.
Definition dispatch_diagnostics.cpp:30
bool enabled() noexcept
Return whether runtime kernel-dispatch diagnostics are enabled.
Definition dispatch_diagnostics.hpp:60
std::function< void(event const &)> sink
Callable receiving structured dispatch events after backend walks.
Definition dispatch_diagnostics.hpp:50
void reset_sink()
Disable runtime kernel-dispatch diagnostics.
Definition dispatch_diagnostics.cpp:41
constexpr bool kernel_attempt_succeeded(KernelAttempt attempt) noexcept
Return whether a backend performed the operation.
Definition kernel_attempt.hpp:40
Structured result of one ordered kernel-dispatch walk.
Definition dispatch_diagnostics.hpp:24
std::optional< std::string_view > selected_backend() const noexcept
Return the selected backend name, if the dispatch succeeded.
Definition dispatch_diagnostics.hpp:39
std::string operation
Definition dispatch_diagnostics.hpp:25
std::vector< KernelBackendAttempt > backend_attempts
Definition dispatch_diagnostics.hpp:26
bool succeeded() const noexcept
Return whether one backend completed the operation.
Definition dispatch_diagnostics.hpp:29