C+
GPU

Metal compute

This example is the docs/examples/recipes/metal_compute recipe from the C+ source tree. It backs the GPU claim narrowly: C+ does not need a built-in GPU language to drive a real vendor GPU runtime. It can call Metal through ordinary FFI and package bindings.

This recipe is macOS-only and requires Apple's Metal toolchain.

Claim How this recipe checks it
C+ can link Apple frameworks Cplus.toml links Metal, Foundation, and -lobjc through cpc build
C+ can embed external artifacts #include_bytes embeds double.metallib; #include_str reads its byte count
Objective-C FFI is enough for GPU dispatch The host uses typed objc_msgSend declarations and MTLCreateSystemDefaultDevice
GPU readback is validated The kernel doubles 64 f32 values and returns nonzero if any output is wrong

What the kernel does

The Metal shader doubles each element in a [f32; 64] buffer. The workload is deliberately simple; the proof is the full host path:

device -> command queue -> library from embedded metallib -> function
-> compute pipeline -> buffers -> command buffer -> encoder -> dispatch
-> wait -> readback -> validate

The C+ host embeds the compiled shader bytes:

let shader_bytes = #include_bytes("../shaders/double.metallib");
let shader_ptr: *u8 = unsafe { shader_bytes as *u8 };
let shader_data: *u8 = ns_dispatch_data(shader_ptr, shader_len());

Then it dispatches the compute work and checks every result:

let mut bad: i32 = 0;
let mut j: usize = 0 as usize;
while j < n {
    let expected: f32 = (j as f32) * 2.0f32;
    let got: f32 = unsafe { outp[j] };
    if got != expected { bad = bad +% 1; }
    j = j +% (1 as usize);
}

if bad != 0 { return 3; }
return 0;

Reproduce

Prerequisite:

xcodebuild -downloadComponent MetalToolchain

From docs/examples/recipes/metal_compute in the C+ source tree:

./build.sh
./metal_compute

Expected result: exit code 0. The build script compiles shaders/double.metal into double.metallib, writes double.metallib.size, then runs cpc build with the framework link settings from the manifest.

This does not claim C+ has a device-side kernel language. The claim is more practical: C+ can be the host language for vendor GPU SDKs through FFI.


‹ Back to all examples