-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathclient.cpp
More file actions
8986 lines (8284 loc) · 323 KB
/
Copy pathclient.cpp
File metadata and controls
8986 lines (8284 loc) · 323 KB
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <arpa/inet.h>
#include <atomic>
#include <cstdint>
#include <cstring>
#include <cuda.h>
#include <cudaProfiler.h>
#include <dlfcn.h>
#include <elf.h>
#include <fcntl.h>
#include <features.h>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <mutex>
#include <netdb.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <signal.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <strings.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <unordered_map>
#include <vector>
#if !defined(__GLIBC__)
#error "Lupine CUDA client requires glibc"
#endif
#define LUPINE_CUDA_COMPAT_TYPES_ONLY
#include "cuda_compat.h"
#undef LUPINE_CUDA_COMPAT_TYPES_ONLY
#include <cstdlib>
#include <cstring>
#include "codegen/gen_api.h"
#include "codegen/gen_client.h"
#include "lupine_attr_sizes.h"
#include "lupine_fatbin.h"
#include "lupine_log.h"
#include "rpc.h"
pthread_mutex_t conn_mutex;
conn_t conns[16];
int nconns = 0;
static bool lupine_rpc_shutting_down = false;
const char *DEFAULT_PORT = "14833";
std::map<void *, void *> host_funcs;
void add_host_node(void *fn, void *udata);
static constexpr uint32_t LUPINE_MODULE_IMAGE_FATBINC_V1 = 1;
static constexpr uint32_t LUPINE_MODULE_IMAGE_FATBIN_RAW = 2;
static constexpr uint32_t LUPINE_MODULE_IMAGE_FATBINC_V2 = 3;
static constexpr uint32_t LUPINE_PRIVATE_EXPORT_MAX_SLOTS = 256;
#ifdef CU_MEM_LOCATION_TYPE_HOST
static constexpr CUmemLocationType LUPINE_CU_MEM_LOCATION_TYPE_HOST =
CU_MEM_LOCATION_TYPE_HOST;
#else
static constexpr CUmemLocationType LUPINE_CU_MEM_LOCATION_TYPE_HOST =
static_cast<CUmemLocationType>(2);
#endif
struct lupine_kernel_param_layout {
uint32_t count = 0;
size_t offsets[64] = {};
size_t sizes[64] = {};
};
struct lupine_kernel_param_layout_key {
int route_id = -2;
CUfunction function = nullptr;
bool operator==(const lupine_kernel_param_layout_key &other) const {
return route_id == other.route_id && function == other.function;
}
};
struct lupine_kernel_param_layout_key_hash {
size_t operator()(const lupine_kernel_param_layout_key &key) const {
size_t hash = std::hash<int>{}(key.route_id);
hash ^= std::hash<uintptr_t>{}(reinterpret_cast<uintptr_t>(key.function)) +
0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
};
struct lupine_private_node_mapping {
CUfunction server_function = nullptr;
uint64_t server_owner = 0;
CUmodule module = nullptr;
std::unordered_map<int, CUfunction> functions_by_route;
};
struct lupine_library_image_record {
uint32_t kind = 0;
const void *code = nullptr;
std::vector<unsigned char> image;
std::unordered_map<int, CUlibrary> libraries_by_route;
};
struct lupine_module_image_record {
uint32_t kind = 0;
const void *image_ptr = nullptr;
std::vector<unsigned char> image;
std::unordered_map<int, CUmodule> modules_by_route;
};
struct lupine_library_kernel_record {
CUlibrary library = nullptr;
std::string name;
std::unordered_map<int, CUkernel> kernels_by_route;
};
struct lupine_module_function_record {
CUmodule module = nullptr;
std::string name;
std::unordered_map<int, CUfunction> functions_by_route;
};
struct lupine_graph_kernel_node_params_storage {
CUDA_KERNEL_NODE_PARAMS params = {};
lupine_kernel_param_layout layout = {};
std::vector<unsigned char> packed;
std::vector<void *> kernel_params;
};
extern int rpc_size();
extern int rpc_open();
extern conn_t *rpc_client_get_connection(unsigned int index);
extern void rpc_close(conn_t *conn);
static bool lupine_stub_missing_enabled() {
static bool enabled = [] {
const char *value = getenv("LUPINE_STUB_MISSING");
return value == nullptr || strcmp(value, "0") != 0;
}();
return enabled;
}
static bool lupine_symbol_looks_like_driver_api(const char *symbol) {
return symbol != nullptr && symbol[0] == 'c' && symbol[1] == 'u' &&
symbol[2] >= 'A' && symbol[2] <= 'Z';
}
using lupine_dlsym_fn = void *(*)(void *, const char *);
static const char *lupine_dlsym_glibc_version() {
#if defined(__x86_64__)
return "GLIBC_2.2.5";
#elif defined(__aarch64__)
return "GLIBC_2.17";
#else
return nullptr;
#endif
}
static void *lupine_real_dlsym(void *handle, const char *name) {
static lupine_dlsym_fn real_dlsym = nullptr;
static bool initialized = false;
if (!initialized) {
initialized = true;
const char *version = lupine_dlsym_glibc_version();
if (version != nullptr) {
real_dlsym = reinterpret_cast<lupine_dlsym_fn>(
dlvsym(RTLD_NEXT, "dlsym", version));
}
}
return real_dlsym != nullptr ? real_dlsym(handle, name) : nullptr;
}
extern "C" CUresult lupine_unsupported_driver_api() {
LUPINE_LOG_ERROR("LUPINE unsupported generic Driver API called");
return CUDA_ERROR_NOT_SUPPORTED;
}
extern "C" CUresult lupine_missing_driver_api_called(const char *symbol) {
LUPINE_LOG_ERROR("LUPINE missing Driver API called: " << symbol);
return CUDA_ERROR_NOT_SUPPORTED;
}
static std::unordered_map<std::string, std::vector<uint64_t>> &
lupine_private_export_hashes() {
static std::unordered_map<std::string, std::vector<uint64_t>> hashes;
return hashes;
}
static std::unordered_map<CUfunction, lupine_private_node_mapping> &
lupine_private_node_map() {
static std::unordered_map<CUfunction, lupine_private_node_mapping> mappings;
return mappings;
}
static std::unordered_map<CUfunction, CUfunction> &lupine_host_function_map() {
static std::unordered_map<CUfunction, CUfunction> mappings;
return mappings;
}
static std::unordered_map<lupine_kernel_param_layout_key,
lupine_kernel_param_layout,
lupine_kernel_param_layout_key_hash> &
lupine_kernel_param_layout_cache() {
static std::unordered_map<lupine_kernel_param_layout_key,
lupine_kernel_param_layout,
lupine_kernel_param_layout_key_hash>
cache;
return cache;
}
static std::mutex &lupine_kernel_param_layout_cache_mutex() {
static std::mutex mutex;
return mutex;
}
static std::vector<CUmodule> &lupine_loaded_modules() {
static std::vector<CUmodule> modules;
return modules;
}
static constexpr int LUPINE_ROUTE_REMOTE = 0;
static constexpr int LUPINE_ROUTE_LOCAL = 1;
static constexpr int LUPINE_ROUTE_INVALID = 2;
struct lupine_route {
int kind = LUPINE_ROUTE_INVALID;
conn_t *conn = nullptr;
};
struct lupine_owner {
bool local = false;
unsigned int conn_index = 0;
};
struct lupine_deviceptr_allocation_record {
CUdeviceptr base = 0;
size_t size = 0;
lupine_owner owner;
};
struct lupine_device_entry {
bool local = false;
int local_ordinal = -1;
CUdevice local_device = -1;
unsigned int conn_index = 0;
int remote_ordinal = 0;
CUdevice remote_device = 0;
};
struct lupine_primary_context_state {
bool valid = false;
unsigned int flags = 0;
int active = 0;
};
struct lupine_device_attribute_key {
int device = 0;
int attribute = 0;
bool operator==(const lupine_device_attribute_key &other) const {
return device == other.device && attribute == other.attribute;
}
};
struct lupine_kernel_function_key {
int route_id = -2;
CUcontext context = nullptr;
CUkernel kernel = nullptr;
bool operator==(const lupine_kernel_function_key &other) const {
return route_id == other.route_id && context == other.context &&
kernel == other.kernel;
}
};
struct lupine_occupancy_key {
int route_id = -2;
CUfunction function = nullptr;
int block_size = 0;
size_t dynamic_smem_size = 0;
unsigned int flags = 0;
bool with_flags = false;
bool operator==(const lupine_occupancy_key &other) const {
return route_id == other.route_id && function == other.function &&
block_size == other.block_size &&
dynamic_smem_size == other.dynamic_smem_size &&
flags == other.flags && with_flags == other.with_flags;
}
};
struct lupine_device_attribute_key_hash {
size_t operator()(const lupine_device_attribute_key &key) const {
return (static_cast<size_t>(static_cast<unsigned int>(key.device)) << 32) ^
static_cast<size_t>(static_cast<unsigned int>(key.attribute));
}
};
struct lupine_kernel_function_key_hash {
size_t operator()(const lupine_kernel_function_key &key) const {
size_t hash = std::hash<int>{}(key.route_id);
hash ^= std::hash<uintptr_t>{}(reinterpret_cast<uintptr_t>(key.context)) +
0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= std::hash<uintptr_t>{}(reinterpret_cast<uintptr_t>(key.kernel)) +
0x9e3779b9 + (hash << 6) + (hash >> 2);
return hash;
}
};
struct lupine_occupancy_key_hash {
size_t operator()(const lupine_occupancy_key &key) const {
size_t hash = std::hash<int>{}(key.route_id);
hash ^= std::hash<uintptr_t>{}(reinterpret_cast<uintptr_t>(key.function)) +
0x9e3779b9 + (hash << 6) + (hash >> 2);
hash ^= std::hash<int>{}(key.block_size) + 0x9e3779b9 + (hash << 6) +
(hash >> 2);
hash ^= std::hash<size_t>{}(key.dynamic_smem_size) + 0x9e3779b9 +
(hash << 6) + (hash >> 2);
hash ^= std::hash<unsigned int>{}(key.flags) + 0x9e3779b9 + (hash << 6) +
(hash >> 2);
hash ^= std::hash<bool>{}(key.with_flags) + 0x9e3779b9 + (hash << 6) +
(hash >> 2);
return hash;
}
};
static std::mutex &lupine_routing_mutex() {
static auto *mutex = new std::mutex();
return *mutex;
}
static std::vector<lupine_device_entry> &lupine_device_table() {
static auto *devices = new std::vector<lupine_device_entry>();
return *devices;
}
static bool &lupine_device_table_ready() {
static bool ready = false;
return ready;
}
static std::unordered_map<CUcontext, lupine_owner> &lupine_context_owners() {
static auto *owners = new std::unordered_map<CUcontext, lupine_owner>();
return *owners;
}
static std::unordered_map<CUmodule, lupine_owner> &lupine_module_owners() {
static auto *owners = new std::unordered_map<CUmodule, lupine_owner>();
return *owners;
}
static std::unordered_map<CUmodule, lupine_module_image_record> &
lupine_module_images() {
static auto *images =
new std::unordered_map<CUmodule, lupine_module_image_record>();
return *images;
}
static std::unordered_map<CUlibrary, lupine_owner> &lupine_library_owners() {
static auto *owners = new std::unordered_map<CUlibrary, lupine_owner>();
return *owners;
}
static std::unordered_map<CUlibrary, lupine_library_image_record> &
lupine_library_images() {
static auto *images =
new std::unordered_map<CUlibrary, lupine_library_image_record>();
return *images;
}
static std::unordered_map<CUkernel, lupine_library_kernel_record> &
lupine_library_kernels() {
static auto *kernels =
new std::unordered_map<CUkernel, lupine_library_kernel_record>();
return *kernels;
}
static std::unordered_map<CUfunction, lupine_module_function_record> &
lupine_module_functions() {
static auto *functions =
new std::unordered_map<CUfunction, lupine_module_function_record>();
return *functions;
}
static std::unordered_map<CUfunction, lupine_owner> &lupine_function_owners() {
static auto *owners = new std::unordered_map<CUfunction, lupine_owner>();
return *owners;
}
static std::unordered_map<CUstream, lupine_owner> &lupine_stream_owners() {
static auto *owners = new std::unordered_map<CUstream, lupine_owner>();
return *owners;
}
static std::unordered_map<CUevent, lupine_owner> &lupine_event_owners() {
static auto *owners = new std::unordered_map<CUevent, lupine_owner>();
return *owners;
}
static std::unordered_map<CUdeviceptr, lupine_owner> &
lupine_deviceptr_owners() {
static auto *owners = new std::unordered_map<CUdeviceptr, lupine_owner>();
return *owners;
}
static std::unordered_map<CUdeviceptr, lupine_deviceptr_allocation_record> &
lupine_deviceptr_allocations() {
static auto *allocations =
new std::unordered_map<CUdeviceptr, lupine_deviceptr_allocation_record>();
return *allocations;
}
static std::unordered_map<int, lupine_primary_context_state> &
lupine_primary_context_states() {
static auto *states =
new std::unordered_map<int, lupine_primary_context_state>();
return *states;
}
static std::unordered_map<lupine_device_attribute_key, int,
lupine_device_attribute_key_hash> &
lupine_device_attribute_cache() {
static auto *cache =
new std::unordered_map<lupine_device_attribute_key, int,
lupine_device_attribute_key_hash>();
return *cache;
}
static std::mutex &lupine_device_attribute_cache_mutex() {
static auto *mutex = new std::mutex();
return *mutex;
}
static std::unordered_map<lupine_kernel_function_key, CUfunction,
lupine_kernel_function_key_hash> &
lupine_kernel_function_cache() {
static auto *cache =
new std::unordered_map<lupine_kernel_function_key, CUfunction,
lupine_kernel_function_key_hash>();
return *cache;
}
static std::mutex &lupine_kernel_function_cache_mutex() {
static auto *mutex = new std::mutex();
return *mutex;
}
static std::unordered_map<lupine_occupancy_key, int,
lupine_occupancy_key_hash> &
lupine_occupancy_cache() {
static auto *cache = new std::unordered_map<lupine_occupancy_key, int,
lupine_occupancy_key_hash>();
return *cache;
}
static std::mutex &lupine_occupancy_cache_mutex() {
static auto *mutex = new std::mutex();
return *mutex;
}
static std::mutex &lupine_host_function_mutex() {
static auto *mutex = new std::mutex();
return *mutex;
}
static std::mutex &lupine_library_kernel_mutex() {
static auto *mutex = new std::mutex();
return *mutex;
}
static unsigned char (&lupine_private_6e16_node_pool())[16][0x500] {
static unsigned char nodes[16][0x500] = {};
return nodes;
}
static std::atomic<unsigned int> &lupine_private_6e16_next_node() {
static std::atomic<unsigned int> next{0};
return next;
}
static std::mutex &lupine_private_node_mutex() {
static std::mutex mutex;
return mutex;
}
static std::mutex &lupine_graph_kernel_node_params_mutex() {
static auto *mutex = new std::mutex();
return *mutex;
}
static std::unordered_map<CUgraphNode,
lupine_graph_kernel_node_params_storage> &
lupine_graph_kernel_node_params_cache() {
static auto *cache =
new std::unordered_map<CUgraphNode,
lupine_graph_kernel_node_params_storage>();
return *cache;
}
static void lupine_remember_loaded_module(CUmodule module) {
if (module == nullptr) {
return;
}
std::lock_guard<std::mutex> lock(lupine_host_function_mutex());
auto &modules = lupine_loaded_modules();
if (std::find(modules.begin(), modules.end(), module) == modules.end()) {
modules.push_back(module);
}
}
extern "C" void lupine_remember_loaded_module_for_rpc(CUmodule module) {
lupine_remember_loaded_module(module);
}
static int lupine_conn_index(conn_t *conn) {
if (conn == nullptr) {
return -1;
}
for (int i = 0; i < nconns; ++i) {
if (&conns[i] == conn) {
return i;
}
}
return -1;
}
static conn_t *lupine_conn_by_index(unsigned int index) {
if (rpc_open() < 0 || index >= static_cast<unsigned int>(nconns)) {
return nullptr;
}
return &conns[index];
}
static bool lupine_env_enabled(const char *name) {
const char *value = getenv(name);
return value != nullptr && strcmp(value, "0") != 0 &&
strcasecmp(value, "false") != 0 && strcasecmp(value, "no") != 0;
}
static void *lupine_local_libcuda_handle() {
static std::once_flag once;
static void *handle = nullptr;
std::call_once(once, []() {
if (lupine_env_enabled("LUPINE_DISABLE_LOCAL")) {
return;
}
const char *override_path = getenv("LUPINE_REAL_LIBCUDA");
const char *paths[] = {
override_path,
"/usr/lib/x86_64-linux-gnu/libcuda.so.1",
"/usr/lib/aarch64-linux-gnu/libcuda.so.1",
"/usr/lib64/libcuda.so.1",
"/usr/lib/wsl/lib/libcuda.so.1",
nullptr,
};
for (const char *path : paths) {
if (path == nullptr || path[0] == '\0') {
continue;
}
handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (handle != nullptr) {
return;
}
}
});
return handle;
}
extern "C" void *lupine_real_cuda_symbol(const char *name) {
void *handle = lupine_local_libcuda_handle();
if (handle == nullptr || name == nullptr) {
return nullptr;
}
return lupine_real_dlsym(handle, name);
}
template <typename Fn> static Fn lupine_real_cuda_fn(const char *name) {
return reinterpret_cast<Fn>(lupine_real_cuda_symbol(name));
}
extern "C" bool lupine_route_is_local(lupine_route route) {
return route.kind == LUPINE_ROUTE_LOCAL;
}
extern "C" conn_t *lupine_route_remote_conn(lupine_route route) {
return route.kind == LUPINE_ROUTE_REMOTE ? route.conn : nullptr;
}
static lupine_route lupine_remote_route_for_conn(conn_t *conn) {
if (conn == nullptr) {
return lupine_route{LUPINE_ROUTE_INVALID, nullptr};
}
return lupine_route{LUPINE_ROUTE_REMOTE, conn};
}
static lupine_route lupine_local_route() {
return lupine_route{LUPINE_ROUTE_LOCAL, nullptr};
}
static lupine_route lupine_route_for_owner(const lupine_owner &owner) {
if (owner.local) {
return lupine_local_route();
}
return lupine_remote_route_for_conn(lupine_conn_by_index(owner.conn_index));
}
static int lupine_route_identity(lupine_route route) {
if (route.kind == LUPINE_ROUTE_LOCAL) {
return -1;
}
if (route.kind == LUPINE_ROUTE_REMOTE) {
return lupine_conn_index(route.conn);
}
return -2;
}
extern "C" bool lupine_routes_share_server(lupine_route first,
lupine_route second) {
return lupine_route_identity(first) == lupine_route_identity(second);
}
extern "C" lupine_route lupine_route_for_deviceptr(CUdeviceptr ptr);
extern "C" bool lupine_deviceptrs_share_route(CUdeviceptr first,
CUdeviceptr second) {
return lupine_routes_share_server(lupine_route_for_deviceptr(first),
lupine_route_for_deviceptr(second));
}
static lupine_route lupine_route_from_identity(int route_id) {
if (route_id == -1) {
return lupine_local_route();
}
if (route_id >= 0) {
return lupine_remote_route_for_conn(
lupine_conn_by_index(static_cast<unsigned int>(route_id)));
}
return lupine_route{LUPINE_ROUTE_INVALID, nullptr};
}
static int lupine_known_deviceptr_route_id(CUdeviceptr ptr) {
if (ptr == 0) {
return -2;
}
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
auto it = lupine_deviceptr_owners().find(ptr);
if (it != lupine_deviceptr_owners().end()) {
return it->second.local ? -1 : static_cast<int>(it->second.conn_index);
}
for (const auto &entry : lupine_deviceptr_allocations()) {
const auto &allocation = entry.second;
if (allocation.base == 0 || allocation.size == 0 || ptr < allocation.base) {
continue;
}
uint64_t offset = static_cast<uint64_t>(ptr - allocation.base);
if (offset < allocation.size) {
return allocation.owner.local
? -1
: static_cast<int>(allocation.owner.conn_index);
}
}
return -2;
}
extern "C" lupine_route lupine_route_for_default();
static bool lupine_is_local_address(const void *ptr);
static CUresult lupine_remote_cuInit(conn_t *conn, unsigned int flags) {
CUresult result = CUDA_ERROR_DEVICE_UNAVAILABLE;
if (conn == nullptr || rpc_write_start_request(conn, RPC_cuInit) < 0 ||
rpc_write(conn, &flags, sizeof(flags)) < 0 ||
rpc_wait_for_response(conn) < 0 ||
rpc_read(conn, &result, sizeof(result)) < 0 || rpc_read_end(conn) < 0) {
return CUDA_ERROR_DEVICE_UNAVAILABLE;
}
return result;
}
static CUresult lupine_remote_cuDeviceGetCount(conn_t *conn, int *count) {
CUresult result = CUDA_ERROR_DEVICE_UNAVAILABLE;
if (conn == nullptr || count == nullptr ||
rpc_write_start_request(conn, RPC_cuDeviceGetCount) < 0 ||
rpc_wait_for_response(conn) < 0 ||
rpc_read(conn, count, sizeof(*count)) < 0 ||
rpc_read(conn, &result, sizeof(result)) < 0 || rpc_read_end(conn) < 0) {
return CUDA_ERROR_DEVICE_UNAVAILABLE;
}
return result;
}
static CUresult lupine_remote_cuDeviceGet(conn_t *conn, CUdevice *device,
int ordinal) {
CUresult result = CUDA_ERROR_DEVICE_UNAVAILABLE;
if (conn == nullptr || device == nullptr ||
rpc_write_start_request(conn, RPC_cuDeviceGet) < 0 ||
rpc_write(conn, &ordinal, sizeof(ordinal)) < 0 ||
rpc_wait_for_response(conn) < 0 ||
rpc_read(conn, device, sizeof(*device)) < 0 ||
rpc_read(conn, &result, sizeof(result)) < 0 || rpc_read_end(conn) < 0) {
return CUDA_ERROR_DEVICE_UNAVAILABLE;
}
return result;
}
static CUresult lupine_ensure_device_table() {
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
if (lupine_device_table_ready()) {
return CUDA_SUCCESS;
}
auto &devices = lupine_device_table();
devices.clear();
using cuDeviceGetCount_fn = CUresult (*)(int *);
using cuDeviceGet_fn = CUresult (*)(CUdevice *, int);
auto local_count_fn =
lupine_real_cuda_fn<cuDeviceGetCount_fn>("cuDeviceGetCount");
auto local_get_fn = lupine_real_cuda_fn<cuDeviceGet_fn>("cuDeviceGet");
if (local_count_fn != nullptr && local_get_fn != nullptr) {
int local_count = 0;
if (local_count_fn(&local_count) == CUDA_SUCCESS && local_count > 0) {
for (int ordinal = 0; ordinal < local_count; ++ordinal) {
CUdevice local_device = 0;
if (local_get_fn(&local_device, ordinal) == CUDA_SUCCESS) {
lupine_device_entry entry;
entry.local = true;
entry.local_ordinal = ordinal;
entry.local_device = local_device;
devices.push_back(entry);
}
}
}
}
if (rpc_open() == 0) {
for (int i = 0; i < nconns; ++i) {
int remote_count = 0;
CUresult result =
lupine_remote_cuDeviceGetCount(&conns[i], &remote_count);
if (result != CUDA_SUCCESS) {
devices.clear();
return result;
}
for (int ordinal = 0; ordinal < remote_count; ++ordinal) {
CUdevice remote_device = 0;
result = lupine_remote_cuDeviceGet(&conns[i], &remote_device, ordinal);
if (result != CUDA_SUCCESS) {
devices.clear();
return result;
}
lupine_device_entry entry;
entry.local = false;
entry.conn_index = static_cast<unsigned int>(i);
entry.remote_ordinal = ordinal;
entry.remote_device = remote_device;
devices.push_back(entry);
}
}
}
if (devices.empty()) {
return CUDA_ERROR_DEVICE_UNAVAILABLE;
}
lupine_device_table_ready() = true;
return CUDA_SUCCESS;
}
extern "C" CUresult lupine_cuInit_multi(unsigned int flags) {
CUresult first_error = CUDA_SUCCESS;
bool initialized_any = false;
using cuInit_fn = CUresult (*)(unsigned int);
auto local_init = lupine_real_cuda_fn<cuInit_fn>("cuInit");
if (local_init != nullptr) {
CUresult result = local_init(flags);
if (result != CUDA_SUCCESS && first_error == CUDA_SUCCESS) {
first_error = result;
} else if (result == CUDA_SUCCESS) {
initialized_any = true;
}
}
if (rpc_open() == 0) {
for (int i = 0; i < nconns; ++i) {
CUresult result = lupine_remote_cuInit(&conns[i], flags);
if (result != CUDA_SUCCESS && first_error == CUDA_SUCCESS) {
first_error = result;
} else if (result == CUDA_SUCCESS) {
initialized_any = true;
}
}
}
return initialized_any ? CUDA_SUCCESS : first_error;
}
extern "C" CUresult lupine_cuDeviceGetCount_multi(int *count) {
if (count == nullptr) {
return CUDA_ERROR_INVALID_VALUE;
}
CUresult result = lupine_ensure_device_table();
if (result != CUDA_SUCCESS) {
return result;
}
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
*count = static_cast<int>(lupine_device_table().size());
return CUDA_SUCCESS;
}
extern "C" CUresult lupine_cuDeviceGet_multi(CUdevice *device, int ordinal) {
if (device == nullptr) {
return CUDA_ERROR_INVALID_VALUE;
}
CUresult result = lupine_ensure_device_table();
if (result != CUDA_SUCCESS) {
return result;
}
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
if (ordinal < 0 ||
ordinal >= static_cast<int>(lupine_device_table().size())) {
return CUDA_ERROR_INVALID_DEVICE;
}
*device = static_cast<CUdevice>(ordinal);
return CUDA_SUCCESS;
}
extern "C" conn_t *lupine_rpc_conn_for_device(CUdevice *device) {
if (device == nullptr || lupine_ensure_device_table() != CUDA_SUCCESS) {
return nullptr;
}
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
int local = static_cast<int>(*device);
auto &devices = lupine_device_table();
if (local < 0 || local >= static_cast<int>(devices.size())) {
return nullptr;
}
const lupine_device_entry &mapped = devices[local];
if (mapped.local) {
*device = mapped.local_device;
return nullptr;
}
*device = mapped.remote_device;
return lupine_conn_by_index(mapped.conn_index);
}
extern "C" lupine_route lupine_route_for_device(CUdevice *device) {
if (device == nullptr || lupine_ensure_device_table() != CUDA_SUCCESS) {
return lupine_route{LUPINE_ROUTE_INVALID, nullptr};
}
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
int local = static_cast<int>(*device);
auto &devices = lupine_device_table();
if (local < 0 || local >= static_cast<int>(devices.size())) {
return lupine_route{LUPINE_ROUTE_INVALID, nullptr};
}
const lupine_device_entry &mapped = devices[local];
if (mapped.local) {
*device = mapped.local_device;
return lupine_local_route();
}
*device = mapped.remote_device;
return lupine_remote_route_for_conn(lupine_conn_by_index(mapped.conn_index));
}
extern "C" bool lupine_translate_device_for_conn(conn_t *conn,
CUdevice *device) {
if (conn == nullptr || device == nullptr ||
lupine_ensure_device_table() != CUDA_SUCCESS) {
return false;
}
int conn_index = lupine_conn_index(conn);
if (conn_index < 0) {
return false;
}
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
int local = static_cast<int>(*device);
auto &devices = lupine_device_table();
if (local < 0 || local >= static_cast<int>(devices.size())) {
return false;
}
const lupine_device_entry &mapped = devices[local];
if (mapped.local ||
mapped.conn_index != static_cast<unsigned int>(conn_index)) {
return false;
}
*device = mapped.remote_device;
return true;
}
extern "C" CUdevice lupine_local_device_for_remote(conn_t *conn,
CUdevice remote_device) {
if (conn == nullptr || lupine_ensure_device_table() != CUDA_SUCCESS) {
return -1;
}
int conn_index = lupine_conn_index(conn);
if (conn_index < 0) {
return -1;
}
std::lock_guard<std::mutex> lock(lupine_routing_mutex());
auto &devices = lupine_device_table();
for (size_t i = 0; i < devices.size(); ++i) {
if (devices[i].conn_index == static_cast<unsigned int>(conn_index) &&
!devices[i].local && devices[i].remote_device == remote_device) {
return static_cast<CUdevice>(i);
}
}
return -1;
}
extern "C" lupine_route lupine_route_for_current_context();
extern "C" lupine_route lupine_route_for_context(CUcontext ctx);
extern "C" CUresult lupine_cuDeviceCanAccessPeer_multi(int *canAccessPeer,
CUdevice dev,
CUdevice peerDev) {
if (canAccessPeer == nullptr) {
return CUDA_ERROR_INVALID_VALUE;
}
lupine_route route = lupine_route_for_device(&dev);
if (lupine_route_is_local(route)) {
CUdevice peer = peerDev;
lupine_route peer_route = lupine_route_for_device(&peer);
if (!lupine_route_is_local(peer_route)) {
*canAccessPeer = 0;
return CUDA_SUCCESS;
}
using real_fn_t = CUresult (*)(int *, CUdevice, CUdevice);
auto real = lupine_real_cuda_fn<real_fn_t>("cuDeviceCanAccessPeer");
return real == nullptr ? CUDA_ERROR_DEVICE_UNAVAILABLE
: real(canAccessPeer, dev, peer);
}
conn_t *conn = lupine_route_remote_conn(route);
if (conn == nullptr) {
return CUDA_ERROR_INVALID_DEVICE;
}
if (!lupine_translate_device_for_conn(conn, &peerDev)) {
*canAccessPeer = 0;
return CUDA_SUCCESS;
}
CUresult result = CUDA_ERROR_DEVICE_UNAVAILABLE;
if (rpc_write_start_request(conn, RPC_cuDeviceCanAccessPeer) < 0 ||
rpc_write(conn, canAccessPeer, sizeof(*canAccessPeer)) < 0 ||
rpc_write(conn, &dev, sizeof(dev)) < 0 ||
rpc_write(conn, &peerDev, sizeof(peerDev)) < 0 ||
rpc_wait_for_response(conn) < 0 ||
rpc_read(conn, canAccessPeer, sizeof(*canAccessPeer)) < 0 ||
rpc_read(conn, &result, sizeof(result)) < 0 || rpc_read_end(conn) < 0) {
return CUDA_ERROR_DEVICE_UNAVAILABLE;
}
return result;
}
extern "C" CUresult lupine_cuCtxEnablePeerAccess_multi(CUcontext peerContext,
unsigned int flags) {
lupine_route current_route = lupine_route_for_current_context();
lupine_route peer_route = lupine_route_for_context(peerContext);
if (lupine_route_identity(current_route) !=
lupine_route_identity(peer_route)) {
return CUDA_ERROR_PEER_ACCESS_UNSUPPORTED;
}
if (lupine_route_is_local(current_route)) {
using real_fn_t = CUresult (*)(CUcontext, unsigned int);
auto real = lupine_real_cuda_fn<real_fn_t>("cuCtxEnablePeerAccess");
return real == nullptr ? CUDA_ERROR_DEVICE_UNAVAILABLE
: real(peerContext, flags);
}
conn_t *conn = lupine_route_remote_conn(current_route);
CUresult result = CUDA_ERROR_DEVICE_UNAVAILABLE;
if (conn == nullptr ||
rpc_write_start_request(conn, RPC_cuCtxEnablePeerAccess) < 0 ||
rpc_write(conn, &peerContext, sizeof(peerContext)) < 0 ||
rpc_write(conn, &flags, sizeof(flags)) < 0 ||
rpc_wait_for_response(conn) < 0 ||
rpc_read(conn, &result, sizeof(result)) < 0 || rpc_read_end(conn) < 0) {
return CUDA_ERROR_DEVICE_UNAVAILABLE;
}
return result;
}
extern "C" CUresult lupine_cuCtxDisablePeerAccess_multi(CUcontext peerContext) {
lupine_route current_route = lupine_route_for_current_context();
lupine_route peer_route = lupine_route_for_context(peerContext);
if (lupine_route_identity(current_route) !=