CPPStandaloneDevice class

(Shortest import: from brian2.devices.cpp_standalone.device import CPPStandaloneDevice)

class brian2.devices.cpp_standalone.device.CPPStandaloneDevice[source]

Bases: brian2.devices.device.Device

The Device used for C++ standalone simulations.

Attributes

arange_arrays

List of all arrays to be filled with numbers (list of (var, varname, start) tuples

array_cache

Dictionary mapping ArrayVariable objects to their value or to None if the value (potentially) depends on executed code.

arrays

Dictionary mapping ArrayVariable objects to their globally unique name

build_on_run

Whether a run should trigger a build

build_options

build options

code_lines

Code lines that have been manually added with device.insert_code() Dictionary mapping slot names to lists of lines.

dynamic_arrays

List of all dynamic arrays Dictionary mapping DynamicArrayVariable objects with 1 dimension to their globally unique name

dynamic_arrays_2d

Dictionary mapping DynamicArrayVariable objects with 2 dimensions to their globally unique name

enable_profiling

Whether to generate profiling information (stored in an instance variable to be accessible during CodeObject generation)

has_been_run

Whether the simulation has been run

profiled_codeobjects

CodeObjects that use profiling (users can potentially enable profiling only for a subset of runs)

project_dir

The directory which contains the generated code and results

static_arrays

Dict of all static saved arrays

zero_arrays

List of all arrays to be filled with zeros (list of (var, varname) )

Methods

add_array(var)

Add an array to this device.

build([directory, compile, run, debug, …])

Build the project

check_openmp_compatible(nb_threads)

code_object(owner, name, abstract_code, …)

code_object_class([codeobj_class, fallback_pref])

Return CodeObject class (either CPPStandaloneCodeObject class or input)

compile_source(directory, compiler, debug, clean)

copy_source_files(writer, directory)

delete([code, data, directory, force])

Delete code and/or data generated/stored by the device.

fill_with_array(var, arr)

Fill an array with the values given in another array.

freeze(code, ns)

generate_codeobj_source(writer)

generate_main_source(writer)

generate_makefile(writer, compiler, …)

generate_network_source(writer, compiler)

generate_objects_source(writer, …)

generate_run_source(writer)

generate_synapses_classes_source(writer)

get_array_filename(var[, basedir])

Return a file name for a variable.

get_array_name(var[, access_data])

Return a globally unique name for var().

get_value(var[, access_data])

init_with_arange(var, start, dtype)

Initialize an array with an integer range.

init_with_zeros(var, dtype)

Initialize an array with zeros.

insert_code(slot, code)

Insert code directly into main.cpp

network_get_profiling_info(net)

network_restore(net, *args, **kwds)

network_run(net, duration[, report, …])

network_store(net, *args, **kwds)

reinit()

Reinitialize the device.

resize(var, new_size)

Resize a DynamicArrayVariable.

run(directory, with_output, run_args)

run_function(name[, include_in_parent])

Context manager to divert code into a function

seed([seed])

Set the seed for the random number generator.

spike_queue(source_start, source_end)

Create and return a new SpikeQueue for this Device.

static_array(name, arr)

variableview_get_subexpression_with_index_array(…)

variableview_get_with_expression(…[, …])

variableview_set_with_index_array(…)

write_static_arrays(directory)

Details

arange_arrays

List of all arrays to be filled with numbers (list of (var, varname, start) tuples

array_cache

Dictionary mapping ArrayVariable objects to their value or to None if the value (potentially) depends on executed code. This mechanism allows to access state variables in standalone mode if their value is known at run time

arrays

Dictionary mapping ArrayVariable objects to their globally unique name

build_on_run

Whether a run should trigger a build

build_options

build options

code_lines

Code lines that have been manually added with device.insert_code() Dictionary mapping slot names to lists of lines. Note that the main slot is handled separately as part of main_queue

dynamic_arrays

List of all dynamic arrays Dictionary mapping DynamicArrayVariable objects with 1 dimension to their globally unique name

dynamic_arrays_2d

Dictionary mapping DynamicArrayVariable objects with 2 dimensions to their globally unique name

enable_profiling

Whether to generate profiling information (stored in an instance variable to be accessible during CodeObject generation)

has_been_run

Whether the simulation has been run

profiled_codeobjects

CodeObjects that use profiling (users can potentially enable profiling only for a subset of runs)

project_dir

The directory which contains the generated code and results

static_arrays

Dict of all static saved arrays

zero_arrays

List of all arrays to be filled with zeros (list of (var, varname) )

add_array(var)[source]

Add an array to this device.

Parameters

var : ArrayVariable

The array to add.

build(directory='output', compile=True, run=True, debug=False, clean=False, with_output=True, additional_source_files=None, run_args=None, direct_call=True, **kwds)[source]

Build the project

TODO: more details

Parameters

directory : str, optional

The output directory to write the project to, any existing files will be overwritten. If the given directory name is None, then a temporary directory will be used (used in the test suite to avoid problems when running several tests in parallel). Defaults to 'output'.

compile : bool, optional

Whether or not to attempt to compile the project. Defaults to True.

run : bool, optional

Whether or not to attempt to run the built project if it successfully builds. Defaults to True.

debug : bool, optional

Whether to compile in debug mode. Defaults to False.

with_output : bool, optional

Whether or not to show the stdout of the built program when run. Output will be shown in case of compilation or runtime error. Defaults to True.

clean : bool, optional

Whether or not to clean the project before building. Defaults to False.

additional_source_files : list of str, optional

A list of additional .cpp files to include in the build.

direct_call : bool, optional

Whether this function was called directly. Is used internally to distinguish an automatic build due to the build_on_run option from a manual device.build call.

check_openmp_compatible(nb_threads)[source]
code_object(owner, name, abstract_code, variables, template_name, variable_indices, codeobj_class=None, template_kwds=None, override_conditional_write=None, compiler_kwds=None)[source]
code_object_class(codeobj_class=None, fallback_pref=None)[source]

Return CodeObject class (either CPPStandaloneCodeObject class or input)

Parameters

codeobj_class : a CodeObject class, optional

If this is keyword is set to None or no arguments are given, this method will return the default (CPPStandaloneCodeObject class).

fallback_pref : str, optional

For the cpp_standalone device this option is ignored.

Returns

codeobj_class : class

The CodeObject class that should be used

compile_source(directory, compiler, debug, clean)[source]
copy_source_files(writer, directory)[source]
delete(code=True, data=True, directory=True, force=False)[source]

Delete code and/or data generated/stored by the device.

Parameters

data : bool, optional

Whether to delete the data generated by the simulation (final values of state variables, data stored in monitors, etc.). Defaults to True.

code : bool, optional

Whether to delete the code generated by the simulation. Includes the numerical values used for initialization of state variables in assignments not using strings. Defaults to True.

directory : bool, optional

Whether to delete the project directory generated by the simulation. Will not delete directories that contain files not created by Brian unless the force option is specfied. Defaults to True.

force : bool, optional

Whether to delete the project directory with all its content, even if it contains files that were not created by Brian. Useful only when the directory option is set to True as well. Defaults to False.

fill_with_array(var, arr)[source]

Fill an array with the values given in another array.

Parameters

var : ArrayVariable

The array to fill.

arr : ndarray

The array values that should be copied to var().

freeze(code, ns)[source]
generate_codeobj_source(writer)[source]
generate_main_source(writer)[source]
generate_makefile(writer, compiler, compiler_flags, linker_flags, nb_threads, debug)[source]
generate_network_source(writer, compiler)[source]
generate_objects_source(writer, arange_arrays, synapses, static_array_specs, networks)[source]
generate_run_source(writer)[source]
generate_synapses_classes_source(writer)[source]
get_array_filename(var, basedir='results')[source]

Return a file name for a variable.

Parameters

var : ArrayVariable

The variable to get a filename for.

basedir : str

The base directory for the filename, defaults to 'results'.

Returns :

——- :

filename : str

A filename of the form 'results/'+varname+'_'+str(hash(varname)), where varname is the name returned by get_array_name.

Notes

The reason that the filename is not simply 'results/' + varname is that this could lead to file names that are not unique in file systems that are not case sensitive (e.g. on Windows).

get_array_name(var, access_data=True)[source]

Return a globally unique name for var().

Parameters

access_data : bool, optional

For DynamicArrayVariable objects, specifying True here means the name for the underlying data is returned. If specifying False, the name of object itself is returned (e.g. to allow resizing).

get_value(var, access_data=True)[source]
init_with_arange(var, start, dtype)[source]

Initialize an array with an integer range.

Parameters

var : ArrayVariable

The array to fill with the integer range.

start : int

The start value for the integer range

dtype : dtype

The data type to use for the array.

init_with_zeros(var, dtype)[source]

Initialize an array with zeros.

Parameters

var : ArrayVariable

The array to initialize with zeros.

dtype : dtype

The data type to use for the array.

insert_code(slot, code)[source]

Insert code directly into main.cpp

network_get_profiling_info(net)[source]
network_restore(net, *args, **kwds)[source]
network_run(net, duration, report=None, report_period=10.0 * second, namespace=None, profile=False, level=0, **kwds)[source]
network_store(net, *args, **kwds)[source]
reinit()[source]

Reinitialize the device. For standalone devices, clears all the internal state of the device.

resize(var, new_size)[source]

Resize a DynamicArrayVariable.

Parameters

var : DynamicArrayVariable

The variable that should be resized.

new_size : int

The new size of the variable

run(directory, with_output, run_args)[source]
run_function(name, include_in_parent=True)[source]

Context manager to divert code into a function

Code that happens within the scope of this context manager will go into the named function.

Parameters

name : str

The name of the function to divert code into.

include_in_parent : bool

Whether or not to include a call to the newly defined function in the parent context.

seed(seed=None)[source]

Set the seed for the random number generator.

Parameters

seed : int, optional

The seed value for the random number generator, or None (the default) to set a random seed.

spike_queue(source_start, source_end)[source]

Create and return a new SpikeQueue for this Device.

Parameters

source_start : int

The start index of the source group (necessary for subgroups)

source_end : int

The end index of the source group (necessary for subgroups)

static_array(name, arr)[source]
variableview_get_subexpression_with_index_array(variableview, item, run_namespace=None)[source]
variableview_get_with_expression(variableview, code, run_namespace=None)[source]
variableview_set_with_index_array(variableview, item, value, check_units)[source]
write_static_arrays(directory)[source]