Skip to content

Commit

Permalink
Fixed automatic append of py_ports and var_ports (lava-nc#669)
Browse files Browse the repository at this point in the history
* Fixed automatic append of py_ports and var_ports

Added a check to prevent re-appending the same PyPort or VarPort that is already in a process model's py_ports or var_ports list.

Adjusted CspRecvPort implementation to allocate numpy _result array once at initialization rather than on every update.

Adjusted PyRefPortVectorDense to allocate header numpy arrays the first time they are used, rather than every time a value is sent.

* Fixing flake formatting.

* Reverting change to preallocate _result

It seems that putting the result of the CspRecvPort.recv into a pre-allocated numpy _result leads to a hang due to shared memory between processes, specifically in cases where the user code grabs data from a Monitor. Reverting that change for now, though in the future it would be good to find a way to effectively preallocate to avoid memory allocations during every timestep.

* Cleanup.

* Fixing flake issue.
  • Loading branch information
tim-shea authored Apr 22, 2023
1 parent 2193f30 commit f42019d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/lava/magma/compiler/channels/pypychannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ def recv(self):
result = self._array[self._idx].copy()
self._idx = (self._idx + 1) % self._size
self._ack.release()

return result

def join(self):
Expand Down
5 changes: 2 additions & 3 deletions src/lava/magma/core/model/py/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ def __setattr__(self, key: str, value: ty.Any):
"""
self.__dict__[key] = value
if isinstance(value, AbstractPyPort):
if isinstance(value, AbstractPyPort) and value not in self.py_ports:
self.py_ports.append(value)
# Store all VarPorts for efficient RefPort -> VarPort handling
if isinstance(value, PyVarPort):
if isinstance(value, PyVarPort) and value not in self.var_ports:
self.var_ports.append(value)

def start(self):
Expand Down
19 changes: 12 additions & 7 deletions src/lava/magma/core/model/py/ports.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,13 +699,16 @@ def read(self) -> np.ndarray:
The value of the referenced Var.
"""
if self._csp_send_port and self._csp_recv_port:
header = np.ones(self._csp_send_port.shape) * VarPortCmd.GET
self._csp_send_port.send(header)

if not hasattr(self, 'get_header'):
self.get_header = (np.ones(self._csp_send_port.shape)
* VarPortCmd.GET)
self._csp_send_port.send(self.get_header)
return self._transformer.transform(self._csp_recv_port.recv(),
self._csp_recv_port)

return np.zeros(self._shape, self._d_type)
else:
if not hasattr(self, 'get_zeros'):
self.get_zeros = np.zeros(self._shape, self._d_type)
return self.get_zeros

def write(self, data: np.ndarray):
"""Abstract method to write data to a VarPort to set the value of the
Expand All @@ -717,8 +720,10 @@ def write(self, data: np.ndarray):
The data to send via _csp_send_port.
"""
if self._csp_send_port:
header = np.ones(self._csp_send_port.shape) * VarPortCmd.SET
self._csp_send_port.send(header)
if not hasattr(self, 'set_header'):
self.set_header = (np.ones(self._csp_send_port.shape)
* VarPortCmd.SET)
self._csp_send_port.send(self.set_header)
self._csp_send_port.send(data)


Expand Down

0 comments on commit f42019d

Please sign in to comment.