MayaFlux 1.0.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 */
70struct MAYAFLUX_API DependencyParams {
71 std::vector<DependencyStage> stages;
72
73 /**
74 * @brief Submit the sequence without waiting for it to complete.
75 *
76 * When true, execute() returns immediately after submission with the
77 * resulting FenceID in the output Datum's metadata under "gpu_fence"
78 * (Portal::Graphics::INVALID_FENCE on submission failure). The caller
79 * polls or waits on that fence and releases it once signaled. When
80 * false (default), execute() blocks until the sequence completes and
81 * returns an empty Datum, as dispatch_core_dependency always has.
82 */
83 bool async { false };
84
90 DependencyParams& operator=(DependencyParams&&) noexcept;
91};
92
93using ExecutionParams = std::variant<
94 std::monostate,
98
99/**
100 * @struct ExecutionContext
101 * @brief Context information controlling how a compute operation executes.
102 *
103 * ExecutionContext provides execution metadata, dependency hints, and hooks
104 * that influence how a Yantra operation is scheduled and run.
105 *
106 * The `execution_metadata` map allows arbitrary user-defined parameters
107 * to be passed into operations. All reads should be performed using the
108 * provided accessors (`get`, `get_or`, `get_or_throw`) which internally
109 * use `safe_any_cast` to provide robust type conversion and diagnostics.
110 *
111 * Typical usage:
112 *
113 * @code
114 * ExecutionContext ctx;
115 *
116 * ctx.set("grain_size", 1024)
117 * .set("hop_size", 512)
118 * .depends_on<MyAnalyzer>();
119 *
120 * auto grain = ctx.get_or<uint32_t>("grain_size", 512);
121 * @endcode
122 */
123struct MAYAFLUX_API ExecutionContext {
124
125 /**
126 * @brief Execution mode controlling scheduling behavior.
127 */
128 ExecutionMode mode = ExecutionMode::SYNC;
129
130 /**
131 * @brief Optional thread pool for asynchronous or parallel execution.
132 */
133 std::shared_ptr<std::thread> thread_pool = nullptr;
134
135 /**
136 * @brief Operation dependencies required before execution.
137 *
138 * Stores type identifiers for operations that must complete before
139 * this context's operation may run.
140 */
141 std::vector<std::type_index> dependencies;
142
143 /**
144 * @brief Optional timeout for operation execution.
145 */
146 std::chrono::milliseconds timeout { 0 };
147
148 /**
149 * @brief Optional parameters specific to the execution mode.
150 *
151 * For example, `ChainedParams` for ExecutionMode::CHAINED,
152 * `ChainedIndirectParams` for ExecutionMode::CHAINED_INDIRECT,
153 * or `DependencyParams` for ExecutionMode::DEPENDENCY.
154 */
156
157 /**
158 * @brief Arbitrary metadata parameters used by operations.
159 *
160 * This key/value store carries runtime configuration such as
161 * algorithm parameters, flags, thresholds, or domain-specific values.
162 *
163 * Values are stored as `std::any` and should be retrieved via
164 * `get()` or `get_or()` to ensure safe casting.
165 */
166 std::unordered_map<std::string, std::any> execution_metadata;
167
168 /**
169 * @brief Optional callback invoked before operation execution.
170 */
171 OperationHookCallback pre_execution_hook = nullptr;
172
173 /**
174 * @brief Optional callback invoked after operation execution.
175 */
176 OperationHookCallback post_execution_hook = nullptr;
177
178 /**
179 * @brief Optional callback used for custom reconstruction of results.
180 */
181 ReconstructionCallback reconstruction_callback = nullptr;
182
183 //=====================================================================
184 // Metadata helpers
185 //=====================================================================
186
187 /**
188 * @brief Insert or update metadata value.
189 *
190 * Adds or replaces a value in the metadata store.
191 *
192 * @tparam T Value type
193 * @param key Metadata key
194 * @param value Value to store
195 * @return Reference to this context for fluent chaining
196 */
197 template <typename T>
198 ExecutionContext& set(std::string key, T&& value)
199 {
200 execution_metadata[std::move(key)] = std::forward<T>(value);
201 return *this;
202 }
203
204 /**
205 * @brief Retrieve metadata value using safe casting.
206 *
207 * Uses `safe_any_cast` internally, allowing numeric conversions
208 * and providing detailed error reporting.
209 *
210 * @tparam T Expected type
211 * @param key Metadata key
212 * @return CastResult containing the value or error details
213 */
214 template <typename T>
215 CastResult<T> get(const std::string& key) const
216 {
217 auto it = execution_metadata.find(key);
218
219 if (it == execution_metadata.end()) {
220 CastResult<T> result;
221 result.error = "ExecutionContext missing key: " + key;
222 return result;
223 }
224
225 return safe_any_cast<T>(it->second);
226 }
227
228 /**
229 * @brief Retrieve metadata value or return a default.
230 *
231 * @tparam T Expected type
232 * @param key Metadata key
233 * @param default_value Value returned if key missing or conversion fails
234 * @return Retrieved or default value
235 */
236 template <typename T>
237 T get_or(const std::string& key, const T& default_value) const
238 {
239 auto it = execution_metadata.find(key);
240
241 if (it == execution_metadata.end())
242 return default_value;
243
244 return safe_any_cast<T>(it->second).value_or(default_value);
245 }
246
247 /**
248 * @brief Retrieve metadata value or throw if unavailable.
249 *
250 * Uses `safe_any_cast_or_throw`.
251 *
252 * @tparam T Expected type
253 * @param key Metadata key
254 * @return Retrieved value
255 *
256 * @throws std::runtime_error if key missing or conversion fails
257 */
258 template <typename T>
259 T get_or_throw(const std::string& key) const
260 {
261 auto it = execution_metadata.find(key);
262
263 if (it == execution_metadata.end())
264 error<std::runtime_error>(Journal::Component::Yantra, Journal::Context::Runtime, std::source_location::current(), "ExecutionContext missing key: {}", key);
265
266 return safe_any_cast_or_throw<T>(it->second);
267 }
268
269 /**
270 * @brief Check whether a metadata key exists.
271 *
272 * @param key Metadata key
273 * @return True if key is present
274 */
275 bool contains(const std::string& key) const
276 {
277 return execution_metadata.contains(key);
278 }
279
280 //=====================================================================
281 // Dependency helpers
282 //=====================================================================
283
284 /**
285 * @brief Register dependency on a specific operation type.
286 *
287 * @tparam T Operation type
288 * @return Reference to this context for fluent chaining
289 */
290 template <typename T>
292 {
293 dependencies.emplace_back(typeid(T));
294 return *this;
295 }
296
297 //=====================================================================
298 // Hook helpers
299 //=====================================================================
300
301 /**
302 * @brief Set pre-execution hook.
303 *
304 * @param cb Callback invoked before operation execution
305 * @return Reference to this context for fluent chaining
306 */
308 {
309 pre_execution_hook = std::move(cb);
310 return *this;
311 }
312
313 /**
314 * @brief Set post-execution hook.
315 *
316 * @param cb Callback invoked after operation execution
317 * @return Reference to this context for fluent chaining
318 */
320 {
321 post_execution_hook = std::move(cb);
322 return *this;
323 }
324
325 /**
326 * @brief Set reconstruction callback.
327 *
328 * @param cb Reconstruction logic for output data
329 * @return Reference to this context for fluent chaining
330 */
332 {
333 reconstruction_callback = std::move(cb);
334 return *this;
335 }
336
337 /**
338 * @brief Set execution timeout.
339 *
340 * @param duration Maximum allowed runtime
341 * @return Reference to this context for fluent chaining
342 */
343 ExecutionContext& with_timeout(std::chrono::milliseconds duration)
344 {
345 timeout = duration;
346 return *this;
347 }
348
349 /**
350 * @brief Set execution mode.
351 *
352 * @param m Desired execution mode
353 * @return Reference to this context for fluent chaining
354 */
356 {
357 mode = m;
358 return *this;
359 }
360};
361
362}
uint32_t mode
glm::vec3 value
@ 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.