|
| 1 | +import copy |
| 2 | +import profile |
| 3 | +try: |
| 4 | + import unittest2 as unittest |
| 5 | +except ImportError: |
| 6 | + import unittest |
| 7 | + |
| 8 | +import pyNN.mock as sim |
| 9 | +#import pyNN.nest as sim |
| 10 | + |
| 11 | +from pyNN.utility import Timer, init_logging |
| 12 | + |
| 13 | +init_logging("test_speed_mock.log", debug=False) |
| 14 | + |
| 15 | + |
| 16 | +class PopulationTest(unittest.TestCase): |
| 17 | + |
| 18 | + @staticmethod |
| 19 | + def do_scaling_per_population_test(N): |
| 20 | + timer = Timer() |
| 21 | + timer.start() |
| 22 | + sim.setup() |
| 23 | + timer.mark("setup") |
| 24 | + p = sim.Population(N, sim.IF_curr_exp()) |
| 25 | + timer.mark("Population: " + str(N)) |
| 26 | + sim.end() |
| 27 | + timer.mark("end") |
| 28 | + elapsed_time = timer.elapsed_time() |
| 29 | + relative_elapsed_time = elapsed_time / N |
| 30 | + print("Creating a {}-sized population took {}s ({}s per neuron)".format(N, elapsed_time, relative_elapsed_time)) |
| 31 | + |
| 32 | + def test_scaling_per_population(self, sim=sim): |
| 33 | + for pN in range(13): # TODO: log? |
| 34 | + N = 2**pN |
| 35 | + with self.subTest(N=N): |
| 36 | + self.do_scaling_per_population_test(N) |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def do_scaling_test(N): |
| 40 | + timer = Timer() |
| 41 | + timer.start() |
| 42 | + sim.setup() |
| 43 | + timer.mark("setup") |
| 44 | + for _ in range(N): |
| 45 | + p = sim.Population(1, sim.IF_curr_exp()) |
| 46 | + timer.mark("Population: " + str(N)) |
| 47 | + sim.end() |
| 48 | + timer.mark("end") |
| 49 | + elapsed_time = timer.elapsed_time() |
| 50 | + relative_elapsed_time = elapsed_time / N |
| 51 | + print("Creating {} populations took {}s ({}s per population)".format(N, elapsed_time, relative_elapsed_time)) |
| 52 | + |
| 53 | + def test_scaling(self, sim=sim): |
| 54 | + for pN in range(13): # TODO: log? |
| 55 | + N = 2**pN |
| 56 | + with self.subTest(N=N): |
| 57 | + self.do_scaling_test(N) |
| 58 | + |
| 59 | + @staticmethod |
| 60 | + def _add_dummy_parameters(celltype, M): |
| 61 | + for i in range(M): |
| 62 | + pname = 'tmp{}'.format(i) |
| 63 | + celltype.default_parameters[pname] = 0.0 |
| 64 | + celltype.units[pname] = 'mV' |
| 65 | + celltype.translations[pname] = { |
| 66 | + 'translated_name': pname.upper(), |
| 67 | + 'forward_transform': pname, |
| 68 | + 'reverse_transform': pname.upper()} |
| 69 | + |
| 70 | + @staticmethod |
| 71 | + def _remove_dummy_parameters(celltype, M): |
| 72 | + for i in range(M): |
| 73 | + pname = 'tmp{}'.format(i) |
| 74 | + del celltype.default_parameters[pname] |
| 75 | + del celltype.units[pname] |
| 76 | + del celltype.translations[pname] |
| 77 | + |
| 78 | + @staticmethod |
| 79 | + def do_scaling_cellparams_test(N, M): |
| 80 | + celltype = copy.deepcopy(sim.IF_cond_exp) |
| 81 | + assert len(celltype.get_parameter_names()) < 100 |
| 82 | + PopulationTest._add_dummy_parameters(celltype, M) |
| 83 | + sim.setup() |
| 84 | + t0 = Timer() |
| 85 | + t0.start() |
| 86 | + pop = sim.Population(N, celltype()) # this is the culprit |
| 87 | + print("Creating a population with {} parameters took {}".format(len(celltype.get_parameter_names()), t0.elapsed_time())) |
| 88 | + sim.end() |
| 89 | + PopulationTest._remove_dummy_parameters(celltype, M) |
| 90 | + |
| 91 | + def test_scaling_cellparams(self, sim=sim): |
| 92 | + for pN in range(16): |
| 93 | + N = 2**pN |
| 94 | + with self.subTest(N=N): |
| 95 | + self.do_scaling_cellparams_test(1, N) |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def do_scaling_cellparams_popview_test(N, M): |
| 99 | + celltype = copy.deepcopy(sim.IF_cond_exp) |
| 100 | + assert len(celltype.get_parameter_names()) < 100 |
| 101 | + PopulationTest._add_dummy_parameters(celltype, M) |
| 102 | + sim.setup() |
| 103 | + pop = sim.Population(N, celltype()) |
| 104 | + pview = sim.PopulationView(pop, [0]) |
| 105 | + post_cell = sim.simulator.ID(pview.first_id) |
| 106 | + post_cell.parent = pop # this is the culprit |
| 107 | + t0 = Timer() |
| 108 | + t0.start() |
| 109 | + post_index = pview.id_to_index(post_cell) |
| 110 | + print("Calling id_to_index on a view into a population with {} parameters took {}".format(len(celltype.get_parameter_names()), t0.elapsed_time())) |
| 111 | + sim.end() |
| 112 | + PopulationTest._remove_dummy_parameters(celltype, M) |
| 113 | + |
| 114 | + def test_scaling_cellparams_popview(self, sim=sim): |
| 115 | + for pN in range(8): |
| 116 | + N = 2**pN |
| 117 | + with self.subTest(N=N): |
| 118 | + self.do_scaling_cellparams_popview_test(1, N) |
| 119 | + |
| 120 | + |
| 121 | +class ProjectionTest(unittest.TestCase): |
| 122 | + |
| 123 | + @staticmethod |
| 124 | + def do_scaling_per_projection_test(N): |
| 125 | + sim.setup() |
| 126 | + pre = sim.Population(N, sim.IF_cond_exp()) |
| 127 | + post = sim.Population(N, sim.IF_cond_exp()) |
| 128 | + |
| 129 | + timer = Timer() |
| 130 | + timer.start() |
| 131 | + proj = sim.Projection(pre, post, sim.OneToOneConnector(), synapse_type=sim.StaticSynapse(weight=1.)) |
| 132 | + timer.mark("Projection: " + str(N)) |
| 133 | + elapsed_time = timer.elapsed_time() |
| 134 | + relative_elapsed_time = elapsed_time / N |
| 135 | + |
| 136 | + sim.end() |
| 137 | + print("Creating {}-sized projection took {}s ({}s per synapse)".format(N, elapsed_time, relative_elapsed_time)) |
| 138 | + |
| 139 | + |
| 140 | + def test_scaling_per_projection(self, sim=sim): |
| 141 | + for pN in range(13): # TODO: log? |
| 142 | + N = 2**pN |
| 143 | + with self.subTest(N=N): |
| 144 | + self.do_scaling_per_projection_test(N) |
| 145 | + |
| 146 | + |
| 147 | + @staticmethod |
| 148 | + def do_scaling_test(N): |
| 149 | + sim.setup() |
| 150 | + pre = sim.Population(N, sim.IF_cond_exp()) |
| 151 | + post = sim.Population(N, sim.IF_cond_exp()) |
| 152 | + |
| 153 | + timer = Timer() |
| 154 | + timer.start() |
| 155 | + for i in range(N): |
| 156 | + |
| 157 | + # FIXME: add check for class-vs-instance in connector, it fails if the connector is a class |
| 158 | + proj = sim.Projection(pre[i:i+1], post[i:i+1], sim.OneToOneConnector(), synapse_type=sim.StaticSynapse(weight=1.)) |
| 159 | + timer.mark("Projection: " + str(N)) |
| 160 | + elapsed_time = timer.elapsed_time() |
| 161 | + relative_elapsed_time = elapsed_time / N |
| 162 | + |
| 163 | + sim.end() |
| 164 | + |
| 165 | + print("Creating {} projections took {}s ({}s per projection)".format(N, elapsed_time, relative_elapsed_time)) |
| 166 | + |
| 167 | + |
| 168 | + def test_scaling(self, sim=sim): |
| 169 | + for pN in range(13): # TODO: log? |
| 170 | + N = 2**pN |
| 171 | + with self.subTest(N=N): |
| 172 | + self.do_scaling_test(N) |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + unittest.main() |
| 177 | + |
| 178 | + #profile.run('print(PopulationTest.do_scaling_test(1000))') |
| 179 | + #profile.run('print(SchmittiTest.do_the_test(64))') |
0 commit comments