-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathclient.cpp
448 lines (365 loc) · 11.7 KB
/
client.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <arpa/inet.h>
#include <cstring>
#include <cuda.h>
#include <cuda_runtime.h>
#include <dlfcn.h>
#include <functional>
#include <iostream>
#include <map>
#include <netdb.h>
#include <netinet/tcp.h>
#include <nvml.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <vector>
#include <csignal>
#include <cstdlib>
#include <cstring>
#include <setjmp.h>
#include <signal.h>
#include <sys/mman.h>
#include "codegen/gen_client.h"
#include "rpc.h"
pthread_mutex_t conn_mutex;
conn_t conns[16];
int nconns = 0;
const char *DEFAULT_PORT = "14833";
static int init = 0;
static jmp_buf catch_segfault;
static void *faulting_address = nullptr;
std::map<conn_t *, std::map<void *, size_t>> unified_devices;
std::map<void *, void *> host_funcs;
static void segfault(int sig, siginfo_t *info, void *unused) {
faulting_address = info->si_addr;
for (const auto &conn_entry : unified_devices) {
for (const auto &[ptr, sz] : conn_entry.second) {
if ((uintptr_t)ptr <= (uintptr_t)faulting_address &&
(uintptr_t)faulting_address < ((uintptr_t)ptr + sz)) {
// ensure we assign memory as close to the faulting address as
// possible... by masking via the allocated unified memory size.
uintptr_t aligned = (uintptr_t)faulting_address & ~(sz - 1);
// Allocate memory at the faulting address
void *allocated =
mmap((void *)aligned, sz + (uintptr_t)faulting_address - aligned,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (allocated == MAP_FAILED) {
perror("Failed to allocate memory at faulting address");
_exit(1);
}
return;
}
}
}
// raise our original segfault handler
struct sigaction sa;
sa.sa_handler = SIG_DFL;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGSEGV, &sa, nullptr) == -1) {
perror("Failed to reset SIGSEGV handler");
_exit(EXIT_FAILURE);
}
raise(SIGSEGV);
}
int is_unified_pointer(conn_t *conn, void *arg) {
auto conn_it = unified_devices.find(conn);
if (conn_it == unified_devices.end()) {
return 0;
}
// now check if the argument exists in the connection's mapped devices
auto &devices = conn_it->second;
if (devices.find(arg) != devices.end()) {
return 1;
}
return 0;
}
int maybe_copy_unified_arg(conn_t *conn, void *arg, enum cudaMemcpyKind kind) {
// find the connection in the map first
auto conn_it = unified_devices.find(conn);
if (conn_it == unified_devices.end()) {
return 0;
}
// now find the argument in the sub-map for this connection
auto &devices = conn_it->second;
auto device_it = devices.find(arg);
if (device_it != devices.end()) {
std::cout << "Found unified arg pointer; copying..." << std::endl;
void *ptr = device_it->first;
size_t size = device_it->second;
cudaError_t res = cudaMemcpy(ptr, ptr, size, kind);
if (res != cudaSuccess) {
std::cerr << "cudaMemcpy failed: " << cudaGetErrorString(res)
<< std::endl;
return -1;
} else {
std::cout << "Successfully copied " << size << " bytes" << std::endl;
}
}
return 0;
}
static void set_segfault_handlers() {
if (init > 0) {
return;
}
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = segfault;
if (sigaction(SIGSEGV, &sa, NULL) == -1) {
perror("sigaction");
exit(EXIT_FAILURE);
}
init = 1;
}
void rpc_close(conn_t *conn) {
if (pthread_mutex_lock(&conn_mutex) < 0)
return;
while (--nconns >= 0)
close(conn->connfd);
pthread_mutex_unlock(&conn_mutex);
}
typedef void (*func_t)(void *);
void add_host_node(void *fn, void *udata) { host_funcs[fn] = udata; }
void invoke_host_func(void *fn) {
for (const auto &pair : host_funcs) {
if (pair.first == fn) {
func_t func = reinterpret_cast<func_t>(pair.first);
std::cout << "Invoking function at: " << pair.first << std::endl;
func(pair.second);
return;
}
}
}
void *rpc_client_dispatch_thread(void *arg) {
conn_t *conn = (conn_t *)arg;
int op;
while (true) {
op = rpc_dispatch(conn, 1);
if (op == 1) {
std::cout << "Transferring memory..." << std::endl;
int found = 0;
rpc_read(conn, &found, sizeof(int));
if (found > 0) {
void *host_data = nullptr;
void *dst = nullptr;
const void *src = nullptr;
size_t count = 0;
cudaError_t result;
int request_id;
if (rpc_read(conn, &dst, sizeof(void *)) < 0 ||
rpc_read(conn, &count, sizeof(size_t)) < 0) {
std::cerr << "Failed to read transfer parameters." << std::endl;
break;
}
host_data = malloc(count);
if (!host_data) {
std::cerr << "Memory allocation failed." << std::endl;
break;
}
// Read the actual data from the server (sent from `src` in device
// memory)
if (rpc_read(conn, host_data, count) < 0) {
std::cerr << "Failed to read device data from server." << std::endl;
free(host_data);
break;
}
// Copy received data to the destination (dst) on the host
memcpy(dst, host_data, count);
}
void *temp_mem;
if (rpc_read(conn, &temp_mem, sizeof(void *)) <= 0) {
std::cerr << "rpc_read failed for mem. Closing connection."
<< std::endl;
break;
}
int request_id = rpc_read_end(conn);
void *mem = temp_mem;
if (mem == nullptr) {
std::cerr << "Invalid function pointer!" << std::endl;
continue;
}
invoke_host_func(mem);
void *res = nullptr;
if (rpc_write_start_response(conn, request_id) < 0 ||
rpc_write(conn, &res, sizeof(void *)) < 0 ||
rpc_write_end(conn) < 0) {
std::cerr << "rpc_write failed. Closing connection." << std::endl;
break;
}
}
}
std::cerr << "Exiting dispatch thread due to an error." << std::endl;
return nullptr;
}
int rpc_open() {
set_segfault_handlers();
sigsetjmp(catch_segfault, 1);
if (pthread_mutex_lock(&conn_mutex) < 0)
return -1;
if (nconns > 0) {
if (pthread_mutex_unlock(&conn_mutex) < 0)
return -1;
return 0;
}
std::cout << "Opening connection to server" << std::endl;
char *server_ips = getenv("SCUDA_SERVER");
if (server_ips == NULL) {
printf("SCUDA_SERVER environment variable not set\n");
std::exit(1);
}
char *server_ip = strdup(server_ips);
char *token;
while ((token = strsep(&server_ip, ","))) {
char *host;
char *port;
// Split the string into IP address and port
char *colon = strchr(token, ':');
if (colon == NULL) {
host = token;
port = const_cast<char *>(DEFAULT_PORT);
} else {
*colon = '\0';
host = token;
port = colon + 1;
}
addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, port, &hints, &res) != 0) {
std::cout << "getaddrinfo of " << host << " port " << port << " failed"
<< std::endl;
continue;
}
int flag = 1;
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd == -1) {
printf("socket creation failed...\n");
exit(1);
}
int opts = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&flag,
sizeof(int));
if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
std::cerr << "Connecting to " << host << " port " << port
<< " failed: " << strerror(errno) << std::endl;
exit(1);
}
conns[nconns] = {sockfd, 0};
if (pthread_mutex_init(&conns[nconns].read_mutex, NULL) < 0 ||
pthread_mutex_init(&conns[nconns].write_mutex, NULL) < 0) {
return -1;
}
pthread_create(&conns[nconns].read_thread, NULL, rpc_client_dispatch_thread,
(void *)&conns[nconns]);
nconns++;
}
if (pthread_mutex_unlock(&conn_mutex) < 0)
return -1;
if (nconns == 0)
return -1;
return 0;
}
conn_t *rpc_client_get_connection(unsigned int index) {
if (rpc_open() < 0)
return nullptr;
return &conns[index];
}
int rpc_size() { return nconns; }
void allocate_unified_mem_pointer(conn_t *conn, void *dev_ptr, size_t size) {
// allocate new space for pointer mapping
unified_devices[conn][dev_ptr] = size;
}
cudaError_t cuda_memcpy_unified_ptrs(conn_t *conn, cudaMemcpyKind kind) {
auto conn_it = unified_devices.find(conn);
if (conn_it == unified_devices.end()) {
return cudaSuccess;
}
for (const auto &[ptr, sz] : conn_it->second) {
size_t size = reinterpret_cast<size_t>(sz);
// ptr is the same on both host/device
cudaError_t res = cudaMemcpy(ptr, ptr, size, kind);
if (res != cudaSuccess)
return res;
}
return cudaSuccess;
}
void maybe_free_unified_mem(conn_t *conn, void *ptr) {
auto conn_it = unified_devices.find(conn);
if (conn_it == unified_devices.end()) {
return;
}
for (const auto &[dev_ptr, sz] : conn_it->second) {
size_t size = reinterpret_cast<size_t>(sz);
if (dev_ptr == ptr) {
munmap(dev_ptr, size);
return;
}
}
}
CUresult cuGetProcAddress_v2(const char *symbol, void **pfn, int cudaVersion,
cuuint64_t flags,
CUdriverProcAddressQueryResult *symbolStatus) {
std::cout << "cuGetProcAddress getting symbol: " << symbol << std::endl;
auto it = get_function_pointer(symbol);
if (it != nullptr) {
*pfn = (void *)(&it);
std::cout << "cuGetProcAddress: Mapped symbol '" << symbol
<< "' to function: " << *pfn << std::endl;
return CUDA_SUCCESS;
}
if (strcmp(symbol, "cuGetProcAddress_v2") == 0 ||
strcmp(symbol, "cuGetProcAddress") == 0) {
*pfn = (void *)&cuGetProcAddress_v2;
return CUDA_SUCCESS;
}
std::cout << "cuGetProcAddress: Symbol '" << symbol
<< "' not found in cudaFunctionMap." << std::endl;
// fall back to dlsym
static void *(*real_dlsym)(void *, const char *) = NULL;
if (real_dlsym == NULL) {
real_dlsym = (void *(*)(void *, const char *))dlvsym(RTLD_NEXT, "dlsym",
"GLIBC_2.2.5");
}
void *libCudaHandle = dlopen("libcuda.so", RTLD_NOW | RTLD_GLOBAL);
if (!libCudaHandle) {
std::cerr << "Error: Failed to open libcuda.so" << std::endl;
return CUDA_ERROR_UNKNOWN;
}
*pfn = real_dlsym(libCudaHandle, symbol);
if (!(*pfn)) {
std::cerr << "Error: Could not resolve symbol '" << symbol
<< "' using dlsym." << std::endl;
return CUDA_ERROR_UNKNOWN;
}
return CUDA_SUCCESS;
}
void *dlsym(void *handle, const char *name) __THROW {
std::cout << "dlsym: " << name << std::endl;
void *func = get_function_pointer(name);
/** proc address function calls are basically dlsym; we should handle this
* differently at the top level. */
if (strcmp(name, "cuGetProcAddress_v2") == 0 ||
strcmp(name, "cuGetProcAddress") == 0) {
return (void *)&cuGetProcAddress_v2;
}
if (func != nullptr) {
// std::cout << "[dlsym] Function address from cudaFunctionMap: " << func
// << " " << name << std::endl;
return func;
}
// Real dlsym lookup
static void *(*real_dlsym)(void *, const char *) = NULL;
if (real_dlsym == NULL) {
real_dlsym = (void *(*)(void *, const char *))dlvsym(RTLD_NEXT, "dlsym",
"GLIBC_2.2.5");
}
// std::cout << "[dlsym] Falling back to real_dlsym for name: " << name <<
// std::endl;
return real_dlsym(handle, name);
}