Repository navigation
Expand file tree
/
Copy path_utils.py
More file actions
156 lines (124 loc) · 4.65 KB
/
Copy path_utils.py
File metadata and controls
156 lines (124 loc) · 4.65 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
# Data Parallel Control (dpctl)
#
# Copyright 2026 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.
"""Shared utilities for dpctl benchmarks.
Every benchmark that needs a device goes through :func:`queue_for` or
:func:`device_for` so that benchmark names stay identical on every node in
the pool: a node without a given device reports the parameter as skipped
rather than failing the whole suite.
"""
import os
from asv_runner.benchmarks.mark import SkipNotImplemented
import dpctl
# Device selectors
_SELECTORS = ["opencl:cpu", "level_zero:gpu"]
# Allocation and transfer sizes, in bytes.
_SIZES = [4 * 1024, 1024**2, 16 * 1024**2, 256 * 1024**2]
# USM kinds.
_USM_TYPES = ["device", "host", "shared"]
# Max fraction of device memory a single allocation may claim.
_MEM_BUDGET = 0.25
_queues = {}
_devices = {}
_spirv = None
def queue_for(selector):
"""Return a memoized queue for *selector*, or skip when unavailable."""
if selector not in _queues:
try:
_queues[selector] = dpctl.SyclQueue(selector)
except dpctl.SyclQueueCreationError:
_queues[selector] = None
q = _queues[selector]
if q is None:
raise SkipNotImplemented(f"no {selector} device available")
return q
def device_for(selector):
"""Return a memoized device for *selector*, or skip when unavailable."""
if selector not in _devices:
try:
_devices[selector] = dpctl.SyclDevice(selector)
except dpctl.SyclDeviceCreationError:
_devices[selector] = None
d = _devices[selector]
if d is None:
raise SkipNotImplemented(f"no {selector} device available")
return d
def usm_class(usm_type):
"""Return the dpctl.memory class allocating *usm_type* memory."""
import dpctl.memory as dpm
return {
"device": dpm.MemoryUSMDevice,
"host": dpm.MemoryUSMHost,
"shared": dpm.MemoryUSMShared,
}[usm_type]
def skip_unless_fits(queue, nbytes):
"""Skip when *nbytes* exceeds this device's allocation budget."""
budget = _MEM_BUDGET * queue.sycl_device.global_mem_size
if nbytes > budget:
raise SkipNotImplemented(
f"{nbytes} bytes exceeds the device memory budget"
)
def opencl_queue_or_skip():
"""Return a memoized OpenCL queue, or skip.
``create_kernel_bundle_from_source`` only supports the OpenCL backend.
"""
return queue_for("opencl")
def sycl_source_queue_or_skip(selector):
"""Return a queue whose device can compile SYCL source, or skip."""
try:
import dpctl.compiler as dpc
except ImportError:
raise SkipNotImplemented("dpctl.compiler is not available")
q = queue_for(selector)
if not dpc.is_sycl_source_compilation_available():
raise SkipNotImplemented("SYCL source compilation extension absent")
if not q.sycl_device.can_compile("sycl"):
raise SkipNotImplemented("device cannot compile SYCL source")
return q
def spirv_bytes():
"""Return the SPIR-V module shipped with the installed dpctl, or skip.
Defines ``add(int*, int*, int*)`` and ``axpy(int*, int*, int*, int)``.
"""
global _spirv
if _spirv is None:
path = os.path.join(
os.path.dirname(os.path.abspath(dpctl.__file__)),
"tests",
"input_files",
"multi_kernel.spv",
)
if not os.path.exists(path):
raise SkipNotImplemented(f"SPIR-V module not found at {path}")
with open(path, "rb") as fh:
_spirv = fh.read()
return _spirv
def ocl_axpy_source(kernel_name="axpy"):
"""Return OpenCL C source for an axpy kernel called *kernel_name*."""
return (
f"kernel void {kernel_name}("
" global int *a, global int *b, global int *c, int d) {"
" size_t index = get_global_id(0);"
" c[index] = d * a[index] + b[index];"
"}"
)
def sycl_axpy_source(kernel_name="axpy"):
"""Return SYCL source for an axpy function called *kernel_name*."""
return f"""
#include <sycl/sycl.hpp>
extern "C" SYCL_EXTERNAL
void {kernel_name}(int* a, int* b, int* c, int d, size_t i) {{
c[i] = d * a[i] + b[i];
}}
"""