Debian Codegraph @

dev/benchmarks/synapse_creation.py

raw
1 '''
2 How long does synapse creation take?
3 '''
4 import time
5 import pickle
6
7 import numpy as np
8 import joblib
9
10 from brian2 import *
11
12 repetitions = 3
13
14 memory = joblib.Memory(cachedir='.', verbose=0)
15
16 @memory.cache
17 def test_connectivity2(N, i, j, n, p, codeobj_class):
18 G = NeuronGroup(N, '')
19 # Do it once without measuring the time to ignore the compilation time for
20 # C code
21 S = Synapses(G, G, '', codeobj_class=codeobj_class)
22 S.connect(i, j, p, n)
23 connections = len(S)
24 del S
25 times = []
26 for _ in range(repetitions):
27 S = Synapses(G, G, '', codeobj_class=codeobj_class)
28 start = time.time()
29 S.connect(i, j, p, n)
30 times.append(time.time() - start)
31 del S
32
33 return float(np.median(times)), connections
34
35 conditions = [('Full', 'True'),
36 ('Full (no-self)', 'i != j'),
37 ('One-to-one', 'i == j'),
38 ('Simple neighbourhood', 'abs(i-j) < 5'),
39 ('Gauss neighbourhood', 'exp(-(i - j)**2/5) > 0.005'),
40 ('Random (50%)', (True, None, 1, 0.5)),
41 ('Random (10%)', (True, None, 1, 0.1)),
42 ('Random (1%)', (True, None, 1, 0.01)),
43 ('Random no-self (50%)', ('(i != j)', None, 1, 0.5)),
44 ('Random no-self (10%)', ('(i != j)', None, 1, 0.1)),
45 ('Random no-self (1%)', ('(i != j)', None, 1, 0.01))]
46 targets = [NumpyCodeObject]
47 results = {}
48 max_connections = 2500000
49 for target in targets:
50 lang_name = target.class_name
51 for pattern, condition in conditions:
52 N = 1
53 connections = took = 0
54 while connections < max_connections and took < 20:
55 print(lang_name, pattern)
56 if isinstance(condition, str):
57 took, connections = test_connectivity2(N, condition, None, 1, 1.,
58 codeobj_class=target)
59 else:
60 took, connections = test_connectivity2(N, *condition,
61 codeobj_class=target)
62 print(N, '%.4fs (for %d connections)' % (took, connections))
63 results[(lang_name, connections, pattern)] = took
64 N *= 2
65
66 with open('synapse_creation_times_brian2.pickle', 'w') as f:
67 pickle.dump(results, f)