Skip to content
Closed
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
8 changes: 8 additions & 0 deletions dpctl/_sycl_context.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ cdef class SyclContext(_SyclContext):
else:
return False

def __hash__(self):
"""
Return a Py_ssize_t hash value by using the address of the
``DPCTLSyclContextRef`` pointer stored in ``self._ctxt_ref``.

"""
return hash(self.addressof_ref())

cdef DPCTLSyclContextRef get_context_ref(self):
return self._ctxt_ref

Expand Down
8 changes: 8 additions & 0 deletions dpctl/_sycl_device.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,14 @@ cdef class SyclDevice(_SyclDevice):
+ "] at {}>".format(hex(id(self)))
)

def __hash__(self):
"""
Return a Py_ssize_t hash value by using the address of the
``DPCTLSyclDeviceRef`` pointer stored in ``self._device_ref``.

"""
return hash(self.addressof_ref())

cdef list create_sub_devices_equally(self, size_t count):
""" Returns a list of sub-devices partitioned from this SYCL device
based on the ``count`` parameter.
Expand Down
8 changes: 8 additions & 0 deletions dpctl/_sycl_queue.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,14 @@ cdef class SyclQueue(_SyclQueue):
else:
return "<dpctl." + self.__name__ + " at {}>".format(hex(id(self)))

def __hash__(self):
"""
Return a Py_ssize_t hash value by using the address of the
``DPCTLSyclQueueRef`` pointer stored in ``self._queue_ref``.

"""
return hash(self.addressof_ref())

def _get_capsule(self):
cdef DPCTLSyclQueueRef QRef = NULL
QRef = DPCTLQueue_Copy(self._queue_ref)
Expand Down
9 changes: 9 additions & 0 deletions dpctl/tests/test_sycl_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,12 @@ def test_context_multi_device():
shmem_1 = dpmem.MemoryUSMShared(256, queue=q1)
shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2)
shmem_2.copy_from_device(shmem_1)


def test_hashing_of_context():
"""
Test that a SyclContext object can be used as a dictionary key.

"""
ctx_dict = {dpctl.SyclContext(): "default_context"}
assert ctx_dict
9 changes: 9 additions & 0 deletions dpctl/tests/test_sycl_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,3 +631,12 @@ def test_filter_string_method():
)
d_r = dpctl.SyclDevice(dev_id)
assert d == d_r, "Failed "


def test_hashing_of_device():
"""
Test that a SyclDevice object can be used as a dictionary key.

"""
device_dict = {dpctl.SyclDevice(): "default_device"}
assert device_dict
9 changes: 9 additions & 0 deletions dpctl/tests/test_sycl_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,12 @@ def test_context_equals():
ctx0 = gpuQ0.get_sycl_context()
ctx1 = gpuQ1.get_sycl_context()
assert ctx0 == ctx1


def test_hashing_of_queue():
"""
Test that a SyclQueue object can be used as a dictionary key.

"""
queue_dict = {dpctl.SyclQueue(): "default_queue"}
assert queue_dict