Skip to content

Commit

Permalink
Close sub-simulators on close
Browse files Browse the repository at this point in the history
Also adds a close method to the emulator (for consistency).

Fixes #102
  • Loading branch information
drasmuss authored and hunse committed Oct 30, 2018
1 parent dc3af4a commit 48a5895
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ Release history
- An error is now raised if
a learning rule is applied to a non-decoded connection.

**Fixed**

- Closing ``nengo_loihi.Simulator`` will now close all the inner
sub-simulators as well
(`#102 <https://github.com/nengo/nengo-loihi/issues/102>`_)

0.3.0 (September 28, 2018)
==========================

Expand Down
4 changes: 4 additions & 0 deletions nengo_loihi/loihi_cx.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ def __init__(self, model, seed=None):

self._probe_filters = {}
self._probe_filter_pos = {}
self.closed = False

@classmethod
def error(cls, msg):
Expand Down Expand Up @@ -841,3 +842,6 @@ def get_probe_output(self, probe):
x = np.asarray(self.probe_outputs[cx_probe], dtype=np.float32)
x = x if cx_probe.weights is None else np.dot(x, cx_probe.weights)
return self._filter_probe(cx_probe, x)

def close(self):
self.closed = True
11 changes: 11 additions & 0 deletions nengo_loihi/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ def close(self):
`.Simulator.step`, and `.Simulator.reset` on a closed simulator raises
a ``SimulatorClosed`` exception.
"""

if self.loihi is not None:
self.loihi.close()
if self.simulator is not None:
self.simulator.close()
if self.precompute:
self.host_pre_sim.close()
self.host_post_sim.close()
else:
self.host_sim.close()

self.closed = True

def _probe(self):
Expand Down
24 changes: 24 additions & 0 deletions nengo_loihi/tests/test_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,27 @@ def test_nengo_comm_channel_compare(simtype, Simulator, seed, plt, allclose):

assert allclose(loihi_sim.data[ap], nengo_sim.data[ap], atol=0.1, rtol=0.2)
assert allclose(loihi_sim.data[bp], nengo_sim.data[bp], atol=0.1, rtol=0.2)


@pytest.mark.parametrize("precompute", (True, False))
def test_close(Simulator, precompute, pytestconfig):
with nengo.Network() as net:
a = nengo.Node([0])
b = nengo.Ensemble(10, 1)
c = nengo.Node(size_in=1)
nengo.Connection(a, b)
nengo.Connection(b, c)

with Simulator(net, precompute=precompute) as sim:
pass

assert sim.closed
if pytestconfig.getoption("--target") == "loihi":
assert sim.loihi.closed
else:
assert sim.simulator.closed
if precompute:
assert sim.host_pre_sim.closed
assert sim.host_post_sim.closed
else:
assert sim.host_sim.closed

0 comments on commit 48a5895

Please sign in to comment.