Skip to content

Commit

Permalink
Use proper queue in HostReceiveNode for speed
Browse files Browse the repository at this point in the history
This makes things faster by not requiring us to increment a counter
through the node, and also makes sure we get all the data.

We also fix the time in this node to use zero-based timesteps
rather than one-based timesteps (to conform with core Nengo).
  • Loading branch information
hunse committed Nov 11, 2019
1 parent 76ddb35 commit 8d1dfb4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
17 changes: 9 additions & 8 deletions nengo_loihi/builder/inputs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import collections

from nengo import Node
from nengo.exceptions import SimulationError
from nengo.params import Default
Expand All @@ -24,21 +26,20 @@ class HostReceiveNode(Node):
"""For receiving chip->host messages"""

def __init__(self, dimensions, label=Default, check_output=False):
self.queue = [(0, np.zeros(dimensions))]
self.queue_index = 0
self.queue = collections.deque()
super(HostReceiveNode, self).__init__(
self.update, size_in=0, size_out=dimensions, label=label
)
if hasattr(self, "check_output"):
self.check_output = check_output

def update(self, t):
while (
len(self.queue) > self.queue_index + 1
and self.queue[self.queue_index][0] < t
):
self.queue_index += 1
return self.queue[self.queue_index][1]
if t <= 0:
return np.zeros(self.size_out)

t1, x = self.queue.popleft()
assert abs(t - t1) < 1e-8
return x

def receive(self, t, x):
self.queue.append((t, x))
Expand Down
2 changes: 1 addition & 1 deletion nengo_loihi/emulator/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def send(self, probe, already_sent, receiver):
if probe.weights is not None:
x = np.dot(x, probe.weights)
for j, xx in enumerate(x):
receiver.receive(self.dt * (already_sent + j + 2), xx)
receiver.receive(self.dt * (already_sent + j + 1), xx)
return len(x)

def update(self, t, compartment):
Expand Down
2 changes: 1 addition & 1 deletion nengo_loihi/hardware/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def _chip2host_monitor(self, probes_receivers):

for j in range(len(x)):
receiver.receive(
self.model.dt * (self._chip2host_sent_steps + j + 2), x[j]
self.model.dt * (self._chip2host_sent_steps + j + 1), x[j]
)

if increment is not None:
Expand Down
2 changes: 1 addition & 1 deletion nengo_loihi/hardware/snips/nengo_io.c.template
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ void nengo_io(runState *s) {
}
}

buffer[0] = s->{{ obfs.step }};
buffer[0] = s->{{ obfs.step }} - 1; // zero-based time step
{% for n_out, core, compartment, key in probes %}
{% if key == 'u' %}
buffer[{{ n_out }}] = core{{ core }}->{{ obfs.state }}[{{ compartment }}].U;
Expand Down

0 comments on commit 8d1dfb4

Please sign in to comment.