|
13 | 13 | cp = None |
14 | 14 | import numpy as np |
15 | 15 | import pytest |
| 16 | +from cuda.bindings import driver |
16 | 17 | from conftest import skipif_need_cuda_headers |
17 | 18 |
|
18 | 19 | from cuda.core import ( |
|
26 | 27 | ) |
27 | 28 | from cuda.core._memory._legacy import _SynchronousMemoryResource |
28 | 29 | from cuda.core._utils.cuda_utils import CUDAError |
29 | | -from cuda.core.typing import ObjectCodeFormatType, SourceCodeType |
| 30 | +from cuda.core.typing import ObjectCodeFormatType, SourceCodeType, SynchronizationPolicyType |
30 | 31 |
|
31 | 32 |
|
32 | 33 | def test_launch_config_init(init_cuda): |
@@ -202,6 +203,107 @@ def test_to_native_launch_config_pdl(): |
202 | 203 | ) |
203 | 204 |
|
204 | 205 |
|
| 206 | +@pytest.mark.parametrize( |
| 207 | + ("policy", "expected_value"), |
| 208 | + [ |
| 209 | + (SynchronizationPolicyType.AUTO, driver.CUsynchronizationPolicy.CU_SYNC_POLICY_AUTO), |
| 210 | + (SynchronizationPolicyType.SPIN, driver.CUsynchronizationPolicy.CU_SYNC_POLICY_SPIN), |
| 211 | + (SynchronizationPolicyType.YIELD, driver.CUsynchronizationPolicy.CU_SYNC_POLICY_YIELD), |
| 212 | + ( |
| 213 | + SynchronizationPolicyType.BLOCKING_SYNC, |
| 214 | + driver.CUsynchronizationPolicy.CU_SYNC_POLICY_BLOCKING_SYNC, |
| 215 | + ), |
| 216 | + (driver.CUsynchronizationPolicy.CU_SYNC_POLICY_SPIN, driver.CUsynchronizationPolicy.CU_SYNC_POLICY_SPIN), |
| 217 | + ], |
| 218 | +) |
| 219 | +def test_to_native_launch_config_synchronization_policy(policy, expected_value): |
| 220 | + """LaunchConfig.synchronization_policy maps to CU_LAUNCH_ATTRIBUTE_SYNCHRONIZATION_POLICY.""" |
| 221 | + from cuda.core._launch_config import _to_native_launch_config |
| 222 | + |
| 223 | + config = LaunchConfig(grid=1, block=1, synchronization_policy=policy) |
| 224 | + assert config.synchronization_policy is SynchronizationPolicyType(int(expected_value)) |
| 225 | + |
| 226 | + native = _to_native_launch_config(config) |
| 227 | + assert native.numAttrs == 1 |
| 228 | + attr = native.attrs[0] |
| 229 | + assert attr.id == driver.CUlaunchAttributeID.CU_LAUNCH_ATTRIBUTE_SYNCHRONIZATION_POLICY |
| 230 | + assert attr.value.syncPolicy == expected_value |
| 231 | + |
| 232 | + |
| 233 | +def test_launch_config_synchronization_policy_default(): |
| 234 | + config = LaunchConfig(grid=1, block=1) |
| 235 | + assert config.synchronization_policy is None |
| 236 | + |
| 237 | + from cuda.core._launch_config import _to_native_launch_config |
| 238 | + |
| 239 | + native = _to_native_launch_config(config) |
| 240 | + assert native.numAttrs == 0 |
| 241 | + |
| 242 | + |
| 243 | +@pytest.mark.parametrize("invalid_policy", ["spin", -1, 99]) |
| 244 | +def test_launch_config_synchronization_policy_invalid(invalid_policy): |
| 245 | + with pytest.raises((TypeError, ValueError)): |
| 246 | + LaunchConfig(grid=1, block=1, synchronization_policy=invalid_policy) |
| 247 | + |
| 248 | + |
| 249 | +def test_to_native_launch_config_synchronization_policy_with_cooperative(monkeypatch): |
| 250 | + """synchronization_policy can be combined with other launch attributes.""" |
| 251 | + from cuda.core import _launch_config as _lc_mod |
| 252 | + from cuda.core._launch_config import _to_native_launch_config |
| 253 | + |
| 254 | + class _FakeProps: |
| 255 | + cooperative_launch = True |
| 256 | + |
| 257 | + class _FakeDev: |
| 258 | + properties = _FakeProps() |
| 259 | + |
| 260 | + monkeypatch.setattr(_lc_mod, "Device", lambda: _FakeDev()) |
| 261 | + |
| 262 | + config = LaunchConfig( |
| 263 | + grid=1, |
| 264 | + block=1, |
| 265 | + is_cooperative=True, |
| 266 | + synchronization_policy=SynchronizationPolicyType.SPIN, |
| 267 | + ) |
| 268 | + native = _to_native_launch_config(config) |
| 269 | + assert native.numAttrs == 2 |
| 270 | + attr_ids = {attr.id for attr in native.attrs} |
| 271 | + assert driver.CUlaunchAttributeID.CU_LAUNCH_ATTRIBUTE_COOPERATIVE in attr_ids |
| 272 | + assert driver.CUlaunchAttributeID.CU_LAUNCH_ATTRIBUTE_SYNCHRONIZATION_POLICY in attr_ids |
| 273 | + sync_attrs = [ |
| 274 | + attr |
| 275 | + for attr in native.attrs |
| 276 | + if attr.id == driver.CUlaunchAttributeID.CU_LAUNCH_ATTRIBUTE_SYNCHRONIZATION_POLICY |
| 277 | + ] |
| 278 | + assert len(sync_attrs) == 1 |
| 279 | + assert sync_attrs[0].value.syncPolicy == driver.CUsynchronizationPolicy.CU_SYNC_POLICY_SPIN |
| 280 | + |
| 281 | + |
| 282 | +@pytest.mark.parametrize( |
| 283 | + "policy", |
| 284 | + [ |
| 285 | + SynchronizationPolicyType.AUTO, |
| 286 | + SynchronizationPolicyType.SPIN, |
| 287 | + SynchronizationPolicyType.YIELD, |
| 288 | + SynchronizationPolicyType.BLOCKING_SYNC, |
| 289 | + ], |
| 290 | +) |
| 291 | +def test_launch_with_synchronization_policy(init_cuda, policy): |
| 292 | + """Driver accepts per-launch synchronization policies on a real kernel launch.""" |
| 293 | + dev = Device() |
| 294 | + dev.set_current() |
| 295 | + stream = dev.create_stream() |
| 296 | + |
| 297 | + code = 'extern "C" __global__ void noop() {}' |
| 298 | + program = Program(code, SourceCodeType.CXX) |
| 299 | + mod = program.compile(ObjectCodeFormatType.CUBIN) |
| 300 | + ker = mod.get_kernel("noop") |
| 301 | + |
| 302 | + config = LaunchConfig(grid=1, block=1, synchronization_policy=policy) |
| 303 | + launch(stream, config, ker) |
| 304 | + stream.sync() |
| 305 | + |
| 306 | + |
205 | 307 | @skipif_need_cuda_headers |
206 | 308 | def test_pdl_primary_secondary_overlap_same_stream(): |
207 | 309 | """Primary + secondary PDL launch on one stream can overlap on Hopper+. |
|
0 commit comments