MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
GpuExecutionContext.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "GpuDispatchCore.hpp"
4
5namespace MayaFlux::Yantra {
6
7/**
8 * @class GpuExecutionContext
9 * @brief Type-parameterised shell over GpuDispatchCore.
10 *
11 * Handles the two type-aware boundary steps:
12 * 1. Extracting input data for dispatch_core — overridable via extract_inputs().
13 * 2. Reconstructing a Datum<OutputType> — overridable via collect_gpu_outputs().
14 *
15 * All resource management, buffer staging, dispatch orchestration, and
16 * virtual override points live in GpuDispatchCore and are compiled once
17 * in GpuDispatchCore.cpp.
18 *
19 * Subclasses override GpuDispatchCore virtuals (declare_buffer_bindings,
20 * on_before_gpu_dispatch, prepare_gpu_inputs, calculate_dispatch_size)
21 * directly;
22 * Subclasses that do not operate on numeric channels (e.g. image-only shaders)
23 * override extract_inputs() to return empty channels and override
24 * collect_gpu_outputs() to pull from get_output_image() instead of the float
25 * readback.
26 *
27 * @tparam InputType ComputeData type accepted.
28 * @tparam OutputType ComputeData type produced.
29 */
30template <ComputeData InputType = std::vector<Kakshya::DataVariant>,
31 ComputeData OutputType = InputType>
32class MAYAFLUX_API GpuExecutionContext : public GpuDispatchCore {
33public:
36
38 : GpuDispatchCore(std::move(config))
39 {
40 }
41
42 ~GpuExecutionContext() override = default;
43
48
49 /**
50 * @brief Dispatch to GPU and reconstruct a typed output Datum.
51 *
52 * Routes to dispatch_core_chained for CHAINED mode; all other modes
53 * use dispatch_core. Both paths are defined in GpuDispatchCore.cpp.
54 *
55 * @param input Input Datum to process.
56 * @param ctx ExecutionContext; CHAINED mode requires 'pass_count' and
57 * 'pc_updater' in execution_metadata.
58 * @throws std::runtime_error If GPU initialisation fails.
59 */
60 virtual output_type execute(const input_type& input, const ExecutionContext& ctx)
61 {
62 if (!ensure_gpu_ready()) {
63 error<std::runtime_error>(
64 Journal::Component::Yantra,
65 Journal::Context::BufferProcessing,
66 std::source_location::current(),
67 "GpuExecutionContext: GPU initialisation failed");
68 }
69
70 auto [ch_copies, structure_info] = extract_inputs(input);
71
72 const GpuChannelResult raw = (ctx.mode == ExecutionMode::CHAINED)
73 ? dispatch_core_chained(ch_copies, structure_info, ctx)
74 : dispatch_core(ch_copies, structure_info);
75
76 return collect_gpu_outputs(raw, ch_copies, structure_info);
77 }
78
79protected:
80 /**
81 * @brief Extract double channels and structure metadata from the input Datum.
82 *
83 * Default replicates the previous inline behaviour: calls
84 * OperationHelper::extract_structured_double and copies spans into owned vectors.
85 *
86 * Override to return ({}, {}) when the shader reads only from pre-staged image
87 * or passthrough bindings and channel extraction is meaningless for the input type.
88 */
89 virtual std::pair<std::vector<std::vector<double>>, DataStructureInfo>
91 {
92 auto [spans, structure_info] = OperationHelper::extract_structured_double(
93 const_cast<input_type&>(input));
94
95 std::vector<std::vector<double>> channels(spans.size());
96 for (size_t c = 0; c < spans.size(); ++c)
97 channels[c].assign(spans[c].begin(), spans[c].end());
98
99 return { std::move(channels), std::move(structure_info) };
100 }
101
102 /**
103 * @brief Reconstruct Datum<OutputType> from a GpuChannelResult.
104 *
105 * Reads the primary float readback into per-channel doubles and
106 * delegates to OperationHelper::reconstruct_from_double. Aux entries
107 * are forwarded into result.metadata keyed as "gpu_output_N".
108 *
109 * Override to perform custom readback interpretation, e.g. when the
110 * output buffer layout does not match the input channel structure.
111 *
112 * @param raw Readback produced by dispatch_core or dispatch_core_chained.
113 * @param channels Double channels extracted from the input Datum.
114 * @param structure_info Dimension/modality metadata.
115 */
117 const GpuChannelResult& raw,
118 const std::vector<std::vector<double>>& channels,
119 const DataStructureInfo& structure_info)
120 {
121 output_type result;
122
123 const size_t total_ch_elements = std::accumulate(
124 channels.begin(), channels.end(), size_t { 0 },
125 [](size_t s, const auto& ch) { return s + ch.size(); });
126
127 if (!raw.primary.empty() && !channels.empty()
128 && raw.primary.size() >= total_ch_elements) {
129 size_t offset = 0;
130 std::vector<std::vector<double>> result_ch(channels.size());
131 for (size_t c = 0; c < channels.size(); ++c) {
132 result_ch[c].resize(channels[c].size());
133 for (size_t i = 0; i < channels[c].size(); ++i)
134 result_ch[c][i] = static_cast<double>(raw.primary[offset++]);
135 }
136 result = Datum<OutputType>(
137 OperationHelper::reconstruct_from_double<OutputType>(result_ch, structure_info));
138 }
139
140 for (const auto& [idx, bytes] : raw.aux)
141 result.metadata["gpu_output_" + std::to_string(idx)] = bytes;
142
143 return result;
144 }
145};
146
147} // namespace MayaFlux::Yantra
Range size
Non-template base that owns all type-independent GPU dispatch logic.
GpuExecutionContext & operator=(const GpuExecutionContext &)=delete
virtual output_type execute(const input_type &input, const ExecutionContext &ctx)
Dispatch to GPU and reconstruct a typed output Datum.
GpuExecutionContext(const GpuExecutionContext &)=delete
virtual output_type collect_gpu_outputs(const GpuChannelResult &raw, const std::vector< std::vector< double > > &channels, const DataStructureInfo &structure_info)
Reconstruct Datum<OutputType> from a GpuChannelResult.
GpuExecutionContext(GpuExecutionContext &&)=delete
GpuExecutionContext & operator=(GpuExecutionContext &&)=delete
virtual std::pair< std::vector< std::vector< double > >, DataStructureInfo > extract_inputs(const input_type &input)
Extract double channels and structure metadata from the input Datum.
Type-parameterised shell over GpuDispatchCore.
Metadata about data structure for reconstruction.
std::unordered_map< std::string, std::any > metadata
Associated metadata.
Definition DataIO.hpp:28
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:24
ExecutionMode mode
Execution mode controlling scheduling behavior.
Context information controlling how a compute operation executes.
std::unordered_map< size_t, std::vector< uint8_t > > aux
Erased output of a GPU dispatch: reconstructed float data plus any raw auxiliary outputs keyed by bin...
Plain-data description of the compute shader to dispatch.