Skip to content

Commit 41bd9ab

Browse files
Diptorup Deboleksandr-pavlyk
authored andcommitted
Defines __hash__ for all classes that define __eq__ method.
- SyclContext, SyclDevice, SyclQueue classes previously where not hashable. The PR defines __hash__ function based on the C API native pointer value stored in the classes.
1 parent aa6935c commit 41bd9ab

File tree

6 files changed

+51
-0
lines changed

6 files changed

+51
-0
lines changed

dpctl/_sycl_context.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ cdef class SyclContext(_SyclContext):
335335
else:
336336
return False
337337

338+
def __hash__(self):
339+
"""
340+
Return a Py_ssize_t hash value by using the address of the
341+
``DPCTLSyclContextRef`` pointer stored in ``self._ctxt_ref``.
342+
343+
"""
344+
return hash(self.addressof_ref())
345+
338346
cdef DPCTLSyclContextRef get_context_ref(self):
339347
return self._ctxt_ref
340348

dpctl/_sycl_device.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,14 @@ cdef class SyclDevice(_SyclDevice):
709709
+ "] at {}>".format(hex(id(self)))
710710
)
711711

712+
def __hash__(self):
713+
"""
714+
Return a Py_ssize_t hash value by using the address of the
715+
``DPCTLSyclDeviceRef`` pointer stored in ``self._device_ref``.
716+
717+
"""
718+
return hash(self.addressof_ref())
719+
712720
cdef list create_sub_devices_equally(self, size_t count):
713721
""" Returns a list of sub-devices partitioned from this SYCL device
714722
based on the ``count`` parameter.

dpctl/_sycl_queue.pyx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,14 @@ cdef class SyclQueue(_SyclQueue):
863863
else:
864864
return "<dpctl." + self.__name__ + " at {}>".format(hex(id(self)))
865865

866+
def __hash__(self):
867+
"""
868+
Return a Py_ssize_t hash value by using the address of the
869+
``DPCTLSyclQueueRef`` pointer stored in ``self._queue_ref``.
870+
871+
"""
872+
return hash(self.addressof_ref())
873+
866874
def _get_capsule(self):
867875
cdef DPCTLSyclQueueRef QRef = NULL
868876
QRef = DPCTLQueue_Copy(self._queue_ref)

dpctl/tests/test_sycl_context.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,12 @@ def test_context_multi_device():
157157
shmem_1 = dpmem.MemoryUSMShared(256, queue=q1)
158158
shmem_2 = dpmem.MemoryUSMDevice(256, queue=q2)
159159
shmem_2.copy_from_device(shmem_1)
160+
161+
162+
def test_hashing_of_context():
163+
"""
164+
Test that a SyclContext object can be used as a dictionary key.
165+
166+
"""
167+
ctx_dict = {dpctl.SyclContext(): "default_context"}
168+
assert ctx_dict

dpctl/tests/test_sycl_device.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,3 +631,12 @@ def test_filter_string_method():
631631
)
632632
d_r = dpctl.SyclDevice(dev_id)
633633
assert d == d_r, "Failed "
634+
635+
636+
def test_hashing_of_device():
637+
"""
638+
Test that a SyclDevice object can be used as a dictionary key.
639+
640+
"""
641+
device_dict = {dpctl.SyclDevice(): "default_device"}
642+
assert device_dict

dpctl/tests/test_sycl_queue.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,3 +369,12 @@ def test_context_equals():
369369
ctx0 = gpuQ0.get_sycl_context()
370370
ctx1 = gpuQ1.get_sycl_context()
371371
assert ctx0 == ctx1
372+
373+
374+
def test_hashing_of_queue():
375+
"""
376+
Test that a SyclQueue object can be used as a dictionary key.
377+
378+
"""
379+
queue_dict = {dpctl.SyclQueue(): "default_queue"}
380+
assert queue_dict

0 commit comments

Comments
 (0)