oximedia-gpu 0.1.1

GPU compute pipeline using WGPU for OxiMedia - cross-platform acceleration
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#![allow(dead_code)]
//! Asynchronous upload queue for staging CPU data to GPU buffers.
//!
//! This module provides a batched upload mechanism that stages host-side data
//! into a queue and flushes it to GPU-side buffers in optimized batches,
//! reducing per-transfer overhead.

use std::collections::VecDeque;
use std::time::Instant;

/// Unique identifier for an upload request.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UploadId(
    /// Inner identifier value.
    pub u64,
);

/// Priority level for upload requests.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum UploadPriority {
    /// Low priority - background uploads.
    Low = 0,
    /// Normal priority - standard uploads.
    #[default]
    Normal = 1,
    /// High priority - latency-sensitive uploads.
    High = 2,
    /// Critical priority - must be processed immediately.
    Critical = 3,
}

/// The current state of an upload request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UploadState {
    /// Queued and waiting to be processed.
    Queued,
    /// Currently being transferred.
    Transferring,
    /// Transfer completed successfully.
    Completed,
    /// Transfer failed.
    Failed,
    /// Transfer was cancelled.
    Cancelled,
}

/// Describes the destination for an upload.
#[derive(Debug, Clone)]
pub struct UploadTarget {
    /// Target buffer identifier.
    pub buffer_id: u64,
    /// Byte offset within the target buffer.
    pub offset: usize,
    /// Expected total size of the target buffer.
    pub buffer_size: usize,
}

impl UploadTarget {
    /// Create a new upload target.
    #[must_use]
    pub fn new(buffer_id: u64, offset: usize, buffer_size: usize) -> Self {
        Self {
            buffer_id,
            offset,
            buffer_size,
        }
    }

    /// Check whether the given data size fits within the target at the offset.
    #[must_use]
    pub fn fits(&self, data_size: usize) -> bool {
        self.offset + data_size <= self.buffer_size
    }
}

/// A single upload request in the queue.
#[derive(Debug, Clone)]
pub struct UploadRequest {
    /// Unique identifier for this request.
    pub id: UploadId,
    /// The data to upload.
    pub data: Vec<u8>,
    /// Target destination for the data.
    pub target: UploadTarget,
    /// Priority level.
    pub priority: UploadPriority,
    /// Current state.
    pub state: UploadState,
    /// Timestamp when the request was enqueued.
    pub enqueue_time: Instant,
    /// Timestamp when the transfer completed.
    pub complete_time: Option<Instant>,
}

impl UploadRequest {
    /// Create a new upload request.
    #[must_use]
    pub fn new(
        id: UploadId,
        data: Vec<u8>,
        target: UploadTarget,
        priority: UploadPriority,
    ) -> Self {
        Self {
            id,
            data,
            target,
            priority,
            state: UploadState::Queued,
            enqueue_time: Instant::now(),
            complete_time: None,
        }
    }

    /// Return the size of the data in bytes.
    #[must_use]
    pub fn data_size(&self) -> usize {
        self.data.len()
    }

    /// Return the transfer latency if the upload is completed.
    #[must_use]
    pub fn latency(&self) -> Option<std::time::Duration> {
        self.complete_time
            .map(|t| t.duration_since(self.enqueue_time))
    }
}

/// Configuration for the upload queue.
#[derive(Debug, Clone)]
pub struct UploadQueueConfig {
    /// Maximum number of pending requests.
    pub max_pending: usize,
    /// Maximum total bytes that can be queued.
    pub max_queued_bytes: usize,
    /// Batch size for flush operations.
    pub flush_batch_size: usize,
    /// Whether to sort by priority before flushing.
    pub priority_sort: bool,
}

impl Default for UploadQueueConfig {
    fn default() -> Self {
        Self {
            max_pending: 256,
            max_queued_bytes: 256 * 1024 * 1024, // 256 MB
            flush_batch_size: 32,
            priority_sort: true,
        }
    }
}

