GCC Code Coverage Report


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

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