Skip to content

GPU: DaCe waits on the stream after every kernel when one nested SDFG mixes host and GPU work (qe_vexx_k: ~2,900 cudaStreamSynchronize per call) #124

Backlog

Description

@nicolasribeiro2028

What we found

DaCe synchronizes the GPU stream far more than needed in qe_vexx_k's GPU program. Results are correct; the cost is time. The host calls cudaStreamSynchronize after every GPU state, also between two kernels on the same in-order stream where nothing on the host reads GPU data:

__cpf_runkernel_extcall_for3_doall102_47_0_11(__state, Tmp43, exxbuff_w_0_0_0, result, k2, npol, nrxxs, __cpf_current_stream);
cpf_gpu_check(cudaStreamSynchronize(__state->gpu_context->streams[0]));   // codegen state footer (cuda.py)
cpf_gpu_check(cudaStreamSynchronize(gpu_streams[0]));                     // stream wiring sync tasklet

Per whole-program call (preset M, all 13 kernels on the GPU, Daint GH200, Nsight Systems, job 5511888):

Per call
cudaStreamSynchronize 2,898 calls, 21.1 ms of host time
Call time (under Nsight) 37.9 ms
GPU busy 13.0 ms (kernels 1-8 µs each, FFTs 2.9 µs)
Kernel launches 3,862 (~12 ms of host time)

The band-pair loop launches ~15 tiny kernels per pair for 256 pairs. With a wait after each, the host cannot queue the next launch while the GPU runs the last one, so the GPU idles about two thirds of the call.

Why it happens. AutoGPUStreamScheduler (dace/transformation/passes/gpu_specialization/gpu_stream_scheduling.py) would place waits only where the host reads GPU results. But it falls back to PerComponentGPUStreamScheduler for the whole SDFG as soon as one node anywhere mixes host and GPU work:

AutoGPUStreamScheduler: 1 top-level node(s) mix host and GPU work
  (first: NestedSDFG 'nested_state43_for_inline' in state 'state182' (SDFG 'extcall_for3'));
  falling back to PerComponentGPUStreamScheduler.
  • classify_node(NestedSDFG) folds the whole inner SDFG; mixed_nodes() collects MIXED nodes at any depth; one hit -> assign_mixed_streams() -> per-component rule (classify_sync_points): a kernel exit writing GPU memory -> wait at the end of its state. Every state.
  • The offending node sits outside the pair loop, at the top level of extcall_for3. It is a guarded specialization (if51): a pinned host fallback loop (for24, a gather into Tmp24 over ngm) and its GPU parallel arm (doall189). One such node -> waits in all 256 pairs.
  • It stays nested because MapToForLoop (map_for_loop.py, inline_after) cannot inline it: its output goes through a reshape view, Tmp24[0:nrxxs] <-> Tmp23[0:n3, 0:n2, 0:n1], and InlineMultistateSDFG.can_be_applied refuses any view at the boundary (isolate_nested_sdfg's test also fails). Five nested SDFGs in this kernel are refused this way.
  • The waits come in pairs because cuda.py's state footer adds its own wait (lines ~1547-1566) on top of the sync tasklet the stream wiring inserts.
  • Not this kernel only: two other units of the same program fall back too, on state 'stage_copyin' (a state SplitStateByGPUClass could not split).

The fallback warning is easy to miss: runs with PYTHONWARNINGS=ignore hide it.

What needs to be found

A way to keep the default rule, wait only before a host read of GPU data, for a program that contains a nested SDFG mixing host and GPU work, instead of switching the whole SDFG to a wait after every state. The required waits stay required: a missing one means the host reads unfinished GPU data and the results are silently wrong, so whatever the fix, it has to be shown correct before it is fast.

Candidate directions (none implemented):

  1. Treat such a nested SDFG as a host level of its own. AutoGPUStreamScheduler.splice_host_reads already places waits inside a nested SDFG that holds gpu_streams (the ICON host-map body case); the classification falls back before that path runs. A nested SDFG at host level (not inside a map) whose inside is host control flow plus GPU maps would be a host level, not MIXED, and the stream wiring would give it gpu_streams.
  2. Fall back per state, not per SDFG. Only the MIXED states get the per-state waits; the Auto rule holds everywhere else.
  3. Inline the node. Let InlineMultistateSDFG handle a boundary reshape view (recreate the view in each inlined state that touches it). Removes the MIXED node itself, but touches a core transformation every inliner uses.
  4. Drop the duplicate wait. Skip cuda.py's state-footer wait when the stream wiring already placed its syncs (is_stream_wiring_applied). Small and low-risk, but a small gain: the second wait runs on an idle stream.

Questions: which direction fits the design of gpu_stream_scheduling.py, and who should review a change there? How to test it: qe_vexx_k on all 11 configurations x 3 shapes on the GPU, DaCe's -m gpu tests with CuPy installed, and a regression test (a mixed host-level nested SDFG keeps the Auto rule and gets waits only before host reads)?

What the fix can gain

This kernel (estimate, not measured). With waits only at real host reads (per-band scalars such as any_match / min_expr / max_expr, copy-backs, the call's end), ~2,900 waits -> on the order of 100 per call. The call is then bound by the ~3,900 launches (~12 ms of host time) and the GPU work (~13 ms), which overlap: ~13-15 ms per call, about 2x over the current 29 ms and ~3.5-4x over the bench's numba baseline (53 ms).

Measured so far on the same kernel (job 5511888, 72 host threads, one GPU): copy placement fix alone (local b10eec3a0c on 774047f913, not pushed) took v10 from 41.7 to 29.1-29.8 ms, from 1.27x to 1.80x over numba.

In general. Any GPU program that DaCe lowers with a host loop launching many small kernels, and that contains one mixed nested SDFG anywhere, pays a wait per state per iteration. The gain grows with the number of short kernels per call: when kernels take a few µs, a host wait plus a launch (~5-10 µs) per kernel is most of the time, and removing the waits lets launches overlap GPU work. How many programs are affected is not known yet; a cheap first count needs no GPU: render each GPU-target kernel of the bench with PYTHONWARNINGS=always::UserWarning and count the falling back to PerComponentGPUStreamScheduler warnings.

Versions and repro

Activity

  1. changed the title [-]GPU: one mixed nested SDFG makes the stream scheduler sync after every kernel (qe_vexx_k: ~2,900 cudaStreamSynchronize per call)[/-] [+]GPU: DaCe waits on the stream after every kernel when one nested SDFG mixes host and GPU work (qe_vexx_k: ~2,900 cudaStreamSynchronize per call)[/+] on Dec 2, 2026
  2. nicolasribeiro2028 commented on Dec 2, 2026

    @nicolasribeiro2028
    CollaboratorAuthor

    Fallback count. 180 scientific_computing kernels (HPCAgent-Bench 1.0.5, Tactics e303f87, DaCe 774047f913), default GPU pipeline at preset S, units rendered only. 175 rendered (4 timeouts, kmeans crashed); 155 have GPU units.

    7 of 155 kernels (12 units) fall back to PerComponentGPUStreamScheduler:

    Cause Units Kernels
    state 'stage_copyin' in sort library units 9 qe_vexx_k, qe_regterg, sparse_cholesky, ilu0, qe_vloc_psi_k_acc
    NestedSDFG 'loop_body' (this issue) 2 quatrex_rgf, qe_vloc_psi_k_acc
    state 'block' in a BLAS library unit 1 laplacian_stencil_3d

    Lower bound: default placement keeps sequential loops on the CPU; the qe_vexx_k case above needs them on the GPU. Cost in these 7 kernels not measured.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions