From 687bc7ec6abccc2774b36caafaf77b9262795abd Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Fri, 10 Jul 2026 11:06:15 -0700 Subject: [PATCH 1/2] [chore] Remove unecessary imports and declarations from some pxd files --- cuda_core/cuda/core/_memory/_buffer.pxd | 12 +- cuda_core/cuda/core/_memory/_buffer.pyx | 133 +++++++++--------- .../core/_memory/_device_memory_resource.pxd | 1 - cuda_core/cuda/core/_memory/_memory_pool.pxd | 10 -- cuda_core/cuda/core/_memory/_memory_pool.pyx | 2 + .../core/_memory/_pinned_memory_resource.pxd | 3 +- 6 files changed, 70 insertions(+), 91 deletions(-) diff --git a/cuda_core/cuda/core/_memory/_buffer.pxd b/cuda_core/cuda/core/_memory/_buffer.pxd index 83dcd4f68c2..b552e69554d 100644 --- a/cuda_core/cuda/core/_memory/_buffer.pxd +++ b/cuda_core/cuda/core/_memory/_buffer.pxd @@ -2,13 +2,10 @@ # # SPDX-License-Identifier: Apache-2.0 -from libc.stdint cimport uintptr_t from libcpp cimport bool as cpp_bool -from libcpp.atomic cimport atomic as std_atomic, memory_order_acquire, memory_order_release +from libcpp.atomic cimport atomic as std_atomic -from cuda.bindings cimport cydriver from cuda.core._resource_handles cimport DevicePtrHandle -from cuda.core._stream cimport Stream cdef struct _MemAttrs: @@ -47,10 +44,3 @@ cdef Buffer Buffer_from_deviceptr_handle( object ipc_descriptor = *, type cls = *, ) - -# Memory attribute query helpers (used by _managed_memory_ops) -cdef void _init_mem_attrs(Buffer self) -cdef int _query_memory_attrs( - _MemAttrs& out, - cydriver.CUdeviceptr ptr, -) except -1 nogil diff --git a/cuda_core/cuda/core/_memory/_buffer.pyx b/cuda_core/cuda/core/_memory/_buffer.pyx index f9b79bfdcc7..1e4d4df6dfc 100644 --- a/cuda_core/cuda/core/_memory/_buffer.pyx +++ b/cuda_core/cuda/core/_memory/_buffer.pyx @@ -6,6 +6,7 @@ from __future__ import annotations cimport cython from libc.stdint cimport uintptr_t +from libcpp.atomic cimport memory_order_acquire, memory_order_release from cuda.bindings cimport cydriver from cuda.core._memory._device_memory_resource import DeviceMemoryResource @@ -76,6 +77,67 @@ register_mr_dealloc_callback(_mr_dealloc_callback) __all__ = ['Buffer', 'MemoryResource'] +# Memory Attribute Query Helpers +# ------------------------------ +cdef inline int _query_memory_attrs( + _MemAttrs& out, + cydriver.CUdeviceptr ptr +) except -1 nogil: + """Query memory attributes for a device pointer.""" + cdef unsigned int memory_type = 0 + cdef int is_managed = 0 + cdef int device_id = 0 + cdef cydriver.CUpointer_attribute attrs[3] + cdef uintptr_t vals[3] + + attrs[0] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_MEMORY_TYPE + attrs[1] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_IS_MANAGED + attrs[2] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL + vals[0] = &memory_type + vals[1] = &is_managed + vals[2] = &device_id + + cdef cydriver.CUresult ret + ret = cydriver.cuPointerGetAttributes(3, attrs, vals, ptr) + if ret == cydriver.CUresult.CUDA_ERROR_NOT_INITIALIZED: + with cython.gil: + # Device class handles the cuInit call internally + Device() + ret = cydriver.cuPointerGetAttributes(3, attrs, vals, ptr) + HANDLE_RETURN(ret) + + # TODO: HMM/ATS-enabled sysmem should also report is_managed=True; the + # CU_POINTER_ATTRIBUTE_IS_MANAGED query does not capture that yet. + out.is_managed = is_managed != 0 + + if memory_type == 0: + # unregistered host pointer + out.is_host_accessible = True + out.is_device_accessible = False + out.device_id = -1 + elif ( + is_managed + or memory_type == cydriver.CUmemorytype.CU_MEMORYTYPE_HOST + ): + # Managed memory or pinned host memory + out.is_host_accessible = True + out.is_device_accessible = True + out.device_id = device_id + elif memory_type == cydriver.CUmemorytype.CU_MEMORYTYPE_DEVICE: + out.is_host_accessible = False + out.is_device_accessible = True + out.device_id = device_id + else: + with cython.gil: + raise ValueError(f"Unsupported memory type: {memory_type}") + return 0 + + +cdef inline void _init_memory_attrs(Buffer self): + """Initialize memory attributes by querying the pointer.""" + if not self._mem_attrs_inited.load(memory_order_acquire): + _query_memory_attrs(self._mem_attrs, as_cu(self._h_ptr)) + self._mem_attrs_inited.store(True, memory_order_release) cdef class Buffer: @@ -381,7 +443,7 @@ cdef class Buffer: """Return the device ordinal of this buffer.""" if self._memory_resource is not None: return self._memory_resource.device_id - _init_mem_attrs(self) + _init_memory_attrs(self) return self._mem_attrs.device_id @property @@ -416,7 +478,7 @@ cdef class Buffer: """Return True if this buffer can be accessed by the GPU, otherwise False.""" if self._memory_resource is not None: return self._memory_resource.is_device_accessible - _init_mem_attrs(self) + _init_memory_attrs(self) return self._mem_attrs.is_device_accessible @property @@ -424,13 +486,13 @@ cdef class Buffer: """Return True if this buffer can be accessed by the CPU, otherwise False.""" if self._memory_resource is not None: return self._memory_resource.is_host_accessible - _init_mem_attrs(self) + _init_memory_attrs(self) return self._mem_attrs.is_host_accessible @property def is_managed(self) -> bool: """Return True if this buffer is CUDA managed (unified) memory, otherwise False.""" - _init_mem_attrs(self) + _init_memory_attrs(self) if self._mem_attrs.is_managed: return True # Pool-allocated managed memory does not set CU_POINTER_ATTRIBUTE_IS_MANAGED, @@ -459,69 +521,6 @@ cdef class Buffer: return self._owner -# Memory Attribute Query Helpers -# ------------------------------ -cdef inline void _init_mem_attrs(Buffer self): - """Initialize memory attributes by querying the pointer.""" - if not self._mem_attrs_inited.load(memory_order_acquire): - _query_memory_attrs(self._mem_attrs, as_cu(self._h_ptr)) - self._mem_attrs_inited.store(True, memory_order_release) - - -cdef inline int _query_memory_attrs( - _MemAttrs& out, - cydriver.CUdeviceptr ptr -) except -1 nogil: - """Query memory attributes for a device pointer.""" - cdef unsigned int memory_type = 0 - cdef int is_managed = 0 - cdef int device_id = 0 - cdef cydriver.CUpointer_attribute attrs[3] - cdef uintptr_t vals[3] - - attrs[0] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_MEMORY_TYPE - attrs[1] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_IS_MANAGED - attrs[2] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL - vals[0] = &memory_type - vals[1] = &is_managed - vals[2] = &device_id - - cdef cydriver.CUresult ret - ret = cydriver.cuPointerGetAttributes(3, attrs, vals, ptr) - if ret == cydriver.CUresult.CUDA_ERROR_NOT_INITIALIZED: - with cython.gil: - # Device class handles the cuInit call internally - Device() - ret = cydriver.cuPointerGetAttributes(3, attrs, vals, ptr) - HANDLE_RETURN(ret) - - # TODO: HMM/ATS-enabled sysmem should also report is_managed=True; the - # CU_POINTER_ATTRIBUTE_IS_MANAGED query does not capture that yet. - out.is_managed = is_managed != 0 - - if memory_type == 0: - # unregistered host pointer - out.is_host_accessible = True - out.is_device_accessible = False - out.device_id = -1 - elif ( - is_managed - or memory_type == cydriver.CUmemorytype.CU_MEMORYTYPE_HOST - ): - # Managed memory or pinned host memory - out.is_host_accessible = True - out.is_device_accessible = True - out.device_id = device_id - elif memory_type == cydriver.CUmemorytype.CU_MEMORYTYPE_DEVICE: - out.is_host_accessible = False - out.is_device_accessible = True - out.device_id = device_id - else: - with cython.gil: - raise ValueError(f"Unsupported memory type: {memory_type}") - return 0 - - cdef class MemoryResource: """Abstract base class for memory resources that manage allocation and deallocation of buffers. diff --git a/cuda_core/cuda/core/_memory/_device_memory_resource.pxd b/cuda_core/cuda/core/_memory/_device_memory_resource.pxd index 47e626e2435..1c3134876eb 100644 --- a/cuda_core/cuda/core/_memory/_device_memory_resource.pxd +++ b/cuda_core/cuda/core/_memory/_device_memory_resource.pxd @@ -3,7 +3,6 @@ # SPDX-License-Identifier: Apache-2.0 from cuda.core._memory._memory_pool cimport _MemPool -from cuda.core._memory._ipc cimport IPCDataForMR cdef class DeviceMemoryResource(_MemPool): diff --git a/cuda_core/cuda/core/_memory/_memory_pool.pxd b/cuda_core/cuda/core/_memory/_memory_pool.pxd index aa9cf833da3..3a6c3107cfd 100644 --- a/cuda_core/cuda/core/_memory/_memory_pool.pxd +++ b/cuda_core/cuda/core/_memory/_memory_pool.pxd @@ -41,13 +41,3 @@ cdef int MP_raise_release_threshold(_MemPool self) except? -1 # Buffer). Subclasses (e.g. ManagedMemoryResource) pass their own buffer # subclass so their `allocate` returns the typed object. cdef Buffer _MP_allocate(_MemPool self, size_t size, Stream stream, type cls = *) - - -cdef class _MemPoolAttributes: - cdef: - MemoryPoolHandle _h_pool - - @staticmethod - cdef _MemPoolAttributes _init(MemoryPoolHandle h_pool) - - cdef int _getattribute(self, cydriver.CUmemPool_attribute attr_enum, void* value) except? -1 diff --git a/cuda_core/cuda/core/_memory/_memory_pool.pyx b/cuda_core/cuda/core/_memory/_memory_pool.pyx index ddcac2d6063..cf7c48068f1 100644 --- a/cuda_core/cuda/core/_memory/_memory_pool.pyx +++ b/cuda_core/cuda/core/_memory/_memory_pool.pyx @@ -38,6 +38,8 @@ if TYPE_CHECKING: cdef class _MemPoolAttributes: """Provides access to memory pool attributes.""" + cdef: + MemoryPoolHandle _h_pool def __init__(self, *args, **kwargs) -> None: raise RuntimeError("_MemPoolAttributes cannot be instantiated directly. Please use MemoryResource APIs.") diff --git a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd index fcfcfeb3465..41336d4f61a 100644 --- a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd +++ b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd @@ -1,9 +1,8 @@ -# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # # SPDX-License-Identifier: Apache-2.0 from cuda.core._memory._memory_pool cimport _MemPool -from cuda.core._memory._ipc cimport IPCDataForMR cdef class PinnedMemoryResource(_MemPool): From 8e2c5449be71564c47476b724cdd52a067efbbda Mon Sep 17 00:00:00 2001 From: Ralf Juengling Date: Fri, 10 Jul 2026 15:18:09 -0700 Subject: [PATCH 2/2] Fix deprecated array syntax --- cuda_core/cuda/core/_memory/_buffer.pyx | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/cuda_core/cuda/core/_memory/_buffer.pyx b/cuda_core/cuda/core/_memory/_buffer.pyx index 1e4d4df6dfc..97ef892547d 100644 --- a/cuda_core/cuda/core/_memory/_buffer.pyx +++ b/cuda_core/cuda/core/_memory/_buffer.pyx @@ -87,15 +87,16 @@ cdef inline int _query_memory_attrs( cdef unsigned int memory_type = 0 cdef int is_managed = 0 cdef int device_id = 0 - cdef cydriver.CUpointer_attribute attrs[3] - cdef uintptr_t vals[3] - - attrs[0] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_MEMORY_TYPE - attrs[1] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_IS_MANAGED - attrs[2] = cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL - vals[0] = &memory_type - vals[1] = &is_managed - vals[2] = &device_id + cdef cydriver.CUpointer_attribute[3] attrs = [ + cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_MEMORY_TYPE, + cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_IS_MANAGED, + cydriver.CUpointer_attribute.CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL, + ] + cdef uintptr_t[3] vals = [ + &memory_type, + &is_managed, + &device_id, + ] cdef cydriver.CUresult ret ret = cydriver.cuPointerGetAttributes(3, attrs, vals, ptr)