Debian Codegraph @

examples/multiprocessing/02_using_standalone.py

raw
1 """
2 Parallel processes using standalone mode
3
4 This example use multiprocessing to run several simulations in parallel.
5 The code is using the C++ standalone mode to compile and execute the code.
6
7 The generated code is stored in a ``standalone{pid}`` directory, with ``pid``
8 being the id of each process.
9
10 Note that the `set_device` call should be in the ``run_sim`` function.
11
12 By moving the `set_device` line into the parallelised function, it creates one
13 C++ standalone device per process.
14 The ``device.reinit()`` needs to be called` if you are running multiple
15 simulations per process (there are 10 tau values and num_proc = 4).
16
17 Each simulation uses it's own code folder to generate the code for the
18 simulation, controlled by the directory keyword to the set_device
19 call. By setting ``directory=None``, a temporary folder with random name is
20 created. This way, each simulation uses a different folder for code generation
21 and there is nothing shared between the parallel processes.
22
23 If you don't set the directory argument, it defaults to ``directory="output"``.
24 In that case each process would use the same files to try to generate and
25 compile your simulation, which would lead to compile/execution errors.
26
27 Setting ``directory=f"standalone{pid}"`` is even better than using
28 ``directory=None`` in this case. That is, giving each parallel process
29 *it's own directory to work on*. This way you avoid the problem of multiple
30 processes working on the same code directories. But you also don't need to
31 recompile the entire project at each simulation. What happens is that in the
32 generated code in two consecutive simulations in a single process will only
33 differ slightly (in this case only the tau parameter). The compiler will
34 therefore only recompile the file that has changed and not the entire project.
35
36 The ``numb_proc`` sets the number of processes. ``run_sim`` is just a toy
37 example that creates a single neuron and connects a `StateMonitor` to record
38 the voltage.
39
40 For more details see the `discussion in the Brian forum <https://brian.discourse.group/t/multiprocessing-in-standalone-mode/142/2>`_.
41
42 Note that Python's `multiprocessing` module cannot deal with user-defined functions (including `TimedArray`) and other
43 complex code structures. If you run into `PicklingError` or `AttributeError` exceptions, you might
44 have to use the `pathos` (https://pypi.org/project/pathos) package instead, which can handle more complex
45 code structures.
46 """
47 import os
48 import multiprocessing
49 from time import time as wall_time
50 from os import system
51 from brian2 import *
52
53 def run_sim(tau):
54 pid = os.getpid()
55 directory = f"standalone{pid}"
56 set_device('cpp_standalone', directory=directory)
57 print(f'RUNNING {pid}')
58
59 G = NeuronGroup(1, 'dv/dt = -v/tau : 1', method='euler')
60 G.v = 1
61
62 mon = StateMonitor(G, 'v', record=0)
63 net = Network()
64 net.add(G, mon)
65 net.run(100 * ms)
66 res = (mon.t/ms, mon.v[0])
67
68 device.reinit()
69
70 print(f'FINISHED {pid}')
71 return res
72
73
74 if __name__ == "__main__":
75 start_time = wall_time()
76
77 num_proc = 4
78 tau_values = np.arange(10)*ms + 5*ms
79 with multiprocessing.Pool(num_proc) as p:
80 results = p.map(run_sim, tau_values)
81
82 print(f"Done in {wall_time() - start_time:10.3f}")
83
84 for tau_value, (t, v) in zip(tau_values, results):
85 plt.plot(t, v, label=str(tau_value))
86 plt.legend()
87 plt.show()