# Running multiple standalone simulations \[Request for Feedback\]

**URL:** <https://brian.discourse.group/t/running-multiple-standalone-simulations-request-for-feedback/802>\
**Category:** Development\
**Tags:** cpp\_standalone, rff\
**Created:** [10 October 2022 10:03 UTC](https://brian.discourse.group/t/running-multiple-standalone-simulations-request-for-feedback/802 "2022-10-10T10:03:48Z")\
**Posts on this page:** 1\
**Showing post:** 20

<div class="post-metadata">

**Author:** ![mstimberg](https://yyz2.discourse-cdn.com/free1/user_avatar/brian.discourse.group/mstimberg/32/11_2.png) [@mstimberg](https://brian.discourse.group/u/mstimberg)\
**Post date:** [24 October 2022 10:20 UTC](https://brian.discourse.group/t/running-multiple-standalone-simulations-request-for-feedback/802/20 "2022-10-24T10:20:49Z")

</div>

> [@rth](#):
>
> ### An Issue
> 
> **Well, it is obviously the spike queue!** Is it possible to save the spike queue somewhere and then restore it from the file? That will solve any problem of a long run with multiple stops.
> 
> _Note_ This method works perfectly if there are no delays in the system. But as soon as spikes have to wait in spike queue, this method have an issue.

Unfortunately, this is not possible at the moment in any clean kind of way. This is one of the things that the `store`/`restore` method deals with ([Running a simulation — Brian 2 2.5.4 documentation](https://brian2.readthedocs.io/en/stable/user/running.html#store-restore)) which is not available for C++ standalone.

But if your issue is only the memory usage of the monitors, then I think an easier solution would be to use C++ code to write your recordings directly to disk instead of keeping them in memory. For a `SpikeMonitor`, you can find a solution here: [Real Time Monitor - #2 by mstimberg](https://brian.discourse.group/t/real-time-monitor/173/2)  
For a `StateMonitor`, you can use a function like this:

```python
def disk_writer(filename, size, name):
    code = '''
    double %name%(int index, double value) {
        static std::ofstream outfile("%filename%", ios::binary | ios::out);
        static double data_container[%size%]; // stores data for one timestep
        data_container[index] = value;
        if (index == %size% - 1) { //write to disk when last value has been written
            outfile.write(reinterpret_cast<char*>(data_container), %size%*sizeof(double));
        }
        return 0.0;
    }
    '''.replace('%name%', name).replace('%filename%', filename).replace('%size%', str(size))

    @implementation('cpp', code)
    @check_units(index=1, value=1, result=1)
    def disk_writer(index, value):
        raise NotImplementedError('C++ only')
    return disk_writer

```

This can then be used in a `run_regularly` operation to replace a `StateMonitor`:

```python
store_v = disk_writer(file_name, len(group), 'store_v')

group.run_regularly('dummy = store_v(i, v)')

```

After the run, you can reload the data like this:

```python
v_values = np.fromfile(file_name)
v_values = v_values.reshape((-1, len(group))).T

```

Here’s a full example demonstrating that it records the same thing as the `StateMonitor`:

> **Full code example**
>
> ```python
> import os
> from brian2 import *
> set_device('cpp_standalone')
> group = NeuronGroup(2, 'dv/dt = -v/(10*ms) + 0.1/sqrt(5*ms)*xi : 1', method='euler')
> mon = StateMonitor(group, 'v', record=True)
> 
> def disk_writer(filename, size, name):
> code = '''
> double %name%(int index, double value) {
> static std::ofstream outfile("%filename%", ios::binary | ios::out);
> static double data_container[%size%]; // stores data for one timestep
> data_container[index] = value;
> if (index == %size% - 1) { //write to disk when last value has been written
> outfile.write(reinterpret_cast<char*>(data_container), %size%*sizeof(double));
> }
> return 0.0;
> }
> '''.replace('%name%', name).replace('%filename%', filename).replace('%size%', str(size))
> 
> @implementation('cpp', code)
> @check_units(index=1, value=1, result=1)
> def disk_writer(index, value):
> raise NotImplementedError('C++ only')
> return disk_writer
> 
> file_name = '/tmp/v_values.npy'
> store_v = disk_writer(file_name, len(group), 'store_v')
> 
> group.run_regularly('dummy = store_v(i, v)')
> 
> run(100*ms, report='text')
> 
> v_values = np.fromfile(file_name)
> v_values = v_values.reshape((-1, len(group))).T
> fig, axs = plt.subplots(2, 1, sharex=True, sharey=True)
> axs[0].plot(mon.t/ms, mon.v.T)
> axs[1].plot(mon.t/ms, v_values.T)
> plt.show()
> 
> ```

![Figure_1](https://global.discourse-cdn.com/free1/uploads/brian/original/1X/c67ad6ae22edd1f48a045bc6c4a157603ffcf47b.png)

---

_[View the full topic](https://brian.discourse.group/t/running-multiple-standalone-simulations-request-for-feedback/802)._
