Skip to content

Commit e3ff57c

Browse files
authored
cuda.core: minor refactoring to prepare for copy with options (#2618)
* cuda.core: minor refactoring to prepare for copy with options * inline capability check helper
1 parent 4736d4a commit e3ff57c

4 files changed

Lines changed: 52 additions & 22 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# Neutral leaf module: declares the CopyOptions-to-CUmemcpyAttributes converter
6+
# and the 13.2 availability gate so both _buffer and _copy_ops can cimport them
7+
# without either depending on the other.
8+
9+
from cuda.bindings cimport cydriver
10+
from cuda.core._utils.version cimport cy_binding_version, cy_driver_version # no-cython-lint
11+
12+
13+
IF CUDA_CORE_BUILD_MAJOR >= 13:
14+
cdef inline bint _with_attributes_available():
15+
return cy_driver_version() >= (13, 2, 0) and cy_binding_version() >= (13, 2, 0)
16+
ELSE:
17+
cdef inline bint _with_attributes_available():
18+
return False
19+
20+
cdef cydriver.CUmemcpyAttributes _to_cu_memcpy_attributes(object attr)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# This file was generated by stubgen-pyx v0.2.6 from cuda_core/cuda/core/_memory/_copy_attributes.pyx
2+
3+
from __future__ import annotations
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
from libc.string cimport memset
6+
7+
from cuda.bindings cimport cydriver
8+
from cuda.core._memory._location cimport to_cumemlocation
9+
10+
from cuda.core._memory._managed_location import _coerce_location
11+
12+
13+
cdef cydriver.CUmemcpyAttributes _to_cu_memcpy_attributes(object attr):
14+
"""Convert a CopyOptions to a cydriver.CUmemcpyAttributes struct."""
15+
cdef cydriver.CUmemcpyAttributes cu_attr
16+
memset(&cu_attr, 0, sizeof(cydriver.CUmemcpyAttributes))
17+
cu_attr.srcAccessOrder = <cydriver.CUmemcpySrcAccessOrder>(<int>attr._to_driver_enum())
18+
cu_attr.flags = <unsigned int>(<int>attr._to_driver_flags())
19+
20+
cdef object src_loc = _coerce_location(attr.src_location_hint, allow_none=True)
21+
cdef object dst_loc = _coerce_location(attr.dst_location_hint, allow_none=True)
22+
23+
if src_loc is not None:
24+
cu_attr.srcLocHint = to_cumemlocation(src_loc.kind, src_loc.id)
25+
if dst_loc is not None:
26+
cu_attr.dstLocHint = to_cumemlocation(dst_loc.kind, dst_loc.id)
27+
28+
return cu_attr

cuda_core/cuda/core/_memory/_copy_ops.pyx

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ from collections.abc import Sequence
99
IF CUDA_CORE_BUILD_MAJOR >= 13:
1010
from libcpp.vector cimport vector
1111

12-
from libc.string cimport memset
13-
1412
from cuda.bindings cimport cydriver
1513
from cuda.core._memory._buffer cimport Buffer, Buffer_coerce_batch
16-
from cuda.core._memory._location cimport to_cumemlocation
14+
from cuda.core._memory._copy_attributes cimport _to_cu_memcpy_attributes # no-cython-lint
1715
from cuda.core._resource_handles cimport as_cu
1816
from cuda.core._stream cimport Stream, Stream_accept, Stream_is_default_token
1917
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
@@ -24,7 +22,6 @@ from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2422
from cuda.core._utils.version cimport cy_driver_version # no-cython-lint
2523

2624
from cuda.core._memory._copy_enums import CopyOptions, _attr_run_starts # no-cython-lint
27-
from cuda.core._memory._managed_location import _coerce_location
2825

2926
_SINGLE_COPY_HINT = "Buffer.copy_to / Buffer.copy_from"
3027

@@ -87,24 +84,6 @@ def _normalize_copy_options(
8784
)
8885

8986

90-
cdef cydriver.CUmemcpyAttributes _to_cu_memcpy_attributes(object attr):
91-
"""Convert a CopyOptions to a cydriver.CUmemcpyAttributes struct."""
92-
cdef cydriver.CUmemcpyAttributes cu_attr
93-
memset(&cu_attr, 0, sizeof(cydriver.CUmemcpyAttributes))
94-
cu_attr.srcAccessOrder = <cydriver.CUmemcpySrcAccessOrder>(<int>attr._to_driver_enum())
95-
cu_attr.flags = <unsigned int>(<int>attr._to_driver_flags())
96-
97-
cdef object src_loc = _coerce_location(attr.src_location_hint, allow_none=True)
98-
cdef object dst_loc = _coerce_location(attr.dst_location_hint, allow_none=True)
99-
100-
if src_loc is not None:
101-
cu_attr.srcLocHint = to_cumemlocation(src_loc.kind, src_loc.id)
102-
if dst_loc is not None:
103-
cu_attr.dstLocHint = to_cumemlocation(dst_loc.kind, dst_loc.id)
104-
105-
return cu_attr
106-
107-
10887
def copy_batch(
10988
stream: Stream,
11089
srcs: Sequence[Buffer],

0 commit comments

Comments
 (0)