-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathmonotonic_buffer.cpp
89 lines (71 loc) · 2.62 KB
/
monotonic_buffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <catch2/catch.hpp>
#include <iostream>
#include "nvexec/detail/memory.cuh"
#include "tracer_resource.h"
namespace {
TEST_CASE("monotonic buffer releases storage", "[cuda][stream][memory][monotonic buffer]") {
tracer_resource resource{};
{
nvdetail::monotonic_buffer_resource buffer{1024, &resource};
void* ptr_1 = buffer.allocate(128, 8);
void* ptr_2 = buffer.allocate(256, 16);
REQUIRE(ptr_1 != nullptr);
REQUIRE(ptr_2 != nullptr);
REQUIRE(ptr_1 != ptr_2);
REQUIRE(1 == resource.allocations.size());
buffer.deallocate(ptr_1, 128, 8);
buffer.deallocate(ptr_2, 256, 16);
REQUIRE(1 == resource.allocations.size());
}
REQUIRE(0 == resource.allocations.size());
}
TEST_CASE(
"monotonic buffer keeps track of new allocations",
"[cuda][stream][memory][monotonic buffer]") {
tracer_resource resource{};
{
nvdetail::monotonic_buffer_resource buffer{1024, &resource};
void* ptr_1 = buffer.allocate(128, 8);
void* ptr_2 = buffer.allocate(256, 16);
void* ptr_3 = buffer.allocate(1024, 1);
void* ptr_4 = buffer.allocate(512, 2);
REQUIRE(ptr_1 != nullptr);
REQUIRE(ptr_2 != nullptr);
REQUIRE(ptr_3 != nullptr);
REQUIRE(ptr_4 != nullptr);
REQUIRE(ptr_1 != ptr_2);
REQUIRE(ptr_2 != ptr_3);
REQUIRE(ptr_3 != ptr_4);
REQUIRE(2 == resource.allocations.size());
buffer.deallocate(ptr_1, 128, 8);
buffer.deallocate(ptr_2, 256, 16);
buffer.deallocate(ptr_3, 1024, 1);
buffer.deallocate(ptr_4, 512, 2);
REQUIRE(2 == resource.allocations.size());
}
REQUIRE(0 == resource.allocations.size());
}
TEST_CASE(
"monotonic buffer provides required allocations",
"[cuda][stream][memory][monotonic buffer]") {
tracer_resource resource{};
nvdetail::monotonic_buffer_resource buffer{1024, &resource};
char* ptr_1 = reinterpret_cast<char*>(buffer.allocate(32, 8));
char* ptr_2 = reinterpret_cast<char*>(buffer.allocate(32, 8));
size_t distance = std::distance(ptr_1, ptr_2);
REQUIRE(distance == 32);
buffer.deallocate(ptr_1, 32, 8);
buffer.deallocate(ptr_2, 32, 16);
}
TEST_CASE(
"monotonic buffer provides required alignment",
"[cuda][stream][memory][monotonic buffer]") {
tracer_resource resource{};
nvdetail::monotonic_buffer_resource buffer{2048, &resource};
for (int alignment = 1; alignment < 512; alignment *= 2) {
void* ptr = buffer.allocate(32, alignment);
REQUIRE(reinterpret_cast<uintptr_t>(ptr) % alignment == 0);
buffer.deallocate(ptr, 32, alignment);
}
}
} // namespace