MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ExecutionTarget.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file ExecutionTarget.hpp
5 * @brief Where and on what an operation can run, as sets of pairs that compose.
6 *
7 * @experimental The shape of Target and its use in resolution are not settled.
8 */
9
10namespace MayaFlux::Yantra {
11
12/**
13 * @enum Device
14 * @brief The silicon the work runs on. A set: values combine with `|`.
15 */
16enum class Device : uint8_t {
17 NONE = 0,
18 CPU = 1U << 0,
19 GPU = 1U << 1,
20 NPU = 1U << 2,
21 CUSTOM_0 = 1U << 6,
22 CUSTOM_1 = 1U << 7,
23};
24
25/**
26 * @enum Runtime
27 * @brief The software stack that executes the work. A set: values combine with `|`.
28 */
29enum class Runtime : uint8_t {
30 NONE = 0,
31 NATIVE = 1U << 0, ///< Compiled C++ in this library
32 VULKAN = 1U << 1, ///< Vulkan compute shaders
33 ONNX = 1U << 2, ///< ONNX runtime
34 CUSTOM_0 = 1U << 6,
35 CUSTOM_1 = 1U << 7,
36};
37
38template <typename E>
39concept FlagEnum = std::same_as<E, Device> || std::same_as<E, Runtime>;
40
41template <FlagEnum E>
42[[nodiscard]] constexpr E operator|(E a, E b) noexcept
43{
44 return static_cast<E>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
45}
46
47template <FlagEnum E>
48[[nodiscard]] constexpr E operator&(E a, E b) noexcept
49{
50 return static_cast<E>(static_cast<uint8_t>(a) & static_cast<uint8_t>(b));
51}
52
53/** @brief True when `set` holds every member of `members`. */
54template <FlagEnum E>
55[[nodiscard]] constexpr bool contains(E set, E members) noexcept
56{
57 return (set & members) == members;
58}
59
60/** @brief True when the two sets share a member. */
61template <FlagEnum E>
62[[nodiscard]] constexpr bool intersects(E a, E b) noexcept
63{
64 return (a & b) != E::NONE;
65}
66
67/**
68 * @struct Target
69 * @brief A set of (device, runtime) pairs. Two targets meet when they share a pair.
70 *
71 * Built from a device set and a runtime set, which gives every combination of
72 * the two (CPU|GPU with ONNX is CPU+ONNX and GPU+ONNX). Joining with `|` is a
73 * union of pairs, so CPU+Native joined with GPU+Vulkan names exactly those two
74 * and does not admit GPU+Native.
75 */
76struct Target {
77 constexpr Target() noexcept = default;
78
79 /** @brief Every pairing of a device in @p device_set with a runtime in @p runtime_set. */
80 constexpr Target(Device device_set, Runtime runtime_set) noexcept
81 {
82 for (unsigned device = 0; device < 8; ++device) {
83 if (((static_cast<unsigned>(device_set) >> device) & 1U) != 0U) {
84 m_pairs |= uint64_t { static_cast<uint8_t>(runtime_set) } << (device * 8U);
85 }
86 }
87 }
88
89 [[nodiscard]] constexpr Target operator|(Target other) const noexcept
90 {
91 Target joined;
92 joined.m_pairs = m_pairs | other.m_pairs;
93 return joined;
94 }
95
96 /** @brief True when the two targets share a (device, runtime) pair. */
97 [[nodiscard]] constexpr bool meets(Target other) const noexcept
98 {
99 return (m_pairs & other.m_pairs) != 0U;
100 }
101
102 /** @brief True when the pair (@p device, @p runtime) is in the set. Both must be single members. */
103 [[nodiscard]] constexpr bool has(Device device, Runtime runtime) const noexcept
104 {
105 return meets(Target { device, runtime });
106 }
107
108 [[nodiscard]] constexpr bool empty() const noexcept { return m_pairs == 0U; }
109
110 /** @brief Devices that appear in any pair. */
111 [[nodiscard]] constexpr Device devices() const noexcept
112 {
113 uint8_t result = 0;
114 for (unsigned device = 0; device < 8; ++device) {
115 if (((m_pairs >> (device * 8U)) & 0xFFU) != 0U) {
116 result |= static_cast<uint8_t>(1U << device);
117 }
118 }
119 return static_cast<Device>(result);
120 }
121
122 /** @brief Runtimes that appear in any pair. */
123 [[nodiscard]] constexpr Runtime runtimes() const noexcept
124 {
125 uint8_t result = 0;
126 for (unsigned device = 0; device < 8; ++device) {
127 result |= static_cast<uint8_t>((m_pairs >> (device * 8U)) & 0xFFU);
128 }
129 return static_cast<Runtime>(result);
130 }
131
132 [[nodiscard]] constexpr bool operator==(const Target&) const noexcept = default;
133
134private:
135 /** One byte per device bit; each byte is the runtime set paired with that device. */
136 uint64_t m_pairs { 0 };
137};
138
139namespace targets {
140 inline constexpr Target cpu { Device::CPU, Runtime::NATIVE };
144}
145
146/**
147 * @brief The target an operation class declares through `static constexpr Target target`,
148 * or the CPU when it declares none.
149 */
150template <typename Op>
151inline constexpr Target target_of_v = [] {
152 if constexpr (requires { { Op::target } -> std::convertible_to<Target>; }) {
153 return Target { Op::target };
154 } else {
155 return targets::cpu;
156 }
157}();
158
159} // namespace MayaFlux::Yantra
@ CPU
Execute the sequence with the Kinesis host executor.
Definition VisionOp.hpp:96
@ GPU
Execute the sequence with the Yantra Vulkan executor.
Definition VisionOp.hpp:95
constexpr bool contains(E set, E members) noexcept
True when set holds every member of members.
constexpr Target target_of_v
The target an operation class declares through static constexpr Target target, or the CPU when it dec...
Device
The silicon the work runs on.
constexpr bool intersects(E a, E b) noexcept
True when the two sets share a member.
ComputationSignature operator|(Left left, Right right)
Compose enum selections in execution order without combining their bits.
constexpr E operator&(E a, E b) noexcept
Runtime
The software stack that executes the work.
@ VULKAN
Vulkan compute shaders.
@ NATIVE
Compiled C++ in this library.
constexpr bool empty() const noexcept
constexpr bool has(Device device, Runtime runtime) const noexcept
True when the pair (device, runtime) is in the set.
constexpr Target() noexcept=default
constexpr bool operator==(const Target &) const noexcept=default
constexpr Target operator|(Target other) const noexcept
constexpr bool meets(Target other) const noexcept
True when the two targets share a (device, runtime) pair.
uint64_t m_pairs
One byte per device bit; each byte is the runtime set paired with that device.
constexpr Device devices() const noexcept
Devices that appear in any pair.
constexpr Runtime runtimes() const noexcept
Runtimes that appear in any pair.
A set of (device, runtime) pairs.