Skip to content

Commit b448d2f

Browse files
authored
PERF: Inline return code checks in cuda_core (#2608)
1 parent 21c4b70 commit b448d2f

2 files changed

Lines changed: 32 additions & 37 deletions

File tree

cuda_core/cuda/core/_utils/cuda_utils.pxd

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,35 @@ ctypedef fused integer_t:
1818
cdef const cydriver.CUcontext CU_CONTEXT_INVALID = <cydriver.CUcontext>(-2)
1919

2020

21-
cdef int HANDLE_RETURN(cydriver.CUresult err) except?-1 nogil
22-
cdef int HANDLE_RETURN_NVRTC(cynvrtc.nvrtcProgram prog, cynvrtc.nvrtcResult err) except?-1 nogil
23-
cdef int HANDLE_RETURN_NVVM(cynvvm.nvvmProgram prog, cynvvm.nvvmResult err) except?-1 nogil
24-
cdef int HANDLE_RETURN_NVJITLINK(
25-
cynvjitlink.nvJitLinkHandle handle, cynvjitlink.nvJitLinkResult err) except?-1 nogil
21+
cdef inline int HANDLE_RETURN(cydriver.CUresult err) except?-1 nogil:
22+
if err != cydriver.CUresult.CUDA_SUCCESS:
23+
return _check_driver_error(err)
24+
return 0
25+
26+
27+
cdef inline int HANDLE_RETURN_NVRTC(cynvrtc.nvrtcProgram prog, cynvrtc.nvrtcResult err) except?-1 nogil:
28+
"""Handle NVRTC result codes, raising NVRTCError with program log on failure."""
29+
if err == cynvrtc.nvrtcResult.NVRTC_SUCCESS:
30+
return 0
31+
with gil:
32+
_raise_nvrtc_error(prog, err)
33+
34+
35+
cdef inline int HANDLE_RETURN_NVVM(cynvvm.nvvmProgram prog, cynvvm.nvvmResult err) except?-1 nogil:
36+
"""Handle NVVM result codes, raising nvvmError with program log on failure."""
37+
if err == cynvvm.nvvmResult.NVVM_SUCCESS:
38+
return 0
39+
with gil:
40+
_raise_nvvm_error(prog, err)
41+
42+
43+
cdef inline int HANDLE_RETURN_NVJITLINK(
44+
cynvjitlink.nvJitLinkHandle handle, cynvjitlink.nvJitLinkResult err) except?-1 nogil:
45+
"""Handle nvJitLink result codes, raising nvJitLinkError with error log on failure."""
46+
if err == cynvjitlink.nvJitLinkResult.NVJITLINK_SUCCESS:
47+
return 0
48+
with gil:
49+
_raise_nvjitlink_error(handle, err)
2650

2751

2852
# Helper for retrieving the current CUDA device. Raises if no active context
@@ -34,7 +58,9 @@ cdef int _get_current_device_id() except? -1
3458
cpdef int _check_driver_error(cydriver.CUresult error) except?-1 nogil
3559
cpdef int _check_runtime_error(error) except?-1
3660
cpdef int _check_nvrtc_error(error) except?-1
37-
61+
cdef int _raise_nvrtc_error(cynvrtc.nvrtcProgram prog, cynvrtc.nvrtcResult err) except -1
62+
cdef int _raise_nvvm_error(cynvvm.nvvmProgram prog, cynvvm.nvvmResult err) except -1
63+
cdef int _raise_nvjitlink_error(cynvjitlink.nvJitLinkHandle handle, cynvjitlink.nvJitLinkResult err) except -1
3864

3965
cpdef check_or_create_options(type cls, options, str options_description=*, bint keep_none=*)
4066

cuda_core/cuda/core/_utils/cuda_utils.pyx

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ def cast_to_3_tuple(label: str, cfg: int | tuple[int, ...]) -> tuple[int, int, i
6363
return cfg + (1,) * (3 - len(cfg))
6464

6565

66-
cdef int HANDLE_RETURN(cydriver.CUresult err) except?-1 nogil:
67-
if err != cydriver.CUresult.CUDA_SUCCESS:
68-
return _check_driver_error(err)
69-
return 0
70-
71-
7266
cdef int _get_current_device_id() except? -1:
7367
"""Return the current thread's bound CUdevice ordinal."""
7468
cdef cydriver.CUdevice dev
@@ -77,14 +71,6 @@ cdef int _get_current_device_id() except? -1:
7771
return <int>dev
7872

7973

80-
cdef int HANDLE_RETURN_NVRTC(cynvrtc.nvrtcProgram prog, cynvrtc.nvrtcResult err) except?-1 nogil:
81-
"""Handle NVRTC result codes, raising NVRTCError with program log on failure."""
82-
if err == cynvrtc.nvrtcResult.NVRTC_SUCCESS:
83-
return 0
84-
with gil:
85-
_raise_nvrtc_error(prog, err)
86-
87-
8874
cdef int _raise_nvrtc_error(cynvrtc.nvrtcProgram prog, cynvrtc.nvrtcResult err) except -1:
8975
"""Build error message with program log and raise NVRTCError."""
9076
cdef const char* err_str = cynvrtc.nvrtcGetErrorString(err)
@@ -103,14 +89,6 @@ cdef int _raise_nvrtc_error(cynvrtc.nvrtcProgram prog, cynvrtc.nvrtcResult err)
10389
raise NVRTCError(err_msg)
10490

10591

106-
cdef int HANDLE_RETURN_NVVM(cynvvm.nvvmProgram prog, cynvvm.nvvmResult err) except?-1 nogil:
107-
"""Handle NVVM result codes, raising nvvmError with program log on failure."""
108-
if err == cynvvm.nvvmResult.NVVM_SUCCESS:
109-
return 0
110-
with gil:
111-
_raise_nvvm_error(prog, err)
112-
113-
11492
cdef int _raise_nvvm_error(cynvvm.nvvmProgram prog, cynvvm.nvvmResult err) except -1:
11593
"""Raise nvvmError annotated with the program log."""
11694
cdef size_t logsize = 0
@@ -128,15 +106,6 @@ cdef int _raise_nvvm_error(cynvvm.nvvmProgram prog, cynvvm.nvvmResult err) excep
128106
raise exc
129107

130108

131-
cdef int HANDLE_RETURN_NVJITLINK(
132-
cynvjitlink.nvJitLinkHandle handle, cynvjitlink.nvJitLinkResult err) except?-1 nogil:
133-
"""Handle nvJitLink result codes, raising nvJitLinkError with error log on failure."""
134-
if err == cynvjitlink.nvJitLinkResult.NVJITLINK_SUCCESS:
135-
return 0
136-
with gil:
137-
_raise_nvjitlink_error(handle, err)
138-
139-
140109
cdef int _raise_nvjitlink_error(
141110
cynvjitlink.nvJitLinkHandle handle, cynvjitlink.nvJitLinkResult err) except -1:
142111
"""Raise nvJitLinkError annotated with the error log."""

0 commit comments

Comments
 (0)