MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SegmentedReductionContext.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include "SegmentGeometry.hpp"
7
8/**
9 * @file SegmentedReductionContext.hpp
10 * @brief Reduces segments of doubles on the GPU, exactly, with no fp64 on the device.
11 */
12
13namespace MayaFlux::Yantra {
14
15/**
16 * @class SegmentedReductionContext
17 * @brief One reduction per segment, as one dispatch of the SegmentedReduction template.
18 *
19 * Values are staged as word pairs and combined by the template's emulated
20 * IEEE double arithmetic, so a result agrees with a CPU reduction up to the
21 * order the partial results are combined in. The map is applied to each value
22 * before the combine. Max and Min follow the library order key, where every
23 * NaN is the largest. An empty segment yields the combine's identity: zero for
24 * Sum, negative infinity for Max, NaN for Min.
25 */
27public:
28 /**
29 * @param combine Sum, Max or Min.
30 * @param map Pointwise map applied before the combine.
31 * @param workgroup Invocations per segment. A power of two, at least 2.
32 */
40
41 /**
42 * @brief Reduce every segment.
43 * @param values Doubles the segments index into.
44 * @param geometry Segment length, stride, first offset and count. Every segment must lie inside values.
45 * @return One double per segment, empty when there are no segments.
46 */
47 [[nodiscard]] std::vector<double> reduce(std::span<const double> values, const SegmentGeometry& geometry)
48 {
49 if (geometry.count == 0) {
50 return {};
51 }
52 stage(values, geometry);
53 execute(Datum<std::vector<Kakshya::DataVariant>> {}, ExecutionContext {});
54
55 const auto result = primary_values();
56 return { result.begin(), result.end() };
57 }
58
59 /**
60 * @brief Stage values and geometry for the next dispatch without running it.
61 *
62 * Follow with dispatch_async and collect_result, then decode_primary, to
63 * run the reduction without blocking. reduce is stage followed by a
64 * synchronous execute.
65 */
66 void stage(std::span<const double> values, const SegmentGeometry& geometry)
67 {
68 stage_words(0, values);
69 stage(geometry);
70 }
71
72 /**
73 * @brief Stage the geometry for values that are already on the device.
74 *
75 * The values are whatever binding 0 resolves to: a shared buffer of this
76 * context, or one imported from another with import_shared. Nothing is
77 * staged from the host.
78 */
79 void stage(const SegmentGeometry& geometry)
80 {
81 m_segments = geometry.count;
82 set_output_size(1, static_cast<size_t>(geometry.count) * sizeof(uint64_t));
83 push(Push { .length = geometry.length, .stride = geometry.stride, .offset = geometry.offset });
84 }
85
86 /**
87 * @brief The spec of the template instance this context drives.
88 */
90 Portal::Graphics::KernelOp combine, Portal::Graphics::ReductionMap map, uint32_t workgroup)
91 {
95 .op(combine)
96 .map(map)
97 .ssbo("values", BindingDirection::Input, Kakshya::GpuDataFormat::VEC2_U32)
98 .ssbo("result", BindingDirection::Output, Kakshya::GpuDataFormat::VEC2_U32)
102 .workgroup(workgroup)
103 .build();
104 }
105
106protected:
107 /**
108 * @brief One workgroup per segment.
109 */
110 [[nodiscard]] std::array<uint32_t, 3> calculate_dispatch_size(
111 size_t /*total_elements*/, const DataStructureInfo& /*structure_info*/) const override
112 {
113 return { m_segments, 1U, 1U };
114 }
115
116private:
117 struct Push {
118 uint32_t length;
119 uint32_t stride;
120 uint32_t offset;
121 };
122
124 : WordPairExecutionContext(config_from_spec(spec), bindings_from_spec(spec), false, "SegmentedReductionContext")
125 {
126 }
127
128 uint32_t m_segments { 1 };
129};
130
131} // namespace MayaFlux::Yantra
Runs a shader on doubles carried as two 32-bit words, with no fp64 on the device.
Assemble & op(KernelOp o)
Set the named operation the emitter will lower to SPIR-V.
ShaderSpec build()
Finalise and return the ShaderSpec.
Assemble & pc(std::string name, Kakshya::GpuDataFormat format)
Declare a push constant field with explicit format.
Assemble & ssbo(std::string name, BindingDirection direction, Kakshya::GpuDataFormat format, Kakshya::DataModality modality=Kakshya::DataModality::SCALAR_F32)
Declare an SSBO binding.
Assemble & workgroup(uint32_t x, uint32_t y=1, uint32_t z=1)
Override workgroup size.
Assemble & tmpl(KernelTemplate t)
Set the kernel template.
Assemble & map(ReductionMap m)
Set the pointwise map of a SegmentedReduction.
Fluent assembler producing a ShaderSpec.
void set_output_size(size_t index, size_t byte_size)
Declare the byte capacity of an output binding independently of input data.
std::array< uint32_t, 3 > calculate_dispatch_size(size_t, const DataStructureInfo &) const override
One workgroup per segment.
std::vector< double > reduce(std::span< const double > values, const SegmentGeometry &geometry)
Reduce every segment.
SegmentedReductionContext(Portal::Graphics::KernelOp combine, Portal::Graphics::ReductionMap map=Portal::Graphics::ReductionMap::Identity, uint32_t workgroup=256)
void stage(std::span< const double > values, const SegmentGeometry &geometry)
Stage values and geometry for the next dispatch without running it.
void stage(const SegmentGeometry &geometry)
Stage the geometry for values that are already on the device.
static Portal::Graphics::ShaderSpec reduction_spec(Portal::Graphics::KernelOp combine, Portal::Graphics::ReductionMap map, uint32_t workgroup)
The spec of the template instance this context drives.
SegmentedReductionContext(const Portal::Graphics::ShaderSpec &spec)
ShaderExecutionContext & push(const T &data)
Set push constants from a trivially copyable struct or value.
output_type execute(const input_type &input, const ExecutionContext &ctx) override
Injects multipass configuration into the context before dispatch when set_multipass() has been called...
WordPairExecutionContext(GpuComputeConfig config, std::vector< GpuBufferBinding > bindings, bool order_key, std::string name="WordPairExecutionContext")
std::span< const double > primary_values() const noexcept
The primary output of the last dispatch decoded to doubles.
void stage_words(uint32_t binding, std::span< const double > values, size_t padded_count=0, bool complement=false)
Encode values and stage them as the data of a binding.
@ VEC2_U32
glm::uvec2: a 64-bit value as low word (x) and high word (y), not a sampled image format
@ SegmentedReduction
One workgroup per segment over word-pair doubles.
BindingDirection
Data flow direction for a shader binding slot.
ReductionMap
Pointwise map applied to each element before a SegmentedReduction combines it.
KernelOp
Named operation the SPIR-V emitter knows how to lower to opcodes.
GpuComputeConfig config_from_spec(const Portal::Graphics::ShaderSpec &spec)
Derive a GpuComputeConfig from a ShaderSpec.
std::vector< GpuBufferBinding > bindings_from_spec(const Portal::Graphics::ShaderSpec &spec)
Derive a GpuBufferBinding list from a ShaderSpec.
Complete declarative description of a generated compute shader.
Dimensions, modality, source type, and optional Composite field schema.
Input/Output container for computation pipeline data flow with structure preservation.
Definition DataIO.hpp:63
Context information controlling how a compute operation executes.
Where the windows of a GPU pass sit in the value array.