MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
WindowedKernelAnalyzer.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include "Analyzer.hpp"
7#include <Eigen/Dense>
8
12
14
15/**
16 * @file WindowedKernelAnalyzer.hpp
17 * @brief Runs a supplied compute kernel over every window of a sequence.
18 *
19 * The extension counterpart of the library's double analyzers: the kernel is
20 * written by the caller, in GLSL, and runs in float32, a fallback from the
21 * library's double, so its results are accurate only to float precision.
22 */
23
24namespace MayaFlux::Yantra {
25
26/**
27 * @brief Where a windowed kernel is written against.
28 *
29 * The kernel runs once per window of a float sequence. Window w covers
30 * window_length samples starting at offset + w * stride. It is either a GLSL
31 * body, wrapped by windowed_kernel_config, or a whole .comp file that follows
32 * the same layout. Both run in float32, the usual precision of GLSL and not the
33 * library's double, so results agree with a double reference only to float
34 * precision.
35 *
36 * Layout, set 0: bindings 0 to inputs - 1 are the samples (float, read), then
37 * the parameters (float, read; one zero when none are given), then the results
38 * (float, written; outputs values per window, window major). With one input
39 * the bindings are 0, 1 and 2. The push constants are uints: window_length,
40 * stride, offset, window_count and outputs, then a stride and an offset for each
41 * further input. The window length is shared by every input.
42 *
43 * A body sees those names, the first input as samples with sample_at(window, k),
44 * the further inputs as samples2 to samples4 with sample2_at to sample4_at and
45 * their stride2, offset2 and so on, window_base (the first sample of the first
46 * input's window) and result_base (the first result of the window). The kernel
47 * must write every result it declares.
48 *
49 * With one lane the workgroup is 64 and each invocation runs one window: the
50 * window index is i, and windows past window_count return before the body runs.
51 *
52 * With lanes of 2 or more, a power of two up to 1024, one workgroup runs one
53 * window and its lanes share the window's work. The body sees window, lane and
54 * lanes, and these functions, which every lane of the window must call together,
55 * outside any branch that some lanes skip:
56 *
57 * float lane_reduce_sum(float v) the sum of v over the lanes, in a fixed order
58 * float lane_reduce_max(float v) the largest v
59 * float lane_reduce_min(float v) the smallest v
60 * uint lane_first(bool condition) the lowest lane whose condition holds, or lanes when none
61 *
62 * Every lane receives the result. Only one lane should write the results, for
63 * example lane 0. The index of a maximum is lane_first(v == lane_reduce_max(v)).
64 * A .comp file follows the same bindings and push constants, with a workgroup
65 * of lanes, or 64 for one lane, and a group count of window_count for lanes of
66 * 2 or more.
67 */
68inline constexpr uint32_t windowed_kernel_workgroup = 64;
69inline constexpr uint32_t windowed_kernel_max_inputs = 4;
70inline constexpr uint32_t windowed_kernel_max_lanes = 1024;
71
72/**
73 * @struct WindowedKernelShape
74 * @brief How many lanes share a window and how many sequences a kernel reads.
75 *
76 * The default is the original layout, one invocation per window over one
77 * sequence, and it never changes.
78 */
80 uint32_t lanes { 1 };
81 uint32_t inputs { 1 };
82
83 [[nodiscard]] uint32_t workgroup() const noexcept { return lanes > 1 ? lanes : windowed_kernel_workgroup; }
84 [[nodiscard]] uint32_t parameters_binding() const noexcept { return inputs; }
85 [[nodiscard]] uint32_t results_binding() const noexcept { return inputs + 1; }
86 [[nodiscard]] uint32_t push_words() const noexcept { return 5 + 2 * (inputs - 1); }
87 [[nodiscard]] uint32_t groups(uint32_t windows) const noexcept
88 {
89 return lanes > 1 ? windows : (windows + windowed_kernel_workgroup - 1) / windowed_kernel_workgroup;
90 }
91 [[nodiscard]] bool operator==(const WindowedKernelShape&) const noexcept = default;
92};
93
94/**
95 * @brief Why a shape cannot be used, empty when it can.
96 */
97[[nodiscard]] inline std::string windowed_kernel_diagnose(const WindowedKernelShape& shape)
98{
99 if (shape.inputs == 0 || shape.inputs > windowed_kernel_max_inputs) {
100 return "A windowed kernel reads 1 to " + std::to_string(windowed_kernel_max_inputs) + " inputs";
101 }
102 if (shape.lanes == 0 || shape.lanes > windowed_kernel_max_lanes || (shape.lanes & (shape.lanes - 1)) != 0) {
103 return "Lanes must be 1 or a power of two up to " + std::to_string(windowed_kernel_max_lanes);
104 }
105 return {};
106}
107
108/**
109 * @brief The binding layout of a windowed kernel.
110 */
111[[nodiscard]] inline std::vector<GpuBufferBinding> windowed_kernel_layout(const WindowedKernelShape& shape = {})
112{
113 std::vector<GpuBufferBinding> layout;
114 for (uint32_t binding = 0; binding < shape.inputs; ++binding) {
115 layout.push_back({ .set = 0, .binding = binding, .direction = GpuBufferBinding::Direction::INPUT, .element_type = GpuBufferBinding::ElementType::FLOAT32 });
116 }
117 layout.push_back({ .set = 0, .binding = shape.parameters_binding(), .direction = GpuBufferBinding::Direction::INPUT, .element_type = GpuBufferBinding::ElementType::FLOAT32 });
118 layout.push_back({ .set = 0, .binding = shape.results_binding(), .direction = GpuBufferBinding::Direction::OUTPUT, .element_type = GpuBufferBinding::ElementType::FLOAT32 });
119 return layout;
120}
121
122namespace detail {
123
124 inline std::string lane_preamble(uint32_t lanes)
125 {
126 const std::string n = std::to_string(lanes);
127 return "shared float lane_scratch[" + n + "];\nshared uint lane_scratch_u[" + n + "];";
128 }
129
130 inline std::string lane_reduction(std::string_view scratch, std::string_view type, std::string_view combine)
131 {
132 std::string body;
133 body += " uint l = gl_LocalInvocationID.x;\n";
134 body += " " + std::string(scratch) + "[l] = v;\n";
135 body += " barrier();\n";
136 body += " for (uint s = gl_WorkGroupSize.x / 2u; s > 0u; s >>= 1u) {\n";
137 body += " if (l < s) { " + std::string(scratch) + "[l] = " + std::string(combine) + "; }\n";
138 body += " barrier();\n";
139 body += " }\n";
140 body += " " + std::string(type) + " r = " + std::string(scratch) + "[0];\n";
141 body += " barrier();\n";
142 body += " return r;";
143 return body;
144 }
145
146} // namespace detail
147
148/**
149 * @brief Config of a GLSL body wrapped in the layout.
150 * @param body Statements run for each window.
151 * @param shape Lanes per window and inputs.
152 * @return A config whose shader_id is INVALID_SHADER when the body does not compile or the shape is unusable.
153 */
154[[nodiscard]] inline GpuComputeConfig windowed_kernel_config(const std::string& body, const WindowedKernelShape& shape = {})
155{
157 if (!windowed_kernel_diagnose(shape).empty()) {
158 return {};
159 }
160
161 std::string text;
162 if (shape.lanes > 1) {
163 text += " uint window = gl_WorkGroupID.x;\n";
164 text += " uint lane = gl_LocalInvocationID.x;\n";
165 text += " uint lanes = gl_WorkGroupSize.x;\n";
166 text += " if (window >= window_count) { return; }\n";
167 text += " uint window_base = offset + window * stride;\n";
168 text += " uint result_base = window * outputs;\n";
169 } else {
170 text += " if (i >= window_count) { return; }\n";
171 text += " uint window_base = offset + i * stride;\n";
172 text += " uint result_base = i * outputs;\n";
173 }
174 text += body;
175
176 auto assemble = Portal::Graphics::ShaderSpec::Assemble {};
177 assemble.ssbo("samples", BindingDirection::Input, Kakshya::GpuDataFormat::FLOAT32);
178 for (uint32_t input = 2; input <= shape.inputs; ++input) {
179 assemble.ssbo("samples" + std::to_string(input), BindingDirection::Input, Kakshya::GpuDataFormat::FLOAT32);
180 }
181 assemble.ssbo("parameters", BindingDirection::Input, Kakshya::GpuDataFormat::FLOAT32)
182 .ssbo("results", BindingDirection::Output, Kakshya::GpuDataFormat::FLOAT32)
183 .pc("window_length", Kakshya::GpuDataFormat::UINT32)
184 .pc("stride", Kakshya::GpuDataFormat::UINT32)
185 .pc("offset", Kakshya::GpuDataFormat::UINT32)
186 .pc("window_count", Kakshya::GpuDataFormat::UINT32)
187 .pc("outputs", Kakshya::GpuDataFormat::UINT32);
188 for (uint32_t input = 2; input <= shape.inputs; ++input) {
189 assemble.pc("stride" + std::to_string(input), Kakshya::GpuDataFormat::UINT32)
190 .pc("offset" + std::to_string(input), Kakshya::GpuDataFormat::UINT32);
191 }
192 assemble.function("float", "sample_at", "uint window, uint k",
193 " return samples[pc.offset + window * pc.stride + k];");
194 for (uint32_t input = 2; input <= shape.inputs; ++input) {
195 const std::string tag = std::to_string(input);
196 assemble.function("float", "sample" + tag + "_at", "uint window, uint k",
197 " return samples" + tag + "[pc.offset" + tag + " + window * pc.stride" + tag + " + k];");
198 }
199 if (shape.lanes > 1) {
200 assemble.preamble(detail::lane_preamble(shape.lanes))
201 .function("float", "lane_reduce_sum", "float v",
202 detail::lane_reduction("lane_scratch", "float", "lane_scratch[l] + lane_scratch[l + s]"))
203 .function("float", "lane_reduce_max", "float v",
204 detail::lane_reduction("lane_scratch", "float", "max(lane_scratch[l], lane_scratch[l + s])"))
205 .function("float", "lane_reduce_min", "float v",
206 detail::lane_reduction("lane_scratch", "float", "min(lane_scratch[l], lane_scratch[l + s])"))
207 .function("uint", "lane_first", "bool condition",
208 " uint v = condition ? gl_LocalInvocationID.x : gl_WorkGroupSize.x;\n"
209 + detail::lane_reduction("lane_scratch_u", "uint", "min(lane_scratch_u[l], lane_scratch_u[l + s])"));
210 }
211
212 return config_from_spec(assemble.kernel(Portal::Graphics::KernelSource { .body = std::move(text) })
213 .workgroup(shape.workgroup())
214 .build());
215}
216
217/**
218 * @brief Config of a .comp file that follows the layout.
219 */
220[[nodiscard]] inline GpuComputeConfig windowed_kernel_file_config(std::string path, const WindowedKernelShape& shape = {})
221{
222 return GpuComputeConfig {
223 .shader_path = std::move(path),
224 .workgroup_size = { shape.workgroup(), 1, 1 },
225 .push_constant_size = shape.push_words() * sizeof(uint32_t),
226 };
227}
228
229/**
230 * @struct WindowedInput
231 * @brief One sequence a windowed kernel reads, and how its windows are laid out.
232 *
233 * Window w of the input starts at offset + w * stride. A resident input is not
234 * staged: its binding already resolves to a buffer of this context or one
235 * imported from another with import_shared.
236 */
238 std::span<const float> samples;
239 uint32_t stride { 0 };
240 uint32_t offset { 0 };
241 bool resident { false };
242
243 /** @brief An input that is already on the device. */
244 [[nodiscard]] static WindowedInput on_device(uint32_t stride, uint32_t offset = 0)
245 {
246 return { .stride = stride, .offset = offset, .resident = true };
247 }
248};
249
250/**
251 * @brief Run a windowed kernel over every window of one or more sequences.
252 * @param context Context built with config and windowed_kernel_layout(shape).
253 * @param config The config the context was built from.
254 * @param shape The shape the config was built for.
255 * @param inputs One entry per input of the shape. Every window must lie inside its input.
256 * @param window_length Samples in each window, the same for every input.
257 * @param window_count Windows to run.
258 * @param outputs Results per window.
259 * @param parameters Floats the kernel reads from the parameters binding.
260 * @return window_count * outputs floats, window major, empty when there is nothing to run.
261 * @throws std::invalid_argument when the number of inputs does not match the shape.
262 */
263[[nodiscard]] inline std::vector<float> run_windowed_kernel(
264 ShaderExecutionContext<>& context, const GpuComputeConfig& config, const WindowedKernelShape& shape,
265 std::span<const WindowedInput> inputs, uint32_t window_length, uint32_t window_count,
266 uint32_t outputs, std::span<const float> parameters)
267{
268 if (inputs.size() != shape.inputs) {
270 std::source_location::current(), "A kernel of {} inputs was given {}", shape.inputs, inputs.size());
271 }
272 if (window_count == 0 || outputs == 0) {
273 return {};
274 }
275
276 std::vector<uint32_t> push { window_length, inputs[0].stride, inputs[0].offset, window_count, outputs };
277 for (size_t input = 1; input < inputs.size(); ++input) {
278 push.push_back(inputs[input].stride);
279 push.push_back(inputs[input].offset);
280 }
281 const size_t result_bytes = static_cast<size_t>(window_count) * outputs * sizeof(float);
282 const float none = 0.0F;
283 const std::vector<WindowedInput> staged(inputs.begin(), inputs.end());
284
286 .config = config,
287 .stage_fn = [staged, parameters, &none, result_bytes, push, shape](GpuDispatchCore& core) {
288 for (size_t input = 0; input < staged.size(); ++input) {
289 if (!staged[input].resident) {
290 core.set_binding_data(input, staged[input].samples);
291 }
292 }
293 core.set_binding_data(shape.parameters_binding(), parameters.empty() ? std::span<const float>(&none, 1) : parameters);
294 core.set_output_size(shape.results_binding(), result_bytes);
295 core.set_push_constants(push.data(), push.size() * sizeof(uint32_t));
296 },
297 .explicit_groups = std::array<uint32_t, 3> { shape.groups(window_count), 1U, 1U },
298 } });
299
300 std::vector<float> results(static_cast<size_t>(window_count) * outputs);
301 context.download_binding(shape.results_binding(), results.data(), result_bytes);
302 return results;
303}
304
305/**
306 * @brief Run a one input, one invocation per window kernel over every window.
307 * @param context Context built with config and windowed_kernel_layout.
308 * @param config The config the context was built from.
309 * @param samples Floats the windows index into. Every window must lie inside it.
310 * @param geometry Window length, stride, first offset and count.
311 * @param outputs Results per window.
312 * @param parameters Floats the kernel reads from binding 1.
313 * @return geometry.count * outputs floats, window major, empty when there is nothing to run.
314 */
315[[nodiscard]] inline std::vector<float> run_windowed_kernel(
316 ShaderExecutionContext<>& context, const GpuComputeConfig& config, std::span<const float> samples,
317 const SegmentGeometry& geometry, uint32_t outputs, std::span<const float> parameters)
318{
319 const WindowedInput input { .samples = samples, .stride = geometry.stride, .offset = geometry.offset };
320 return run_windowed_kernel(context, config, WindowedKernelShape {}, std::span<const WindowedInput>(&input, 1),
321 geometry.length, geometry.count, outputs, parameters);
322}
323
324/**
325 * @struct WindowedKernelConfig
326 * @brief Settings of a WindowedKernelAnalyzer.
327 *
328 * Exactly one of kernel and shader_path is set. kernel is the body of the
329 * per-window GLSL function, in the layout described above windowed_kernel_workgroup.
330 * shader_path names a whole .comp file written against the same layout.
331 *
332 * lanes is how many invocations share a window, 1 or a power of two up to 1024.
333 * inputs is how many sequences the kernel reads. With one input every channel is
334 * analysed on its own. With more, the first inputs channels of the data are the
335 * inputs of one kernel run, with the same window and hop, and the analysis has
336 * one channel; windows run while every input has them.
337 */
339 [[nodiscard]] WindowedKernelShape shape() const noexcept { return { .lanes = lanes, .inputs = inputs }; }
340
341 /** Results the kernel writes for each window. */
342 uint32_t outputs { 1 };
343 /** GLSL statements run for each window. */
344 std::string kernel;
345 /** A .comp file following the windowed kernel layout, used instead of kernel. */
346 std::string shader_path;
347 /** Floats the kernel reads as parameters[k]. */
348 std::vector<float> parameters;
349 /** Invocations that share one window. */
350 uint32_t lanes { 1 };
351 /** Sequences the kernel reads. */
352 uint32_t inputs { 1 };
353};
354
355MF_DESCRIBE(WindowedKernelConfig)
356
357/**
358 * @struct ChannelWindowedKernel
359 * @brief Kernel results of one channel.
360 */
361struct MAYAFLUX_API ChannelWindowedKernel {
362 /** Window major: window w, output j is values[w * outputs + j]. */
363 std::vector<float> values;
364 /** Results per window. */
365 uint32_t outputs { 0 };
366 /** First and one past the last sample of each window. */
367 std::vector<std::pair<size_t, size_t>> window_positions;
368};
369
370/**
371 * @struct WindowedKernelAnalysis
372 * @brief Analysis result of a WindowedKernelAnalyzer.
373 */
374struct MAYAFLUX_API WindowedKernelAnalysis {
375 /** Settings the analysis was produced under. */
377 std::vector<ChannelWindowedKernel> channels;
378};
379
381
382/**
383 * @class WindowedKernelAnalyzer
384 * @brief Runs a caller's compute kernel once per window of every channel.
385 *
386 * Where WaveformAnalyzer and SpectralAnalyzer are fixed measurements computed
387 * in double, this is the place for a measurement the library does not have. The
388 * kernel reads the window through sample_at and writes its results to the
389 * result array; see the layout described above windowed_kernel_workgroup for the names it sees. Samples are
390 * converted to float, the kernel runs in float32 as a fallback from double, and
391 * results are float, so they agree with a double reference only to float
392 * precision.
393 *
394 * Nothing about the windows is specific to audio: a window is a run of
395 * consecutive values of any one-dimensional sequence. It is on the vulkan
396 * target, has no CPU twin, and is not registered with the default registry
397 * because it has no fixed selection to resolve by. A kernel that does not
398 * compile makes the analysis fail with the shader log's message.
399 */
400template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = Eigen::MatrixXd>
401class WindowedKernelAnalyzer : public Analyzer<InputType, OutputType> {
402public:
407
408 static constexpr Target target = targets::vulkan;
409
410 /**
411 * @brief Construct from a kernel and its window settings.
412 * @param config Settings, validated before they are stored.
413 */
419
420 [[nodiscard]] ConfigRefs configs() const override
421 {
422 return this->stored_configs(supports {});
423 }
424
425 /**
426 * @brief Type-safe windowed kernel analysis
427 */
429 {
430 auto result = this->analyze_data(data);
431 return safe_any_cast_or_throw<WindowedKernelAnalysis>(result);
432 }
433
435 {
436 return this->analyze_windows(input_type { data });
437 }
438
440 {
441 return safe_any_cast_or_throw<WindowedKernelAnalysis>(this->get_current_analysis());
442 }
443
444 [[nodiscard]] AnalysisType get_analysis_type() const override
445 {
447 }
448
449 [[nodiscard]] std::string get_name() const override
450 {
451 return "WindowedKernelAnalyzer";
452 }
453
454 /**
455 * @brief One while a device is up, zero otherwise.
456 */
457 static float fitness(const input_type& /*input*/)
458 {
460 }
461
462protected:
464 {
465 try {
466 const auto& cfg = this->template config<WindowedKernelConfig>();
467
468 auto [data_span, structure_info] = OperationHelper::extract_structured_double(
469 const_cast<input_type&>(input));
470
471 auto& context = this->context(cfg);
472
473 WindowedKernelAnalysis analysis;
474 analysis.config = cfg;
475
476 std::vector<std::vector<double>> channel_values;
477
478 if (cfg.inputs == 1) {
479 analysis.channels.resize(data_span.size());
480 channel_values.reserve(data_span.size());
481 for (size_t channel = 0; channel < data_span.size(); ++channel) {
482 const std::array<std::span<const double>, 1> inputs { std::span<const double> { data_span[channel].data(), data_span[channel].size() } };
483 analyse(context, analysis.channels[channel], inputs, cfg);
484 channel_values.emplace_back(analysis.channels[channel].values.begin(), analysis.channels[channel].values.end());
485 }
486 } else {
487 if (data_span.size() < cfg.inputs) {
489 std::source_location::current(), "A kernel of {} inputs needs that many channels and {} were given", cfg.inputs, data_span.size());
490 }
491 std::vector<std::span<const double>> inputs;
492 for (uint32_t input = 0; input < cfg.inputs; ++input) {
493 inputs.emplace_back(data_span[input].data(), data_span[input].size());
494 }
495 analysis.channels.resize(1);
496 analyse(context, analysis.channels[0], inputs, cfg);
497 channel_values.emplace_back(analysis.channels[0].values.begin(), analysis.channels[0].values.end());
498 }
499
500 this->store_current_analysis(analysis);
501
502 output_type output = this->convert_result(channel_values, structure_info);
503 output.metadata = input.metadata;
504 output.facets = input.facets;
505 output.facets.set(analysis);
506 return output;
507 } catch (const std::exception& e) {
508 MF_ERROR(Journal::Component::Yantra, Journal::Context::ComputeMatrix, "Windowed kernel analysis failed: {}", e.what());
509 output_type error_result;
510 error_result.metadata = input.metadata;
511 error_result.facets = input.facets;
512 return error_result;
513 }
514 }
515
516private:
517 static constexpr size_t k_chunk_windows = 16384;
518
519 mutable std::unique_ptr<ShaderExecutionContext<>> m_context;
521 mutable std::string m_key;
522
523 static void validate_config(const WindowedKernelConfig& cfg)
524 {
525 const auto fail = [](std::string_view message) {
527 std::source_location::current(), "{}", message);
528 };
529 if (cfg.kernel.empty() == cfg.shader_path.empty()) {
530 fail("Exactly one of kernel and shader_path must be set");
531 }
532 if (cfg.outputs == 0) {
533 fail("A kernel must write at least one output per window");
534 }
535 if (cfg.window_size == 0 || cfg.hop_size == 0) {
536 fail("Window and hop sizes must be greater than 0");
537 }
538 if (const std::string reason = windowed_kernel_diagnose(cfg.shape()); !reason.empty()) {
539 fail(reason);
540 }
541 }
542
544 {
545 const auto shape = cfg.shape();
546 const std::string key = (cfg.kernel.empty() ? "file:" + cfg.shader_path : "body:" + cfg.kernel)
547 + "#" + std::to_string(shape.lanes) + "x" + std::to_string(shape.inputs);
548 if (!m_context || m_key != key) {
549 GpuComputeConfig gpu = cfg.kernel.empty()
551 : windowed_kernel_config(cfg.kernel, shape);
552 if (gpu.shader_id == Portal::Graphics::INVALID_SHADER && gpu.shader_path.empty()) {
554 std::source_location::current(), "The kernel did not compile; the shader log has the compiler's message");
555 }
556 m_context = std::make_unique<ShaderExecutionContext<>>(gpu, windowed_kernel_layout(shape), "WindowedKernel");
557 m_gpu = std::move(gpu);
558 m_key = key;
559 }
560 return *m_context;
561 }
562
564 std::span<const std::span<const double>> inputs, const WindowedKernelConfig& cfg) const
565 {
566 size_t shortest = inputs.front().size();
567 for (const auto& input : inputs) {
568 shortest = std::min(shortest, input.size());
569 }
570 const size_t windows = shortest < cfg.window_size ? 0 : (shortest - cfg.window_size) / cfg.hop_size + 1;
571
572 result.outputs = cfg.outputs;
573 for (size_t first = 0; first < windows; first += k_chunk_windows) {
574 const size_t count = std::min(k_chunk_windows, windows - first);
575 run_chunk(context, result, inputs, first, count, cfg);
576 }
577 }
578
580 std::span<const std::span<const double>> inputs, size_t first, size_t count, const WindowedKernelConfig& cfg) const
581 {
582 const size_t begin = first * cfg.hop_size;
583 const size_t end = (first + count - 1) * cfg.hop_size + cfg.window_size;
584
585 std::vector<std::vector<float>> chunks;
586 chunks.reserve(inputs.size());
587 std::vector<WindowedInput> bound;
588 bound.reserve(inputs.size());
589 for (const auto& samples : inputs) {
590 chunks.emplace_back(samples.begin() + static_cast<std::ptrdiff_t>(begin), samples.begin() + static_cast<std::ptrdiff_t>(end));
591 bound.push_back({ .samples = chunks.back(), .stride = cfg.hop_size, .offset = 0 });
592 }
593
594 const auto values = run_windowed_kernel(context, m_gpu, cfg.shape(), bound, cfg.window_size,
595 static_cast<uint32_t>(count), cfg.outputs, cfg.parameters);
596 out.values.insert(out.values.end(), values.begin(), values.end());
597 for (size_t w = 0; w < count; ++w) {
598 const size_t start = (first + w) * cfg.hop_size;
599 out.window_positions.emplace_back(start, start + cfg.window_size);
600 }
601 }
602};
603
604/// Standard windowed kernel analyzer: DataVariant -> MatrixXd
606
607/// Container windowed kernel analyzer: SignalContainer -> MatrixXd
609
610} // namespace MayaFlux::Yantra
Modern, digital-first analyzer framework for Maya Flux.
#define MF_ERROR(comp, ctx,...)
#define MF_DESCRIBE(...)
Requests a generated member description for a struct at namespace scope.
Definition Describe.hpp:14
void set(T value)
Stores a struct under its type identity.
Definition Facets.hpp:107
bool is_initialized() const
Check if compiler is initialized.
void store_current_analysis(AnalysisResultType &&result) const
Definition Analyzer.hpp:234
std::any get_current_analysis() const
Access cached analysis from last operation.
Definition Analyzer.hpp:175
std::any analyze_data(const input_type &data)
User-facing analysis method - returns analysis results directly.
Definition Analyzer.hpp:138
Template-flexible analyzer base with instance-defined I/O types.
Definition Analyzer.hpp:101
void set_config(Config config)
Stores a config struct, whole, under its own type.
ConfigRefs stored_configs(Supports< Configs... >) const
The stored configs among those listed, in listed order.
output_type convert_result(std::vector< std::vector< double > > &result_data, DataStructureInfo &metadata)
Convert processed double data back to OutputType using metadata and optional callback.
void download_binding(size_t index, void *dest, size_t byte_size)
Read back a specific binding into a caller-provided destination.
void record_stages(const std::vector< DependencyStage > &stages)
Stage and record each stage in order, one dispatch per stage.
Non-template base that owns all type-independent GPU dispatch logic.
static std::tuple< NumericChannels, DataStructureInfo > extract_structured_double(T &compute_data)
Extract structured double data from Datum container or direct ComputeData with automatic container ha...
Concrete GpuExecutionContext for a single fixed shader with fixed bindings.
AnalysisType get_analysis_type() const override
Gets the analysis type category for this analyzer.
WindowedKernelAnalysis analyze_windows(const input_type &data)
Type-safe windowed kernel analysis.
ConfigRefs configs() const override
The base view of the config structs this operation holds.
WindowedKernelAnalysis get_windowed_kernel_analysis() const
void analyse(ShaderExecutionContext<> &context, ChannelWindowedKernel &result, std::span< const std::span< const double > > inputs, const WindowedKernelConfig &cfg) const
void run_chunk(ShaderExecutionContext<> &context, ChannelWindowedKernel &out, std::span< const std::span< const double > > inputs, size_t first, size_t count, const WindowedKernelConfig &cfg) const
WindowedKernelAnalysis analyze_windows(const InputType &data)
std::string get_name() const override
Get operation name for debugging/introspection.
WindowedKernelAnalyzer(WindowedKernelConfig config)
Construct from a kernel and its window settings.
static float fitness(const input_type &)
One while a device is up, zero otherwise.
static void validate_config(const WindowedKernelConfig &cfg)
Analyzer< InputType, OutputType > base_type
output_type analyze_implementation(const input_type &input) override
Pure virtual analysis implementation - derived classes implement this.
Runs a caller's compute kernel once per window of every channel.
@ ComputeMatrix
Compute operations (Yantra - algorithms, matrices, DSP).
@ Yantra
DSP algorithms, computational units, matrix operations, Grammar.
@ FLOAT32
32-bit float (standard GPU)
constexpr ShaderID INVALID_SHADER
MAYAFLUX_API ShaderFoundry & get_shader_foundry()
Get the global shader compiler instance.
BindingDirection
Data flow direction for a shader binding slot.
std::string lane_reduction(std::string_view scratch, std::string_view type, std::string_view combine)
std::string lane_preamble(uint32_t lanes)
GpuComputeConfig windowed_kernel_config(const std::string &body, const WindowedKernelShape &shape={})
Config of a GLSL body wrapped in the layout.
std::vector< float > run_windowed_kernel(ShaderExecutionContext<> &context, const GpuComputeConfig &config, const WindowedKernelShape &shape, std::span< const WindowedInput > inputs, uint32_t window_length, uint32_t window_count, uint32_t outputs, std::span< const float > parameters)
Run a windowed kernel over every window of one or more sequences.
AnalysisType
Categories of analysis operations for discovery and organization.
Definition Analyzer.hpp:33
@ CUSTOM
User-defined analysis types.
Definition Analyzer.hpp:41
std::string windowed_kernel_diagnose(const WindowedKernelShape &shape)
Why a shape cannot be used, empty when it can.
constexpr uint32_t windowed_kernel_max_inputs
GpuComputeConfig windowed_kernel_file_config(std::string path, const WindowedKernelShape &shape={})
Config of a .comp file that follows the layout.
Portal::Graphics::GpuComputeConfig GpuComputeConfig
constexpr uint32_t windowed_kernel_workgroup
Where a windowed kernel is written against.
std::vector< GpuBufferBinding > windowed_kernel_layout(const WindowedKernelShape &shape={})
The binding layout of a windowed kernel.
WindowedKernelAnalyzer< std::vector< Kakshya::DataVariant >, Eigen::MatrixXd > StandardWindowedKernelAnalyzer
Standard windowed kernel analyzer: DataVariant -> MatrixXd.
WindowedKernelAnalyzer< std::shared_ptr< Kakshya::SignalSourceContainer >, Eigen::MatrixXd > ContainerWindowedKernelAnalyzer
Container windowed kernel analyzer: SignalContainer -> MatrixXd.
constexpr uint32_t windowed_kernel_max_lanes
GpuComputeConfig config_from_spec(const Portal::Graphics::ShaderSpec &spec)
Derive a GpuComputeConfig from a ShaderSpec.
std::vector< std::reference_wrapper< const OperationConfig > > ConfigRefs
Borrowed views of the configs an operation holds.
enum MayaFlux::Portal::Graphics::GpuBufferBinding::Direction INPUT
enum MayaFlux::Portal::Graphics::GpuBufferBinding::ElementType FLOAT32
Plain-data description of the compute shader to dispatch.
uint32_t window_size
Analysis window length in samples.
Definition Analyzer.hpp:64
uint32_t hop_size
Distance between successive windows in samples.
Definition Analyzer.hpp:66
Settings every analyzer family accepts.
Definition Analyzer.hpp:62
std::vector< std::pair< size_t, size_t > > window_positions
First and one past the last sample of each window.
std::vector< float > values
Window major: window w, output j is values[w * outputs + j].
Facets facets
Typed results that travel with the data, read by type.
Definition DataIO.hpp:68
std::unordered_map< std::string, std::any > metadata
Associated metadata.
Definition DataIO.hpp:67
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:63
One stage in a record_stages call.
Where the windows of a GPU pass sit in the value array.
The config structs an operation class accepts, declared as using supports.
A set of (device, runtime) pairs.
static WindowedInput on_device(uint32_t stride, uint32_t offset=0)
An input that is already on the device.
One sequence a windowed kernel reads, and how its windows are laid out.
std::vector< ChannelWindowedKernel > channels
WindowedKernelConfig config
Settings the analysis was produced under.
Analysis result of a WindowedKernelAnalyzer.
uint32_t lanes
Invocations that share one window.
uint32_t inputs
Sequences the kernel reads.
std::vector< float > parameters
Floats the kernel reads as parameters[k].
std::string kernel
GLSL statements run for each window.
std::string shader_path
A .comp file following the windowed kernel layout, used instead of kernel.
uint32_t outputs
Results the kernel writes for each window.
WindowedKernelShape shape() const noexcept
Settings of a WindowedKernelAnalyzer.
bool operator==(const WindowedKernelShape &) const noexcept=default
uint32_t groups(uint32_t windows) const noexcept
How many lanes share a window and how many sequences a kernel reads.