| 1 |
How Brian works |
| 2 |
=============== |
| 3 |
|
| 4 |
In this section we will briefly cover some of the internals of how Brian |
| 5 |
works. This is included here to understand the general process that Brian |
| 6 |
goes through in running a simulation, but it will not be sufficient to |
| 7 |
understand the source code of Brian itself or to extend it to do new things. |
| 8 |
For a more detailed view of this, see the documentation in the |
| 9 |
:doc:`../developer/index`. |
| 10 |
|
| 11 |
Clock-driven versus event-driven |
| 12 |
-------------------------------- |
| 13 |
|
| 14 |
Brian is a clock-driven simulator. This means that the simulation time is |
| 15 |
broken into an equally spaced time grid, 0, dt, 2*dt, 3*dt, .... At each |
| 16 |
time step t, the differential equations specifying the models are first |
| 17 |
integrated giving the values at time t+dt. Spikes are generated when a |
| 18 |
condition such as ``v>vt`` is satisfied, and spikes can only occur on the |
| 19 |
time grid. |
| 20 |
|
| 21 |
The advantage of clock driven simulation is that it is very |
| 22 |
flexible (arbitrary differential equations can be used) and |
| 23 |
computationally efficient. However, the time grid approximation can lead |
| 24 |
to an overestimate of the amount of synchrony that is present in a network. |
| 25 |
This is usually not a problem, and can be managed by reducing the time |
| 26 |
step dt, but it can be an issue for some models. |
| 27 |
|
| 28 |
Note that the |
| 29 |
inaccuracy introduced by the spike time approximation is of order |
| 30 |
O(dt), so the total accuracy of the simulation is of order O(dt) per |
| 31 |
time step. This means that in many cases, there is no need to use a |
| 32 |
higher order numerical integration method than forward Euler, as it |
| 33 |
will not improve the order of the error beyond O(dt). See |
| 34 |
:doc:`state_update` for more details of numerical integration methods. |
| 35 |
|
| 36 |
Some simulators use an event-driven method. With this method, spikes can |
| 37 |
occur at arbitrary times instead of just on the grid. This method can be |
| 38 |
more accurate than a clock-driven simulation, but it is usually |
| 39 |
substantially more computationally expensive (especially for larger |
| 40 |
networks). In addition, they are usually more restrictive in terms of |
| 41 |
the class of differential equations that can be solved. |
| 42 |
|
| 43 |
For a review of some of the simulation strategies that have been |
| 44 |
used, see |
| 45 |
`Brette et al. 2007 <http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2638500/>`_. |
| 46 |
|
| 47 |
Code overview |
| 48 |
------------- |
| 49 |
|
| 50 |
The user-visible part of Brian consists of a number of objects such as |
| 51 |
`NeuronGroup`, `Synapses`, `Network`, etc. These are all written in pure |
| 52 |
Python and essentially work to translate the user specified model into the |
| 53 |
computational engine. The end state of this translation is a collection of |
| 54 |
short blocks of code operating on a namespace, which are called |
| 55 |
in a sequence by the `Network`. Examples of these short blocks of code are the |
| 56 |
"state updaters" which perform numerical integration, or the synaptic |
| 57 |
propagation step. The namespaces consist of a mapping from names to values, |
| 58 |
where the possible values can be scalar values, fixed-length or dynamically |
| 59 |
sized arrays, and functions. |
| 60 |
|
| 61 |
Syntax layer |
| 62 |
------------ |
| 63 |
|
| 64 |
The syntax layer consists of everything that is independent of the way the |
| 65 |
final simulation is computed (i.e. the language and device it is running on). |
| 66 |
This includes things like `NeuronGroup`, `Synapses`, `Network`, `Equations`, |
| 67 |
etc. |
| 68 |
|
| 69 |
The user-visible part of this is documented fully in the :doc:`../user/index` |
| 70 |
and the :doc:`../advanced/index`. In particular, things such as the analysis |
| 71 |
of equations and assignment of numerical integrators. The end result of this |
| 72 |
process, which is passed to the computational engine, is a specification of |
| 73 |
the simulation consisting of the following data: |
| 74 |
|
| 75 |
* A collection of variables which are scalar values, fixed-length arrays, |
| 76 |
dynamically sized arrays, and functions. These are handled by `Variable` |
| 77 |
objects detailed in :doc:`../developer/variables_indices`. Examples: |
| 78 |
each state variable of a `NeuronGroup` is assigned an `ArrayVariable`; |
| 79 |
the list of spike indices stored by a `SpikeMonitor` is assigned a |
| 80 |
`DynamicArrayVariable`; etc. |
| 81 |
* A collection of code blocks specified via an "abstract code block" and a |
| 82 |
template name. The "abstract code block" is a sequence of statements such |
| 83 |
as ``v = vr`` which are to be executed. In the case that say, ``v`` and |
| 84 |
``vr`` are arrays, then the statement is to be executed for each element of |
| 85 |
the array. These abstract code blocks are either given directly by the user |
| 86 |
(in the case of neuron threshold and reset, and synaptic pre and post codes), |
| 87 |
or generated from differential equations combined with a numerical |
| 88 |
integrator. The template name is one of a small set (around 20 total) which |
| 89 |
give additional context. For example, the code block ``a = b`` when |
| 90 |
considered as part of a "state update" means execute that for each neuron |
| 91 |
index. In the context of a reset statement, it means execute it for each |
| 92 |
neuron index of a neuron that has spiked. Internally, these templates need |
| 93 |
to be implemented for each target language/device, but there are relatively |
| 94 |
few of them. |
| 95 |
* The order of execution of these code blocks, as defined by the `Network`. |
| 96 |
|
| 97 |
Computational engine |
| 98 |
-------------------- |
| 99 |
|
| 100 |
The computational engine covers everything from generating to running code in |
| 101 |
a particular language or on a particular device. It starts with the |
| 102 |
abstract definition of the simulation resulting from the syntax layer |
| 103 |
described above. |
| 104 |
|
| 105 |
The computational engine is described by a `Device` object. This is used for |
| 106 |
allocating memory, generating and running code. There are two types of device, |
| 107 |
"runtime" and "standalone". In runtime mode, everything is managed by Python, |
| 108 |
even if individual code blocks are in a different language. Memory is managed |
| 109 |
using numpy arrays (which can be passed as pointers to use in other |
| 110 |
languages). In standalone mode, the output of the process (after calling |
| 111 |
`Device.build`) is a complete source code project that handles everything, |
| 112 |
including memory management, and is independent of Python. |
| 113 |
|
| 114 |
For both types of device, one of the key steps that works in the same way is |
| 115 |
code generation, the creation of a compilable and runnable block of code from an |
| 116 |
abstract code block and a collection of variables. This happens in two stages: |
| 117 |
first of all, the abstract code block is converted into a code snippet, |
| 118 |
which is a syntactically correct block of code in the target language, but |
| 119 |
not one that can run on its own (it doesn't handle accessing the variables |
| 120 |
from memory, etc.). This code snippet typically represents the inner loop code. |
| 121 |
This step is handled by a `CodeGenerator` object. In some |
| 122 |
cases it will involve a syntax translation (e.g. the Python syntax ``x**y`` in |
| 123 |
C++ should be ``pow(x, y)``). The |
| 124 |
next step is to insert this code snippet into a template to form a compilable |
| 125 |
code block. This code block is then passed to a runtime `CodeObject`. In the |
| 126 |
case of standalone mode, this doesn't do anything, but for runtime devices |
| 127 |
it handles compiling the code and then running the compiled code block in the |
| 128 |
given namespace. |