tpt-gpu-runtime 0.1.0

TPT Runtime - Core library
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
//! GPU Memory Allocators: Slab, Buddy, and Fallback strategies.
use crate::error::{ErrorCode, TptrError, TptrResult};
use crate::memory::types::{Alignment, MemAccess, MemType, MemoryAllocation, MemoryRegion};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};

#[derive(Debug, Clone, Default)]
pub struct AllocatorStats {
    pub total_allocations: u64,
    pub total_frees: u64,
    pub bytes_allocated: u64,
    pub bytes_freed: u64,
    pub current_usage: u64,
    pub peak_usage: u64,
    pub allocation_failures: u64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllocationStrategy {
    Slab,
    Buddy,
    Fallback,
}

pub trait GpuAllocator: Send + Sync {
    fn allocate(
        &mut self,
        size: u64,
        region: MemoryRegion,
        mem_type: MemType,
        access: MemAccess,
    ) -> TptrResult<MemoryAllocation>;
    fn free(&mut self, allocation: &MemoryAllocation) -> TptrResult<()>;
    /// Release the bookkeeping for an allocation by its handle alone, without
    /// re-matching the device pointer (used when the real device pointer has
    /// been substituted for the allocator's fake address).
    fn free_handle(&mut self, handle: u64) -> TptrResult<()>;
    fn stats(&self) -> AllocatorStats;
    fn reset(&mut self) -> TptrResult<()>;
}

// Slab Allocator
#[derive(Debug)]
struct Slab {
    base_addr: u64,
    free_list: Vec<u64>,
    block_size: u64,
}

#[derive(Debug)]
pub struct SlabAllocator {
    slab_size: u64,
    slabs: Vec<Slab>,
    next_handle: AtomicU64,
    stats: AllocatorStats,
}

impl SlabAllocator {
    pub fn new(base_addr: u64, total_size: u64, block_size: u64) -> Self {
        let bs = block_size.max(256);
        let num = total_size / bs;
        let free: Vec<u64> = (0..num).map(|i| base_addr + i * bs).collect();
        Self {
            slab_size: total_size,
            slabs: vec![Slab {
                base_addr,
                free_list: free,
                block_size: bs,
            }],
            next_handle: AtomicU64::new(1),
            stats: AllocatorStats::default(),
        }
    }
}

impl GpuAllocator for SlabAllocator {
    fn allocate(
        &mut self,
        size: u64,
        region: MemoryRegion,
        mem_type: MemType,
        access: MemAccess,
    ) -> TptrResult<MemoryAllocation> {
        let bs = self.slabs[0].block_size;
        let needed = size.div_ceil(bs);
        for slab in &mut self.slabs {
            if slab.free_list.len() >= needed as usize {
                let dp = slab.free_list.pop().unwrap_or(0);
                let h = self.next_handle.fetch_add(1, Ordering::SeqCst);
                let asize = needed * bs;
                self.stats.total_allocations += 1;
                self.stats.bytes_allocated += asize;
                self.stats.current_usage += asize;
                self.stats.peak_usage = self.stats.peak_usage.max(self.stats.current_usage);
                return Ok(MemoryAllocation::new(
                    h,
                    asize,
                    region,
                    mem_type,
                    access,
                    dp,
                    Alignment::DEFAULT,
                ));
            }
        }
        self.stats.allocation_failures += 1;
        Err(TptrError::new(
            ErrorCode::OutOfMemory,
            format!("SlabAllocator: no free block for size {}", size),
        ))
    }
    fn free(&mut self, allocation: &MemoryAllocation) -> TptrResult<()> {
        allocation.mark_freed();
        self.slabs[0].free_list.push(allocation.device_ptr());
        self.stats.bytes_freed += allocation.size();
        self.stats.current_usage = self.stats.current_usage.saturating_sub(allocation.size());
        self.stats.total_frees += 1;
        Ok(())
    }
    fn free_handle(&mut self, _handle: u64) -> TptrResult<()> {
        Ok(())
    }
    fn stats(&self) -> AllocatorStats {
        self.stats.clone()
    }
    fn reset(&mut self) -> TptrResult<()> {
        let bs = self.slabs[0].block_size;
        let num = self.slab_size / bs;
        self.slabs[0].free_list = (0..num).map(|i| self.slabs[0].base_addr + i * bs).collect();
        self.stats = AllocatorStats::default();
        Ok(())
    }
}

// Buddy Allocator
#[derive(Debug, Clone)]
struct BuddyBlock {
    addr: u64,
    order: usize,
}

#[derive(Debug)]
pub struct BuddyAllocator {
    base_addr: u64,
    min_block: u64,
    free_lists: Vec<Vec<u64>>,
    allocations: HashMap<u64, BuddyBlock>,
    next_handle: AtomicU64,
    stats: AllocatorStats,
}

impl BuddyAllocator {
    pub fn new(base_addr: u64, total_size: u64, min_block: u64) -> Self {
        let mb = min_block.next_power_of_two().max(256);
        let ts = total_size.next_power_of_two().max(mb);
        let mo = (ts / mb).trailing_zeros() as usize;
        let mut fl = vec![Vec::new(); mo + 1];
        fl[mo].push(base_addr);
        Self {
            base_addr,
            min_block: mb,
            free_lists: fl,
            allocations: HashMap::new(),
            next_handle: AtomicU64::new(1),
            stats: AllocatorStats::default(),
        }
    }
    fn order_for(&self, size: u64) -> usize {
        let a = size.next_power_of_two().max(self.min_block);
        ((a / self.min_block) as f64).log2().ceil() as usize
    }
    fn block_size(&self, order: usize) -> u64 {
        self.min_block * (1 << order)
    }
    fn buddy(&self, addr: u64, size: u64) -> u64 {
        addr ^ size
    }
}

impl GpuAllocator for BuddyAllocator {
    fn allocate(
        &mut self,
        size: u64,
        region: MemoryRegion,
        mem_type: MemType,
        access: MemAccess,
    ) -> TptrResult<MemoryAllocation> {
        let order = self.order_for(size);
        let asize = self.block_size(order);
        let mut found = false;
        let mut addr = 0u64;
        let mut ao = order;
        for o in order..self.free_lists.len() {
            if let Some(b) = self.free_lists[o].pop() {
                addr = b;
                ao = o;
                found = true;
                break;
            }
        }
        if !found {
            self.stats.allocation_failures += 1;
            return Err(TptrError::new(
                ErrorCode::OutOfMemory,
                format!("BuddyAllocator: no free block for size {}", size),
            ));
        }
        while ao > order {
            ao -= 1;
            let half = self.block_size(ao);
            self.free_lists[ao].push(addr + half);
        }
        let h = self.next_handle.fetch_add(1, Ordering::SeqCst);
        self.allocations.insert(addr, BuddyBlock { addr, order });
        self.stats.total_allocations += 1;
        self.stats.bytes_allocated += asize;
        self.stats.current_usage += asize;
        self.stats.peak_usage = self.stats.peak_usage.max(self.stats.current_usage);
        Ok(MemoryAllocation::new(
            h,
            asize,
            region,
            mem_type,
            access,
            addr,
            Alignment::DEFAULT,
        ))
    }
    fn free(&mut self, allocation: &MemoryAllocation) -> TptrResult<()> {
        allocation.mark_freed();
        let addr = allocation.device_ptr();
        let block = self.allocations.remove(&addr).ok_or_else(|| {
            TptrError::new(
                ErrorCode::InvalidAddress,
                format!("double free at {:x}", addr),
            )
        })?;
        let mut fa = block.addr;
        let mut fo = block.order;
        while fo < self.free_lists.len() - 1 {
            let b = self.buddy(fa, self.block_size(fo));
            if let Some(pos) = self.free_lists[fo].iter().position(|&x| x == b) {
                self.free_lists[fo].swap_remove(pos);
                fa = fa.min(b);
                fo += 1;
            } else {
                break;
            }
        }
        self.free_lists[fo].push(fa);
        self.stats.bytes_freed += allocation.size();
        self.stats.current_usage = self.stats.current_usage.saturating_sub(allocation.size());
        self.stats.total_frees += 1;
        Ok(())
    }
    fn free_handle(&mut self, _handle: u64) -> TptrResult<()> {
        Ok(())
    }
    fn stats(&self) -> AllocatorStats {
        self.stats.clone()
    }
    fn reset(&mut self) -> TptrResult<()> {
        let mo = self.free_lists.len() - 1;
        for list in &mut self.free_lists {
            list.clear();
        }
        self.free_lists[mo].push(self.base_addr);
        self.allocations.clear();
        self.stats = AllocatorStats::default();
        Ok(())
    }
}

// Fallback Allocator
#[derive(Debug)]
pub struct FallbackAllocator {
    base_addr: u64,
    total_size: u64,
    next_free: u64,
    next_handle: AtomicU64,
    stats: AllocatorStats,
}

impl FallbackAllocator {
    pub fn new(base_addr: u64, total_size: u64) -> Self {
        Self {
            base_addr,
            total_size,
            next_free: base_addr,
            next_handle: AtomicU64::new(1),
            stats: AllocatorStats::default(),
        }
    }
}

impl GpuAllocator for FallbackAllocator {
    fn allocate(
        &mut self,
        size: u64,
        region: MemoryRegion,
        mem_type: MemType,
        access: MemAccess,
    ) -> TptrResult<MemoryAllocation> {
        let aligned = Alignment(256).align_up(size);
        let end = self
            .next_free
            .checked_add(aligned)
            .ok_or_else(|| TptrError::new(ErrorCode::OutOfMemory, "overflow"))?;
        if end > self.base_addr + self.total_size {
            self.stats.allocation_failures += 1;
            return Err(TptrError::new(ErrorCode::OutOfMemory, "OOM"));
        }
        let addr = self.next_free;
        self.next_free = end;
        let h = self.next_handle.fetch_add(1, Ordering::SeqCst);
        self.stats.total_allocations += 1;
        self.stats.bytes_allocated += aligned;
        self.stats.current_usage += aligned;
        self.stats.peak_usage = self.stats.peak_usage.max(self.stats.current_usage);
        Ok(MemoryAllocation::new(
            h,
            aligned,
            region,
            mem_type,
            access,
            addr,
            Alignment::DEFAULT,
        ))
    }
    fn free(&mut self, allocation: &MemoryAllocation) -> TptrResult<()> {
        allocation.mark_freed();
        self.stats.bytes_freed += allocation.size();
        self.stats.current_usage = self.stats.current_usage.saturating_sub(allocation.size());
        self.stats.total_frees += 1;
        Ok(())
    }
    fn free_handle(&mut self, _handle: u64) -> TptrResult<()> {
        Ok(())
    }
    fn stats(&self) -> AllocatorStats {
        self.stats.clone()
    }
    fn reset(&mut self) -> TptrResult<()> {
        self.next_free = self.base_addr;
        self.stats = AllocatorStats::default();
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_slab() {
        let mut a = SlabAllocator::new(0x1000_0000, 1 << 20, 4096);
        let m = a
            .allocate(
                1024,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        assert!(m.device_ptr() >= 0x1000_0000);
        a.free(&m).unwrap();
        assert!(m.is_freed());
    }
    #[test]
    fn test_buddy() {
        let mut a = BuddyAllocator::new(0x2000_0000, 1 << 24, 4096);
        let m1 = a
            .allocate(
                4096,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        assert_eq!(m1.device_ptr(), 0x2000_0000);
        let m2 = a
            .allocate(
                4096,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        assert_eq!(m2.device_ptr(), 0x2000_1000);
        a.free(&m1).unwrap();
        a.free(&m2).unwrap();
        let m3 = a
            .allocate(
                8192,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        assert_eq!(m3.device_ptr(), 0x2000_0000);
    }
    #[test]
    fn test_fallback() {
        let mut a = FallbackAllocator::new(0x3000_0000, 1 << 20);
        let m = a
            .allocate(
                65536,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        assert_eq!(m.device_ptr(), 0x3000_0000);
    }
    #[test]
    fn test_oom() {
        let mut a = FallbackAllocator::new(0x4000_0000, 4096);
        let _ = a
            .allocate(
                4096,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        assert!(a
            .allocate(
                1,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite
            )
            .is_err());
    }
    #[test]
    fn test_reset() {
        let mut a = BuddyAllocator::new(0x5000_0000, 1 << 20, 4096);
        let m = a
            .allocate(
                4096,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        a.free(&m).unwrap();
        a.reset().unwrap();
        let m2 = a
            .allocate(
                4096,
                MemoryRegion::Global,
                MemType::Device,
                MemAccess::ReadWrite,
            )
            .unwrap();
        assert_eq!(m2.device_ptr(), 0x5000_0000);
    }
}