Code Generation in Computational Neuroscience: A Review of Tools and Techniques

Inga Blundell, Romain Brette, Thomas A. Cleland, Thomas G. Close, Daniel Coca, Andrew P. Davison, Sandra Diaz-Pier, Carlos Fernandez Musoles, Padraig Gleeson, Dan F. M. Goodman, Michael L. Hines, Michael W. Hopkins, Pramod S. Kumbhar, David R. Lester, Bóris Marin, Abigail Morrison, Eric Müller, Thomas Nowotny, Alexander Peyser, Dimitri Plotnikov, Paul Richmond, Andrew Rowley, Bernhard Rumpe, Marcel Stimberg, Alan B. Stokes, Adam R. Tomkins, Guido Trensch, Michael Marmaduke Woodman, Jochen Martin Eppler

Abstract

Computational neuroscience faces a significant challenge in bridging the gap between high-level mathematical descriptions of biological systems and the low-level, high-performance code required for simulation. This review explores the emergence of code generation as a solution to this "conceptual gap." By utilizing Domain-Specific Languages (DSLs) and automated translation pipelines, researchers can decouple biological modeling from hardware optimization. We examine various tools that enable efficient simulation across CPUs, GPUs, and specialized neuromorphic hardware, highlighting the benefits of symbolic mathematics and hardware abstraction.

Introduction: The Conceptual Gap

Historically, neuroscientists have been forced to choose between the reliability of fixed libraries and the flexibility of custom-written code. While libraries are fast and reliable, they often lack the versatility to accommodate novel mathematical models. Conversely, custom code offers flexibility but is prone to errors and requires significant expertise in high-performance computing (HPC). As neural models increase in complexity and hardware architectures diversify—ranging from standard CPUs to massive GPU clusters and non-Von Neumann chips like SpiNNaker—manual coding has become a primary bottleneck in the field.

The Code Generation Workflow

Code generation tools address these challenges by automatically transforming high-level model specifications into optimized, machine-executable code. This process typically involves several stages:

  1. Model Specification: Users define models using DSLs (e.g., NESTML, NineML) or high-level languages like Python (e.g., Brian).
  2. Intermediate Representation (IR): The tool parses equations into an Abstract Syntax Tree (AST) or symbolic representation.
  3. Optimization: Pipelines perform symbolic integration, constant folding, and hardware-specific optimizations such as memory alignment.
  4. Target Code Generation: Using templating engines (e.g., Jinja2), the tool produces source code in C++, CUDA, or OpenCL.
  5. Compilation: The generated code is compiled into a "lean and mean" binary for execution.
Schematic representation of neuron and synapse models, highlighting the components handled by code generation pipelines.

As shown in the figure above, these pipelines must manage complex interactions between continuous changes (differential equations) and discrete events (thresholds and resets) within both neuron and synapse models.

Key Insights and Hardware Abstraction

A core advantage of code generation is the separation of concerns. Neuroscientists can focus on biological accuracy while computer scientists optimize the underlying hardware implementation. This abstraction is particularly critical for specialized platforms like GPUs and neuromorphic chips, where managing parallel threads and unique memory layouts is prohibitively complex for most researchers.

Furthermore, the use of symbolic mathematics (e.g., via SymPy) allows tools like Brian and NESTML to analyze equations. If an equation can be solved analytically, the generator can produce an exact update rule, significantly improving performance over numerical approximations.

Tool-Specific Implementations

The review highlights several prominent tools, each optimized for different use cases:

  • Brian 2: Noted for its user-friendly Python syntax and flexible code generation for CPUs and GPUs.
  • GeNN: A powerhouse for GPU acceleration that generates highly optimized CUDA kernels from a C++ API.
  • SpiNNaker: Demonstrates how code generation can map neural populations onto a unique architecture consisting of thousands of small ARM processors.
  • TVB-HPC: Focuses on large-scale brain simulations, generating parallel kernels using the Loopy library.
The GeNN code generation workflow, illustrating the transformation from high-level model definitions to optimized executables.

The GeNN workflow (illustrated above) exemplifies the "lean and mean" approach, where a large, comprehensive library is used to generate a specific, optimized runner and kernel for a given model architecture and detected hardware.

Limitations and Future Directions

Despite significant progress, challenges remain. The "impedance mismatch" between high-level, hardware-agnostic descriptions (like NeuroML) and highly specific optimized code requires more advanced semantic processing. Additionally, while interoperability is a goal, different simulators still maintain unique requirements that hinder true "write once, run anywhere" portability. Future work will likely focus on improving scalability through emerging interconnects like NVLink and enhancing the security of web-based simulation platforms that must manage the execution of arbitrary generated code.

Summary updated

BibTeX
@article{blundell2018code,
  author = {Inga Blundell and Romain Brette and Thomas A. Cleland and Thomas G. Close and Daniel Coca and Andrew P. Davison and Sandra Diaz-Pier and Carlos Fernandez Musoles and Padraig Gleeson and Dan F. M. Goodman and Michael L. Hines and Michael W. Hopkins and Pramod S. Kumbhar and David R. Lester and Bóris Marin and Abigail Morrison and Eric Müller and Thomas Nowotny and Alexander Peyser and Dimitri Plotnikov and Paul Richmond and Andrew Rowley and Bernhard Rumpe and Marcel Stimberg and Alan B. Stokes and Adam R. Tomkins and Guido Trensch and Michael Marmaduke Woodman and Jochen Martin Eppler},
  title = {{Code Generation in Computational Neuroscience: A Review of Tools and Techniques}},
  journal = {Frontiers Neuroinformatics},
  year = {2018},
  doi = {10.3389/fninf.2018.00068},
}
Figure