MayaFlux 0.5.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
ExecutionContext.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <typeindex>
6
7namespace MayaFlux::Yantra {
8
9struct DependencyStage;
10
11/**
12 * @enum OperationType
13 * @brief Operation categories for organization and discovery
14 */
15enum class OperationType : uint8_t {
17 SORTER,
20 CUSTOM
21};
22
23/**
24 * @enum ExecutionMode
25 * @brief Execution paradigms for operations
26 */
27enum class ExecutionMode : uint8_t {
28 SYNC, ///< Synchronous execution
29 ASYNC, ///< Asynchronous execution
30 PARALLEL, ///< Parallel with other operations
31 CHAINED, ///< Part of a sequential chain
32 CHAINED_INDIRECT, ///< Chained dispatch where a GPU-written indirect buffer gates each pass's workgroup count.
33 DEPENDENCY ///< Part of dependency graph
34};
35
36/**
37 * @brief Callback type for pre/post operation hooks
38 */
39using OperationHookCallback = std::function<void(std::any&)>;
40
41/**
42 * @brief Callback type for custom reconstruction logic
43 */
44using ReconstructionCallback = std::function<std::any(std::vector<std::vector<double>>&, std::any&)>;
45
46/**
47 * @brief Parameters for ExecutionMode::CHAINED.
48 */
50 uint32_t pass_count;
51 std::function<void(uint32_t, void*)> pc_updater;
52 std::optional<uint32_t> passes_per_batch;
53};
54
55/**
56 * @brief Parameters for ExecutionMode::CHAINED_INDIRECT.
57 *
58 * The indirect binding itself is discovered from declare_buffer_bindings
59 * via usage_hint == INDIRECT, not carried here.
60 */
62 uint32_t pass_count;
63 std::function<void(uint32_t, uint32_t, void*)> pc_updater;
64 std::optional<uint32_t> passes_per_batch;
65};
66
67/**
68 * @brief Parameters for ExecutionMode::DEPENDENCY.
69 */
80
81using ExecutionParams = std::variant<
82 std::monostate,
86
87/**
88 * @struct ExecutionContext
89 * @brief Context information controlling how a compute operation executes.
90 *
91 * ExecutionContext provides execution metadata, dependency hints, and hooks
92 * that influence how a Yantra operation is scheduled and run.
93 *
94 * The `execution_metadata` map allows arbitrary user-defined parameters
95 * to be passed into operations. All reads should be performed using the
96 * provided accessors (`get`, `get_or`, `get_or_throw`) which internally
97 * use `safe_any_cast` to provide robust type conversion and diagnostics.
98 *
99 * Typical usage:
100 *
101 * @code
102 * ExecutionContext ctx;
103 *
104 * ctx.set("grain_size", 1024)
105 * .set("hop_size", 512)
106 * .depends_on<MyAnalyzer>();
107 *
108 * auto grain = ctx.get_or<uint32_t>("grain_size", 512);
109 * @endcode
110 */
111struct MAYAFLUX_API ExecutionContext {
112
113 /**
114 * @brief Execution mode controlling scheduling behavior.
115 */
117
118 /**
119 * @brief Optional thread pool for asynchronous or parallel execution.
120 */
121 std::shared_ptr<std::thread> thread_pool = nullptr;
122
123 /**
124 * @brief Operation dependencies required before execution.
125 *
126 * Stores type identifiers for operations that must complete before
127 * this context's operation may run.
128 */
129 std::vector<std::type_index> dependencies;
130
131 /**
132 * @brief Optional timeout for operation execution.
133 */
134 std::chrono::milliseconds timeout { 0 };
135
136 /**
137 * @brief Optional parameters specific to the execution mode.
138 *
139 * For example, `ChainedParams` for ExecutionMode::CHAINED,
140 * `ChainedIndirectParams` for ExecutionMode::CHAINED_INDIRECT,
141 * or `DependencyParams` for ExecutionMode::DEPENDENCY.
142 */
144
145 /**
146 * @brief Arbitrary metadata parameters used by operations.
147 *
148 * This key/value store carries runtime configuration such as
149 * algorithm parameters, flags, thresholds, or domain-specific values.
150 *
151 * Values are stored as `std::any` and should be retrieved via
152 * `get()` or `get_or()` to ensure safe casting.
153 */
154 std::unordered_map<std::string, std::any> execution_metadata;
155
156 /**
157 * @brief Optional callback invoked before operation execution.
158 */
159 OperationHookCallback pre_execution_hook = nullptr;
160
161 /**
162 * @brief Optional callback invoked after operation execution.
163 */
164 OperationHookCallback post_execution_hook = nullptr;
165
166 /**
167 * @brief Optional callback used for custom reconstruction of results.
168 */
169 ReconstructionCallback reconstruction_callback = nullptr;
170
171 //=====================================================================
172 // Metadata helpers
173 //=====================================================================
174
175 /**
176 * @brief Insert or update metadata value.
177 *
178 * Adds or replaces a value in the metadata store.
179 *
180 * @tparam T Value type
181 * @param key Metadata key
182 * @param value Value to store
183 * @return Reference to this context for fluent chaining
184 */
185 template <typename T>
186 ExecutionContext& set(std::string key, T&& value)
187 {
188 execution_metadata[std::move(key)] = std::forward<T>(value);
189 return *this;
190 }
191
192 /**
193 * @brief Retrieve metadata value using safe casting.
194 *
195 * Uses `safe_any_cast` internally, allowing numeric conversions
196 * and providing detailed error reporting.
197 *
198 * @tparam T Expected type
199 * @param key Metadata key
200 * @return CastResult containing the value or error details
201 */
202 template <typename T>
203 CastResult<T> get(const std::string& key) const
204 {
205 auto it = execution_metadata.find(key);
206
207 if (it == execution_metadata.end()) {
208 CastResult<T> result;
209 result.error = "ExecutionContext missing key: " + key;
210 return result;
211 }
212
213 return safe_any_cast<T>(it->second);
214 }
215
216 /**
217 * @brief Retrieve metadata value or return a default.
218 *
219 * @tparam T Expected type
220 * @param key Metadata key
221 * @param default_value Value returned if key missing or conversion fails
222 * @return Retrieved or default value
223 */
224 template <typename T>
225 T get_or(const std::string& key, const T& default_value) const
226 {
227 auto it = execution_metadata.find(key);
228
229 if (it == execution_metadata.end())
230 return default_value;
231
232 return safe_any_cast<T>(it->second).value_or(default_value);
233 }
234
235 /**
236 * @brief Retrieve metadata value or throw if unavailable.
237 *
238 * Uses `safe_any_cast_or_throw`.
239 *
240 * @tparam T Expected type
241 * @param key Metadata key
242 * @return Retrieved value
243 *
244 * @throws std::runtime_error if key missing or conversion fails
245 */
246 template <typename T>
247 T get_or_throw(const std::string& key) const
248 {
249 auto it = execution_metadata.find(key);
250
251 if (it == execution_metadata.end())
252 error<std::runtime_error>(Journal::Component::Yantra, Journal::Context::Runtime, std::source_location::current(), "ExecutionContext missing key: {}", key);
253
254 return safe_any_cast_or_throw<T>(it->second);
255 }
256
257 /**
258 * @brief Check whether a metadata key exists.
259 *
260 * @param key Metadata key
261 * @return True if key is present
262 */
263 bool contains(const std::string& key) const
264 {
265 return execution_metadata.contains(key);
266 }
267
268 //=====================================================================
269 // Dependency helpers
270 //=====================================================================
271
272 /**
273 * @brief Register dependency on a specific operation type.
274 *
275 * @tparam T Operation type
276 * @return Reference to this context for fluent chaining
277 */
278 template <typename T>
280 {
281 dependencies.emplace_back(typeid(T));
282 return *this;
283 }
284
285 //=====================================================================
286 // Hook helpers
287 //=====================================================================
288
289 /**
290 * @brief Set pre-execution hook.
291 *
292 * @param cb Callback invoked before operation execution
293 * @return Reference to this context for fluent chaining
294 */
296 {
297 pre_execution_hook = std::move(cb);
298 return *this;
299 }
300
301 /**
302 * @brief Set post-execution hook.
303 *
304 * @param cb Callback invoked after operation execution
305 * @return Reference to this context for fluent chaining
306 */
308 {
309 post_execution_hook = std::move(cb);
310 return *this;
311 }
312
313 /**
314 * @brief Set reconstruction callback.
315 *
316 * @param cb Reconstruction logic for output data
317 * @return Reference to this context for fluent chaining
318 */
320 {
321 reconstruction_callback = std::move(cb);
322 return *this;
323 }
324
325 /**
326 * @brief Set execution timeout.
327 *
328 * @param duration Maximum allowed runtime
329 * @return Reference to this context for fluent chaining
330 */
331 ExecutionContext& with_timeout(std::chrono::milliseconds duration)
332 {
333 timeout = duration;
334 return *this;
335 }
336
337 /**
338 * @brief Set execution mode.
339 *
340 * @param m Desired execution mode
341 * @return Reference to this context for fluent chaining
342 */
344 {
345 mode = m;
346 return *this;
347 }
348};
349
350}
float value
@ Runtime
General runtime operations (default fallback)
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
@ CUSTOM
User-defined analysis types.
std::function< void(std::any &)> OperationHookCallback
Callback type for pre/post operation hooks.
std::variant< std::monostate, ChainedParams, ChainedIndirectParams, DependencyParams > ExecutionParams
OperationType
Operation categories for organization and discovery.
ExecutionMode
Execution paradigms for operations.
@ SYNC
Synchronous execution.
@ CHAINED
Part of a sequential chain.
@ DEPENDENCY
Part of dependency graph.
@ CHAINED_INDIRECT
Chained dispatch where a GPU-written indirect buffer gates each pass's workgroup count.
@ ASYNC
Asynchronous execution.
@ PARALLEL
Parallel with other operations.
std::function< std::any(std::vector< std::vector< double > > &, std::any &)> ReconstructionCallback
Callback type for custom reconstruction logic.
std::function< void(uint32_t, uint32_t, void *)> pc_updater
Parameters for ExecutionMode::CHAINED_INDIRECT.
std::function< void(uint32_t, void *)> pc_updater
std::optional< uint32_t > passes_per_batch
Parameters for ExecutionMode::CHAINED.
DependencyParams(const DependencyParams &)
std::vector< DependencyStage > stages
DependencyParams(DependencyParams &&) noexcept
Parameters for ExecutionMode::DEPENDENCY.
ExecutionContext & on_reconstruct(ReconstructionCallback cb)
Set reconstruction callback.
std::vector< std::type_index > dependencies
Operation dependencies required before execution.
CastResult< T > get(const std::string &key) const
Retrieve metadata value using safe casting.
ExecutionContext & on_pre(OperationHookCallback cb)
Set pre-execution hook.
ExecutionContext & set_mode(ExecutionMode m)
Set execution mode.
ExecutionContext & with_timeout(std::chrono::milliseconds duration)
Set execution timeout.
bool contains(const std::string &key) const
Check whether a metadata key exists.
ExecutionParams parameters
Optional parameters specific to the execution mode.
T get_or_throw(const std::string &key) const
Retrieve metadata value or throw if unavailable.
std::unordered_map< std::string, std::any > execution_metadata
Arbitrary metadata parameters used by operations.
ExecutionContext & on_post(OperationHookCallback cb)
Set post-execution hook.
T get_or(const std::string &key, const T &default_value) const
Retrieve metadata value or return a default.
ExecutionContext & depends_on()
Register dependency on a specific operation type.
ExecutionContext & set(std::string key, T &&value)
Insert or update metadata value.
Context information controlling how a compute operation executes.