Running thousands of sequential simulations with the same network in cpp_standalone

Description of problem

Our aim is to teach a network with natural images. We can do this by concatenating a large number of input files as a preprocessing step. Nevertheless, memory limitations and fluency call for a more flexible solution.

We are building simulations with potentially up to ten thousand input files that should be processed sequentially using the same network with the cpp_standalone backend. The network itself does not change: the number of neurons and synapses are fixed. However, their connection weights evolve through learning.

Here the system gets retina ganglion cell spike indices and times in the from of preprocessed retina output:

            spk_generator_sp = spikes_data["spikes_0"][0]

            spk_generator_ti = spikes_data["spikes_0"][1]

            spk_generator = b2.SpikeGeneratorGroup(

                number_of_neurons, spk_generator_sp, spk_generator_ti

            )

separately for ON and OFF ganglion cells.

The only change is in the input spikes from the SpikeGeneratorGroup. For example, for 10,000 input files and a runtime of 1 second, we would like the first simulation to use the first input file, simulate for 1s, keep the complete network state, replace the SpikeGeneratorGroup spikes with those from the second input file, simulate for 1s, and so on. In other words, we want the simulations to behave as if they were one continuous simulation consisting of 10,000 sequential 1-second segments, with the input changing between segments but the network state being preserved. Kind of streaming the input.

To achieve this, we initially tried to update the SpikeGeneratorGroup with its set_spikes() method:

# (Network setup omitted)

target_directory = ”/our/target/dir/”

for n, input_file in enumerate(input_files):

    start_time = n * runtime * second

    on_data = load_data(on_input_file)

    off_data = load_data(off_input_file)

    spike_generator1.set_spikes(

        on_data["spikes_0"][0],

        on_data["spikes_0"][1] + start_time,

        sorted=True,

    )

    spike_generator2.set_spikes(

        off_data["spikes_0"][0],

        off_data["spikes_0"][1] + start_time,

        sorted=True,

    )

 

    b2.run(runtime, report="text")

#End of loop

b2.device.build(

    directory=target_directory,

    run=False,

    compile=True,

) # Same result with or without clean=True

b2.device.run()

With this approach we run into different problems.

The first problem is that, for a large enough number of input files, we get the error

“/bin/sh: Argument list too long

make: *** [makefile:23: make.deps] Error 127

This seems to be related to how Make handles the list of source files. The generated Makefile contains:

 make.deps: $(SRCS) $(H_SRCS)
        $(CXX) $(CXXFLAGS) -MM $(SRCS) > make.deps

$(SRCS) contains a list of source files (up to ten thousand in our case), which leads the expanded command to hit the ARG_MAX limit in Linux, hence the error /bin/sh: Argument list too long. We cannot rely on increasing ARG_MAX on the HPC system where we will be running our simulations.

The second problem is that this strategy dilates compilation times so much that almost nullifies the advantages of switching to the cpp backend over python.

As a general solution, we were wondering whether we could compile a network without specifying in advance what input files will be presented to it. Our understanding is that changing input spikes with set_spikes(), then calling b2.run(self.runtime, report=“text”) will make data part of the generated standalone simulation/program rather than being supplied to a persistent compiled model at runtime. Would it be possible instead to compile a network independently of its future SpikeGeneratorGroup inputs, and subsequently provide arbitrary spike indices/times to the already-compiled model at runtime? If not, what is the best way to implement such a workflow? Our main goal is to avoid generating and compiling a new piece of simulation code for every input file.

Minimal code to reproduce problem

See section above for relevant code.

What you have already tried

An interim workaround for us is either to run the simulations with the Python backend (which is slower) or to concatenate the 5000 input files into a single large input file and then run the simulation with the cpp backend, as suggested in the documentation.

Full traceback of error

(relevant part; skipped repetitive compiler output)

/bin/sh: Argument list too long

make: *** [makefile:23: make.deps]

Error 127 make: *** Waiting for unfinished jobs…

