molgfx-core 0.2.0

The semantic scene graph: columnar tables, GPU record layouts, the borrowed coordinate seam.
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Core residency state transitions and deterministic pressure handling.
//!
//! Entry lookup, transitions and pressure selection are `O(log chunks)`.

use super::pressure::PressureKey;
use super::types::{
    ChunkFootprint, DeviceLossReport, Eviction, FailureReason, ResidencyBudget, ResidencyClass,
    ResidencyError, ResidencyKey, ResidencyOutput, ResidencyPhase, ResidencyRequest,
    ResidencySnapshot, ResidencyTicket, StaleCompletion, Usage,
};
use std::collections::{BTreeMap, BTreeSet};

#[cfg(test)]
#[path = "machine_tests.rs"]
mod tests;

#[derive(Clone, Copy, Debug)]
pub(super) struct Entry {
    pub(super) generation: u64,
    pub(super) phase: ResidencyPhase,
    pub(super) footprint: ChunkFootprint,
    pub(super) class: ResidencyClass,
    pub(super) priority: i32,
    pub(super) failure: Option<FailureReason>,
}

impl Entry {
    const fn snapshot(self) -> ResidencySnapshot {
        ResidencySnapshot {
            phase: self.phase,
            generation: self.generation,
            failure: self.failure,
        }
    }
}

/// Stateful out-of-core coordinator with incrementally maintained pressure indices.
#[derive(Debug)]
pub struct ResidencyMachine {
    pub(super) budget: ResidencyBudget,
    pub(super) usage: Usage,
    pub(super) entries: BTreeMap<ResidencyKey, Entry>,
    pub(super) resident_hot: BTreeSet<PressureKey>,
    pub(super) resident_warm: BTreeSet<PressureKey>,
    pub(super) requested: BTreeSet<PressureKey>,
    pub(super) ready_cpu: BTreeSet<PressureKey>,
    pub(super) uploading: BTreeSet<PressureKey>,
}

impl ResidencyMachine {
    /// Creates an empty machine with hard independent tier budgets.
    #[must_use]
    pub fn new(budget: ResidencyBudget) -> Self {
        Self {
            budget,
            usage: Usage::default(),
            entries: BTreeMap::new(),
            resident_hot: BTreeSet::new(),
            resident_warm: BTreeSet::new(),
            requested: BTreeSet::new(),
            ready_cpu: BTreeSet::new(),
            uploading: BTreeSet::new(),
        }
    }

    /// Current hard limits.
    #[must_use]
    pub const fn budget(&self) -> ResidencyBudget {
        self.budget
    }

    /// Current charged bytes.
    #[must_use]
    pub const fn usage(&self) -> Usage {
        self.usage
    }

    /// Returns the state of a known key. Unknown keys are absent at generation zero.
    #[must_use]
    pub fn snapshot(&self, key: ResidencyKey) -> ResidencySnapshot {
        match self.entries.get(&key).copied() {
            Some(entry) => entry.snapshot(),
            None => ResidencySnapshot {
                phase: ResidencyPhase::Absent,
                generation: 0,
                failure: None,
            },
        }
    }

    pub(super) fn footprint(&self, key: ResidencyKey) -> Option<ChunkFootprint> {
        self.entries.get(&key).map(|entry| entry.footprint)
    }

