cgpu 0.1.0

A tunable GPU compute executor with automatic CPU fallback, byte-based batching, and inline shader generation.
Documentation
use std::time::Instant;

use crate::batch::{pack_by_bytes, GpuBatchPlan, WorkItem};
use crate::budget::GpuMemoryBudget;
use crate::error::GpuError;
use crate::telemetry::GpuTelemetry;

#[derive(Clone, Debug)]
pub struct BatchPlan {
    pub plan: GpuBatchPlan,
    pub telemetry: GpuTelemetry,
}

impl BatchPlan {
    pub fn spans(&self) -> &[crate::batch::BatchSpan] {
        self.plan.spans()
    }

    pub fn into_spans(self) -> Vec<crate::batch::BatchSpan> {
        self.plan.into_spans()
    }

    pub fn pack_ms(&self) -> f64 {
        self.telemetry.pack_ms
    }
}

pub fn plan_batches<T: WorkItem>(
    items: &[T],
    budget: GpuMemoryBudget,
) -> Result<BatchPlan, GpuError> {
    let mut telemetry = GpuTelemetry::default();
    let plan = pack_by_bytes(items, budget, &mut telemetry)?;
    Ok(BatchPlan { plan, telemetry })
}

pub fn elapsed_ms(started: Instant) -> f64 {
    started.elapsed().as_secs_f64() * 1000.0
}