-
Notifications
You must be signed in to change notification settings - Fork 84
/
ccudart.pyx
1962 lines (1617 loc) · 79.1 KB
/
ccudart.pyx
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
# Copyright 2021-2022 NVIDIA Corporation. All rights reserved.
#
# Please refer to the NVIDIA end user license agreement (EULA) associated
# with this source code for terms and conditions that govern your use of
# this software. Any use, reproduction, disclosure, or distribution of
# this software and related documentation outside the terms of the EULA
# is strictly prohibited.
from cuda.ccudart cimport *
from cuda._lib.ccudart.utils cimport *
from libc.stdlib cimport malloc, free, calloc
from libc.string cimport memset, memcpy, strncmp
from libcpp cimport bool
cimport cuda._cuda.ccuda as ccuda
cdef cudaPythonGlobal m_global = globalGetInstance()
cdef cudaError_t _cudaMemcpy(void* dst, const void* src, size_t count, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpyDispatch(dst, src, count, kind)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaStreamCreate(cudaStream_t* pStream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuStreamCreate(pStream, 0)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaEventCreate(cudaEvent_t* event) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuEventCreate(event, ccuda.CUevent_flags_enum.CU_EVENT_DEFAULT)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaEventQuery(cudaEvent_t event) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = <cudaError_t>ccuda._cuEventQuery(<ccuda.CUevent>event)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaChannelFormatDesc _cudaCreateChannelDesc(int x, int y, int z, int w, cudaChannelFormatKind f) nogil:
cdef cudaChannelFormatDesc desc
desc.x = x
desc.y = y
desc.z = z
desc.w = w
desc.f = f
return desc
cdef cudaError_t _cudaDriverGetVersion(int* driverVersion) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = <cudaError_t>ccuda._cuDriverGetVersion(driverVersion)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaRuntimeGetVersion(int* runtimeVersion) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
runtimeVersion[0] = m_global.CUDART_VERSION
return cudaSuccess
cdef cudaError_t _cudaDeviceGetTexture1DLinearMaxWidth(size_t* maxWidthInElements, const cudaChannelFormatDesc* fmtDesc, int device) nogil except ?cudaErrorCallRequiresNewerDriver:
if fmtDesc == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
cdef cudaError_t err
cdef ccuda.CUarray_format fmt
cdef int numChannels = 0
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = getDescInfo(fmtDesc, &numChannels, &fmt)
if err == cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuDeviceGetTexture1DLinearMaxWidth(maxWidthInElements, fmt, <unsigned>numChannels, device)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMallocHost(void** ptr, size_t size) nogil except ?cudaErrorCallRequiresNewerDriver:
if ptr == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = mallocHost(size, ptr, 0)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMallocPitch(void** devPtr, size_t* pitch, size_t width, size_t height) nogil except ?cudaErrorCallRequiresNewerDriver:
if devPtr == NULL or pitch == NULL:
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = mallocPitch(width, height, 1, devPtr, pitch)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMallocMipmappedArray(cudaMipmappedArray_t* mipmappedArray, const cudaChannelFormatDesc* desc, cudaExtent extent, unsigned int numLevels, unsigned int flags) nogil except ?cudaErrorCallRequiresNewerDriver:
if mipmappedArray == NULL or desc == NULL:
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = mallocMipmappedArray(mipmappedArray, desc, extent.depth, extent.height, extent.width, numLevels, flags)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpy2D(void* dst, size_t dpitch, const void* src, size_t spitch, size_t width, size_t height, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy2DPtr(<char*>dst, dpitch, <const char*>src, spitch, width, height, kind, NULL, False)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpy2DAsync(void* dst, size_t dpitch, const void* src, size_t spitch, size_t width, size_t height, cudaMemcpyKind kind, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy2DPtr(<char*>dst, dpitch, <const char*>src, spitch, width, height, kind, stream, True)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpyAsync(void* dst, const void* src, size_t count, cudaMemcpyKind kind, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpyAsyncDispatch(dst, src, count, kind, stream)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphAddMemcpyNode(cudaGraphNode_t* pGraphNode, cudaGraph_t graph, const cudaGraphNode_t* pDependencies, size_t numDependencies, const cudaMemcpy3DParms* pCopyParams) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUcontext context
cdef ccuda.CUDA_MEMCPY3D_v2 driverNodeParams
cdef cudaError_t err
if pCopyParams == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuCtxGetCurrent(&context)
if err != cudaSuccess:
_setLastError(err)
return err
err = toDriverMemCopy3DParams(pCopyParams, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuGraphAddMemcpyNode(pGraphNode, graph, pDependencies, numDependencies, &driverNodeParams, context)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphAddMemcpyNode1D(cudaGraphNode_t* pGraphNode, cudaGraph_t graph, const cudaGraphNode_t* pDependencies, size_t numDependencies, void* dst, const void* src, size_t count, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUcontext context
cdef ccuda.CUDA_MEMCPY3D_v2 driverNodeParams
cdef cudaMemcpy3DParms copyParams
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuCtxGetCurrent(&context)
if err != cudaSuccess:
_setLastError(err)
return err
copy1DConvertTo3DParams(dst, src, count, kind, ©Params)
err = toDriverMemCopy3DParams(©Params, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuGraphAddMemcpyNode(pGraphNode, graph, pDependencies, numDependencies, &driverNodeParams, context)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphMemcpyNodeSetParams1D(cudaGraphNode_t node, void* dst, const void* src, size_t count, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUDA_MEMCPY3D_v2 driverNodeParams
cdef cudaMemcpy3DParms copyParams
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
copy1DConvertTo3DParams(dst, src, count, kind, ©Params)
err = toDriverMemCopy3DParams(©Params, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuGraphMemcpyNodeSetParams(node, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphExecMemcpyNodeSetParams(cudaGraphExec_t hGraphExec, cudaGraphNode_t node, const cudaMemcpy3DParms* pNodeParams) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUcontext context
cdef ccuda.CUDA_MEMCPY3D_v2 driverNodeParams
cdef cudaError_t err
if pNodeParams == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuCtxGetCurrent(&context)
if err != cudaSuccess:
_setLastError(err)
return err
err = toDriverMemCopy3DParams(pNodeParams, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuGraphExecMemcpyNodeSetParams(hGraphExec, node, &driverNodeParams, context)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphExecMemcpyNodeSetParams1D(cudaGraphExec_t hGraphExec, cudaGraphNode_t node, void* dst, const void* src, size_t count, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUcontext context
cdef ccuda.CUDA_MEMCPY3D_v2 driverNodeParams
cdef cudaMemcpy3DParms copyParams
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuCtxGetCurrent(&context)
if err != cudaSuccess:
_setLastError(err)
return err
copy1DConvertTo3DParams(dst, src, count, kind, ©Params)
err = toDriverMemCopy3DParams(©Params, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuGraphExecMemcpyNodeSetParams(hGraphExec, node, &driverNodeParams, context)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGetDriverEntryPoint(const char* symbol, void** funcPtr, unsigned long long flags) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = <cudaError_t>ccuda._cuGetProcAddress(symbol, funcPtr, m_global.CUDART_VERSION, flags)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphAddMemsetNode(cudaGraphNode_t* pGraphNode, cudaGraph_t graph, const cudaGraphNode_t* pDependencies, size_t numDependencies, const cudaMemsetParams* pMemsetParams) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUcontext context
cdef ccuda.CUDA_MEMSET_NODE_PARAMS driverParams
cdef cudaError_t err
if pMemsetParams == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuCtxGetCurrent(&context)
if err != cudaSuccess:
_setLastError(err)
return err
toDriverMemsetNodeParams(pMemsetParams, &driverParams)
err = <cudaError_t>ccuda._cuGraphAddMemsetNode(pGraphNode, graph, pDependencies, numDependencies, &driverParams, context)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphExecMemsetNodeSetParams(cudaGraphExec_t hGraphExec, cudaGraphNode_t node, const cudaMemsetParams* pNodeParams) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUcontext context
cdef ccuda.CUDA_MEMSET_NODE_PARAMS driverParams
cdef cudaError_t err
if pNodeParams == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuCtxGetCurrent(&context)
if err != cudaSuccess:
_setLastError(err)
return err
toDriverMemsetNodeParams(pNodeParams, &driverParams)
err = <cudaError_t>ccuda._cuGraphExecMemsetNodeSetParams(hGraphExec, node, &driverParams, context)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphMemcpyNodeSetParams(cudaGraphNode_t node, const cudaMemcpy3DParms* pNodeParams) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUDA_MEMCPY3D_v2 driverNodeParams
cdef cudaError_t err
if pNodeParams == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = toDriverMemCopy3DParams(pNodeParams, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuGraphMemcpyNodeSetParams(node, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaGraphMemcpyNodeGetParams(cudaGraphNode_t node, cudaMemcpy3DParms* p) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef ccuda.CUDA_MEMCPY3D_v2 driverNodeParams
if p == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuGraphMemcpyNodeGetParams(node, &driverNodeParams)
if err != cudaSuccess:
_setLastError(err)
return err
err = toCudartMemCopy3DParams(&driverNodeParams, p)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaFuncGetAttributes(cudaFuncAttributes* attr, const void* func) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
if NULL == attr:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
cdef int bytes
memset(attr, 0, sizeof(cudaFuncAttributes))
err = <cudaError_t>ccuda._cuFuncGetAttribute(&attr[0].maxThreadsPerBlock, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_MAX_THREADS_PER_BLOCK, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuFuncGetAttribute(&attr[0].numRegs, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_NUM_REGS, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuFuncGetAttribute(&attr[0].ptxVersion, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_PTX_VERSION, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuFuncGetAttribute(&attr[0].binaryVersion, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_BINARY_VERSION, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuFuncGetAttribute(&bytes, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_SHARED_SIZE_BYTES, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
attr[0].sharedSizeBytes = <size_t>bytes
err = <cudaError_t>ccuda._cuFuncGetAttribute(&bytes, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_CONST_SIZE_BYTES, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
attr[0].constSizeBytes = <size_t>bytes
err = <cudaError_t>ccuda._cuFuncGetAttribute(&bytes, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_LOCAL_SIZE_BYTES, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
attr[0].localSizeBytes = <size_t>bytes
err = <cudaError_t>ccuda._cuFuncGetAttribute(&attr[0].cacheModeCA, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_CACHE_MODE_CA, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuFuncGetAttribute(&bytes, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuFuncGetAttribute(&attr[0].preferredShmemCarveout, ccuda.CUfunction_attribute_enum.CU_FUNC_ATTRIBUTE_PREFERRED_SHARED_MEMORY_CARVEOUT, <ccuda.CUfunction>func)
if err != cudaSuccess:
_setLastError(err)
return err
attr[0].maxDynamicSharedSizeBytes = <size_t>bytes
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMallocArray(cudaArray_t* arrayPtr, const cudaChannelFormatDesc* desc, size_t width, size_t height, unsigned int flags) nogil except ?cudaErrorCallRequiresNewerDriver:
if arrayPtr == NULL or desc == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = mallocArray(arrayPtr, desc, 0, height, width, 0, flags)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMalloc3D(cudaPitchedPtr* pitchedDevPtr, cudaExtent extent) nogil except ?cudaErrorCallRequiresNewerDriver:
if pitchedDevPtr == NULL:
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = mallocPitch(extent.width, extent.height, extent.depth, &pitchedDevPtr[0].ptr, &pitchedDevPtr[0].pitch)
if err != cudaSuccess:
_setLastError(err)
return err
pitchedDevPtr[0].xsize = extent.width
pitchedDevPtr[0].ysize = extent.height
return err
cdef cudaError_t _cudaMalloc3DArray(cudaArray_t* arrayPtr, const cudaChannelFormatDesc* desc, cudaExtent extent, unsigned int flags) nogil except ?cudaErrorCallRequiresNewerDriver:
if arrayPtr == NULL or desc == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = mallocArray(arrayPtr, desc, extent.depth, extent.height, extent.width, 0, flags)
if err != cudaSuccess:
_setLastError(err)
return err
cdef const char* _cudaGetErrorString(cudaError_t error) nogil except ?NULL:
cdef const char* pStr = NULL
cdef cudaError_t err
err = <cudaError_t>ccuda._cuGetErrorString(<ccuda.CUresult>error, &pStr)
if err != cudaSuccess:
_setLastError(err)
if err == <cudaError_t>cudaErrorInvalidValue:
pStr = "unrecognized error code"
return pStr
cdef cudaError_t _cudaStreamAddCallback(cudaStream_t stream, cudaStreamCallback_t callback, void* userData, unsigned int flags) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = streamAddCallbackCommon(stream, callback, userData, flags)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaStreamGetCaptureInfo(cudaStream_t stream, cudaStreamCaptureStatus* captureStatus_out, unsigned long long* id_out) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = streamGetCaptureInfoCommon(stream, captureStatus_out, id_out, <cudaGraph_t *>0, <const cudaGraphNode_t **>0, <size_t *>0)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaStreamGetCaptureInfo_v2(cudaStream_t stream, cudaStreamCaptureStatus* captureStatus_out, unsigned long long* id_out, cudaGraph_t* graph_out, const cudaGraphNode_t** dependencies_out, size_t* numDependencies_out) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = streamGetCaptureInfoCommon(stream, captureStatus_out, id_out, graph_out, dependencies_out, numDependencies_out)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaImportExternalSemaphore(cudaExternalSemaphore_t* extSem_out, const cudaExternalSemaphoreHandleDesc* semHandleDesc) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
cdef ccuda.CUDA_EXTERNAL_SEMAPHORE_HANDLE_DESC driverSemHandleDesc
if semHandleDesc == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
memset(&driverSemHandleDesc, 0, sizeof(driverSemHandleDesc))
if semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeOpaqueFd:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD
driverSemHandleDesc.handle.fd = semHandleDesc.handle.fd
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeOpaqueWin32:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32
driverSemHandleDesc.handle.win32.handle = semHandleDesc.handle.win32.handle
driverSemHandleDesc.handle.win32.name = semHandleDesc.handle.win32.name
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeOpaqueWin32Kmt:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_WIN32_KMT
driverSemHandleDesc.handle.win32.handle = semHandleDesc.handle.win32.handle
driverSemHandleDesc.handle.win32.name = semHandleDesc.handle.win32.name
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeD3D12Fence:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D12_FENCE
driverSemHandleDesc.handle.win32.handle = semHandleDesc.handle.win32.handle
driverSemHandleDesc.handle.win32.name = semHandleDesc.handle.win32.name
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeD3D11Fence:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_FENCE
driverSemHandleDesc.handle.win32.handle = semHandleDesc.handle.win32.handle
driverSemHandleDesc.handle.win32.name = semHandleDesc.handle.win32.name
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeNvSciSync:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_NVSCISYNC
driverSemHandleDesc.handle.nvSciSyncObj = semHandleDesc.handle.nvSciSyncObj
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeKeyedMutex:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX
driverSemHandleDesc.handle.win32.handle = semHandleDesc.handle.win32.handle
driverSemHandleDesc.handle.win32.name = semHandleDesc.handle.win32.name
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeKeyedMutexKmt:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_D3D11_KEYED_MUTEX_KMT
driverSemHandleDesc.handle.win32.handle = semHandleDesc.handle.win32.handle
driverSemHandleDesc.handle.win32.name = semHandleDesc.handle.win32.name
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeTimelineSemaphoreFd:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_FD
driverSemHandleDesc.handle.fd = semHandleDesc.handle.fd
elif semHandleDesc.type == cudaExternalSemaphoreHandleType.cudaExternalSemaphoreHandleTypeTimelineSemaphoreWin32:
driverSemHandleDesc.type = ccuda.CUexternalSemaphoreHandleType_enum.CU_EXTERNAL_SEMAPHORE_HANDLE_TYPE_TIMELINE_SEMAPHORE_WIN32
driverSemHandleDesc.handle.win32.handle = semHandleDesc.handle.win32.handle
driverSemHandleDesc.handle.win32.name = semHandleDesc.handle.win32.name
driverSemHandleDesc.flags = semHandleDesc.flags
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuImportExternalSemaphore(<ccuda.CUexternalSemaphore *>extSem_out, &driverSemHandleDesc)
if err != <cudaError_t>cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaSignalExternalSemaphoresAsync(const cudaExternalSemaphore_t* extSemArray, const cudaExternalSemaphoreSignalParams* paramsArray, unsigned int numExtSems, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuSignalExternalSemaphoresAsync(<const ccuda.CUexternalSemaphore *>extSemArray, <ccuda.CUDA_EXTERNAL_SEMAPHORE_SIGNAL_PARAMS *>paramsArray, numExtSems, stream)
if err != cudaSuccess:
_setLastError(err)
return err
return cudaSuccess
cdef cudaError_t _cudaWaitExternalSemaphoresAsync(const cudaExternalSemaphore_t* extSemArray, const cudaExternalSemaphoreWaitParams* paramsArray, unsigned int numExtSems, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = <cudaError_t>ccuda._cuWaitExternalSemaphoresAsync(<const ccuda.CUexternalSemaphore *>extSemArray, <ccuda.CUDA_EXTERNAL_SEMAPHORE_WAIT_PARAMS *>paramsArray, numExtSems, stream)
if err != cudaSuccess:
_setLastError(err)
return err
return cudaSuccess
cdef cudaError_t _cudaArrayGetInfo(cudaChannelFormatDesc* desc, cudaExtent* extent, unsigned int* flags, cudaArray_t array) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
cdef ccuda.CUDA_ARRAY3D_DESCRIPTOR_v2 driverDesc
cdef size_t width = 0
cdef size_t height = 0
cdef size_t depth = 0
# Zero out parameters in case cuArray3DGetDescriptor fails
if flags:
flags[0] = 0
if desc:
memset(desc, 0, sizeof(desc[0]))
if extent:
memset(extent, 0, sizeof(extent[0]))
err = <cudaError_t>ccuda._cuArray3DGetDescriptor_v2(&driverDesc, <ccuda.CUarray>array)
if err != <cudaError_t>cudaSuccess:
_setLastError(err)
return err
# Flags are copied directly from the driver API
if flags:
flags[0] = driverDesc.Flags
# Convert from driver API types to runtime API types. extent.Depth = 0
# indicates a 2D array.
if desc:
width = 0
height = 0
depth = 0
err = getChannelFormatDescFromDriverDesc(desc, &depth, &height, &width, &driverDesc)
if err != <cudaError_t>cudaSuccess:
_setLastError(err)
return err
if extent:
extent.width = driverDesc.Width
extent.height = driverDesc.Height
extent.depth = driverDesc.Depth
return cudaSuccess
cdef cudaError_t _cudaMemcpy2DToArray(cudaArray_t dst, size_t wOffset, size_t hOffset, const void* src, size_t spitch, size_t width, size_t height, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy2DToArray(dst, hOffset, wOffset, <const char*>src, spitch, width, height, kind, NULL, False)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpy2DFromArray(void* dst, size_t dpitch, cudaArray_const_t src, size_t wOffset, size_t hOffset, size_t width, size_t height, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy2DFromArray(<char*>dst, dpitch, src, hOffset, wOffset, width, height, kind, NULL, False)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpy2DArrayToArray(cudaArray_t dst, size_t wOffsetDst, size_t hOffsetDst, cudaArray_const_t src, size_t wOffsetSrc, size_t hOffsetSrc, size_t width, size_t height, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy2DArrayToArray(dst, hOffsetDst, wOffsetDst, src, hOffsetSrc, wOffsetSrc, width, height, kind)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpy2DToArrayAsync(cudaArray_t dst, size_t wOffset, size_t hOffset, const void* src, size_t spitch, size_t width, size_t height, cudaMemcpyKind kind, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy2DToArray(dst, hOffset, wOffset, <const char*>src, spitch, width, height, kind, stream, True)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpy2DFromArrayAsync(void* dst, size_t dpitch, cudaArray_const_t src, size_t wOffset, size_t hOffset, size_t width, size_t height, cudaMemcpyKind kind, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy2DFromArray(<char*>dst, dpitch, src, hOffset, wOffset, width, height, kind, stream, True)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemset3D(cudaPitchedPtr pitchedDevPtr, int value, cudaExtent extent) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memset3DPtr(pitchedDevPtr, value, extent, NULL, False)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemset3DAsync(cudaPitchedPtr pitchedDevPtr, int value, cudaExtent extent, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memset3DPtr(pitchedDevPtr, value, extent, stream, True)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpyToArray(cudaArray_t dst, size_t wOffset, size_t hOffset, const void* src, size_t count, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpyToArray(dst, hOffset, wOffset, <const char*>src, count, kind, NULL, False)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpyFromArray(void* dst, cudaArray_const_t src, size_t wOffset, size_t hOffset, size_t count, cudaMemcpyKind kind) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpyFromArray(<char*>dst, src, hOffset, wOffset, count, kind, NULL, 0)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpyToArrayAsync(cudaArray_t dst, size_t wOffset, size_t hOffset, const void* src, size_t count, cudaMemcpyKind kind, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpyToArray(dst, hOffset, wOffset, <const char*>src, count, kind, stream, True)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpyFromArrayAsync(void* dst, cudaArray_const_t src, size_t wOffset, size_t hOffset, size_t count, cudaMemcpyKind kind, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpyFromArray(<char*>dst, src, hOffset, wOffset, count, kind, stream, True)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaPointerGetAttributes(cudaPointerAttributes* attributes, const void* ptr) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
cdef cudaPointerAttributes attrib
cdef ccuda.CUcontext driverContext = NULL
cdef ccuda.CUmemorytype driverMemoryType
cdef int isManaged
cdef ccuda.CUpointer_attribute[6] query
query[0] = ccuda.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_CONTEXT
query[1] = ccuda.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_MEMORY_TYPE
query[2] = ccuda.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_DEVICE_POINTER
query[3] = ccuda.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_HOST_POINTER
query[4] = ccuda.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_IS_MANAGED
query[5] = ccuda.CUpointer_attribute_enum.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL
cdef void** data = [
&driverContext,
&driverMemoryType,
&attrib.devicePointer,
&attrib.hostPointer,
&isManaged,
&attrib.device
]
if attributes == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
# Get all the attributes we need
err = <cudaError_t>ccuda._cuPointerGetAttributes(<unsigned int>(sizeof(query)/sizeof(query[0])), query, data, <ccuda.CUdeviceptr_v2>ptr)
if err != cudaSuccess:
if attributes != NULL:
memset(attributes, 0, sizeof(attributes[0]))
attributes[0].device = -1
_setLastError(err)
return err
if driverMemoryType == ccuda.CUmemorytype_enum.CU_MEMORYTYPE_HOST:
if isManaged:
attrib.type = cudaMemoryTypeManaged
else:
attrib.type = cudaMemoryTypeHost
elif driverMemoryType == ccuda.CUmemorytype_enum.CU_MEMORYTYPE_DEVICE:
if isManaged:
attrib.type = cudaMemoryTypeManaged
else:
attrib.type = cudaMemoryTypeDevice
else:
if driverMemoryType == 0:
attrib.type = cudaMemoryTypeUnregistered
else:
if attributes != NULL:
memset(attributes, 0, sizeof(attributes[0]))
attributes[0].device = -1
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
# copy to user structure
attributes[0] = attrib
return cudaSuccess
cdef cudaError_t _cudaGetDeviceFlags(unsigned int* flags) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
if flags == NULL:
_setLastError(cudaErrorInvalidValue)
return cudaErrorInvalidValue
cdef ccuda.CUcontext driverContext
err = <cudaError_t>ccuda._cuCtxGetCurrent(&driverContext)
if err != cudaSuccess:
_setLastError(err)
return err
# Get the flags from the current context
if driverContext != NULL:
err = <cudaError_t>ccuda._cuCtxGetFlags(flags)
if err != cudaSuccess:
_setLastError(err)
return err
# Assume first valid device and get its implicit flags
cdef ccuda.CUdevice dev
cdef unsigned int pcFlags
cdef int pcActive
err = <cudaError_t>ccuda._cuDeviceGet(&dev, 0)
if err != cudaSuccess:
_setLastError(err)
return err
err = <cudaError_t>ccuda._cuDevicePrimaryCtxGetState(dev, &pcFlags, &pcActive)
if err != cudaSuccess:
_setLastError(err)
return err
flags[0] = pcFlags | cudaDeviceMapHost
return cudaSuccess
cdef cudaError_t _cudaMemcpy3D(const cudaMemcpy3DParms* p) nogil except ?cudaErrorCallRequiresNewerDriver:
if p == NULL:
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy3D(p, False, 0, 0, NULL, False)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemcpy3DAsync(const cudaMemcpy3DParms* p, cudaStream_t stream) nogil except ?cudaErrorCallRequiresNewerDriver:
if p == NULL:
return cudaErrorInvalidValue
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
err = memcpy3D(p, False, 0, 0, stream, True)
if err != cudaSuccess:
_setLastError(err)
return err
cdef cudaError_t _cudaMemPoolSetAccess(cudaMemPool_t memPool, const cudaMemAccessDesc* descList, size_t count) nogil except ?cudaErrorCallRequiresNewerDriver:
cdef cudaError_t err
err = m_global.lazyInit()
if err != cudaSuccess:
return err
cdef size_t MAX_DEVICES = 32
cdef ccuda.CUmemAccessDesc localList[32]
cdef ccuda.CUmemAccessDesc *cuDescList
cdef size_t i = 0
if (count > MAX_DEVICES):
cuDescList = <ccuda.CUmemAccessDesc*>calloc(sizeof(ccuda.CUmemAccessDesc), count)
else:
cuDescList = localList
if cuDescList == NULL:
_setLastError(cudaErrorMemoryAllocation)
return cudaErrorMemoryAllocation
while i < count:
cuDescList[i].location.type = <ccuda.CUmemLocationType>descList[i].location.type
cuDescList[i].location.id = descList[i].location.id
cuDescList[i].flags = <ccuda.CUmemAccess_flags>descList[i].flags
i += 1
err = <cudaError_t>ccuda._cuMemPoolSetAccess(memPool, cuDescList, count)
if err != cudaSuccess:
_setLastError(err)
return err
if count > MAX_DEVICES:
free(cuDescList)
return cudaSuccess
cdef cudaError_t _cudaDeviceReset() nogil except ?cudaErrorCallRequiresNewerDriver:
cdef int deviceOrdinal = 0
cdef cudaError_t err
if not m_global._cudaPythonInit:
return cudaSuccess