Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dpctl/tests/helper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from ._helper import (
create_invalid_capsule,
get_queue_or_skip,
get_spirv_queue_or_skip,
get_sub_device_or_skip,
has_cpu,
has_gpu,
Expand All @@ -34,6 +35,7 @@
"has_gpu",
"has_sycl_platforms",
"get_queue_or_skip",
"get_spirv_queue_or_skip",
"get_sub_device_or_skip",
"is_adaptivecpp",
"is_wsl_or_windows",
Expand Down
13 changes: 13 additions & 0 deletions dpctl/tests/helper/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ def get_queue_or_skip(args=()):
return q


def get_spirv_queue_or_skip():
"""Returns a default queue that accepts SPIR-V kernel bundles, or skips
the test.
"""
q = get_queue_or_skip()
if q.backend not in (
dpctl.backend_type.opencl,
dpctl.backend_type.level_zero,
):
pytest.skip(f"SPIR-V kernel bundles are not supported on {q.backend}")
return q


def get_sub_device_or_skip():
for dev in dpctl.get_devices():
for partition in ("next_partitionable", [1, 1]):
Expand Down
17 changes: 5 additions & 12 deletions dpctl/tests/test_sycl_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import dpctl.memory as dpm
from dpctl.compiler.utils import parse_spirv_specializations

from .helper import get_spirv_queue_or_skip


def _get_opencl_queue_or_skip():
try:
Expand Down Expand Up @@ -234,10 +236,7 @@ def test_create_kernel_bundle_from_source_ocl():

@pytest.mark.unsupported_on_acpp
def test_get_sycl_kernel_undefined_name():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create default queue")
q = get_spirv_queue_or_skip()
spirv_file = get_spirv_abspath("multi_kernel.spv")
with open(spirv_file, "rb") as fin:
spirv = fin.read()
Expand Down Expand Up @@ -297,10 +296,7 @@ def test_create_kernel_bundle_from_invalid_src_ocl():

@pytest.mark.unsupported_on_acpp
def test_create_kernel_bundle_with_spec_const():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create default queue")
q = get_spirv_queue_or_skip()

spec_id = 0
sp = dpc.SpecializationConstant(spec_id, "i4", 42)
Expand Down Expand Up @@ -332,10 +328,7 @@ def test_create_kernel_bundle_with_spec_const():

@pytest.mark.unsupported_on_acpp
def test_create_kernel_bundle_with_composite_spec_const():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create default queue")
q = get_spirv_queue_or_skip()

# composite specialization constants are separated into individual
# specialization constants with unique spec_ids
Expand Down
98 changes: 87 additions & 11 deletions dpctl/tests/test_sycl_ipc_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"""Tests for IPC memory handle functionality."""

import gc
import subprocess
import sys

import pytest

Expand Down Expand Up @@ -57,6 +59,14 @@ def _get_ipc_queue():
return dpctl.SyclQueue(dev)


def _get_local_open_ipc_queue():
"""Return an IPC queue whose handles this process can open itself."""
q = _get_ipc_queue()
if q.backend == dpctl.backend_type.cuda:
pytest.skip("CUDA cannot open an IPC handle in the exporting process")
return q


# ─── Handle creation tests ─────────────────────────────────────────────


Expand Down Expand Up @@ -88,7 +98,7 @@ def test_get_handle_rejects_host_memory(self):

class TestIPCOpenClose:
def test_open_returns_ipc_device_memory(self):
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -99,7 +109,7 @@ def test_open_returns_ipc_device_memory(self):
assert mapped.nbytes == 4096

def test_close_mapping_nulls_pointer(self):
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -112,7 +122,7 @@ def test_close_mapping_nulls_pointer(self):
assert mapped.nbytes == 0

def test_close_mapping_idempotent(self):
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand Down Expand Up @@ -151,7 +161,7 @@ def test_host_memory_is_not_ipc(self):
assert not isinstance(mem, MemoryIPCDevice)

def test_ipc_mapped_memory_is_ipc_type(self):
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -160,7 +170,7 @@ def test_ipc_mapped_memory_is_ipc_type(self):
assert isinstance(mapped, MemoryIPCDevice)

def test_type_persists_after_close_mapping(self):
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -170,7 +180,7 @@ def test_type_persists_after_close_mapping(self):
assert isinstance(mapped, MemoryIPCDevice)

def test_is_closed_property(self):
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -188,7 +198,7 @@ def test_is_closed_property(self):
class TestIPCLifetime:
def test_source_memory_outlives_mapped(self):
"""Source memory must stay alive while receiver uses the mapping."""
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -204,7 +214,7 @@ def test_source_memory_outlives_mapped(self):
def test_del_mapped_memory_does_not_crash(self):
"""Deleting IPC-mapped memory should call CloseHandle, not
sycl::free, and not crash."""
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -219,7 +229,7 @@ def test_del_mapped_memory_does_not_crash(self):
def test_del_mapped_memory_after_explicit_close(self):
"""If close_mapping was already called, __dealloc__ should
be a no-op (ptr is NULL)."""
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -234,7 +244,7 @@ def test_del_mapped_memory_after_explicit_close(self):
def test_multiple_opens_from_same_handle(self):
"""Each open() produces an independent mapping that must be
closed independently."""
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(4096, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -252,7 +262,7 @@ def test_multiple_opens_from_same_handle(self):

def test_del_mapped_via_gc(self):
"""Full lifecycle: get handle, open, use, let GC collect mapped."""
q = _get_ipc_queue()
q = _get_local_open_ipc_queue()
dev = q.sycl_device
mem = MemoryUSMDevice(2048, queue=q)
handle_bytes = SyclIPCGetMemHandle(mem)
Expand All @@ -267,3 +277,69 @@ def test_del_mapped_via_gc(self):

# Source is still valid after receiver is gone
assert mem.nbytes == 2048


# ─── Cross-process tests ──────────────────────────────────────────────

# The child exports and the parent opens: Level Zero handles carry a file
# descriptor that only a process allowed to ptrace the exporter can import.
_EXPORTER_SRC = """
import sys

import dpctl
from dpctl.memory import MemoryUSMDevice, SyclIPCGetMemHandle

nbytes = int(sys.argv[2])
mem = MemoryUSMDevice(nbytes, queue=dpctl.SyclQueue(sys.argv[1]))
mem.copy_from_host(bytes(i % 256 for i in range(nbytes)))
print(SyclIPCGetMemHandle(mem).hex(), flush=True)
sys.stdin.readline()
assert bytes(mem.copy_to_host()) == bytes(255 - i % 256 for i in range(nbytes))
"""


class TestIPCAcrossProcesses:
@pytest.mark.parametrize("close", ["explicit", "dealloc"])
def test_open_handle_from_another_process(self, close, tmp_path):
dev = _get_ipc_device()
nbytes = 4096
with (
open(tmp_path / "stderr", "w+") as err,
subprocess.Popen(
[
sys.executable,
"-c",
_EXPORTER_SRC,
dev.filter_string,
str(nbytes),
],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=err,
text=True,
) as child,
):
try:
handle_hex = child.stdout.readline().strip()
assert handle_hex, "exporter exited without a handle"
mapped = SyclIPCOpenMemHandle(
bytes.fromhex(handle_hex), dev, nbytes
)
assert isinstance(mapped, MemoryIPCDevice)
assert mapped.nbytes == nbytes
assert bytes(mapped.copy_to_host()) == bytes(
i % 256 for i in range(nbytes)
)
mapped.copy_from_host(
bytes(255 - i % 256 for i in range(nbytes))
)
if close == "explicit":
SyclIPCCloseMemHandle(mapped)
assert mapped.is_closed
else:
del mapped
gc.collect()
finally:
child.communicate("\n", timeout=120)
err.seek(0)
assert child.returncode == 0, err.read()
12 changes: 4 additions & 8 deletions dpctl/tests/test_sycl_kernel_submit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import dpctl.memory as dpm
from dpctl._sycl_queue import kernel_arg_type

from .helper import get_spirv_queue_or_skip


@pytest.mark.parametrize(
"ctype_str,dtype,ctypes_ctor",
Expand Down Expand Up @@ -459,10 +461,7 @@ def test_submit_wrong_number_of_args_sycl_source(backend):

@pytest.mark.unsupported_on_acpp
def test_submit_wrong_number_of_args():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create default queue")
q = get_spirv_queue_or_skip()
fn = get_spirv_abspath("multi_kernel.spv")
with open(fn, "br") as f:
spirv_bytes = f.read()
Expand Down Expand Up @@ -505,10 +504,7 @@ def test_submit_wrong_number_of_args():
# int main() { return 0; }
@pytest.mark.unsupported_on_acpp
def test_submit_local_accessor_arg_spirv_from_sycl():
try:
q = dpctl.SyclQueue()
except dpctl.SyclQueueCreationError:
pytest.skip("Could not create default queue")
q = get_spirv_queue_or_skip()
fn = get_spirv_abspath("local-accessor-free-function-kernel.spv")
with open(fn, "br") as f:
spirv_bytes = f.read()
Expand Down
Loading