|
2 | 2 | # |
3 | 3 | # SPDX-License-Identifier: Apache-2.0 |
4 | 4 |
|
5 | | -# Conversion from the internal ``_LocSpec`` record produced by |
6 | | -# ``_managed_location._coerce_location`` to the driver's ``CUmemLocation``. |
| 5 | +# Conversion helpers for the driver's ``CUmemLocation`` struct. |
7 | 6 | # |
8 | 7 | # Header-only so both the managed-memory ops and the batched copy path can |
9 | 8 | # cimport it without either module depending on the other. ``CUmemLocation`` |
10 | 9 | # is only populated on a CUDA 13 build; the CUDA 12 stub exists so callers |
11 | 10 | # compiled there still resolve the symbol. |
12 | 11 | # |
13 | | -# Construction uses field assignment rather than Cython struct literals so |
14 | | -# the same source compiles against both the CUDA 13.3 two-member declaration |
15 | | -# and the CUDA 13.4 declaration that adds the ``localized`` union arm. |
16 | | -# ``CUDA_CORE_HAS_LOCALIZED_LOCATION`` selects a helper signature with an |
17 | | -# optional ``localized`` argument (13.4+) or without it (13.3). Passing |
18 | | -# ``localized=...`` is therefore a Cython compile-time error on 13.3. |
| 12 | +# Both helpers use field assignment rather than Cython struct literals |
| 13 | +# (``CUmemLocation(type=..., id=...)``) so this source keeps compiling if a |
| 14 | +# future generated ``cydriver.pxd`` adds a sibling member to the struct's |
| 15 | +# anonymous union (e.g. CUDA 13.4's ``localized`` arm): Cython's struct-literal |
| 16 | +# coercion warns "Not all members given for struct" whenever a call site does |
| 17 | +# not name every declared member, and cuda_core promotes that warning to a |
| 18 | +# build error. |
19 | 19 |
|
20 | 20 | from cuda.bindings cimport cydriver |
21 | 21 |
|
22 | 22 |
|
23 | 23 | IF CUDA_CORE_BUILD_MAJOR >= 13: |
24 | | - cdef inline void _fill_id_location( |
25 | | - cydriver.CUmemLocation* cu_loc, str kind, int loc_id |
26 | | - ) except *: |
| 24 | + cdef inline cydriver.CUmemLocation cumemlocation_from_type( |
| 25 | + cydriver.CUmemLocationType loc_type, int loc_id |
| 26 | + ): |
| 27 | + """Build a ``CUmemLocation`` from an already-known ``CUmemLocationType``. |
| 28 | +
|
| 29 | + For call sites that already carry a ``CUmemLocationType`` value |
| 30 | + (e.g. from a pool-configuration parameter), rather than the |
| 31 | + ``kind`` string used by :func:`to_cumemlocation`. |
| 32 | + """ |
| 33 | + cdef cydriver.CUmemLocation cu_loc |
| 34 | + cu_loc.type = loc_type |
| 35 | + cu_loc.id = loc_id |
| 36 | + return cu_loc |
| 37 | + |
| 38 | + cdef inline cydriver.CUmemLocation to_cumemlocation(str kind, int loc_id): |
27 | 39 | if kind == "device": |
28 | | - cu_loc.type = cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_DEVICE |
29 | | - cu_loc.id = loc_id |
| 40 | + return cumemlocation_from_type( |
| 41 | + cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_DEVICE, loc_id) |
30 | 42 | elif kind == "host": |
31 | | - cu_loc.type = cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST |
32 | | - cu_loc.id = 0 |
| 43 | + return cumemlocation_from_type( |
| 44 | + cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST, 0) |
33 | 45 | elif kind == "host_numa": |
34 | | - cu_loc.type = cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST_NUMA |
35 | | - cu_loc.id = loc_id |
| 46 | + return cumemlocation_from_type( |
| 47 | + cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST_NUMA, loc_id) |
36 | 48 | elif kind == "host_numa_current": |
37 | | - cu_loc.type = cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST_NUMA_CURRENT |
38 | | - cu_loc.id = 0 |
| 49 | + return cumemlocation_from_type( |
| 50 | + cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_HOST_NUMA_CURRENT, 0) |
39 | 51 | else: |
40 | 52 | raise ValueError(f"unknown location kind: {kind!r}") |
41 | | - |
42 | | - IF CUDA_CORE_HAS_LOCALIZED_LOCATION: |
43 | | - cdef inline cydriver.CUmemLocation to_cumemlocation( |
44 | | - str kind, int loc_id=0, tuple localized=None |
45 | | - ): |
46 | | - cdef cydriver.CUmemLocation cu_loc |
47 | | - if kind == "device_locality_domain": |
48 | | - if localized is None: |
49 | | - raise ValueError( |
50 | | - "kind='device_locality_domain' requires " |
51 | | - "localized=(device_id, locality_domain_id)" |
52 | | - ) |
53 | | - cu_loc.type = ( |
54 | | - cydriver.CUmemLocationType.CU_MEM_LOCATION_TYPE_DEVICE_LOCALITY_DOMAIN |
55 | | - ) |
56 | | - cu_loc.localized.deviceId = localized[0] |
57 | | - cu_loc.localized.localityDomainId = localized[1] |
58 | | - return cu_loc |
59 | | - _fill_id_location(&cu_loc, kind, loc_id) |
60 | | - return cu_loc |
61 | | - ELSE: |
62 | | - cdef inline cydriver.CUmemLocation to_cumemlocation(str kind, int loc_id=0): |
63 | | - cdef cydriver.CUmemLocation cu_loc |
64 | | - _fill_id_location(&cu_loc, kind, loc_id) |
65 | | - return cu_loc |
66 | 53 | ELSE: |
67 | | - cdef inline cydriver.CUmemLocation to_cumemlocation(str kind, int loc_id=0): |
| 54 | + cdef inline cydriver.CUmemLocation cumemlocation_from_type( |
| 55 | + cydriver.CUmemLocationType loc_type, int loc_id |
| 56 | + ): |
| 57 | + cdef cydriver.CUmemLocation cu_loc |
| 58 | + cu_loc.type = loc_type |
| 59 | + cu_loc.id = loc_id |
| 60 | + return cu_loc |
| 61 | + |
| 62 | + cdef inline cydriver.CUmemLocation to_cumemlocation(str kind, int loc_id): |
68 | 63 | raise NotImplementedError( |
69 | 64 | "CUmemLocation requires cuda.core built against CUDA 13 headers" |
70 | 65 | ) |
0 commit comments