|
| 1 | +# Data Parallel Control (dpctl) |
| 2 | +# |
| 3 | +# Copyright 2026 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 | +"""Shared utilities for dpctl benchmarks. |
| 18 | +
|
| 19 | +Every benchmark that needs a device goes through :func:`queue_for` or |
| 20 | +:func:`device_for` so that benchmark names stay identical on every node in |
| 21 | +the pool: a node without a given device reports the parameter as skipped |
| 22 | +rather than failing the whole suite. |
| 23 | +""" |
| 24 | + |
| 25 | +import os |
| 26 | + |
| 27 | +from asv_runner.benchmarks.mark import SkipNotImplemented |
| 28 | + |
| 29 | +import dpctl |
| 30 | +import dpctl.compiler as dpc |
| 31 | + |
| 32 | +# Device axis. Filter-selector strings accepted by SyclQueue/SyclDevice. |
| 33 | +_SELECTORS = ["cpu", "gpu"] |
| 34 | + |
| 35 | +# Allocation and transfer size sweep, in bytes: 4 KiB, 1 MiB, 16 MiB, 256 MiB. |
| 36 | +_SIZES = [4 * 1024, 1024**2, 16 * 1024**2, 256 * 1024**2] |
| 37 | + |
| 38 | +# USM kinds, mapped to their constructors. |
| 39 | +_USM_TYPES = ["device", "host", "shared"] |
| 40 | + |
| 41 | +# Fraction of device global memory a single benchmark allocation may claim. |
| 42 | +_MEM_BUDGET = 0.25 |
| 43 | + |
| 44 | +_queues = {} |
| 45 | +_devices = {} |
| 46 | +_spirv = None |
| 47 | + |
| 48 | + |
| 49 | +def queue_for(selector): |
| 50 | + """Return a memoized queue for *selector*, or skip when unavailable.""" |
| 51 | + if selector not in _queues: |
| 52 | + try: |
| 53 | + _queues[selector] = dpctl.SyclQueue(selector) |
| 54 | + except dpctl.SyclQueueCreationError: |
| 55 | + _queues[selector] = None |
| 56 | + q = _queues[selector] |
| 57 | + if q is None: |
| 58 | + raise SkipNotImplemented(f"no {selector} device available") |
| 59 | + return q |
| 60 | + |
| 61 | + |
| 62 | +def device_for(selector): |
| 63 | + """Return a memoized device for *selector*, or skip when unavailable.""" |
| 64 | + if selector not in _devices: |
| 65 | + try: |
| 66 | + _devices[selector] = dpctl.SyclDevice(selector) |
| 67 | + except dpctl.SyclDeviceCreationError: |
| 68 | + _devices[selector] = None |
| 69 | + d = _devices[selector] |
| 70 | + if d is None: |
| 71 | + raise SkipNotImplemented(f"no {selector} device available") |
| 72 | + return d |
| 73 | + |
| 74 | + |
| 75 | +def usm_class(usm_type): |
| 76 | + """Return the dpctl.memory class allocating *usm_type* memory.""" |
| 77 | + import dpctl.memory as dpm |
| 78 | + |
| 79 | + return { |
| 80 | + "device": dpm.MemoryUSMDevice, |
| 81 | + "host": dpm.MemoryUSMHost, |
| 82 | + "shared": dpm.MemoryUSMShared, |
| 83 | + }[usm_type] |
| 84 | + |
| 85 | + |
| 86 | +def skip_unless_fits(queue, nbytes): |
| 87 | + """Skip when *nbytes* exceeds this device's allocation budget.""" |
| 88 | + budget = _MEM_BUDGET * queue.sycl_device.global_mem_size |
| 89 | + if nbytes > budget: |
| 90 | + raise SkipNotImplemented( |
| 91 | + f"{nbytes} bytes exceeds the device memory budget" |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def opencl_queue_or_skip(): |
| 96 | + """Return a memoized OpenCL queue, or skip. |
| 97 | +
|
| 98 | + ``create_kernel_bundle_from_source`` only supports the OpenCL backend. |
| 99 | + """ |
| 100 | + return queue_for("opencl") |
| 101 | + |
| 102 | + |
| 103 | +def sycl_source_queue_or_skip(selector): |
| 104 | + """Return a queue whose device can compile SYCL source, or skip.""" |
| 105 | + q = queue_for(selector) |
| 106 | + if not dpc.is_sycl_source_compilation_available(): |
| 107 | + raise SkipNotImplemented("SYCL source compilation extension absent") |
| 108 | + if not q.sycl_device.can_compile("sycl"): |
| 109 | + raise SkipNotImplemented("device cannot compile SYCL source") |
| 110 | + return q |
| 111 | + |
| 112 | + |
| 113 | +def spirv_bytes(): |
| 114 | + """Return the SPIR-V module shipped with the installed dpctl, or skip. |
| 115 | +
|
| 116 | + Defines ``add(int*, int*, int*)`` and ``axpy(int*, int*, int*, int)``. |
| 117 | + """ |
| 118 | + global _spirv |
| 119 | + if _spirv is None: |
| 120 | + path = os.path.join( |
| 121 | + os.path.dirname(os.path.abspath(dpctl.__file__)), |
| 122 | + "tests", |
| 123 | + "input_files", |
| 124 | + "multi_kernel.spv", |
| 125 | + ) |
| 126 | + if not os.path.exists(path): |
| 127 | + raise SkipNotImplemented(f"SPIR-V module not found at {path}") |
| 128 | + with open(path, "rb") as fh: |
| 129 | + _spirv = fh.read() |
| 130 | + return _spirv |
| 131 | + |
| 132 | + |
| 133 | +def ocl_axpy_source(kernel_name="axpy"): |
| 134 | + """Return OpenCL C source for an axpy kernel called *kernel_name*.""" |
| 135 | + return ( |
| 136 | + f"kernel void {kernel_name}(" |
| 137 | + " global int *a, global int *b, global int *c, int d) {" |
| 138 | + " size_t index = get_global_id(0);" |
| 139 | + " c[index] = d * a[index] + b[index];" |
| 140 | + "}" |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +def sycl_axpy_source(kernel_name="axpy"): |
| 145 | + """Return SYCL source for an axpy kernel called *kernel_name*.""" |
| 146 | + return f""" |
| 147 | + #include <sycl/sycl.hpp> |
| 148 | +
|
| 149 | + namespace syclext = sycl::ext::oneapi::experimental; |
| 150 | +
|
| 151 | + extern "C" SYCL_EXTERNAL |
| 152 | + SYCL_EXT_ONEAPI_FUNCTION_PROPERTY((syclext::nd_range_kernel<1>)) |
| 153 | + void {kernel_name}(int* a, int* b, int* c, int d) {{ |
| 154 | + sycl::nd_item<1> item = |
| 155 | + sycl::ext::oneapi::this_work_item::get_nd_item<1>(); |
| 156 | + size_t i = item.get_global_linear_id(); |
| 157 | + c[i] = d * a[i] + b[i]; |
| 158 | + }} |
| 159 | + """ |
0 commit comments