Repository navigation
Expand file tree
/
Copy path_sycl_platform.pyx
More file actions
946 lines (811 loc) · 33 KB
/
Copy path_sycl_platform.pyx
File metadata and controls
946 lines (811 loc) · 33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
# Data Parallel Control (dpctl)
#
# Copyright 2021 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# distutils: language = c++
# cython: language_level=3
# cython: linetrace=True
# cython: freethreading_compatible = True
""" Implements SyclPlatform Cython extension type.
"""
from libcpp cimport bool
from ._backend cimport ( # noqa: E211
DPCTLCString_Delete,
DPCTLDeviceSelector_Delete,
DPCTLDeviceVector_Delete,
DPCTLDeviceVector_GetAt,
DPCTLDeviceVector_Size,
DPCTLDeviceVectorRef,
DPCTLFilterSelector_Create,
DPCTLPlatform_AreEq,
DPCTLPlatform_Copy,
DPCTLPlatform_Create,
DPCTLPlatform_CreateFromSelector,
DPCTLPlatform_Delete,
DPCTLPlatform_GetBackend,
DPCTLPlatform_GetCompositeDevices,
DPCTLPlatform_GetDefaultContext,
DPCTLPlatform_GetDevices,
DPCTLPlatform_GetName,
DPCTLPlatform_GetPlatforms,
DPCTLPlatform_GetVendor,
DPCTLPlatform_GetVersion,
DPCTLPlatform_HasAspect,
DPCTLPlatform_Hash,
DPCTLPlatformMgr_GetInfo,
DPCTLPlatformMgr_PrintInfo,
DPCTLPlatformVector_Delete,
DPCTLPlatformVector_GetAt,
DPCTLPlatformVector_Size,
DPCTLPlatformVectorRef,
DPCTLSyclContextRef,
DPCTLSyclDeviceRef,
DPCTLSyclDeviceSelectorRef,
DPCTLSyclPlatformRef,
_aspect_type,
_backend_type,
_device_type,
)
import warnings
from ._sycl_context import SyclContextCreationError
from .enum_types import backend_type
from .enum_types import device_type as device_type_t
from ._sycl_context cimport SyclContext
from ._sycl_device cimport SyclDevice
__all__ = [
"get_platforms",
"lsplatform",
"SyclPlatform",
]
cdef class _SyclPlatform:
""" Data owner for SyclPlatform
"""
def __dealloc__(self):
DPCTLPlatform_Delete(self._platform_ref)
DPCTLCString_Delete(self._name)
DPCTLCString_Delete(self._vendor)
DPCTLCString_Delete(self._version)
cdef void _init_helper(_SyclPlatform platform, DPCTLSyclPlatformRef PRef):
"Populate attributes of class from opaque reference PRef"
platform._platform_ref = PRef
platform._name = DPCTLPlatform_GetName(PRef)
platform._version = DPCTLPlatform_GetVersion(PRef)
platform._vendor = DPCTLPlatform_GetVendor(PRef)
cdef class SyclPlatform(_SyclPlatform):
""" SyclPlatform(self, arg=None)
Python class representing ``sycl::platform`` class.
There are two ways of creating a :class:`.SyclPlatform`
instance:
- Invoking the constructor with no arguments creates a
platform using the default selector.
:Example:
.. code-block:: python
import dpctl
# Create a SyclPlatform for default-selected device
pl = dpctl.SyclPlatform()
print(pl.name, pl.version)
- Invoking the constructor with specific filter selector string that
creates a queue for the device corresponding to the filter string.
:Example:
.. code-block:: python
import dpctl
# Create a SyclPlatform for device selected by
# filter-selector string
pl = dpctl.SyclPlatform("opencl:cpu")
print(pl.name, pl.version)
"""
@staticmethod
cdef SyclPlatform _create(DPCTLSyclPlatformRef pref):
"""
This function calls ``DPCTLPlatform_Delete(pref)``.
The user of this function must pass a copy to keep the
pref argument alive.
"""
cdef _SyclPlatform p = _SyclPlatform.__new__(_SyclPlatform)
# Initialize the attributes of the SyclPlatform object
_init_helper(<_SyclPlatform>p, pref)
return SyclPlatform(p)
cdef int _init_from__SyclPlatform(self, _SyclPlatform other):
self._platform_ref = DPCTLPlatform_Copy(other._platform_ref)
if (self._platform_ref is NULL):
return -1
self._name = DPCTLPlatform_GetName(self._platform_ref)
self._version = DPCTLPlatform_GetVersion(self._platform_ref)
self._vendor = DPCTLPlatform_GetVendor(self._platform_ref)
cdef int _init_from_cstring(self, const char *string):
cdef DPCTLSyclDeviceSelectorRef DSRef = NULL
DSRef = DPCTLFilterSelector_Create(string)
ret = self._init_from_selector(DSRef)
return ret
cdef int _init_from_selector(self, DPCTLSyclDeviceSelectorRef DSRef):
# Initialize the SyclPlatform from a DPCTLSyclDeviceSelectorRef
cdef DPCTLSyclPlatformRef PRef = DPCTLPlatform_CreateFromSelector(DSRef)
DPCTLDeviceSelector_Delete(DSRef)
if PRef is NULL:
return -1
else:
_init_helper(self, PRef)
return 0
cdef DPCTLSyclPlatformRef get_platform_ref(self):
""" Returns the ``DPCTLSyclPlatformRef`` pointer for this class.
"""
return self._platform_ref
@property
def __name__(self):
return "SyclPlatform"
def __repr__(self):
return (
"<dpctl."
+ self.__name__
+ " ["
+ self.name
+ ", "
+ self.vendor
+ ", "
+ self.version + f"] at {hex(id(self))}>"
)
def __cinit__(self, arg=None):
if type(arg) is unicode:
string = bytes(<unicode>arg, "utf-8")
filter_c_str = string
ret = self._init_from_cstring(filter_c_str)
if ret == -1:
raise ValueError(
"Could not create a SyclPlatform with the selector string"
)
elif isinstance(arg, unicode):
string = bytes(<unicode>unicode(arg), "utf-8")
filter_c_str = string
ret = self._init_from_cstring(filter_c_str)
if ret == -1:
raise ValueError(
"Could not create a SyclPlatform with the selector string"
)
elif isinstance(arg, _SyclPlatform):
ret = self._init_from__SyclPlatform(arg)
if ret == -1:
raise ValueError(
"Could not create SyclPlatform from _SyclPlatform instance"
)
elif arg is None:
PRef = DPCTLPlatform_Create()
if PRef is NULL:
raise ValueError(
"Could not create a SyclPlatform from default selector"
)
else:
_init_helper(self, PRef)
else:
raise ValueError(
"Invalid argument. Argument should be a str object specifying "
"a SYCL filter selector string."
)
def print_platform_info(self, verbosity=0):
""" Print information about the SYCL platform.
The level of information printed out by the function can be controlled
by the optional ``vebosity`` setting.
- **Verbosity level 0**: Prints out the list of platforms and their
names.
- **Verbosity level 1**: Prints out the name, version, vendor,
backend, number of devices for each platform.
- **Verbosity level 2**: At the highest level of verbosity
everything in the previous levels along with the name, version,
and filter string for each device is printed.
Args:
verbosity (Literal[0, 1, 2], optional):
The verbosity controls how much information is printed by the
function. Value ``0`` is the lowest level set by default and
``2`` is the highest level to print the most verbose output.
Default: ``0``
"""
cdef size_t v = 0
if not isinstance(verbosity, int):
warnings.warn(
"Illegal verbosity level. Accepted values are 0, 1, or 2. "
"Using the default verbosity level of 0."
)
else:
v = <size_t>(verbosity)
if v > 2:
warnings.warn(
"Illegal verbosity level. Accepted values are 0, 1, or 2. "
"Using the default verbosity level of 0."
)
v = 0
DPCTLPlatformMgr_PrintInfo(self._platform_ref, v)
@property
def vendor(self):
"""
Returns the platform vendor name as a string.
Returns:
str:
Vendor name
"""
return self._vendor.decode()
@property
def version(self):
""" Returns a backend-defined driver version as a string.
Returns:
str:
Version of the backend-defined driver
"""
return self._version.decode()
@property
def name(self):
""" Returns the name of the platform as a string.
Returns:
str:
Name of the platform
"""
return self._name.decode()
@property
def backend(self):
"""Returns the backend_type enum value for this platform
Returns:
backend_type:
The backend for the platform.
"""
cdef _backend_type BTy = (
DPCTLPlatform_GetBackend(self._platform_ref)
)
if BTy == _backend_type._CUDA:
return backend_type.cuda
elif BTy == _backend_type._HIP:
return backend_type.hip
elif BTy == _backend_type._LEVEL_ZERO:
return backend_type.level_zero
elif BTy == _backend_type._OPENCL:
return backend_type.opencl
elif BTy == _backend_type._OPENMP:
return backend_type.openmp
else:
raise ValueError("Unknown backend type.")
@property
def has_aspect_cpu(self):
""" Returns ``True`` if all devices of this platform (if any) are CPU
devices, ``False`` otherwise.
:Example:
.. code-block:: python
>>> import dpctl
>>> pl = dpctl.SyclPlatform("opencl:cpu")
>>> pl.has_aspect_cpu
True
Returns:
bool:
Indicates whether all devices of the platform are cpus.
"""
cdef _aspect_type AT = _aspect_type._cpu
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_gpu(self):
""" Returns ``True`` if all devices of this platform (if any) are GPU
devices, ``False`` otherwise.
Returns:
bool:
Indicates whether all devices of the platform are gpus.
"""
cdef _aspect_type AT = _aspect_type._gpu
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_accelerator(self):
""" Returns ``True`` if all devices of this platform (if any) are
accelerator devices, ``False`` otherwise.
SYCL considers an accelerator to be a device that usually uses a
peripheral interconnect for communication.
Returns:
bool:
Indicates whether all devices of the platform are
accelerators.
"""
cdef _aspect_type AT = _aspect_type._accelerator
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_custom(self):
""" Returns ``True`` if all devices of this platform (if any) are
custom devices, ``False`` otherwise.
A custom device can be a dedicated accelerator that can use the
SYCL API, but programmable kernels cannot be dispatched to the device,
only fixed functionality is available. Refer SYCL spec for more details.
Returns:
bool:
Indicates whether all devices of the platform are custom SYCL
devices.
"""
cdef _aspect_type AT = _aspect_type._custom
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_fp16(self):
""" Returns ``True`` if all devices of this platform (if any) support
half-precision floating point operations, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support half
precision floating point operations.
"""
cdef _aspect_type AT = _aspect_type._fp16
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_fp64(self):
""" Returns ``True`` if all devices of this platform (if any) support
64-bit precision floating point operations, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support 64-bit
precision floating point operations.
"""
cdef _aspect_type AT = _aspect_type._fp64
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_atomic64(self):
""" Returns ``True`` if all devices of this platform (if any) support a
basic set of atomic operations, ``False`` otherwise.
Indicates that the devices support the following atomic operations on
64-bit values:
- ``sycl::atomic_ref::load``
- ``sycl::atomic_ref::store``
- ``sycl::atomic_ref::fetch_add``
- ``sycl::atomic_ref::fetch_sub``
- ``sycl::atomic_ref::exchange``
- ``sycl::atomic_ref::compare_exchange_strong``
- ``sycl::atomic_ref::compare_exchange_weak``
Returns:
bool:
Indicates that all devices of the platform support a basic set
of atomic operations on 64-bit values.
"""
cdef _aspect_type AT = _aspect_type._atomic64
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_image(self):
""" Returns ``True`` if all devices of this platform (if any) support
images, ``False`` otherwise (refer Sec 4.15.3 of SYCL 2020 spec).
Returns:
bool:
Indicates that all devices of the platform support images.
"""
cdef _aspect_type AT = _aspect_type._image
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_online_compiler(self):
""" Returns ``True`` if all devices of this platform (if any) support
online compilation of device code, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support online
compilation of device code.
"""
cdef _aspect_type AT = _aspect_type._online_compiler
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_online_linker(self):
""" Returns ``True`` if all devices of this platform (if any) support
online linking of device code, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support online
linking of device code.
"""
cdef _aspect_type AT = _aspect_type._online_linker
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_queue_profiling(self):
""" Returns ``True`` if all devices of this platform (if any) support
queue profiling, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support queue
profiling.
"""
cdef _aspect_type AT = _aspect_type._queue_profiling
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_usm_device_allocations(self):
""" Returns ``True`` if all devices of this platform (if any) support
explicit USM allocations, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support explicit USM
allocations.
"""
cdef _aspect_type AT = _aspect_type._usm_device_allocations
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_usm_host_allocations(self):
""" Returns ``True`` if all devices of this platform (if any) can
access USM memory allocated via ``sycl::malloc_host``, ``False``
otherwise.
Returns:
bool:
Indicates that all devices of the platform can access USM
memory allocated via ``sycl::malloc_host``.
"""
cdef _aspect_type AT = _aspect_type._usm_host_allocations
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_usm_shared_allocations(self):
""" Returns ``True`` if all devices of this platform (if any) support
USM memory allocated via ``sycl::malloc_shared``, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support USM memory
allocated via ``sycl::malloc_shared``.
"""
cdef _aspect_type AT = _aspect_type._usm_shared_allocations
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_usm_system_allocations(self):
""" Returns ``True`` if the system allocator may be used instead of
SYCL USM allocation mechanisms for ``shared`` allocations on all
devices of this platform (if any), ``False`` otherwise.
Returns:
bool:
Indicates that the system allocator may be used instead of
SYCL USM allocation mechanisms for ``shared`` allocations.
"""
cdef _aspect_type AT = _aspect_type._usm_system_allocations
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_usm_atomic_host_allocations(self):
""" Returns ``True`` if all devices of this platform (if any) support
USM memory allocated via ``sycl::malloc_host`` and can be accessed
atomically, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support USM memory
allocated via ``sycl::malloc_host`` and can be accessed
atomically.
"""
cdef _aspect_type AT = _aspect_type._usm_atomic_host_allocations
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_usm_atomic_shared_allocations(self):
""" Returns ``True`` if all devices of this platform (if any) support
USM memory allocated via ``sycl::malloc_shared`` and can be accessed
atomically, ``False`` otherwise.
Returns:
bool:
Indicates that all devices of the platform support USM memory
allocated via ``sycl::malloc_shared`` and can be accessed
atomically.
"""
cdef _aspect_type AT = _aspect_type._usm_atomic_shared_allocations
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_host_debuggable(self):
""" Returns ``True`` if kernels running on all devices of this platform
(if any) can be debugged using standard debuggers that are normally
available on the host system, ``False`` otherwise.
Returns:
bool:
Indicates that kernels running on all devices of the platform
can be debugged using standard debuggers that are normally
available on the host system.
"""
cdef _aspect_type AT = _aspect_type._host_debuggable
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_emulated(self):
""" Returns ``True`` if all devices of this platform (if any) are
somehow emulated, ``False`` otherwise. A device with this aspect is not
intended for performance, and instead will generally have another
non-emulated platform as a better representation for performance.
Returns:
bool:
Indicates that all devices of the platform are emulated.
"""
cdef _aspect_type AT = _aspect_type._emulated
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_is_component(self):
""" Returns ``True`` if all devices of this platform (if any) are
component devices, ``False`` otherwise. A device with this aspect will
have a composite device from which it is descended. Always ``False``
if the SYCL implementation does not support the aspect.
Returns:
bool:
Indicates if all devices of the platform are component devices.
"""
cdef _aspect_type AT = _aspect_type._is_component
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_is_composite(self):
""" Returns ``True`` if all devices of this platform (if any) are
composite devices, ``False`` otherwise. A device with this aspect
contains component devices. Always ``False`` if the SYCL
implementation does not support the aspect.
Returns:
bool:
Indicates if all devices of the platform are composite devices.
"""
cdef _aspect_type AT = _aspect_type._is_composite
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def has_aspect_ext_oneapi_ipc_memory(self):
""" Returns ``True`` if all devices of this platform (if any) support
inter-process communication (IPC) memory handles, ``False`` otherwise.
Always ``False`` if the SYCL implementation does not support the
aspect.
Returns:
bool:
Indicates if all devices of the platform support IPC memory.
"""
cdef _aspect_type AT = _aspect_type._ext_oneapi_ipc_memory
return DPCTLPlatform_HasAspect(self._platform_ref, AT)
@property
def default_context(self):
"""Returns the default platform context for this platform
Returns:
:class:`dpctl.SyclContext`
The default context for the platform.
Raises:
SyclContextCreationError
If default_context is not supported
"""
cdef DPCTLSyclContextRef CRef = (
DPCTLPlatform_GetDefaultContext(self._platform_ref)
)
if (CRef == NULL):
raise SyclContextCreationError(
"Getting default_context ran into a problem"
)
else:
return SyclContext._create(CRef)
cdef bool equals(self, SyclPlatform other):
"""
Returns true if the :class:`dpctl.SyclPlatform` argument has the
same underlying ``DPCTLSyclPlatformRef`` object as this
:class:`dpctl.SyclPlatform` instance.
Returns:
bool:
``True`` if the two :class:`dpctl.SyclPlatform` objects
point to the same ``DPCTLSyclPlatformRef`` object, otherwise
``False``.
"""
return DPCTLPlatform_AreEq(self._platform_ref, other.get_platform_ref())
def __eq__(self, other):
"""
Returns True if the :class:`dpctl.SyclPlatform` argument has the
same underlying ``DPCTLSyclPlatformRef`` object as this
:class:`dpctl.SyclPlatform` instance.
Returns:
bool:
``True`` if the two :class:`dpctl.SyclPlatform` objects
point to the same ``DPCTLSyclPlatformRef`` object, otherwise
``False``.
"""
if isinstance(other, SyclPlatform):
return self.equals(<SyclPlatform> other)
else:
return False
def __hash__(self):
"""
Returns a hash value by hashing the underlying ``sycl::platform``
object.
Returns:
int:
Hash value
"""
return DPCTLPlatform_Hash(self._platform_ref)
def get_devices(self, device_type=device_type_t.all):
"""
Returns a tuple of :class:`dpctl.SyclDevice` objects associated with
:class:`dpctl.SyclPlatform` instance selected based on
the given :class:`dpctl.device_type`.
Args:
device_type (str, :class:`dpctl.device_type`, optional):
A :class:`dpctl.device_type` enum value or a string that
specifies a SYCL device type. Currently, accepted values are:
"gpu", "cpu", "accelerator", "automatic", "custom", or "all",
and their equivalent ``dpctl.device_type`` enumerators.
Only OpenCL platforms support custom devices; for other
platforms, "custom" returns an empty tuple.
Default: ``dpctl.device_type.all``.
Returns:
Tuple[:class:`dpctl.SyclDevice`]:
A tuple of :class:`dpctl.SyclDevice` objects
that belong to this platform.
Raises:
TypeError:
If `device_type` is not a string or :class:`dpctl.device_type`
enum.
ValueError:
If the ``DPCTLPlatform_GetDevices`` call returned
``NULL`` instead of a ``DPCTLDeviceVectorRef`` object.
"""
cdef _device_type DTy = _device_type._ALL_DEVICES
cdef DPCTLDeviceVectorRef DVRef = NULL
cdef size_t num_devs
cdef size_t i
cdef DPCTLSyclDeviceRef DRef
if isinstance(device_type, str):
dty_str = device_type.strip().lower()
if dty_str == "accelerator":
DTy = _device_type._ACCELERATOR
elif dty_str == "all":
DTy = _device_type._ALL_DEVICES
elif dty_str == "automatic":
DTy = _device_type._AUTOMATIC
elif dty_str == "cpu":
DTy = _device_type._CPU
elif dty_str == "custom":
DTy = _device_type._CUSTOM
elif dty_str == "gpu":
DTy = _device_type._GPU
else:
DTy = _device_type._UNKNOWN_DEVICE
elif isinstance(device_type, device_type_t):
if device_type == device_type_t.accelerator:
DTy = _device_type._ACCELERATOR
elif device_type == device_type_t.all:
DTy = _device_type._ALL_DEVICES
elif device_type == device_type_t.automatic:
DTy = _device_type._AUTOMATIC
elif device_type == device_type_t.cpu:
DTy = _device_type._CPU
elif device_type == device_type_t.custom:
DTy = _device_type._CUSTOM
elif device_type == device_type_t.gpu:
DTy = _device_type._GPU
else:
DTy = _device_type._UNKNOWN_DEVICE
else:
raise TypeError(
"device type should be specified as a str or an "
"``enum_types.device_type``."
)
DVRef = DPCTLPlatform_GetDevices(self.get_platform_ref(), DTy)
if (DVRef is NULL):
raise ValueError("Internal error: NULL device vector encountered")
num_devs = DPCTLDeviceVector_Size(DVRef)
devices = []
for i in range(num_devs):
DRef = DPCTLDeviceVector_GetAt(DVRef, i)
devices.append(SyclDevice._create(DRef))
DPCTLDeviceVector_Delete(DVRef)
return tuple(devices)
def get_composite_devices(self):
"""
Returns a tuple of composite :class:`dpctl.SyclDevice` objects
associated with :class:`dpctl.SyclPlatform` instance.
Returns:
Tuple[:class:`dpctl.SyclDevice`]:
A tuple of composite :class:`dpctl.SyclDevice` objects
that belong to this platform.
Raises:
ValueError:
If the ``DPCTLPlatform_GetCompositeDevices`` call returned
``NULL`` instead of a ``DPCTLDeviceVectorRef`` object.
"""
cdef DPCTLDeviceVectorRef DVRef = NULL
cdef size_t num_devs
cdef size_t i
cdef DPCTLSyclDeviceRef DRef
DVRef = DPCTLPlatform_GetCompositeDevices(self.get_platform_ref())
if (DVRef is NULL):
raise ValueError("Internal error: NULL device vector encountered")
num_devs = DPCTLDeviceVector_Size(DVRef)
composite_devices = []
for i in range(num_devs):
DRef = DPCTLDeviceVector_GetAt(DVRef, i)
composite_devices.append(SyclDevice._create(DRef))
DPCTLDeviceVector_Delete(DVRef)
return tuple(composite_devices)
def lsplatform(verbosity=0):
"""
Prints out the list of available SYCL platforms, and optionally extra
metadata about each platform.
The level of information printed out by the function can be controlled by
the optional ``vebosity`` setting.
- **Verbosity level 0**: Prints out the list of platforms and their names.
- **Verbosity level 1**: Prints out the name, version, vendor, backend,
number of devices for each platform.
- **Verbosity level 2**: At the highest level of verbosity everything in the
previous levels along with the name, version, and filter string for each
device is printed.
At verbosity level 2 (highest level) the following output is generated.
:Example:
On a system with an OpenCL CPU driver, OpenCL GPU driver,
Level Zero GPU driver, running the command:
.. code-block:: bash
$ python -c "import dpctl; dpctl.lsplatform(verbosity=2)"
outputs
.. code-block:: text
:caption: Sample output of lsplatform(verbosity=2)
Platform 0 ::
Name Intel(R) OpenCL
Version OpenCL 2.1 LINUX
Vendor Intel(R) Corporation
Backend opencl
Num Devices 1
# 0
Name Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Version 2020.11.11.0.13_160000
Filter string opencl:cpu:0
Platform 1 ::
Name Intel(R) OpenCL HD Graphics
Version OpenCL 3.0
Vendor Intel(R) Corporation
Backend opencl
Num Devices 1
# 0
Name Intel(R) Graphics Gen9 [0x3e98]
Version 20.47.18513
Filter string opencl:gpu:0
Platform 2 ::
Name Intel(R) Level-Zero
Version 1.0
Vendor Intel(R) Corporation
Backend level_zero
Num Devices 1
# 0
Name Intel(R) Graphics Gen9 [0x3e98]
Version 1.0.18513
Filter string level_zero:gpu:0
Args:
verbosity (Literal[0,1,2], optional):
The verbosity controls how much information is printed by the
function. 0 is the lowest level set by default and 2 is the highest
level to print the most verbose output. Default: `0`.
"""
cdef DPCTLPlatformVectorRef PVRef = NULL
cdef size_t v = 0
cdef size_t size = 0
cdef const char * info_str = NULL
cdef DPCTLSyclPlatformRef PRef = NULL
if not isinstance(verbosity, int):
warnings.warn(
"Illegal verbosity level. Accepted values are 0, 1, or 2. "
"Using the default verbosity level of 0."
)
else:
v = <size_t>(verbosity)
if v > 2:
warnings.warn(
"Illegal verbosity level. Accepted values are 0, 1, or 2. "
"Using the default verbosity level of 0."
)
v = 0
PVRef = DPCTLPlatform_GetPlatforms()
if PVRef is not NULL:
size = DPCTLPlatformVector_Size(PVRef)
for i in range(size):
if v != 0:
print("Platform ", i, "::")
PRef = DPCTLPlatformVector_GetAt(PVRef, i)
info_str = DPCTLPlatformMgr_GetInfo(PRef, v)
py_info = <bytes> info_str
DPCTLCString_Delete(info_str)
DPCTLPlatform_Delete(PRef)
print(py_info.decode("utf-8"), end="")
DPCTLPlatformVector_Delete(PVRef)
cpdef tuple get_platforms():
"""
Returns a tuple of all available SYCL platforms on the system.
Returns:
Tuple[:class:`.SyclPlatform`]:
A tuple of SYCL platforms on the system.
"""
cdef list platforms = []
cdef DPCTLPlatformVectorRef PVRef = NULL
cdef size_t size = 0
PVRef = DPCTLPlatform_GetPlatforms()
if PVRef is not NULL:
size = DPCTLPlatformVector_Size(PVRef)
for i in range(size):
PRef = DPCTLPlatformVector_GetAt(PVRef, i)
P = SyclPlatform._create(PRef)
platforms.append(P)
DPCTLPlatformVector_Delete(PVRef)
return tuple(platforms)