Skip to content

Commit 7c12d84

Browse files
committed
Lower green context resource handling to Cython
1 parent ac5c0fc commit 7c12d84

4 files changed

Lines changed: 142 additions & 84 deletions

File tree

cuda_core/cuda/core/_context.pyx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
from __future__ import annotations
6+
7+
from collections.abc import Sequence
58
from dataclasses import dataclass
69

710
from cuda.bindings cimport cydriver
11+
from cuda.core._device_resources import SMResource, WorkqueueResource
812
from cuda.core._resource_handles cimport (
913
ContextHandle,
1014
GreenCtxHandle,
@@ -21,6 +25,9 @@ from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
2125
__all__ = ['Context', 'ContextOptions']
2226

2327

28+
DeviceResourcesT = SMResource | WorkqueueResource | Sequence[SMResource | WorkqueueResource]
29+
30+
2431
cdef class Context:
2532
"""CUDA context wrapper.
2633
@@ -100,12 +107,12 @@ cdef class Context:
100107

101108

102109
@dataclass
103-
class ContextOptions:
110+
cdef class ContextOptions:
104111
"""Options for context creation.
105112
106113
Attributes
107114
----------
108-
resources : Sequence[SMResource | WorkqueueResource], optional
115+
resources : :obj:`~_context.DeviceResourcesT`
109116
Device resources used to create a green context.
110117
"""
111-
resources: object = None
118+
resources: DeviceResourcesT

cuda_core/cuda/core/_device.pyx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,14 @@ from __future__ import annotations
77
cimport cpython
88

99
from cuda.bindings cimport cydriver
10-
from cuda.core._utils.cuda_utils cimport HANDLE_RETURN
10+
from cuda.core._utils.cuda_utils cimport check_or_create_options, HANDLE_RETURN
1111
from libc.stdlib cimport free, malloc
1212

1313
import threading
1414

1515
from cuda.core._context cimport Context
1616
from cuda.core._context import ContextOptions
1717
from cuda.core._device_resources cimport DeviceResources, SMResource, WorkqueueResource
18-
from cuda.core._device_resources import (
19-
DeviceResources,
20-
SMResource,
21-
WorkqueueResource,
22-
)
2318
from cuda.core._event cimport Event as cyEvent
2419
from cuda.core._event import Event, EventOptions
2520
from cuda.core._memory._buffer cimport Buffer, MemoryResource
@@ -1309,7 +1304,7 @@ class Device:
13091304
if options is None:
13101305
raise NotImplementedError("WIP: https://github.com/NVIDIA/cuda-python/issues/189")
13111306

1312-
assert_type(options, ContextOptions)
1307+
options = check_or_create_options(ContextOptions, options, "Context options")
13131308
if options.resources is None:
13141309
raise NotImplementedError("WIP: https://github.com/NVIDIA/cuda-python/issues/189")
13151310

cuda_core/cuda/core/_device_resources.pxd

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ from cuda.bindings cimport cydriver
88
cdef class SMResource:
99
cdef:
1010
cydriver.CUdevResource _resource
11+
unsigned int _sm_count
12+
unsigned int _min_partition_size
13+
unsigned int _coscheduled_alignment
14+
unsigned int _flags
1115
bint _is_usable
1216
object __weakref__
1317

1418
@staticmethod
15-
cdef SMResource _from_dev_resource(cydriver.CUdevResource res)
19+
cdef SMResource _from_dev_resource(cydriver.CUdevResource res, int device_id)
1620

1721
@staticmethod
18-
cdef SMResource _from_dry_run_resource(cydriver.CUdevResource res)
22+
cdef SMResource _from_split_resource(cydriver.CUdevResource res, SMResource parent, bint is_usable)
1923

2024

2125
cdef class WorkqueueResource:

0 commit comments

Comments
 (0)