Allocate a pinned CPU buffer whose first usable byte is page-aligned, and return a torch view of ``size`` bytes plus its base pointer. Internally over-allocates one extra page on a backing tensor, then slices the aligned region out. The slice shares storage with the backing ten
(size: int)
| 378 | |
| 379 | |
| 380 | def _alloc_page_aligned_pinned_view(size: int) -> Tuple[torch.Tensor, int]: |
| 381 | """ |
| 382 | Allocate a pinned CPU buffer whose first usable byte is page-aligned, |
| 383 | and return a torch view of ``size`` bytes plus its base pointer. |
| 384 | |
| 385 | Internally over-allocates one extra page on a backing tensor, then |
| 386 | slices the aligned region out. The slice shares storage with the |
| 387 | backing tensor, so keeping the slice alive keeps the underlying |
| 388 | memory alive (no need to track the backing tensor separately). |
| 389 | """ |
| 390 | backing = torch.empty(size + _PAGE_SIZE, dtype=torch.uint8, pin_memory=False) |
| 391 | # First-touch initialization on the entire backing region |
| 392 | backing.fill_(0) |
| 393 | base = backing.data_ptr() |
| 394 | # Distance from `base` to the next page boundary (0..PAGE_SIZE-1). |
| 395 | offset = (-base) % _PAGE_SIZE |
| 396 | aligned_view = backing[offset : offset + size] |
| 397 | return aligned_view, aligned_view.data_ptr() |
| 398 | |
| 399 | |
| 400 | def alloc_pinned_numa_ptr(size: int, numa_id: int = 0) -> int: |
no test coverage detected