From 8138894c0ef9137b006858346ec859d24a5484cb Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 23 Nov 2026 04:56:39 +0300 Subject: [PATCH] gpu/adapter: fall back to numeric model token when index is out of range --- crates/xcl/src/gpu/adapter.rs | 60 ++++++++++++++++++++++++++--- crates/xcl/src/gpu/adapter_tests.rs | 34 +++++++++++++++- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/crates/xcl/src/gpu/adapter.rs b/crates/xcl/src/gpu/adapter.rs index 85a6ded..cd0b61f 100644 --- a/crates/xcl/src/gpu/adapter.rs +++ b/crates/xcl/src/gpu/adapter.rs @@ -3,7 +3,9 @@ //! - `auto` (or empty): the hardware adapters in high-performance order, the first one that //! creates a device; //! - `warp`: the software renderer; -//! - a number `N`: the N-th hardware adapter in high-performance order (0 = the first); +//! - a number `N`: the N-th hardware adapter in high-performance order (0 = the first); if `N` +//! is out of range, falls back to matching `N` as a standalone numeric token in the adapter +//! name (so e.g. `5070`, `3080`, or `7900` selects that GPU model); //! - anything else: the hardware adapters whose name contains it (case-insensitive), e.g. //! `radeon` or `rtx 3080`. //! @@ -34,8 +36,11 @@ impl Selector { Selector::Auto } else if s.eq_ignore_ascii_case("warp") { Selector::Warp - } else if let Ok(n) = s.parse::() { - Selector::Index(n) + } else if (s == "0" || !s.starts_with('0')) && s.bytes().all(|b| b.is_ascii_digit()) { + match s.parse::() { + Ok(n) => Selector::Index(n), + Err(_) => Selector::Name(s.to_lowercase()), + } } else { Selector::Name(s.to_lowercase()) } @@ -49,7 +54,8 @@ impl Selector { } } - /// Whether the hardware adapter at `index` (high-performance order) named `name` is chosen. + /// Whether the hardware adapter at `index` (high-performance order) named `name` matches + /// directly (without the out-of-range numeric model fallback applied by [`candidates`]). pub fn allows(&self, index: usize, name: &str) -> bool { match self { Selector::Auto => true, @@ -71,6 +77,44 @@ impl fmt::Display for Selector { } } +/// True if `name` contains `digits` not immediately preceded or followed by another ASCII digit +/// (e.g. `"5070"` matches `"RTX 5070"` and `"RX 7900XTX"` matches `"7900"`, while `"2"` does not +/// match `"RTX 2080"`). +fn contains_numeric_token(name: &str, digits: &str) -> bool { + if digits.is_empty() { + return false; + } + let bytes = name.as_bytes(); + let mut start = 0; + while let Some(rel) = name[start..].find(digits) { + let pos = start + rel; + let end = pos + digits.len(); + let before_ok = pos == 0 || !bytes[pos - 1].is_ascii_digit(); + let after_ok = end == bytes.len() || !bytes[end].is_ascii_digit(); + if before_ok && after_ok { + return true; + } + start = end; + } + false +} + +/// Indices into `names` allowed by `sel`. For `Selector::Index(n)` with `n >= names.len()`, +/// falls back to matching `n` as a standalone numeric token in each adapter's name. +pub(crate) fn select_indices>(names: &[S], sel: &Selector) -> Vec { + let direct: Vec = names.iter().enumerate().filter(|(i, n)| sel.allows(*i, n.as_ref())).map(|(i, _)| i).collect(); + if !direct.is_empty() { + return direct; + } + if let Selector::Index(n) = sel { + if *n >= names.len() { + let token = n.to_string(); + return names.iter().enumerate().filter(|(_, name)| contains_numeric_token(name.as_ref(), &token)).map(|(i, _)| i).collect(); + } + } + Vec::new() +} + /// The adapter's name from its description. pub fn name(desc: &DXGI_ADAPTER_DESC1) -> String { let n = desc.Description.iter().position(|&c| c == 0).unwrap_or(desc.Description.len()); @@ -96,7 +140,13 @@ pub fn hardware(factory: &IDXGIFactory6) -> Vec<(IDXGIAdapter1, DXGI_ADAPTER_DES /// The hardware adapters `sel` allows, in high-performance order (empty for `warp`). pub fn candidates(factory: &IDXGIFactory6, sel: &Selector) -> Vec<(IDXGIAdapter1, DXGI_ADAPTER_DESC1)> { - hardware(factory).into_iter().enumerate().filter(|(i, (_, d))| sel.allows(*i, &name(d))).map(|(_, a)| a).collect() + let hw = hardware(factory); + let names: Vec = hw.iter().map(|(_, d)| name(d)).collect(); + let mut keep = vec![false; hw.len()]; + for i in select_indices(&names, sel) { + keep[i] = true; + } + hw.into_iter().enumerate().filter(|(i, _)| keep[*i]).map(|(_, a)| a).collect() } /// "`sel` matches no adapter (have: 0 = …, 1 = …)" – for logs and test skips. diff --git a/crates/xcl/src/gpu/adapter_tests.rs b/crates/xcl/src/gpu/adapter_tests.rs index f2fd0e8..70fc209 100644 --- a/crates/xcl/src/gpu/adapter_tests.rs +++ b/crates/xcl/src/gpu/adapter_tests.rs @@ -1,11 +1,14 @@ -use super::Selector; +use super::{select_indices, Selector}; #[test] fn selector_parsing() { assert_eq!(Selector::parse(""), Selector::Auto); assert_eq!(Selector::parse(" Auto "), Selector::Auto); assert_eq!(Selector::parse("WARP"), Selector::Warp); + assert_eq!(Selector::parse("0"), Selector::Index(0)); assert_eq!(Selector::parse("1"), Selector::Index(1)); + assert_eq!(Selector::parse("007"), Selector::Name("007".into())); + assert_eq!(Selector::parse("+1"), Selector::Name("+1".into())); assert_eq!(Selector::parse("Radeon"), Selector::Name("radeon".into())); assert_eq!(Selector::parse(" RTX 3080 "), Selector::Name("rtx 3080".into())); } @@ -21,3 +24,32 @@ fn selector_matching() { assert!(radeon.allows(1, amd) && !radeon.allows(0, nv)); assert!(Selector::parse("geforce rtx").allows(0, nv)); } + +#[test] +fn numeric_selector_falls_back_to_model_token_when_out_of_range() { + let gpus = [ + "NVIDIA GeForce RTX 5070", + "AMD Radeon™ RX 7900XTX", + "Intel® Arc™ A770 Graphics", + ]; + // In-range numbers still pick by index. + assert_eq!(select_indices(&gpus, &Selector::parse("0")), vec![0]); + assert_eq!(select_indices(&gpus, &Selector::parse("1")), vec![1]); + assert_eq!(select_indices(&gpus, &Selector::parse("2")), vec![2]); + + // Out-of-range numbers match standalone numeric model tokens (bounded by non-digits). + assert_eq!(select_indices(&gpus, &Selector::parse("5070")), vec![0]); + assert_eq!(select_indices(&gpus, &Selector::parse("7900")), vec![1]); + assert_eq!(select_indices(&gpus, &Selector::parse("770")), vec![2]); + + // Empty adapter list produces no match. + let empty: [&str; 0] = []; + assert!(select_indices(&empty, &Selector::parse("0")).is_empty()); + + // Partial digit substrings inside a larger model number must NOT match. + let two = ["NVIDIA GeForce RTX 2080", "AMD Radeon(TM) Graphics"]; + assert!(select_indices(&two, &Selector::parse("2")).is_empty()); + assert!(select_indices(&two, &Selector::parse("208")).is_empty()); + assert!(select_indices(&two, &Selector::parse("80")).is_empty()); + assert_eq!(select_indices(&two, &Selector::parse("2080")), vec![0]); +}