-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.cpp
81 lines (70 loc) · 1.54 KB
/
alloc.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
#include <cuda_runtime_api.h>
#include <stdio.h>
#include <fcntl.h>
#include <numa.h>
#include <stdlib.h>
#include <unistd.h>
#include <numaif.h>
#include <cassert>
#include <mutex>
size_t THRESHOLD = 1UL << 20;
size_t normal_max = 0;
size_t normal_cur = 0;
size_t n_normal = 0;
size_t numa_max = 0;
size_t numa_cur = 0;
size_t n_numa = 0;
void print_cur() {
printf("%lu,%lu,%lu,%lu\n", normal_cur, n_normal, numa_cur, n_numa);
fflush(stdout);
}
extern "C" {
void* my_malloc(ssize_t size, int device, cudaStream_t stream) {
void *ptr;
if (size < THRESHOLD) {
cudaMallocAsync(&ptr, size, stream);
#ifdef TRACK
normal_cur += size;
n_normal++;
if (normal_cur > normal_max) {
print_cur();
normal_max = normal_cur;
}
#endif
} else {
ptr = numa_alloc_onnode(size, NODE);
#ifdef TRACK
numa_cur += size;
n_numa++;
if (numa_cur > numa_max) {
print_cur();
numa_max = numa_cur;
}
#endif
}
return ptr;
}
void my_free(void* ptr, ssize_t size, int device, cudaStream_t stream) {
if (size < THRESHOLD) {
cudaFreeAsync(ptr, stream);
#ifdef TRACK
normal_cur -= size;
n_normal--;
#endif
} else {
numa_free(ptr, size);
#ifdef TRACK
numa_cur -= size;
n_numa--;
#endif
}
}
void* cuda_malloc(ssize_t size, int device, cudaStream_t stream) {
void *ptr;
cudaMallocAsync(&ptr, size, stream);
return ptr;
}
void cuda_free(void* ptr, ssize_t size, int device, cudaStream_t stream) {
cudaFreeAsync(ptr, stream);
}
}