diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 2dd0f67..01b001c 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -24,6 +24,7 @@ mod ffi { unsafe extern "C" { pub fn vetta_hip_query_device(dev_id: c_int, out: *mut DeviceInfoRaw) -> c_int; + pub fn vetta_hip_init_blocking_sync() -> c_int; pub fn vetta_buf_alloc(out: *mut *mut c_void, bytes: usize) -> c_int; pub fn vetta_buf_free(p: *mut c_void) -> c_int; pub fn vetta_buf_h2d(dst: *mut c_void, src: *const c_void, bytes: usize) -> c_int; @@ -173,6 +174,17 @@ fn pool_enabled() -> bool { #[cfg(feature = "hip")] impl DeviceBuf { pub fn alloc(bytes: usize) -> Result { + // Set the blocking host-wait schedule once, before the first hipMalloc + // creates the device context, so GPU waits sleep instead of spinning at + // 100% CPU (exact strata --hip-sync blocking). + static SYNC_INIT: std::sync::Once = std::sync::Once::new(); + SYNC_INIT.call_once(|| unsafe { + // Escape hatch VETTA_NO_BLOCKING_SYNC=1 keeps the ROCm default spin + // schedule (for A/B CPU measurement). + if std::env::var("VETTA_NO_BLOCKING_SYNC").is_err() { + ffi::vetta_hip_init_blocking_sync(); + } + }); if pool_enabled() { if let Some(ptr) = BUF_POOL.with(|p| p.borrow_mut().get_mut(&bytes).and_then(|v| v.pop())) { return Ok(Self { ptr, bytes }); diff --git a/kernels/hip/vetta_kernels.h b/kernels/hip/vetta_kernels.h index b9741ca..1ebcfe7 100644 --- a/kernels/hip/vetta_kernels.h +++ b/kernels/hip/vetta_kernels.h @@ -27,6 +27,9 @@ int vetta_hip_register_uma_pages(void* host_aligned_ptr, size_t bytes, void** ou int vetta_hip_unregister_uma_pages(void* host_aligned_ptr); // Minimal device buffer lifecycle (executor foundation). +// Set blocking host-wait schedule (no 100% CPU spin during GPU waits); call once +// before the first allocation creates the device context. +int vetta_hip_init_blocking_sync(void); int vetta_buf_alloc(void** out, size_t bytes); int vetta_buf_free(void* p); int vetta_buf_h2d(void* dst, const void* src, size_t bytes); diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index f5f2064..7db14c0 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -43,6 +43,16 @@ extern "C" int vetta_hip_query_device(int dev_id, vetta_device_info_t* out) { // ---- minimal device buffer lifecycle (UMA-aware) --------------------------- +// Host-wait schedule: BLOCKING so host synchronization (blocking hipMemcpy / +// hipStreamSynchronize) sleeps instead of spinning at 100% CPU while the GPU +// runs. Exact strata --hip-sync blocking (hipDeviceScheduleBlockingSync). Must +// run before the device context is created; tolerate hipErrorSetOnActiveProcess. +extern "C" int vetta_hip_init_blocking_sync(void) { + hipError_t e = hipSetDeviceFlags(hipDeviceScheduleBlockingSync); + if (e != hipSuccess && e != hipErrorSetOnActiveProcess) { (void)hipGetLastError(); return 1; } + return 0; +} + extern "C" int vetta_buf_alloc(void** out, size_t bytes) { if (!out) return 1; HIP_CHECK(hipMalloc(out, bytes));