Skip to content

Commit 12f86a2

Browse files
authored
Add Isaac Morales blog (#1606)
* Add Isaac Morales blog * Improve blog with Lukas suggestions
1 parent c8b4a28 commit 12f86a2

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
project: CERN-HSF
3+
title: "Wrapping up GSoC'24: Improving performance of BioDynaMo using ROOT C++ Modules"
4+
layout: blog_post
5+
intro: "This project, part of Google Summer of Code 2024, aims to reduce the header parsing in BioDynaMo using the ROOT C++ Modules"
6+
author: Isaac Morales Santana
7+
photo: blog_authors/IsaacMorales.jpg
8+
logo: CERN-HSF-GSoC-logo.png
9+
date: 2024-10-17
10+
year: 2024
11+
tags: gsoc root cmake c++
12+
---
13+
14+
### Introduction
15+
16+
I am Isaac Morales, a Computer Engineering student at the University of Granada, Spain.
17+
This summer I had the opportunity to participate in Google Summer of Code 2024. My project
18+
revolved around enhancing BioDynaMo's performance using the ROOT C++ Modules.
19+
20+
**Mentors**: Vassil Vassilev, Lukas Breitwieser.
21+
22+
23+
### Project overview
24+
25+
BioDynaMo is an agent-based simulation platform designed to facilitate complex simulations,
26+
particularly in fields like cancer research, epidemiology, and social sciences. It leverages
27+
ROOT—a framework widely used in high-energy physics—for statistical analysis, random number
28+
generation, C++ Jupyter notebooks, and I/O operations. However, enhancing BioDynaMo’s performance
29+
remains a key challenge. This is where this Google Summer of Code 2024 (GSoC ‘24) project comes
30+
into play, focusing on optimizing the platform through ROOT C++ Modules.
31+
32+
### The Challenge: Performance Bottlenecks in BioDynaMo
33+
BioDynaMo’s reflection system, which utilizes Cling (an interactive C++ interpreter from ROOT),
34+
experiences significant runtime performance and memory usage issues. Specifically, parts of the system
35+
that rely on reflection information, such as simulation initialization, are particularly slow.
36+
At startup, the reflection subsystem has to be launched, which involves repeated parsing of library descriptors and class definitions [[1]](#1).
37+
This process consumes substantial memory and time, especially for smaller simulations with few agents,
38+
where more time is spent on initialization than on actual simulation steps.
39+
40+
41+
### The Solution: Integrating ROOT C++ Modules.
42+
The primary goal of the GSoC project was to integrate ROOT’s C++ Modules into BioDynaMo to minimize
43+
the performance overhead caused by Cling's repeated parsing of headers and descriptors. Without C++ modules,
44+
Cling must load dictionaries and parse large libraries repeatedly at every startup. This dictionary loading process
45+
introduces delays, as Cling resolves types and performs other initialization tasks.
46+
47+
48+
#### Cling Startup and Dictionary Loading Without C++ Modules
49+
When Cling initializes without C++ modules, it parses the header files and dictionaries of the necessary libraries from scratch.
50+
This parsing process involves reading the contents of each header file, resolving dependencies, and loading the types
51+
and methods defined in the libraries into Cling's reflection system. The "repeated parsing" refers to this process occurring
52+
every time the simulation starts, since there is no cached or precompiled version of the libraries' reflection data.
53+
Thus, each startup incurs a significant performance cost, especially for larger libraries.
54+
55+
#### How C++ Modules Solve These Challenges
56+
C++ Modules fundamentally change how code is parsed and loaded by introducing a precompiled module format that Cling can load directly.
57+
Instead of parsing all the header files at startup, the necessary library information is encapsulated in binary files (the modules).
58+
When the simulation starts, Cling reads the precompiled modules, which contain reflection information, class layouts,
59+
and other metadata required to run the simulation. This eliminates the need for repeated parsing and significantly reduces both memory consumption and startup time.
60+
As a result, the overall performance—particularly at startup—is vastly improved.
61+
62+
By "overall performance," I refer specifically to the reduction in both memory overhead and the time required to initialize a simulation.
63+
While the runtime of the simulation itself remains unaffected, the time it takes to get the simulation running is now much shorter,
64+
which is critical in scenarios where multiple small simulations are run.
65+
66+
### Promising Results
67+
68+
69+
### Benchmarking and Memory Improvements
70+
To assess the effectiveness of the changes, we conducted benchmark simulations
71+
72+
The updated `modules.idx` file, which is the Global Module Index, plays a critical role in these optimizations.
73+
It maintains a mapping of all available modules and ensures that Cling can efficiently load the correct modules at startup. We can add BioDynaMo modules to this index using ```root.exe -l -b -q``` after the module generation.
74+
In the updated version, improvements include faster module lookups and better memory management during the startup phase.
75+
76+
This further contributes to the reduction in initialization time and memory overhead.
77+
The results have been promising, showcasing significant performance gains. Benchmarking revealed
78+
improvements ranging from 18% reduction in peak memory usage with the default modules.idx to 25% with the
79+
updated one.
80+
![Plot of the peak memory usage in various demos](https://compiler-research.org/images/blog/bdm-peak-memory.png)
81+
82+
83+
Moreover, the startup phase saw an impressive 80% reduction in time, thanks to the optimized
84+
handling of header parsing. That highlights the efficiency of C++ Modules in minimizing
85+
Cling’s parsing overhead
86+
![Plot of the startup time in various demos](https://compiler-research.org/images/blog/bdm-startup-time.png)
87+
88+
89+
### Impact on Simulation Runtime
90+
It's important to note that these changes specifically target the startup phase of the simulation.
91+
No significant change in simulation runtime is expected, as the improvements are focused on reducing the overhead associated with loading and parsing libraries at startup.
92+
Once the simulation is running, the computational tasks remain unchanged, meaning the performance benefits primarily affect the time it takes to begin the simulation, not the duration of the simulation itself.
93+
94+
## What I did?:
95+
1. **Reworking CMake Rules:** The project incorporated ROOT and another packages
96+
efficiently using FetchContent, modifying CMake rules accordingly (e.g., PR [#365](https://github.com/BioDynaMo/biodynamo/pull/365)
97+
and [#387](https://github.com/BioDynaMo/biodynamo/pull/387), both merged)
98+
2. **Replacing genreflex with rootcling:** This switch was crucial to enable C++ Modules and
99+
streamline the generation of reflection information (PR [#379](https://github.com/BioDynaMo/biodynamo/pull/379))
100+
3. **C++ Modules changes** Among other things, I used automatic generation for the module map with relative paths,
101+
modified the `selection.xml` file to support the new dictionaries and fixed headers with missing includes (PR [#385](https://github.com/BioDynaMo/biodynamo/pull/385).
102+
4. **Updated some CI workflows** I fixed some failing workflows in PR [#377](https://github.com/BioDynaMo/biodynamo/pull/377).
103+
Also, I did some minor changes in some flags in PR's [#378](https://github.com/BioDynaMo/biodynamo/pull/378) and [#367](https://github.com/BioDynaMo/biodynamo/pull/367)
104+
105+
106+
### Future Steps and Challenges Ahead
107+
Despite these advances, several challenges remain. PR's #365 is ready to merge and #385 needs some changes. For instance, memory leaks have been observed when using the new
108+
`ROOT_GENERATE_DICTIONARY`, even with C++ Modules disabled. Additionally, the build system for individual demos has
109+
caused compatibility issues with the main build system. Also, there is a problem with the Jupyter notebooks.
110+
Resolving these issues and finalizing the integration of C++ Modules will be essential for ensuring long-term stability and reliability.
111+
112+
Looking ahead, further optimizations are planned, including potential module-based optimizations for BioDynaMo’s
113+
core components. At this time, only one big module is generated (biodynamo.pcm). Our goal is to split this module into smaller ones to load them on-demand,
114+
improving even more the peak memory usage reduction. Collaboration with the BioDynaMo team continues, with upcoming meetings scheduled
115+
to align efforts and resolve outstanding issues.
116+
117+
### Conclusion
118+
The integration of ROOT C++ Modules into BioDynaMo represents a significant step forward in improving the platform's performance, particularly in terms of startup time and memory consumption.
119+
By addressing the root cause of inefficiencies in the reflection system, we have achieved a more streamlined and efficient simulation initialization process.
120+
Future work could focus on further optimizations, but the foundation laid by this project promises a more scalable and efficient BioDynaMo platform.
121+
122+
123+
### Related Links
124+
125+
- [ROOT website](https://root.cern)
126+
- [BioDynaMo website](https://www.biodynamo.org/)
127+
- [My GitHub Profile](https://github.com/imorlxs)
128+
129+
### References
130+
- <a id="1">[1]</a>
131+
[Calibration of stochastic, agent-based neuron growth models with approximate Bayesian computation](https://doi.org/10.1007/s00285-024-02144-2)
132+
- [BioDynaMo: a modular platform for high-performance agent-based simulation](https://doi.org/10.1093/bioinformatics/btab649)
133+
- [High-Performance and Scalable Agent-Based Simulation with BioDynaMo](https://doi.org/10.1145/3572848.3577480)
134+
135+

images/blog_authors/IsaacMorales.jpg

72.1 KB
Loading

0 commit comments

Comments
 (0)