ERROR Brian 2 encountered an unexpected error. If you think this is a bug in Brian 2, please report this issue either to the discourse forum at http://brian.discourse.group/, or to the issue tracker at https://github.com/brian-team/brian2/issues. Please include this file with debug information in your report: /tmp/brian_debug_iice5b24.log Additionally, you can also include a copy of the script that was run, available at: /tmp/brian_script_5o2uxdau.py You can also include a copy of the redirected std stream outputs, available at ‘/tmp/brian_stdout_ea0ry_gg.log’ and ‘/tmp/brian_stderr_mob_o25f.log’. Thanks! [brian2]

Traceback (most recent call last):

File “/opt2/git_repos/smart/docs/examples/example_plasticity_training.py”, line 91, in run_with_overrides(run_pipeline) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^

File “/opt2/git_repos/smart/docs/examples/example_plasticity_training.py”, line 72, in run_with_overrides run_pipeline() ~~~~~~~~~~~~^^

File “/opt2/git_repos/smart/docs/examples/example_plasticity_training.py”, line 82, in run_pipeline sim_results = session.simulation.simulate()

File “/opt2/git_repos/smart/smart/simulation/simulation_module.py”, line 1097, in simulate results = self._run_simulation_in_cxsystem( anatomy_path, …<2 lines>… unit_position_df=unit_positions, )

File “/opt2/git_repos/smart/smart/simulation/simulation_module.py”, line 992, in _run_simulation_in_cxsystem results = CM.run()

File “/home/francescangelo/.cache/pypoetry/virtualenvs/smart-wO9jYNL1-py3.14/lib/python3.14/site-packages/cxsystem2/core/cxsystem.py”, line 562, in run b2.device.build(directory=target_directory, run=False, compile=True, clean=True) ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File “/home/francescangelo/.cache/pypoetry/virtualenvs/smart-wO9jYNL1-py3.14/lib/python3.14/site-packages/brian2/devices/cpp_standalone/device.py”, line 1642, in build self.compile_source(directory, compiler, debug, clean) ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File “/home/francescangelo/.cache/pypoetry/virtualenvs/smart-wO9jYNL1-py3.14/lib/python3.14/site-packages/brian2/devices/cpp_standalone/device.py”, line 1193, in compile_source raise RuntimeError(error_message) RuntimeError: Project compilation failed (error code: 512).

Francescangelo Vedele & Simo Vanni

One thing to keep in mind is that in cpp_standalone all the run() calls in your loop get compiled into a single program, so set_spikes() inside a Python loop is baked in at build time rather than evaluated between segments. That is probably why the loop turns into one huge simulation with the memory problems you describe. A pattern that may work better is to build the standalone project once and pass the per-segment data through run_args, for example by making the spike times and indices into variables that are set from the command line when you call device.run(). You can then run the compiled binary once per input file from a small outer script. To keep the learned weights between segments, you could write the synaptic weights out at the end of each run and feed them back in as the initial values for the next one.

Hi @francescangelo. Indeed, as @colorfulcms said, the run_args mechanism is in principle the way to go here. See Computational methods and efficiency — Brian 2 2.11.0 documentation for some documentation. Unfortunately, this is only straightforward for setting “normal” variables and TimedArrays. For SpikeGeneratorGroup, this is still an open issue: Make `SpikeGeneratorGroup` work for `run_args` · Issue #1578 · brian-team/brian2 · GitHub

So at the moment the simplest solution is to concatenate all input patterns as you seem to use as a workaround already. Is this getting to big to be feasible? If yes, I might be able to come up with a more complex workaround, but for that could you please give a bit more information about your input patterns: are they completely arbitrary or do they have some specific properties, e.g. the same number of spikes, or only one spike per neuron, or something else along these lines?

Hi, thank you for your reply! Indeed, it seems that the solution we were looking for overlaps with the issue you linked, and as you point out in the issue itself it might not be straightforward to implement. For now we will keep concatenating the inputs as it’s not too big of a challenge to handle, at least yet. Spikes are arbitrary so unfortunately no pattern repeats. By the way, I might have identified a memory leak with the python backend when running multiple simulations; if so I will follow up soon with an issue on GitHub