GSLCodeGenerator class

(Shortest import: from brian2.codegen.generators import GSLCodeGenerator)

class brian2.codegen.generators.GSL_generator.GSLCodeGenerator(variables, variable_indices, owner, iterate_all, codeobj_class, name, template_name, override_conditional_write=None, allows_scalar_write=False)[source]

Bases: object

GSL code generator.

Methods

add_gsl_variables_as_non_scalar(diff_vars)

Add _gsl variables as non-scalar.

add_meta_variables(options)

c_data_type(dtype)

Get string version of object dtype that is attached to Brian variables.

diff_var_to_replace(diff_vars)

Add differential variable-related strings that need to be replaced to go from normal brian to GSL code

find_differential_variables(code)

Find the variables that were tagged _gsl_{var}_f{ind} and return var, ind pairs.

find_function_names()

Return a list of used function names in the self.variables dictionary

find_undefined_variables(statements)

Find identifiers that are not in self.variables dictionary.

find_used_variables(statements, other_variables)

Find all the variables used on the right hand side of the given expressions.

get_dimension_code(diff_num)

Generate code for function that sets the dimension of the ODE system.

initialize_array(varname, values)

Initialize a static array with given floating point values.

is_constant_and_cpp_standalone(var_obj)

Check whether self.cpp_standalone and variable is Constant.

is_cpp_standalone()

Check whether we're running with cpp_standalone.

make_function_code(lines)

Add lines of GSL translated vector code to 'non-changing' _GSL_func code.

scale_array_code(diff_vars, method_options)

Return code for definition of _GSL_scale_array in generated code.

to_replace_vector_vars(variables_in_vector)

Create dictionary containing key, value pairs with to be replaced text to translate from conventional Brian to GSL.

translate(code, dtype)

Translates an abstract code block into the target language.

translate_scalar_code(code_lines, ...)

Translate scalar code: if calculated variables are used in the vector_code their value is added to the variable in the _dataholder.

translate_vector_code(code_lines, to_replace)

Translate vector code to GSL compatible code by substituting fragments of code.

unpack_namespace(variables_in_vector, ...[, ...])

Write code that unpacks Brian namespace to cython/cpp namespace.

unpack_namespace_single(var_obj, in_vector, ...)

Writes the code necessary to pull single variable out of the Brian namespace into the generated code.

var_init_lhs(var, type)

Get string version of the left hand side of an initializing expression

write_dataholder(variables_in_vector)

Return string with full code for _dataholder struct.

write_dataholder_single(var_obj)

Return string declaring a single variable in the _dataholder struct.

yvector_code(diff_vars)

Generate code for function dealing with GSLs y vector.

Details

add_gsl_variables_as_non_scalar(diff_vars)[source]

Add _gsl variables as non-scalar.

In GSLStateUpdater the differential equation variables are substituted with GSL tags that describe the information needed to translate the conventional Brian code to GSL compatible code. This function tells Brian that the variables that contain these tags should always be vector variables. If we don’t do this, Brian renders the tag-variables as scalar if no vector variables are used in the right hand side of the expression.

add_meta_variables(options)[source]
c_data_type(dtype)[source]

Get string version of object dtype that is attached to Brian variables. c pp_generator already has this function, but the Cython generator does not, but we need it for GSL code generation.

diff_var_to_replace(diff_vars)[source]

Add differential variable-related strings that need to be replaced to go from normal brian to GSL code

