Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up testsuite #213

Merged
merged 10 commits into from
Nov 18, 2024
17 changes: 15 additions & 2 deletions cuda_core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@
# is strictly prohibited.

from cuda.core.experimental._device import Device
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
from cuda.core.experimental import _device
from cuda import cuda
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
from cuda.core.experimental._utils import handle_return
import pytest

@pytest.fixture(scope="module")
@pytest.fixture(scope="function")
def init_cuda():
device = Device()
device.set_current()

yield
handle_return(cuda.cuCtxPopCurrent())
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
with _device._tls_lock:
del _device._tls.devices

@pytest.fixture(scope="function")
def deinit_cuda():
yield
handle_return(cuda.cuCtxPopCurrent())
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
with _device._tls_lock:
del _device._tls.devices
2 changes: 1 addition & 1 deletion cuda_core/tests/example_tests/test_basic_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
'example', sample_files
)
class TestExamples:
def test_example(self, example):
def test_example(self, example, deinit_cuda):
filename = os.path.basename(example)
run_example(samples_path, example)
1 change: 0 additions & 1 deletion cuda_core/tests/example_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
# this software and related documentation outside the terms of the EULA
# is strictly prohibited.

from cuda import cuda
import gc
import os
import sys
Expand Down
15 changes: 7 additions & 8 deletions cuda_core/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

from cuda import cuda, cudart
from cuda.core.experimental._device import Device
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
from cuda.core.experimental._utils import handle_return, ComputeCapability, CUDAError, \
precondition
import pytest
from cuda.core.experimental._utils import handle_return, ComputeCapability

def test_device_set_current(deinit_cuda):
device = Device()
device.set_current()
assert handle_return(cuda.cuCtxGetCurrent()) is not None

def test_device_repr():
device = Device(0)
assert str(device).startswith('<Device 0')
Expand All @@ -24,11 +27,7 @@ def test_device_alloc(init_cuda):
assert buffer.size == 1024
assert buffer.device_id == 0

def test_device_set_current():
device = Device()
device.set_current()

def test_device_create_stream():
def test_device_create_stream(init_cuda):
device = Device()
stream = device.create_stream()
assert stream is not None
Expand Down
48 changes: 30 additions & 18 deletions cuda_core/tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,46 @@
# this software and related documentation outside the terms of the EULA
# is strictly prohibited.

from cuda import cuda
from cuda.core.experimental._event import EventOptions, Event
from cuda.core.experimental._utils import handle_return
from cuda.core.experimental._event import EventOptions
from cuda.core.experimental._device import Device
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
import pytest

def test_is_timing_disabled():
def test_is_timing_disabled(init_cuda):
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
options = EventOptions(enable_timing=False)
event = Event._init(options)
stream = Device().create_stream()
event = stream.record(options=options)
assert event.is_timing_disabled == True

options = EventOptions(enable_timing=True)
stream = Device().create_stream()
event = stream.record(options=options)
assert event.is_timing_disabled == False

def test_is_sync_busy_waited():
options = EventOptions(busy_waited_sync=True)
event = Event._init(options)
def test_is_sync_busy_waited(init_cuda):
options = EventOptions(enable_timing=False, busy_waited_sync=True)
stream = Device().create_stream()
event = stream.record(options=options)
assert event.is_sync_busy_waited == True

def test_sync():
options = EventOptions()
event = Event._init(options)
options = EventOptions(enable_timing=False)
stream = Device().create_stream()
event = stream.record(options=options)
assert event.is_sync_busy_waited == False

def test_sync(init_cuda):
options = EventOptions(enable_timing=False)
stream = Device().create_stream()
event = stream.record(options=options)
event.sync()
assert event.is_done == True

def test_is_done():
options = EventOptions()
event = Event._init(options)
def test_is_done(init_cuda):
options = EventOptions(enable_timing=False)
stream = Device().create_stream()
event = stream.record(options=options)
assert event.is_done == True

def test_handle():
options = EventOptions()
event = Event._init(options)
def test_handle(init_cuda):
options = EventOptions(enable_timing=False)
stream = Device().create_stream()
event = stream.record(options=options)
assert isinstance(event.handle, int)
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 2 additions & 4 deletions cuda_core/tests/test_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
# this software and related documentation outside the terms of the EULA
# is strictly prohibited.

from cuda import cuda
from cuda.core.experimental._launcher import LaunchConfig
from cuda.core.experimental._stream import Stream
from cuda.core.experimental._device import Device
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
from cuda.core.experimental._utils import handle_return
import pytest

def test_launch_config_init():
def test_launch_config_init(init_cuda):
config = LaunchConfig(grid=(1, 1, 1), block=(1, 1, 1), stream=None, shmem_size=0)
assert config.grid == (1, 1, 1)
assert config.block == (1, 1, 1)
Expand Down Expand Up @@ -50,7 +48,7 @@ def test_launch_config_invalid_values():
with pytest.raises(ValueError):
LaunchConfig(grid=(1, 1, 1), block=(0, 1))

