flynnel 0.6.0

K-aware NUMA-aware work-stealing scheduler with extended-Flynn-taxonomy dispatch (MIMD / SIMC / MIMC / MISD / SIMT / MIMT): a rayon-shaped pool reshaped around per-call execution-class plans with first-class CPU+GPU hybrid (join_hybrid, hybrid_pipeline) and per-call dispatch profiles.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Which GPU watchdog, if any, resets a device whose work runs too long,
//! read from the host rather than assumed.
//!
//! Windows' timeout detection and recovery (TDR) covers devices under the
//! WDDM and MCDM driver models, does not cover TCC devices, and is off
//! entirely at TDR level 0. Its level and delay are registry values with
//! documented defaults. No other platform has an equivalent read here.
//!
//! Reading is separate from deciding: [`detect`] reads the driver model
//! and the TDR settings, and [`decide`] turns what was read into a
//! [`WatchdogState`], so every rule is testable without a device.

use std::ffi::{CStr, CString, c_char, c_void};
use std::fmt;

/// Windows' TDR level when the registry carries no `TdrLevel` value:
/// recover the adapter.
pub const TDR_DEFAULT_LEVEL: u32 = 3;

/// Windows' TDR delay in seconds when the registry carries no `TdrDelay`
/// value.
pub const TDR_DEFAULT_DELAY_S: u32 = 2;

/// A device's driver model as NVML reports it.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DriverModel {
    /// Windows Display Driver Model, which TDR covers.
    Wddm,
    /// Tesla Compute Cluster, NVML's `WDM`, which TDR does not cover.
    Tcc,
    /// Microsoft Compute Driver Model, which TDR covers.
    Mcdm,
}

/// The watchdog that applies to one device, and what was read to decide.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WatchdogState {
    /// How long one piece of device work may run before the watchdog
    /// resets the device, or `None` when no watchdog applies.
    pub delay_ns: Option<u64>,
    /// What was read and what each read returned, including every read
    /// that failed.
    pub basis: String,
}

impl WatchdogState {
    /// Whether a watchdog applies to the device.
    pub fn applies(&self) -> bool {
        self.delay_ns.is_some()
    }
}

impl fmt::Display for WatchdogState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.basis)
    }
}

/// The TDR level and delay in seconds.
pub type TdrSettings = (u32, u32);

/// The watchdog state for what was read.
///
/// `tdr` is `None` on a platform without TDR. A TDR read that failed
/// takes the documented default delay, and the basis says so: a
/// watchdog that is probably present and treated as absent ends in a
/// device reset, while one treated as present only shortens slices.
/// An unreadable driver model is treated as covered, for the same
/// reason, since only a TCC reading rules TDR out.
pub fn decide(
    model: &Result<DriverModel, String>,
    tdr: Option<&Result<TdrSettings, String>>,
) -> WatchdogState {
    let (model_text, outside_tdr) = match model {
        Ok(DriverModel::Tcc) => (format!("driver model {:?}", DriverModel::Tcc), true),
        Ok(covered) => (format!("driver model {covered:?}"), false),
        Err(err) => (format!("driver model unreadable ({err})"), false),
    };
    let Some(tdr) = tdr else {
        return WatchdogState {
            delay_ns: None,
            basis: format!("{model_text}; no watchdog is known on this platform"),
        };
    };
    if outside_tdr {
        return WatchdogState {
            delay_ns: None,
            basis: format!("{model_text}; TCC devices are outside TDR"),
        };
    }
    match tdr {
        Ok((0, delay_s)) => WatchdogState {
            delay_ns: None,
            basis: format!("{model_text}; TdrLevel 0 disables detection (TdrDelay {delay_s} s)"),
        },
        Ok((level, delay_s)) => WatchdogState {
            delay_ns: Some(u64::from(*delay_s) * 1_000_000_000),
            basis: format!("{model_text}; TdrLevel {level}, TdrDelay {delay_s} s"),
        },
        Err(err) => WatchdogState {
            delay_ns: Some(u64::from(TDR_DEFAULT_DELAY_S) * 1_000_000_000),
            basis: format!(
                "{model_text}; TDR settings unreadable ({err}), so the documented \
                 {TDR_DEFAULT_DELAY_S} s delay is taken"
            ),
        },
    }
}

/// Read the driver model and the TDR settings for CUDA device `ordinal`
/// and decide which watchdog applies.
///
/// Read once per device per process. Both reads describe hardware and a
/// driver configuration that a running process cannot change, and the
/// driver-model read loads NVML, initializes it and shuts it down, which
/// costs tens of milliseconds whatever the span being sized.
pub fn detect(ordinal: usize) -> WatchdogState {
    detect_with_model(ordinal).1
}

