cgen – C-Generation Reference Documentation

cgen.dtype_to_ctype(dtype)[source]
class cgen.Generable[source]

Bases: object

__str__()[source]

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

generate(with_semicolon=True)[source]

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

class cgen.Block(contents=[])[source]

Bases: cgen.Generable

class cgen.Collection(contents=[])[source]

Bases: cgen.Block

cgen.Module[source]

alias of cgen.Collection

class cgen.Line(text='')[source]

Bases: cgen.Generable

Data and Functions

class cgen.FunctionBody(fdecl, body)[source]

Bases: cgen.Generable

class cgen.Initializer(vdecl, data)[source]

Bases: cgen.Generable

Preprocessor Code

class cgen.Define(symbol, value)[source]

Bases: cgen.Generable

class cgen.Comment(text, skip_space=False)[source]

Bases: cgen.Generable

class cgen.Include(filename, system=True)[source]

Bases: cgen.Generable

class cgen.Pragma(value)[source]

Bases: cgen.Generable

Declarators

class cgen.Declarator[source]

Bases: cgen.Generable

generate(with_semicolon=True)[source]

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

get_decl_pair()[source]

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)[source]

Return the declarator as a single line.

class cgen.Value(typename, name)[source]

Bases: cgen.Declarator

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

class cgen.POD(dtype, name)[source]

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)[source]

Bases: cgen.Declarator

A structure declarator.

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

Bases: cgen.Struct

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

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__()[source]

Return the number of bytes occupied by this struct.

make(**kwargs)[source]

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

make_with_defaults(**kwargs)[source]

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()[source]

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

class cgen.Enum[source]

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()[source]

Return a string with C defines corresponding to these constants.

classmethod stringify_value(val)[source]

Return a string description of the flags set in val.

Nested Declarators

class cgen.NestedDeclarator(subdecl)[source]

Bases: cgen.Declarator

class cgen.ArrayOf(subdecl, count=None)[source]

Bases: cgen.NestedDeclarator

class cgen.Const(subdecl)[source]

Bases: cgen.NestedDeclarator

class cgen.FunctionDeclaration(subdecl, arg_decls)[source]

Bases: cgen.NestedDeclarator

class cgen.MaybeUnused(subdecl)[source]

Bases: cgen.NestedDeclarator

class cgen.Pointer(subdecl)[source]

Bases: cgen.NestedDeclarator

class cgen.Reference(subdecl)[source]

Bases: cgen.Pointer

class cgen.Template(template_spec, subdecl)[source]

Bases: cgen.NestedDeclarator

Declaration Specifiers

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

Bases: cgen.NestedDeclarator

class cgen.Static(subdecl)[source]

Bases: cgen.DeclSpecifier

class cgen.Typedef(subdecl)[source]

Bases: cgen.DeclSpecifier

Statements

class cgen.Statement(text)[source]

Bases: cgen.Generable

class cgen.Assign(lvalue, rvalue)[source]

Bases: cgen.Generable

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

Bases: cgen.Generable

cgen.make_multiple_ifs(conditions_and_blocks, base=None)[source]
class cgen.Loop(body)[source]

Bases: cgen.Generable

class cgen.DoWhile(condition, body)[source]

Bases: cgen.Loop

class cgen.For(start, condition, update, body)[source]

Bases: cgen.Loop

class cgen.While(condition, body)[source]

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)[source]

Bases: cgen.DeclSpecifier

class cgen.cuda.CudaDevice(subdecl)[source]

Bases: cgen.DeclSpecifier

class cgen.cuda.CudaShared(subdecl)[source]

Bases: cgen.DeclSpecifier

class cgen.cuda.CudaConstant(subdecl)[source]

Bases: cgen.DeclSpecifier

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

cgen.opencl.dtype_to_cltype(dtype)[source]

Kernels and Kernel Arguments

class cgen.opencl.CLKernel(subdecl)[source]

Bases: cgen.DeclSpecifier

class cgen.opencl.CLConstant(subdecl)[source]

Bases: cgen.DeclSpecifier

class cgen.opencl.CLLocal(subdecl)[source]

Bases: cgen.DeclSpecifier

class cgen.opencl.CLGlobal(subdecl)[source]

Bases: cgen.DeclSpecifier

class cgen.opencl.CLImage(dims, mode, name)[source]

Bases: cgen.Value

Function Attributes

class cgen.opencl.CLVecTypeHint(subdecl, dtype=None, count=None, type_str=None)[source]

Bases: cgen.NestedDeclarator

class cgen.opencl.CLWorkGroupSizeHint(dim, subdecl)[source]

See Sec 6.7.2 of OpenCL 2.0 spec, Version V2.2-11.

class cgen.opencl.CLRequiredWorkGroupSize(dim, subdecl)[source]

See Sec 6.7.2 of OpenCL 2.0 spec, Version V2.2-11.

Vector PODs

class cgen.opencl.CLVectorPOD(dtype, count, name)[source]

Bases: cgen.Declarator