Chapter 23: GPU Offload (Metal / WebGPU) & Bare-Metal Portability
"Core algorithms stay pure C23; platform accelerators plug cleanly into an isolated boundary."
23.1 Architectural Isolation: Keeping OS & GPU Headers Out of Core
Embedding CUDA, Metal, or Vulkan headers directly into a core math header destroys cross-compilation. RCRE enforces a hard architectural boundary:
- include/ & dist/rcre.h: strictly ISO C23 standard headers (<stdint.h>, <stddef.h>, <stdbool.h>), compilable on bare metal.
- include/extras/rcre_gpu.h & src/extras/: host-specific GPU backends compiled only when targeting that platform (gpu_metal.m on macOS, gpu_stub.c on Linux/CI, web/wasm/rcre_webgpu.js in browsers).
Caller Matrix Op
|
+--> [ Small N*M < 4096 ] ---> rcre_hal.gemm_f32 (CPU NEON/AVX2, 0 dispatch latency)
|
+--> [ Large Compute Tile ] --> Apple Metal (MTLResourceStorageModeShared, unified RAM)
or Browser WebGPU (WGSL 16x16 workgroups)
23.2 Unified-Memory Metal Compute (gpu_metal.m) & WebGPU (rcre_webgpu.js)
On Apple Silicon (Apple M5 Max (Metal)), CPU and GPU share identical physical LPDDR/unified memory banks. Allocating MTLResourceStorageModeShared buffers passes pointers between C23 CPU arenas and the Metal Shading Language (MSL) 16 x 16 tiled SGEMM kernel without PCIe DMA staging copies. In browser runtimes, RCREWebGPU compiles an identical @workgroup_size(16, 16) WGSL shader reading Float32Array slices exported from rcre.wasm linear memory.
23.3 Freestanding Microcontrollers & Multi-Language ABI Packaging
Passing -DRCRE_FREESTANDING strips every dynamic heap allocation (malloc/free) from src/core/arena.c, pairing with rcre_arena_from_buffer() over static SRAM arrays. For polyglot ecosystems, RCRE ships explicit ABI version validation (rcre_abi_version() == 0x020000U), CMakeLists.txt, pkg-config (rcre.pc.in), Homebrew/vcpkg recipes, and thin zero-copy bindings across Python, Rust, Go (cgo), Zig (@cImport), and Julia (ccall).
23.4 Chapter Project: Zero-Heap Static-Buffer Arena & ABI Verification
Companion code in examples/ch23/main.c (make ch23) demonstrates a complete freestanding 64-byte aligned arena over static .bss storage with zero malloc calls verified under ASan and UBSan.