-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathtest_helpers.py
More file actions
84 lines (72 loc) · 2.89 KB
/
test_helpers.py
File metadata and controls
84 lines (72 loc) · 2.89 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
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
import time
import pytest
from cuda.core import Device
from helpers import IS_WINDOWS, IS_WSL
from helpers.buffers import PatternGen, compare_equal_buffers, make_scratch_buffer
from helpers.latch import LatchKernel
from helpers.logging import TimestampedLogger
from cuda_python_test_helpers import under_compute_sanitizer
ENABLE_LOGGING = False # Set True for test debugging and development
NBYTES = 64
pytestmark = pytest.mark.usefixtures("requires_concurrent_managed_access")
def test_latchkernel():
"""Test LatchKernel."""
log = TimestampedLogger(enabled=ENABLE_LOGGING)
log("begin")
device = Device()
device.set_current()
stream = device.create_stream()
target = make_scratch_buffer(device, 0, NBYTES)
zeros = make_scratch_buffer(device, 0, NBYTES)
ones = make_scratch_buffer(device, 1, NBYTES)
latch = LatchKernel(device)
log("launching latch kernel")
latch.launch(stream)
log("launching copy (0->1) kernel")
target.copy_from(ones, stream=stream)
log("going to sleep")
time.sleep(1)
if not IS_WINDOWS and not IS_WSL:
# On any sort of Windows system, checking the memory before stream
# sync results in a page error.
log("checking target == 0")
assert compare_equal_buffers(target, zeros)
log("releasing latch and syncing")
latch.release()
stream.sync()
log("checking target == 1")
assert compare_equal_buffers(target, ones)
log("done")
@pytest.mark.skipif(
under_compute_sanitizer(),
reason="Too slow under compute-sanitizer (UVM-heavy test).",
)
def test_patterngen_seeds():
"""Test PatternGen with seed argument."""
device = Device()
device.set_current()
buffer = make_scratch_buffer(device, 0, NBYTES)
# All seeds are pairwise different.
# We test a sampling of values because exhaustive testing is too slow,
# especially on Windows. See https://github.com/NVIDIA/cuda-python/issues/1455
pgen = PatternGen(device, NBYTES)
for i in (ii for ii in range(0, 256) if ii < 5 or ii % 17 == 0):
pgen.fill_buffer(buffer, seed=i)
pgen.verify_buffer(buffer, seed=i)
for j in (jj for jj in range(i + 1, 256) if jj < 5 or jj % 19 == 0):
with pytest.raises(AssertionError):
pgen.verify_buffer(buffer, seed=j)
def test_patterngen_values():
"""Test PatternGen with value argument, also compare_equal_buffers."""
device = Device()
device.set_current()
ones = make_scratch_buffer(device, 1, NBYTES)
twos = make_scratch_buffer(device, 2, NBYTES)
assert compare_equal_buffers(ones, ones)
assert not compare_equal_buffers(ones, twos)
pgen = PatternGen(device, NBYTES)
pgen.verify_buffer(ones, value=1)
pgen.verify_buffer(twos, value=2)