CLyther
Encyclopedia
CLyther is a Python
Python (programming language)
Python is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...

 tool similar to Cython
Cython
Cython is a programming language to simplify writing C and C++ extension modules for the CPython Python runtime. Strictly speaking, Cython syntax is a superset of Python syntax additionally supporting:...

. CLyther is a Python language extension that makes writing OpenCL code as easy as Python itself. CLyther currently only supports a subset of the Python language definition but adds many new features to OpenCL. CLyther was inspired by PyCUDA and its views on metaprogramming
Metaprogramming
Metaprogramming is the writing of computer programs that write or manipulate other programs as their data, or that do part of the work at compile time that would otherwise be done at runtime...

.

CLyther exposes both the OpenCL C library as well as the OpenCL language to python.

Features

  • Fast prototyping of OpenCL code
  • Create OpenCL code using the Python language definition
  • Strong object-oriented programming
    Object-oriented programming
    Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

    in OpenCL code
  • Passing functions as arguments to kernel functions
  • Python emulation mode of OpenCL code
  • Fancy indexing of arrays
  • Device memory management
  • Dynamic compilation at runtime

Example

This is an example of a generic OpenCL reduce operator. The arguments include oper which may be a Python
function passed to reduce at runtime.


@clyther.kernel
@clyther.const( 'group_size' )
@clyther.bind( 'global_work_size', 1 )
@clyther.bind( 'local_work_size', 'group_size' )
def reduce( output, input, shared, oper, group_size=512 ):

lid = clrt.get_local_id(0)

gid = clrt.get_group_id(0)
gsize = clrt.get_num_groups(0)

gs2 = group_size * 2

stride = gs2 * gsize

shared[lid] = 0.0

i = gid * gs2 + lid

shared[lid] = 0

while i < input.size:
shared[lid] = oper( shared[lid], input[i] )
shared[lid] = oper( shared[lid], input[i + group_size] )

i += stride

clrt.barrier( clrt.K_LOCAL_MEM_FENCE )

for cgs in [ 512, 256, 128, 64, 32, 16, 8, 4, 2 ]:
if group_size >= cgs:
if lid < cgs/2:
shared[lid] = oper( shared[lid], shared[lid + cgs/2] )
clrt.barrier( clrt.K_LOCAL_MEM_FENCE )

if lid 0:
output[gid] = shared[0]


This example shows an extensible OpenCL reduction operation written with CLyther.

To use this example you may run something like.

  1. where `clarray` is a device array.


@cl.device
def add( a, b ):
return a+b

output = clyther.buffer( [1], ctype=clarray.ctype )
shared = clyther.shared( ctype=ctypes.c_float )

reduce( output, clarray, shared, add, group_size=128 )

print output.item

External links
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK