Debian Codegraph @

dev/benchmarks/openmp/example_standalone.py

raw
1 #!/usr/bin/env python
2 # coding: latin-1
3 """
4 CUBA example with delays.
5 """
6
7 import sys, time, os
8 from brian2 import *
9
10 standalone = int(sys.argv[-2])
11 n_threads = int(sys.argv[-1])
12 path = 'data_example_%d' %n_threads
13
14 if standalone == 1:
15 set_device('cpp_standalone')
16 brian_prefs.codegen.cpp_standalone.openmp_threads = n_threads
17
18 start = time.time()
19 n_cells = 1000
20
21 numpy.random.seed(42)
22 connectivity = numpy.random.randn(n_cells, n_cells)
23
24 taum = 20 * ms
25 taus = 5 * ms
26 Vt = -50 * mV
27 Vr = -60 * mV
28 El = -49 * mV
29
30 fac = (60 * 0.27 / 10) # excitatory synaptic weight (voltage)
31
32 gmax = 20*fac
33 dApre = .01
34 taupre = 20 * ms
35 taupost = taupre
36 dApost = -dApre * taupre / taupost * 1.05
37 dApost *= 0.1*gmax
38 dApre *= 0.1*gmax
39
40
41 eqs = Equations('''
42 dv/dt = (g-(v-El))/taum : volt
43 g : volt
44 ''')
45
46 P = NeuronGroup(n_cells, model=eqs, threshold='v>Vt', reset='v=Vr', refractory=5 * ms)
47 P.v = Vr + numpy.random.rand(len(P)) * (Vt - Vr)
48 P.g = 0 * mV
49
50
51
52 S = Synapses(P, P,
53 model = '''dApre/dt=-Apre/taupre : 1 (event-driven)
54 dApost/dt=-Apost/taupost : 1 (event-driven)
55 w : 1
56 dg/dt = -g/taus : volt
57 g_post = g : volt (summed)''',
58 pre = '''g += w*mV
59 Apre += dApre
60 w = w + Apost''',
61 post = '''Apost += dApost
62 w = w + Apre''')
63 S.connect()
64 S.w = fac*connectivity.flatten()
65
66
67 spike_mon = SpikeMonitor(P)
68 state_mon = StateMonitor(S, 'w', record=range(10), when=Clock(dt=0.1*second))
69 v_mon = StateMonitor(P, 'v', record=range(10))
70
71 if standalone == 1:
72 device.insert_code('main', 'std::clock_t start = std::clock();')
73
74 run(5 * second, report='text')
75
76 if standalone == 1:
77 device.insert_code('main', '''
78 std::ofstream myfile ("speed.txt");
79 if (myfile.is_open())
80 {
81 double value = (double) (std::clock() - start)/(%d * CLOCKS_PER_SEC);
82 myfile << value << std::endl;
83 myfile.close();
84 }
85 ''' %(max(1, n_threads)))
86
87 try:
88 os.removedirs(path)
89 except Exception:
90 pass
91
92 if standalone == 1:
93 device.build(project_dir='data_example_%d' %n_threads, compile_project=True, run_project=True, debug=False)