Skip to content

Dispatch through mruby-gpu-narray's dispatch_pipeline() - #2

Open
yujiteshima wants to merge 2 commits into
mainfrom
deferred-submit
Open

yujiteshima wants to merge 2 commits into
mainfrom
deferred-submit

Conversation

@yujiteshima

Copy link
Copy Markdown
Owner

What

Drops the private copy of the dispatch routine. A traced kernel — one or two inputs, element-wise or reduction — is now recorded into the base gem's deferred batch like any built-in operator: a.map { ... } queues one dispatch, and nothing is submitted until a result is read (GPU.pending counts it, GPU.sync flushes it). Reductions keep their host-side combine; mapping the partials is what flushes the batch.

examples/kernel_dsl.rb now times the operator chain three ways — a wait per operator (GPU.sync_mode = :eager), one batch, one traced kernel — because under deferred submission the old loop measured recording rather than execution:

  a * 2 + 1 - 3 + 4      1.087 ms   (4 dispatches, 4 waits -- GPU.sync_mode = :eager)
  same, one batch        0.22 ms    (4 dispatches, 1 wait)
  one traced kernel      0.228 ms   (1 dispatch,  1 wait)

Depends on

Tests

58/58 against mruby-gpu-narray deferred-submit; the base gem's 59 tests also pass with this gem in the build. examples/kernel_dsl_2.rb (map2, sum { }, where, iterate) runs inside a batch.

🤖 Generated with Claude Code

yujiteshima and others added 2 commits September 12, 2026 22:18
- 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>
Drops the private copy of the dispatch routine. A traced kernel -- one or
two inputs, element-wise or reduction -- is now recorded into the base gem's
deferred batch like any built-in operator, so a.map { ... } queues one
dispatch and nothing is submitted until a result is read. For reductions,
mapping the partials is what flushes the batch.

examples/kernel_dsl.rb times the operator chain three ways (a wait per
operator, one batch, one traced kernel), because under deferred submission
the old loop measured recording rather than execution.

Needs a mruby-gpu-narray that exports dispatch_pipeline() (its
deferred-submit branch, PR #4). Stacked on dsl-control-flow (PR #1).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant