Debian Codegraph @

dev/benchmarks/openmp/CUBA_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_cuba_%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 = 20000
20 n_exc = int(0.8*n_cells)
21 p_conn = 0.1
22 taum = 20 * ms
23 taue = 5 * ms
24 taui = 10 * ms
25 Vt = -50 * mV
26 Vr = -60 * mV
27 El = -49 * mV
28
29 eqs = Equations('''
30 dv/dt = (ge+gi-(v-El))/taum : volt
31 dge/dt = -ge/taue : volt
32 dgi/dt = -gi/taui : volt
33 ''')
34
35 P = NeuronGroup(n_cells, model=eqs, threshold='v>Vt', reset='v=Vr', refractory=5 * ms)
36 P.v = Vr + rand(len(P)) * (Vt - Vr)
37 P.ge = 0 * mV
38 P.gi = 0 * mV
39
40 Pe = P[0:n_exc]
41 Pi = P[n_exc:]
42
43 we = (60 * 0.27 / 10) # excitatory synaptic weight (voltage)
44 wi = (-20 * 4.5 / 10) # inhibitory synaptic weight
45
46 Se = Synapses(Pe, P, model = 'w : 1', pre = 'ge += w*mV')
47 Se.connect('i != j', p=p_conn)
48 Se.w = '%g' %(we)
49 Se.delay ='rand()*ms'
50
51 Si = Synapses(Pi, P, model = 'w : 1', pre = 'gi += w*mV')
52 Si.connect('i != j', p=p_conn)
53 Si.w = '%g' %(wi)
54 Si.delay ='rand()*ms'
55
56 spike_mon = SpikeMonitor(P)
57
58 net = Network(P, Se, Si, spike_mon, name='stdp_net')
59
60 if standalone == 1:
61 device.insert_code('main', 'std::clock_t start = std::clock();')
62
63 net.run(10 * second, report='text')
64
65 if standalone == 1:
66 device.insert_code('main', '''
67 std::ofstream myfile ("speed.txt");
68 if (myfile.is_open())
69 {
70 double value = (double) (std::clock() - start)/(%d * CLOCKS_PER_SEC);
71 myfile << value << std::endl;
72 myfile.close();
73 }
74 ''' %(max(1, n_threads)))
75
76 try:
77 os.removedirs(path)
78 except Exception:
79 pass
80
81 if standalone == 1:
82 device.build(project_dir=path, compile_project=True, run_project=True, debug=False)