    /// Starts a new generation and emits a provider request.
    ///
    /// Existing asynchronous work is cancelled. Resident data is evicted before
    /// the replacement request is emitted.
    ///
    /// # Errors
    ///
    /// Returns a typed error if the request exceeds the in-flight budget or the
    /// key has exhausted its generation counter.
    pub fn request_into(
        &mut self,
        request: ResidencyRequest,
        output: &mut ResidencyOutput,
    ) -> Result<ResidencyTicket, ResidencyError> {
        output.clear();
        let previous = self.entries.get(&request.key).copied();
        let replaced_bytes = match previous {
            Some(entry) if entry.phase == ResidencyPhase::Requested => entry.footprint.source_bytes,
            _ => 0,
        };
        let projected = self
            .usage
            .in_flight
            .saturating_sub(replaced_bytes)
            .saturating_add(request.footprint.source_bytes);
        if request.footprint.source_bytes > self.budget.in_flight
            || projected > self.budget.in_flight
        {
            return Err(ResidencyError::BudgetExceeded { tier: "in-flight" });
        }
        let generation = match previous {
            Some(entry) => match entry.generation.checked_add(1) {
                Some(value) => value,
                None => return Err(ResidencyError::GenerationExhausted),
            },
            None => 1,
        };
        if let Some(entry) = previous {
            self.release(request.key, entry, output);
            self.index_remove(request.key, entry);
        }
        let ticket = ResidencyTicket::issue(request.key, generation);
        let entry = Entry {
            generation,
            phase: ResidencyPhase::Requested,
            footprint: request.footprint,
            class: request.class,
            priority: request.priority,
            failure: None,
        };
        self.entries.insert(request.key, entry);
        self.index_insert(request.key, entry);
        self.usage.in_flight = self
            .usage
            .in_flight
            .saturating_add(request.footprint.source_bytes);
        output.requests.push(ticket);
        Ok(ticket)
    }

    /// Accepts a decoded payload from the provider.
    ///
    /// # Errors
    ///
    /// Returns a typed error for a current ticket in the wrong phase or when
    /// neither resident eviction nor the CPU budget can admit the payload.
    pub fn ready_cpu_into(
        &mut self,
        ticket: ResidencyTicket,
        output: &mut ResidencyOutput,
    ) -> Result<bool, ResidencyError> {
        output.clear();
        let Some(entry) = self.current(ticket, output) else {
            return Ok(false);
        };
        if entry.phase != ResidencyPhase::Requested {
            return Err(Self::invalid(ResidencyPhase::Requested, entry.phase));
        }
        self.usage.in_flight = self
            .usage
            .in_flight
            .saturating_sub(entry.footprint.source_bytes);
        self.evict_for_cpu(entry.footprint.host_bytes, Some(ticket.key), output);
        if self.usage.cpu.saturating_add(entry.footprint.host_bytes) > self.budget.cpu {
            self.set_absent(ticket.key, Some(FailureReason::BudgetExceeded));
            return Err(ResidencyError::BudgetExceeded { tier: "cpu" });
        }
        self.usage.cpu = self.usage.cpu.saturating_add(entry.footprint.host_bytes);
        self.set_phase(ticket.key, ResidencyPhase::ReadyCpu);
        Ok(true)
    }

    /// Reserves staging for an upload.
    ///
    /// # Errors
    ///
    /// Returns a typed error for a current ticket in the wrong phase or when
    /// the staging budget cannot admit the upload.
    pub fn begin_upload_into(
        &mut self,
        ticket: ResidencyTicket,
        output: &mut ResidencyOutput,
    ) -> Result<bool, ResidencyError> {
        output.clear();
        let Some(entry) = self.current(ticket, output) else {
            return Ok(false);
        };
        if entry.phase != ResidencyPhase::ReadyCpu {
            return Err(Self::invalid(ResidencyPhase::ReadyCpu, entry.phase));
        }
        if self
            .usage
            .staging
            .saturating_add(entry.footprint.staging_bytes)
            > self.budget.staging
        {
            return Err(ResidencyError::BudgetExceeded { tier: "staging" });
        }
        self.usage.staging = self
            .usage
            .staging
            .saturating_add(entry.footprint.staging_bytes);
        self.set_phase(ticket.key, ResidencyPhase::Uploading);
        Ok(true)
    }

    /// Releases staging after backpressure while preserving the CPU payload.
    ///
    /// # Errors
    ///
    /// Rejects stale tickets and phases other than uploading.
    pub fn defer_upload_into(
        &mut self,
        ticket: ResidencyTicket,
        output: &mut ResidencyOutput,
    ) -> Result<bool, ResidencyError> {
        output.clear();
        let Some(entry) = self.current(ticket, output) else {
            return Ok(false);
        };
        if entry.phase != ResidencyPhase::Uploading {
            return Err(Self::invalid(ResidencyPhase::Uploading, entry.phase));
        }
        self.usage.staging = self
            .usage
            .staging
            .saturating_sub(entry.footprint.staging_bytes);
        self.set_phase(ticket.key, ResidencyPhase::ReadyCpu);
        Ok(true)
    }

