-
Notifications
You must be signed in to change notification settings - Fork 41
/
cpu-server-runtime.c
1933 lines (1714 loc) · 66.8 KB
/
cpu-server-runtime.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
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
#define _GNU_SOURCE
#include <cuda_runtime_api.h>
#include <cuda.h>
#include <driver_types.h>
#include <dlfcn.h>
#include <cuda_profiler_api.h>
//for strerror
#include <string.h>
#include <errno.h>
//For SHM
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "cpu_rpc_prot.h"
#include "cpu-common.h"
#include "cpu-utils.h"
#include "log.h"
#include "list.h"
#include "rpc/types.h"
#ifdef WITH_IB
#include <pthread.h>
#include "cpu-ib.h"
#endif //WITH_IB
#define WITH_RECORDER
#include "api-recorder.h"
#include "resource-mg.h"
#include "cpu-server-runtime.h"
#include "cr.h"
#include "cpu-server-cusolver.h"
#include "cpu-server-cublas.h"
#include "cpu-server-cublaslt.h"
#include "mt-memcpy.h"
typedef struct host_alloc_info {
size_t idx;
size_t size;
void *client_ptr;
void *server_ptr;
} host_alloc_info_t;
static host_alloc_info_t hainfo[64];
static size_t hainfo_cnt = 0;
list mt_memcpy_list = {0};
static int hainfo_getserverindex(void *server_ptr)
{
int i;
for (i=0; i < hainfo_cnt; ++i) {
if (hainfo[i].server_ptr != 0 &&
hainfo[i].server_ptr == server_ptr) {
return i;
}
}
return -1;
}
int server_runtime_init(int restore)
{
#ifdef WITH_IB
#endif //WITH_IB
int ret = 0;
if (list_init(&mt_memcpy_list, sizeof(mt_memcpy_server_t)) != 0) {
LOGE(LOG_ERROR, "mt_memcpy: failed to initialize list.");
ret &= 1;
}
if (!restore) {
ret &= resource_mg_init(&rm_streams, 1);
ret &= resource_mg_init(&rm_events, 1);
ret &= resource_mg_init(&rm_arrays, 1);
ret &= resource_mg_init(&rm_memory, 1);
ret &= cusolver_init(1, &rm_streams, &rm_memory);
ret &= cublas_init(1, &rm_memory);
ret &= cublaslt_init(1, &rm_memory);
} else {
ret &= resource_mg_init(&rm_streams, 0);
ret &= resource_mg_init(&rm_events, 0);
ret &= resource_mg_init(&rm_arrays, 0);
ret &= resource_mg_init(&rm_memory, 0);
ret &= resource_mg_init(&rm_kernels, 0);
ret &= cusolver_init(0, &rm_streams, &rm_memory);
ret &= cublas_init(0, &rm_memory);
ret &= server_runtime_restore("ckp");
ret &= cublaslt_init(0, &rm_memory);
}
// Make sure runtime API is initialized
// If we don't do this and use the driver API, it might be unintialized
cudaError_t cres;
if ((cres = cudaSetDevice(0)) != cudaSuccess) {
LOG(LOG_ERROR, "cudaSetDevice failed: %d", cres);
ret = 1;
}
cudaDeviceSynchronize();
return ret;
}
int server_runtime_deinit(void)
{
resource_mg_free(&rm_streams);
resource_mg_free(&rm_events);
resource_mg_free(&rm_arrays);
resource_mg_free(&rm_memory);
resource_mg_free(&rm_kernels);
cusolver_deinit();
cublas_deinit();
list_free(&mt_memcpy_list);
return 0;
}
int server_runtime_checkpoint(const char *path, int dump_memory, unsigned long prog, unsigned long vers)
{
if (cr_dump_rpc_id(path, prog, vers) != 0) {
LOGE(LOG_ERROR, "error dumping api_records");
return 1;
}
if (cr_dump(path) != 0) {
LOGE(LOG_ERROR, "error dumping api_records");
return 1;
}
if (dump_memory == 1) {
if (cr_dump_memory(path) != 0) {
LOGE(LOG_ERROR, "error dumping memory");
return 1;
}
}
return 0;
}
int server_runtime_restore(const char *path)
{
struct timeval start, end;
double time = 0;
gettimeofday(&start, NULL);
if (cr_restore(path, &rm_memory, &rm_streams, &rm_events, &rm_arrays, cusolver_get_rm(), cublas_get_rm()) != 0) {
LOGE(LOG_ERROR, "error restoring api_records");
return 1;
}
gettimeofday(&end, NULL);
time = ((double)((end.tv_sec * 1e6 + end.tv_usec) -
(start.tv_sec * 1e6 + start.tv_usec)))/1.e6;
LOGE(LOG_INFO, "time: %f", time);
return 0;
}
/** implementation for CUDA_REGISTER_FUNCTION(ptr, str, str, str, int)
*
*/
bool_t cuda_register_function_1_svc(ptr fatCubinHandle, ptr hostFun, char* deviceFun, char* deviceName, int thread_limit, int* result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaRegisterFunction(%p, %p, %s, %s, %d)", fatCubinHandle, hostFun, deviceFun, deviceName, thread_limit);
void (*serverFun)(void);
if ( (serverFun = dlsym(RTLD_NEXT, "dlopen")) == NULL) {
LOGE(LOG_ERROR, "failed to get dlopen %s", dlerror());
*result = 1;
return 1;
}
if (resource_mg_add_sorted(&rm_kernels, (void*)hostFun, serverFun) != 0) {
LOGE(LOG_ERROR, "failed to add kernel to resource manager");
*result = 1;
return 1;
}
LOGE(LOG_DEBUG, "added kernel %p->%p to resource manager", hostFun, serverFun);
// __cudaRegisterFunction(&fatCubinHandle, hostFun, deviceFun,
// deviceName, thread_limit, &tid, &bid, &bDim, &gDim, &wSize);
// LOGE(LOG_DEBUG, "-> %p, {%d, %d, %d}, {%d, %d, %d}, {%d, %d, %d}, {%d, %d, %d}, %d)",
// fatCubinHandle,
// tid.x, tid.y, tid.z,
// bid.x, bid.y, bid.z,
// bDim.x, bDim.y, bDim.z,
// gDim.x, gDim.y, gDim.z,
// wSize);
*result = 0;
return 1;
}
/* ############### RUNTIME API ############### */
/* ### Device Management ### */
bool_t cuda_choose_device_1_svc(mem_data prop, int_result *result, struct svc_req *rqstp)
{
struct cudaDeviceProp *cudaProp;
LOGE(LOG_DEBUG, "cudaChooseDevice");
if (prop.mem_data_len != sizeof(struct cudaDeviceProp)) {
LOGE(LOG_ERROR, "Received wrong amount of data: expected %zu but got %zu", sizeof(struct cudaDeviceProp), prop.mem_data_len);
return 0;
}
cudaProp = (struct cudaDeviceProp*)prop.mem_data_val;
RECORD_API(struct cudaDeviceProp);
RECORD_SINGLE_ARG(*cudaProp);
result->err = cudaChooseDevice(&result->int_result_u.data, cudaProp);
RECORD_RESULT(integer, result->int_result_u.data);
return 1;
}
bool_t cuda_device_get_attribute_1_svc(int attr, int device, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaDeviceGetAttribute");
result->err = cudaDeviceGetAttribute(&result->int_result_u.data, (enum cudaDeviceAttr)attr, device);
return 1;
}
bool_t cuda_device_get_by_pci_bus_id_1_svc(char* pciBusId, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaDeviceGetByPCIBusId");
result->err = cudaDeviceGetByPCIBusId(&result->int_result_u.data, pciBusId);
return 1;
}
bool_t cuda_device_get_cache_config_1_svc(int_result *result, struct svc_req *rqstp)
{
enum cudaFuncCache res;
LOGE(LOG_DEBUG, "cudaDeviceGetCacheConfig");
result->err = cudaDeviceGetCacheConfig(&res);
result->int_result_u.data = (int)res;
return 1;
}
bool_t cuda_device_get_limit_1_svc(int limit, u64_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaDeviceLimit");
result->err = cudaDeviceGetLimit(&result->u64_result_u.u64, limit);
return 1;
}
// /*mem_result CUDA_DEVICE_GET_NVSCISYNC_ATTRIBUTES(int ,int) = 106;*/
bool_t cuda_device_get_p2p_attribute_1_svc(int attr, int srcDevice, int dstDevice, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaDeviceGetP2PAttribute");
result->err = cudaDeviceGetP2PAttribute(&result->int_result_u.data, attr,
srcDevice, dstDevice);
return 1;
}
bool_t cuda_device_get_pci_bus_id_1_svc(int len, int device, str_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaDeviceGetPCIBusId");
if ((result->str_result_u.str = malloc(len)) == NULL) {
LOGE(LOG_ERROR, "malloc failed");
return 0;
}
result->err = cudaDeviceGetPCIBusId(result->str_result_u.str, len, device);
return 1;
}
bool_t cuda_device_get_shared_mem_config_1_svc(int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaDeviceGetSharedMemConfig");
result->err = cudaDeviceGetSharedMemConfig((enum cudaSharedMemConfig*)&result->int_result_u.data);
return 1;
}
bool_t cuda_device_get_stream_priority_range_1_svc(dint_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaDeviceGetStreamPriorityRange");
result->err = cudaDeviceGetStreamPriorityRange(&result->dint_result_u.data.i1, &result->dint_result_u.data.i2);
return 1;
}
bool_t cuda_device_get_texture_lmw_1_svc(cuda_channel_format_desc fmtDesc, int device, u64_result *result, struct svc_req *rqstp)
{
#if CUDART_VERSION >= 11000
struct cudaChannelFormatDesc desc = {
.f = fmtDesc.f,
.w = fmtDesc.w,
.x = fmtDesc.x,
.y = fmtDesc.y,
.z = fmtDesc.z,
};
LOGE(LOG_DEBUG, "cudaDeviceGetTexture1DLinearMaxWidth");
result->err = cudaDeviceGetTexture1DLinearMaxWidth(&result->u64_result_u.u64,
&desc, device);
return 1;
#else
LOGE(LOG_ERROR, "not compiled with CUDA 11 support");
return 1;
#endif
}
bool_t cuda_device_reset_1_svc(int *result, struct svc_req *rqstp)
{
RECORD_VOID_API;
LOGE(LOG_DEBUG, "cudaDeviceReset");
*result = cudaDeviceReset();
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_device_set_cache_config_1_svc(int cacheConfig, int *result, struct svc_req *rqstp)
{
RECORD_API(int);
RECORD_SINGLE_ARG(cacheConfig);
LOGE(LOG_DEBUG, "cudaFuncSetCacheConfig");
*result = cudaDeviceSetCacheConfig(cacheConfig);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_device_set_limit_1_svc(int limit, size_t value, int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_device_set_limit_1_argument);
RECORD_ARG(1, limit);
RECORD_ARG(2, value);
LOGE(LOG_DEBUG, "cudaFuncSetLimit");
*result = cudaDeviceSetLimit(limit, value);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_device_set_shared_mem_config_1_svc(int config, int *result, struct svc_req *rqstp)
{
RECORD_API(int);
RECORD_SINGLE_ARG(config);
LOGE(LOG_DEBUG, "cudaFuncSetSharedMemConfig");
*result = cudaDeviceSetSharedMemConfig(config);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_device_synchronize_1_svc(int *result, struct svc_req *rqstp)
{
RECORD_VOID_API;
LOGE(LOG_DEBUG, "cudaDeviceSynchronize");
*result = cudaDeviceSynchronize();
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_get_device_1_svc(int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaGetDevice");
result->err = cudaGetDevice(&result->int_result_u.data);
return 1;
}
bool_t cuda_get_device_count_1_svc(int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaGetDeviceCount");
result->err = cudaGetDeviceCount(&result->int_result_u.data);
return 1;
}
bool_t cuda_get_device_flags_1_svc(int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaGetDeviceFlags");
result->err = cudaGetDeviceFlags((unsigned*)&result->int_result_u.data);
return 1;
}
bool_t cuda_get_device_properties_1_svc(int device, cuda_device_prop_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaGetDeviceProperties");
if (sizeof(result->cuda_device_prop_result_u.data) != sizeof(struct cudaDeviceProp)) {
LOGE(LOG_ERROR, "cuda_device_prop_result size mismatch, result %d prop %d", sizeof(result->cuda_device_prop_result_u.data), sizeof(struct cudaDeviceProp));
return 0;
}
result->err = cudaGetDeviceProperties((void*)result->cuda_device_prop_result_u.data, device);
return 1;
}
// /*int CUDA_IPC_CLOSE_MEM_HANDLE(ptr) = 121;*/
// /*ptr_result CUDA_IPC_GET_EVENT_HANDLE(int) = 122;*/
// /*ptr_result CUDA_IPC_GET_MEM_HANDLE(ptr) = 123;*/
// /*ptr_result CUDA_IPC_OPEN_EVENT_HANDLE(ptr) = 124;*/
// /*ptr_result CUDA_IPC_OPEN_MEM_HANDLE(ptr, int) = 125;*/
bool_t cuda_set_device_1_svc(int device, int *result, struct svc_req *rqstp)
{
RECORD_API(int);
RECORD_SINGLE_ARG(device);
LOGE(LOG_DEBUG, "cudaSetDevice(%d)", device);
*result = cudaSetDevice(device);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_set_device_flags_1_svc(int flags, int *result, struct svc_req *rqstp)
{
RECORD_API(int);
RECORD_SINGLE_ARG(flags);
LOGE(LOG_DEBUG, "cudaSetDevice");
*result = cudaSetDeviceFlags(flags);
RECORD_RESULT(integer, *result);
return 1;
}
struct cuda_set_valid_device_param {
int* arg1;
int arg2;
};
bool_t cuda_set_valid_devices_1_svc(mem_data device_arr, int len, int *result, struct svc_req *rqstp)
{
RECORD_API(struct cuda_set_valid_device_param);
#ifdef WITH_RECORDER
int *valid_device = malloc(len*sizeof(int));
if (valid_device == NULL) {
LOGE(LOG_ERROR, "malloc failed.");
return 0;
}
/* TODO: We actually create a memory leak here. We need to explicity
* clean up the allocated memory region when the recorder list is cleaned */
memcpy(valid_device, device_arr.mem_data_val, len*sizeof(int));
#endif
RECORD_ARG(1, valid_device);
RECORD_ARG(2, len);
LOGE(LOG_DEBUG, "cudaSetValidDevices");
if (device_arr.mem_data_len != len*sizeof(int)) {
LOGE(LOG_ERROR, "mismatch between expected size (%d) and received size (%d)", len*sizeof(int), device_arr.mem_data_len);
return 0;
}
*result = cudaSetValidDevices((int*)device_arr.mem_data_val, len);
RECORD_RESULT(integer, *result);
return 1;
}
/* ### Error Handling ### */
bool_t cuda_get_error_name_1_svc(int error, str_result *result, struct svc_req *rqstp)
{
const char* str;
result->str_result_u.str = malloc(128);
LOGE(LOG_DEBUG, "cudaGetErrorName");
str = cudaGetErrorName((cudaError_t)error);
strncpy(result->str_result_u.str, str, 128);
result->err = 0;
return 1;
}
bool_t cuda_get_error_string_1_svc(int error, str_result *result, struct svc_req *rqstp)
{
const char* str;
result->str_result_u.str = malloc(128);
LOGE(LOG_DEBUG, "cudaGetErrorString");
str = cudaGetErrorString((cudaError_t)error);
strncpy(result->str_result_u.str, str, 128);
result->err = 0;
return 1;
}
bool_t cuda_get_last_error_1_svc(int *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaGetLastError");
*result = cudaGetLastError();
return 1;
}
bool_t cuda_peek_at_last_error_1_svc(int *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaPeekAtLastError");
*result = cudaPeekAtLastError();
return 1;
}
/* ### Stream Management ### */
bool_t cuda_ctx_reset_persisting_l2cache_1_svc(int *result, struct svc_req *rqstp)
{
#if CUDART_VERSION >= 11000
RECORD_VOID_API;
LOGE(LOG_DEBUG, "cudaCtxResetPersistingL2Cache");
*result = cudaCtxResetPersistingL2Cache();
RECORD_RESULT(integer, *result);
#else
LOGE(LOG_ERROR, "Compiled without CUDA 11 support");
#endif
return 1;
}
/* Requires us do call a callback on the client side to make sense */
// int CUDA_STREAM_ADD_CALLBACK(ptr, ptr, mem_data, int) = 251;
/* Requires unified memory OR attaching a shared memory region. */
// int CUDA_STREAM_ATTACH_MEM_ASYNC(ptr, ptr, size_t, int)= 252;
/* Requires Graph API to make sense */
// int CUDA_STREAM_BEGIN_CAPTURE(ptr, int) = 253;
bool_t cuda_stream_copy_attributes_1_svc(ptr dst, ptr src, int *result, struct svc_req *rqstp)
{
#if CUDART_VERSION >= 11000
RECORD_API(cuda_stream_copy_attributes_1_argument);
RECORD_ARG(1, dst);
RECORD_ARG(2, src);
LOGE(LOG_DEBUG, "cudaStreamCopyAttributes");
*result = cudaStreamCopyAttributes(resource_mg_get(&rm_streams, (void*)dst),
resource_mg_get(&rm_streams, (void*)src));
RECORD_RESULT(integer, *result);
return 1;
#else
LOGE(LOG_ERROR, "not compiled with CUDA 11 support");
return 1;
#endif
}
bool_t cuda_stream_create_1_svc(ptr_result *result, struct svc_req *rqstp)
{
RECORD_VOID_API;
LOGE(LOG_DEBUG, "cudaStreamCreate");
result->err = cudaStreamCreate((void*)&result->ptr_result_u.ptr);
if (resource_mg_create(&rm_streams, (void*)result->ptr_result_u.ptr) != 0) {
LOGE(LOG_ERROR, "error in resource manager");
}
RECORD_RESULT(ptr_result_u, *result);
return 1;
}
bool_t cuda_stream_create_with_flags_1_svc(int flags, ptr_result *result, struct svc_req *rqstp)
{
RECORD_API(int);
RECORD_SINGLE_ARG(flags);
LOGE(LOG_DEBUG, "cudaStreamCreateWithFlags");
result->err = cudaStreamCreateWithFlags((void*)&result->ptr_result_u.ptr, flags);
if (resource_mg_create(&rm_streams, (void*)result->ptr_result_u.ptr) != 0) {
LOGE(LOG_ERROR, "error in resource manager");
}
LOG(LOG_DEBUG, "add to stream rm: %p", result->ptr_result_u.ptr);
RECORD_RESULT(ptr_result_u, *result);
return 1;
}
bool_t cuda_stream_create_with_priority_1_svc(int flags, int priority, ptr_result *result, struct svc_req *rqstp)
{
RECORD_API(cuda_stream_create_with_priority_1_argument);
RECORD_ARG(1, flags);
RECORD_ARG(2, priority);
LOGE(LOG_DEBUG, "cudaStreamCreateWithPriority");
result->err = cudaStreamCreateWithPriority((void*)&result->ptr_result_u.ptr, flags, priority);
if (resource_mg_create(&rm_streams, (void*)result->ptr_result_u.ptr) != 0) {
LOGE(LOG_ERROR, "error in resource manager");
}
RECORD_RESULT(ptr_result_u, *result);
return 1;
}
bool_t cuda_stream_destroy_1_svc(ptr stream, int *result, struct svc_req *rqstp)
{
RECORD_API(ptr);
RECORD_SINGLE_ARG(stream);
LOGE(LOG_DEBUG, "cudaStreamDestroy");
*result = cudaStreamDestroy(resource_mg_get(&rm_streams, (void*)stream));
RECORD_RESULT(integer, *result);
return 1;
}
/* Capture API does not make sense without graph API */
// /*ptr_result CUDA_STREAM_END_CAPTURE(ptr) = 259;*/
/* What datatypes are in the union cudaStreamAttrValue? */
// /* ? CUDA_STREAM_GET_ATTRIBUTE(ptr, int) = 260;*/
/* Capture API does not make sense without graph API */
// /* ? CUDA_STREAM_GET_CAPTURE_INFO(ptr) = 261;*/
bool_t cuda_stream_get_flags_1_svc(ptr hStream, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaStreamGetFlags");
result->err = cudaStreamGetFlags(resource_mg_get(&rm_streams, (void*)hStream),
(unsigned*)&result->int_result_u.data);
return 1;
}
bool_t cuda_stream_get_priority_1_svc(ptr hStream, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaStreamGetPriority");
result->err = cudaStreamGetPriority(
resource_mg_get(&rm_streams, (void*)hStream),
&result->int_result_u.data);
return 1;
}
bool_t cuda_stream_is_capturing_1_svc(ptr stream, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaStreamIsCapturing");
result->err = cudaStreamIsCapturing(
resource_mg_get(&rm_streams, (void*)stream),
(enum cudaStreamCaptureStatus*)&result->int_result_u.data);
return 1;
}
bool_t cuda_stream_query_1_svc(ptr hStream, int *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaStreamQuery");
*result = cudaStreamQuery(
resource_mg_get(&rm_streams, (void*)hStream));
return 1;
}
/* What datatypes are in the union cudaStreamAttrValue? */
// /*int CUDA_STREAM_SET_ATTRIBUTE(ptr, int, ?) = 266;*/
bool_t cuda_stream_synchronize_1_svc(ptr stream, int *result, struct svc_req *rqstp)
{
RECORD_API(uint64_t);
RECORD_SINGLE_ARG(stream);
LOGE(LOG_DEBUG, "cudaStreamSynchronize");
*result = cudaStreamSynchronize(
resource_mg_get(&rm_streams, (void*)stream));
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_stream_wait_event_1_svc(ptr stream, ptr event, int flags, int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_stream_wait_event_1_argument);
RECORD_ARG(1, stream);
RECORD_ARG(2, event);
RECORD_ARG(3, flags);
LOGE(LOG_DEBUG, "cudaStreamWaitEvent");
*result = cudaStreamWaitEvent(
resource_mg_get(&rm_streams, (void*)stream),
resource_mg_get(&rm_events, (void*)event),
flags);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_thread_exchange_stream_capture_mode_1_svc(int mode, int_result *result, struct svc_req *rqstp)
{
RECORD_API(int);
RECORD_SINGLE_ARG(mode);
LOGE(LOG_DEBUG, "cudaThreadExchangeStreamCaptureMode");
result->int_result_u.data = mode;
result->err = cudaThreadExchangeStreamCaptureMode((void*)&result->int_result_u.data);
RECORD_RESULT(integer, result->int_result_u.data);
return 1;
}
/* ### Event Management ### */
bool_t cuda_event_create_1_svc(ptr_result *result, struct svc_req *rqstp)
{
RECORD_VOID_API;
LOGE(LOG_DEBUG, "cudaEventCreate");
result->err = cudaEventCreate((struct CUevent_st**)&result->ptr_result_u.ptr);
if (resource_mg_create(&rm_events, (void*)result->ptr_result_u.ptr) != 0) {
LOGE(LOG_ERROR, "error in resource manager");
}
RECORD_RESULT(ptr_result_u, *result);
return 1;
}
bool_t cuda_event_create_with_flags_1_svc(int flags, ptr_result *result, struct svc_req *rqstp)
{
RECORD_API(int);
RECORD_SINGLE_ARG(flags);
LOGE(LOG_DEBUG, "cudaEventCreateWithFlags");
result->err = cudaEventCreateWithFlags((struct CUevent_st**)&result->ptr_result_u.ptr, flags);
if (resource_mg_create(&rm_events, (void*)result->ptr_result_u.ptr) != 0) {
LOGE(LOG_ERROR, "error in resource manager");
}
RECORD_RESULT(ptr_result_u, *result);
return 1;
}
bool_t cuda_event_destroy_1_svc(ptr event, int *result, struct svc_req *rqstp)
{
RECORD_API(ptr);
RECORD_SINGLE_ARG(event);
LOGE(LOG_DEBUG, "cudaEventDestroy");
*result = cudaEventDestroy(
resource_mg_get(&rm_events, (void*)event));
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_event_elapsed_time_1_svc(ptr start, ptr end, float_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaEventElapsedTime");
result->err = cudaEventElapsedTime(&result->float_result_u.data,
resource_mg_get(&rm_events, (void*)start),
resource_mg_get(&rm_events, (void*)end));
return 1;
}
bool_t cuda_event_query_1_svc(ptr event, int *result, struct svc_req *rqstp)
{
RECORD_API(ptr);
RECORD_SINGLE_ARG(event);
LOGE(LOG_DEBUG, "cudaEventQuery");
*result = cudaEventQuery(
resource_mg_get(&rm_events, (void*)event));
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_event_record_1_svc(ptr event, ptr stream, int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_event_record_1_argument);
RECORD_ARG(1, event);
RECORD_ARG(2, stream);
LOGE(LOG_DEBUG, "cudaEventRecord");
*result = cudaEventRecord(
resource_mg_get(&rm_events, (void*)event),
resource_mg_get(&rm_streams, (void*)stream));
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_event_record_with_flags_1_svc(ptr event, ptr stream, int flags, int *result, struct svc_req *rqstp)
{
#if CUDART_VERSION >= 11000
RECORD_API(cuda_event_record_with_flags_1_argument);
RECORD_ARG(1, event);
RECORD_ARG(2, stream);
RECORD_ARG(3, flags);
LOGE(LOG_DEBUG, "cudaEventRecordWithFlags");
*result = cudaEventRecordWithFlags(
resource_mg_get(&rm_events, (void*)event),
resource_mg_get(&rm_streams, (void*)stream),
flags);
RECORD_RESULT(integer, *result);
return 1;
#else
LOGE(LOG_ERROR, "compiled without CUDA 11 support");
return 1;
#endif
}
bool_t cuda_event_synchronize_1_svc(ptr event, int *result, struct svc_req *rqstp)
{
RECORD_API(ptr);
RECORD_SINGLE_ARG(event);
LOGE(LOG_DEBUG, "cudaEventSynchronize");
*result = cudaEventSynchronize(
resource_mg_get(&rm_events, (void*)event));
RECORD_RESULT(integer, *result);
return 1;
}
/* Execution Control */
bool_t cuda_func_get_attributes_1_svc(ptr func, mem_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaFuncGetAttributes");
result->mem_result_u.data.mem_data_val =
malloc(sizeof(struct cudaFuncAttributes));
result->mem_result_u.data.mem_data_len = sizeof(struct cudaFuncAttributes);
result->err = cudaFuncGetAttributes(
(struct cudaFuncAttributes*) result->mem_result_u.data.mem_data_val,
(void*)func);
/* func is a pointer to program memory. It will be static across executions,
* so we do not need a resource manager */
return 1;
}
bool_t cuda_func_set_attributes_1_svc(ptr func, int attr, int value, int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_func_set_attributes_1_argument);
RECORD_ARG(1, func);
RECORD_ARG(2, attr);
RECORD_ARG(3, value);
LOGE(LOG_DEBUG, "cudaFuncSetAttributes");
*result = cudaFuncSetAttribute((void*)func, attr, value);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_func_set_cache_config_1_svc(ptr func, int cacheConfig, int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_func_set_cache_config_1_argument);
RECORD_ARG(1, func);
RECORD_ARG(2, cacheConfig);
LOGE(LOG_DEBUG, "cudaFuncSetCacheConfig");
*result = cudaFuncSetCacheConfig((void*)func, cacheConfig);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_func_set_shared_mem_config_1_svc(ptr func, int config, int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_func_set_shared_mem_config_1_argument);
RECORD_ARG(1, func);
RECORD_ARG(2, config);
LOGE(LOG_DEBUG, "cudaFuncSetSharedMemConfig");
*result = cudaFuncSetSharedMemConfig((void*)func, config);
RECORD_RESULT(integer, *result);
return 1;
}
bool_t cuda_launch_cooperative_kernel_1_svc(ptr func, rpc_dim3 gridDim, rpc_dim3 blockDim, mem_data args, size_t sharedMem, ptr stream, int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_launch_cooperative_kernel_1_argument);
RECORD_ARG(1, func);
RECORD_ARG(2, gridDim);
RECORD_ARG(3, blockDim);
//TODO: Store parameters explicitly
//RECORD_ARG(4, args);
RECORD_ARG(5, sharedMem);
RECORD_ARG(6, stream);
dim3 cuda_gridDim = {gridDim.x, gridDim.y, gridDim.z};
dim3 cuda_blockDim = {blockDim.x, blockDim.y, blockDim.z};
void **cuda_args;
uint16_t *arg_offsets;
size_t param_num = *((size_t*)args.mem_data_val);
arg_offsets = (uint16_t*)(args.mem_data_val+sizeof(size_t));
cuda_args = malloc(param_num*sizeof(void*));
for (size_t i = 0; i < param_num; ++i) {
cuda_args[i] = args.mem_data_val+sizeof(size_t)+param_num*sizeof(uint16_t)+arg_offsets[i];
// LOGE(LOG_DEBUG, "arg: %p (%d)\n", *(void**)cuda_args[i], *(int*)cuda_args[i]);
}
LOGE(LOG_DEBUG, "cudaLaunchCooperativeKernel(func=%p, gridDim=[%d,%d,%d], blockDim=[%d,%d,%d], args=%p, sharedMem=%d, stream=%p)", func, cuda_gridDim.x, cuda_gridDim.y, cuda_gridDim.z, cuda_blockDim.x, cuda_blockDim.y, cuda_blockDim.z, cuda_args, sharedMem, (void*)stream);
*result = cudaLaunchCooperativeKernel(
resource_mg_get(&rm_kernels, (void*)func),
cuda_gridDim,
cuda_blockDim,
cuda_args,
sharedMem,
resource_mg_get(&rm_streams, (void*)stream));
RECORD_RESULT(integer, *result);
LOGE(LOG_DEBUG, "cudaLaunchCooperativeKernel result: %d", *result);
return 1;
}
/* This would require RPCs in the opposite direction.
* __host__ cudaError_t cudaLaunchHostFunc ( cudaStream_t stream, cudaHostFn_t fn, void* userData )
* Enqueues a host function call in a stream.
*/
bool_t cuda_launch_kernel_1_svc(ptr func, rpc_dim3 gridDim, rpc_dim3 blockDim,
mem_data args, size_t sharedMem, ptr stream,
int *result, struct svc_req *rqstp)
{
RECORD_API(cuda_launch_kernel_1_argument);
RECORD_ARG(1, func);
RECORD_ARG(2, gridDim);
RECORD_ARG(3, blockDim);
RECORD_DATA(args.mem_data_len, args.mem_data_val);
RECORD_ARG(5, sharedMem);
RECORD_ARG(6, stream);
dim3 cuda_gridDim = {gridDim.x, gridDim.y, gridDim.z};
dim3 cuda_blockDim = {blockDim.x, blockDim.y, blockDim.z};
void **cuda_args;
uint16_t *arg_offsets;
size_t param_num = *((size_t*)args.mem_data_val);
arg_offsets = (uint16_t*)(args.mem_data_val+sizeof(size_t));
cuda_args = malloc(param_num*sizeof(void*));
for (size_t i = 0; i < param_num; ++i) {
cuda_args[i] = args.mem_data_val+sizeof(size_t)+param_num*sizeof(uint16_t)+arg_offsets[i];
*(void**)cuda_args[i] = resource_mg_get(&rm_memory, *(void**)cuda_args[i]);
LOGE(LOG_DEBUG, "arg: %p (%d)", *(void**)cuda_args[i], *(int*)cuda_args[i]);
}
LOGE(LOG_DEBUG, "cudaLaunchKernel(func=%p, gridDim=[%d,%d,%d], blockDim=[%d,%d,%d], args=%p, sharedMem=%d, stream=%p)",
resource_mg_get(&rm_functions, (void*)func),
cuda_gridDim.x, cuda_gridDim.y, cuda_gridDim.z,
cuda_blockDim.x, cuda_blockDim.y, cuda_blockDim.z,
cuda_args,
sharedMem,
(void*)stream);
*result = cuLaunchKernel((CUfunction)resource_mg_get(&rm_functions, (void*)func),
gridDim.x, gridDim.y, gridDim.z,
blockDim.x, blockDim.y, blockDim.z,
sharedMem,
resource_mg_get(&rm_streams, (void*)stream),
cuda_args, NULL);
// *result = cudaLaunchKernel(
// resource_mg_get(&rm_functions, (void*)func),
// cuda_gridDim,
// cuda_blockDim,
// cuda_args,
// sharedMem,
// resource_mg_get(&rm_streams, (void*)stream));
free(cuda_args);
RECORD_RESULT(integer, *result);
LOGE(LOG_DEBUG, "cudaLaunchKernel result: %d", *result);
return 1;
}
/* cudaSetDoubleForDevice ( double* d ) is deprecated */
/* cudaSetDoubleForHost ( double* d ) is deprecated */
/* Occupancy */
bool_t cuda_occupancy_available_dsmpb_1_svc(ptr func, int numBlocks, int blockSize, u64_result *result, struct svc_req *rqstp)
{
#if CUDART_VERSION >= 11000
LOGE(LOG_DEBUG, "cudaOccupancyAvailableDynamicSMemPerBlock");
result->err = cudaOccupancyAvailableDynamicSMemPerBlock(
&result->u64_result_u.u64, (void*)func, numBlocks, blockSize);
return 1;
#else
LOGE(LOG_ERROR, "compiled without CUDA 11 support");
return 1;
#endif
}
bool_t cuda_occupancy_max_active_bpm_1_svc(ptr func, int blockSize, size_t dynamicSMemSize, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaOccupancyMaxActiveBlocksPerMultiprocessor");
result->err = cudaOccupancyMaxActiveBlocksPerMultiprocessor(
&result->int_result_u.data, (void*)func, blockSize, dynamicSMemSize);
return 1;
}
bool_t cuda_occupancy_max_active_bpm_with_flags_1_svc(ptr func, int blockSize, size_t dynamicSMemSize, int flags, int_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags");
result->err = cudaOccupancyMaxActiveBlocksPerMultiprocessorWithFlags(
&result->int_result_u.data, (void*)func, blockSize, dynamicSMemSize, flags);
return 1;
}
/* Memory Management */
bool_t cuda_array_get_info_1_svc(ptr array, mem_result *result, struct svc_req *rqstp)
{
LOGE(LOG_DEBUG, "cudaArrayGetInfo");
result->mem_result_u.data.mem_data_len =
sizeof(struct cudaChannelFormatDesc)+sizeof(struct cudaExtent)+sizeof(int);
result->mem_result_u.data.mem_data_val = malloc(
sizeof(struct cudaChannelFormatDesc)+sizeof(struct cudaExtent)+sizeof(int));
struct cudaChannelFormatDesc* desc = (void*)result->mem_result_u.data.mem_data_val;
struct cudaExtent *extent = (void*)result->mem_result_u.data.mem_data_val+
sizeof(struct cudaChannelFormatDesc);
unsigned *flags = (void*)&result->mem_result_u.data.mem_data_val+
sizeof(struct cudaChannelFormatDesc)+
sizeof(struct cudaExtent);
result->err = cudaArrayGetInfo(desc,
extent,
flags,
resource_mg_get(&rm_arrays, (void*)array));
return 1;
}
bool_t cuda_array_get_sparse_properties_1_svc(ptr array, mem_result *result, struct svc_req *rqstp)
{
#if CUDART_VERSION >= 11000
LOGE(LOG_DEBUG, "cudaArrayGetSparseProperties");
result->mem_result_u.data.mem_data_len =
sizeof(struct cudaArraySparseProperties);
result->mem_result_u.data.mem_data_val = malloc(
sizeof(struct cudaArraySparseProperties));
struct cudaArraySparseProperties* sparseProperties = (void*)result->mem_result_u.data.mem_data_val;
result->err = cudaArrayGetSparseProperties(
sparseProperties,
resource_mg_get(&rm_arrays, (void*)array));
return 1;
#else
LOGE(LOG_ERROR, "not compiled with CUDA 11 support");
return 1;
#endif
}
bool_t cuda_free_1_svc(ptr devPtr, int *result, struct svc_req *rqstp)
{
RECORD_API(ptr);
RECORD_SINGLE_ARG(devPtr);
int index = hainfo_getserverindex((void*)devPtr);
int ret = 1;
uint64_t arg;
api_record_t *r;
LOGE(LOG_DEBUG, "cudaFree");
#ifdef WITH_IB
ib_free_memreg((void*)devPtr, index, true);
hainfo[index].server_ptr = 0;
*result = 0;
#else
*result = cudaFree(resource_mg_get(&rm_memory, (void*)devPtr));
#endif
/* The cleanup/simplification of the record could also be
* done during checkpoint creation. What is better depends
* on whether we want to minimize overhead of cudaFree or
* of the checkpoint creation. */
//for (size_t i = 0; i < api_records.length; ++i) {
// r = (api_record_t*)api_records.elements[i];