brian-team/brian2cuda

Fix `ReferencError` in spatialneuron tests

denisalevi opened this issue · 3 comments

This test from the brian2 test suite (test_spatialneuron.py::test_basic_diffusion) is failing in cuda_standalone:

# this imports all kind of assert functions
from numpy.testing import *

from brian2 import *
import brian2cuda

device_name = 'cuda_standalone'
#device_name = 'cpp_standalone'
set_device(device_name, directory=device_name)

# A very basic test that shows that propagation is working in a very basic
# sense, testing all morphological classes

defaultclock.dt = 0.01*ms

EL = -70*mV
gL = 1e-4*siemens/cm**2
target = -10*mV
eqs = '''
Im = gL*(EL-v) + gClamp*(target-v): amp/meter**2
gClamp : siemens/meter**2
'''

morph = Soma(diameter=30*um)
morph.axon = Cylinder(n=10, diameter=10*um, length=100*um)
morph.dend = Section(n=10, diameter=[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.1]*um,
                     length=np.ones(10)*10*um)

neuron = SpatialNeuron(morph, eqs)
neuron.v = EL
neuron.axon.gClamp[0] = 100*siemens/cm**2

mon = StateMonitor(neuron, 'v', record=True)

run(0.25*ms)

assert all(abs(mon.v[:, -1]/mV + 10) < 0.25), mon.v[:, -1]/mV

This fails with

ReferenceError: weakly-referenced object no longer exists

It looks like some of the spatialneuron variables in array_specs have no owner attribute, which I am checking for here in objects.cu.

@mstimberg I came across a comment that sounds like this is something you took care of before (see here). Is there an easy pattern by which I could detect all those var that have no owner, since it seems to be related to only spatialneuron variables? jinja has no hasattr filter, so I couldn't use that. I could of course just do the test on Python side, I'm realizing while writing this...

Hmm, I don't see why these owners should no longer exist, this should only be an issue with temporarily created SpatialSubgroup objects (somewhat similar to when record from neurons[3:5], where the subgroup ceases to exist). Can you see for what variables that happens? All variables have owners, but they are weak references and this is why accessing its attributes can get you a ReferenceError – even if you had hasattr, this would raise an error as well. I don't see a way to do this check in the template, it would have to be in Python code with a try/except, I think.

I implemented it in the Python code. It was only one variable: _array_spatialneuron_subgroup__sub_idx. Even without using a SpatialSubgroup.

A SpatialSubgroup is also automatically created if you write something like neuron.axon.gClamp (the neuron.axon is a SpatialSubgroup and the owner of the gClamp variable in this context) – I think this is where the issue came from.