Skip to content

Commit 173733b

Browse files
authored
Allow opaque CUDA objects to compare equal if the underlying address is the same (NVIDIA#769)
* allow opaque structs to compare equal if the underlying address is the same * add tests * ensure __eq__ is always accompanied by __hash__
1 parent a7dc44b commit 173733b

6 files changed

Lines changed: 318 additions & 4 deletions

File tree

cuda_bindings/cuda/bindings/driver.pyx.in

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import cython
88
import ctypes
99
from libc.stdlib cimport calloc, malloc, free
1010
from libc cimport string
11-
from libc.stdint cimport int32_t, uint32_t, int64_t, uint64_t
11+
from libc.stdint cimport int32_t, uint32_t, int64_t, uint64_t, uintptr_t
1212
from libc.stddef cimport wchar_t
1313
from libc.limits cimport CHAR_MIN
1414
from libcpp.vector cimport vector
@@ -6525,6 +6525,12 @@ cdef class CUcontext:
65256525
return '<CUcontext ' + str(hex(self.__int__())) + '>'
65266526
def __index__(self):
65276527
return self.__int__()
6528+
def __eq__(self, other):
6529+
if not isinstance(other, CUcontext):
6530+
return False
6531+
return self._pvt_ptr[0] == (<CUcontext>other)._pvt_ptr[0]
6532+
def __hash__(self):
6533+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
65286534
def __int__(self):
65296535
return <void_ptr>self._pvt_ptr[0]
65306536
def getPtr(self):
@@ -6556,6 +6562,12 @@ cdef class CUmodule:
65566562
return '<CUmodule ' + str(hex(self.__int__())) + '>'
65576563
def __index__(self):
65586564
return self.__int__()
6565+
def __eq__(self, other):
6566+
if not isinstance(other, CUmodule):
6567+
return False
6568+
return self._pvt_ptr[0] == (<CUmodule>other)._pvt_ptr[0]
6569+
def __hash__(self):
6570+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
65596571
def __int__(self):
65606572
return <void_ptr>self._pvt_ptr[0]
65616573
def getPtr(self):
@@ -6587,6 +6599,12 @@ cdef class CUfunction:
65876599
return '<CUfunction ' + str(hex(self.__int__())) + '>'
65886600
def __index__(self):
65896601
return self.__int__()
6602+
def __eq__(self, other):
6603+
if not isinstance(other, CUfunction):
6604+
return False
6605+
return self._pvt_ptr[0] == (<CUfunction>other)._pvt_ptr[0]
6606+
def __hash__(self):
6607+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
65906608
def __int__(self):
65916609
return <void_ptr>self._pvt_ptr[0]
65926610
def getPtr(self):
@@ -6618,6 +6636,12 @@ cdef class CUlibrary:
66186636
return '<CUlibrary ' + str(hex(self.__int__())) + '>'
66196637
def __index__(self):
66206638
return self.__int__()
6639+
def __eq__(self, other):
6640+
if not isinstance(other, CUlibrary):
6641+
return False
6642+
return self._pvt_ptr[0] == (<CUlibrary>other)._pvt_ptr[0]
6643+
def __hash__(self):
6644+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
66216645
def __int__(self):
66226646
return <void_ptr>self._pvt_ptr[0]
66236647
def getPtr(self):
@@ -6649,6 +6673,12 @@ cdef class CUkernel:
66496673
return '<CUkernel ' + str(hex(self.__int__())) + '>'
66506674
def __index__(self):
66516675
return self.__int__()
6676+
def __eq__(self, other):
6677+
if not isinstance(other, CUkernel):
6678+
return False
6679+
return self._pvt_ptr[0] == (<CUkernel>other)._pvt_ptr[0]
6680+
def __hash__(self):
6681+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
66526682
def __int__(self):
66536683
return <void_ptr>self._pvt_ptr[0]
66546684
def getPtr(self):
@@ -6680,6 +6710,12 @@ cdef class CUarray:
66806710
return '<CUarray ' + str(hex(self.__int__())) + '>'
66816711
def __index__(self):
66826712
return self.__int__()
6713+
def __eq__(self, other):
6714+
if not isinstance(other, CUarray):
6715+
return False
6716+
return self._pvt_ptr[0] == (<CUarray>other)._pvt_ptr[0]
6717+
def __hash__(self):
6718+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
66836719
def __int__(self):
66846720
return <void_ptr>self._pvt_ptr[0]
66856721
def getPtr(self):
@@ -6711,6 +6747,12 @@ cdef class CUmipmappedArray:
67116747
return '<CUmipmappedArray ' + str(hex(self.__int__())) + '>'
67126748
def __index__(self):
67136749
return self.__int__()
6750+
def __eq__(self, other):
6751+
if not isinstance(other, CUmipmappedArray):
6752+
return False
6753+
return self._pvt_ptr[0] == (<CUmipmappedArray>other)._pvt_ptr[0]
6754+
def __hash__(self):
6755+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
67146756
def __int__(self):
67156757
return <void_ptr>self._pvt_ptr[0]
67166758
def getPtr(self):
@@ -6742,6 +6784,12 @@ cdef class CUtexref:
67426784
return '<CUtexref ' + str(hex(self.__int__())) + '>'
67436785
def __index__(self):
67446786
return self.__int__()
6787+
def __eq__(self, other):
6788+
if not isinstance(other, CUtexref):
6789+
return False
6790+
return self._pvt_ptr[0] == (<CUtexref>other)._pvt_ptr[0]
6791+
def __hash__(self):
6792+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
67456793
def __int__(self):
67466794
return <void_ptr>self._pvt_ptr[0]
67476795
def getPtr(self):
@@ -6773,6 +6821,12 @@ cdef class CUsurfref:
67736821
return '<CUsurfref ' + str(hex(self.__int__())) + '>'
67746822
def __index__(self):
67756823
return self.__int__()
6824+
def __eq__(self, other):
6825+
if not isinstance(other, CUsurfref):
6826+
return False
6827+
return self._pvt_ptr[0] == (<CUsurfref>other)._pvt_ptr[0]
6828+
def __hash__(self):
6829+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
67766830
def __int__(self):
67776831
return <void_ptr>self._pvt_ptr[0]
67786832
def getPtr(self):
@@ -6804,6 +6858,12 @@ cdef class CUevent:
68046858
return '<CUevent ' + str(hex(self.__int__())) + '>'
68056859
def __index__(self):
68066860
return self.__int__()
6861+
def __eq__(self, other):
6862+
if not isinstance(other, CUevent):
6863+
return False
6864+
return self._pvt_ptr[0] == (<CUevent>other)._pvt_ptr[0]
6865+
def __hash__(self):
6866+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
68076867
def __int__(self):
68086868
return <void_ptr>self._pvt_ptr[0]
68096869
def getPtr(self):
@@ -6835,6 +6895,12 @@ cdef class CUstream:
68356895
return '<CUstream ' + str(hex(self.__int__())) + '>'
68366896
def __index__(self):
68376897
return self.__int__()
6898+
def __eq__(self, other):
6899+
if not isinstance(other, CUstream):
6900+
return False
6901+
return self._pvt_ptr[0] == (<CUstream>other)._pvt_ptr[0]
6902+
def __hash__(self):
6903+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
68386904
def __int__(self):
68396905
return <void_ptr>self._pvt_ptr[0]
68406906
def getPtr(self):
@@ -6866,6 +6932,12 @@ cdef class CUgraphicsResource:
68666932
return '<CUgraphicsResource ' + str(hex(self.__int__())) + '>'
68676933
def __index__(self):
68686934
return self.__int__()
6935+
def __eq__(self, other):
6936+
if not isinstance(other, CUgraphicsResource):
6937+
return False
6938+
return self._pvt_ptr[0] == (<CUgraphicsResource>other)._pvt_ptr[0]
6939+
def __hash__(self):
6940+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
68696941
def __int__(self):
68706942
return <void_ptr>self._pvt_ptr[0]
68716943
def getPtr(self):
@@ -6897,6 +6969,12 @@ cdef class CUexternalMemory:
68976969
return '<CUexternalMemory ' + str(hex(self.__int__())) + '>'
68986970
def __index__(self):
68996971
return self.__int__()
6972+
def __eq__(self, other):
6973+
if not isinstance(other, CUexternalMemory):
6974+
return False
6975+
return self._pvt_ptr[0] == (<CUexternalMemory>other)._pvt_ptr[0]
6976+
def __hash__(self):
6977+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
69006978
def __int__(self):
69016979
return <void_ptr>self._pvt_ptr[0]
69026980
def getPtr(self):
@@ -6928,6 +7006,12 @@ cdef class CUexternalSemaphore:
69287006
return '<CUexternalSemaphore ' + str(hex(self.__int__())) + '>'
69297007
def __index__(self):
69307008
return self.__int__()
7009+
def __eq__(self, other):
7010+
if not isinstance(other, CUexternalSemaphore):
7011+
return False
7012+
return self._pvt_ptr[0] == (<CUexternalSemaphore>other)._pvt_ptr[0]
7013+
def __hash__(self):
7014+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
69317015
def __int__(self):
69327016
return <void_ptr>self._pvt_ptr[0]
69337017
def getPtr(self):
@@ -6959,6 +7043,12 @@ cdef class CUgraph:
69597043
return '<CUgraph ' + str(hex(self.__int__())) + '>'
69607044
def __index__(self):
69617045
return self.__int__()
7046+
def __eq__(self, other):
7047+
if not isinstance(other, CUgraph):
7048+
return False
7049+
return self._pvt_ptr[0] == (<CUgraph>other)._pvt_ptr[0]
7050+
def __hash__(self):
7051+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
69627052
def __int__(self):
69637053
return <void_ptr>self._pvt_ptr[0]
69647054
def getPtr(self):
@@ -6990,6 +7080,12 @@ cdef class CUgraphNode:
69907080
return '<CUgraphNode ' + str(hex(self.__int__())) + '>'
69917081
def __index__(self):
69927082
return self.__int__()
7083+
def __eq__(self, other):
7084+
if not isinstance(other, CUgraphNode):
7085+
return False
7086+
return self._pvt_ptr[0] == (<CUgraphNode>other)._pvt_ptr[0]
7087+
def __hash__(self):
7088+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
69937089
def __int__(self):
69947090
return <void_ptr>self._pvt_ptr[0]
69957091
def getPtr(self):
@@ -7021,6 +7117,12 @@ cdef class CUgraphExec:
70217117
return '<CUgraphExec ' + str(hex(self.__int__())) + '>'
70227118
def __index__(self):
70237119
return self.__int__()
7120+
def __eq__(self, other):
7121+
if not isinstance(other, CUgraphExec):
7122+
return False
7123+
return self._pvt_ptr[0] == (<CUgraphExec>other)._pvt_ptr[0]
7124+
def __hash__(self):
7125+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
70247126
def __int__(self):
70257127
return <void_ptr>self._pvt_ptr[0]
70267128
def getPtr(self):
@@ -7052,6 +7154,12 @@ cdef class CUmemoryPool:
70527154
return '<CUmemoryPool ' + str(hex(self.__int__())) + '>'
70537155
def __index__(self):
70547156
return self.__int__()
7157+
def __eq__(self, other):
7158+
if not isinstance(other, CUmemoryPool):
7159+
return False
7160+
return self._pvt_ptr[0] == (<CUmemoryPool>other)._pvt_ptr[0]
7161+
def __hash__(self):
7162+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
70557163
def __int__(self):
70567164
return <void_ptr>self._pvt_ptr[0]
70577165
def getPtr(self):
@@ -7083,6 +7191,12 @@ cdef class CUuserObject:
70837191
return '<CUuserObject ' + str(hex(self.__int__())) + '>'
70847192
def __index__(self):
70857193
return self.__int__()
7194+
def __eq__(self, other):
7195+
if not isinstance(other, CUuserObject):
7196+
return False
7197+
return self._pvt_ptr[0] == (<CUuserObject>other)._pvt_ptr[0]
7198+
def __hash__(self):
7199+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
70867200
def __int__(self):
70877201
return <void_ptr>self._pvt_ptr[0]
70887202
def getPtr(self):
@@ -7114,6 +7228,12 @@ cdef class CUgraphDeviceNode:
71147228
return '<CUgraphDeviceNode ' + str(hex(self.__int__())) + '>'
71157229
def __index__(self):
71167230
return self.__int__()
7231+
def __eq__(self, other):
7232+
if not isinstance(other, CUgraphDeviceNode):
7233+
return False
7234+
return self._pvt_ptr[0] == (<CUgraphDeviceNode>other)._pvt_ptr[0]
7235+
def __hash__(self):
7236+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
71177237
def __int__(self):
71187238
return <void_ptr>self._pvt_ptr[0]
71197239
def getPtr(self):
@@ -7145,6 +7265,12 @@ cdef class CUasyncCallbackHandle:
71457265
return '<CUasyncCallbackHandle ' + str(hex(self.__int__())) + '>'
71467266
def __index__(self):
71477267
return self.__int__()
7268+
def __eq__(self, other):
7269+
if not isinstance(other, CUasyncCallbackHandle):
7270+
return False
7271+
return self._pvt_ptr[0] == (<CUasyncCallbackHandle>other)._pvt_ptr[0]
7272+
def __hash__(self):
7273+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
71487274
def __int__(self):
71497275
return <void_ptr>self._pvt_ptr[0]
71507276
def getPtr(self):
@@ -7176,6 +7302,12 @@ cdef class CUgreenCtx:
71767302
return '<CUgreenCtx ' + str(hex(self.__int__())) + '>'
71777303
def __index__(self):
71787304
return self.__int__()
7305+
def __eq__(self, other):
7306+
if not isinstance(other, CUgreenCtx):
7307+
return False
7308+
return self._pvt_ptr[0] == (<CUgreenCtx>other)._pvt_ptr[0]
7309+
def __hash__(self):
7310+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
71797311
def __int__(self):
71807312
return <void_ptr>self._pvt_ptr[0]
71817313
def getPtr(self):
@@ -7205,6 +7337,12 @@ cdef class CUlinkState:
72057337
return '<CUlinkState ' + str(hex(self.__int__())) + '>'
72067338
def __index__(self):
72077339
return self.__int__()
7340+
def __eq__(self, other):
7341+
if not isinstance(other, CUlinkState):
7342+
return False
7343+
return self._pvt_ptr[0] == (<CUlinkState>other)._pvt_ptr[0]
7344+
def __hash__(self):
7345+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
72087346
def __int__(self):
72097347
return <void_ptr>self._pvt_ptr[0]
72107348
def getPtr(self):
@@ -7236,6 +7374,12 @@ cdef class CUdevResourceDesc:
72367374
return '<CUdevResourceDesc ' + str(hex(self.__int__())) + '>'
72377375
def __index__(self):
72387376
return self.__int__()
7377+
def __eq__(self, other):
7378+
if not isinstance(other, CUdevResourceDesc):
7379+
return False
7380+
return self._pvt_ptr[0] == (<CUdevResourceDesc>other)._pvt_ptr[0]
7381+
def __hash__(self):
7382+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
72397383
def __int__(self):
72407384
return <void_ptr>self._pvt_ptr[0]
72417385
def getPtr(self):
@@ -7265,6 +7409,12 @@ cdef class CUlogsCallbackHandle:
72657409
return '<CUlogsCallbackHandle ' + str(hex(self.__int__())) + '>'
72667410
def __index__(self):
72677411
return self.__int__()
7412+
def __eq__(self, other):
7413+
if not isinstance(other, CUlogsCallbackHandle):
7414+
return False
7415+
return self._pvt_ptr[0] == (<CUlogsCallbackHandle>other)._pvt_ptr[0]
7416+
def __hash__(self):
7417+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
72687418
def __int__(self):
72697419
return <void_ptr>self._pvt_ptr[0]
72707420
def getPtr(self):
@@ -7296,6 +7446,12 @@ cdef class CUeglStreamConnection:
72967446
return '<CUeglStreamConnection ' + str(hex(self.__int__())) + '>'
72977447
def __index__(self):
72987448
return self.__int__()
7449+
def __eq__(self, other):
7450+
if not isinstance(other, CUeglStreamConnection):
7451+
return False
7452+
return self._pvt_ptr[0] == (<CUeglStreamConnection>other)._pvt_ptr[0]
7453+
def __hash__(self):
7454+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
72997455
def __int__(self):
73007456
return <void_ptr>self._pvt_ptr[0]
73017457
def getPtr(self):
@@ -7325,6 +7481,12 @@ cdef class EGLImageKHR:
73257481
return '<EGLImageKHR ' + str(hex(self.__int__())) + '>'
73267482
def __index__(self):
73277483
return self.__int__()
7484+
def __eq__(self, other):
7485+
if not isinstance(other, EGLImageKHR):
7486+
return False
7487+
return self._pvt_ptr[0] == (<EGLImageKHR>other)._pvt_ptr[0]
7488+
def __hash__(self):
7489+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
73287490
def __int__(self):
73297491
return <void_ptr>self._pvt_ptr[0]
73307492
def getPtr(self):
@@ -7354,6 +7516,12 @@ cdef class EGLStreamKHR:
73547516
return '<EGLStreamKHR ' + str(hex(self.__int__())) + '>'
73557517
def __index__(self):
73567518
return self.__int__()
7519+
def __eq__(self, other):
7520+
if not isinstance(other, EGLStreamKHR):
7521+
return False
7522+
return self._pvt_ptr[0] == (<EGLStreamKHR>other)._pvt_ptr[0]
7523+
def __hash__(self):
7524+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
73577525
def __int__(self):
73587526
return <void_ptr>self._pvt_ptr[0]
73597527
def getPtr(self):
@@ -7383,6 +7551,12 @@ cdef class EGLSyncKHR:
73837551
return '<EGLSyncKHR ' + str(hex(self.__int__())) + '>'
73847552
def __index__(self):
73857553
return self.__int__()
7554+
def __eq__(self, other):
7555+
if not isinstance(other, EGLSyncKHR):
7556+
return False
7557+
return self._pvt_ptr[0] == (<EGLSyncKHR>other)._pvt_ptr[0]
7558+
def __hash__(self):
7559+
return hash(<uintptr_t><void*>(self._pvt_ptr[0]))
73867560
def __int__(self):
73877561
return <void_ptr>self._pvt_ptr[0]
73887562
def getPtr(self):

0 commit comments

Comments
 (0)