cgen – C-Generation Reference Documentation

cgen.dtype_to_ctype(dtype)
class cgen.Generable

Bases: object

__str__()

Return a single string (possibly containing newlines) representing this code construct.

generate(with_semicolon=True)

Generate (i.e. yield) the lines making up this code construct.

class cgen.Block(contents=[])

Bases: cgen.Generable

class cgen.Module(contents=[])

Bases: cgen.Block

class cgen.Line(text='')

Bases: cgen.Generable

Data and Functions

class cgen.FunctionBody(fdecl, body)

Bases: cgen.Generable

Initialize a function definition. fdecl is expected to be a FunctionDeclaration instance, while body is a Block.

class cgen.Initializer(vdecl, data)

Bases: cgen.Generable

Preprocessor Code

class cgen.Define(symbol, value)

Bases: cgen.Generable

class cgen.Comment(text)

Bases: cgen.Generable

class cgen.Include(filename, system=True)

Bases: cgen.Generable

class cgen.Pragma(value)

Bases: cgen.Generable

Declarators

class cgen.Declarator

Bases: cgen.Generable

get_decl_pair()

Return a tuple (type_lines, rhs).

type_lines is a non-empty list of lines (most often just a single one) describing the type of this declarator. rhs is the right- hand side that actually contains the function/array/constness notation making up the bulk of the declarator syntax.

inline(with_semicolon=True)

Return the declarator as a single line.

class cgen.Value(typename, name)

Bases: cgen.Declarator

A simple declarator: typename and name are given as strings.

class cgen.POD(dtype, name)

Bases: cgen.Declarator

A simple declarator: The type is given as a numpy.dtype and the name is given as a string.

class cgen.Struct(tpname, fields, declname=None, pad_bytes=0)

Bases: cgen.Declarator

A structure declarator.

Initialize the structure declarator. tpname is the name of the structure, while declname is the name used for the declarator. pad_bytes is the number of padding bytes added at the end of the structure. fields is a list of Declarator instances.

class cgen.GenerableStruct(tpname, fields, declname=None, align_bytes=None, aligned_prime_to=[])

Bases: cgen.Struct

Initialize a structure declarator. tpname is the name of the structure, while declname is the name used for the declarator. pad_bytes is the number of padding bytes added at the end of the structure. fields is a list of Declarator instances.

align_bytes is an integer that causes the structure to be padded to an integer multiple of itself. aligned_prime_to is a list of integers. If the resulting structure’s size is s, then s//align_bytes will be made prime to all numbers in aligned_prime_to. (Sounds obscure? It’s needed for avoiding bank conflicts in CUDA programming.)

__init__(tpname, fields, declname=None, align_bytes=None, aligned_prime_to=[])

Initialize a structure declarator. tpname is the name of the structure, while declname is the name used for the declarator. pad_bytes is the number of padding bytes added at the end of the structure. fields is a list of Declarator instances.

align_bytes is an integer that causes the structure to be padded to an integer multiple of itself. aligned_prime_to is a list of integers. If the resulting structure’s size is s, then s//align_bytes will be made prime to all numbers in aligned_prime_to. (Sounds obscure? It’s needed for avoiding bank conflicts in CUDA programming.)

__len__()

Return the number of bytes occupied by this struct.

make(**kwargs)

Build a binary, packed representation of self in a str instance with members set to the values specified in kwargs.

make_with_defaults(**kwargs)

Build a binary, packed representation of self in a str instance with members set to the values specified in kwargs.

Unlike make(), not all members have to occur in kwargs.

struct_format()

Return the format of the struct as digested by the struct module.

class cgen.Enum

Bases: cgen.Generable

An enum-like class for Python that can generate an equivalent C-level declaration. Does not work within the usual ‘declarator’ framework because it uses macros to define values, and a separate typedef to define the value type.

New in version 2013.2.

To use, derive from this class, and define the following things:

c_name
dtype
c_value_prefix

A (usually upper-case) prefix to be used as a prefix to the names defined on the Python side.

VALUE

Any class attribute with an all-upper-case name is taken to be as an enum value.

classmethod get_c_defines()

Return a string with C defines corresponding to these constants.

classmethod stringify_value(val)

Return a string description of the flags set in val.

Nested Declarators

class cgen.NestedDeclarator(subdecl)

Bases: cgen.Declarator

class cgen.ArrayOf(subdecl, count=None)

Bases: cgen.NestedDeclarator

class cgen.Const(subdecl)

Bases: cgen.NestedDeclarator

class cgen.FunctionDeclaration(subdecl, arg_decls)

Bases: cgen.NestedDeclarator

class cgen.MaybeUnused(subdecl)

Bases: cgen.NestedDeclarator

class cgen.Pointer(subdecl)

Bases: cgen.NestedDeclarator

class cgen.Reference(subdecl)

Bases: cgen.Pointer

class cgen.Template(template_spec, subdecl)

Bases: cgen.NestedDeclarator

Declaration Specifiers

class cgen.DeclSpecifier(subdecl, spec, sep=' ')

Bases: cgen.NestedDeclarator

class cgen.Static(subdecl)

Bases: cgen.DeclSpecifier

class cgen.Typedef(subdecl)

Bases: cgen.DeclSpecifier

Statements

class cgen.Statement(text)

Bases: cgen.Generable

class cgen.Assign(lvalue, rvalue)

Bases: cgen.Generable

class cgen.If(condition, then_, else_=None)

Bases: cgen.Generable

cgen.make_multiple_ifs(conditions_and_blocks, base=None)
class cgen.DoWhile(condition, body)

Bases: cgen.Loop

class cgen.For(start, condition, update, body)

Bases: cgen.Loop

class cgen.While(condition, body)

Bases: cgen.Loop

cgen.cuda – Extensions to generate CUDA-compatible C Sources

This module adds a few Nvidia CUDA features to cgen’s repertoire. This makes cgen a perfect complement to PyCuda: cgen generates the code, PyCuda compiles it, uploads it to the GPU and executes it.

The PyCuda manual has a tutorial on using the two together.

class cgen.cuda.CudaGlobal(subdecl)

Bases: cgen.DeclSpecifier

class cgen.cuda.CudaDevice(subdecl)

Bases: cgen.DeclSpecifier

class cgen.cuda.CudaShared(subdecl)

Bases: cgen.DeclSpecifier

class cgen.cuda.CudaConstant(subdecl)

Bases: cgen.DeclSpecifier

cgen.opencl – Extensions to generate OpenCL-compatible C Sources

cgen.opencl.dtype_to_cltype(dtype)

Kernels and Kernel Arguments

class cgen.opencl.CLKernel(subdecl)

Bases: cgen.DeclSpecifier

class cgen.opencl.CLConstant(subdecl)

Bases: cgen.DeclSpecifier

class cgen.opencl.CLLocal(subdecl)

Bases: cgen.DeclSpecifier

class cgen.opencl.CLGlobal(subdecl)

Bases: cgen.DeclSpecifier

class cgen.opencl.CLImage(dims, mode, name)

Bases: cgen.Value

Function Attributes

class cgen.opencl.CLVecTypeHint(dtype=None, count=None, type_str=None)

Bases: cgen.NestedDeclarator

class cgen.opencl.CLWorkGroupSizeHint(dim, subdecl)

Bases: cgen.opencl._CLWorkGroupSizeDeclarator

class cgen.opencl.CLRequiredWorkGroupSize(dim, subdecl)

Bases: cgen.opencl._CLWorkGroupSizeDeclarator

Vector PODs

class cgen.opencl.CLVectorPOD(dtype, count, name)

Bases: cgen.Declarator