# Using brian2cuda and cpp\_stardalone for a brian2 script with loop of runs

**URL:** <https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731>\
**Category:** Support\
**Tags:** brian2cuda\
**Created:** [30 June 2022 22:06 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731 "2022-06-30T22:06:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidKing2020](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@DavidKing2020](https://brian.discourse.group/u/DavidKing2020)\
**Post date:** [30 June 2022 22:06 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731/1 "2022-06-30T22:06:51Z")

</div>

I’m trying to use brian2cuda and cpp\_standalone to run the STDP example at:

[https://brian2.readthedocs.io/en/stable/examples/standalone.STDP\_standalone.html](https://brian2.readthedocs.io/en/stable/examples/standalone.STDP_standalone.html)

The only thing I changed is to add a loop to the runs

```
  for i in range(2):
      run(100*second, report='text')

```

The run will crash in the 2nd iteration as expected .  
So in cpp\_standalone mode, I modified the “main.cpp” as the follows and it runs fine.

```
  for(int i = 0; i<2; i++) {
      std::cout<<"i: "<<i<<std::endl;
      magicnetwork.run(100.0, report_progress, 10.0);
  }

```

when using brian2cuda, same modification on main.cu will leads to the following errors when running the 2nd iteration:

```
    .......
    98.989 s (98%) simulated in 1m 10s, estimated 1s remaining.
    100.000 s (100%) simulated in 1m 10s
    i: 1
    Starting simulation at t=100.000 s for duration 100.000 s
    ERROR: cudaMemcpy( &_num_events, &_eventspace[_num_spikespace - 1], sizeof(int32_t), cudaMemcpyDeviceToHost ) failed at code_objects/spikemonitor_codeobject.cu:395 : an illegal memory access was encountered
    terminate called after throwing an instance of 'thrust::system::system_error'
      what(): device free failed: driver shutting down
    Aborted

```

Is there a way to allow brian2cuda to run in a loop? The reason for question is that I’m looping over a large sample of stimuli and run, e.g. 100\*ms, for each stimulus.

---

<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:** [1 July 2022 12:06 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731/2 "2022-07-01T12:06:59Z")

</div>

Hi. Both `cpp_standalone` and `cuda_standalone` support multiple runs without editing the generated code, by using `set_device` with `build_on_run=False` and an explicit `device.build` call: [Computational methods and efficiency — Brian 2 2.5.1 documentation](https://brian2.readthedocs.io/en/stable/user/computation.html#multiple-run-calls)

That said, this will be quite inefficient if you do it for many runs, due to a number of duplicated code files that have to be compiled. How are you changing the stimuli between the runs, actually? In most cases, there is no need to use multiple runs to deal with multiple stimuli, you can usually have a single run and switch stimuli via a `TimedArray` or a `run_regularly` statement: [Input stimuli — Brian 2 2.5.1 documentation](https://brian2.readthedocs.io/en/stable/user/input.html)

---

<div class="post-metadata">

**Author:** ![DavidKing2020](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@DavidKing2020](https://brian.discourse.group/u/DavidKing2020)\
**Post date:** [1 July 2022 14:39 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731/3 "2022-07-01T14:39:44Z")

</div>

When going through a loop of samples, I need to rerun a sample with different condition, e.g. stimulus rate, until the condition is satisfied before moving to the next sample. This is what we discussed in this thread:

[Re-run the model in a previous time segment during a long run](https://brian.discourse.group/t/re-run-the-model-in-a-previous-time-segment-during-a-long-run/296)

I used runtime mode since then. Now trying to use brian2cuda, I might have to make changes directly in the cuda source. Further inputs would be appreciated.

---

<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:** [1 July 2022 14:53 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731/4 "2022-07-01T14:53:50Z")

</div>

One other option would be to use this slightly hacky approach here (the second, “sequential” script):

> <https://gist.github.com/mstimberg/572d5cc3d303da2326a6193980c701db#file-sequential-py>

You do a run, check the results, change some parameter of the model, and then run the model again, without recompiling anything. This means that each run starts again from 0s, but that might not be a problem for you? You can keep, e.g., synaptic weights if you use the same mechanism to initialize them, and then set them for each run with the previous run’s values.

---

<div class="post-metadata">

**Author:** ![DavidKing2020](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@DavidKing2020](https://brian.discourse.group/u/DavidKing2020)\
**Post date:** [1 July 2022 16:39 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731/5 "2022-07-01T16:39:11Z")

</div>

Thanks Marcel, I will give it a try

---

<div class="post-metadata">

**Author:** ![DavidKing2020](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@DavidKing2020](https://brian.discourse.group/u/DavidKing2020)\
**Post date:** [12 July 2022 22:08 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731/6 "2022-07-12T22:08:16Z")

</div>

Hi Marcel

One quick question to make sure I use the function correctly:

```
 input = PoissonGroup(N, rates=0*Hz)
 F = initialize_parameter(input.rates, 15*Hz)
 print('1----: ', input.rates)

```

Will the PoissonGroup run with 0_Hz or 15_Hz. The printout after “initialize\_parameter” still print 0_Hz instead of 15_Hz. After executing run() command, the print out is 15\*Hz. Did I do it correctly?

---

<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:** [13 July 2022 07:24 UTC](https://brian.discourse.group/t/using-brian2cuda-and-cpp-stardalone-for-a-brian2-script-with-loop-of-runs/731/7 "2022-07-13T07:24:55Z")

</div>

Hi @DavidKing2020 . Yes, this is correct, it will use 15Hz. The `initialize_parameter`/`set_parameter_value` function directly set the values in the generated C++ code which is only executed when you call `run`. Before that, the code on the Python side does not know what the value will be. The solution in my gist is a bit hacky, there would be ways to make this work in a more intuitive way.  
However, I am not 100% sure whether it will correctly broadcast the scalar value to an array here. To be on the safe side, try to always set the array with the size of the group, e.g. here you’d use `initialize_parameter(input.rates, np.ones(N)*15*Hz)`.
