GSoC 2024 Project Idea 18.3 Replace Brian’s just-in-time compilation mechanism (350 h)

Brian is a clock-driven spiking neural network simulator which is easy to learn and use, highly flexible and easy to extend. It is written in Python and allows describing and running arbitrary neural and synaptic models without having to write code in any other programming language. It is built on a code-generation framework that transforms the model description into efficient low-level code. In Brian’s “runtime mode”, the generated code can interact with Python code by accessing the same data structures in memory. To make this work seamlessly, Brian makes use of Cython. This approach comes with two major disadvantages: 1) Cython compilation is slow (it generates a lot of code for error checking, etc.). For Cython, this is not a big downside, since it is commonly used to compile libraries once, but for Brian it matters since it needs to compile dynamically generated code frequently 2) We need to maintain a third code generation target besides Python and C++, with small but non-trivial differences to both of them.

The aim of this project is to:

  • Replace the Python data structures that are currently used from within Cython code (dynamic arrays and the “spike queue”) by C++ equivalents
  • Research solutions to call the compiled C++ code from Python and make it directly access the memory in the shared data structures storing the simulation variables. This could build upon existing just-in-time compilation technologies such as numba, or a package such as scipy-weave.
  • Implement the above solution, and refactor the current code to make use of it.

Skill level: Intermediate/Advanced

Required skills: Python, C++

Time commitment: Full-time (350 h)

Lead mentor: Marcel Stimberg (marcel.stimberg@sorbonne-universite.fr)

Project website: https://github.com/brian-team/brian2

Backup mentors: Dan Goodman (d.goodman@imperial.ac.uk)

Tech keywords: Python, C++, compilation, JIT

Good afternoon from California @mstimberg @d.goodman !

My name is Min Goo, and I recently graduated with a Bachelor’s degree in Mathematics and Computer Science from UC Berkeley in December. I have industry experience in software development, but as I have not participated in open-source contributions, I hope to take GSoC as an opportunity to contribute to theoretical natural sciences with my technical skillsets.

From the list of INCF’s projects, improving Brian’s JIT implementation was most aligned with my interest as my previous experiences dealt with improving user experience by adding backend APIs and optimizing service flows.

I have started delving into Brian’s documentation, particularly the User’s guide and Developer’s guide. If there are any additional resources or specific areas you recommend, I would greatly appreciate your guidance. I look forward to discussing how I can contribute to this project!

Hello @mstimberg @d.goodman.

I am Arnav Luhadiya, a 2nd year undergrad at SRM Institute of Technology. I feel this project is a good fit for me since I am well versed with data structures and algorithms (I won a gold medal at Indian National Olympiad of Informatics). I have started to try to understand the codebase on the GitHub repository. I would be extremely grateful for your advice regarding resources and ways to contribute to the project.

Dear @matcha, dear @arnav_luhadiya, many thanks for your interest in the project. This project is a bit different from the others we propose, in that there will be a significant “exploratory” component where we figure out what the best approach will be. Therefore, in addition to getting an idea of how Brian works, it would be good to get an overview of existing solutions to interface C/C++ and Python (Cython, ctypes, cffi, …). Regarding the way Brian’s runtime code generation currently works with Cython, I invite you to run the code posted below. It will simulate a (very simplified and not very interesting) neuron model and record its activity over time. You can find its generated code in the directory that gets printed out. Hopefully that should give you a bit of an idea of what the generated code looks like at the moment, and how it links between Python and Cython/C++. Don’t hesitate to ask any questions you will most likely have about this.

from brian2 import *
from brian2.codegen.runtime.cython_rt.extension_manager import get_cython_cache_dir

try:
    clear_cache("cython")  # Just to make sure that we start with a clean slate
except Exception:
    pass  # there might be nothing to clear

prefs.codegen.target = "cython"
prefs.codegen.runtime.cython.delete_source_files = False

# A not-so-interesting example of a neuron simulation
neurons = NeuronGroup(10, "dv/dt = -v / (10*ms) : 1", method="exact")
neurons.v = "rand()"
state_monitor = StateMonitor(neurons, "v", record=True)

run(25*ms)

print("Generated code in:", get_cython_cache_dir())

# Just to show that the simulation worked
plt.plot(state_monitor.t/ms, state_monitor.v.T)
plt.show()

Please also see our general guidelines for GSoC applications here: GSoC 2024 | The Brian spiking neural network simulator

:postal_horn: Dear prospective candidates, please be aware that the submission deadline for application proposals is Apr 2nd. Unfortunately (well, not for me :wink: ), I will be away for a break over the Easter weekend and can therefore not give extensive feedback close to the deadline date. Please send me a draft before Wed 27th for detailed feedback. I will still be reachable after that date, but most likely not be able to look into your document in detail. Thanks for your understanding and happy proposal writing :blush: ! :postal_horn: