brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
16 / 28
57.1%
Branches:
4 / 16
25.0%
Functions:
5 / 9
55.6%

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 84830 void* cpu_alloc(std::size_t bytes, int) {
17
1/2
✓ Branch 0 taken 84830 times.
✗ Branch 1 not taken.
84830 if (bytes == 0) return nullptr;
18 84830 void* p = std::malloc(bytes);
19
1/2
✓ Branch 0 taken 84830 times.
✗ Branch 1 not taken.
84830 if (!p) throw std::bad_alloc();
20 84830 return p;
21 84830 }
22
23 84830 void cpu_free(void* ptr, int) {
24
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 84830 times.
84830 if (ptr) std::free(ptr);
25 84830 }
26
27 ✗ void cpu_memcpy_h2d(void* dst, const void* src, std::size_t n, int) {
28
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (n) std::memcpy(dst, src, n);
41 ✗ }
42
43 16211 void cpu_memset_zero(void* dst, std::size_t n, int) {
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16211 times.
16211 if (n) std::memset(dst, 0, n);
45 16211 }
46
47 4853 void cpu_sync(int) {
48 // no-op — CPU ops are synchronous
49 4853 }
50
51 190 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 190 return t;
63 }
64
65 } // namespace brotensor::detail::cpu
66