r/cpp • u/Important-Trash-4868 • Mar 15 '26
I built a C++20 zero-copy graph engine to stream 50GB PyTorch datasets using mmap and nanobind.
Hi r/cpp,
Iβm an undergrad CS student and I recently open-sourced GraphZero (v0.2). It's a zero-copy data engine designed to stop PyTorch from crashing out of memory when training massive Graph Neural Networks.
I wanted to share the architecture here because getting a C++20 extension compiling across Windows, Linux, and macOS in CI/CD was an absolute trial by fire.
The Architecture: To bypass Python's memory overhead, the engine compiles raw datasets into a custom binary format. It then uses POSIX mmap (and Windows equivalents) to map the files directly from the SSD. Using nanobind, I take the raw C++ pointers and expose them directly to PyTorch as zero-copy NumPy arrays. The OS handles all the data streaming via Page Faults while PyTorch trains the model.
Under the hood:
- Template Dispatching: Used heavily for the feature store to enforce
FLOAT32andINT64memory layouts natively. - Concurrency: Used OpenMP to multi-thread the graph traversal and neighbor sampling, releasing the Python GIL so the C++ side can saturate the SSD bandwidth.
- The Apple Clang Trap: I used C++17's
std::from_charsto parse CSVs without heap allocations. It worked perfectly on GCC and MSVC, but I discovered the hard way that Apple'slibc++still hasn't implementedfrom_charsfor floating-point numbers, forcing me to write a compile-time fallback macro just to get the macOS runner to pass.
If anyone here has experience with high-performance C++ Python extensions, I would absolutely love a code review. Specifically, I'm looking for critiques on:
- The template dispatching implementation.
- How I handled the memory mapping abstraction.
GitHub Repo: repo
15
u/Jannik2099 Mar 15 '26
One issue with memory-mapped IO is that it's still a blocking operation. You are probably doing IO while holding the GIL?
I'm not sure if async IO into buffers wouldn't be better
39
u/LongestNamesPossible Mar 15 '26
AI project from a name that is 5 years old but just started posting 10 hours ago.
4
u/c_plus_plus Mar 16 '26
On linux you can also close the file after it has been mapped (just line on Windows), so you don't need to keep the
fdaround open.NumPy can also do mmap natively, see the "mmap_mode" argument to
numpy.load.24
u/NanoNett Mar 16 '26
Lmfao, as a sparse-graph lover, the first thing I noticed was "Compressed CSR" in your README - definitely AI slop. Would love to hear about your Compressed Compressed Sparse Row format tho, super neat bud
1
u/[deleted] Mar 16 '26
[removed] β view removed comment