API

clyther

class clyther.kernel(func)

Create an OpenCL kernel from a Python function.

This class can be used as a decorator:

@kernel
def foo(a):
    ...
clear_cache()

Clear the binary cache in memory.

compile(ctx, source_only=False, cly_meta=None, **kwargs)

Compile a kernel or lookup in cache.

Parameters:
  • ctx – openCL context
  • cly_meta – meta-information for inspecting the cache. (does nothing)
  • kwargs – All other keyword arguments are used for type information.
Returns:

An OpenCL kernel

compile_or_cly(ctx, source_only=False, cly_meta=None, **kwargs)

internal

db_filename

get the filename that the binaries can be cached to

run_kernel(cl_kernel, queue, kernel_args, kwargs)

Run a kernel this method is subclassable for the task class.

source(ctx, *args, **kwargs)

Get the source that would be compiled for specific argument types.

Note

This is meant to have a similar signature to the function call. i.e:

print func.source(queue.context, arg1, arg2) 
func(queue, arg1, arg2)
translate(ctx, **kwargs)

Translate this func into a tuple of (args, defaults, kernel_name, source)

clyther.global_work_size(arg)

Bind the global work size of an nd range kernel to a arguments.

Parameters:arg – can be either a list of integers or a function with the same signature as the python kernel.
clyther.local_work_size(arg)

Bind the local work size of an nd range kernel to a arguments.

Parameters:arg – can be either a list of integers or a function with the same signature as the python kernel.
clyther.cache(chache_obj)

Set the caching type for a kernel binaries to file. (default is clyther.caching.NoFileCache) Use cache as a decorator:

@cache(HDFCache)
@cly.kernel
def foo(...):
    ...
Parameters:chache_obj – Cache object
class clyther.task(func)

Create an OpenCL kernel from a Python function.

Calling this object will enqueue a task.

This class can be used as a decorator:

@task
def foo(a):
    ...
emulate(ctx, *args, **kwargs)

Run this function in emulation mode.

run_kernel(cl_kernel, queue, kernel_args, kwargs)

Run the kernel as a single thread task.

clyther.test(stream=<open file '<stdout>', mode 'w' at 0x23f078>, descriptions=True, verbosity=2, failfast=False, buffer=False)[source]

Load and run the clyther test suite.

clyther.runtime

clyther.runtime.get_global_id(c_ulong)

This is the doc for get_global_id

clyther.runtime.get_group_id(c_ulong)
clyther.runtime.get_local_id(c_ulong)
clyther.runtime.get_num_groups(c_ulong)
clyther.runtime.get_global_size(c_ulong)

Returns the number of global work-items specified for dimension identified by dimindx. This value is given by the global_work_size argument to

clyther.array

The clyther.array module is designed to replicatie the functionality of numpy for OpenCL memory objects.

the main entry point into this module is the CLArrayContext class.

The end goal is to seamlessly be able to switch between OpenCL and numpy.

Where you would write:

import numpy as np

a = np.arange(0, 10.)

To specify a GPU array you would write:

import opencl as cl
from clyther.array import CLArrayContext

ca = CLArrayContext(device_type=cl.Device.GPU)

a = ca.arange(0, 10.)
class clyther.array.CLArrayContext(*args, **kwargs)

classdocs

add()
arange(ctx, *args, **kwargs)
asarray(ctx, other, queue=None, copy=True)
empty(context, shape, ctype='f', cls=<class 'clyther.array.clarray.CLArray'>, queue=None)
empty_like(context, A)
classmethod func(func)
linspace(ctx, start, stop, num=50, ctype='f', queue=None)
classmethod method(name)
multiply()
power()
queue
setslice(context, arr, value)
sin()
subtract()
sum()
zeros(context, shape, ctype='f', cls=<class 'clyther.array.clarray.CLArray'>, queue=None)

clyther.runtime

clyther.runtime.get_global_id(c_ulong)

This is the doc for get_global_id

clyther.runtime.get_group_id(c_ulong)
clyther.runtime.get_local_id(c_ulong)
clyther.runtime.get_num_groups(c_ulong)
clyther.runtime.get_global_size(c_ulong)

Returns the number of global work-items specified for dimension identified by dimindx. This value is given by the global_work_size argument to

clyther.rttt

Run Time Type Tree (rttt)

class clyther.rttt.RuntimeConstant(name, rtt)[source]

define a constant value that is defined in the OpenCL runtime.

Parameters:
  • name – the name of the constant in OpenCL.
  • rtt – the ctype of the constant.
class clyther.rttt.RuntimeFunction(name, return_type, *argtypes, **kwargs)[source]

A function that is defined in the openCL runtime.

Parameters:
  • name – the name of the function as per the oencl specification.
  • return_type – Either a ctype or a function that returns a ctype
  • argtypes – Either a ctype or a function that returns a ctype
Keyword only parameters:
param doc:Either a ctype or a function that returns a ctype
param builtin:a python builtin function that is equivalent to this function
param emulate:A function that emulates the behavior of this function in python. This argument is not required if builtin is given.

If return_type is a function it must have the same signature as the runtime function.

class clyther.rttt.TypeReplacer(defined_types)[source]

Replace ctype with opencl type string.

class clyther.rttt.gentype(*types)[source]

a generic numeric type in OpenCL

class clyther.rttt.sgentype(*types)[source]

a signed generic numeric type in OpenCL

class clyther.rttt.ugentype(*types)[source]

an unsigned generic numeric type in OpenCL

clyther.pipeline

clyther.pipeline.create_kernel_source(function, argtypes)[source]

Create OpenCL source code from a Python function.

Parameters:
  • function – A pure python function
  • argtypes – A dict of name:type for the compiler to use in optimizing the function.

Steps:

  • Decompile to AST.:

    Get AST from python bytecode

  • Typify AST:

    This transforms the AST in to a partially typed OpenCL AST. It will recursively dive into pure Python functions and add thoes to the OpenCL module. Function names will be replace with placeholders for the namespace conflict resolution stage.

  • Replace Constants:

    Replace Python constants with values. e.g. math.e -> 2.7182

  • Unpack memobjects:

    To support multi-dimensional indexing and non contiguous memory arrays. CLyther adds a uint8 to the function signature to store this information.

  • Replace calls of types to type casts e.g. int(i) -> ((int)(i))

  • Format for loops:

    only range or a explicit Python list is currently supported as the iterator in a for loop.

  • Remove arguments to functions that are constant. e.g. python functions.

  • Move keyword arguments in function calls to positional arguments.

  • Resolve function place-holders

  • Make printf statements from print

  • Replace Types:

    Replace python ctype objects with OpenCL type names. This will also define structs in the module if required.

  • Generate Source

Table Of Contents

Previous topic

CLyther for NumPy Users

Next topic

Contributing

This Page