forked from yzs981130/cuda-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cuda-wrapper-dev-v1.1.c
255 lines (242 loc) · 6.78 KB
/
cuda-wrapper-dev-v1.1.c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <cuda.h>
#include <string.h>
#include <pthread.h>
#include <cuda_runtime.h>
#include <sys/types.h>
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
#define SIZE 10000
unsigned long long mod = 9973L;
static const char LIB_STRING[] = "libcudart.so";
static const char LIB_STRING_STATIC[] = "libcuda.so";
static const char LIB_STRING_t[] = "/usr/local/cuda-9.0/targets/x86_64-linux/lib/libcudatr.so";
static const char CONFIG_STRING[] = "WRAPPER_MAX_MEMORY";
int open_flag = 0;
int static_open_flag = 0;
void *handle = NULL;
void *staticHandle = NULL;
static size_t total_mem = 0L;
static size_t total_quota = 421792896L;
static size_t pytorch_offset_size = 500000000L;
static pthread_mutex_t mem_cnt_lock;
char *error;
char timebuf[30];
struct HashArray {
unsigned long long key;
size_t value;
struct HashArray *next;
} allocsize[10000];
void addHash(unsigned long long key, size_t value) {
int temp = (key % mod);
if (allocsize[temp].key == 0) {
allocsize[temp].key = key;
allocsize[temp].value = value;
} else if (allocsize[temp].key == key) {
allocsize[temp].value = value;
} else {
struct HashArray *p = &allocsize[temp];
while (p->key != key && p->next != NULL) {
p = p->next;
}
if (p->key == key) {
p->value = value;
} else {
p->next = (struct HashArray *) malloc(sizeof(struct HashArray));
p = p->next;
p->key = key;
p->value = value;
p->next = NULL;
}
}
}
size_t getHash(unsigned long long key) {
int temp = key % mod;
struct HashArray *p = &allocsize[temp];
if (p == NULL) {
return 0;
}
while (p->key != key && p->next != NULL) {
p = p->next;
}
if (p->key == key) {
return p->value;
} else {
return 0;
}
}
void set_quota() {
char *q = NULL;
q = getenv(CONFIG_STRING);
if (q == NULL) {
} else {
total_quota = strtoull(q, NULL, 10);
}
}
void init_func() {
if (open_flag == 0 && handle == NULL) {
// pytorch_offset_size = 500000000L
//char *error;
handle = dlopen(LIB_STRING, RTLD_LAZY);
if (!handle) {
exit(1);
}
open_flag = 1;
dlerror();
pthread_mutex_init(&mem_cnt_lock, NULL);
set_quota();
}
}
int check_alloc_valid(size_t bytesize) {
pthread_mutex_lock(&mem_cnt_lock);
if (total_mem + bytesize + pytorch_offset_size > total_quota) {
pthread_mutex_unlock(&mem_cnt_lock);
return 0;
}
pthread_mutex_unlock(&mem_cnt_lock);
return 1;
}
/**
* pytorch runtime cuda api
*/
// runtime
cudaError_t cudaMalloc(void **devPtr, size_t bytesize) {
init_func();
cudaError_t(*fakecudaMalloc)(
void** , size_t );
fakecudaMalloc = dlsym(handle, "cudaMalloc");
if ((error = dlerror()) != NULL) {
exit(1);
}
if (check_alloc_valid(bytesize)) {
pthread_mutex_lock(&mem_cnt_lock);
total_mem += bytesize;
pthread_mutex_unlock(&mem_cnt_lock);
cudaError_t r = (*fakecudaMalloc)(devPtr, bytesize);
if (cudaSuccess != r) {
pthread_mutex_lock(&mem_cnt_lock);
total_mem -= bytesize;
pthread_mutex_unlock(&mem_cnt_lock);
} else {
unsigned long long p = (unsigned long long) (*devPtr);
addHash(p, bytesize);
}
return r;
} else {
return cudaErrorMemoryAllocation;
}
}
cudaError_t cudaFree(void *devPtr) {
init_func();
void *hand;
hand = dlopen(LIB_STRING_t, RTLD_LAZY);
if (!hand) {
exit(1);
}
dlerror();
cudaError_t(*fakecudaFree)(
void* );
fakecudaFree = dlsym(hand, "cudaFree");
if ((error = dlerror()) != NULL) {
exit(1);
}
cudaError_t r = (*fakecudaFree)(devPtr);
if (r == CUDA_SUCCESS) {
pthread_mutex_lock(&mem_cnt_lock);
size_t tbytesize = getHash((unsigned long long) (devPtr));
total_mem -= tbytesize;
pthread_mutex_unlock(&mem_cnt_lock);
}
dlclose(hand);
return r;
}
cudaError_t cudaMemGetInfo(size_t *free, size_t *total) {
init_func();
cudaError_t(*fakecudaMemGetInfo)(size_t * , size_t * );
fakecudaMemGetInfo = dlsym(handle, "cudaMemGetInfo");
if ((error = dlerror()) != NULL) {
exit(1);
}
cudaError_t r = (*fakecudaMemGetInfo)(free, total);
*free = total_quota - total_mem - pytorch_offset_size;
*total = total_quota;
return r;
}
/**
* tensorflow static api
*/
// static
void before_func(){
if (static_open_flag == 0 && staticHandle == NULL) {
// pytorch_offset_size = 0L
//char *error;
staticHandle = dlopen(LIB_STRING_STATIC, RTLD_LAZY);
if (!staticHandle) {
exit(1);
}
static_open_flag = 1;
dlerror();
}
pthread_mutex_init(&mem_cnt_lock, NULL);
set_quota();
}
CUresult cuMemGetInfo_v2(size_t *free, size_t *total) {
before_func();
CUresult(*fakecuMemGetInfo_v2)(size_t * , size_t * );
fakecuMemGetInfo_v2 = dlsym(staticHandle, "cuMemGetInfo_v2");
if ((error = dlerror()) != NULL) {
exit(1);
}
CUresult r = (*fakecuMemGetInfo_v2)(free, total);
// change free and total to proper value
if (*free > total_quota) {
*free = total_quota - total_mem;
}
*total = total_quota;
printf("cumemgetinfo: free : %zu, total : %zu\n", *free, *total);
return r;
}
CUresult cuMemAlloc_v2(CUdeviceptr *dptr, size_t bytesize) {
before_func();
CUresult(*fakecuMemAlloc_v2)(CUdeviceptr * , size_t);
fakecuMemAlloc_v2 = dlsym(staticHandle, "cuMemAlloc_v2");
if ((error = dlerror()) != NULL) {
exit(1);
}
if (check_alloc_valid(bytesize)) {
pthread_mutex_lock(&mem_cnt_lock);
total_mem += bytesize;
pthread_mutex_unlock(&mem_cnt_lock);
CUresult r = (*fakecuMemAlloc_v2)(dptr, bytesize);
if (CUDA_SUCCESS != r) {
pthread_mutex_lock(&mem_cnt_lock);
total_mem -= bytesize;
pthread_mutex_unlock(&mem_cnt_lock);
} else {
addHash((unsigned long long) dptr, bytesize);
}
return r;
} else {
return CUDA_ERROR_OUT_OF_MEMORY;
}
}
CUresult cuMemFree_v2(CUdeviceptr dptr) {
before_func();
CUresult(*fakecuMemFree_v2)(CUdeviceptr);
fakecuMemFree_v2 = dlsym(staticHandle, "cuMemFree_v2");
if ((error = dlerror()) != NULL) {
exit(1);
}
CUresult r = (*fakecuMemFree_v2)(dptr);
if (CUDA_SUCCESS == r) {
pthread_mutex_lock(&mem_cnt_lock);
size_t tbytesize = getHash(dptr);
total_mem -= tbytesize;
pthread_mutex_unlock(&mem_cnt_lock);
}
return r;
}