C & C++ Systems Literacy

GPU kernels, runtimes and Python extensions cross native boundaries. You need enough layout, lifetime and build fluency to make a safe bounded change and understand the resulting machine behavior.

Section 5 of 5 · 9 units · about 41 hours

0/9

By the end you can Build a tiny Python-callable C++ component, explain its layout and ownership, and repair one seeded lifetime or bounds error from sanitizer evidence in a clean build.

Units

Build it

Mini project · about 12 to 16 hours

Native run-duration histogram with a strict pybind11 binding and a sanitizer-backed repair

Move the run-duration histogram into C++ behind a strict Python boundary. Build it with CMake in debug, release and sanitizer trees, test it against NumPy, and follow one sanitizer report from evidence to regression test.

  1. 1Write the histogram core over spans with an owning Histogram class, and test copy independence, moves and bounds errors in a native test executable, using the AlgoMaster RAII and Copy-and-Swap Idiom exercises as reference.
  2. 2Seed an off-by-one that overflows the bins, capture the sanitizer report in a sanitizer build tree, repair it, connect the report to the frame with gdb, and keep the input as a regression test.
  3. 3Test the histogram against numpy.histogram on random values and on values at and beside every edge, then adopt NumPy's edge rule or document and test your own.
  4. 4Bind the core with pybind11 under dtype, shape, stride, alignment, ownership, exception and GIL contracts, and test every accepted and rejected input plus output lifetime.
  5. 5Build debug, release and sanitizer trees with CMake following the CMake Tutorial building and testing steps and register the native tests with CTest, then document the exported symbols and linked libraries and diagnose one link or loader failure from its message.
  6. 6Document the record layout from measured offsets, compare array-of-struct and struct-of-array scans with equal checksums, and split one call across threads with private bins merged after join, alongside a shared atomic-bin variant.
What you hand in
  • The native component: core library, owning Histogram, native tests, extension module and CMake trees.
  • The sanitizer write-up with the preserved report, the gdb session and the regression test.
  • Binding and oracle tests plus a boundary note with symbols, libraries, the diagnosed failure and the layout comparison.
How to check it
  • Self-grade with the rubric below
How it's graded

Oracle agreement

Pass: The binding agrees with numpy.histogram on random data and at every edge, or a documented edge rule is tested instead.

Great: Edge cases come from a generator, and a naive index version is kept as a comparison that fails.

Strict boundary

Pass: Wrong dtype, lists, strided, reversed and multi-dimensional inputs are rejected with the stated exception types, and the result outlives the input.

Great: The binding names the owner of every buffer and the GIL state at each point.

Sanitizer repair

Pass: The report is preserved and explained, and the regression fails on the seeded code and passes after the repair.

Great: The write-up names what the sanitizers cannot prove here, such as a wrong but in-bounds bin.

Build and ABI

Pass: Debug, release and sanitizer trees build from scratch, CTest catches a false check, and one failure is diagnosed from its message.

Great: The note says which upgrades force a rebuild of the extension and why.

Layout evidence

Pass: Predicted offsets match the measured ones, both layouts agree, and raw timings are kept.

Great: The report separates the measured difference from cache explanations it did not test.

Resources

  • AlgoMaster C++AlgoMaster

    Use the sections on compilation, pointers and references, memory, RAII and smart pointers, copy and move, STL containers, templates, errors, concurrency and paired exercises. Skip competitive-programming breadth already covered in Algorithms.

  • C++ Core GuidelinesStandard C++ Foundation community

    Use the Philosophy, Interfaces, Resource management, Expressions, Containers and concurrency rule groups to review ownership and safety decisions in the native component.

  • Clang AddressSanitizer and UndefinedBehaviorSanitizerLLVM

    Compile seeded memory defects with AddressSanitizer and UndefinedBehaviorSanitizer, preserve the reports, repair the root cause and keep regression tests.

  • pybind11 documentationpybind11 project

    Follow First steps and NumPy support to expose one small native function with explicit shape, dtype, stride, ownership, GIL and exception behavior.

  • cppreferencecppreference community

    Consult language lifetime, object model, value categories, memory model, atomics, containers and iterator invalidation while reading or modifying code.

  • CMake tutorialCMake

    Use the introductory steps for targets, libraries, testing and install rules. Produce one clean debug build and one optimized build without copied one-off commands.

  • Algorithmica - AoS and SoASergey Slotin

    Read the AoS and SoA experiment for a measured case where keeping each record together wins, the counterpart to a single-field scan.

  • `ld.so`: dynamic linker/loaderLinux man-pages project

    Read DESCRIPTION, Dynamic string tokens and LD_DEBUG to distinguish link-time symbols from runtime shared-library discovery on Linux.

  • Memory Layout TransformationsIntel

    Read Overview and Topics through AOS Form and SOA Form for field-wise access and locality trade-offs. Skip Intel array-notation syntax and the LIBOR example.

  • GDB manual: BacktracesGNU Project

    Use section 8.2 for frame numbers, bt, bt -full and optimized-out arguments when inspecting a native failure.