|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2020-2021 Intel Corporation |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +import dpctl |
| 17 | + |
| 18 | + |
| 19 | +class Device: |
| 20 | + """ |
| 21 | + Class representing Data-API concept of device. |
| 22 | +
|
| 23 | + This is a wrapper around :class:`dpctl.SyclQueue` with custom |
| 24 | + formatting. The class does not have public constructor, |
| 25 | + but a class method to construct it from device= keyword |
| 26 | + in Array-API functions. |
| 27 | +
|
| 28 | + Instance can be queried for ``sycl_queue``, ``sycl_context``, |
| 29 | + or ``sycl_device``. |
| 30 | + """ |
| 31 | + |
| 32 | + def __new__(cls, *args, **kwargs): |
| 33 | + raise TypeError("No public constructor") |
| 34 | + |
| 35 | + @classmethod |
| 36 | + def create_device(cls, dev): |
| 37 | + """ |
| 38 | + Device.create_device(device) |
| 39 | +
|
| 40 | + Creates instance of Device from argument. |
| 41 | +
|
| 42 | + Args: |
| 43 | + device: None, :class:`.Device`, :class:`dpctl.SyclQueue`, or |
| 44 | + a :class:`dpctl.SyclDevice` corresponding to a root |
| 45 | + SYCL device. |
| 46 | + Raises: |
| 47 | + ValueError: if an instance of :class:`dpctl.SycDevice` corresponding |
| 48 | + to a sub-device was specified as the argument |
| 49 | + SyclQueueCreationError: if :class:`dpctl.SyclQueue` could not be |
| 50 | + created from the argument |
| 51 | + """ |
| 52 | + obj = super().__new__(cls) |
| 53 | + if isinstance(dev, Device): |
| 54 | + obj.sycl_queue_ = dev.sycl_queue |
| 55 | + elif isinstance(dev, dpctl.SyclQueue): |
| 56 | + obj.sycl_queue_ = dev |
| 57 | + elif isinstance(dev, dpctl.SyclDevice): |
| 58 | + par = dev.parent_device |
| 59 | + if par is None: |
| 60 | + obj.sycl_queue_ = dpctl.SyclQueue(dev) |
| 61 | + else: |
| 62 | + raise ValueError( |
| 63 | + "Using non-root device {} to specify offloading " |
| 64 | + "target is ambiguous. Please use dpctl.SyclQueue " |
| 65 | + "targeting this device".format(dev) |
| 66 | + ) |
| 67 | + else: |
| 68 | + obj.sycl_queue_ = dpctl.SyclQueue(dev) |
| 69 | + return obj |
| 70 | + |
| 71 | + @property |
| 72 | + def sycl_queue(self): |
| 73 | + """ |
| 74 | + :class:`dpctl.SyclQueue` used to offload to this :class:`.Device`. |
| 75 | + """ |
| 76 | + return self.sycl_queue_ |
| 77 | + |
| 78 | + @property |
| 79 | + def sycl_context(self): |
| 80 | + """ |
| 81 | + :class:`dpctl.SyclContext` associated with this :class:`.Device`. |
| 82 | + """ |
| 83 | + return self.sycl_queue_.sycl_context |
| 84 | + |
| 85 | + @property |
| 86 | + def sycl_device(self): |
| 87 | + """ |
| 88 | + :class:`dpctl.SyclDevice` targed by this :class:`.Device`. |
| 89 | + """ |
| 90 | + return self.sycl_queue_.sycl_device |
| 91 | + |
| 92 | + def __repr__(self): |
| 93 | + try: |
| 94 | + sd = self.sycl_device |
| 95 | + except AttributeError: |
| 96 | + raise ValueError( |
| 97 | + "Instance of {} is not initialized".format(self.__class__) |
| 98 | + ) |
| 99 | + try: |
| 100 | + fs = sd.filter_string |
| 101 | + return "Device({})".format(fs) |
| 102 | + except TypeError: |
| 103 | + # This is a sub-device |
| 104 | + return repr(self.sycl_queue) |
0 commit comments