Skip to content

[BUG]: cuda-core launch cost grows linearly with the leases an open ExecutionContext holds on a buffer #1786

Open

Description

@physwkim

Description

An open cuda-async ExecutionContext keeps every AllocationLease in its submission until the submission completes, and each lease leaves an Access with completion: None in the buffer's Accesses::active. Accesses::register (cuda-core/src/allocation.rs:52-58) walks active on every acquire, and AllocationLease::drop (allocation.rs:346) finds its entry with position, so N launches that touch one buffer through one context cost O(N²) on the host, once at submit and again at release. Stream::synchronize does not shrink active, since completion is set only when the submission is dropped.

Error / actual behavior

Host time per launch, mean over the 1000 launches ending at the count shown (µs), all modes run back to back:

launches one sync core fresh
1000 2.46 2.44 2.06 8.91
4000 4.72 6.05 4.63 7.85
16000 15.03 20.56 14.90 8.83
64000 64.41 88.85 94.87 12.53
256000 488.42 319.67 486.88 14.70

one uses a single ExecutionContext; sync is the same with Stream::synchronize every 100 launches; core skips cuda-async and holds RawDeviceBuffer::acquire leases in a Vec; fresh (control) creates a new ExecutionContext per launch. Dropping the held leases in core took 22.8 ms for 16000, 358 ms for 64000 and 10.9 s for 256000.

Expected behavior

Host cost per acquire and per lease release independent of how many leases are already held, for example by having a submission reuse its existing lease on the same buffer and stream, or by keeping per-stream reader/writer counts in Accesses instead of one entry per lease.

Minimal reproduction example

Cargo.toml:

[package]
name = "repro"
version = "0.0.0"
edition = "2024"
publish = false

[dependencies]
cuda-core = { git = "https://github.com/NVIDIA/cuda-rust", rev = "e1f6df0adecd2c9410a364c21384847a04d46854" }
cuda-async = { git = "https://github.com/NVIDIA/cuda-rust", rev = "e1f6df0adecd2c9410a364c21384847a04d46854" }

[workspace]

src/main.rs:

//! Usage: repro [one|sync|fresh|core] [launches]
use cuda_async::device_operation::ExecutionContext;
use cuda_core::{CudaContext, DeviceBuffer, launch_kernel_on_stream};
use std::time::Instant;

const PTX: &str = ".version 8.0\n.target sm_52\n.address_size 64\n.visible .entry noop()\n{\n\tret;\n}\n";
const WINDOW: usize = 1000;

fn main() {
    let mut args = std::env::args().skip(1);
    let mode = args.next().unwrap_or_else(|| "one".into());
    let launches: usize = args.next().map_or(100_000, |a| a.parse().expect("launches"));
    assert!(
        matches!(mode.as_str(), "one" | "sync" | "fresh" | "core"),
        "mode is one, sync, fresh or core"
    );

    let ctx = CudaContext::new(0).expect("context");
    let stream = ctx.new_stream().expect("stream");
    let buf: DeviceBuffer<f32> = DeviceBuffer::zeroed(&stream, 1 << 16).expect("buffer");
    let module = ctx.load_module_from_ptx_src(PTX).expect("module");
    let noop = module.load_function("noop").expect("function");

    let mut exec = ExecutionContext::new(stream.clone());
    let mut held = Vec::new();
    let mut start = Instant::now();
    let mut checkpoint = WINDOW;
    for i in 1..=launches {
        if mode == "fresh" {
            exec = ExecutionContext::new(stream.clone());
        }
        if mode == "core" {
            held.push(buf.storage().acquire(&stream, true, false).expect("acquire"));
        } else {
            exec.retain_buffer(buf.storage().clone(), true).expect("retain");
        }
        // SAFETY: `noop` takes no parameters and touches no memory.
        unsafe { launch_kernel_on_stream(&noop, (1, 1, 1), (1, 1, 1), 0, &stream, &mut []) }
            .expect("launch");
        if mode == "sync" && i % 100 == 0 {
            stream.synchronize().expect("sync");
        }
        if i + WINDOW == checkpoint {
            start = Instant::now();
        }
        if i == checkpoint {
            let us = start.elapsed().as_secs_f64() * 1e6 / WINDOW as f64;
            println!("{mode} {i:>7}: {us:7.2} us per launch over launches {}..={i}", i - WINDOW + 1);
            checkpoint *= 4;
        }
    }
    stream.synchronize().expect("sync");
    let n = held.len();
    let t = Instant::now();
    drop(held);
    if n > 0 {
        println!("{mode}: dropping {n} leases took {:.1} ms", t.elapsed().as_secs_f64() * 1e3);
    }
}
cargo build --release
for m in one sync fresh core; do target/release/repro $m 256000; done

Operating System

Ubuntu 24.04.5 LTS (kernel 6.14)

GPU and GPU Architecture

NVIDIA RTX 5000 Ada Generation (sm_89)

CUDA driver version

620.70.06 (CUDA 13.5)

rustc version

rustc 1.99.1 (58cf8f9c3 2026-10-13)

Activity

  1. SycamoreQ commented on Nov 12, 2026

    @SycamoreQ

    Hello there!

    I gave a shot at implemented the lease-tracking fix and opened PR #1787: #1787. The change replaces linear scans with indexed lease lookup/removal and per-stream access counters while preserving cross-stream conflict checks. Formatting and repro-runner syntax checks pass; the focused Rust unit test could not build because I currently have no CUDA 13.0+ toolkit. The Modal runner and GPU workload were not executed. I will be doing it soon.

  2. added
    cutile-rs
    on Nov 12, 2026
  3. SycamoreQ commented on Nov 13, 2026

    @SycamoreQ
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

    cutile-rs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions