Saving data to disk in the middle of a simulation in standalone mode

Hi @nvar. Indeed, this approach does not work, since all the run calls will end up together in a single C++/CUDA file – you cannot run Python code in between. This is a topic that we’ve been discussing for quite a while, unfortunately we still do not have an elegant solution for it (if anyone wants to work on it, I’d be happy to give a few pointers :blush: ). As a minor note: the StateMonitor is recording everything with the default time step – quite often this is excessive, e.g. using dt=1*ms would directly reduce the necessary memory by 10.

But as a complete solution/workaround, for now, the best approach would be to write your own custom implementation of a recording function in C++, instead of using the StateMonitor. Slightly adapted from my answer here, you could add the following function definition to your script:

@implementation('cpp','''
// Note that functions always need a return value at the moment
double store_state(int i, double time, double value) {
    static std::ofstream f("/some/path/values.txt");  // opens the file the first time
    f << i << "\t" << time << "\t" << value << "\n";
    return 0.;  // unused
}
'''
@check_units(i=1, t=second, value=volt, result=1)
def store_state(i, t, value):
    raise NotImplementedError('Use standalone mode')

Instead of adding a StateMonitor, your script would call the function like this:

G.run_regularly("dummy = store_state(i, t, v)", dt=defaultclock.dt)

This will call your function at every time step for every neuron, and write the values to a text file.

(I didn’t run the above function, so there might be minor errors)

Does that sound that a possible approach for you?