/// The driver model and the watchdog state for `ordinal`, from one
/// reading.
///
/// [`detect`] answers the state alone, which is what sizing a quantum
/// needs. A caller reporting both takes them together: the model read
/// loads NVML, initializes it and shuts it down, so asking twice pays
/// that twice for one fact that cannot change while the process runs.
///
/// The model is a `Result` because an unreadable model is a different
/// answer from a known one, and [`decide`] treats it as covered rather
/// than as absent. A reader shown only the state cannot tell those
/// apart.
pub fn detect_with_model(ordinal: usize) -> (Result<DriverModel, String>, WatchdogState) {
    /// Devices the cache covers. A reading is kept per ordinal and
    /// never evicted, and a host has a handful of GPUs.
    const CACHED_ORDINALS: usize = 64;
    type Reading = (Result<DriverModel, String>, WatchdogState);
    static CACHE: [std::sync::OnceLock<Reading>; CACHED_ORDINALS] =
        [const { std::sync::OnceLock::new() }; CACHED_ORDINALS];

    // One slot per ordinal rather than one lock over all of them. The
    // NVML load still happens once per device, which is what the slot
    // is for; two ordinals read at the same time no longer wait on
    // each other to do it.
    match CACHE.get(ordinal) {
        Some(slot) => slot.get_or_init(|| read_watchdog(ordinal)).clone(),
        // Past the cache. Reading it uncached is right rather than
        // refusing: the answer is the same, it is just paid for again.
        None => read_watchdog(ordinal),
    }
}

/// One reading of the driver model and the TDR settings for `ordinal`.
fn read_watchdog(ordinal: usize) -> (Result<DriverModel, String>, WatchdogState) {
    let model = pci_bus_id(ordinal).and_then(|bus_id| nvml_driver_model(&bus_id));
    #[cfg(windows)]
    let tdr = Some(tdr_settings());
    #[cfg(not(windows))]
    let tdr: Option<Result<TdrSettings, String>> = None;
    let state = decide(&model, tdr.as_ref());
    (model, state)
}

/// The PCI bus id the CUDA driver reports for `ordinal`, which is how the
/// CUDA ordinal is matched to NVML's own enumeration.
fn pci_bus_id(ordinal: usize) -> Result<String, String> {
    use cudarc::driver::sys as cu;

    if !crate::backend::detect::cuda_available() {
        return Err("the CUDA driver is not loadable".to_string());
    }
    // SAFETY: only attempts a library load on cudarc's candidate names
    // and reports whether one resolved.
    if !unsafe { cu::is_culib_present() } {
        return Err("cudarc found no CUDA driver library".to_string());
    }
    // SAFETY: the driver is loadable, and cuInit is idempotent.
    let rc = unsafe { cu::cuInit(0) };
    if rc != cu::CUresult::CUDA_SUCCESS {
        return Err(format!("cuInit returned {rc:?}"));
    }
    let driver_ordinal = i32::try_from(ordinal)
        .map_err(|err| format!("device ordinal {ordinal} does not fit the driver's type ({err})"))?;
    let mut dev: cu::CUdevice = 0;
    // SAFETY: the driver is initialized and `dev` is a live out-parameter.
    let rc = unsafe { cu::cuDeviceGet(&mut dev, driver_ordinal) };
    if rc != cu::CUresult::CUDA_SUCCESS {
        return Err(format!("cuDeviceGet({ordinal}) returned {rc:?}"));
    }
    let mut buf = [0 as c_char; 32];
    // SAFETY: the buffer is 32 bytes and that length is passed, so the
    // driver writes within it.
    let rc = unsafe { cu::cuDeviceGetPCIBusId(buf.as_mut_ptr(), buf.len() as i32, dev) };
    if rc != cu::CUresult::CUDA_SUCCESS {
        return Err(format!("cuDeviceGetPCIBusId({ordinal}) returned {rc:?}"));
    }
    // SAFETY: on success the driver leaves a NUL-terminated string inside
    // the buffer.
    let id = unsafe { CStr::from_ptr(buf.as_ptr()) };
    id.to_str()
        .map(str::to_string)
        .map_err(|err| format!("the PCI bus id is not UTF-8 ({err})"))
}

#[cfg(windows)]
const NVML_LIBRARY: &str = "nvml.dll";
#[cfg(not(windows))]
const NVML_LIBRARY: &str = "libnvidia-ml.so.1";

const NVML_SUCCESS: i32 = 0;
const NVML_DRIVER_WDDM: i32 = 0;
const NVML_DRIVER_WDM: i32 = 1;
const NVML_DRIVER_MCDM: i32 = 2;

type NvmlDevice = *mut c_void;
type NvmlInit = unsafe extern "C" fn() -> i32;
type NvmlByBusId = unsafe extern "C" fn(*const c_char, *mut NvmlDevice) -> i32;
type NvmlDriverModel = unsafe extern "C" fn(NvmlDevice, *mut i32, *mut i32) -> i32;

