forked from NVIDIA/cuda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_event.py
More file actions
129 lines (103 loc) · 4.43 KB
/
test_event.py
File metadata and controls
129 lines (103 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Copyright 2024 NVIDIA Corporation. All rights reserved.
#
# Please refer to the NVIDIA end user license agreement (EULA) associated
# with this source code for terms and conditions that govern your use of
# this software. Any use, reproduction, disclosure, or distribution of
# this software and related documentation outside the terms of the EULA
# is strictly prohibited.
import os
import time
import pytest
import cuda.core.experimental
from cuda.core.experimental import Device, EventOptions
def test_event_init_disabled():
with pytest.raises(RuntimeError, match=r"^Event objects cannot be instantiated directly\."):
cuda.core.experimental._event.Event() # Ensure back door is locked.
def test_timing_success(init_cuda):
options = EventOptions(enable_timing=True)
stream = Device().create_stream()
delay_seconds = 0.5
e1 = stream.record(options=options)
time.sleep(delay_seconds)
e2 = stream.record(options=options)
e2.sync()
elapsed_time_ms = e2 - e1
assert isinstance(elapsed_time_ms, float)
# Using a generous tolerance, to avoid flaky tests:
# We only want to exercise the __sub__ method, this test is not meant
# to stress-test the CUDA driver or time.sleep().
delay_ms = delay_seconds * 1000
if os.name == "nt": # noqa: SIM108
# For Python <=3.10, the Windows timer resolution is typically limited to 15.6 ms by default.
generous_tolerance = 100
else:
# Most modern Linux kernels have a default timer resolution of 1 ms.
generous_tolerance = 20
assert delay_ms - generous_tolerance <= elapsed_time_ms < delay_ms + generous_tolerance
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 is True
options = EventOptions(enable_timing=False)
stream = Device().create_stream()
event = stream.record(options=options)
assert event.is_sync_busy_waited is 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 is True
def test_is_done(init_cuda):
options = EventOptions(enable_timing=False)
stream = Device().create_stream()
event = stream.record(options=options)
# Without a sync, the captured work might not have yet completed
# Therefore this check should never raise an exception
assert event.is_done in (True, False)
def test_error_timing_disabled():
device = Device()
device.set_current()
enabled = EventOptions(enable_timing=True)
disabled = EventOptions(enable_timing=False)
stream = device.create_stream()
event1 = stream.record(options=enabled)
event2 = stream.record(options=disabled)
assert not event1.is_timing_disabled
assert event2.is_timing_disabled
stream.sync()
with pytest.raises(RuntimeError, match="^Both Events must be created with timing enabled"):
event2 - event1
event1 = stream.record(options=disabled)
event2 = stream.record(options=disabled)
stream.sync()
with pytest.raises(RuntimeError, match="^Both Events must be created with timing enabled"):
event2 - event1
def test_error_timing_recorded():
device = Device()
device.set_current()
enabled = EventOptions(enable_timing=True)
stream = device.create_stream()
event1 = stream.record(options=enabled)
event2 = device.create_event(options=enabled)
event3 = device.create_event(options=enabled)
stream.sync()
with pytest.raises(RuntimeError, match="^Both Events must be recorded"):
event2 - event1
with pytest.raises(RuntimeError, match="^Both Events must be recorded"):
event1 - event2
with pytest.raises(RuntimeError, match="^Both Events must be recorded"):
event3 - event2
def test_error_timing_incomplete():
device = Device()
device.set_current()
enabled = EventOptions(enable_timing=True)
stream = device.create_stream()
event1 = stream.record(options=enabled)
event2 = device.create_event(options=enabled)
stream.wait(event2)
event3 = stream.record(options=enabled)
# event3 will never complete because the stream is waiting on event2 which is never recorded
with pytest.raises(RuntimeError, match="^One or both events have not completed."):
event3 - event1