codepy.cgen – C-Generation Reference Documentation

class codepy.cgen.Generable

Bases: object

generate(with_semicolon=True)
Generate (i.e. yield) the lines making up this code construct.
__str__()
Return a single string (possibly containing newlines) representing this code construct.
class codepy.cgen.Block(contents=[])
Bases: codepy.cgen.Generable
class codepy.cgen.Module(contents=[])
Bases: codepy.cgen.Block
class codepy.cgen.Line(text='')
Bases: codepy.cgen.Generable

Data and Functions

class codepy.cgen.FunctionBody(fdecl, body)

Bases: codepy.cgen.Generable

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

class codepy.cgen.Initializer(vdecl, data)
Bases: codepy.cgen.Generable

Preprocessor Code

class codepy.cgen.Define(symbol, value)
Bases: codepy.cgen.Generable
class codepy.cgen.Comment(text)
Bases: codepy.cgen.Generable
class codepy.cgen.Include(filename, system=True)
Bases: codepy.cgen.Generable
class codepy.cgen.Pragma(value)
Bases: codepy.cgen.Generable

Declarators

class codepy.cgen.Declarator

Bases: codepy.cgen.Generable

get_decl_pair()

Return a tuple (typename, rhs).

typename is a 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 codepy.cgen.Value(typename, name)

Bases: codepy.cgen.Declarator

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

class codepy.cgen.POD(dtype, name)

Bases: codepy.cgen.Declarator

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

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

Bases: codepy.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 codepy.cgen.GenerableStruct(tpname, fields, declname=None, align_bytes=1, aligned_prime_to=[])

Bases: codepy.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=1, 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.)

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.

__len__()
Return the number of bytes occupied by this struct.
struct_format()
Return the format of the struct as digested by the struct module.

Nested Declarators

class codepy.cgen.NestedDeclarator(subdecl)
Bases: codepy.cgen.Declarator
class codepy.cgen.ArrayOf(subdecl, count=None)
Bases: codepy.cgen.NestedDeclarator
class codepy.cgen.Const(subdecl)
Bases: codepy.cgen.NestedDeclarator
class codepy.cgen.FunctionDeclaration(subdecl, arg_decls)
Bases: codepy.cgen.NestedDeclarator
class codepy.cgen.MaybeUnused(subdecl)
Bases: codepy.cgen.NestedDeclarator
class codepy.cgen.Pointer(subdecl)
Bases: codepy.cgen.NestedDeclarator
class codepy.cgen.Reference(subdecl)
Bases: codepy.cgen.Pointer
class codepy.cgen.Template(template_spec, subdecl)
Bases: codepy.cgen.NestedDeclarator

Declaration Specifiers

class codepy.cgen.DeclSpecifier(subdecl, spec)
Bases: codepy.cgen.NestedDeclarator
class codepy.cgen.Static(subdecl)
Bases: codepy.cgen.DeclSpecifier
class codepy.cgen.Typedef(subdecl)
Bases: codepy.cgen.DeclSpecifier

Statements

class codepy.cgen.Statement(text)
Bases: codepy.cgen.Generable
class codepy.cgen.Assign(lvalue, rvalue)
Bases: codepy.cgen.Generable
class codepy.cgen.If(condition, then_, else_=None)
Bases: codepy.cgen.Generable
codepy.cgen.make_multiple_ifs(conditions_and_blocks, base=None)
class codepy.cgen.DoWhile(condition, body)
Bases: codepy.cgen.Loop
class codepy.cgen.For(start, condition, end, body)
Bases: codepy.cgen.Loop
class codepy.cgen.While(condition, body)
Bases: codepy.cgen.Loop

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

This module adds a few Nvidia CUDA features to CodePy’s repertoire. This makes CodePy a perfect complement to PyCuda: CodePy 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 codepy.cgen.cuda.CudaGlobal(subdecl)
Bases: codepy.cgen.DeclSpecifier
class codepy.cgen.cuda.CudaDevice(subdecl)
Bases: codepy.cgen.DeclSpecifier
class codepy.cgen.cuda.CudaShared(subdecl)
Bases: codepy.cgen.DeclSpecifier
class codepy.cgen.cuda.CudaConstant(subdecl)
Bases: codepy.cgen.DeclSpecifier