/// The driver model NVML reports for the device at `bus_id`.
///
/// `nvmlDeviceGetDriverModel_v2` is the entry point that knows MCDM, and
/// the older `nvmlDeviceGetDriverModel` is used when a driver predates it.
fn nvml_driver_model(bus_id: &str) -> Result<DriverModel, String> {
    // SAFETY: loading NVML runs the vendor library's initializers, which
    // every NVML consumer runs.
    let lib = unsafe { libloading::Library::new(NVML_LIBRARY) }
        .map_err(|err| format!("{NVML_LIBRARY} did not load ({err})"))?;
    // SAFETY: each signature follows nvml.h for the named symbol.
    let init: libloading::Symbol<NvmlInit> = unsafe { lib.get(b"nvmlInit_v2") }
        .map_err(|err| format!("nvmlInit_v2 is missing ({err})"))?;
    // SAFETY: as above.
    let shutdown: libloading::Symbol<NvmlInit> = unsafe { lib.get(b"nvmlShutdown") }
        .map_err(|err| format!("nvmlShutdown is missing ({err})"))?;
    // SAFETY: as above.
    let by_bus: libloading::Symbol<NvmlByBusId> =
        unsafe { lib.get(b"nvmlDeviceGetHandleByPciBusId_v2") }
            .map_err(|err| format!("nvmlDeviceGetHandleByPciBusId_v2 is missing ({err})"))?;
    // SAFETY: as above; the two entry points share a signature.
    let model_fn: libloading::Symbol<NvmlDriverModel> =
        match unsafe { lib.get(b"nvmlDeviceGetDriverModel_v2") } {
            Ok(f) => f,
            Err(v2_err) => unsafe { lib.get(b"nvmlDeviceGetDriverModel") }.map_err(|err| {
                format!(
                    "neither nvmlDeviceGetDriverModel_v2 ({v2_err}) nor \
                     nvmlDeviceGetDriverModel ({err}) is exported"
                )
            })?,
        };

    let bus = CString::new(bus_id)
        .map_err(|err| format!("the PCI bus id {bus_id:?} holds a NUL ({err})"))?;
    // SAFETY: NVML is loaded; init takes no arguments.
    let rc = unsafe { init() };
    if rc != NVML_SUCCESS {
        return Err(format!("nvmlInit_v2 returned {rc}"));
    }
    let mut device: NvmlDevice = std::ptr::null_mut();
    // SAFETY: NVML is initialized, `bus` is NUL-terminated and outlives the
    // call, and `device` is a live out-parameter.
    let rc = unsafe { by_bus(bus.as_ptr(), &mut device) };
    let result = if rc != NVML_SUCCESS {
        Err(format!("nvmlDeviceGetHandleByPciBusId_v2({bus_id}) returned {rc}"))
    } else {
        let mut current = -1i32;
        let mut pending = -1i32;
        // SAFETY: `device` is the handle NVML just returned, and both
        // out-parameters are live.
        let rc = unsafe { model_fn(device, &mut current, &mut pending) };
        match (rc, current) {
            (NVML_SUCCESS, NVML_DRIVER_WDDM) => Ok(DriverModel::Wddm),
            (NVML_SUCCESS, NVML_DRIVER_WDM) => Ok(DriverModel::Tcc),
            (NVML_SUCCESS, NVML_DRIVER_MCDM) => Ok(DriverModel::Mcdm),
            (NVML_SUCCESS, other) => Err(format!("NVML reported driver model {other}")),
            (failed, word) => Err(format!(
                "the driver model query returned {failed}, leaving the model word at {word}"
            )),
        }
    };
    // SAFETY: balances the successful init above.
    let rc = unsafe { shutdown() };
    if rc != NVML_SUCCESS {
        notice!("flynnel gpu_peer watchdog: nvmlShutdown returned {rc}");
    }
    result
}

/// The TDR level and delay from the registry, each taking its documented
/// default when the value is absent.
#[cfg(windows)]
fn tdr_settings() -> Result<TdrSettings, String> {
    let level = match registry_dword("TdrLevel")? {
        Some(value) => value,
        None => TDR_DEFAULT_LEVEL,
    };
    let delay_s = match registry_dword("TdrDelay")? {
        Some(value) => value,
        None => TDR_DEFAULT_DELAY_S,
    };
    Ok((level, delay_s))
}

