unigpu 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- unigpu-0.3.0/LICENSE +21 -0
- unigpu-0.3.0/PKG-INFO +451 -0
- unigpu-0.3.0/README.md +400 -0
- unigpu-0.3.0/pyproject.toml +62 -0
- unigpu-0.3.0/setup.cfg +4 -0
- unigpu-0.3.0/unigpu/__init__.py +75 -0
- unigpu-0.3.0/unigpu/_version.py +7 -0
- unigpu-0.3.0/unigpu/api.py +199 -0
- unigpu-0.3.0/unigpu/backends/__init__.py +22 -0
- unigpu-0.3.0/unigpu/backends/metal/__init__.py +85 -0
- unigpu-0.3.0/unigpu/backends/metal/bridge.py +221 -0
- unigpu-0.3.0/unigpu/backends/metal/driver.py +250 -0
- unigpu-0.3.0/unigpu/backends/metal/imports.py +84 -0
- unigpu-0.3.0/unigpu/backends/numba_backend/__init__.py +31 -0
- unigpu-0.3.0/unigpu/backends/numba_backend/discovery.py +124 -0
- unigpu-0.3.0/unigpu/backends/numba_backend/driver.py +174 -0
- unigpu-0.3.0/unigpu/backends/numba_backend/imports.py +48 -0
- unigpu-0.3.0/unigpu/backends/numba_backend/limitations.py +93 -0
- unigpu-0.3.0/unigpu/backends/numba_backend/rebinding.py +164 -0
- unigpu-0.3.0/unigpu/backends/numba_backend/simulator.py +89 -0
- unigpu-0.3.0/unigpu/backends/opencl.py +289 -0
- unigpu-0.3.0/unigpu/backends/sim/__init__.py +12 -0
- unigpu-0.3.0/unigpu/backends/sim/analysis.py +16 -0
- unigpu-0.3.0/unigpu/backends/sim/barrier.py +69 -0
- unigpu-0.3.0/unigpu/backends/sim/checked.py +60 -0
- unigpu-0.3.0/unigpu/backends/sim/cooperative.py +231 -0
- unigpu-0.3.0/unigpu/backends/sim/diagnostics.py +21 -0
- unigpu-0.3.0/unigpu/backends/sim/driver.py +87 -0
- unigpu-0.3.0/unigpu/backends/sim/executor.py +161 -0
- unigpu-0.3.0/unigpu/backends/sim/provider.py +66 -0
- unigpu-0.3.0/unigpu/backends/sim/runtime.py +78 -0
- unigpu-0.3.0/unigpu/backends/sim/strategy.py +17 -0
- unigpu-0.3.0/unigpu/backends/translated.py +115 -0
- unigpu-0.3.0/unigpu/backends/translation.py +79 -0
- unigpu-0.3.0/unigpu/benchmark/__init__.py +30 -0
- unigpu-0.3.0/unigpu/benchmark/cases.py +117 -0
- unigpu-0.3.0/unigpu/benchmark/charts.py +117 -0
- unigpu-0.3.0/unigpu/benchmark/measures.py +125 -0
- unigpu-0.3.0/unigpu/benchmark/tables.py +76 -0
- unigpu-0.3.0/unigpu/compiler/__init__.py +52 -0
- unigpu-0.3.0/unigpu/compiler/coerce.py +29 -0
- unigpu-0.3.0/unigpu/compiler/context.py +166 -0
- unigpu-0.3.0/unigpu/compiler/dialects/__init__.py +12 -0
- unigpu-0.3.0/unigpu/compiler/dialects/base.py +298 -0
- unigpu-0.3.0/unigpu/compiler/dialects/metal.py +158 -0
- unigpu-0.3.0/unigpu/compiler/dialects/opencl.py +120 -0
- unigpu-0.3.0/unigpu/compiler/expressions.py +277 -0
- unigpu-0.3.0/unigpu/compiler/function.py +178 -0
- unigpu-0.3.0/unigpu/compiler/interface.py +93 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/__init__.py +26 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/atomics.py +108 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/builtins.py +160 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/indexing.py +48 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/mathlib.py +103 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/memory.py +101 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/registry.py +112 -0
- unigpu-0.3.0/unigpu/compiler/intrinsics/sync.py +54 -0
- unigpu-0.3.0/unigpu/compiler/naming.py +49 -0
- unigpu-0.3.0/unigpu/compiler/operators.py +227 -0
- unigpu-0.3.0/unigpu/compiler/passes.py +15 -0
- unigpu-0.3.0/unigpu/compiler/program.py +137 -0
- unigpu-0.3.0/unigpu/compiler/scope.py +116 -0
- unigpu-0.3.0/unigpu/compiler/signatures.py +123 -0
- unigpu-0.3.0/unigpu/compiler/source.py +105 -0
- unigpu-0.3.0/unigpu/compiler/statements.py +289 -0
- unigpu-0.3.0/unigpu/compiler/text.py +34 -0
- unigpu-0.3.0/unigpu/compiler/translators.py +59 -0
- unigpu-0.3.0/unigpu/compiler/typeof.py +43 -0
- unigpu-0.3.0/unigpu/compiler/types.py +190 -0
- unigpu-0.3.0/unigpu/compiler/values.py +77 -0
- unigpu-0.3.0/unigpu/compiler/writer.py +48 -0
- unigpu-0.3.0/unigpu/core/__init__.py +0 -0
- unigpu-0.3.0/unigpu/core/arguments.py +68 -0
- unigpu-0.3.0/unigpu/core/cache.py +46 -0
- unigpu-0.3.0/unigpu/core/code.py +63 -0
- unigpu-0.3.0/unigpu/core/device.py +154 -0
- unigpu-0.3.0/unigpu/core/dims.py +49 -0
- unigpu-0.3.0/unigpu/core/driver.py +145 -0
- unigpu-0.3.0/unigpu/core/families.py +31 -0
- unigpu-0.3.0/unigpu/core/launch.py +74 -0
- unigpu-0.3.0/unigpu/core/memory.py +295 -0
- unigpu-0.3.0/unigpu/core/options.py +52 -0
- unigpu-0.3.0/unigpu/core/provider.py +70 -0
- unigpu-0.3.0/unigpu/core/registry.py +101 -0
- unigpu-0.3.0/unigpu/core/report.py +58 -0
- unigpu-0.3.0/unigpu/core/scope.py +49 -0
- unigpu-0.3.0/unigpu/core/session.py +132 -0
- unigpu-0.3.0/unigpu/core/streams.py +153 -0
- unigpu-0.3.0/unigpu/dispatcher.py +175 -0
- unigpu-0.3.0/unigpu/errors.py +85 -0
- unigpu-0.3.0/unigpu/lang/__init__.py +48 -0
- unigpu-0.3.0/unigpu/lang/runtime.py +93 -0
- unigpu-0.3.0/unigpu/lang/stubs.py +282 -0
- unigpu-0.3.0/unigpu/selftest.py +312 -0
- unigpu-0.3.0/unigpu.egg-info/PKG-INFO +451 -0
- unigpu-0.3.0/unigpu.egg-info/SOURCES.txt +97 -0
- unigpu-0.3.0/unigpu.egg-info/dependency_links.txt +1 -0
- unigpu-0.3.0/unigpu.egg-info/requires.txt +50 -0
- unigpu-0.3.0/unigpu.egg-info/top_level.txt +1 -0
unigpu-0.3.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lilian Aveneau
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
unigpu-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: unigpu
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: GPU programming in Python with the numba-cuda API, on CUDA, ROCm, Metal, OpenCL and a CPU simulator
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Homepage, https://unigpu.pages.xlim.fr/unigpu/
|
|
7
|
+
Project-URL: Documentation, https://unigpu.pages.xlim.fr/unigpu/docs/api.html
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Topic :: Education
|
|
10
|
+
Classifier: Topic :: Scientific/Engineering
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy>=1.19
|
|
15
|
+
Provides-Extra: cuda
|
|
16
|
+
Requires-Dist: numba-cuda[cu12]; extra == "cuda"
|
|
17
|
+
Requires-Dist: numpy<2.5; extra == "cuda"
|
|
18
|
+
Provides-Extra: hip
|
|
19
|
+
Requires-Dist: numba-hip>=0.2; sys_platform == "linux" and extra == "hip"
|
|
20
|
+
Provides-Extra: opencl
|
|
21
|
+
Requires-Dist: pyopencl; extra == "opencl"
|
|
22
|
+
Provides-Extra: opencl-cpu
|
|
23
|
+
Requires-Dist: pyopencl; extra == "opencl-cpu"
|
|
24
|
+
Requires-Dist: pocl-binary-distribution; (sys_platform == "linux" and platform_machine == "x86_64") and extra == "opencl-cpu"
|
|
25
|
+
Provides-Extra: metal
|
|
26
|
+
Requires-Dist: pyobjc-framework-Metal; extra == "metal"
|
|
27
|
+
Provides-Extra: metal-rubicon
|
|
28
|
+
Requires-Dist: rubicon-objc; extra == "metal-rubicon"
|
|
29
|
+
Provides-Extra: docs
|
|
30
|
+
Requires-Dist: mkdocs<2,>=1.6; extra == "docs"
|
|
31
|
+
Requires-Dist: mkdocs-material>=9.5; extra == "docs"
|
|
32
|
+
Requires-Dist: mkdocs-same-dir>=0.1; extra == "docs"
|
|
33
|
+
Provides-Extra: notebooks
|
|
34
|
+
Requires-Dist: matplotlib; extra == "notebooks"
|
|
35
|
+
Requires-Dist: jupyterlab; extra == "notebooks"
|
|
36
|
+
Provides-Extra: test
|
|
37
|
+
Requires-Dist: pytest; extra == "test"
|
|
38
|
+
Requires-Dist: pytest-cov; extra == "test"
|
|
39
|
+
Requires-Dist: matplotlib; extra == "test"
|
|
40
|
+
Requires-Dist: nbclient; extra == "test"
|
|
41
|
+
Requires-Dist: ipykernel; extra == "test"
|
|
42
|
+
Provides-Extra: dev
|
|
43
|
+
Requires-Dist: pytest; extra == "dev"
|
|
44
|
+
Requires-Dist: pytest-cov; extra == "dev"
|
|
45
|
+
Requires-Dist: matplotlib; extra == "dev"
|
|
46
|
+
Requires-Dist: nbclient; extra == "dev"
|
|
47
|
+
Requires-Dist: ipykernel; extra == "dev"
|
|
48
|
+
Requires-Dist: ruff; extra == "dev"
|
|
49
|
+
Requires-Dist: mypy; extra == "dev"
|
|
50
|
+
Dynamic: license-file
|
|
51
|
+
|
|
52
|
+
# unigpu: GPU programming in Python, from CUDA to OpenCL
|
|
53
|
+
|
|
54
|
+
`unigpu` is made to **teach GPU programming**. Kernels are written in
|
|
55
|
+
Python, with exactly the API of [numba-cuda](https://github.com/NVIDIA/numba-cuda).
|
|
56
|
+
The same code then runs on every detected platform:
|
|
57
|
+
|
|
58
|
+
| Backend | Hardware | How it works | Python package |
|
|
59
|
+
|----------|--------------------------------------------|---------------------------------------|--------------------------|
|
|
60
|
+
| `cuda` | NVIDIA GPUs | compiled by numba-cuda | `numba-cuda` |
|
|
61
|
+
| `hip` | AMD GPUs (ROCm) | compiled by numba-hip | `numba-hip` |
|
|
62
|
+
| `metal` | Apple GPUs (Mac, iPad) | translated to Metal Shading Language | `pyobjc-framework-Metal` (Mac), `rubicon-objc` or Pythonista (iPad) |
|
|
63
|
+
| `opencl` | any OpenCL device: GPU, CPU, FPGA | translated to OpenCL C 1.2 | `pyopencl` |
|
|
64
|
+
| `sim` | any computer | **simulator** in pure Python | none (NumPy only) |
|
|
65
|
+
|
|
66
|
+
The API covers the **common core** of these models: kernels and device
|
|
67
|
+
functions, grid configuration, device memory, shared memory (static and
|
|
68
|
+
dynamic), local and constant memory, synchronization of the threads of a
|
|
69
|
+
block, atomics, streams and events. Hardware discovery follows OpenCL's
|
|
70
|
+
model: **platforms**, and **devices** per platform.
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import numpy as np
|
|
74
|
+
import unigpu as gpu # instead of: from numba import cuda
|
|
75
|
+
|
|
76
|
+
@gpu.jit
|
|
77
|
+
def addition(a, b, c):
|
|
78
|
+
i = gpu.grid(1) # blockIdx.x * blockDim.x + threadIdx.x
|
|
79
|
+
if i < c.size:
|
|
80
|
+
c[i] = a[i] + b[i]
|
|
81
|
+
|
|
82
|
+
a = np.random.rand(1_000_000).astype(np.float32)
|
|
83
|
+
d_a = gpu.to_device(a) # host → device
|
|
84
|
+
d_c = gpu.device_array_like(a) # allocation
|
|
85
|
+
addition[(a.size + 255) // 256, 256](d_a, d_a, d_c) # [blocks, threads per block]
|
|
86
|
+
c = d_c.copy_to_host() # device → host
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
---
|
|
90
|
+
|
|
91
|
+
## Installation: three levels of equipment
|
|
92
|
+
|
|
93
|
+
unigpu is on PyPI: `pip install unigpu`, with the backend's option in
|
|
94
|
+
brackets (`pip install "unigpu[cuda]"`). The examples, the notebooks and the
|
|
95
|
+
tests are in the repository: clone it, then install that folder with
|
|
96
|
+
`pip install -e .`, with the same options (`pip install -e ".[cuda]"`).
|
|
97
|
+
Step-by-step instructions for each kind of machine (Windows, macOS, Linux,
|
|
98
|
+
Chromebook, iPad, Google Colab): [INSTALL.md](https://unigpu.pages.xlim.fr/unigpu/INSTALL.html).
|
|
99
|
+
|
|
100
|
+
**Level 0: basic version, without a GPU.** For the least equipped students,
|
|
101
|
+
Python 3.9+ and NumPy are enough:
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
pip install unigpu
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The simulator runs kernels on any computer (Windows, macOS, Linux,
|
|
108
|
+
Chromebook with Linux, iPad). The whole programming model is respected:
|
|
109
|
+
grid, blocks, barriers, shared memory, atomics. Kernels with barriers are
|
|
110
|
+
rewritten as generators: no system thread is needed, so it also runs in
|
|
111
|
+
Python environments based on WebAssembly (Pyodide, JupyterLite). The
|
|
112
|
+
simulator also detects out-of-bounds accesses and divergent barriers, and
|
|
113
|
+
`print()` and `breakpoint()` work inside kernels. It is slow: work on small
|
|
114
|
+
data.
|
|
115
|
+
|
|
116
|
+
**Level 1: fast CPU through OpenCL.** No GPU, but real parallelism:
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
pip install "unigpu[opencl-cpu]" # PyOpenCL + POCL (OpenCL on the CPU), Linux x86-64
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
pip distributes POCL for Linux on x86-64 only (also WSL 2). For
|
|
123
|
+
the other systems, see [INSTALL.md](https://unigpu.pages.xlim.fr/unigpu/INSTALL.html).
|
|
124
|
+
|
|
125
|
+
Kernels are then translated to OpenCL C, compiled and run on every core of
|
|
126
|
+
the processor.
|
|
127
|
+
|
|
128
|
+
**Level 2: GPU.**
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
pip install "unigpu[cuda]" # NVIDIA: numba-cuda
|
|
132
|
+
pip install "unigpu[hip]" # AMD, with ROCm installed: numba-hip
|
|
133
|
+
pip install "unigpu[opencl]" # AMD, Intel, NVIDIA: PyOpenCL + the GPU's OpenCL driver
|
|
134
|
+
pip install "unigpu[metal]" # Mac: PyObjC Metal
|
|
135
|
+
pip install "unigpu[metal-rubicon]" # iPad (or Mac): Metal through rubicon-objc, pure Python
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
On iPad, `pip install` works in the Python apps that have pip (Pyto,
|
|
139
|
+
a-Shell, Carnets, Code App; Pythonista through StaSh). Pythonista already
|
|
140
|
+
provides `objc_util`, which unigpu can use too: see [`docs/testing-ipad-colab.md`](https://unigpu.pages.xlim.fr/unigpu/docs/testing-ipad-colab.html).
|
|
141
|
+
|
|
142
|
+
The `hip` option installs [numba-hip](https://pypi.org/project/numba-hip/),
|
|
143
|
+
AMD's equivalent of numba-cuda, developed in
|
|
144
|
+
[ROCm/hip-python](https://github.com/ROCm/hip-python). ROCm itself must
|
|
145
|
+
already be installed. pip takes the HIP Python bindings of the latest ROCm
|
|
146
|
+
release: with an older ROCm, see the installation guide of HIP Python.
|
|
147
|
+
numba-hip requires numba < 0.64. It does not provide atomics on arrays,
|
|
148
|
+
dynamic shared memory, `print()` in a kernel nor `float16`: unigpu says so
|
|
149
|
+
before compiling. The `opencl` backend, with ROCm's OpenCL runtime, supports
|
|
150
|
+
them on the same GPU.
|
|
151
|
+
|
|
152
|
+
> **Tip**: Google Colab provides an NVIDIA GPU for free (*Runtime → Change
|
|
153
|
+
> runtime type → GPU*). Installing unigpu with the `cuda` backend is then
|
|
154
|
+
> enough: `pip install "unigpu[cuda]"`.
|
|
155
|
+
|
|
156
|
+
---
|
|
157
|
+
|
|
158
|
+
## Discover and choose the hardware
|
|
159
|
+
|
|
160
|
+
```python
|
|
161
|
+
>>> import unigpu as gpu
|
|
162
|
+
>>> for p in gpu.get_platforms(): # like clGetPlatformIDs
|
|
163
|
+
... print(p.backend, p.name, [d.name for d in p.get_devices()])
|
|
164
|
+
opencl Portable Computing Language ['pthread-Intel(R) Xeon(R) ...']
|
|
165
|
+
sim unigpu simulator ['CPU simulator (Python)']
|
|
166
|
+
>>> gpu.list_devices(gpu.DeviceType.GPU) # like clGetDeviceIDs(..., GPU)
|
|
167
|
+
[]
|
|
168
|
+
>>> gpu.detect() # complete report (like cuda.detect())
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Each device describes its characteristics in `device.info`: name, vendor,
|
|
172
|
+
type, compute units, threads per block, maximum block dimensions, shared
|
|
173
|
+
memory per block, global memory, `float64` support.
|
|
174
|
+
|
|
175
|
+
By default, unigpu takes the best available device: a GPU, otherwise an
|
|
176
|
+
accelerator, then a CPU, then the simulator. To choose one:
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
gpu.select_device("opencl") # first OpenCL device
|
|
180
|
+
gpu.select_device("opencl:0:1") # platform 0, device 1
|
|
181
|
+
gpu.select_device("cuda:1") # second CUDA GPU
|
|
182
|
+
gpu.select_device("sim") # simulator
|
|
183
|
+
gpu.select_device(gpu.DeviceType.GPU)
|
|
184
|
+
gpu.select_device(2) # rank in gpu.list_devices()
|
|
185
|
+
|
|
186
|
+
with gpu.gpus[1]: # temporarily (like "with cuda.gpus[1]:")
|
|
187
|
+
...
|
|
188
|
+
with gpu.device_scope(device): # the same, for a Device of gpu.list_devices()
|
|
189
|
+
...
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
The environment works too: `UNIGPU_DEVICE=sim python lab1.py`.
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## The programming model
|
|
197
|
+
|
|
198
|
+
### Matching concepts
|
|
199
|
+
|
|
200
|
+
| unigpu = numba.cuda | CUDA C | OpenCL C | Metal |
|
|
201
|
+
|-----------------------------------|-------------------------|--------------------------------|--------------------------------|
|
|
202
|
+
| `@gpu.jit` | `__global__` | `__kernel` | `kernel` |
|
|
203
|
+
| `@gpu.jit(device=True)` | `__device__` | function | function |
|
|
204
|
+
| `kernel[g, b, stream, nbytes](...)` | `kernel<<<g, b, nbytes, stream>>>` | `clEnqueueNDRangeKernel` | `dispatchThreadgroups` |
|
|
205
|
+
| `gpu.threadIdx.x` | `threadIdx.x` | `get_local_id(0)` | `thread_position_in_threadgroup` |
|
|
206
|
+
| `gpu.blockIdx.x` | `blockIdx.x` | `get_group_id(0)` | `threadgroup_position_in_grid` |
|
|
207
|
+
| `gpu.blockDim.x`, `gpu.gridDim.x` | `blockDim.x`, `gridDim.x` | `get_local_size(0)`, `get_num_groups(0)` | `threads_per_threadgroup`, `threadgroups_per_grid` |
|
|
208
|
+
| `gpu.grid(1)`, `gpu.gridsize(1)` | explicit computation | `get_global_id(0)`, `get_global_size(0)` | `thread_position_in_grid` |
|
|
209
|
+
| `gpu.shared.array(n, gpu.float32)`| `__shared__ float t[n]` | `__local float t[n]` | `threadgroup float t[n]` |
|
|
210
|
+
| `gpu.shared.array(0, gpu.float32)`| `extern __shared__` | `__local` argument | `[[threadgroup(0)]]` |
|
|
211
|
+
| `gpu.local.array(n, gpu.int32)` | `int t[n]` | `int t[n]` | `int t[n]` |
|
|
212
|
+
| `gpu.const.array_like(TABLE)` | `__constant__` | `__constant` | `constant` |
|
|
213
|
+
| `gpu.syncthreads()` | `__syncthreads()` | `barrier(...)` | `threadgroup_barrier(...)` |
|
|
214
|
+
| `gpu.syncthreads_count(p)` | `__syncthreads_count` | emulated (shared counter) | emulated (shared counter) |
|
|
215
|
+
| `gpu.atomic.add(t, i, v)` | `atomicAdd(&t[i], v)` | `atomic_add(&t[i], v)` | `atomic_fetch_add_explicit` |
|
|
216
|
+
| stream: `gpu.stream()` | `cudaStream_t` | command queue | `MTLCommandQueue` |
|
|
217
|
+
| `gpu.event()` | `cudaEvent_t` | `cl_event` marker | timestamp |
|
|
218
|
+
| block / thread | block / thread | work-group / work-item | threadgroup / thread |
|
|
219
|
+
|
|
220
|
+
The primitives can also be imported on their own (`from unigpu import
|
|
221
|
+
threadIdx, shared`). numba itself only accepts them as attributes of its
|
|
222
|
+
module (`cuda.threadIdx`): on CUDA and HIP, unigpu rewrites the kernel
|
|
223
|
+
accordingly.
|
|
224
|
+
|
|
225
|
+
### Memory, launch, streams
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
d = gpu.to_device(a) # allocation + copy (stream=0: default stream)
|
|
229
|
+
d = gpu.device_array((n, m), np.float32) # allocation only; float64 by default, as in numba
|
|
230
|
+
d = gpu.device_array_like(a)
|
|
231
|
+
d.copy_to_device(a) ; h = d.copy_to_host()
|
|
232
|
+
d.reshape(m, n), d.ravel(), d.view(np.uint8) # views on the same memory
|
|
233
|
+
|
|
234
|
+
kernel[blocks, threads](args) # 1D; tuples for 2D and 3D
|
|
235
|
+
kernel[blocks, threads, stream](args) # on a stream
|
|
236
|
+
kernel[blocks, threads, 0, nbytes](args) # + dynamic shared memory
|
|
237
|
+
kernel.forall(n)(args) # grid computed for n tasks
|
|
238
|
+
|
|
239
|
+
stream = gpu.stream()
|
|
240
|
+
start, end = gpu.event(), gpu.event()
|
|
241
|
+
start.record(stream) ; kernel[g, b, stream](d) ; end.record(stream)
|
|
242
|
+
end.synchronize() ; print(start.elapsed_time(end), "ms")
|
|
243
|
+
with stream.auto_synchronize(): ...
|
|
244
|
+
gpu.synchronize() # launches are asynchronous
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
Passing a NumPy array directly to a kernel works too. It is then copied
|
|
248
|
+
both ways, with a `UnigpuPerformanceWarning`, like numba's
|
|
249
|
+
`NumbaPerformanceWarning`: an opportunity to talk about the cost of
|
|
250
|
+
transfers.
|
|
251
|
+
|
|
252
|
+
### Signatures and options (as in numba)
|
|
253
|
+
|
|
254
|
+
```python
|
|
255
|
+
@gpu.jit("void(float32[:], float32)") # declared types: scalars are converted
|
|
256
|
+
def scale(a, factor): ...
|
|
257
|
+
|
|
258
|
+
@gpu.jit(device=True)
|
|
259
|
+
def square(x): return x * x
|
|
260
|
+
|
|
261
|
+
@gpu.jit(fastmath=True) # -cl-fast-relaxed-math, fastMathEnabled...
|
|
262
|
+
def fast(a): ...
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Atomics
|
|
266
|
+
|
|
267
|
+
`gpu.atomic.add`, `sub`, `and_`, `or_`, `xor`, `exch`, `inc`, `dec`, `max`,
|
|
268
|
+
`min`, `nanmax`, `nanmin` and `cas` work in global memory and in shared
|
|
269
|
+
memory. They return the old value. On OpenCL and Metal, they are available
|
|
270
|
+
for `int32`, `uint32` and `float32`; the operations without a native
|
|
271
|
+
equivalent (floats, CUDA-style `inc`/`dec`) are emulated with
|
|
272
|
+
compare-and-swap.
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## What can be written in a kernel
|
|
277
|
+
|
|
278
|
+
The accepted subset of Python is the same on every backend:
|
|
279
|
+
|
|
280
|
+
* scalar variables, typed by **inference** (`s = 0.0`) or by annotation
|
|
281
|
+
(`acc: gpu.float32 = 0`);
|
|
282
|
+
* `if / elif / else`, `for i in range(...)`, `while`, `break`, `continue`,
|
|
283
|
+
`return`, `a if c else b`, `a, b = b, a`;
|
|
284
|
+
* arithmetic, comparison (chained too: `0 < x < n`), logical and bitwise
|
|
285
|
+
operators, with **Python's semantics** for `//`, `%`, `/` and `**`;
|
|
286
|
+
* arrays: `a[i]`, `a[i, j]`, `a.shape`, `a.size`, `a.ndim`, `len(a)`,
|
|
287
|
+
aliases `b = a`;
|
|
288
|
+
* the usual `math` and `numpy` functions, `min`, `max`, `abs`, conversions
|
|
289
|
+
`int()`, `float()`, `gpu.float32()`, `np.int32()`...;
|
|
290
|
+
* global constants of the module (`TPB = 16`), `print()` (except on Metal
|
|
291
|
+
and HIP).
|
|
292
|
+
|
|
293
|
+
**Not** accepted: lists, dictionaries, objects, exceptions, slices `a[1:3]`,
|
|
294
|
+
recursion, calls of undecorated functions. The translator explains it,
|
|
295
|
+
giving the file and the line:
|
|
296
|
+
|
|
297
|
+
```
|
|
298
|
+
TranslationError: [lab1.py:12, in my_kernel()] slices (a[i:j]) are not supported in a kernel
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**Types.** As in numba, a Python integer is an `int64` and a Python float a
|
|
302
|
+
`float64`. The only exception is Metal, or an OpenCL device without double
|
|
303
|
+
precision, where a float is a `float32`. Thread indices and dimensions are
|
|
304
|
+
`int32`. Literals adapt: `x * 2.0` stays `float32` if `x` is. Use
|
|
305
|
+
`np.float32` for good performance.
|
|
306
|
+
|
|
307
|
+
## Debug and understand
|
|
308
|
+
|
|
309
|
+
```python
|
|
310
|
+
gpu.select_device("sim") # simulator: print, pdb, IndexError
|
|
311
|
+
print(gpu.translate(kernel, a, b, target="metal")) # the Metal code, without a GPU
|
|
312
|
+
print(kernel.inspect_code(a, b)) # code for the current device (PTX on CUDA)
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
On the simulator, an out-of-bounds access raises an `IndexError` giving
|
|
316
|
+
`threadIdx` and `blockIdx`, where a real GPU silently corrupts memory. A
|
|
317
|
+
barrier placed in a branch that not every thread of the block takes raises a
|
|
318
|
+
`DivergentBarrierError`; on a real GPU, the kernel would hang or give a wrong
|
|
319
|
+
result. Device functions can also be called directly from Python, so they
|
|
320
|
+
can be unit-tested.
|
|
321
|
+
|
|
322
|
+
## Examples
|
|
323
|
+
|
|
324
|
+
The `examples/` folder offers a progression of lab sessions: download it
|
|
325
|
+
whole, [`examples.zip`](https://unigpu.pages.xlim.fr/unigpu/examples.zip)
|
|
326
|
+
(the scripts share `common.py`), or read each script from the table below.
|
|
327
|
+
Each one takes the device as an argument, for instance
|
|
328
|
+
`python examples/03_matrix_multiplication.py sim`:
|
|
329
|
+
|
|
330
|
+
| Script | Concepts |
|
|
331
|
+
|--------|----------|
|
|
332
|
+
| [`00_discovery.py`](https://unigpu.pages.xlim.fr/unigpu/examples/00_discovery.py) | platforms, devices, characteristics, choice of the device |
|
|
333
|
+
| [`01_vector_addition.py`](https://unigpu.pages.xlim.fr/unigpu/examples/01_vector_addition.py) | first kernel, 1D grid, transfers |
|
|
334
|
+
| [`02_saxpy_grid_stride.py`](https://unigpu.pages.xlim.fr/unigpu/examples/02_saxpy_grid_stride.py) | "grid-stride" loop, signature, `forall`, bandwidth |
|
|
335
|
+
| [`03_matrix_multiplication.py`](https://unigpu.pages.xlim.fr/unigpu/examples/03_matrix_multiplication.py) | 2D grid, shared memory, tiling, timing with events |
|
|
336
|
+
| [`04_reduction.py`](https://unigpu.pages.xlim.fr/unigpu/examples/04_reduction.py) | tree reduction, `syncthreads` |
|
|
337
|
+
| [`05_histogram.py`](https://unigpu.pages.xlim.fr/unigpu/examples/05_histogram.py) | global atomics, privatization in shared memory |
|
|
338
|
+
| [`06_heat_2d.py`](https://unigpu.pages.xlim.fr/unigpu/examples/06_heat_2d.py) | 2D stencil, double buffering |
|
|
339
|
+
| [`07_mandelbrot.py`](https://unigpu.pages.xlim.fr/unigpu/examples/07_mandelbrot.py) | device function, divergence |
|
|
340
|
+
| [`08_generated_code.py`](https://unigpu.pages.xlim.fr/unigpu/examples/08_generated_code.py) | the same kernel in OpenCL C and in Metal |
|
|
341
|
+
| [`09_streams_events.py`](https://unigpu.pages.xlim.fr/unigpu/examples/09_streams_events.py) | streams, events, overlap |
|
|
342
|
+
| [`10_dynamic_constant_memory.py`](https://unigpu.pages.xlim.fr/unigpu/examples/10_dynamic_constant_memory.py) | dynamic shared memory, constant memory, block votes |
|
|
343
|
+
|
|
344
|
+
## Examples notebook and performance measurements
|
|
345
|
+
|
|
346
|
+
[`notebooks/examples.ipynb`](https://unigpu.pages.xlim.fr/unigpu/notebooks/examples.ipynb) goes through every
|
|
347
|
+
example. For each one, it shows the code, runs it, then **measures it for
|
|
348
|
+
growing data sizes**. The measurements are shown as a table, a summary and a
|
|
349
|
+
chart:
|
|
350
|
+
|
|
351
|
+
- duration of the kernel, measured with events;
|
|
352
|
+
- duration of the transfers;
|
|
353
|
+
- duration of the same computation with NumPy;
|
|
354
|
+
- speedup, with and without the transfers;
|
|
355
|
+
- throughput, in GB/s or in GFLOP/s.
|
|
356
|
+
|
|
357
|
+
It works on Colab (CUDA and OpenCL), on iPad (Metal, in Carnets or Juno), on
|
|
358
|
+
a Mac or a PC, and even on the simulator, with small sizes.
|
|
359
|
+
|
|
360
|
+
The benchmarks are defined in [`examples/benchmarks.py`](https://unigpu.pages.xlim.fr/unigpu/examples/benchmarks.py),
|
|
361
|
+
one per example. They can also all run from the command line:
|
|
362
|
+
`python examples/benchmarks.py [device] [--quick]`. To measure your own
|
|
363
|
+
kernel, describe it with `unigpu.benchmark.Case`:
|
|
364
|
+
|
|
365
|
+
```python
|
|
366
|
+
from unigpu import benchmark
|
|
367
|
+
|
|
368
|
+
case = benchmark.Case("Addition", arrays=lambda n: (a(n), b(n), np.zeros(n, np.float32)),
|
|
369
|
+
launch=lambda a, b, c: addition[(c.size + 255) // 256, 256](a, b, c),
|
|
370
|
+
reference=lambda a, b, c: a + b,
|
|
371
|
+
sizes=[2 ** k for k in range(10, 25, 2)], simulator_sizes=[256, 1024],
|
|
372
|
+
bytes_moved=lambda n: 12 * n)
|
|
373
|
+
results = benchmark.run(case) # or benchmark.compare(case): every device
|
|
374
|
+
print(benchmark.format_table(results))
|
|
375
|
+
benchmark.plot(results) # with matplotlib
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
## Self-test
|
|
379
|
+
|
|
380
|
+
`python -m unigpu.selftest all` (or `python autotest.py`) checks the common
|
|
381
|
+
core on each detected device in a few seconds: indices, shared memory,
|
|
382
|
+
barriers, atomics, dynamic and constant memory, votes, streams, events and
|
|
383
|
+
math functions. It needs neither pytest nor any installation, which makes it
|
|
384
|
+
handy on iPad.
|
|
385
|
+
|
|
386
|
+
## Documentation
|
|
387
|
+
|
|
388
|
+
* [`docs/testing-ipad-colab.md`](https://unigpu.pages.xlim.fr/unigpu/docs/testing-ipad-colab.html): testing on iPad
|
|
389
|
+
(Metal on the M4 GPU, simulator) and on Google Colab (NVIDIA GPU);
|
|
390
|
+
* [`docs/api.md`](https://unigpu.pages.xlim.fr/unigpu/docs/api.html): API reference, generated from the docstrings
|
|
391
|
+
(`python docs/generate_api.py`);
|
|
392
|
+
* [`docs/architecture.md`](https://unigpu.pages.xlim.fr/unigpu/docs/architecture.html): organization of the code,
|
|
393
|
+
SOLID/DRY principles, the rules every change follows (interfaces, unit
|
|
394
|
+
tests, TDD), how to add a backend or a primitive.
|
|
395
|
+
|
|
396
|
+
## Tests and quality
|
|
397
|
+
|
|
398
|
+
```bash
|
|
399
|
+
pip install -e ".[dev,opencl-cpu]"
|
|
400
|
+
pytest --cov=unigpu # unit + functional tests, on each detected device
|
|
401
|
+
ruff check .
|
|
402
|
+
mypy unigpu # not at zero errors yet
|
|
403
|
+
python tests/tools/check_clean_code.py # rules of clean code, not all followed yet
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
Every change follows the rules of
|
|
407
|
+
[`docs/architecture.md`](https://unigpu.pages.xlim.fr/unigpu/docs/architecture.html#software-engineering-rules):
|
|
408
|
+
every class behind an interface, its collaborators injected, small classes
|
|
409
|
+
and functions (*Clean Code*), unit tests of each class with its
|
|
410
|
+
collaborators mocked, test-driven development. The same page says where
|
|
411
|
+
the code stands.
|
|
412
|
+
|
|
413
|
+
* The functional tests run each kernel on every detected device. The CUDA
|
|
414
|
+
backend is tested by default with numba's simulator
|
|
415
|
+
(`NUMBA_ENABLE_CUDASIM=1`). On an NVIDIA machine, run
|
|
416
|
+
`UNIGPU_TEST_CUDA=real pytest`.
|
|
417
|
+
* Every kernel of the project (tests, self-test, examples, benchmarks) is
|
|
418
|
+
compiled to PTX by numba-cuda's real compiler, without a GPU, when
|
|
419
|
+
numba-cuda is installed (`tests/tools/check_cuda_ptx.py`). Likewise, they
|
|
420
|
+
are compiled to AMD GPU code objects by numba-hip and COMGR, without a GPU,
|
|
421
|
+
where ROCm and numba-hip are installed (`tests/tools/check_hip_codeobj.py`,
|
|
422
|
+
for gfx90a, gfx942, gfx1100 and gfx1201). The GitLab CI (`.gitlab-ci.yml`)
|
|
423
|
+
runs both checks, with `--strict`: a missing compiler is then a failure.
|
|
424
|
+
* The generated OpenCL C code is compiled by `clang -cl-std=CL1.2 -target spir64 -Werror`.
|
|
425
|
+
The generated Metal code is compiled by `clang++` against a stand-in
|
|
426
|
+
`<metal_stdlib>` header, which checks syntax and types without a Mac.
|
|
427
|
+
* The Metal backend is tested with fake PyObjC and rubicon-objc/objc_util
|
|
428
|
+
modules, and the HIP backend with numba's simulator (`tests/fakes`).
|
|
429
|
+
|
|
430
|
+
## Status of the backends
|
|
431
|
+
|
|
432
|
+
| Backend | What has been checked |
|
|
433
|
+
|----------|-----------------------|
|
|
434
|
+
| `sim` | Fully tested. |
|
|
435
|
+
| `opencl` | Fully tested on POCL (CPU). Not tried on a GPU yet; the known differences between drivers are handled (limits of a kernel read before its arguments are set, events without marker timestamps, largest buffer). |
|
|
436
|
+
| `cuda` | Tested with numba's simulator; every kernel of the project is compiled to PTX by the real numba-cuda compiler. Everything is delegated to numba-cuda, but it remains to be tried on a real NVIDIA GPU. |
|
|
437
|
+
| `hip` | Same code as `cuda`, with numba-hip, minus what numba-hip does not provide (atomics on arrays, dynamic shared memory, `print()`, `float16`). Every kernel of the project is compiled to AMD GPU code objects by numba-hip, without a GPU; not tried on an AMD GPU yet. |
|
|
438
|
+
| `metal` | Works on an iPad (M4) in Carnets and in Code App, through rubicon-objc. Driver logic tested with fake PyObjC and rubicon-objc/objc_util; PyObjC calls checked against its metadata; Metal code syntax checked by clang++. Not tried on a Mac (PyObjC) yet. |
|
|
439
|
+
|
|
440
|
+
Test reports on real hardware are welcome.
|
|
441
|
+
|
|
442
|
+
> **Note**: according to its README, numba-cuda is now in "maintenance mode";
|
|
443
|
+
> NVIDIA directs new developments to
|
|
444
|
+
> [numba-cuda-mlir](https://github.com/NVIDIA/numba-cuda-mlir). The kernel
|
|
445
|
+
> API, which unigpu reproduces, stays the same.
|
|
446
|
+
|
|
447
|
+
## License
|
|
448
|
+
|
|
449
|
+
unigpu is distributed under the [MIT license](https://unigpu.pages.xlim.fr/unigpu/LICENSE): anyone may use, copy,
|
|
450
|
+
modify and redistribute it, provided that the copyright notice and the license
|
|
451
|
+
are kept.
|