    /// Commits an uploaded allocation to its requested device tier.
    ///
    /// # Errors
    ///
    /// Returns a typed error for a current ticket in the wrong phase or when
    /// deterministic eviction cannot free enough device memory.
    pub fn complete_upload_into(
        &mut self,
        ticket: ResidencyTicket,
        output: &mut ResidencyOutput,
    ) -> Result<bool, ResidencyError> {
        output.clear();
        let Some(entry) = self.current(ticket, output) else {
            return Ok(false);
        };
        if entry.phase != ResidencyPhase::Uploading {
            return Err(Self::invalid(ResidencyPhase::Uploading, entry.phase));
        }
        self.usage.staging = self
            .usage
            .staging
            .saturating_sub(entry.footprint.staging_bytes);
        self.evict_for_gpu(
            entry.class,
            entry.footprint.gpu_bytes,
            Some(ticket.key),
            output,
        );
        if !self.gpu_fits(entry.class, entry.footprint.gpu_bytes) {
            self.set_phase(ticket.key, ResidencyPhase::ReadyCpu);
            return Err(ResidencyError::BudgetExceeded {
                tier: Self::gpu_tier(entry.class),
            });
        }
        self.add_gpu(entry.class, entry.footprint.gpu_bytes);
        self.set_phase(ticket.key, ResidencyPhase::Resident);
        Ok(true)
    }

    /// Cancels current provider or upload work. Stale tickets are ignored.
    pub fn cancel_into(&mut self, ticket: ResidencyTicket, output: &mut ResidencyOutput) -> bool {
        output.clear();
        let Some(entry) = self.current(ticket, output) else {
            return false;
        };
        self.release(ticket.key, entry, output);
        self.set_absent(ticket.key, None);
        true
    }

    /// Records a provider or validation failure and releases active resources.
    pub fn fail_into(
        &mut self,
        ticket: ResidencyTicket,
        reason: FailureReason,
        output: &mut ResidencyOutput,
    ) -> bool {
        output.clear();
        let Some(entry) = self.current(ticket, output) else {
            return false;
        };
        self.release(ticket.key, entry, output);
        self.set_absent(ticket.key, Some(reason));
        true
    }

    /// Updates hard budgets and evicts deterministically until resident tiers fit.
    pub fn set_budget_into(&mut self, budget: ResidencyBudget, output: &mut ResidencyOutput) {
        output.clear();
        self.budget = budget;
        self.reconcile_resident(output);
    }

    /// Invalidates all device allocations while preserving CPU-ready payloads.
    ///
    /// # Errors
    ///
    /// Returns a typed error without changing state if an uploading ticket can
    /// no longer advance to a fresh generation.
    pub fn device_lost_into(
        &mut self,
        output: &mut ResidencyOutput,
    ) -> Result<DeviceLossReport, ResidencyError> {
        output.clear();
        if self
            .entries
            .values()
            .any(|entry| entry.phase == ResidencyPhase::Uploading && entry.generation == u64::MAX)
        {
            return Err(ResidencyError::GenerationExhausted);
        }
        let mut report = DeviceLossReport::default();
        for (key, entry) in &mut self.entries {
            if matches!(
                entry.phase,
                ResidencyPhase::Uploading | ResidencyPhase::Resident
            ) {
                if entry.phase == ResidencyPhase::Resident {
                    output.evictions.push(Eviction {
                        key: *key,
                        generation: entry.generation,
                    });
                    report.invalidated = report.invalidated.saturating_add(1);
                } else {
                    output
                        .cancellations
                        .push(ResidencyTicket::issue(*key, entry.generation));
                    entry.generation += 1;
                }
                entry.phase = ResidencyPhase::ReadyCpu;
                output
                    .ready_uploads
                    .push(ResidencyTicket::issue(*key, entry.generation));
                report.ready_cpu = report.ready_cpu.saturating_add(1);
            }
        }
        self.usage.staging = 0;
        self.usage.gpu_hot = 0;
        self.usage.gpu_warm = 0;
        self.rebuild_pressure_indices();
        Ok(report)
    }

