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