|
| 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 | + |
| 17 | +import dpctl |
| 18 | +import dpctl.memory |
| 19 | + |
| 20 | + |
| 21 | +def is_root_device(d): |
| 22 | + """ |
| 23 | + Returns True if d is an instance of SyclDevice that does not |
| 24 | + have a parent_device, or False otherwise. |
| 25 | + """ |
| 26 | + if not isinstance(d, dpctl.SyclDevice): |
| 27 | + return False |
| 28 | + # d.parent_device is None for a root device, |
| 29 | + # or a SyclDevice object representing the parent device |
| 30 | + # for a sub-device |
| 31 | + return d.parent_device is None |
| 32 | + |
| 33 | + |
| 34 | +def subdivide_root_cpu_device(): |
| 35 | + """ |
| 36 | + Create root CPU device, and equally partition it |
| 37 | + into smaller CPU devices 4 execution units each, |
| 38 | + and then further parition those subdevice into |
| 39 | + smaller sub-devices |
| 40 | + """ |
| 41 | + cpu_d = dpctl.SyclDevice("cpu") |
| 42 | + print( |
| 43 | + "cpu_d is " |
| 44 | + + ("a root device." if is_root_device(cpu_d) else "not a root device.") |
| 45 | + ) |
| 46 | + sub_devs = cpu_d.create_sub_devices(partition=4) |
| 47 | + print("Sub-device #EU: ", [d.max_compute_units for d in sub_devs]) |
| 48 | + print("Sub-device is_root: ", [is_root_device(d) for d in sub_devs]) |
| 49 | + print( |
| 50 | + "Sub-device parent is what we expected: ", |
| 51 | + [d.parent_device == cpu_d for d in sub_devs], |
| 52 | + ) |
| 53 | + |
| 54 | + # Further partition each sub-device |
| 55 | + subsub_dev_eu_count = [ |
| 56 | + [sd.max_compute_units for sd in d.create_sub_devices(partition=(1, 3))] |
| 57 | + for d in sub_devs |
| 58 | + ] |
| 59 | + print("Sub-sub-device #EU: ", subsub_dev_eu_count) |
| 60 | + |
| 61 | + |
| 62 | +def subdivide_by_affinity(affinity="numa"): |
| 63 | + """ |
| 64 | + Create sub-devices partitioning by affinity. |
| 65 | + """ |
| 66 | + cpu_d = dpctl.SyclDevice("cpu") |
| 67 | + sub_devs = cpu_d.create_sub_devices(partition=affinity) |
| 68 | + return sub_devs |
| 69 | + |
| 70 | + |
| 71 | +def create_subdevice_queue(): |
| 72 | + """ |
| 73 | + Partition a CPU sycl device into sub-devices. |
| 74 | + Create a multi-device sycl context. |
| 75 | +
|
| 76 | + """ |
| 77 | + cpu_d = dpctl.SyclDevice("cpu") |
| 78 | + cpu_count = cpu_d.max_compute_units |
| 79 | + sub_devs = cpu_d.create_sub_devices(partition=cpu_count // 2) |
| 80 | + multidevice_ctx = dpctl.SyclContext(sub_devs) |
| 81 | + # create a SyclQueue for each sub-device, using commont |
| 82 | + # multi-device context |
| 83 | + q0, q1 = [dpctl.SyclQueue(multidevice_ctx, d) for d in sub_devs] |
| 84 | + # for each sub-device allocate 26 bytes |
| 85 | + m0 = dpctl.memory.MemoryUSMDevice(26, queue=q0) |
| 86 | + m1 = dpctl.memory.MemoryUSMDevice(26, queue=q1) |
| 87 | + # populate m0 with host data of spaces |
| 88 | + hostmem = bytearray(b" " * 26) |
| 89 | + # copy spaces into m1 |
| 90 | + m1.copy_from_host(hostmem) |
| 91 | + for i in range(26): |
| 92 | + hostmem[i] = ord("a") + i |
| 93 | + # copy character sequence into m0 |
| 94 | + m0.copy_from_host(hostmem) |
| 95 | + # from from m0 to m1. Due to using multi-device context, |
| 96 | + # copying can be done directly |
| 97 | + m1.copy_from_device(m0) |
| 98 | + return bytes(m1.copy_to_host()) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + print("") |
| 103 | + print("Executing subdivide_root_cpu_device:") |
| 104 | + subdivide_root_cpu_device() |
| 105 | + print("") |
| 106 | + print("Exectuting create_subdevice_queue:") |
| 107 | + print(create_subdevice_queue()) |
0 commit comments