zc2 0.0.53

P2P compute broker with yen-denominated billing, WAL, and broker mesh support
//! Metal probe (macOS): MPSMatrixMultiplication GEMMs (fp32, fp16) and a blit copy.
use crate::bench_node::budget::Budget;
use crate::bench_node::fingerprint::GpuId;
use crate::bench_node::report::GpuMetrics;

/// `None` when there is no Metal device (always, off macOS).
#[cfg(not(target_os = "macos"))]
pub fn probe(_b: &Budget) -> Option<Result<GpuMetrics, String>> {
    None
}

/// Identity of the Metal device, without running anything on it.
#[cfg(not(target_os = "macos"))]
pub fn enumerate() -> Vec<GpuId> {
    vec![]
}

#[cfg(target_os = "macos")]
pub fn enumerate() -> Vec<GpuId> {
    metal::Device::system_default()
        .map(|dev| GpuId {
            vendor: "apple".into(),
            model: dev.name().to_string(),
            uuid: format!("metal-{}", dev.registry_id()),
            vram_bytes: dev.recommended_max_working_set_size(),
        })
        .into_iter()
        .collect()
}

#[cfg(target_os = "macos")]
pub fn probe(b: &Budget) -> Option<Result<GpuMetrics, String>> {
    let dev = metal::Device::system_default()?;
    Some(metal::objc::rc::autoreleasepool(|| run(&dev, b)))
}

#[cfg(target_os = "macos")]
const N: u64 = 4096;
#[cfg(target_os = "macos")]
const RUNS: usize = 5;
#[cfg(target_os = "macos")]
const COPY_BYTES: u64 = 1 << 30;

#[cfg(target_os = "macos")]
#[link(name = "MetalPerformanceShaders", kind = "framework")]
extern "C" {}

// MPSDataType: MPSDataTypeFloatBit (0x10000000) | bit width.
#[cfg(target_os = "macos")]
const MPS_FLOAT32: u32 = 0x1000_0000 | 32;
#[cfg(target_os = "macos")]
const MPS_FLOAT16: u32 = 0x1000_0000 | 16;

#[allow(unexpected_cfgs)] // objc 0.2's msg_send! tests a cfg the host crate doesn't declare
/// Times `RUNS` n×n `MPSMatrixMultiplication`s (after one warm-up) and returns TFLOPS.
/// Buffers are filled with a small constant (zeroed memory draws less power than real data).
#[cfg(target_os = "macos")]
fn mps_gemm(
    dev: &metal::DeviceRef,
    queue: &metal::CommandQueueRef,
    b: &Budget,
    dtype: u32,
    elem: u64,
) -> Result<f64, String> {
    use metal::foreign_types::ForeignTypeRef;
    use metal::objc::rc::StrongPtr;
    use metal::objc::runtime::{Class, Object, BOOL, NO};
    use metal::objc::{msg_send, sel, sel_impl};
    use std::time::Instant;

    if b.expired() {
        return Err("budget expired".into());
    }
    let class = |n: &str| Class::get(n).ok_or_else(|| format!("{n} unavailable"));
    let opts = metal::MTLResourceOptions::StorageModeShared;
    let bytes = N * N * elem;
    let bufs = [0, 1, 2].map(|_| dev.new_buffer(bytes, opts));
    // 0x3C00 is 1.0 in half; for float the same 16-bit pattern repeated is a tiny finite value.
    let fill = |buf: &metal::Buffer| unsafe {
        let p = buf.contents() as *mut u16;
        for i in 0..(bytes as usize / 2) {
            *p.add(i) = 0x3C00;
        }
    };
    bufs.iter().for_each(fill);

    unsafe {
        let desc_cls = class("MPSMatrixDescriptor")?;
        let mat_cls = class("MPSMatrix")?;
        let mul_cls = class("MPSMatrixMultiplication")?;
        let desc: *mut Object = msg_send![desc_cls,
            matrixDescriptorWithRows: N columns: N rowBytes: N * elem dataType: dtype];
        if desc.is_null() {
            return Err("MPSMatrixDescriptor creation failed".into());
        }
        let mats: Vec<StrongPtr> = bufs
            .iter()
            .map(|buf| {
                let m: *mut Object = msg_send![mat_cls, alloc];
                let m: *mut Object = msg_send![m, initWithBuffer: buf.as_ptr() descriptor: desc];
                StrongPtr::new(m)
            })
            .collect();
        if mats.iter().any(|m| m.is_null()) {
            return Err("MPSMatrix creation failed".into());
        }
        let mul: *mut Object = msg_send![mul_cls, alloc];
        let mul: *mut Object = msg_send![mul, initWithDevice: dev.as_ptr()
            transposeLeft: NO as BOOL transposeRight: NO as BOOL
            resultRows: N resultColumns: N interiorColumns: N
            alpha: 1.0f64 beta: 0.0f64];
        let mul = StrongPtr::new(mul);
        if mul.is_null() {
            return Err("MPSMatrixMultiplication creation failed".into());
        }
        let run = |count: usize| -> Result<f64, String> {
            let cb = queue.new_command_buffer();
            for _ in 0..count {
                let _: () = msg_send![*mul, encodeToCommandBuffer: cb.as_ptr()
                    leftMatrix: *mats[0] rightMatrix: *mats[1] resultMatrix: *mats[2]];
            }
            let t = Instant::now();
            cb.commit();
            cb.wait_until_completed();
            let secs = t.elapsed().as_secs_f64();
            match cb.status() {
                metal::MTLCommandBufferStatus::Completed => Ok(secs),
                st => Err(format!("command buffer ended with status {}", st as u32)),
            }
        };
        run(1)?; // warm-up
        let secs = run(RUNS)? / RUNS as f64;
        Ok(2.0 * (N as f64).powi(3) / secs / 1e12)
    }
}

