Brian2 code generation
Use this route when the task names prefs.codegen.target, Cython, NumPy code
objects, set_device, cpp_standalone, a compiler/build directory, repeated
standalone runs, run_args, or GSL integration. This sub-skill is written for
Brian2 2.9.0 and Python >=3.12.
Route quickly
- Ordinary runtime simulation: keep the default
runtime device. Start
with prefs.codegen.target = "auto"; it prefers Cython when its test
compilation succeeds and otherwise falls back to NumPy with a warning.
Choose "numpy" explicitly when compiler setup is unavailable or a
reproducible no-compiler fallback is more important than speed.
- Compiled runtime: choose
prefs.codegen.target = "cython" only after
Cython and a working C++ compiler are available. The first use compiles and
caches extensions; later compatible code can reuse the cache.
- Standalone project: call
set_device("cpp_standalone") before model
construction. Use the default build_on_run=True for one final run; use
build_on_run=False for multiple run statements or a build/reuse loop.
device.build controls generation, compilation, and optional first
execution: compile=False writes a project but cannot run it, while
run_args is forwarded to that first execution. Read
standalone-workflows.md before
changing a runtime script.
- Reuse: build once with
device.build(run=False) and call
device.run(run_args=..., results_directory=...) for subsequent full
executions. run_args accepts VariableView/TimedArray mappings or
command-line assignment lists; result directories are relative to the
project and must be unique when outputs are retained or concurrent. The
model and externally defined constants are not generally mutable after
compilation; use equation variables with suitable flags and run_args
instead.
- Optional GSL: treat
method="gsl" and the gsl_* methods as an
optional native-library path. Runtime GSL needs Cython plus GSL; standalone
also needs GSL headers and libraries. If unavailable, use a supported
non-GSL state updater and record the numerical/feature limitation.
Operating guardrails
get_device() returns the active device and device is a proxy to it. A
set_device call is global to the current process; restore the runtime
device when a larger application continues after a standalone section.
- A CPU/package import check proves only Python/package readiness. It does not
prove that Cython can compile or that a C++ standalone project can build and
run. Treat compiler preparation and the final native build as separate gates.
- Standalone writes generated sources, a binary, and result files to a project
directory. Use a fresh temporary or uniquely named directory for checks and
never reuse a result directory concurrently.
device.delete(data=True, ...)
targets the active result directory; use a disposable project or explicitly
remove older result directories as well.
- State arrays and synaptic indices whose values depend on executed code cannot
be inspected during standalone setup. Access them after a successful run;
use string expressions for dependent initialization where possible.
- GSL state-updater code is experimental, requires native GSL linkage, is not a
NumPy runtime path, and rejects stochastic equations. A non-GSL updater is a
functional but potentially non-equivalent fallback; do not claim GSL support
without a tiny target-specific compile/run.
- Do not use this route to design neuron/synapse equations, schedule ordinary
networks, or explain general preferences. Hand those requests to the
corresponding modeling, simulation, or configuration route. Long benchmarks,
multiprocessing studies, and large training runs are reference-only here.
Bundled operating references
- api-reference.md: target, device, build, run,
cache, and cleanup API facts.
- runtime-targets.md: runtime target selection,
Cython prerequisites, NumPy fallback, and cache reuse.
- standalone-workflows.md: one-run,
multi-run,
run_args, result directories, and standalone limitations.
- optional-backends.md: GSL prerequisites,
supported paths, and fallback boundaries.
- troubleshooting.md: install/import,
optional-dependency, data/configuration, API, compiler/cache, and workflow
failure recovery.
For a safe native smoke after the integrated skill has been prepared, run the
bundled script with --help first and then, only with an intentionally prepared
compiler, run the default tiny check:
python scripts/standalone_smoke.py --help
python scripts/standalone_smoke.py
The script uses a temporary output directory, a tiny deterministic model, and
cleanup. It is not a substitute for a real project build of a user's model.
1---2name: code-generation3description: "Choose Brian2 runtime and C++ standalone code-generation targets,4 manage builds and reusable runs, and recover compiler, cache,5 result-directory, and optional GSL failures."6disable-model-invocation: true7metadata:8 source: https://skillmd.com/skills/vectorspacelab/code-generation9 disco-role: operating10license: NOASSERTION11---1213# Brian2 code generation1415Use this route when the task names `prefs.codegen.target`, Cython, NumPy code16objects, `set_device`, `cpp_standalone`, a compiler/build directory, repeated17standalone runs, `run_args`, or GSL integration. This sub-skill is written for18Brian2 2.9.0 and Python >=3.12.1920## Route quickly21221. **Ordinary runtime simulation:** keep the default `runtime` device. Start23 with `prefs.codegen.target = "auto"`; it prefers Cython when its test24 compilation succeeds and otherwise falls back to NumPy with a warning.25 Choose `"numpy"` explicitly when compiler setup is unavailable or a26 reproducible no-compiler fallback is more important than speed.272. **Compiled runtime:** choose `prefs.codegen.target = "cython"` only after28 Cython and a working C++ compiler are available. The first use compiles and29 caches extensions; later compatible code can reuse the cache.303. **Standalone project:** call `set_device("cpp_standalone")` before model31 construction. Use the default `build_on_run=True` for one final `run`; use32 `build_on_run=False` for multiple `run` statements or a build/reuse loop.33 `device.build` controls generation, compilation, and optional first34 execution: `compile=False` writes a project but cannot run it, while35 `run_args` is forwarded to that first execution. Read36 [standalone-workflows.md](references/standalone-workflows.md) before37 changing a runtime script.384. **Reuse:** build once with `device.build(run=False)` and call39 `device.run(run_args=..., results_directory=...)` for subsequent full40 executions. `run_args` accepts `VariableView`/`TimedArray` mappings or41 command-line assignment lists; result directories are relative to the42 project and must be unique when outputs are retained or concurrent. The43 model and externally defined constants are not generally mutable after44 compilation; use equation variables with suitable flags and `run_args`45 instead.465. **Optional GSL:** treat `method="gsl"` and the `gsl_*` methods as an47 optional native-library path. Runtime GSL needs Cython plus GSL; standalone48 also needs GSL headers and libraries. If unavailable, use a supported49 non-GSL state updater and record the numerical/feature limitation.5051## Operating guardrails5253- `get_device()` returns the active device and `device` is a proxy to it. A54 `set_device` call is global to the current process; restore the runtime55 device when a larger application continues after a standalone section.56- A CPU/package import check proves only Python/package readiness. It does **not**57 prove that Cython can compile or that a C++ standalone project can build and58 run. Treat compiler preparation and the final native build as separate gates.59- Standalone writes generated sources, a binary, and result files to a project60 directory. Use a fresh temporary or uniquely named directory for checks and61 never reuse a result directory concurrently. `device.delete(data=True, ...)`62 targets the active result directory; use a disposable project or explicitly63 remove older result directories as well.64- State arrays and synaptic indices whose values depend on executed code cannot65 be inspected during standalone setup. Access them after a successful run;66 use string expressions for dependent initialization where possible.67- GSL state-updater code is experimental, requires native GSL linkage, is not a68 NumPy runtime path, and rejects stochastic equations. A non-GSL updater is a69 functional but potentially non-equivalent fallback; do not claim GSL support70 without a tiny target-specific compile/run.71- Do not use this route to design neuron/synapse equations, schedule ordinary72 networks, or explain general preferences. Hand those requests to the73 corresponding modeling, simulation, or configuration route. Long benchmarks,74 multiprocessing studies, and large training runs are reference-only here.7576## Bundled operating references7778- [api-reference.md](references/api-reference.md): target, device, build, run,79 cache, and cleanup API facts.80- [runtime-targets.md](references/runtime-targets.md): runtime target selection,81 Cython prerequisites, NumPy fallback, and cache reuse.82- [standalone-workflows.md](references/standalone-workflows.md): one-run,83 multi-run, `run_args`, result directories, and standalone limitations.84- [optional-backends.md](references/optional-backends.md): GSL prerequisites,85 supported paths, and fallback boundaries.86- [troubleshooting.md](references/troubleshooting.md): install/import,87 optional-dependency, data/configuration, API, compiler/cache, and workflow88 failure recovery.8990For a safe native smoke after the integrated skill has been prepared, run the91bundled script with `--help` first and then, only with an intentionally prepared92compiler, run the default tiny check:9394```text95python scripts/standalone_smoke.py --help96python scripts/standalone_smoke.py97```9899The script uses a temporary output directory, a tiny deterministic model, and100cleanup. It is not a substitute for a real project build of a user's model.