|
16 | 16 |
|
17 | 17 | """Defines unit test cases for the SyclQueue class.""" |
18 | 18 |
|
| 19 | +import copy |
19 | 20 | import ctypes |
| 21 | +import pickle |
| 22 | +import subprocess |
20 | 23 | import sys |
21 | 24 |
|
22 | 25 | import pytest |
23 | 26 |
|
24 | 27 | import dpctl |
25 | 28 | import dpctl.memory |
26 | 29 |
|
27 | | -from .helper import create_invalid_capsule |
| 30 | +from .helper import ( |
| 31 | + create_invalid_capsule, |
| 32 | + get_queue_or_skip, |
| 33 | + get_sub_device_or_skip, |
| 34 | +) |
28 | 35 |
|
29 | 36 |
|
30 | 37 | def test_standard_selectors(device_selector, check): |
@@ -480,3 +487,67 @@ def test_submit_keep_args_alive_deprecated(): |
480 | 487 | ht_ev.wait() |
481 | 488 | # attributed to the caller |
482 | 489 | assert record[0].filename == __file__ |
| 490 | + |
| 491 | + |
| 492 | +@pytest.mark.parametrize( |
| 493 | + "props", |
| 494 | + [ |
| 495 | + (), |
| 496 | + ("in_order",), |
| 497 | + ("enable_profiling",), |
| 498 | + ("in_order", "enable_profiling"), |
| 499 | + ], |
| 500 | +) |
| 501 | +def test_queue_pickling(props): |
| 502 | + try: |
| 503 | + q = dpctl.SyclQueue(property=props or "default") |
| 504 | + except dpctl.SyclQueueCreationError: |
| 505 | + pytest.skip("Default-constructed queue could not be created") |
| 506 | + q2 = pickle.loads(pickle.dumps(q)) |
| 507 | + assert isinstance(q2, dpctl.SyclQueue) |
| 508 | + assert q2.sycl_device == q.sycl_device |
| 509 | + assert q2.sycl_context == q.sycl_context |
| 510 | + assert q2.is_in_order == ("in_order" in props) |
| 511 | + assert q2.has_enable_profiling == ("enable_profiling" in props) |
| 512 | + |
| 513 | + |
| 514 | +def test_queue_pickling_non_default_context(): |
| 515 | + q = get_queue_or_skip() |
| 516 | + dev = q.sycl_device |
| 517 | + ctx = dpctl.SyclContext([dev]) |
| 518 | + if ctx == q.sycl_context: |
| 519 | + pytest.skip("Context created from device list is the default context") |
| 520 | + with pytest.raises(pickle.PicklingError): |
| 521 | + pickle.dumps(dpctl.SyclQueue(ctx, dev)) |
| 522 | + |
| 523 | + |
| 524 | +def test_queue_pickling_sub_device(): |
| 525 | + sub_dev = get_sub_device_or_skip() |
| 526 | + q = dpctl.SyclQueue(sub_dev) |
| 527 | + with pytest.raises(pickle.PicklingError): |
| 528 | + pickle.dumps(q) |
| 529 | + |
| 530 | + |
| 531 | +def test_queue_unpickling_in_subprocess(): |
| 532 | + q = get_queue_or_skip() |
| 533 | + q = dpctl.SyclQueue(q.sycl_device, property="in_order") |
| 534 | + script = ( |
| 535 | + "import pickle, sys\n" |
| 536 | + "q = pickle.loads(sys.stdin.buffer.read())\n" |
| 537 | + "print(q.sycl_device.filter_string, q.is_in_order)\n" |
| 538 | + ) |
| 539 | + res = subprocess.run( |
| 540 | + [sys.executable, "-c", script], |
| 541 | + input=pickle.dumps(q), |
| 542 | + capture_output=True, |
| 543 | + check=True, |
| 544 | + ) |
| 545 | + assert res.stdout.decode().split() == [q.sycl_device.filter_string, "True"] |
| 546 | + |
| 547 | + |
| 548 | +def test_queue_copy(): |
| 549 | + q = get_queue_or_skip() |
| 550 | + assert copy.copy(q) is q |
| 551 | + assert copy.deepcopy(q) is q |
| 552 | + d = copy.deepcopy({"queue": q}) |
| 553 | + assert d["queue"] is q |
0 commit comments