/// A DWORD under `HKLM\SYSTEM\CurrentControlSet\Control\GraphicsDrivers`,
/// or `None` when the value is not present.
#[cfg(windows)]
fn registry_dword(name: &str) -> Result<Option<u32>, String> {
    use std::os::windows::ffi::OsStrExt;

    // The predefined key is the 32-bit value 0x80000002 sign-extended to
    // pointer width, which is how the Windows headers define it.
    const HKEY_LOCAL_MACHINE: isize = 0x8000_0002_u32 as i32 as isize;
    const RRF_RT_REG_DWORD: u32 = 0x0000_0010;
    const ERROR_SUCCESS: i32 = 0;
    const ERROR_FILE_NOT_FOUND: i32 = 2;

    #[link(name = "advapi32")]
    unsafe extern "system" {
        fn RegGetValueW(
            hkey: isize,
            sub_key: *const u16,
            value: *const u16,
            flags: u32,
            kind: *mut u32,
            data: *mut c_void,
            data_len: *mut u32,
        ) -> i32;
    }

    let wide = |s: &str| -> Vec<u16> { std::ffi::OsStr::new(s).encode_wide().chain(Some(0)).collect() };
    let key = wide(r"SYSTEM\CurrentControlSet\Control\GraphicsDrivers");
    let value_name = wide(name);
    let mut data: u32 = 0;
    let mut len: u32 = 4;
    // SAFETY: both names are NUL-terminated UTF-16 that outlive the call,
    // `data` is a four-byte out-parameter whose size `len` states, and
    // RRF_RT_REG_DWORD makes the call refuse a value of any other type.
    let status = unsafe {
        RegGetValueW(
            HKEY_LOCAL_MACHINE,
            key.as_ptr(),
            value_name.as_ptr(),
            RRF_RT_REG_DWORD,
            std::ptr::null_mut(),
            (&mut data as *mut u32).cast(),
            &mut len,
        )
    };
    match status {
        ERROR_SUCCESS => Ok(Some(data)),
        ERROR_FILE_NOT_FOUND => Ok(None),
        other => Err(format!("RegGetValueW(GraphicsDrivers\\{name}) returned {other}")),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_platform_without_tdr_has_no_watchdog() {
        let state = decide(&Ok(DriverModel::Wddm), None);
        assert!(!state.applies());
        assert!(state.basis.contains("no watchdog is known"), "{}", state.basis);
    }

    #[test]
    fn a_tcc_device_is_outside_tdr_whatever_the_settings() {
        let state = decide(&Ok(DriverModel::Tcc), Some(&Ok((3, 2))));
        assert!(!state.applies());
        assert!(state.basis.contains("TCC"), "{}", state.basis);
    }

    #[test]
    fn tdr_level_zero_disables_the_watchdog() {
        let state = decide(&Ok(DriverModel::Wddm), Some(&Ok((0, 2))));
        assert!(!state.applies());
        assert!(state.basis.contains("TdrLevel 0"), "{}", state.basis);
    }

    #[test]
    fn a_covered_device_takes_the_registry_delay() {
        let wddm = decide(&Ok(DriverModel::Wddm), Some(&Ok((3, 8))));
        assert_eq!(wddm.delay_ns, Some(8_000_000_000));
        let mcdm = decide(&Ok(DriverModel::Mcdm), Some(&Ok((3, 2))));
        assert_eq!(mcdm.delay_ns, Some(2_000_000_000));
    }

    #[test]
    fn unreadable_settings_take_the_documented_delay_and_say_so() {
        let state = decide(&Ok(DriverModel::Wddm), Some(&Err("denied".to_string())));
        assert_eq!(state.delay_ns, Some(u64::from(TDR_DEFAULT_DELAY_S) * 1_000_000_000));
        assert!(state.basis.contains("unreadable (denied)"), "{}", state.basis);
    }

    #[test]
    fn an_unreadable_driver_model_is_treated_as_covered() {
        let state = decide(&Err("no NVML".to_string()), Some(&Ok((3, 2))));
        assert_eq!(state.delay_ns, Some(2_000_000_000));
        assert!(state.basis.contains("driver model unreadable (no NVML)"), "{}", state.basis);
    }

    #[test]
    fn the_model_returned_beside_a_state_is_the_one_that_state_was_decided_from() {
        // Runs on a host with a card and on one without: the pair has
        // to agree either way, and the deviceless case is the one where
        // a mismatch would be invisible because both halves are an
        // error.
        let (model, state) = detect_with_model(0);
        let expected = match &model {
            Ok(DriverModel::Tcc) => "driver model Tcc".to_string(),
            Ok(covered) => format!("driver model {covered:?}"),
            Err(err) => format!("driver model unreadable ({err})"),
        };
        assert!(
            state.basis.starts_with(&expected),
            "the state says {:?} and the model beside it is {:?}",
            state.basis,
            model
        );
    }

    #[test]
    fn detect_answers_the_state_half_of_the_pair() {
        // The two entry points read the same cache, so a caller that
        // uses both never pays the NVML load twice and never sees two
        // different answers for one device.
        assert_eq!(detect(0), detect_with_model(0).1);
    }
}