Two inputs, fused reductions, and control flow in the traced DSL - #1
Open
yujiteshima wants to merge 1 commit into
Open
yujiteshima wants to merge 1 commit into
yujiteshima wants to merge 1 commit into
Conversation
- map2 / GPU.kernel { |x, y| }: a second input array (LAYOUT_3BUF), arity
taken from the block or given as arity: 2; size mismatch is ArgumentError
- sum { |x| ... }, dot(b), GPU.kernel(reduce: :sum | :min | :max): the
expression and a shared-memory tree reduction in one shader, partials
combined on the host in double (same arrangement as narray's #sum)
- where(cond, a, b), comparisons > < >= <= == != and & | ! on Expr; a
condition used as a number (or vice versa) is a TypeError before glslang
- iterate(init, count) { |acc, i| } emits a real GLSL for loop via a small
statement-emitting Codegen; loops nest; count must be an Integer at trace
time. Ruby loops with a known count are simply unrolled (documented)
- one-input shaders are byte-identical to before; 58/58 tests
Merge note: dispatch_kernel() in src/gpu_kernel.c now takes a layout and 2 or
3 buffers. The deferred-submit branch replaces that function with narray's
dispatch_pipeline(); when merging, keep the layout/3-buffer call sites here
and drop the local dispatch body in favour of the upstream entry point.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Three things the README listed under Not yet:
a.map2(b) { |x, y| x * y + 1 }, orGPU.kernel { |x, y| ... }.call(a, b). The input count comes fromProc#arity(GPU.kernel(arity: 2)to say it explicitly); two inputs useLAYOUT_3BUF. A size mismatch raisesArgumentError.a.sum { |x| x * 2 + 1 }evaluates the expression and reduces it in shared memory in the same shader; the per-workgroup partials are combined on the host in double, like the base gem'ssum.GPU.kernel(reduce: :sum | :min | :max)is the general form,a.dot(b)is a one-dispatch dot product.sumwithout a block still goes to the C implementation.> < >= <= == !=) yield boolean nodes,&|!combine them,where(cond, a, b)(aliasselect) becomes a GLSL ternary, anditerate(init, count) { |acc, i| ... }becomes a real GLSLforloop —countis a trace-time Integer, loops nest. Ruby's ownif/whilestill cannot be traced (the condition is a proxy, so it is always truthy); that is documented, and it is the same trade-offcupy.wheremakes. Constant-count Ruby loops unroll at trace time.iterate(x, 20) { |y, i| (y + x / y) * 0.5 }generates:The one-input map shader is byte-identical to
main's.Tests and numbers
58/58 — the 20 existing tests plus 38 new ones (map2, size and arity errors,
sum { }, dot, min/max across workgroups,where/ comparisons /&|!, Newton sqrt ==sqrt(x), nestediterate, unrolled3.times, type errors for bool arithmetic and non-Integer counts).examples/bench_reduce.rb, Apple M5, 1,024 elements:a.map { }.sum(2 dispatches) 0.52 ms →a.sum { }(1 dispatch) 0.22 ms.Notes
src/gpu_kernel.c's dispatch now takes 2 or 3 buffers and does the reduction combine. The companiondeferred-submitPR replaces the dispatch body with mruby-gpu-narray'sdispatch_pipeline(); merge this one first.examples/kernel_dsl.rbis unchanged (it is a talk script); the new material is inexamples/kernel_dsl_2.rb.🤖 Generated with Claude Code