Skip to main content

An easy way to run OpenCL kernel files

Project description

OpenCL Kernel Python Wrapper

Install

Requirements

  • OpenCL GPU hardware
  • numpy
  • cmake(if compile from source)

Install from wheel

pip install pyoclk

or 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

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.ascontiguousarray(a, np.float32)
out = np.zeros(a.shape)
out = np.ascontiguousarray(out, np.float32)

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, "type": "int"},
        {"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.ascontiguousarray(a,np.float32)

out = np.zeros_like(a)
out = np.ascontiguousarray(out,np.float32)
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

API Reference

example

import numpy as np

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)

a = np.ascontiguousarray(a,dtype=np.float32)
b = np.ascontiguousarray(b,dtype=np.float32)
c = np.ascontiguousarray(c,dtype=np.float32)

run(kernel_name='add',
    input=[
        {"name": "a", "value": a, },
        {"name": "b", "value": b, },
        {"name": "int_arg", "value": 1, "type": "int"},
        {"name": "float_arg", "value": 12.34},
        {"name": "c", "value": c}
    ],
    output=['c'],
    local_work_size=[1, 1, 1],
    global_work_size=a.shape
    )

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

pyoclk-1.1.2-cp312-cp312-manylinux_2_28_x86_64.whl (768.5 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ x86-64

pyoclk-1.1.2-cp311-cp311-manylinux_2_28_x86_64.whl (769.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

pyoclk-1.1.2-cp310-cp310-manylinux_2_28_x86_64.whl (767.9 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

pyoclk-1.1.2-cp310-cp310-manylinux2010_x86_64.whl (679.8 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ x86-64

pyoclk-1.1.2-cp39-cp39-manylinux_2_28_x86_64.whl (768.0 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ x86-64

pyoclk-1.1.2-cp39-cp39-manylinux2010_x86_64.whl (679.8 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

pyoclk-1.1.2-cp38-cp38-manylinux_2_28_x86_64.whl (767.7 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.28+ x86-64

pyoclk-1.1.2-cp38-cp38-manylinux2010_x86_64.whl (679.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

pyoclk-1.1.2-cp37-cp37m-manylinux_2_28_x86_64.whl (770.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.28+ x86-64

pyoclk-1.1.2-cp37-cp37m-manylinux2010_x86_64.whl (682.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

pyoclk-1.1.2-cp36-cp36m-manylinux_2_28_x86_64.whl (770.5 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.28+ x86-64

pyoclk-1.1.2-cp36-cp36m-manylinux2010_x86_64.whl (682.7 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page