87 [[nodiscard]] uint32_t
groups(uint32_t windows)
const noexcept
113 std::vector<GpuBufferBinding> layout;
114 for (uint32_t binding = 0; binding < shape.inputs; ++binding) {
126 const std::string n = std::to_string(lanes);
127 return "shared float lane_scratch[" + n +
"];\nshared uint lane_scratch_u[" + n +
"];";
130 inline std::string
lane_reduction(std::string_view scratch, std::string_view type, std::string_view combine)
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";
140 body +=
" " + std::string(type) +
" r = " + std::string(scratch) +
"[0];\n";
141 body +=
" barrier();\n";
142 body +=
" return r;";
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";
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";
176 auto assemble = Portal::Graphics::ShaderSpec::Assemble {};
178 for (uint32_t input = 2; input <= shape.inputs; ++input) {
188 for (uint32_t input = 2; input <= shape.inputs; ++input) {
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];");
199 if (shape.lanes > 1) {
201 .function(
"float",
"lane_reduce_sum",
"float v",
203 .function(
"float",
"lane_reduce_max",
"float v",
205 .function(
"float",
"lane_reduce_min",
"float v",
207 .function(
"uint",
"lane_first",
"bool condition",
208 " uint v = condition ? gl_LocalInvocationID.x : gl_WorkGroupSize.x;\n"
212 return config_from_spec(assemble.kernel(Portal::Graphics::KernelSource { .body = std::move(text) })
213 .workgroup(shape.workgroup())
223 .shader_path = std::move(path),
224 .workgroup_size = { shape.workgroup(), 1, 1 },
225 .push_constant_size = shape.push_words() *
sizeof(uint32_t),
246 return { .stride =
stride, .offset =
offset, .resident =
true };
265 std::span<const WindowedInput> inputs, uint32_t window_length, uint32_t window_count,
266 uint32_t outputs, std::span<const float> parameters)
268 if (inputs.size() != shape.
inputs) {
270 std::source_location::current(),
"A kernel of {} inputs was given {}", shape.
inputs, inputs.size());
272 if (window_count == 0 || outputs == 0) {
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);
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());
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);
293 core.set_binding_data(shape.
parameters_binding(), parameters.empty() ? std::span<const float>(&none, 1) : parameters);
295 core.set_push_constants(push.data(), push.size() *
sizeof(uint32_t));
297 .explicit_groups = std::array<uint32_t, 3> { shape.
groups(window_count), 1U, 1U },
300 std::vector<float> results(
static_cast<size_t>(window_count) * outputs);
317 const SegmentGeometry& geometry, uint32_t outputs, std::span<const float> parameters)
321 geometry.
length, geometry.
count, outputs, parameters);
400template <ComputeData InputType = std::vector<Kakshya::DataVariant>, ComputeData OutputType = Eigen::MatrixXd>
431 return safe_any_cast_or_throw<WindowedKernelAnalysis>(result);
449 [[nodiscard]] std::string
get_name()
const override
451 return "WindowedKernelAnalyzer";
476 std::vector<std::vector<double>> channel_values;
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() } };
484 channel_values.emplace_back(analysis.
channels[channel].values.begin(), analysis.
channels[channel].values.end());
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());
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());
497 channel_values.emplace_back(analysis.
channels[0].values.begin(), analysis.
channels[0].values.end());
507 }
catch (
const std::exception& e) {
519 mutable std::unique_ptr<ShaderExecutionContext<>>
m_context;
525 const auto fail = [](std::string_view message) {
527 std::source_location::current(),
"{}", message);
530 fail(
"Exactly one of kernel and shader_path must be set");
533 fail(
"A kernel must write at least one output per window");
536 fail(
"Window and hop sizes must be greater than 0");
545 const auto shape = cfg.
shape();
547 +
"#" + std::to_string(shape.lanes) +
"x" + std::to_string(shape.inputs);
554 std::source_location::current(),
"The kernel did not compile; the shader log has the compiler's message");
557 m_gpu = std::move(gpu);
566 size_t shortest = inputs.front().size();
567 for (
const auto& input : inputs) {
568 shortest = std::min(shortest, input.size());
580 std::span<
const std::span<const double>> inputs,
size_t first,
size_t count,
const WindowedKernelConfig& cfg)
const
582 const size_t begin = first * cfg.
hop_size;
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 });
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;
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.
void set(T value)
Stores a struct under its type identity.
bool is_initialized() const
Check if compiler is initialized.
void store_current_analysis(AnalysisResultType &&result) const
std::any get_current_analysis() const
Access cached analysis from last operation.
std::any analyze_data(const input_type &data)
User-facing analysis method - returns analysis results directly.
Template-flexible analyzer base with instance-defined I/O types.
const Config & config() const
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.
static constexpr Target target
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.
Datum< OutputType > output_type
ConfigRefs configs() const override
The base view of the config structs this operation holds.
std::unique_ptr< ShaderExecutionContext<> > m_context
Datum< InputType > input_type
WindowedKernelAnalysis get_windowed_kernel_analysis() const
Supports< WindowedKernelConfig > supports
static constexpr size_t k_chunk_windows
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.
ShaderExecutionContext & context(const WindowedKernelConfig &cfg) const
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.
@ CUSTOM
User-defined analysis types.
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.
uint32_t hop_size
Distance between successive windows in samples.
Settings every analyzer family accepts.
std::vector< std::pair< size_t, size_t > > window_positions
First and one past the last sample of each window.
uint32_t outputs
Results per window.
std::vector< float > values
Window major: window w, output j is values[w * outputs + j].
Kernel results of one channel.
Facets facets
Typed results that travel with the data, read by type.
std::unordered_map< std::string, std::any > metadata
Associated metadata.
Input/Output container for computation pipeline data flow with structure preservation.
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.
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.
uint32_t workgroup() const noexcept
uint32_t results_binding() const noexcept
bool operator==(const WindowedKernelShape &) const noexcept=default
uint32_t groups(uint32_t windows) const noexcept
uint32_t push_words() const noexcept
uint32_t parameters_binding() const noexcept
How many lanes share a window and how many sequences a kernel reads.