    fn current(&self, ticket: ResidencyTicket, output: &mut ResidencyOutput) -> Option<Entry> {
        let current = self.entries.get(&ticket.key).copied();
        let (generation, phase) = match current {
            Some(entry) => (entry.generation, entry.phase),
            None => (0, ResidencyPhase::Absent),
        };
        if generation != ticket.generation() || phase == ResidencyPhase::Absent {
            output.stale.push(StaleCompletion {
                ticket,
                current_generation: generation,
                current_phase: phase,
            });
            return None;
        }
        current
    }

    fn set_phase(&mut self, key: ResidencyKey, phase: ResidencyPhase) {
        let Some(previous) = self.entries.get(&key).copied() else {
            return;
        };
        self.index_remove(key, previous);
        if let Some(entry) = self.entries.get_mut(&key) {
            entry.phase = phase;
            entry.failure = None;
        }
        if let Some(entry) = self.entries.get(&key).copied() {
            self.index_insert(key, entry);
        }
    }

    pub(super) fn set_absent(&mut self, key: ResidencyKey, failure: Option<FailureReason>) {
        let Some(previous) = self.entries.get(&key).copied() else {
            return;
        };
        self.index_remove(key, previous);
        if let Some(entry) = self.entries.get_mut(&key) {
            entry.phase = ResidencyPhase::Absent;
            entry.failure = failure;
        }
    }

    pub(super) fn release(
        &mut self,
        key: ResidencyKey,
        entry: Entry,
        output: &mut ResidencyOutput,
    ) {
        if matches!(
            entry.phase,
            ResidencyPhase::Requested | ResidencyPhase::Uploading
        ) {
            output
                .cancellations
                .push(ResidencyTicket::issue(key, entry.generation));
        }
        if entry.phase == ResidencyPhase::Resident {
            output.evictions.push(Eviction {
                key,
                generation: entry.generation,
            });
        }
        self.release_accounting(entry);
    }

    fn release_accounting(&mut self, entry: Entry) {
        match entry.phase {
            ResidencyPhase::Absent => {}
            ResidencyPhase::Requested => {
                self.usage.in_flight = self
                    .usage
                    .in_flight
                    .saturating_sub(entry.footprint.source_bytes);
            }
            ResidencyPhase::ReadyCpu => {
                self.usage.cpu = self.usage.cpu.saturating_sub(entry.footprint.host_bytes);
            }
            ResidencyPhase::Uploading => {
                self.usage.cpu = self.usage.cpu.saturating_sub(entry.footprint.host_bytes);
                self.usage.staging = self
                    .usage
                    .staging
                    .saturating_sub(entry.footprint.staging_bytes);
            }
            ResidencyPhase::Resident => {
                self.usage.cpu = self.usage.cpu.saturating_sub(entry.footprint.host_bytes);
                self.sub_gpu(entry.class, entry.footprint.gpu_bytes);
            }
        }
    }

    const fn invalid(required: ResidencyPhase, actual: ResidencyPhase) -> ResidencyError {
        ResidencyError::InvalidPhase { required, actual }
    }

    const fn gpu_tier(class: ResidencyClass) -> &'static str {
        match class {
            ResidencyClass::Hot => "gpu-hot",
            ResidencyClass::Warm => "gpu-warm",
        }
    }

    fn add_gpu(&mut self, class: ResidencyClass, bytes: u64) {
        match class {
            ResidencyClass::Hot => self.usage.gpu_hot = self.usage.gpu_hot.saturating_add(bytes),
            ResidencyClass::Warm => self.usage.gpu_warm = self.usage.gpu_warm.saturating_add(bytes),
        }
    }

    fn sub_gpu(&mut self, class: ResidencyClass, bytes: u64) {
        match class {
            ResidencyClass::Hot => self.usage.gpu_hot = self.usage.gpu_hot.saturating_sub(bytes),
            ResidencyClass::Warm => self.usage.gpu_warm = self.usage.gpu_warm.saturating_sub(bytes),
        }
    }
}