From the code generated by Brian’s ‘normal’ generators (cpp_generator or cython_generator a few bits of text need to be replaced to get GSL compatible code. The bits of text related to differential equation variables are put in the replacer dictionary in this function.

find_differential_variables(code)[source]

Find the variables that were tagged _gsl_{var}_f{ind} and return var, ind pairs.

GSLStateUpdater tagged differential variables and here we extract the information given in these tags.

find_function_names()[source]

Return a list of used function names in the self.variables dictionary

Functions need to be ignored in the GSL translation process, because the brian generator already sufficiently dealt with them. However, the brian generator also removes them from the variables dict, so there is no way to check whether an identifier is a function after the brian translation process. This function is called before this translation process and the list of function names is stored to be used in the GSL translation.

find_undefined_variables(statements)[source]

Find identifiers that are not in self.variables dictionary.

Brian does not save the _lio_ variables it uses anywhere. This is problematic for our GSL implementation because we save the lio variables in the _dataholder struct (for which we need the datatype of the variables). This function adds the left hand side variables that are used in the vector code to the variable dictionary as AuxiliaryVariables (all we need later is the datatype).

find_used_variables(statements, other_variables)[source]

Find all the variables used on the right hand side of the given expressions.

get_dimension_code(diff_num)[source]

Generate code for function that sets the dimension of the ODE system.

GSL needs to know how many differential variables there are in the ODE system. Since the current approach is to have the code in the vector loop the same for all simulations, this dimension is set by an external function. The code for this set_dimension functon is written here. It is assumed the code will be the same for each target language with the exception of some syntactical differences

initialize_array(varname, values)[source]

Initialize a static array with given floating point values. E.g. in C++, when called with arguments array and [1.0, 3.0, 2.0], this method should return double array[] = {1.0, 3.0, 2.0}.

is_constant_and_cpp_standalone(var_obj)[source]

Check whether self.cpp_standalone and variable is Constant.

This check is needed because in the case of using the cpp_standalone device we do not want to apply our GSL variable conversion (var –> _GSL_dataholder.var), because the cpp_standalone code generation process involves replacing constants with their actual value (‘freezing’). This results in code that looks like (if for example var = 1.2): _GSL_dataholder.1.2 = 1.2 and _GSL_dataholder->1.2. To prevent repetitive calls to get_device() etc. the outcome of is_cpp_standalone is saved.

is_cpp_standalone()[source]

Check whether we’re running with cpp_standalone.

Test if get_device() is instance CPPStandaloneDevice.

make_function_code(lines)[source]

Add lines of GSL translated vector code to ‘non-changing’ _GSL_func code.

Adds nonchanging aspects of GSL _GSL_func code to lines of code written somewhere else (translate_vector_code). Here these lines are put between the non-changing parts of the code and the target-language specific syntax is added.

scale_array_code(diff_vars, method_options)[source]

Return code for definition of _GSL_scale_array in generated code.

to_replace_vector_vars(variables_in_vector, ignore=frozenset({}))[source]

Create dictionary containing key, value pairs with to be replaced text to translate from conventional Brian to GSL.

translate(code, dtype)[source]

Translates an abstract code block into the target language.

translate_scalar_code(code_lines, variables_in_scalar, variables_in_vector)[source]

Translate scalar code: if calculated variables are used in the vector_code their value is added to the variable in the _dataholder.

translate_vector_code(code_lines, to_replace)[source]

Translate vector code to GSL compatible code by substituting fragments of code.

unpack_namespace(variables_in_vector, variables_in_scalar, ignore=frozenset({}))[source]

Write code that unpacks Brian namespace to cython/cpp namespace.

For vector code this means putting variables in _dataholder (i.e. _GSL_dataholder->var or _GSL_dataholder.var = …) Note that code is written so a variable could occur both in scalar and vector code

unpack_namespace_single(var_obj, in_vector, in_scalar)[source]

Writes the code necessary to pull single variable out of the Brian namespace into the generated code.

The code created is significantly different between cpp and cython, so I decided to not make this function general over all target languages (i.e. in contrast to most other functions that only have syntactical differences)

var_init_lhs(var, type)[source]

Get string version of the left hand side of an initializing expression

write_dataholder(variables_in_vector)[source]

Return string with full code for _dataholder struct.

write_dataholder_single(var_obj)[source]

Return string declaring a single variable in the _dataholder struct.

yvector_code(diff_vars)[source]

Generate code for function dealing with GSLs y vector.

The values of differential variables have to be transferred from Brian’s namespace to a vector that is given to GSL. The transferring from Brian –> y and back from y –> Brian after integration happens in separate functions. The code for these is written here.