Debian Codegraph @

docs_sphinx/developer/devices.rst

raw
1 Devices
2 =======
3
4 This document describes how to implement a new `Device` for Brian. This is a
5 somewhat complicated process, and you should first be familiar with devices
6 from the user point of view (:doc:`/user/computation`) as well as the code
7 generation system (:doc:`codegen`).
8
9 We wrote Brian's devices system to allow for two major use cases, although
10 it can potentially be extended beyond this. The two use cases are:
11
12 1. Runtime mode. In this mode, everything is managed by Python, including
13 memory management (using numpy by default) and running the simulation.
14 Actual computational work can be carried out in several different ways,
15 including numpy or Cython.
16 2. Standalone mode. In this mode, running a Brian script leads to generating
17 an entire source code project tree which can be compiled and run
18 independently of Brian or Python.
19
20 Runtime mode is handled by `RuntimeDevice` and is already implemented, so here
21 I will mainly discuss standalone devices. A good way to understand these
22 devices is to look at the implementation of `CPPStandaloneDevice` (the only
23 one implemented in the core of Brian). In many cases, the simplest way to
24 implement a new standalone device would be to derive a class from
25 `CPPStandaloneDevice` and overwrite just a few methods.
26
27 Memory management
28 -----------------
29
30 Memory is managed primarily via the `Device.add_array`, `Device.get_value` and
31 `Device.set_value` methods. When a new array is created, the `~Device.add_array`
32 method is called, and when trying to access this memory the other two are
33 called. The `RuntimeDevice` uses numpy to manage the memory and returns the
34 underlying arrays in these methods. The `CPPStandaloneDevice` just stores
35 a dictionary of array names but doesn't allocate any memory. This information
36 is later used to generate code that will allocate the memory, etc.
37
38 Code objects
39 ------------
40
41 As in the case of runtime code generation, computational work is done by
42 a collection of `CodeObject` s. In `CPPStandaloneDevice`, each code object
43 is converted into a pair of ``.cpp`` and ``.h`` files, and this is probably
44 a fairly typical way to do it.
45
46 Building
47 --------
48
49 The method `Device.build` is used to generate the project. This can be
50 implemented any way you like, although looking at `CPPStandaloneDevice.build`
51 is probably a good way to get an idea of how to do it.
52
53 Device override methods
54 -----------------------
55
56 Several functions and methods in Brian are decorated with the `device_override`
57 decorator. This mechanism allows a standalone device to override the behaviour
58 of any of these functions by implementing a method with the name provided to
59 `device_override`. For example, the `CPPStandaloneDevice` uses this to
60 override `Network.run` as `CPPStandaloneDevice.network_run`.
61
62 Other methods
63 -------------
64
65 There are some other methods to implement, including initialising arrays,
66 creating spike queues for synaptic propagation. Take a look at the source code
67 for these.