#[cfg(target_os = "macos")]
fn run(dev: &metal::DeviceRef, b: &Budget) -> Result<GpuMetrics, String> {
    use std::time::Instant;
    let queue = dev.new_command_queue();
    let opts = metal::MTLResourceOptions::StorageModeShared;
    let tflops_f32 = mps_gemm(dev, &queue, b, MPS_FLOAT32, 4)?;
    let tflops_f16 = mps_gemm(dev, &queue, b, MPS_FLOAT16, 2)?;

    if b.expired() {
        return Err("budget expired".into());
    }
    let (src, dst) = (
        dev.new_buffer(COPY_BYTES, opts),
        dev.new_buffer(COPY_BYTES, opts),
    );
    // Copies `count` times; the first pass also faults in the fresh pages.
    let copy = |count: usize| -> Result<f64, String> {
        let cb = queue.new_command_buffer();
        for _ in 0..count {
            let enc = cb.new_blit_command_encoder();
            enc.copy_from_buffer(&src, 0, &dst, 0, COPY_BYTES);
            enc.end_encoding();
        }
        let t = Instant::now();
        cb.commit();
        cb.wait_until_completed();
        let secs = t.elapsed().as_secs_f64();
        match cb.status() {
            metal::MTLCommandBufferStatus::Completed => Ok(secs),
            st => Err(format!(
                "blit command buffer ended with status {}",
                st as u32
            )),
        }
    };
    copy(1)?; // warm-up
    let mem_bw_gbps = 2.0 * COPY_BYTES as f64 * RUNS as f64 / copy(RUNS)? / 1e9;

    Ok(GpuMetrics {
        vendor: "apple".into(),
        model: dev.name().to_string(),
        uuid: format!("metal-{}", dev.registry_id()),
        tflops_f32,
        tflops_f16,
        mem_bw_gbps,
        vram_bytes: dev.recommended_max_working_set_size(),
    })
}

#[cfg(all(test, target_os = "macos"))]
mod tests {
    use super::*;
    use std::sync::{atomic::AtomicBool, Arc};
    use std::time::Duration;

    #[test]
    fn apple_gpu_reports_positive_numbers() {
        let b = Budget::new(Duration::from_secs(30), Arc::new(AtomicBool::new(false)));
        let Some(r) = probe(&b) else { return };
        let g = r.unwrap();
        assert_eq!(g.vendor, "apple");
        assert!(g.uuid.starts_with("metal-"));
        assert!(
            g.tflops_f32 > 0.0 && g.tflops_f16 > 0.0 && g.mem_bw_gbps > 0.0 && g.vram_bytes > 0
        );
    }
}