@@ -72,8 +72,9 @@ from ._backend cimport (
72
72
)
73
73
from . import backend_type, device_type
74
74
from libc.stdint cimport uint32_t
75
- import warnings
76
75
from libc.stdlib cimport malloc, free
76
+ import warnings
77
+ import collections
77
78
78
79
__all__ = [
79
80
" SyclDevice" ,
@@ -102,6 +103,9 @@ cdef class _SyclDevice:
102
103
103
104
104
105
cdef list _get_devices(DPCTLDeviceVectorRef DVRef):
106
+ """
107
+ Deletes DVRef. Pass a copy in case an original reference is needed.
108
+ """
105
109
cdef list devices = []
106
110
cdef size_t nelems = 0
107
111
if DVRef:
@@ -110,6 +114,7 @@ cdef list _get_devices(DPCTLDeviceVectorRef DVRef):
110
114
DRef = DPCTLDeviceVector_GetAt(DVRef, i)
111
115
D = SyclDevice._create(DRef)
112
116
devices.append(D)
117
+ DPCTLDeviceVector_Delete(DVRef)
113
118
114
119
return devices
115
120
@@ -198,87 +203,6 @@ cdef class SyclDevice(_SyclDevice):
198
203
SyclDevice._init_helper(self , DRef)
199
204
return 0
200
205
201
- cdef _raise_sub_devices_creation_error(self , fname, errcode):
202
- e = SubDeviceCreationError(" Sub-devices were not created." )
203
- e.fname = fname
204
- e.code = errcode
205
- raise e
206
-
207
- cdef list create_sub_devices_equally(self , size_t count):
208
- """ Returns a vector of sub devices partitioned from this SYCL device
209
- based on the count parameter. The returned
210
- vector contains as many sub devices as can be created such that each sub
211
- device contains count compute units. If the device’s total number of compute
212
- units is not evenly divided by count, then the remaining compute units are
213
- not included in any of the sub devices.
214
- """
215
- cdef DPCTLDeviceVectorRef DVRef = NULL
216
- DVRef = DPCTLDevice_CreateSubDevicesEqually(self ._device_ref, count)
217
- if DVRef is NULL :
218
- self ._raise_sub_devices_creation_error(" DPCTLSubDeviceCreationError" , - 1 )
219
- cdef list devices = _get_devices(DVRef)
220
- DPCTLDeviceVector_Delete(DVRef)
221
- return devices
222
-
223
- cdef list create_sub_devices_by_counts(self , list counts):
224
- """ Returns a vector of sub devices
225
- partitioned from this SYCL device based on the counts parameter. For each
226
- non-zero value M in the counts vector, a sub device with M compute units
227
- is created.
228
- """
229
- cdef size_t ncounts = len (counts)
230
- cdef size_t * counts_buff = < size_t * > malloc(ncounts * sizeof(size_t))
231
- cdef DPCTLDeviceVectorRef DVRef = NULL
232
- cdef int i
233
- for i in range (ncounts):
234
- counts_buff[i] = counts[i]
235
- DVRef = DPCTLDevice_CreateSubDevicesByCounts(self ._device_ref, counts_buff, ncounts)
236
- if DVRef is NULL :
237
- self ._raise_sub_devices_creation_error(" DPCTLSubDeviceCreationError" , - 1 )
238
- cdef list devices = _get_devices(DVRef)
239
- free(counts_buff)
240
- DPCTLDeviceVector_Delete(DVRef)
241
- return devices
242
-
243
- cdef list create_sub_devices_by_affinity(self , _partition_affinity_domain_type domain):
244
- """ Returns a vector of sub devices
245
- partitioned from this SYCL device by affinity domain based on the domain
246
- parameter.
247
- """
248
- cdef DPCTLDeviceVectorRef DVRef = NULL
249
- DVRef = DPCTLDevice_CreateSubDevicesByAffinity(self ._device_ref, domain)
250
- if DVRef is NULL :
251
- self ._raise_sub_devices_creation_error(" DPCTLSubDeviceCreationError" , - 1 )
252
- cdef list devices = _get_devices(DVRef)
253
- DPCTLDeviceVector_Delete(DVRef)
254
- return devices
255
-
256
- def create_sub_devices (self , partition ):
257
- if isinstance (partition, int ) and partition > 0 :
258
- return self .create_sub_devices_equally(partition)
259
- elif isinstance (partition, list ) and all ([i > 0 for i in partition]):
260
- return self .create_sub_devices_by_counts(partition)
261
- elif isinstance (partition, str ):
262
- if partition == " not_applicable" :
263
- domain_type = _partition_affinity_domain_type._not_applicable
264
- elif partition == " numa" :
265
- domain_type = _partition_affinity_domain_type._numa
266
- elif partition == " L4_cache" :
267
- domain_type = _partition_affinity_domain_type._L4_cache
268
- elif partition == " L3_cache" :
269
- domain_type = _partition_affinity_domain_type._L3_cache
270
- elif partition == " L2_cache" :
271
- domain_type = _partition_affinity_domain_type._L2_cache
272
- elif partition == " L1_cache" :
273
- domain_type = _partition_affinity_domain_type._L1_cache
274
- elif partition == " next_partitionable" :
275
- domain_type = _partition_affinity_domain_type._next_partitionable
276
- else :
277
- raise Exception (' Unsupported type of domain' )
278
- return self .create_sub_devices_by_affinity(domain_type)
279
- else :
280
- raise Exception (' Unsupported type of sub-device argument' )
281
-
282
206
def __cinit__ (self , arg = None ):
283
207
cdef DPCTLSyclDeviceSelectorRef DSRef = NULL
284
208
cdef const char * filter_c_str = NULL
@@ -665,3 +589,88 @@ cdef class SyclDevice(_SyclDevice):
665
589
return (" <dpctl." + self .__name__ + " [" +
666
590
str (self .backend) + " , " + str (self .device_type) + " , " +
667
591
" " + self .device_name + " ] at {}>" .format(hex (id (self ))) )
592
+
593
+ cdef list create_sub_devices_equally(self , size_t count):
594
+ """ Returns a vector of sub devices partitioned from this SYCL device
595
+ based on the count parameter. The returned
596
+ vector contains as many sub devices as can be created such that each sub
597
+ device contains count compute units. If the device’s total number of compute
598
+ units is not evenly divided by count, then the remaining compute units are
599
+ not included in any of the sub devices.
600
+ """
601
+ cdef DPCTLDeviceVectorRef DVRef = NULL
602
+ DVRef = DPCTLDevice_CreateSubDevicesEqually(self ._device_ref, count)
603
+ if DVRef is NULL :
604
+ raise SubDeviceCreationError(" Sub-devices were not created." )
605
+ return _get_devices(DVRef)
606
+
607
+ cdef list create_sub_devices_by_counts(self , object counts):
608
+ """ Returns a vector of sub devices
609
+ partitioned from this SYCL device based on the counts parameter. For each
610
+ non-zero value M in the counts vector, a sub device with M compute units
611
+ is created.
612
+ """
613
+ cdef int ncounts = len (counts)
614
+ cdef size_t * counts_buff = NULL
615
+ cdef DPCTLDeviceVectorRef DVRef = NULL
616
+ cdef int i
617
+
618
+ if (ncounts == 0 ):
619
+ raise TypeError (" Non-empty object representing list of counts is expected." )
620
+ counts_buff = < size_t * > malloc((< size_t> ncounts) * sizeof(size_t))
621
+ if (counts_buff is NULL ):
622
+ raise MemoryError (" Allocation of counts array of size {} failed." .format(ncounts))
623
+ for i in range (ncounts):
624
+ counts_buff[i] = counts[i]
625
+ DVRef = DPCTLDevice_CreateSubDevicesByCounts(self ._device_ref, counts_buff, ncounts)
626
+ free(counts_buff)
627
+ if DVRef is NULL :
628
+ raise SubDeviceCreationError(" Sub-devices were not created." )
629
+ return _get_devices(DVRef)
630
+
631
+ cdef list create_sub_devices_by_affinity(self , _partition_affinity_domain_type domain):
632
+ """ Returns a vector of sub devices
633
+ partitioned from this SYCL device by affinity domain based on the domain
634
+ parameter.
635
+ """
636
+ cdef DPCTLDeviceVectorRef DVRef = NULL
637
+ DVRef = DPCTLDevice_CreateSubDevicesByAffinity(self ._device_ref, domain)
638
+ if DVRef is NULL :
639
+ raise SubDeviceCreationError(" Sub-devices were not created." )
640
+ return _get_devices(DVRef)
641
+
642
+ def create_sub_devices (self , **kwargs ):
643
+ if not kwargs.has_key(' partition' ):
644
+ raise TypeError (" create_sub_devices(partition=parition_spec) is expected." )
645
+ partition = kwargs.pop(' partition' )
646
+ if (kwargs):
647
+ raise TypeError (" create_sub_devices(partition=parition_spec) is expected." )
648
+ if isinstance (partition, int ) and partition > 0 :
649
+ return self .create_sub_devices_equally(partition)
650
+ elif isinstance (partition, str ):
651
+ if partition == " not_applicable" :
652
+ domain_type = _partition_affinity_domain_type._not_applicable
653
+ elif partition == " numa" :
654
+ domain_type = _partition_affinity_domain_type._numa
655
+ elif partition == " L4_cache" :
656
+ domain_type = _partition_affinity_domain_type._L4_cache
657
+ elif partition == " L3_cache" :
658
+ domain_type = _partition_affinity_domain_type._L3_cache
659
+ elif partition == " L2_cache" :
660
+ domain_type = _partition_affinity_domain_type._L2_cache
661
+ elif partition == " L1_cache" :
662
+ domain_type = _partition_affinity_domain_type._L1_cache
663
+ elif partition == " next_partitionable" :
664
+ domain_type = _partition_affinity_domain_type._next_partitionable
665
+ else :
666
+ raise TypeError (" Partition affinity domain {} is not understood." .format(partition))
667
+ return self .create_sub_devices_by_affinity(domain_type)
668
+ elif (isinstance (partition, collections.abc.Sized) and
669
+ isinstance (partition, collections.abc.Iterable)):
670
+ return self .create_sub_devices_by_counts(partition)
671
+ else :
672
+ try :
673
+ partition = int (partition)
674
+ return self .create_sub_devices_equally(partition)
675
+ except Exception as e:
676
+ raise TypeError (" Unsupported type of sub-device argument" )
0 commit comments