diff --git a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx index f50472758b9..1f79f1f5885 100644 --- a/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx +++ b/cuda_core/cuda/core/_memory/_managed_memory_resource.pyx @@ -4,12 +4,11 @@ from __future__ import annotations -from cuda.bindings cimport cydriver +from cuda.bindings cimport cydriver # no-cython-lint from cuda.core._memory._memory_pool cimport _MemPool, _MP_allocate, MP_check_open from cuda.core._memory._memory_pool cimport MP_init_create_pool, MP_init_current_pool # no-cython-lint from cuda.core._stream cimport Stream, Stream_accept -from cuda.core._utils.cuda_utils cimport HANDLE_RETURN from cuda.core._utils.cuda_utils cimport check_or_create_options # no-cython-lint from cuda.core._utils.cuda_utils import CUDAError # no-cython-lint @@ -18,6 +17,7 @@ import threading from typing import TYPE_CHECKING import warnings +from cuda.core._device import Device from cuda.core._memory._managed_buffer import ManagedBuffer from cuda.core.typing import ManagedMemoryLocationType @@ -308,19 +308,13 @@ cdef inline _check_concurrent_managed_access(): if _concurrent_access_warned: return - cdef int c_concurrent = 0 with _concurrent_access_lock: if _concurrent_access_warned: return # concurrent_managed_access is a system-level attribute for sm_60 and - # later, so any device will do. - with nogil: - HANDLE_RETURN(cydriver.cuDeviceGetAttribute( - &c_concurrent, - cydriver.CUdevice_attribute.CU_DEVICE_ATTRIBUTE_CONCURRENT_MANAGED_ACCESS, - 0)) - if not c_concurrent: + # later, so any device will do. This runs once per process. + if not Device(0).properties.concurrent_managed_access: warnings.warn( "This platform does not support concurrent managed memory access " "(Device.properties.concurrent_managed_access is False). Host access to any managed " diff --git a/cuda_core/tests/test_managed_memory_warning.py b/cuda_core/tests/test_managed_memory_warning.py index 60e99439b26..0639b1ee35d 100644 --- a/cuda_core/tests/test_managed_memory_warning.py +++ b/cuda_core/tests/test_managed_memory_warning.py @@ -5,7 +5,8 @@ Test that a warning is emitted when ManagedMemoryResource is created on a platform without concurrent managed memory access. -These tests only run on affected platforms (concurrent_managed_access is False). +No CI platform supports managed memory pools without concurrent managed access, +so the warning tests make Device.properties report no concurrent managed access. """ import sys @@ -50,6 +51,44 @@ def device_without_concurrent_managed_access(init_cuda): return device +class _PropertiesWithoutConcurrentManagedAccess: + """Device properties that report no concurrent managed access and delegate everything else.""" + + concurrent_managed_access = False + + def __init__(self, properties): + self._properties = properties + + def __getattr__(self, name): + return getattr(self._properties, name) + + +@pytest.fixture +def faked_no_concurrent_managed_access(init_cuda, monkeypatch): + """Make the warning check see no concurrent managed access, on any device with memory pools. + + Only the property value is faked: creating the resource, the warning and + its once-per-process lock run for real. + """ + device = Device() + device.set_current() + + if not device.properties.memory_pools_supported: + pytest.skip("Device does not support memory pools") + + properties = Device.properties + monkeypatch.setattr( + Device, + "properties", + property(lambda self: _PropertiesWithoutConcurrentManagedAccess(properties.__get__(self))), + ) + reset_concurrent_access_warning() + try: + yield device + finally: + reset_concurrent_access_warning() + + @requires_cuda_13 def test_default_pool_error_without_concurrent_access(device_without_concurrent_managed_access): """ManagedMemoryResource() raises RuntimeError when the default pool doesn't support managed.""" @@ -65,12 +104,11 @@ def test_default_pool_error_without_concurrent_access(device_without_concurrent_ raise -# This test is not currently run in any CI job @requires_cuda_13 -def test_warning_emitted(device_without_concurrent_managed_access): +@pytest.mark.thread_unsafe(reason="the patched Device.properties and the warning flag are process-wide") +def test_warning_emitted(faked_no_concurrent_managed_access): """ManagedMemoryResource emits a warning when concurrent managed access is unsupported.""" - dev_id = device_without_concurrent_managed_access.device_id - reset_concurrent_access_warning() + dev_id = faked_no_concurrent_managed_access.device_id with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") @@ -86,12 +124,11 @@ def test_warning_emitted(device_without_concurrent_managed_access): mr.close() -# This test is not currently run in any CI job @requires_cuda_13 -def test_warning_emitted_only_once(device_without_concurrent_managed_access): +@pytest.mark.thread_unsafe(reason="the patched Device.properties and the warning flag are process-wide") +def test_warning_emitted_only_once(faked_no_concurrent_managed_access): """Warning fires only once even when multiple ManagedMemoryResources are created.""" - dev_id = device_without_concurrent_managed_access.device_id - reset_concurrent_access_warning() + dev_id = faked_no_concurrent_managed_access.device_id with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always")