Name: opencl4py
Owner: Samsung
Description: Python cffi OpenCL bindings and helper classes
Created: 2014-03-25 06:47:54.0
Updated: 2017-05-31 15:46:19.0
Pushed: 2016-01-03 20:31:18.0
Homepage: null
Size: 167
Language: Python
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Python cffi OpenCL bindings and helper classes.
Tested with Python 2.7, Python 3.3, Python 3.4 and PyPy on Linux, MacOSX and Windows.
To use clBLAS, libclBLAS.so (clBLAS.dll) should be present.
Not all OpenCL api is currently covered.
To install the module run:
install .
or just copy src/opencl4py to any place where python interpreter will be able to find it.
To run the tests, execute:
for Python 2.7:
ONPATH=src nosetests -w tests
for Python 3.3, 3.4:
ONPATH=src nosetests3 -w tests
for PyPy:
ONPATH=src pypy -m nose -w tests
Example usage:
rt opencl4py as cl
rt logging
rt numpy
_name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
platforms = cl.Platforms()
logging.info("OpenCL devices available:\n\n%s\n",
platforms.dump_devices())
ctx = platforms.create_some_context()
queue = ctx.create_queue(ctx.devices[0])
prg = ctx.create_program(
"""
__kernel void test(__global const float *a, __global const float *b,
__global float *c, const float k) {
size_t i = get_global_id(0);
c[i] = (a[i] + b[i]) * k;
}
""")
krn = prg.get_kernel("test")
a = numpy.arange(1000000, dtype=numpy.float32)
b = numpy.arange(1000000, dtype=numpy.float32)
c = numpy.empty(1000000, dtype=numpy.float32)
k = numpy.array([0.5], dtype=numpy.float32)
a_buf = ctx.create_buffer(cl.CL_MEM_READ_ONLY | cl.CL_MEM_COPY_HOST_PTR,
a)
b_buf = ctx.create_buffer(cl.CL_MEM_READ_ONLY | cl.CL_MEM_COPY_HOST_PTR,
b)
c_buf = ctx.create_buffer(cl.CL_MEM_WRITE_ONLY | cl.CL_MEM_ALLOC_HOST_PTR,
size=c.nbytes)
krn.set_args(a_buf, b_buf, c_buf, k[0:1])
queue.execute_kernel(krn, [a.size], None)
queue.read_buffer(c_buf, c)
max_diff = numpy.fabs(c - (a + b) * k[0]).max()
logging.info("max_diff = %.6f", max_diff)
Released under Simplified BSD License. Copyright (c) 2014, Samsung Electronics Co.,Ltd.