advanced26 min studyLesson 528 in Engineering

Outlining kernels and mapping launch coordinates

Preserve captured arguments, execution scope, and asynchronous dependencies when separating host and device work.

On this page

What you’ll understand

  • Distinguish extracting a kernel body from choosing a parallel mapping.
  • Map launch coordinates to valid logical indices.
  • Preserve host/device ownership and launch dependency boundaries.

Kernel outlining moves a device execution region into a callable kernel and makes its inputs and launch configuration explicit. It does not by itself prove that every loop can run in parallel or that host and device share the same memory. Launch mapping chooses which workgroups, threads, or sequential iterations perform logical work. Keep those decisions separate so a syntactically valid kernel does not hide an illegal schedule or an unfinished asynchronous dependency.

Captured state becomes an explicit interface

An outlined kernel needs arguments or another supported mechanism for values used from outside its body. Those values include buffers, shapes, strides, scalars, and sometimes execution dependencies. Constants can be embedded when their semantics permit, while mutable or address-space-sensitive data needs a valid representation. MLIR's GPU dialect distinguishes launch regions and outlined kernel functions, but exact syntax and passes evolve. Record the compiler revision and pipeline instead of assuming a current textual example applies unchanged to every release.

The host/device boundary imposes ABI, address-space, allocation, and availability rules. A host pointer is not automatically a device-accessible allocation, and copying only its numeric value does not establish accessibility. Dynamic shapes and strides must match the buffer actually passed. A callable kernel signature also needs target-compatible argument lowering. Test interface mismatches and layout variants rather than validating only a single statically shaped contiguous array.

i=bxBx+txi=b_x B_x+t_x
For a one-dimensional mapping, bx is a nonnegative block index, Bx a positive threads-per-block count, and tx a thread index in[0, Bx). i is a logical element index; it still requires i<N before unpadded memory access.↔ Scroll wide equations horizontally.

Parallel execution has defined synchronization scopes

Independent logical elements can map to different threads, but a recurrence or global dependence cannot be repaired by simply choosing more blocks. Ordinary workgroup barriers synchronize only their documented scope, not arbitrary blocks across an entire launch. If one phase requires all blocks to finish before another reads global results, a separate ordered kernel launch can provide a boundary, or a supported cooperative mechanism may apply under additional constraints. Removing that boundary requires an actual synchronization proof.

A launch may return before the device work finishes. Later host reads, buffer reuse, deallocation, or another queue's kernel must respect completion and dependency semantics. Submission order within one supported stream is not a blanket ordering guarantee across every independent stream or host thread. Propagate events or dependency tokens where needed. Kernel outlining should preserve the execution graph, not merely extract instructions and hope runtime ordering matches the previous region's meaning.

G=⌈NBx⌉,0≤i<NG=\left\lceil\frac{N}{B_x}\right\rceil,\qquad0\leq i<N
For N≥0 independent elements and positive Bx, G counts one-dimensional blocks. The separate validity condition protects tail threads. Zero-sized launches and supported grid limits require runtime/target-specific handling.↔ Scroll wide equations horizontally.

Measure launch savings without deleting guarantees

Small kernels can spend substantial time in launch and scheduling overhead, motivating fusion or batching. Large kernels may instead be constrained by resource use, data movement, or the number of independent blocks. Compare complete dependent workloads, including required transfers and synchronization, rather than timing an empty kernel and extrapolating blindly. A faster launch path is not acceptable if host code reads incomplete output or buffers are freed before the asynchronous work retires.

The queue lab illustrates aggregate submission pressure only. Reset and set both λ=4, μ=2, predict growing backlog from four and Step. Reset and re-set λ=2, μ=3 for a declining comparison. Its fluid jobs are not GPU blocks or commands, and it has no stream ordering, captures, events, or device memory. Use it to explain why faster submission can merely accumulate more work if execution capacity is unchanged, then identify the actual completion and dependency measurements a launch experiment needs.

One thousand elements, four blocks

An independent length 1000 elementwise operation maps one element per thread with 256 threads per block. Storage is unpadded, and the kernel guards memory operations with i<1000.

  1. The grid uses ceil(1000/256)=4 blocks, giving 1024 total thread positions. Logical index is block_id×256+thread_id.

  2. The final block covers indices 768 through 1023. Its first 232 threads are valid, and the remaining 24 must not access unpadded elements.

  3. Outlining must pass the actual buffer and length, preserve the guard, and keep storage alive until device completion. Four blocks do not establish a global barrier inside the kernel or permission to parallelize a dependent recurrence.

The result

Launch geometry provides a coverage mapping only after bounds, independence, interfaces, and asynchronous lifetime are preserved.

Explore the model

Interactive lab

Interactive lab · queue

Submission rate is not execution order

Reset, set λ=4, μ=2, predict growth, then Step. Reset and re-set λ=2, μ=3, predict decline. This fluid model contains no kernels, threads, streams, captured arguments, or dependency events.

Submission rate is not execution orderFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA launch interface must preserve completion and ordering, not only make submission faster.

Check your understanding

What does kernel outlining alone establish?

Try it yourself

Map 513 independent elements with 128 threads per block. Count blocks and inactive tail threads. Then name a condition needed before the host can safely consume the output.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Five blocks provide 640 thread positions, not 513 allocated elements.

Compare with the solution

There are ceil(513/128)=5 blocks and 640 positions, leaving 127 inactive tail threads. Their memory operations require bounds protection. The host must observe the runtime's relevant completion/transfer and visibility conditions before reading output, with buffers remaining alive throughout. Successful launch submission alone does not establish that the device finished or the host-visible result is ready.

Take it with you

  • Outlining makes captured interfaces explicit without proving parallel legality.
  • Launch coordinates need bounds and target-compatible memory arguments.
  • Host/device lifetime and synchronization remain part of lowering correctness.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.