| 1 |
Standalone implementation |
| 2 |
========================= |
| 3 |
|
| 4 |
.. contents:: |
| 5 |
:local: |
| 6 |
:depth: 1 |
| 7 |
|
| 8 |
This – currently very incomplete – document describes some of the implementation details of :ref:`cpp_standalone`. |
| 9 |
|
| 10 |
.. _array_cache: |
| 11 |
|
| 12 |
Array cache |
| 13 |
----------- |
| 14 |
|
| 15 |
As described in :ref:`standalone variables <standalone_variables>`, in standalone mode Python code does not |
| 16 |
usually have access to state variables and synaptic indices, since the code necessary to initialize/create them has |
| 17 |
not been run yet. Concretely, accessing a state variable (or other variables like synaptic indices), will call |
| 18 |
`.ArrayVariable.get_value` which delegates to `.CPPStandaloneDevice.get_value`. After a run, this will read the |
| 19 |
corresponding file from the disk and return the values. The user can therefore use the same code to analyze the |
| 20 |
results as for runtime mode. Before a run, this file does not exist, but `.CPPStandaloneDevice.get_value` has another |
| 21 |
mechanism to return values: the "array cache". This cache is a simple dictionary, stored in |
| 22 |
`.CPPStandaloneDevice.array_cache`, mapping `.ArrayVariable` objects to their respective values. If the requested |
| 23 |
object is present in this cache, its values can be accessed even before the simulation is run. Values are added |
| 24 |
to this cache, whenever simulation code sets variables with concrete values. Methods such as |
| 25 |
`.CPPStandaloneDevice.fill_with_array` or `.CPPStandaloneDevice.init_with_zeros` write the provided values |
| 26 |
into the array cache so that they can be retrieved later. Conversely, `.CPPStandaloneDevice.code_object` will delete |
| 27 |
any existing information in ``array_cache`` for variables that are changed by a code object, i.e. invalidate any |
| 28 |
previously stored values:: |
| 29 |
|
| 30 |
>>> set_device('cpp_standalone') |
| 31 |
>>> G = NeuronGroup(10, 'v : volt') |
| 32 |
>>> v_var = G.variables['v'] |
| 33 |
>>> print(device.array_cache[v_var]) # CPPStandaloneDevice.init_with_zeros stored initial zero values |
| 34 |
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] |
| 35 |
>>> G.v = -70*mV |
| 36 |
>>> print(device.array_cache[v_var]) # CPPStandaloneDevice.fill_with_array updated the values |
| 37 |
[-0.07 -0.07 -0.07 -0.07 -0.07 -0.07 -0.07 -0.07 -0.07 -0.07] |
| 38 |
>>> G.v = '-70*mV + i*2*mV' |
| 39 |
>>> print(device.array_cache[v_var]) # Array cache for v has been invalidated |
| 40 |
None |
| 41 |
>>> set_device('runtime') # Reset device to avoid problems in other doctests |
| 42 |
|
| 43 |
Command line arguments |
| 44 |
---------------------- |
| 45 |
|
| 46 |
The mechanisms described in :ref:`standalone_multiple_full_runs` are implemented via command-line arguments |
| 47 |
to the ``main`` binary. A call such as:: |
| 48 |
|
| 49 |
device.run(results_directory='results_1', run_args={group.tau : 10*ms}) |
| 50 |
|
| 51 |
will be executed by calling the compiled binary as follows: |
| 52 |
|
| 53 |
.. code-block:: shell |
| 54 |
|
| 55 |
./main --results_dir /full/path/to/results_1 neurongroup.tau=0.01 |
| 56 |
|
| 57 |
where ``neurongroup`` is ``group.name`` (either a default name, e.g. ``neurongroup``, or the name |
| 58 |
set during construction with the ``name`` argument). The generated code applies the initialization |
| 59 |
for ``tau`` after the usual variable initializations, before the call of ``network.run`` |
| 60 |
(assuming that application has not been moved to a different place by using |
| 61 |
`~brian2.devices.cpp_standalone.device.CPPStandaloneDevice.apply_run_args`). |
| 62 |
|
| 63 |
For initializations with a vector of values, the values are written to disk (in the same, simple |
| 64 |
binary format that is used elsewhere, e.g. for the results). The command line argument then |
| 65 |
specifies the file name instead of a value: |
| 66 |
|
| 67 |
.. code-block:: shell |
| 68 |
|
| 69 |
./main neurongroup.tau=static_arrays/init_neurongroup_v_aca4cd6a3f7e526a61bb5a07468b377e.dat |
| 70 |
|
| 71 |
The hex string in the filename is an automatically generated MD5 hash of the array content. This |
| 72 |
makes it possible to assure that each array is only written to disk once, even for repeated and |
| 73 |
parallel executions with the same values (file locking is used to make sure only one process |
| 74 |
writes to each file). |