Skip to content

Make dpctl tensor device hashable, added equality comparison #1048

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 28, 2023
Merged
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
12 changes: 12 additions & 0 deletions dpctl/tensor/_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,18 @@ def wait(self):
"""
self.sycl_queue_.wait()

def __eq__(self, other):
"""Equality comparison based on underlying ``sycl_queue``."""
if isinstance(other, Device):
return self.sycl_queue.__eq__(other.sycl_queue)
elif isinstance(other, dpctl.SyclQueue):
return self.sycl_queue.__eq__(other)
return False

def __hash__(self):
"""Compute object's hash value."""
return self.sycl_queue.__hash__()


def normalize_queue_device(sycl_queue=None, device=None):
"""
Expand Down
16 changes: 16 additions & 0 deletions dpctl/tests/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,3 +1575,19 @@ def test_asarray_uint64():
Xnp = np.ndarray(1, dtype=np.uint64)
X = dpt.asarray(Xnp)
assert X.dtype == Xnp.dtype


def test_Device():
try:
dev = dpctl.select_default_device()
d1 = dpt.Device.create_device(dev)
d2 = dpt.Device.create_device(dev)
except (dpctl.SyclQueueCreationError, dpctl.SyclDeviceCreationError):
pytest.skip(
"Could not create default device, or a queue that targets it"
)
assert d1 == d2
dict = {d1: 1}
assert dict[d2] == 1
assert d1 == d2.sycl_queue
assert not d1 == Ellipsis