Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cuda_core/cuda/core/experimental/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
Buffer,
DeviceMemoryResource,
DeviceMemoryResourceOptions,
GraphMemoryResource,
LegacyPinnedMemoryResource,
MemoryResource,
VirtualMemoryResource,
Expand Down
16 changes: 9 additions & 7 deletions cuda_core/cuda/core/experimental/_device.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ cimport cpython
from libc.stdint cimport uintptr_t

from cuda.bindings cimport cydriver

from cuda.core.experimental._utils.cuda_utils cimport HANDLE_RETURN

import threading
from typing import Union
from typing import Optional, TYPE_CHECKING, Union

from cuda.core.experimental._context import Context, ContextOptions
from cuda.core.experimental._event import Event, EventOptions
from cuda.core.experimental._graph import GraphBuilder
from cuda.core.experimental._memory import Buffer, DeviceMemoryResource, MemoryResource, _SynchronousMemoryResource
from cuda.core.experimental._stream import IsStreamT, Stream, StreamOptions
from cuda.core.experimental._utils.clear_error_support import assert_type
from cuda.core.experimental._utils.cuda_utils import (
Expand All @@ -27,7 +25,8 @@ from cuda.core.experimental._utils.cuda_utils import (
)
from cuda.core.experimental._stream cimport default_stream


if TYPE_CHECKING:
from cuda.core.experimental._memory import Buffer, MemoryResource

# TODO: I prefer to type these as "cdef object" and avoid accessing them from within Python,
# but it seems it is very convenient to expose them for testing purposes...
Expand Down Expand Up @@ -996,8 +995,10 @@ class Device:
)
)
if attr == 1:
from cuda.core.experimental._memory import DeviceMemoryResource
device._mr = DeviceMemoryResource(dev_id)
else:
from cuda.core.experimental._memory import _SynchronousMemoryResource
device._mr = _SynchronousMemoryResource(dev_id)

device._has_inited = False
Expand Down Expand Up @@ -1131,6 +1132,7 @@ class Device:

@memory_resource.setter
def memory_resource(self, mr):
from cuda.core.experimental._memory import MemoryResource
assert_type(mr, MemoryResource)
self._mr = mr

Expand Down Expand Up @@ -1240,7 +1242,7 @@ class Device:
"""
raise NotImplementedError("WIP: https://github.com/NVIDIA/cuda-python/issues/189")

def create_stream(self, obj: IsStreamT | None = None, options: StreamOptions | None = None) -> Stream:
def create_stream(self, obj: Optional[IsStreamT] = None, options: StreamOptions | None = None) -> Stream:
"""Create a Stream object.

New stream objects can be created in two different ways:
Expand Down Expand Up @@ -1293,7 +1295,7 @@ class Device:
ctx = self._get_current_context()
return Event._init(self._id, ctx, options, True)

def allocate(self, size, stream: Stream | None = None) -> Buffer:
def allocate(self, size, stream: Optional[IsStreamT] = None) -> Buffer:
"""Allocate device memory from a specified stream.

Allocates device memory of `size` bytes on the specified `stream`
Expand All @@ -1309,7 +1311,7 @@ class Device:
----------
size : int
Number of bytes to allocate.
stream : :obj:`~_stream.Stream`, optional
stream : :obj:`~_stream.IsStreamT`, optional
The stream establishing the stream ordering semantic.
Default value of `None` uses default stream.

Expand Down
5 changes: 2 additions & 3 deletions cuda_core/cuda/core/experimental/_event.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ from __future__ import annotations
cimport cpython
from libc.stdint cimport uintptr_t
from libc.string cimport memcpy

from cuda.bindings cimport cydriver

from cuda.core.experimental._utils.cuda_utils cimport (
check_or_create_options,
HANDLE_RETURN
)

import cython
from dataclasses import dataclass
import multiprocessing
from typing import TYPE_CHECKING, Optional
Expand Down Expand Up @@ -287,7 +286,7 @@ cdef class IPCEventDescriptor:
raise RuntimeError("IPCEventDescriptor objects cannot be instantiated directly. Please use Event APIs.")

@classmethod
def _init(cls, reserved: bytes, busy_waited: bint):
def _init(cls, reserved: bytes, busy_waited: cython.bint):
cdef IPCEventDescriptor self = IPCEventDescriptor.__new__(cls)
self._reserved = reserved
self._busy_waited = busy_waited
Expand Down
28 changes: 6 additions & 22 deletions cuda_core/cuda/core/experimental/_launcher.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
#
# SPDX-License-Identifier: Apache-2.0

from libc.stdint cimport uintptr_t

from cuda.core.experimental._stream cimport _try_to_get_stream_ptr

from typing import Union

from cuda.core.experimental._kernel_arg_handler import ParamHolder
from cuda.core.experimental._launch_config cimport LaunchConfig, _to_native_launch_config
from cuda.core.experimental._module import Kernel
Expand Down Expand Up @@ -39,13 +33,13 @@ def _lazy_init():
_inited = True


def launch(stream: Union[Stream, IsStreamT], config: LaunchConfig, kernel: Kernel, *kernel_args):
def launch(stream: IsStreamT, config: LaunchConfig, kernel: Kernel, *kernel_args):
"""Launches a :obj:`~_module.Kernel`
object with launch-time configuration.

Parameters
----------
stream : :obj:`~_stream.Stream`
stream : :obj:`~_stream.IsStreamT`
The stream establishing the stream ordering semantic of a
launch.
config : :obj:`LaunchConfig`
Expand All @@ -58,17 +52,7 @@ def launch(stream: Union[Stream, IsStreamT], config: LaunchConfig, kernel: Kerne
launching kernel.

"""
if stream is None:
raise ValueError("stream cannot be None, stream must either be a Stream object or support __cuda_stream__")
try:
stream_handle = stream.handle
except AttributeError:
try:
stream_handle = driver.CUstream(<uintptr_t>(_try_to_get_stream_ptr(stream)))
except Exception:
raise ValueError(
f"stream must either be a Stream object or support __cuda_stream__ (got {type(stream)})"
) from None
stream = Stream._init(stream)
assert_type(kernel, Kernel)
_lazy_init()
config = check_or_create_options(LaunchConfig, config, "launch config")
Expand All @@ -85,20 +69,20 @@ def launch(stream: Union[Stream, IsStreamT], config: LaunchConfig, kernel: Kerne
# rich.
if _use_ex:
drv_cfg = _to_native_launch_config(config)
drv_cfg.hStream = stream_handle
drv_cfg.hStream = stream.handle
if config.cooperative_launch:
_check_cooperative_launch(kernel, config, stream)
handle_return(driver.cuLaunchKernelEx(drv_cfg, int(kernel._handle), args_ptr, 0))
else:
# TODO: check if config has any unsupported attrs
handle_return(
driver.cuLaunchKernel(
int(kernel._handle), *config.grid, *config.block, config.shmem_size, stream_handle, args_ptr, 0
int(kernel._handle), *config.grid, *config.block, config.shmem_size, stream.handle, args_ptr, 0
)
)


def _check_cooperative_launch(kernel: Kernel, config: LaunchConfig, stream: Stream):
cdef _check_cooperative_launch(kernel: Kernel, config: LaunchConfig, stream: Stream):
dev = stream.device
num_sm = dev.properties.multiprocessor_count
max_grid_size = (
Expand Down
Loading