File: test_chunk_balancer.py

package info (click to toggle)
meep-openmpi 1.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 64,556 kB
  • sloc: cpp: 32,214; python: 27,958; lisp: 1,225; makefile: 505; sh: 249; ansic: 131; javascript: 5
file content (338 lines) | stat: -rw-r--r-- 11,677 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import unittest

import meep.binary_partition_utils as bpu
import numpy as np
import parameterized
from meep.chunk_balancer import ChunkBalancer
from meep.timing_measurements import TIMING_MEASUREMENT_IDS, MeepTimingMeasurements

import meep as mp


class MockSimulation(mp.Simulation):
    """Class which emulates the multi-core MPI behavior while on a single core.

    This inherits all methods from mp.Simulation but overrides two behaviors:
      1. sim.time_spent_on() will provide fake timing data where all entries are
         [0.0 ... 0.0] except for time_stepping, which will have an array of
         values where the elapsed time for each process is equal to the pixel
         volume of that process's chunk.
      2. sim.structure.get_chunk_owners will return a list of process IDs which
         would be the values that you would see if running the simulation in MPI
         mode. (Otherwise in a single-core test environment, an array of zeros
         would be returned.)
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.relative_loads = None

    def _structure_get_chunk_owners(self):
        # Hacky workaround to make this test work on single-core systems
        proc_ids = [
            leaf.proc_id for leaf in bpu.enumerate_leaf_nodes(self.chunk_layout)
        ]

        return np.array(proc_ids)

    def init_sim(self):
        super().init_sim()
        setattr(self.structure, "get_chunk_owners", self._structure_get_chunk_owners)

    def time_spent_on(self, time_sink: int):
        # We're going to pretend the amount of time spent is ~volume so that
        # chunks should converge to an equal volume
        num_processes = self.chunk_layout.numchunks()
        chunk_volumes = self.structure.get_chunk_volumes()
        chunk_owners = self.structure.get_chunk_owners()

        if chunk_volumes[0].nz() > 0:  # 3D case
            volumes = [v.nx() * v.ny() * v.nz() for v in chunk_volumes]
        else:  # 2D case
            volumes = [v.nx() * v.ny() for v in chunk_volumes]

        total_volume_by_proc = np.zeros(num_processes)
        for i, v in enumerate(volumes):
            total_volume_by_proc[chunk_owners[i]] += v

        if time_sink == TIMING_MEASUREMENT_IDS["time_stepping"]:
            times = 1.0 * total_volume_by_proc
        else:
            times = 0.0 * np.ones(num_processes)

        if self.relative_loads is not None:
            times = times * self.relative_loads

        return times


# The test sim instances are lambda functions () => MockSimulation because
# the chunk balancer tests will mutate the chunk_layout attribute.

TEST_SIM_1 = lambda: MockSimulation(
    cell_size=mp.Vector3(10.0, 5.0, 0),
    resolution=20,
    chunk_layout=mp.BinaryPartition(
        data=[(mp.X, -2.0), 0, [(mp.Y, 1.5), [(mp.X, 4.0), 1, [(mp.Y, 0.5), 4, 3]], 2]]
    ),
)
TEST_SIM_2 = lambda: MockSimulation(
    cell_size=mp.Vector3(10.0, 5.0, 3.0),
    resolution=10,
    chunk_layout=mp.BinaryPartition(
        data=[(mp.X, -2.0), 0, [(mp.Y, 1.0), [(mp.X, 3.0), 1, [(mp.Y, 0.5), 4, 3]], 2]]
    ),
)
TEST_SIM_3 = lambda: MockSimulation(
    cell_size=mp.Vector3(6.0, 4.0, 0),
    resolution=10,
    chunk_layout=mp.BinaryPartition(data=[(mp.X, -2.0), 0, [(mp.X, 2.0), 1, 2]]),
)
TEST_SIM_4 = lambda: MockSimulation(
    cell_size=mp.Vector3(6.0, 4.0, 0),
    resolution=10,
    chunk_layout=mp.BinaryPartition(
        data=[(mp.X, -2.0), 0, [(mp.X, -0.5), 1, [(mp.X, 1.0), 2, [(mp.X, 2.0), 3, 4]]]]
    ),
)

TEST_SIM_DUPLICATE_PROC_ID = lambda: MockSimulation(
    cell_size=mp.Vector3(10.0, 5.0, 0),
    resolution=10,
    chunk_layout=mp.BinaryPartition(
        data=[(mp.X, -2.0), 0, [(mp.Y, 1.5), [(mp.X, 4.0), 1, [(mp.Y, 0.5), 2, 1]], 2]]
    ),
)

TEST_CHUNK_DATA_1 = {
    "chunk_layout": mp.BinaryPartition(data=[(mp.X, -2.5), 0, [(mp.X, 2.5), 1, 2]]),
    "cell_size": mp.Vector3(6.0, 4.0, 0),
    "time_stepping": [1.0, 1.0, 1.0],
    "new_chunk_layout": mp.BinaryPartition(data=[(mp.X, -2.5), 0, [(mp.X, 2.5), 1, 2]]),
}
TEST_CHUNK_DATA_2 = {
    "chunk_layout": mp.BinaryPartition(data=[(mp.X, -2.5), 0, [(mp.X, 2.5), 1, 2]]),
    "cell_size": mp.Vector3(6.0, 4.0, 0),
    "time_stepping": [3.0 - 2.5, 2.5 + 2.5, 3.0 - 2.5],
    "new_chunk_layout": mp.BinaryPartition(data=[(mp.X, -1.0), 0, [(mp.X, 1.0), 1, 2]]),
}
TEST_CHUNK_DATA_3 = {
    "chunk_layout": mp.BinaryPartition(data=[(mp.X, 2.0), 0, 1]),
    "cell_size": mp.Vector3(6.0, 4.0, 0),
    "time_stepping": [1.0, 1.0],
    "new_chunk_layout": mp.BinaryPartition(data=[(mp.X, 2.0), 0, 1]),
}
TEST_CHUNK_DATA_4 = {
    "chunk_layout": mp.BinaryPartition(data=[(mp.X, 2.0), 0, 1]),
    "cell_size": mp.Vector3(6.0, 4.0, 0),
    "time_stepping": [5.0, 1.0],
    "new_chunk_layout": mp.BinaryPartition(data=[(mp.X, 0.0), 0, 1]),
}
TEST_CHUNK_DATA_5 = {
    "chunk_layout": mp.BinaryPartition(
        data=[(mp.X, -2.0), 0, [(mp.Y, 1.5), [(mp.X, 4.0), 1, [(mp.Y, 0.5), 4, 3]], 2]]
    ),
    "cell_size": mp.Vector3(10.0, 5.0, 0),
    "time_stepping": [1.0, 1.0, 1.0, 1.0, 1.0],
    "new_chunk_layout": mp.BinaryPartition(
        data=[(mp.X, -2.0), 0, [(mp.Y, 1.5), [(mp.X, 4.0), 1, [(mp.Y, 0.5), 4, 3]], 2]]
    ),
}
TEST_CHUNK_DATA_6 = {
    "chunk_layout": mp.BinaryPartition(
        data=[(mp.X, -2.0), 0, [(mp.Y, 1.5), [(mp.X, 4.0), 1, [(mp.Y, 0.5), 4, 3]], 2]]
    ),
    "cell_size": mp.Vector3(10.0, 5.0, 0),
    "time_stepping": [1500.0, 2400.0, 700.0, 100.0, 300.0],
    "new_chunk_layout": mp.BinaryPartition(
        data=[
            (mp.X, -3.0),
            0,
            [(mp.Y, 1.25), [(mp.X, -0.3333333333333335), 1, [(mp.Y, -0.625), 4, 3]], 2],
        ]
    ),
}


class MockSimulationTest(unittest.TestCase):
    @parameterized.parameterized.expand(
        [
            (TEST_SIM_1, [6000.0, 9600.0, 2800.0, 400.0, 1200.0]),
            (TEST_SIM_2, [45000.0, 52500.0, 31500.0, 3000.0, 18000.0]),
            (TEST_SIM_3, [400.0, 1600.0, 400.0]),
            (TEST_SIM_4, [400.0, 600.0, 600.0, 400.0, 400.0]),
        ]
    )
    def test_time_spent_on(self, test_sim_constructor, expected_stepping_times):
        test_sim = test_sim_constructor()
        test_sim.init_sim()

        for time_sink in TIMING_MEASUREMENT_IDS.values():
            if time_sink == TIMING_MEASUREMENT_IDS["time_stepping"]:
                self.assertListEqual(
                    list(test_sim.time_spent_on(time_sink)), expected_stepping_times
                )
            else:
                self.assertListEqual(
                    list(test_sim.time_spent_on(time_sink)),
                    [0.0] * len(expected_stepping_times),
                )

    @parameterized.parameterized.expand(
        [
            (TEST_SIM_1, [0, 1, 4, 3, 2]),
            (TEST_SIM_2, [0, 1, 4, 3, 2]),
            (TEST_SIM_3, [0, 1, 2]),
            (TEST_SIM_4, [0, 1, 2, 3, 4]),
        ]
    )
    def test_structure_get_chunk_owners(
        self, test_sim_constructor, expected_chunk_owners
    ):
        test_sim = test_sim_constructor()
        test_sim.init_sim()

        chunk_owners = test_sim.structure.get_chunk_owners()

        self.assertListEqual(list(chunk_owners), expected_chunk_owners)


class ChunkBalancerTest(unittest.TestCase):
    @parameterized.parameterized.expand(
        [
            (TEST_SIM_1, False),
            (TEST_SIM_DUPLICATE_PROC_ID, True),
        ]
    )
    def test_validate_sim(self, test_sim_constructor, should_raise_exception):
        test_sim = test_sim_constructor()
        test_sim.init_sim()

        chunk_balancer = ChunkBalancer()

        if should_raise_exception:
            with self.assertRaises(ValueError):
                chunk_balancer._validate_sim(test_sim)
        else:
            chunk_balancer._validate_sim(test_sim)

    @parameterized.parameterized.expand(
        [
            (TEST_SIM_1,),
            (TEST_SIM_2,),
            (TEST_SIM_3,),
            (TEST_SIM_4,),
        ]
    )
    def test_chunk_layout_improvement(self, test_sim_constructor):
        """Tests that chunk_balancer improves balance after 1 iteration."""
        test_sim = test_sim_constructor()
        test_sim.init_sim()

        old_timing_measurements = MeepTimingMeasurements.new_from_simulation(
            test_sim, -1
        )

        chunk_balancer = ChunkBalancer()

        chunk_balancer.adjust_chunk_layout(test_sim, sensitivity=1.0)

        new_timing_measurements = MeepTimingMeasurements.new_from_simulation(
            test_sim, -1
        )

        old_step_times = np.array(old_timing_measurements.measurements["time_stepping"])
        new_step_times = np.array(new_timing_measurements.measurements["time_stepping"])

        old_max_time = np.max(old_step_times)
        new_max_time = np.max(new_step_times)

        old_min_time = np.min(old_step_times)
        new_min_time = np.min(new_step_times)

        self.assertLess(new_max_time, old_max_time)
        self.assertGreater(new_min_time, old_min_time)

    @parameterized.parameterized.expand(
        [
            (TEST_SIM_1,),
            (TEST_SIM_2,),
            (TEST_SIM_3,),
            (TEST_SIM_4,),
        ]
    )
    def test_chunk_layout_convergence(self, test_sim_constructor):
        """Tests that chunk_balancer converges to load balanced state."""
        test_sim = test_sim_constructor()
        test_sim.init_sim()

        chunk_balancer = ChunkBalancer()

        num_iterations = 25

        for _ in range(num_iterations):
            chunk_balancer.adjust_chunk_layout(test_sim, sensitivity=0.5)

            new_timing_measurements = MeepTimingMeasurements.new_from_simulation(
                test_sim, -1
            )

            new_step_times = np.array(
                new_timing_measurements.measurements["time_stepping"]
            )

        # Check that new stepping times have converged to close to the mean value
        tolerance = 0.05
        mean_step_time = np.mean(new_step_times)
        self.assertTrue(np.allclose(mean_step_time, new_step_times, rtol=tolerance))

    @parameterized.parameterized.expand(
        [
            (TEST_CHUNK_DATA_1,),
            (TEST_CHUNK_DATA_2,),
            (TEST_CHUNK_DATA_3,),
            (TEST_CHUNK_DATA_4,),
            (TEST_CHUNK_DATA_5,),
            (TEST_CHUNK_DATA_6,),
        ]
    )
    def test_split_pos_adjustment(self, test_chunk_data):
        chunk_layout = test_chunk_data["chunk_layout"]
        sim = MockSimulation(
            cell_size=test_chunk_data["cell_size"],
            resolution=10,
            chunk_layout=chunk_layout,
        )
        sim.init_sim()
        chunk_volumes = sim.structure.get_chunk_volumes()
        chunk_owners = sim.structure.get_chunk_owners()

        chunk_balancer = ChunkBalancer()

        measurements = {}
        num_procs = len(test_chunk_data["time_stepping"])
        for name in TIMING_MEASUREMENT_IDS.keys():
            if name == "time_stepping":
                measurements[name] = test_chunk_data["time_stepping"]
            else:
                measurements[name] = [0] * num_procs
        timing_measurements = MeepTimingMeasurements(
            measurements, -1, None, None, None, None, None
        )

        new_chunk_layout = chunk_balancer.compute_new_chunk_layout(
            timing_measurements,
            chunk_layout,
            chunk_volumes,
            chunk_owners,
            sensitivity=1.0,
        )
        expected_chunk_layout = test_chunk_data["new_chunk_layout"]

        self.assertTrue(
            bpu.partitions_are_equal(new_chunk_layout, expected_chunk_layout)
        )


if __name__ == "__main__":
    unittest.main()