File: examples-multiprocessing_multiple_runs_simple.txt

package info (click to toggle)
brian 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, stretch
  • size: 23,436 kB
  • sloc: python: 68,707; cpp: 29,040; ansic: 5,182; sh: 111; makefile: 61
file content (62 lines) | stat: -rw-r--r-- 2,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
.. currentmodule:: brian

.. index::
   pair: example usage; plot
   pair: example usage; linspace
   pair: example usage; show

.. _example-multiprocessing_multiple_runs_simple:

Example: multiple_runs_simple (multiprocessing)
===============================================

Example of using Python multiprocessing module to distribute simulations over
multiple processors.

The general procedure for using multiprocessing is to define and run a network
inside a function, and then use multiprocessing.Pool.map to call the function
with multiple parameter values. Note that on Windows, any code that should only
run once should be placed inside an if __name__=='__main__' block.

::

    
    from brian import *
    import multiprocessing
    
    # This is the function that we want to compute for various different parameters
    def how_many_spikes(excitatory_weight):
        # These two lines reset the clock to 0 and clear any remaining data so that
        # memory use doesn't build up over multiple runs.
        reinit_default_clock()
        clear(True)
        eqs = '''
        dv/dt = (ge+gi-(v+49*mV))/(20*ms) : volt
        dge/dt = -ge/(5*ms) : volt
        dgi/dt = -gi/(10*ms) : volt
        '''
        P = NeuronGroup(4000, eqs, threshold= -50 * mV, reset= -60 * mV)
        P.v = -60 * mV + 10 * mV * rand(len(P))
        Pe = P.subgroup(3200)
        Pi = P.subgroup(800)
        Ce = Connection(Pe, P, 'ge')
        Ci = Connection(Pi, P, 'gi')
        Ce.connect_random(Pe, P, 0.02, weight=excitatory_weight)
        Ci.connect_random(Pi, P, 0.02, weight= -9 * mV)
        M = SpikeMonitor(P)
        run(100 * ms)
        return M.nspikes
    
    if __name__ == '__main__':
        # Note that on Windows platforms, all code that is executed rather than
        # just defining functions and classes has to be in the if __name__=='__main__'
        # block, otherwise it will be executed by each process that starts. This
        # isn't a problem on Linux.
        pool = multiprocessing.Pool() # uses num_cpu processes by default
        weights = linspace(0, 3.5, 100) * mV
        args = [w * volt for w in weights]
        results = pool.map(how_many_spikes, args) # launches multiple processes
        plot(weights, results, '.')
        show()