src/cpu/alloc.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU AllocVTable ─────────────────────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // Plain host memory. malloc/free + memcpy/memset, sync is a no-op. All | ||
| 4 | // "device" pointers for the CPU backend are simply host pointers; the | ||
| 5 | // d2h / h2d / d2d hooks all collapse to std::memcpy so cross-device copies | ||
| 6 | // involving CPU work without any special casing in Tensor::to(). | ||
| 7 | |||
| 8 | #include <brotensor/detail/dispatch.h> | ||
| 9 | |||
| 10 | #include <cstdlib> | ||
| 11 | #include <cstring> | ||
| 12 | #include <new> | ||
| 13 | |||
| 14 | namespace brotensor::detail::cpu { | ||
| 15 | |||
| 16 | 71892 | void* cpu_alloc(std::size_t bytes, int) { | |
| 17 |
1/2✓ Branch 0 taken 71892 times.
✗ Branch 1 not taken.
|
71892 | if (bytes == 0) return nullptr; |
| 18 | 71892 | void* p = std::malloc(bytes); | |
| 19 |
1/2✓ Branch 0 taken 71892 times.
✗ Branch 1 not taken.
|
71892 | if (!p) throw std::bad_alloc(); |
| 20 | 71892 | return p; | |
| 21 | 71892 | } | |
| 22 | |||
| 23 | 71892 | void cpu_free(void* ptr, int) { | |
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71892 times.
|
71892 | if (ptr) std::free(ptr); |
| 25 | 71892 | } | |
| 26 | |||
| 27 | ✗ | void cpu_memcpy_h2d(void* dst, const void* src, std::size_t n, int) { | |
| 28 | ✗ | if (n) std::memcpy(dst, src, n); | |
| 29 | ✗ | } | |
| 30 | |||
| 31 | ✗ | void cpu_memcpy_d2h(void* dst, const void* src, std::size_t n, int) { | |
| 32 | ✗ | if (n) std::memcpy(dst, src, n); | |
| 33 | ✗ | } | |
| 34 | |||
| 35 | ✗ | void cpu_memcpy_d2d(void* dst, const void* src, std::size_t n, int) { | |
| 36 | ✗ | if (n) std::memcpy(dst, src, n); | |
| 37 | ✗ | } | |
| 38 | |||
| 39 | ✗ | void cpu_memcpy_peer(void* dst, int, const void* src, int, std::size_t n) { | |
| 40 | ✗ | if (n) std::memcpy(dst, src, n); | |
| 41 | ✗ | } | |
| 42 | |||
| 43 | 10515 | void cpu_memset_zero(void* dst, std::size_t n, int) { | |
| 44 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10515 times.
|
10515 | if (n) std::memset(dst, 0, n); |
| 45 | 10515 | } | |
| 46 | |||
| 47 | 2914 | void cpu_sync(int) { | |
| 48 | // no-op — CPU ops are synchronous | ||
| 49 | 2914 | } | |
| 50 | |||
| 51 | 165 | const AllocVTable& cpu_alloc_table() { | |
| 52 | static const AllocVTable t = { | ||
| 53 | &cpu_alloc, | ||
| 54 | &cpu_free, | ||
| 55 | &cpu_memcpy_h2d, | ||
| 56 | &cpu_memcpy_d2h, | ||
| 57 | &cpu_memcpy_d2d, | ||
| 58 | &cpu_memcpy_peer, | ||
| 59 | &cpu_memset_zero, | ||
| 60 | &cpu_sync, | ||
| 61 | }; | ||
| 62 | 165 | return t; | |
| 63 | } | ||
| 64 | |||
| 65 | } // namespace brotensor::detail::cpu | ||
| 66 |