A easy way to run OpenCL kernel files
Project description
OpenCL Kernel Python Wrapper
Install
Install from wheel
download wheel from release and install
Compile from source
Clone this repo
clone by http
git clone --recursive https://github.com/jinmingyi1998/opencl_kernels.git
with ssh
git clone --recursive git@github.com:jinmingyi1998/opencl_kernels.git
Install
cd opencl_kernels
python setup.py install
DO NOT move this directory after install
Requirements
- OpenCL GPU hardware
- numpy
- cmake > 3.16
Usage
Kernel File:
a file named add.cl
kernel void add(global float*a, global float*out, int int_arg, float float_arg){
int x = get_global_id(0);
if(x==0){
printf(" accept int arg: %d, accept float arg: %f\n",int_arg,float_arg);
}
out[x] = a[x] * float_arg + int_arg;
}
Python Code
OOP Style
import numpy as np
import oclk
a = np.random.rand(100, 100).reshape([10, -1])
a = np.float32(a)
out = np.zeros(a.shape)
out = np.float32(out)
runner = oclk.Runner()
runner.load_kernel("add.cl", "add", "")
timer = oclk.TimerArgs(
enable=True,
warmup=10,
repeat=50,
name='add_kernel'
)
runner.run(
kernel_name="add",
input=[
{"name": "a", "value": a, },
{"name": "out", "value": out, },
{"name": "int_arg", "value": 1, },
{"name": "float_arg", "value": 12.34}
],
output=['out'],
local_work_size=[1, 1],
global_work_size=a.shape,
timer=timer
)
# check result
a = a.reshape([-1])
out = out.reshape([-1])
print(a[:8])
print(out[:8])
Call with Functions
import numpy as np
import oclk
a = np.random.rand(100, 100).reshape([10, -1])
a = np.float32(a)
out = np.zeros(a.shape)
out = np.float32(out)
oclk.init()
oclk.load_kernel("add.cl", "add", "")
r = oclk.run(
kernel_name="add",
input=[
{"name": "a", "value": a, },
{"name": "out", "value": out, },
{"name": "int_arg", "value": 1, },
{"name": "float_arg", "value": 12.34}
],
output=['out'],
local_work_size=[1, 1],
global_work_size=a.shape
)
# check result
a = a.reshape([-1])
out = out.reshape([-1])
print(a[:8])
print(out[:8])
Python api Usage
API
def run(*, kernel_name: str,
input: Dict[str, Union[int, float, np.array]],
output: List[str],
local_work_size: List[int],
global_work_size: List[int],
wait: bool = True,
timer: Union[Dict, TimerArgs] = TimerArgsDisabled) -> List[np.ndarray]: ...
- input: Dictionary to set input args, in the same order as kernel function
- args from np.array should be contiguous array
- constant args only support (will support more types):
- python type: float -> c type: float
- python type: int -> c type: long
- output: List of names to specify which array will be get back from GPU buffer
- local_work_size/global_work_work: list of integer, specified work sizes
- wait: Optional, default true, wait for GPU
- timer: Optional, arguments to set up a timer for benchmark kernels
example
a = np.zeros([16, 16, 16], dtype=np.float32)
b = np.zeros([16, 16, 16], dtype=np.float32)
c = np.zeros([16, 16, 16], dtype=np.float32)
timer = TimerArgs(enable=True,
warmup=10,
repeat=100,
name='timer_name'
)
run(kernel_name='add',
input=[
{"name": "a", "value": a, },
{"name": "b", "value": b, },
{"name": "int_arg", "value": 1, },
{"name": "float_arg", "value": 12.34},
{"name": "c", "value": c}
],
output=['c'],
local_work_size=[1, 1, 1],
global_work_size=a.shape,
timer=timer
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distributions
Close
Hashes for pyoclk-1.0.2-cp312-cp312-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff28199c4357db994d8fedf8e607afc829f27162998c5be9262e4ee3a9a208d0 |
|
MD5 | a822d54ca04e5bdd4414081d29cc2985 |
|
BLAKE2b-256 | f004e5c6d9a10f6b356264534b322ee42f129ba8ccf337de069d25ec732a01ec |
Close
Hashes for pyoclk-1.0.2-cp311-cp311-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a71656840e5d15af91c20f48c9f32a9210e9915fad282020aea51505668a106 |
|
MD5 | a220ac6dccc66f2aa9dab7d29f3b043d |
|
BLAKE2b-256 | d4d89ce63494508b404dee7caa95f38f9991035f28c2282e5cd6a180e48d044a |
Close
Hashes for pyoclk-1.0.2-cp310-cp310-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d4da0b0aba2c2022fff829156b34e4d419f0701a4349681f4670ff96dd58313c |
|
MD5 | 80e4fc25d392e21885fa1d434b4685d8 |
|
BLAKE2b-256 | 638a6ab381357444fee8d493d9edf8234714eaa22af73fa85f51b6db1a1f905c |
Close
Hashes for pyoclk-1.0.2-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbce80c31d89ef75efcce28a7ff4c63044f4c5346d4bd30b1bbfb1a339ce794c |
|
MD5 | e3050a7f222012af28745a5115a37bf8 |
|
BLAKE2b-256 | 54b6368addc8660bde91be8fc149ddc2a169b2e313b97c289447ea9df640a94e |
Close
Hashes for pyoclk-1.0.2-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68526f5e10bb1430593a6f67b6f423ed138ca84f78c5569f83a35340754b43bc |
|
MD5 | fb0c6fb84a4ed5e3954ac6ab46388e54 |
|
BLAKE2b-256 | a77d055f191012bbdb344c6ef129492eea602ab5dd6115b76b143009007006c4 |
Close
Hashes for pyoclk-1.0.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76622c53943593831570c39d2b6ae125296b738cc05425783857379d5e7c5ae0 |
|
MD5 | e7ae2a471cf3eb97c71031365a71ad24 |
|
BLAKE2b-256 | 4c2a35ec1fde509db8c4caee50facacfe19e1c8758dbfd8f7117d470e5163995 |
Close
Hashes for pyoclk-1.0.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e21b99dc49336905a52d8c6284739d9d9c042037ce5ffcf2e9efa52a312c9c50 |
|
MD5 | bdfda2c3871cff53df55b89886745a0e |
|
BLAKE2b-256 | 74ae27d099fe1c8354d4b999c2ccba1a55b098af52923a2394e802c2dd82d6c9 |