diff --git a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd index 41336d4f61ad..a09c2fb46ed9 100644 --- a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd +++ b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pxd @@ -7,3 +7,5 @@ from cuda.core._memory._memory_pool cimport _MemPool cdef class PinnedMemoryResource(_MemPool): cdef int _numa_id + # False only for an imported pool whose node is unknown (see numa_id). + cdef bint _numa_id_known diff --git a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi index 9f9a7204fa81..bc01164db503 100644 --- a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi +++ b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyi @@ -145,7 +145,7 @@ class PinnedMemoryResource(_MemPool): If the memory resource already has a different UUID. """ @classmethod - def from_allocation_handle(cls, alloc_handle: int | IPCAllocationHandle) -> PinnedMemoryResource: + def from_allocation_handle(cls, alloc_handle: int | IPCAllocationHandle, numa_id: int | None=None) -> PinnedMemoryResource: """Create a host-pinned memory resource from an allocation handle. Construct a new `PinnedMemoryResource` instance that imports a memory @@ -159,6 +159,18 @@ class PinnedMemoryResource(_MemPool): The shareable handle of the host-pinned memory resource to import. If an integer is supplied, it must represent a valid platform-specific handle. It is the caller's responsibility to close that handle. + numa_id : int | None, optional + The host NUMA node the exporting process placed the pool on, that + is, the exporting resource's :attr:`numa_id`. Node IDs are the same + in every process on the host. The pool's node is read from the + driver when possible (a CUDA 13 build of ``cuda.core`` and a 13.2 + or later driver); otherwise this value is the imported resource's + :attr:`numa_id`. Unpickling a resource passes the exporter's node + automatically if it was known. If the handle's resource is already + registered in this process with an unknown node, this value becomes + its node. If the node comes from neither the driver nor this + argument, the import succeeds but :attr:`numa_id` raises + ``RuntimeError``. Must be >= 0 if given. Returns ------- @@ -172,6 +184,11 @@ class PinnedMemoryResource(_MemPool): The pool's kind is checked with a CUDA 13 build of ``cuda.core`` and a 13.2 or later driver. Otherwise it is checked only when the handle's memory resource is already registered in this process. + Also raised if ``numa_id`` is not an integer. + ValueError + If ``numa_id`` is negative or too large for a C ``int``, or + differs from the node of the pool, as reported by the driver or by + the resource already registered in this process for the handle. """ @property def allocation_handle(self) -> IPCAllocationHandle: @@ -187,10 +204,17 @@ class PinnedMemoryResource(_MemPool): def numa_id(self) -> int: """The host NUMA node ID used for pool placement, or -1 for OS-managed placement. - For a resource imported with :meth:`from_allocation_handle`, this is the - pool's node only if the pool's location can be queried (a CUDA 13 build - of ``cuda.core`` and a 13.2 or later driver). Otherwise it is 0, which is - not known to be the pool's node. + For a resource imported with :meth:`from_allocation_handle`, the node + comes from the driver if the pool's location can be queried (a CUDA 13 + build of ``cuda.core`` and a 13.2 or later driver), otherwise from the + ``numa_id`` argument of :meth:`from_allocation_handle`, which unpickling + passes automatically if the exporter's node was known. + + Raises + ------ + RuntimeError + If the resource was imported and none of these sources gave the + pool's node. """ @property def is_device_accessible(self) -> bool: diff --git a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx index 965f4df59a4e..d2d7acfe566e 100644 --- a/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx +++ b/cuda_core/cuda/core/_memory/_pinned_memory_resource.pyx @@ -4,6 +4,7 @@ from __future__ import annotations +cimport cython from cuda.bindings cimport cydriver from cuda.core._memory._buffer cimport Buffer from cuda.core._memory._memory_pool cimport ( @@ -22,10 +23,12 @@ from cuda.core._utils.cuda_utils cimport ( check_or_create_options, HANDLE_RETURN, ) +from libc.limits cimport INT_MAX from libcpp.vector cimport vector from dataclasses import dataclass import multiprocessing +import operator import platform # no-cython-lint import uuid @@ -198,7 +201,7 @@ cdef class PinnedMemoryResource(_MemPool): @classmethod def from_allocation_handle( - cls, alloc_handle: int | IPCAllocationHandle + cls, alloc_handle: int | IPCAllocationHandle, numa_id: int | None = None ) -> PinnedMemoryResource: """Create a host-pinned memory resource from an allocation handle. @@ -213,6 +216,18 @@ cdef class PinnedMemoryResource(_MemPool): The shareable handle of the host-pinned memory resource to import. If an integer is supplied, it must represent a valid platform-specific handle. It is the caller's responsibility to close that handle. + numa_id : int | None, optional + The host NUMA node the exporting process placed the pool on, that + is, the exporting resource's :attr:`numa_id`. Node IDs are the same + in every process on the host. The pool's node is read from the + driver when possible (a CUDA 13 build of ``cuda.core`` and a 13.2 + or later driver); otherwise this value is the imported resource's + :attr:`numa_id`. Unpickling a resource passes the exporter's node + automatically if it was known. If the handle's resource is already + registered in this process with an unknown node, this value becomes + its node. If the node comes from neither the driver nor this + argument, the import succeeds but :attr:`numa_id` raises + ``RuntimeError``. Must be >= 0 if given. Returns ------- @@ -226,7 +241,19 @@ cdef class PinnedMemoryResource(_MemPool): The pool's kind is checked with a CUDA 13 build of ``cuda.core`` and a 13.2 or later driver. Otherwise it is checked only when the handle's memory resource is already registered in this process. + Also raised if ``numa_id`` is not an integer. + ValueError + If ``numa_id`` is negative or too large for a C ``int``, or + differs from the node of the pool, as reported by the driver or by + the resource already registered in this process for the handle. """ + if numa_id is not None: + # Reject a float or other non-integer instead of truncating it later. + numa_id = operator.index(numa_id) + # The node is stored as a C int once the pool is imported. + if not 0 <= numa_id <= INT_MAX: + raise ValueError(f"numa_id must be between 0 and {INT_MAX}, got {numa_id}") + # cuMemPoolImportFromShareableHandle requires CUDA to be initialized, but in # a child process CUDA may not be initialized yet. For DeviceMemoryResource, # this is not a concern because most likely when retrieving the device_id the @@ -234,8 +261,12 @@ cdef class PinnedMemoryResource(_MemPool): # not device-specific it is unlikelt the case. HANDLE_RETURN(cydriver.cuInit(0)) - return ( - _ipc.MP_from_allocation_handle(cls, alloc_handle, _PMR_init_imported)) + cdef PinnedMemoryResource mr = ( + _ipc.MP_from_allocation_handle(cls, alloc_handle, _PMR_init_imported, numa_id)) + # The import hook does not run for a resource that is already registered, + # whether found at the start or registered by a concurrent import. + _PMR_apply_numa_id(mr, numa_id) + return mr @property def allocation_handle(self) -> IPCAllocationHandle: @@ -255,14 +286,27 @@ cdef class PinnedMemoryResource(_MemPool): return -1 @property + @cython.critical_section def numa_id(self) -> int: """The host NUMA node ID used for pool placement, or -1 for OS-managed placement. - For a resource imported with :meth:`from_allocation_handle`, this is the - pool's node only if the pool's location can be queried (a CUDA 13 build - of ``cuda.core`` and a 13.2 or later driver). Otherwise it is 0, which is - not known to be the pool's node. + For a resource imported with :meth:`from_allocation_handle`, the node + comes from the driver if the pool's location can be queried (a CUDA 13 + build of ``cuda.core`` and a 13.2 or later driver), otherwise from the + ``numa_id`` argument of :meth:`from_allocation_handle`, which unpickling + passes automatically if the exporter's node was known. + + Raises + ------ + RuntimeError + If the resource was imported and none of these sources gave the + pool's node. """ + if not self._numa_id_known: + raise RuntimeError( + "The NUMA node of this imported memory pool is not known in this " + "process. Pass numa_id to PinnedMemoryResource.from_allocation_handle " + "to name the node the exporting process placed the pool on.") return self._numa_id @property @@ -322,6 +366,7 @@ cdef inline _PMR_init(PinnedMemoryResource self, options): loc_type = cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST self._numa_id = numa_id + self._numa_id_known = True if opts is None: MP_init_current_pool( @@ -365,23 +410,60 @@ cdef inline int _PMR_grant_device_access( return 0 -cdef int _PMR_init_imported(_MemPool mr, object unused) except? -1: +cdef inline int _PMR_apply_numa_id(PinnedMemoryResource self, object numa_id) except? -1: + # A node the caller passed must agree with a node that is known. A resource + # whose node is unknown, such as one registered by an earlier import that + # was given no node, takes the caller's node. + # Concurrent imports of one handle share this resource, so on a free-threaded + # build the check and the write are one step. Otherwise two imports with + # different nodes could both find the node unknown and both succeed. + cdef int node + if numa_id is None: + return 0 + node = numa_id + with cython.critical_section(self): + if not self._numa_id_known: + self._numa_id = node + self._numa_id_known = True + elif self._numa_id != node: + raise ValueError( + f"numa_id {node} does not match the NUMA node of the memory pool, " + f"which is {self._numa_id}") + return 0 + + +cdef int _PMR_init_imported(_MemPool mr, object numa_id) except? -1: cdef PinnedMemoryResource self = mr cdef cydriver.CUmemLocation loc # IPC-enabled pinned pools created by cuda.core are always placed on a host # NUMA node. Without a location query (a CUDA 12 build or a driver before - # 13.2), the pool's kind is not checked and numa_id keeps its default. + # 13.2), the pool's kind is not checked and the node is the one the caller + # passed, if any. Otherwise it is unknown and the numa_id property raises. cdef bint host_numa = True if _ipc.MP_check_import_location(self, True, &loc): host_numa = loc.type == cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST_NUMA self._numa_id = loc.id if host_numa else -1 + self._numa_id_known = True + _PMR_apply_numa_id(self, numa_id) + elif numa_id is not None: + self._numa_id = numa_id + self._numa_id_known = True return _PMR_grant_device_access(self, host_numa, True) def _deep_reduce_pinned_memory_resource(mr: object) -> tuple[object, ...]: check_multiprocessing_start_method() - alloc_handle = mr.allocation_handle - return mr.from_allocation_handle, (alloc_handle,) + cdef PinnedMemoryResource pmr = mr + alloc_handle = pmr.allocation_handle + # NUMA node IDs are the same in every process on the host, so the node needs + # no translation as a device ordinal does. An unknown node is not sent, so + # that the importer does not mistake a placeholder for the pool's node, and + # neither is -1, which from_allocation_handle rejects. + numa_id = None + with cython.critical_section(pmr): + if pmr._numa_id_known and pmr._numa_id >= 0: + numa_id = pmr._numa_id + return pmr.from_allocation_handle, (alloc_handle, numa_id) multiprocessing.reduction.register(PinnedMemoryResource, _deep_reduce_pinned_memory_resource) diff --git a/cuda_core/docs/source/release/1.4.0-notes.rst b/cuda_core/docs/source/release/1.4.0-notes.rst index 1be489a39591..f83634bd4326 100644 --- a/cuda_core/docs/source/release/1.4.0-notes.rst +++ b/cuda_core/docs/source/release/1.4.0-notes.rst @@ -59,3 +59,17 @@ Fixes and enhancements later driver; with a CUDA 12 build or an older driver, imports behave as before. (`#3502 `__) + +- An imported :class:`PinnedMemoryResource` reports the exporting resource's + NUMA node as ``numa_id`` also with a CUDA 12 build of ``cuda.core`` or a + driver before 13.2, where the pool's node cannot be queried. Unpickling a + resource now carries the node, and + :meth:`PinnedMemoryResource.from_allocation_handle` accepts an optional + ``numa_id`` argument for a raw allocation handle. If the handle's resource is + already registered in the process with an unknown node, the argument becomes + its node. ``from_allocation_handle`` raises ``TypeError`` for a ``numa_id`` + that is not an integer, and ``ValueError`` for a negative or too large one + or one that differs from the pool's known node. When the node can be neither queried nor taken from the + argument, :attr:`PinnedMemoryResource.numa_id` now raises ``RuntimeError`` + instead of returning 0, which was not known to be the pool's node. + (`#3508 `__) diff --git a/cuda_core/tests/conftest.py b/cuda_core/tests/conftest.py index 0a8c9d44786c..f442bc325719 100644 --- a/cuda_core/tests/conftest.py +++ b/cuda_core/tests/conftest.py @@ -577,6 +577,12 @@ def ipc_pool_memory_resource(request, ipc_device): yield from _ipc_memory_resource(request.param, ipc_device) +@pytest.fixture +def ipc_pinned_memory_resource(ipc_device): + """Provides an IPC-enabled pinned memory pool, or skips.""" + yield from _ipc_memory_resource("pinned", ipc_device) + + @pytest.fixture(params=[_DEVICE_MR, _VIRTUAL_MR]) def ipc_memory_resource_factory(request, ipc_device): """Provides a callable that creates fresh IPC-enabled resources of one kind; all are closed at teardown.""" diff --git a/cuda_core/tests/helpers/memory.py b/cuda_core/tests/helpers/memory.py index 8f12e5712e89..7b1cf4fe7b76 100644 --- a/cuda_core/tests/helpers/memory.py +++ b/cuda_core/tests/helpers/memory.py @@ -66,15 +66,18 @@ def is_pool_resource(mr): return isinstance(mr, (DeviceMemoryResource, PinnedMemoryResource)) -def import_pool_resource(cls, device, alloc_handle): +def import_pool_resource(cls, device, alloc_handle, numa_id=None): """Import the pool shared by ``alloc_handle`` as ``cls``, the class of the exporting resource. The class must match the exporter: importing a pinned pool as a ``DeviceMemoryResource`` (or the reverse) raises ``TypeError`` (#3502). + ``numa_id`` is passed to ``PinnedMemoryResource.from_allocation_handle`` + and must be ``None`` for a ``DeviceMemoryResource``. """ if cls is PinnedMemoryResource: - return PinnedMemoryResource.from_allocation_handle(alloc_handle) + return PinnedMemoryResource.from_allocation_handle(alloc_handle, numa_id=numa_id) assert cls is DeviceMemoryResource, cls + assert numa_id is None, numa_id return DeviceMemoryResource.from_allocation_handle(device, alloc_handle) @@ -103,13 +106,13 @@ def drain_on_exit(stack, synchronizable): stack.callback(synchronizable.sync) -def enter_imported_pool(stack, cls, device, alloc_handle): +def enter_imported_pool(stack, cls, device, alloc_handle, numa_id=None): """Import the pool like :func:`import_pool_resource`, and register it to close on ``stack``. ``stack`` is a :class:`contextlib.ExitStack`. A pool resource is not a context manager, so it is entered through :func:`contextlib.closing`. """ - return stack.enter_context(contextlib.closing(import_pool_resource(cls, device, alloc_handle))) + return stack.enter_context(contextlib.closing(import_pool_resource(cls, device, alloc_handle, numa_id))) def pool_access_flags(mr, device): diff --git a/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py b/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py index b3c14f7c65fd..49f2a575f0eb 100644 --- a/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py +++ b/cuda_core/tests/memory_ipc/test_ipc_pool_kind.py @@ -12,7 +12,9 @@ """ import contextlib +import functools import multiprocessing as mp +import threading import pytest from helpers.child_processes import run_child @@ -26,6 +28,7 @@ skip_if_host_numa_pinned_memory_unsupported, skip_if_pool_location_query_unsupported, ) +from helpers.misc import run_in_threads from cuda.bindings import driver from cuda.core import ( @@ -37,8 +40,10 @@ ) from cuda.core._memory import _memory_pool from cuda.core._memory._buffer import _live_owned_mempool_count +from cuda.core._memory._pinned_memory_resource import _deep_reduce_pinned_memory_resource NBYTES = 64 +THREADS = 4 # these tests spawn new processes and files which fails for very many threads; # child processes and IPC fixtures are shared across workers (#2784); cap at two threads @@ -124,10 +129,140 @@ def child_rejected_import_keeps_existing_import(mr_type, alloc_handle, descripto def child_imported_numa_id(alloc_handle, numa_id): - Device().set_current() - mr = PinnedMemoryResource.from_allocation_handle(alloc_handle) - assert mr.numa_id == numa_id - mr.close() + device = Device() + device.set_current() + with contextlib.ExitStack() as stack: + mr = enter_imported_pool(stack, PinnedMemoryResource, device, alloc_handle) + assert mr.numa_id == numa_id + + +def child_unpickled_numa_id(mr, numa_id): + # The resource was unpickled while the child's arguments were loaded. + with contextlib.closing(mr): + assert mr.numa_id == numa_id + + +def _without_location_query(): + # Stand in for a 13.0 or 13.1 driver, as test_program_compile_cache.py does + # for _program: MP_location_query_available reads driver_version from the + # _memory_pool module on every import. The monkeypatch fixture cannot reach a + # spawned child, and this process exits after the test. + _memory_pool.driver_version = lambda: (13, 1, 0) + + +# A node that the tests cannot confuse with the pool's actual node or with +# Cython's zero initialization. Without a location query nothing checks it +# against the pool. +CLAIMED_NUMA_ID = 7 + + +def child_import_with_numa_id_without_location_query(alloc_handle): + _without_location_query() + device = Device() + device.set_current() + + # A raw file descriptor carries no UUID, so each import is a separate resource. + with contextlib.ExitStack() as stack: + omitted = enter_imported_pool(stack, PinnedMemoryResource, device, int(alloc_handle)) + with pytest.raises(RuntimeError): + _ = omitted.numa_id + given = enter_imported_pool(stack, PinnedMemoryResource, device, int(alloc_handle), CLAIMED_NUMA_ID) + assert given.numa_id == CLAIMED_NUMA_ID + + +def child_pickle_numa_id(conn, alloc_handle, numa_id): + _without_location_query() + device = Device() + device.set_current() + + with contextlib.ExitStack() as stack: + # Receiving the resource unpickles it, which imports the pool without a + # location query. The patch above has to come first, so the resource + # cannot be an argument of run_child, which unpickles before this runs. + mr = stack.enter_context(contextlib.closing(conn.recv())) + assert mr.numa_id == numa_id + + # An imported resource sends on only a node it knows. The reducer is + # called directly because a third process would need the same patch. + unknown = enter_imported_pool(stack, PinnedMemoryResource, device, int(alloc_handle)) + assert _deep_reduce_pinned_memory_resource(unknown)[1][1] is None + + known = enter_imported_pool(stack, PinnedMemoryResource, device, int(alloc_handle), CLAIMED_NUMA_ID) + assert _deep_reduce_pinned_memory_resource(known)[1][1] == CLAIMED_NUMA_ID + + +def child_registered_import_applies_numa_id(alloc_handle): + _without_location_query() + device = Device() + device.set_current() + baseline = _live_owned_mempool_count() + + with contextlib.ExitStack() as stack: + mr = enter_imported_pool(stack, PinnedMemoryResource, device, alloc_handle) + with pytest.raises(RuntimeError): + _ = mr.numa_id + # The handle's UUID is now registered, so the resource is returned, and + # it takes the node that its first import lacked. + assert PinnedMemoryResource.from_allocation_handle(alloc_handle, numa_id=CLAIMED_NUMA_ID) is mr + assert mr.numa_id == CLAIMED_NUMA_ID + assert PinnedMemoryResource.from_allocation_handle(alloc_handle, numa_id=CLAIMED_NUMA_ID) is mr + with pytest.raises(ValueError): + PinnedMemoryResource.from_allocation_handle(alloc_handle, numa_id=CLAIMED_NUMA_ID + 1) + assert PinnedMemoryResource.from_allocation_handle(alloc_handle) is mr + assert mr.numa_id == CLAIMED_NUMA_ID + assert _live_owned_mempool_count() == baseline + + +def child_concurrent_numa_ids_agree(alloc_handle): + _without_location_query() + device = Device() + device.set_current() + baseline = _live_owned_mempool_count() + + with contextlib.ExitStack() as stack: + mr = enter_imported_pool(stack, PinnedMemoryResource, device, alloc_handle) + barrier = threading.Barrier(THREADS) + adopted = [] + + def importer(numa_id): + barrier.wait() + try: + PinnedMemoryResource.from_allocation_handle(alloc_handle, numa_id=numa_id) + except ValueError: + return + adopted.append(numa_id) + + # Each thread names a different node for a resource whose node is unknown. + run_in_threads( + [functools.partial(importer, CLAIMED_NUMA_ID + i) for i in range(THREADS)], + barrier=barrier, + timeout=60, + ) + # Exactly one node wins, and no import that named another node succeeds. + assert adopted == [mr.numa_id] + assert _live_owned_mempool_count() == baseline + + +def child_numa_id_is_checked_against_driver(alloc_handle): + device = Device() + device.set_current() + baseline = _live_owned_mempool_count() + + first = import_pool_resource(PinnedMemoryResource, device, int(alloc_handle)) + node = first.numa_id + # Imports of one pool share a native owner that releases the driver's + # references only when the last import closes. Close the first so that the + # rejected import below is the last one and its release happens in the capture. + first.close() + # Releasing the rejected import cannot raise; a failed cuMemPoolDestroy + # surfaces only as a CUDAWarning. + with assert_no_cuda_warning(), pytest.raises(ValueError): + PinnedMemoryResource.from_allocation_handle(int(alloc_handle), numa_id=node + 1) + + with contextlib.ExitStack() as stack: + matching = enter_imported_pool(stack, PinnedMemoryResource, device, int(alloc_handle), node) + assert matching.numa_id == node + assert _live_owned_mempool_count() == baseline def child_matching_import(mr_type, alloc_handle, descriptor): @@ -148,11 +283,7 @@ def child_matching_import(mr_type, alloc_handle, descriptor): def child_import_without_location_query(mr_type, alloc_handle, descriptor): - # Stand in for a 13.0 or 13.1 driver, as test_program_compile_cache.py does - # for _program: MP_location_query_available reads driver_version from the - # _memory_pool module on every import. The monkeypatch fixture cannot reach a - # spawned child, and this process exits after the test. - _memory_pool.driver_version = lambda: (13, 1, 0) + _without_location_query() device = Device() device.set_current() stream = device.default_stream @@ -165,9 +296,6 @@ def child_import_without_location_query(mr_type, alloc_handle, descriptor): with contextlib.ExitStack() as stack: drain_on_exit(stack, stream) mr = enter_imported_pool(stack, mr_type, device, int(alloc_handle)) - if mr_type is PinnedMemoryResource: - # Without the location query the pool's node is unknown (see numa_id). - assert mr.numa_id == 0 buffer = stack.enter_context(Buffer.from_ipc_descriptor(mr, descriptor, stream=stream)) buffer.fill(7, stream=stream) @@ -249,8 +377,43 @@ def test_imported_pinned_resource_reports_numa_id(self, ipc_device): pytest.skip("needs a host NUMA node other than 0 with memory") ipc_device.set_current() mr = PinnedMemoryResource(PinnedMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True, numa_id=numa_id)) - try: + with contextlib.closing(mr): assert mr.numa_id == numa_id run_child(child_imported_numa_id, mr.allocation_handle, numa_id) - finally: - mr.close() + # Unpickling the resource is the other import path. + run_child(child_unpickled_numa_id, mr, numa_id) + + @pytest.mark.agent_authored(model="claude-sonnet-5-5") + def test_numa_id_is_checked_against_driver(self, ipc_pinned_memory_resource): + run_child(child_numa_id_is_checked_against_driver, ipc_pinned_memory_resource.allocation_handle) + + +class TestPinnedImportNumaId(_IpcPoolFixtures): + """Without a location query, an imported pinned pool's node comes from the caller or the pickle (#3508).""" + + @pytest.mark.agent_authored(model="claude-sonnet-5-5") + def test_import_uses_given_numa_id(self, ipc_pinned_memory_resource): + run_child(child_import_with_numa_id_without_location_query, ipc_pinned_memory_resource.allocation_handle) + + @pytest.mark.agent_authored(model="claude-opus-5-5") + def test_pickle_numa_id(self, ipc_pinned_memory_resource): + mr = ipc_pinned_memory_resource + parent_conn, child_conn = mp.Pipe() + with parent_conn, child_conn: + parent_conn.send(mr) + run_child(child_pickle_numa_id, child_conn, mr.allocation_handle, mr.numa_id) + + @pytest.mark.agent_authored(model="claude-sonnet-5-5") + def test_registered_import_applies_numa_id(self, ipc_pinned_memory_resource): + run_child(child_registered_import_applies_numa_id, ipc_pinned_memory_resource.allocation_handle) + + @pytest.mark.agent_authored(model="claude-opus-5-5") + def test_concurrent_imports_agree_on_numa_id(self, ipc_pinned_memory_resource): + run_child(child_concurrent_numa_ids_agree, ipc_pinned_memory_resource.allocation_handle) + + @pytest.mark.agent_authored(model="claude-opus-5-5") + @pytest.mark.parametrize(("numa_id", "error"), [(-1, ValueError), (2**40, ValueError), (1.75, TypeError)]) + def test_invalid_numa_id_raises(self, numa_id, error): + # Rejected before the handle is used or CUDA is initialized. + with pytest.raises(error): + PinnedMemoryResource.from_allocation_handle(0, numa_id=numa_id)