def test_launch_config_stream():
def test_launch_config_stream(init_cuda):
stream = Device().create_stream()
config = LaunchConfig(grid=(1, 1, 1), block=(1, 1, 1), stream=stream, shmem_size=0)
assert config.stream == stream
Expand Down
1 change: 0 additions & 1 deletion cuda_core/tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from cuda.core.experimental._device import Device
from cuda.core.experimental._utils import handle_return
import ctypes
import pytest

class DummyDeviceMemoryResource(MemoryResource):
def __init__(self, device):
Expand Down
5 changes: 1 addition & 4 deletions cuda_core/tests/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
# this software and related documentation outside the terms of the EULA
# is strictly prohibited.

from cuda import cuda
from cuda.core.experimental._device import Device
from cuda.core.experimental._module import Kernel, ObjectCode
from cuda.core.experimental._utils import handle_return
from cuda.core.experimental._module import ObjectCode
import pytest
import importlib

Expand Down
1 change: 0 additions & 1 deletion cuda_core/tests/test_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from cuda.core.experimental._program import Program
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved
from cuda.core.experimental._module import ObjectCode, Kernel
from cuda.core.experimental._device import Device
import pytest

def test_program_init_valid_code_type():
Expand Down
50 changes: 25 additions & 25 deletions cuda_core/tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,67 @@
# is strictly prohibited.

from cuda.core.experimental._stream import Stream, StreamOptions, LEGACY_DEFAULT_STREAM, PER_THREAD_DEFAULT_STREAM, default_stream
from cuda.core.experimental._event import Event, EventOptions
from cuda.core.experimental._event import Event
from cuda.core.experimental._device import Device
import pytest

def test_stream_init():
with pytest.raises(NotImplementedError):
Stream()

def test_stream_init_with_options():
stream = Stream._init(options=StreamOptions(nonblocking=True, priority=0))
def test_stream_init_with_options(init_cuda):
stream = Device().create_stream(options=StreamOptions(nonblocking=True, priority=0))
assert stream.is_nonblocking is True
assert stream.priority == 0

def test_stream_handle():
stream = Stream._init(options=StreamOptions())
def test_stream_handle(init_cuda):
stream = Device().create_stream(options=StreamOptions())
assert isinstance(stream.handle, int)

def test_stream_is_nonblocking():
stream = Stream._init(options=StreamOptions(nonblocking=True))
def test_stream_is_nonblocking(init_cuda):
stream = Device().create_stream(options=StreamOptions(nonblocking=True))
assert stream.is_nonblocking is True

def test_stream_priority():
stream = Stream._init(options=StreamOptions(priority=0))
def test_stream_priority(init_cuda):
stream = Device().create_stream(options=StreamOptions(priority=0))
assert stream.priority == 0
stream = Stream._init(options=StreamOptions(priority=-1))
stream = Device().create_stream(options=StreamOptions(priority=-1))
assert stream.priority == -1
with pytest.raises(ValueError):
stream = Stream._init(options=StreamOptions(priority=1))
stream = Device().create_stream(options=StreamOptions(priority=1))

def test_stream_sync():
stream = Stream._init(options=StreamOptions())
def test_stream_sync(init_cuda):
stream = Device().create_stream(options=StreamOptions())
stream.sync() # Should not raise any exceptions

def test_stream_record():
stream = Stream._init(options=StreamOptions())
def test_stream_record(init_cuda):
stream = Device().create_stream(options=StreamOptions())
event = stream.record()
assert isinstance(event, Event)

def test_stream_record_invalid_event():
stream = Stream._init(options=StreamOptions())
def test_stream_record_invalid_event(init_cuda):
stream = Device().create_stream(options=StreamOptions())
with pytest.raises(TypeError):
stream.record(event="invalid_event")

def test_stream_wait_event():
stream = Stream._init(options=StreamOptions())
def test_stream_wait_event(init_cuda):
stream = Device().create_stream(options=StreamOptions())
event = Event._init()
stream.record(event)
stream.wait(event) # Should not raise any exceptions
ksimpson-work marked this conversation as resolved.
Show resolved Hide resolved

def test_stream_wait_invalid_event():
stream = Stream._init(options=StreamOptions())
def test_stream_wait_invalid_event(init_cuda):
stream = Device().create_stream(options=StreamOptions())
with pytest.raises(ValueError):
stream.wait(event_or_stream="invalid_event")

def test_stream_device():
stream = Stream._init(options=StreamOptions())
def test_stream_device(init_cuda):
stream = Device().create_stream(options=StreamOptions())
device = stream.device
assert isinstance(device, Device)

def test_stream_context():
stream = Stream._init(options=StreamOptions())
def test_stream_context(init_cuda):
stream = Device().create_stream(options=StreamOptions())
context = stream.context
assert context is not None

Expand Down