codegen package

Package providing the code generation framework.

_prefs module

Module declaring general code generation preferences.


Code generation preferences

codegen.loop_invariant_optimisations = True

Whether to pull out scalar expressions out of the statements, so that they are only evaluated once instead of once for every neuron/synapse/… Can be switched off, e.g. because it complicates the code (and the same optimisation is already performed by the compiler) or because the code generation target does not deal well with it. Defaults to True.

codegen.max_cache_dir_size = 1000

The size of a directory (in MB) with cached code for weave or Cython that triggers a warning. Set to 0 to never get a warning.

codegen.string_expression_target = 'numpy'

Default target for the evaluation of string expressions (e.g. when indexing state variables). Should normally not be changed from the default numpy target, because the overhead of compiling code is not worth the speed gain for simple expressions.

Accepts the same arguments as, except for 'auto' = 'auto'

Default target for code generation.

Can be a string, in which case it should be one of:

  • 'auto' the default, automatically chose the best code generation target available.
  • 'weave' uses scipy.weave to generate and compile C++ code, should work anywhere where gcc is installed and available at the command line.
  • 'cython', uses the Cython package to generate C++ code. Needs a working installation of Cython and a C++ compiler.
  • 'numpy' works on all platforms and doesn’t need a C compiler but is often less efficient.

Or it can be a CodeObject class.

codeobject module

Module providing the base CodeObject and related functions.

Exported members: CodeObject, CodeObjectUpdater, constant_or_scalar


CodeObject(owner, code, variables, …[, name]) Executable code object.


constant_or_scalar(varname, variable) Convenience function to generate code to access the value of a variable.
create_runner_codeobj(group, code, …[, …]) Create a CodeObject for the execution of code in the context of a Group.

cpp_prefs module

Preferences related to C++ compilation


C++ compilation preferences

codegen.cpp.compiler = ''

Compiler to use (uses default if empty)

Should be gcc or msvc.

codegen.cpp.define_macros = []

List of macros to define; each macro is defined using a 2-tuple, where ‘value’ is either the string to define it to or None to define it without a particular value (equivalent of “#define FOO” in source or -DFOO on Unix C compiler command line).

codegen.cpp.extra_compile_args = None

Extra arguments to pass to compiler (if None, use either extra_compile_args_gcc or extra_compile_args_msvc).

codegen.cpp.extra_compile_args_gcc = ['-w', '-O3', '-ffast-math', '-fno-finite-math-only', '-march=native']

Extra compile arguments to pass to GCC compiler

codegen.cpp.extra_compile_args_msvc = ['/Ox', '/w', '/arch:AVX2', '/MP']

Extra compile arguments to pass to MSVC compiler (the default /arch: flag is determined based on the processor architecture)
Any extra platform- and compiler-specific information to use when linking object files together.

codegen.cpp.headers = []

A list of strings specifying header files to use when compiling the code. The list might look like [“<vector>”,“‘my_header’”]. Note that the header strings need to be in a form than can be pasted at the end of a #include statement in the C++ code.

codegen.cpp.include_dirs = []

Include directories to use. Note that $prefix/include will be appended to the end automatically, where $prefix is Python’s site-specific directory prefix as returned by sys.prefix.

codegen.cpp.libraries = []

List of library names (not filenames or paths) to link against.

codegen.cpp.library_dirs = []

List of directories to search for C/C++ libraries at link time. Note that $prefix/lib will be appended to the end automatically, where $prefix is Python’s site-specific directory prefix as returned by sys.prefix.

codegen.cpp.msvc_architecture = ''

MSVC architecture name (or use system architectue by default).

Could take values such as x86, amd64, etc.

codegen.cpp.msvc_vars_location = ''

Location of the MSVC command line tool (or search for best by default).

codegen.cpp.runtime_library_dirs = []

List of directories to search for C/C++ libraries at run time.

Exported members: get_compiler_and_args


get_compiler_and_args() Returns the computed compiler and compilation flags
update_for_cross_compilation(library_dirs, …) Update the compiler arguments to allow cross-compilation for 32bit on a 64bit Linux system.

optimisation module

Simplify and optimise sequences of statements by rewriting and pulling out loop invariants.

Exported members: optimise_statements, ArithmeticSimplifier, Simplifier


ArithmeticSimplifier(variables) Carries out the following arithmetic simplifications:
Simplifier(variables, scalar_statements[, …]) Carry out arithmetic simplifications (see ArithmeticSimplifier) and loop invariants


cancel_identical_terms(primary, inverted) Cancel terms in a collection, e.g.
collect(node) Attempts to collect commutative operations into one and simplifies them.
collect_commutative(node, primary, inverted, …)
evaluate_expr(expr, ns) Try to evaluate the expression in the given namespace
expression_complexity(expr, variables)
optimise_statements(scalar_statements, …) Optimise a sequence of scalar and vector statements
reduced_node(terms, op) Reduce a sequence of terms with the given operator

permutation_analysis module

Module for analysing synaptic pre and post code for synapse order independence.

Exported members: OrderDependenceError, check_for_order_independence




check_for_order_independence(statements, …) Check that the sequence of statements doesn’t depend on the order in which the indices are iterated through.

statements module

Module providing the Statement class.


Statement(var, op, expr, comment, dtype[, …]) A single line mathematical statement.

targets module

Module that stores all known code generation targets as codegen_targets.

Exported members: codegen_targets

templates module

Handles loading templates from a directory.

Exported members: Templater


CodeObjectTemplate(template, template_source) Single template object returned by Templater and used for final code generation
LazyTemplateLoader(environment, extension) Helper object to load templates only when they are needed.
MultiTemplate(module) Code generated by a CodeObjectTemplate with multiple blocks
Templater(package_name, extension[, env_globals]) Class to load and return all the templates a CodeObject defines.


variables_to_array_names(variables[, …])

translation module

This module translates a series of statements into a language-specific syntactically correct code block that can be inserted into a template.

It infers whether or not a variable can be declared as constant, etc. It should handle common subexpressions, and so forth.

The input information needed:

  • The sequence of statements (a multiline string) in standard mathematical form
  • The list of known variables, common subexpressions and functions, and for each variable whether or not it is a value or an array, and if an array what the dtype is.
  • The dtype to use for newly created variables
  • The language to translate to

Exported members: make_statements(), analyse_identifiers(), get_identifiers_recursively()


LineInfo(**kwds) A helper class, just used to store attributes.


analyse_identifiers(code, variables[, recursive]) Analyses a code string (sequence of statements) to find all identifiers by type.
get_identifiers_recursively(expressions, …) Gets all the identifiers in a list of expressions, recursing down into subexpressions.
is_scalar_expression(expr, variables) Whether the given expression is scalar.
make_statements(code, variables, dtype[, …]) Turn a series of abstract code statements into Statement objects, inferring whether each line is a set/declare operation, whether the variables are constant or not, and handling the cacheing of subexpressions.