/// Statistics for the upload queue.
#[derive(Debug, Clone, Default)]
pub struct UploadQueueStats {
    /// Number of requests currently in the queue.
    pub queued_count: usize,
    /// Total bytes currently queued.
    pub queued_bytes: usize,
    /// Total number of requests processed.
    pub total_processed: u64,
    /// Total bytes transferred.
    pub total_bytes_transferred: u64,
    /// Number of failed requests.
    pub failed_count: u64,
    /// Number of cancelled requests.
    pub cancelled_count: u64,
    /// Average transfer throughput in bytes per second.
    pub avg_throughput_bps: f64,
}

impl UploadQueueStats {
    /// Return queued bytes in megabytes.
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn queued_mb(&self) -> f64 {
        self.queued_bytes as f64 / (1024.0 * 1024.0)
    }

    /// Return total transferred in megabytes.
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn transferred_mb(&self) -> f64 {
        self.total_bytes_transferred as f64 / (1024.0 * 1024.0)
    }
}

/// An asynchronous upload queue that batches host-to-device transfers.
pub struct UploadQueue {
    /// Pending upload requests.
    queue: VecDeque<UploadRequest>,
    /// Completed upload requests (for stats).
    completed: Vec<UploadRequest>,
    /// Configuration.
    config: UploadQueueConfig,
    /// Counter for generating unique upload IDs.
    next_id: u64,
    /// Total bytes transferred.
    total_bytes_transferred: u64,
    /// Failed request count.
    failed_count: u64,
    /// Cancelled request count.
    cancelled_count: u64,
}

impl UploadQueue {
    /// Create a new upload queue with default configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::with_config(UploadQueueConfig::default())
    }

    /// Create a new upload queue with the given configuration.
    #[must_use]
    pub fn with_config(config: UploadQueueConfig) -> Self {
        Self {
            queue: VecDeque::new(),
            completed: Vec::new(),
            config,
            next_id: 0,
            total_bytes_transferred: 0,
            failed_count: 0,
            cancelled_count: 0,
        }
    }

    /// Enqueue an upload request. Returns the `UploadId` if successful,
    /// or `None` if the queue is full.
    pub fn enqueue(
        &mut self,
        data: Vec<u8>,
        target: UploadTarget,
        priority: UploadPriority,
    ) -> Option<UploadId> {
        let current_bytes: usize = self.queue.iter().map(|r| r.data.len()).sum();
        if self.queue.len() >= self.config.max_pending {
            return None;
        }
        if current_bytes + data.len() > self.config.max_queued_bytes {
            return None;
        }
        let id = UploadId(self.next_id);
        self.next_id += 1;
        let request = UploadRequest::new(id, data, target, priority);
        self.queue.push_back(request);
        Some(id)
    }

    /// Flush a batch of uploads, simulating the transfer to GPU.
    /// Returns the IDs of requests that were processed.
    pub fn flush(&mut self) -> Vec<UploadId> {
        if self.config.priority_sort {
            let mut items: Vec<UploadRequest> = self.queue.drain(..).collect();
            items.sort_by(|a, b| b.priority.cmp(&a.priority));
            self.queue = items.into_iter().collect();
        }

        let batch_size = self.config.flush_batch_size.min(self.queue.len());
        let mut processed = Vec::with_capacity(batch_size);

        for _ in 0..batch_size {
            if let Some(mut request) = self.queue.pop_front() {
                if request.target.fits(request.data.len()) {
                    request.state = UploadState::Completed;
                    request.complete_time = Some(Instant::now());
                    self.total_bytes_transferred += request.data.len() as u64;
                } else {
                    request.state = UploadState::Failed;
                    self.failed_count += 1;
                }
                processed.push(request.id);
                self.completed.push(request);
            }
        }

        processed
    }

    /// Cancel a pending upload by ID.
    pub fn cancel(&mut self, id: UploadId) -> bool {
        if let Some(pos) = self.queue.iter().position(|r| r.id == id) {
            let mut request = match self.queue.remove(pos) {
                Some(r) => r,
                None => return false,
            };
            request.state = UploadState::Cancelled;
            self.cancelled_count += 1;
            self.completed.push(request);
            true
        } else {
            false
        }
    }

    /// Return the number of pending requests.
    #[must_use]
    pub fn pending_count(&self) -> usize {
        self.queue.len()
    }

    /// Return the total queued bytes.
    #[must_use]
    pub fn queued_bytes(&self) -> usize {
        self.queue.iter().map(|r| r.data.len()).sum()
    }

    /// Check if the queue is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.queue.is_empty()
    }

    /// Return the state of a request by ID.
    #[must_use]
    pub fn request_state(&self, id: UploadId) -> Option<UploadState> {
        self.queue
            .iter()
            .find(|r| r.id == id)
            .map(|r| r.state)
            .or_else(|| self.completed.iter().find(|r| r.id == id).map(|r| r.state))
    }

    /// Return statistics about the queue.
    #[allow(clippy::cast_precision_loss)]
    #[must_use]
    pub fn stats(&self) -> UploadQueueStats {
        let queued_bytes: usize = self.queue.iter().map(|r| r.data.len()).sum();
        let total_processed = self.completed.len() as u64;
        let latencies: Vec<f64> = self
            .completed
            .iter()
            .filter_map(UploadRequest::latency)
            .map(|d| d.as_secs_f64())
            .collect();
        let avg_throughput_bps = if latencies.is_empty() {
            0.0
        } else {
            let total_secs: f64 = latencies.iter().sum();
            if total_secs > 0.0 {
                self.total_bytes_transferred as f64 / total_secs
            } else {
                0.0
            }
        };

        UploadQueueStats {
            queued_count: self.queue.len(),
            queued_bytes,
            total_processed,
            total_bytes_transferred: self.total_bytes_transferred,
            failed_count: self.failed_count,
            cancelled_count: self.cancelled_count,
            avg_throughput_bps,
        }
    }

    /// Clear all pending requests (marking them cancelled).
    pub fn clear(&mut self) {
        while let Some(mut req) = self.queue.pop_front() {
            req.state = UploadState::Cancelled;
            self.cancelled_count += 1;
            self.completed.push(req);
        }
    }

    /// Peek at the next request that would be processed.
    #[must_use]
    pub fn peek_next(&self) -> Option<&UploadRequest> {
        self.queue.front()
    }
}

