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 | 70845 | void* cpu_alloc(std::size_t bytes) { | |
| 17 |
1/2✓ Branch 0 taken 70845 times.
✗ Branch 1 not taken.
|
70845 | if (bytes == 0) return nullptr; |
| 18 | 70845 | void* p = std::malloc(bytes); | |
| 19 |
1/2✓ Branch 0 taken 70845 times.
✗ Branch 1 not taken.
|
70845 | if (!p) throw std::bad_alloc(); |
| 20 | 70845 | return p; | |
| 21 | 70845 | } | |
| 22 | |||
| 23 | 70845 | void cpu_free(void* ptr) { | |
| 24 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 70845 times.
|
70845 | if (ptr) std::free(ptr); |
| 25 | 70845 | } | |
| 26 | |||
| 27 | ✗ | void cpu_memcpy_h2d(void* dst, const void* src, std::size_t n) { | |
| 28 | ✗ | if (n) std::memcpy(dst, src, n); | |
| 29 | ✗ | } | |
| 30 | |||
| 31 | ✗ | void cpu_memcpy_d2h(void* dst, const void* src, std::size_t n) { | |
| 32 | ✗ | if (n) std::memcpy(dst, src, n); | |
| 33 | ✗ | } | |
| 34 | |||
| 35 | ✗ | void cpu_memcpy_d2d(void* dst, const void* src, std::size_t n) { | |
| 36 | ✗ | if (n) std::memcpy(dst, src, n); | |
| 37 | ✗ | } | |
| 38 | |||
| 39 | 9820 | void cpu_memset_zero(void* dst, std::size_t n) { | |
| 40 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9820 times.
|
9820 | if (n) std::memset(dst, 0, n); |
| 41 | 9820 | } | |
| 42 | |||
| 43 | 2450 | void cpu_sync() { | |
| 44 | // no-op — CPU ops are synchronous | ||
| 45 | 2450 | } | |
| 46 | |||
| 47 | 163 | const AllocVTable& cpu_alloc_table() { | |
| 48 | static const AllocVTable t = { | ||
| 49 | &cpu_alloc, | ||
| 50 | &cpu_free, | ||
| 51 | &cpu_memcpy_h2d, | ||
| 52 | &cpu_memcpy_d2h, | ||
| 53 | &cpu_memcpy_d2d, | ||
| 54 | &cpu_memset_zero, | ||
| 55 | &cpu_sync, | ||
| 56 | }; | ||
| 57 | 163 | return t; | |
| 58 | } | ||
| 59 | |||
| 60 | } // namespace brotensor::detail::cpu | ||
| 61 |