impl Default for UploadQueue {
    fn default() -> Self {
        Self::new()
    }
}

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

    fn make_target(size: usize) -> UploadTarget {
        UploadTarget::new(1, 0, size)
    }

    #[test]
    fn test_create_queue() {
        let queue = UploadQueue::new();
        assert!(queue.is_empty());
        assert_eq!(queue.pending_count(), 0);
    }

    #[test]
    fn test_enqueue_request() {
        let mut queue = UploadQueue::new();
        let data = vec![0u8; 1024];
        let target = make_target(2048);
        let id = queue.enqueue(data, target, UploadPriority::Normal);
        assert!(id.is_some());
        assert_eq!(queue.pending_count(), 1);
    }

    #[test]
    fn test_enqueue_limit() {
        let config = UploadQueueConfig {
            max_pending: 2,
            ..Default::default()
        };
        let mut queue = UploadQueue::with_config(config);
        let t1 = make_target(4096);
        let t2 = make_target(4096);
        let t3 = make_target(4096);
        assert!(queue
            .enqueue(vec![0; 100], t1, UploadPriority::Normal)
            .is_some());
        assert!(queue
            .enqueue(vec![0; 100], t2, UploadPriority::Normal)
            .is_some());
        assert!(queue
            .enqueue(vec![0; 100], t3, UploadPriority::Normal)
            .is_none());
    }

    #[test]
    fn test_enqueue_byte_limit() {
        let config = UploadQueueConfig {
            max_queued_bytes: 200,
            ..Default::default()
        };
        let mut queue = UploadQueue::with_config(config);
        let t1 = make_target(4096);
        let t2 = make_target(4096);
        assert!(queue
            .enqueue(vec![0; 150], t1, UploadPriority::Normal)
            .is_some());
        assert!(queue
            .enqueue(vec![0; 100], t2, UploadPriority::Normal)
            .is_none());
    }

    #[test]
    fn test_flush_batch() {
        let mut queue = UploadQueue::new();
        for i in 0..5 {
            let target = make_target(4096);
            queue.enqueue(vec![0u8; 100], target, UploadPriority::Normal);
            let _ = i;
        }
        let processed = queue.flush();
        assert!(!processed.is_empty());
    }

    #[test]
    fn test_flush_priority_ordering() {
        let config = UploadQueueConfig {
            priority_sort: true,
            flush_batch_size: 1,
            ..Default::default()
        };
        let mut queue = UploadQueue::with_config(config);
        let t1 = make_target(4096);
        let t2 = make_target(4096);
        let _low_id = queue
            .enqueue(vec![1], t1, UploadPriority::Low)
            .expect("enqueue should succeed");
        let high_id = queue
            .enqueue(vec![2], t2, UploadPriority::Critical)
            .expect("operation should succeed in test");
        let processed = queue.flush();
        assert_eq!(processed.len(), 1);
        assert_eq!(processed[0], high_id);
    }

    #[test]
    fn test_cancel_request() {
        let mut queue = UploadQueue::new();
        let target = make_target(4096);
        let id = queue
            .enqueue(vec![0; 100], target, UploadPriority::Normal)
            .expect("operation should succeed in test");
        assert!(queue.cancel(id));
        assert!(queue.is_empty());
        assert_eq!(queue.request_state(id), Some(UploadState::Cancelled));
    }

    #[test]
    fn test_cancel_nonexistent() {
        let mut queue = UploadQueue::new();
        assert!(!queue.cancel(UploadId(999)));
    }

    #[test]
    fn test_request_state_tracking() {
        let mut queue = UploadQueue::new();
        let target = make_target(4096);
        let id = queue
            .enqueue(vec![0; 100], target, UploadPriority::Normal)
            .expect("operation should succeed in test");
        assert_eq!(queue.request_state(id), Some(UploadState::Queued));
        queue.flush();
        assert_eq!(queue.request_state(id), Some(UploadState::Completed));
    }

    #[test]
    fn test_failed_transfer() {
        let mut queue = UploadQueue::new();
        // Target too small for data
        let target = UploadTarget::new(1, 0, 10);
        let id = queue
            .enqueue(vec![0; 100], target, UploadPriority::Normal)
            .expect("operation should succeed in test");
        queue.flush();
        assert_eq!(queue.request_state(id), Some(UploadState::Failed));
    }

    #[test]
    fn test_queued_bytes() {
        let mut queue = UploadQueue::new();
        let t1 = make_target(4096);
        let t2 = make_target(4096);
        queue.enqueue(vec![0; 100], t1, UploadPriority::Normal);
        queue.enqueue(vec![0; 200], t2, UploadPriority::Normal);
        assert_eq!(queue.queued_bytes(), 300);
    }

    #[test]
    fn test_stats() {
        let mut queue = UploadQueue::new();
        let target = make_target(4096);
        queue.enqueue(vec![0; 1024], target, UploadPriority::Normal);
        queue.flush();
        let stats = queue.stats();
        assert_eq!(stats.total_processed, 1);
        assert_eq!(stats.total_bytes_transferred, 1024);
    }

    #[test]
    fn test_stats_mb_conversion() {
        let stats = UploadQueueStats {
            queued_bytes: 1_048_576,
            total_bytes_transferred: 2_097_152,
            ..Default::default()
        };
        assert!((stats.queued_mb() - 1.0).abs() < 0.001);
        assert!((stats.transferred_mb() - 2.0).abs() < 0.001);
    }

    #[test]
    fn test_clear_queue() {
        let mut queue = UploadQueue::new();
        for _ in 0..5 {
            let target = make_target(4096);
            queue.enqueue(vec![0; 100], target, UploadPriority::Normal);
        }
        queue.clear();
        assert!(queue.is_empty());
        assert_eq!(queue.stats().cancelled_count, 5);
    }

    #[test]
    fn test_peek_next() {
        let mut queue = UploadQueue::new();
        assert!(queue.peek_next().is_none());
        let target = make_target(4096);
        let id = queue
            .enqueue(vec![0; 100], target, UploadPriority::Normal)
            .expect("operation should succeed in test");
        let peeked = queue.peek_next().expect("peek should return next item");
        assert_eq!(peeked.id, id);
    }

    #[test]
    fn test_upload_target_fits() {
        let target = UploadTarget::new(1, 10, 100);
        assert!(target.fits(90));
        assert!(target.fits(0));
        assert!(!target.fits(91));
    }
}